Search in sources :

Example 6 with IVisitor

use of com.servoy.j2db.util.visitor.IVisitor in project servoy-client by Servoy.

the class JSDatabaseManager method validateQueryArguments.

private boolean validateQueryArguments(ISQLQuery select) {
    if (select != null) {
        if (select instanceof QuerySelect && ((QuerySelect) select).getColumns() == null) {
            application.reportJSError("Custom query: " + select + " not executed because no columns are specified to be selected", null);
            return false;
        }
        final List<Placeholder> placeHolders = new ArrayList<Placeholder>();
        AbstractBaseQuery.acceptVisitor(select, new IVisitor() {

            public Object visit(Object o) {
                if (o instanceof Placeholder) {
                    placeHolders.add((Placeholder) o);
                }
                return o;
            }
        });
        for (Placeholder placeholder : placeHolders) {
            if (!placeholder.isSet()) {
                application.reportJSError(// $NON-NLS-1$
                "Custom query: " + select + " not executed because not all arguments have been set: " + placeholder.getKey(), // $NON-NLS-1$
                null);
                return false;
            }
            Object value = placeholder.getValue();
            if (value instanceof DbIdentValue && ((DbIdentValue) value).getPkValue() == null) {
                application.reportJSError(// $NON-NLS-1$
                "Custom query: " + select + " not executed because the arguments have a database ident value that is null, from a not yet saved record", // $NON-NLS-1$
                null);
                return false;
            }
            if (value instanceof java.util.Date && !(value instanceof Timestamp) && !(value instanceof Time)) {
                placeholder.setValue(new Timestamp(((java.util.Date) value).getTime()));
            }
        }
    }
    return true;
}
Also used : Placeholder(com.servoy.j2db.query.Placeholder) IVisitor(com.servoy.j2db.util.visitor.IVisitor) DbIdentValue(com.servoy.j2db.dataprocessing.ValueFactory.DbIdentValue) ArrayList(java.util.ArrayList) NativeObject(org.mozilla.javascript.NativeObject) Time(java.sql.Time) QuerySelect(com.servoy.j2db.query.QuerySelect) Timestamp(java.sql.Timestamp) Date(java.util.Date)

Aggregations

IVisitor (com.servoy.j2db.util.visitor.IVisitor)6 BaseQueryTable (com.servoy.base.query.BaseQueryTable)3 QuerySelect (com.servoy.j2db.query.QuerySelect)3 ArrayList (java.util.ArrayList)3 BaseQueryColumn (com.servoy.base.query.BaseQueryColumn)2 Column (com.servoy.j2db.persistence.Column)2 IColumn (com.servoy.j2db.persistence.IColumn)2 RepositoryException (com.servoy.j2db.persistence.RepositoryException)2 Placeholder (com.servoy.j2db.query.Placeholder)2 QueryColumn (com.servoy.j2db.query.QueryColumn)2 QueryJoin (com.servoy.j2db.query.QueryJoin)2 QueryTable (com.servoy.j2db.query.QueryTable)2 HashSet (java.util.HashSet)2 RelatedFindState (com.servoy.j2db.dataprocessing.FindState.RelatedFindState)1 DbIdentValue (com.servoy.j2db.dataprocessing.ValueFactory.DbIdentValue)1 AbstractBase (com.servoy.j2db.persistence.AbstractBase)1 IDataProvider (com.servoy.j2db.persistence.IDataProvider)1 LiteralDataprovider (com.servoy.j2db.persistence.LiteralDataprovider)1 ScriptCalculation (com.servoy.j2db.persistence.ScriptCalculation)1 AndCondition (com.servoy.j2db.query.AndCondition)1