Search in sources :

Example 1 with Form

use of lucee.runtime.type.scope.Form in project Lucee by lucee.

the class Update method createSQL.

/**
 * @param keys primary Keys
 * @return return SQL String for update
 * @throws PageException
 */
private SQL createSQL(DatasourceConnection dc, String[] keys, 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 set = new StringBuffer();
    StringBuffer where = new StringBuffer();
    ArrayList setItems = new ArrayList();
    ArrayList whereItems = 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 (ArrayUtil.indexOfIgnoreCase(keys, field) == -1) {
                if (set.length() == 0)
                    set.append(" set ");
                else
                    set.append(",");
                set.append(field);
                set.append("=?");
                ColumnInfo ci = (ColumnInfo) meta.get(field);
                if (ci != null)
                    setItems.add(new SQLItemImpl(form.get(field, null), ci.getType()));
                else
                    setItems.add(new SQLItemImpl(form.get(field, null)));
            } else {
                if (where.length() == 0)
                    where.append(" where ");
                else
                    where.append(" and ");
                where.append(field);
                where.append("=?");
                whereItems.add(new SQLItemImpl(form.get(field, null)));
            }
        }
    }
    if ((setItems.size() + whereItems.size()) == 0)
        return null;
    if (whereItems.size() == 0)
        throw new DatabaseException("can't find primary keys [" + ListUtil.arrayToList(keys, ",") + "] of table [" + tablename + "] in form scope", null, null, dc);
    StringBuffer sql = new StringBuffer();
    sql.append("update ");
    if (tablequalifier != null && tablequalifier.length() > 0) {
        sql.append(tablequalifier);
        sql.append('.');
    }
    if (tableowner != null && tableowner.length() > 0) {
        sql.append(tableowner);
        sql.append('.');
    }
    sql.append(tablename);
    sql.append(set);
    sql.append(where);
    return new SQLImpl(sql.toString(), arrayMerge(setItems, whereItems));
}
Also used : SQLImpl(lucee.runtime.db.SQLImpl) Form(lucee.runtime.type.scope.Form) ArrayList(java.util.ArrayList) SQLItemImpl(lucee.runtime.db.SQLItemImpl) DatabaseException(lucee.runtime.exp.DatabaseException)

Example 2 with Form

use of lucee.runtime.type.scope.Form in project Lucee by lucee.

the class InternalRequest method fillForm.

private static void fillForm(PageContextImpl _pc, Struct src) throws PageException {
    if (src == null)
        return;
    Iterator<Entry<Key, Object>> it = src.entryIterator();
    Form trg = _pc.formScope();
    Entry<Key, Object> e;
    while (it.hasNext()) {
        e = it.next();
        trg.set(e.getKey(), e.getValue());
    }
}
Also used : Entry(java.util.Map.Entry) Form(lucee.runtime.type.scope.Form) Key(lucee.runtime.type.Collection.Key)

Example 3 with Form

use of lucee.runtime.type.scope.Form in project Lucee by lucee.

the class FileTag method getFormItem.

/**
 * rreturn fileItem matching to filefiled definition or throw a exception
 * @return FileItem
 * @throws ApplicationException
 */
private static FormItem getFormItem(PageContext pageContext, String filefield) throws PageException {
    // check filefield
    if (StringUtil.isEmpty(filefield)) {
        FormItem[] items = getFormItems(pageContext);
        if (ArrayUtil.isEmpty(items))
            throw new ApplicationException("no file send with this form");
        return items[0];
    }
    PageException pe = pageContext.formScope().getInitException();
    if (pe != null)
        throw pe;
    lucee.runtime.type.scope.Form upload = pageContext.formScope();
    FormItem fileItem = upload.getUploadResource(filefield);
    if (fileItem == null) {
        FormItem[] items = upload.getFileItems();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < items.length; i++) {
            if (i != 0)
                sb.append(", ");
            sb.append(items[i].getFieldName());
        }
        String add = ".";
        if (sb.length() > 0)
            add = ", valid field names are [" + sb + "].";
        if (pageContext.formScope().get(filefield, null) == null)
            throw new ApplicationException("form field [" + filefield + "] is not a file field" + add);
        throw new ApplicationException("form field [" + filefield + "] doesn't exist or has no content" + add);
    }
    return fileItem;
}
Also used : PageException(lucee.runtime.exp.PageException) ApplicationException(lucee.runtime.exp.ApplicationException) Form(lucee.runtime.type.scope.Form) FormItem(lucee.runtime.type.scope.FormItem)

Example 4 with Form

use of lucee.runtime.type.scope.Form in project Lucee by lucee.

the class FileTag method getFormItems.

private static FormItem[] getFormItems(PageContext pageContext) throws PageException {
    PageException pe = pageContext.formScope().getInitException();
    if (pe != null)
        throw pe;
    Form scope = pageContext.formScope();
    return scope.getFileItems();
}
Also used : PageException(lucee.runtime.exp.PageException) Form(lucee.runtime.type.scope.Form)

Example 5 with Form

use of lucee.runtime.type.scope.Form 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)

Aggregations

Form (lucee.runtime.type.scope.Form)5 ArrayList (java.util.ArrayList)2 SQLImpl (lucee.runtime.db.SQLImpl)2 SQLItemImpl (lucee.runtime.db.SQLItemImpl)2 PageException (lucee.runtime.exp.PageException)2 Entry (java.util.Map.Entry)1 SQLItem (lucee.runtime.db.SQLItem)1 ApplicationException (lucee.runtime.exp.ApplicationException)1 DatabaseException (lucee.runtime.exp.DatabaseException)1 Key (lucee.runtime.type.Collection.Key)1 FormItem (lucee.runtime.type.scope.FormItem)1