Search in sources :

Example 6 with SQLImpl

use of lucee.runtime.db.SQLImpl in project Lucee by lucee.

the class Ansi92 method clean.

@Override
public void clean(Config config, DatasourceConnection dc, int type, StorageScopeEngine engine, DatasourceStorageScopeCleaner cleaner, StorageScopeListener listener, Log log) throws PageException {
    String strType = VariableInterpreter.scopeInt2String(type);
    // select
    SQL sqlSelect = new SQLImpl("select cfid,name from " + PREFIX + "_" + strType + "_data where expires<=?", new SQLItem[] { new SQLItemImpl(System.currentTimeMillis(), Types.VARCHAR) });
    Query query;
    try {
        query = new QueryImpl(ThreadLocalPageContext.get(), dc, sqlSelect, -1, -1, null, "query");
    } catch (Throwable t) {
        ExceptionUtil.rethrowIfNecessary(t);
        // possible that the table not exist, if not there is nothing to clean
        return;
    }
    int recordcount = query.getRecordcount();
    String cfid, name;
    for (int row = 1; row <= recordcount; row++) {
        cfid = Caster.toString(query.getAt(KeyConstants._cfid, row, null), null);
        name = Caster.toString(query.getAt(KeyConstants._name, row, null), null);
        if (listener != null)
            listener.doEnd(engine, cleaner, name, cfid);
        ScopeContext.info(log, "remove " + strType + "/" + name + "/" + cfid + " from datasource " + dc.getDatasource().getName());
        engine.remove(type, name, cfid);
        SQLImpl sql = new SQLImpl("delete from " + StorageScopeDatasource.PREFIX + "_" + strType + "_data where cfid=? and name=?", new SQLItem[] { new SQLItemImpl(cfid, Types.VARCHAR), new SQLItemImpl(name, Types.VARCHAR) });
        new QueryImpl(ThreadLocalPageContext.get(), dc, sql, -1, -1, null, "query");
    }
}
Also used : SQLImpl(lucee.runtime.db.SQLImpl) QueryImpl(lucee.runtime.type.QueryImpl) Query(lucee.runtime.type.Query) SQLItemImpl(lucee.runtime.db.SQLItemImpl) SQL(lucee.runtime.db.SQL)

Example 7 with SQLImpl

use of lucee.runtime.db.SQLImpl 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 8 with SQLImpl

use of lucee.runtime.db.SQLImpl 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)

Aggregations

SQLImpl (lucee.runtime.db.SQLImpl)8 SQLItemImpl (lucee.runtime.db.SQLItemImpl)7 DatabaseException (lucee.runtime.exp.DatabaseException)4 SQL (lucee.runtime.db.SQL)3 QueryImpl (lucee.runtime.type.QueryImpl)3 ArrayList (java.util.ArrayList)2 Log (lucee.commons.io.log.Log)2 CacheHandler (lucee.runtime.cache.tag.CacheHandler)2 CacheHandlerPro (lucee.runtime.cache.tag.CacheHandlerPro)2 CacheItem (lucee.runtime.cache.tag.CacheItem)2 ConfigImpl (lucee.runtime.config.ConfigImpl)2 SQLItem (lucee.runtime.db.SQLItem)2 ApplicationException (lucee.runtime.exp.ApplicationException)2 PageException (lucee.runtime.exp.PageException)2 Key (lucee.runtime.type.Collection.Key)2 Query (lucee.runtime.type.Query)2 Struct (lucee.runtime.type.Struct)2 StructImpl (lucee.runtime.type.StructImpl)2 CallableStatement (java.sql.CallableStatement)1 ResultSet (java.sql.ResultSet)1