@Override @SuppressWarnings("unchecked") protected Object transformToSource(Object o) { List<Map<String, Object>> valueList = new ArrayList<>(); if (o instanceof Map) { ((Map<String, List<String>>) o).forEach((key, value) -> valueList.add(Context.create() .set(KEY, key) .set(VALUE, value))); } return valueList; } }
/** * Adds a parameter. * * @param parameter the name of the parameter as referenced in the SQL statement (${name} or #{name}). * @param value the value of the parameter * @return the query itself to support fluent calls */ public SQLQuery set(String parameter, Object value) { params.put(parameter, value); return this; }
@Override protected void writeBlobToParameter(String name, Blob blob) throws SQLException { OutputStream out = (OutputStream) params.get(name); if (out == null) { return; } try (InputStream in = blob.getBinaryStream()) { ByteStreams.copy(in, out); } catch (IOException e) { throw new SQLException(e); } }
/** * Boilerplate method to use {@link #insertRow(String, sirius.kernel.commons.Context)} with plan maps. * * @param table the target table to insert a row * @param row contains names and values to insert into the database * @return a Row containing all generated keys * @throws SQLException in case of a database error */ public Row insertRow(String table, Map<String, Object> row) throws SQLException { Context context = Context.create(); context.putAll(row); return insertRow(table, context); }
/** * Returns all values defined in this extension as {@link Context}. * * @return a context containing all values defined by this extension or the <tt>default</tt> extension. */ @Nonnull public Context getContext() { Context ctx = Context.create(); for (Map.Entry<String, ConfigValue> entry : config.entrySet()) { ctx.put(entry.getKey(), get(entry.getKey()).get()); } return ctx; }
@Override public Object put(String key, Object value) { set(key, value); return value; }
/** * Puts all name-value-pairs stored in the given map. * * @param data the name value pairs to be put into the context * @return <tt>this</tt> to permit fluent method calls */ public Context setAll(Map<String, Object> data) { putAll(data); return this; }
/** * Writes all parameters into the given {@link javax.script.ScriptContext} * * @param ctx the context to be filled with the internally stored name value pairs */ public void applyTo(ScriptContext ctx) { for (Entry<String, Object> e : entrySet()) { ctx.setAttribute(e.getKey(), e.getValue(), ScriptContext.ENGINE_SCOPE); } }
/** * Creates a new context * * @return a newly created and empty context */ public static Context create() { return new Context(); }
/** * Sets the given value (its string representation), mit limits this to <tt>limit</tt> characters. * * @param key the key to which the value should be associated * @param value the value which string representation should be put into the context * @param limit the maximal number of characters to put into the map. Everything after that will be discarded */ public void putLimited(String key, Object value, int limit) { set(key, Strings.limit(value, limit)); }
/** * Sets all parameters of the given context. * * @param ctx the containing pairs of names and values to add to the query * @return the query itself to support fluent calls */ public SQLQuery set(Map<String, Object> ctx) { params.putAll(ctx); return this; }
/** * Sents the given context as POST to the designated server. * * @param params the data to POST * @param charset the charset to use when encoding the post data * @return the outcall itself for fluent method calls * @throws IOException in case of any IO error */ public Outcall postData(Context params, Charset charset) throws IOException { connection.setRequestMethod(REQUEST_METHOD_POST); connection.setRequestProperty(HEADER_CONTENT_TYPE, CONTENT_TYPE_FORM_URLENCODED); this.charset = charset; OutputStreamWriter writer = new OutputStreamWriter(getOutput(), charset.name()); StringBuilder sb = new StringBuilder(); boolean first = true; for (Map.Entry<String, Object> entry : params.entrySet()) { if (!first) { sb.append("&"); } first = false; sb.append(URLEncoder.encode(entry.getKey(), charset.name())); sb.append("="); sb.append(URLEncoder.encode(NLS.toMachineString(entry.getValue()), charset.name())); } writer.write(sb.toString()); writer.flush(); return this; }
@Override @SuppressWarnings("unchecked") protected Object transformToSource(Object o) { List<Map<String, Object>> valueList = new ArrayList<>(); if (o instanceof Map) { ((Map<String, String>) o).forEach((key, value) -> valueList.add(Context.create() .set(KEY, key) .set(VALUE, value))); } return valueList; }
private Script computeUpdateScript(Entity parent, boolean updateParent) { sirius.kernel.commons.Context ctx = sirius.kernel.commons.Context.create(); StringBuilder sb = new StringBuilder(); for (Reference ref : references) { sb.append("ctx._source."); sb.append(ref.getLocalProperty().getName()); sb.append("=params."); sb.append(ref.getLocalProperty().getName()); sb.append(";"); ctx.put(ref.getLocalProperty().getName(), parent == null ? null : ref.getRemoteProperty().writeToSource(parent)); } if (updateParent) { sb.append("ctx._source."); sb.append(getName()); sb.append("=params."); sb.append(getName()); sb.append(";"); ctx.put(getName(), parent != null ? parent.getId() : null); } return new Script(ScriptType.INLINE, LANGUAGE_PAINLESS, sb.toString(), ctx); }
/** * Provides the value associated with the given key as {@link Value} * * @param key the key for which the value should ne returned * @return a value wrapping the internally associated value for the given key */ @Nonnull public Value getValue(Object key) { return Value.of(get(key)); }
ctx.putAll(ext.getContext()); this.name = name; this.driver = ext.get(KEY_DRIVER).isEmptyString() ?
protected void prepareValues(Context ctx, StringBuilder fields, StringBuilder values, List<Object> valueList) { for (Map.Entry<String, Object> entry : ctx.entrySet()) { if (entry.getValue() != null) { if (fields.length() > 0) { fields.append(", "); values.append(", "); } if (!SANE_COLUMN_NAME.matcher(entry.getKey()).matches()) { throw Exceptions.handle() .to(Databases.LOG) .withSystemErrorMessage("Cannot use '%s' as column name for an insert. " + "Only characters, digits and '_' is allowed!", entry.getKey()) .handle(); } fields.append(entry.getKey()); values.append("?"); valueList.add(Databases.convertValue(entry.getValue())); } } }
@Override protected void createEntity(SQLEntity entity, EntityDescriptor ed) throws Exception { Context insertData = Context.create(); for (Property p : ed.getProperties()) { if (!SQLEntity.ID.getName().equals(p.getName())) { insertData.set(p.getPropertyName(), p.getValueForDatasource(OMA.class, entity)); } } if (ed.isVersioned()) { insertData.set(VERSION, 1); } Row keys = getDatabase(ed.getRealm()).insertRow(ed.getRelationName(), insertData); loadCreatedId(entity, keys); entity.setVersion(1); }
private Object computeEffectiveParameterValue(String fullParameterName) throws SQLException { String accessPath = null; String parameterName = fullParameterName; if (fullParameterName.contains(".")) { accessPath = parameterName.substring(parameterName.indexOf('.') + 1); parameterName = parameterName.substring(0, parameterName.indexOf('.')); } Object paramValue = context.get(parameterName); if (accessPath == null || paramValue == null) { return paramValue; } try { return Reflection.evalAccessPath(accessPath, paramValue); } catch (Exception e) { throw new SQLException(NLS.fmtr("StatementCompiler.cannotEvalAccessPath") .set("name", parameterName) .set("path", accessPath) .set("value", paramValue) .set("query", originalSQL) .format(), e); } }