use of lucee.runtime.db.SQLItemImpl in project Lucee by lucee.
the class ColumnInfo method createSQL.
/**
* @param meta
* @return return SQL String for insert
* @throws PageException
*/
private SQL createSQL(Struct meta) throws PageException {
String[] fields = null;
Form form = pageContext.formScope();
if (formfields != null)
fields = ListUtil.toStringArray(ListUtil.listToArrayRemoveEmpty(formfields, ','));
else
fields = CollectionUtil.keysAsString(pageContext.formScope());
StringBuffer names = new StringBuffer();
StringBuffer values = new StringBuffer();
ArrayList items = new ArrayList();
String field;
for (int i = 0; i < fields.length; i++) {
field = StringUtil.trim(fields[i], null);
if (StringUtil.startsWithIgnoreCase(field, "form."))
field = field.substring(5);
if (!field.equalsIgnoreCase("fieldnames")) {
if (names.length() > 0) {
names.append(',');
values.append(',');
}
names.append(field);
values.append('?');
ColumnInfo ci = (ColumnInfo) meta.get(field, null);
if (ci != null)
items.add(new SQLItemImpl(form.get(field, null), ci.getType()));
else
items.add(new SQLItemImpl(form.get(field, null)));
}
}
if (items.size() == 0)
return null;
StringBuffer sql = new StringBuffer();
sql.append("insert into ");
if (tablequalifier.length() > 0) {
sql.append(tablequalifier);
sql.append('.');
}
if (tableowner.length() > 0) {
sql.append(tableowner);
sql.append('.');
}
sql.append(tablename);
sql.append('(');
sql.append(names);
sql.append(")values(");
sql.append(values);
sql.append(")");
return new SQLImpl(sql.toString(), (SQLItem[]) items.toArray(new SQLItem[items.size()]));
}
use of lucee.runtime.db.SQLItemImpl in project Lucee by lucee.
the class QueryParam method release.
@Override
public void release() {
separator = ",";
list = false;
maxlength = -1;
item = new SQLItemImpl();
}
use of lucee.runtime.db.SQLItemImpl in project Lucee by lucee.
the class Ansi92 method select.
@Override
public Query select(Config config, String cfid, String applicationName, DatasourceConnection dc, int type, Log log, boolean createTableIfNotExist) throws PageException {
String strType = VariableInterpreter.scopeInt2String(type);
Query query = null;
SQL sqlSelect = new SQLImpl("select data from " + PREFIX + "_" + strType + "_data where cfid=? and name=? and expires > ?", new SQLItem[] { new SQLItemImpl(cfid, Types.VARCHAR), new SQLItemImpl(applicationName, Types.VARCHAR), new SQLItemImpl(now(config), Types.VARCHAR) });
PageContext pc = ThreadLocalPageContext.get();
try {
query = new QueryImpl(pc, dc, sqlSelect, -1, -1, null, "query");
} catch (DatabaseException de) {
if (dc == null || !createTableIfNotExist)
throw de;
// table does not exist???
try {
SQL sql = createSQL(dc, DataSourceUtil.isMySQL(dc) ? "longtext" : "ntext", strType);
ScopeContext.info(log, sql.toString());
new QueryImpl(pc, dc, sql, -1, -1, null, "query");
} catch (DatabaseException _de) {
// don't like "ntext", try text
try {
SQL sql = createSQL(dc, "text", strType);
ScopeContext.info(log, sql.toString());
new QueryImpl(pc, dc, sql, -1, -1, null, "query");
} catch (DatabaseException __de) {
// don't like text, try "memo"
try {
SQL sql = createSQL(dc, "memo", strType);
ScopeContext.info(log, sql.toString());
new QueryImpl(pc, dc, sql, -1, -1, null, "query");
} catch (DatabaseException ___de) {
// don't like "memo", try clob
try {
SQL sql = createSQL(dc, "clob", strType);
ScopeContext.info(log, sql.toString());
new QueryImpl(pc, dc, sql, -1, -1, null, "query");
} catch (DatabaseException ____de) {
___de.initCause(__de);
__de.initCause(_de);
_de.initCause(de);
// we could not create the table, so there seem to be an other ecception we cannot solve
DatabaseException exp = new DatabaseException("Unable to select from your client storage database, and was also unable to create the tables. Here's the exceptions we encountered.", null, null, dc);
exp.initCause(de);
throw exp;
}
}
}
}
query = new QueryImpl(pc, dc, sqlSelect, -1, -1, null, "query");
}
ScopeContext.info(log, sqlSelect.toString());
return query;
}
use of lucee.runtime.db.SQLItemImpl in project Lucee by lucee.
the class QueryParamConverter method convert.
public static SQL convert(String sql, Array params) throws PageException {
Iterator<Object> it = params.valueIterator();
List<SQLItems<NamedSQLItem>> namedItems = new ArrayList<SQLItems<NamedSQLItem>>();
List<SQLItems<SQLItem>> items = new ArrayList<SQLItems<SQLItem>>();
Object value, paramValue;
while (it.hasNext()) {
value = it.next();
if (Decision.isStruct(value)) {
Struct sct = (Struct) value;
// name (optional)
String name = null;
Object oName = sct.get(KeyConstants._name, null);
if (oName != null)
name = Caster.toString(oName);
// value (required)
paramValue = sct.get(KeyConstants._value);
if (StringUtil.isEmpty(name)) {
items.add(new SQLItems<SQLItem>(new SQLItemImpl(paramValue, Types.VARCHAR), sct));
} else {
namedItems.add(new SQLItems<NamedSQLItem>(new NamedSQLItem(name, paramValue, Types.VARCHAR), sct));
}
} else {
items.add(new SQLItems<SQLItem>(new SQLItemImpl(value)));
}
}
return convert(sql, items, namedItems);
}
Aggregations