Search in sources :

Example 6 with SQLItemImpl

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()]));
}
Also used : SQLImpl(lucee.runtime.db.SQLImpl) Form(lucee.runtime.type.scope.Form) ArrayList(java.util.ArrayList) SQLItemImpl(lucee.runtime.db.SQLItemImpl) SQLItem(lucee.runtime.db.SQLItem)

Example 7 with SQLItemImpl

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();
}
Also used : SQLItemImpl(lucee.runtime.db.SQLItemImpl)

Example 8 with 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;
}
Also used : SQLImpl(lucee.runtime.db.SQLImpl) QueryImpl(lucee.runtime.type.QueryImpl) Query(lucee.runtime.type.Query) SQLItemImpl(lucee.runtime.db.SQLItemImpl) ThreadLocalPageContext(lucee.runtime.engine.ThreadLocalPageContext) PageContext(lucee.runtime.PageContext) DatabaseException(lucee.runtime.exp.DatabaseException) SQL(lucee.runtime.db.SQL)

Example 9 with SQLItemImpl

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);
}
Also used : ArrayList(java.util.ArrayList) SQLItem(lucee.runtime.db.SQLItem) Struct(lucee.runtime.type.Struct) SQLItemImpl(lucee.runtime.db.SQLItemImpl)

Aggregations

SQLItemImpl (lucee.runtime.db.SQLItemImpl)9 SQLImpl (lucee.runtime.db.SQLImpl)7 ArrayList (java.util.ArrayList)3 DatabaseException (lucee.runtime.exp.DatabaseException)3 QueryImpl (lucee.runtime.type.QueryImpl)3 SQL (lucee.runtime.db.SQL)2 SQLItem (lucee.runtime.db.SQLItem)2 Query (lucee.runtime.type.Query)2 Struct (lucee.runtime.type.Struct)2 Form (lucee.runtime.type.scope.Form)2 CallableStatement (java.sql.CallableStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Iterator (java.util.Iterator)1 Entry (java.util.Map.Entry)1 Log (lucee.commons.io.log.Log)1 PageContext (lucee.runtime.PageContext)1 CacheHandler (lucee.runtime.cache.tag.CacheHandler)1 CacheHandlerPro (lucee.runtime.cache.tag.CacheHandlerPro)1 CacheItem (lucee.runtime.cache.tag.CacheItem)1