Search in sources :

Example 6 with ParserString

use of lucee.commons.lang.ParserString in project Lucee by lucee.

the class SelectParser method selectExpressions.

// { (selectStatement) [AS] label | tableName [AS] label}
private void selectExpressions(ParserString raw, Select select) throws SQLParserException {
    Expression exp = null;
    do {
        raw.removeSpace();
        exp = expression(raw);
        if (exp == null)
            throw new SQLParserException("missing expression in select part of query");
        raw.removeSpace();
        if (raw.forwardIfCurrent("as ")) {
            String alias = identifier(raw, new RefBooleanImpl(false));
            if (alias == null)
                throw new SQLParserException("missing alias in select part");
            exp.setAlias(alias);
        } else {
            int start = raw.getPos();
            RefBoolean hb = new RefBooleanImpl(false);
            String alias = identifier(raw, hb);
            if (!hb.toBooleanValue() && "from".equalsIgnoreCase(alias))
                raw.setPos(start);
            else if (alias != null)
                exp.setAlias(alias);
        }
        select.addSelectExpression(exp);
        raw.removeSpace();
    } while (raw.forwardIfCurrent(','));
}
Also used : RefBoolean(lucee.commons.lang.types.RefBoolean) ColumnExpression(lucee.runtime.sql.exp.ColumnExpression) Expression(lucee.runtime.sql.exp.Expression) ParserString(lucee.commons.lang.ParserString) ValueString(lucee.runtime.sql.exp.value.ValueString) RefBooleanImpl(lucee.commons.lang.types.RefBooleanImpl)

Example 7 with ParserString

use of lucee.commons.lang.ParserString in project Lucee by lucee.

the class SQLPrettyfier method prettyfie.

public static String prettyfie(String sql, boolean validZql) {
    ParserString ps = new ParserString(sql.trim());
    boolean insideString = false;
    // short insideKlammer=0;
    StringBuilder sb = new StringBuilder(sql.length());
    outer: while (!ps.isAfterLast()) {
        if (insideString) {
            if (ps.isCurrent('\'')) {
                if (!ps.hasNext() || !ps.isNext('\''))
                    insideString = false;
            }
        } else {
            if (ps.isCurrent('\''))
                insideString = true;
            else if (ps.isCurrent('?')) {
                sb.append(" " + PLACEHOLDER_QUESTION + " ");
                ps.next();
                continue;
            } else if (ps.isCurrent('{')) {
                StringBuilder date = new StringBuilder();
                int pos = ps.getPos();
                while (true) {
                    if (ps.isAfterLast()) {
                        ps.setPos(pos);
                        break;
                    } else if (ps.isCurrent('}')) {
                        date.append('}');
                        DateTime d;
                        try {
                            d = DateCaster.toDateAdvanced(date.toString(), null);
                        } catch (PageException e) {
                            ps.setPos(pos);
                            break;
                        }
                        sb.append('\'');
                        sb.append(new DateFormat(Locale.US).format(d, "yyyy-mm-dd"));
                        sb.append(' ');
                        sb.append(new TimeFormat(Locale.US).format(d, "HH:mm:ss"));
                        sb.append('\'');
                        ps.next();
                        continue outer;
                    } else {
                        date.append(ps.getCurrent());
                        ps.next();
                    }
                }
            } else if (ps.isCurrent('*')) {
                sb.append(" " + PLACEHOLDER_ASTERIX + " ");
                ps.next();
                // last=ps.getCurrent();
                continue;
            } else if (validZql && ps.isCurrent('a')) {
                if (ps.isPreviousWhiteSpace() && ps.isNext('s') && ps.isNextNextWhiteSpace()) {
                    ps.next();
                    ps.next();
                    ps.removeSpace();
                    continue;
                }
            }
        /*for(int i=0;i<reseved_words.length;i++) {
		        	if(ps.isCurrent(reseved_words[i])) {
			        	int pos=ps.getPos();
			        	ps.setPos(pos+4);
			        	if(ps.isCurrentWhiteSpace()) {
			        		sb.append(" placeholder_"+reseved_words[i]+" ");
			        		continue;
			        	}
			        	if(ps.isCurrent(',')) {
			        		sb.append(" placeholder_"+reseved_words[i]+",");
			        		continue;
			        	}
			        	ps.setPos(pos);
			        }
		        }*/
        /*if(ps.isCurrent("char")) {
		        	int pos=ps.getPos();
		        	ps.setPos(pos+4);
		        	if(ps.isCurrentWhiteSpace()) {
		        		sb.append(" "+PLACEHOLDER_CHAR+" ");
		        		continue;
		        	}
		        	if(ps.isCurrent(',')) {
		        		sb.append(" "+PLACEHOLDER_CHAR+",");
		        		continue;
		        	}
		        	ps.setPos(pos);
		        }*/
        }
        sb.append(ps.getCurrent());
        ps.next();
    }
    ;
    if (!ps.isLast(';'))
        sb.append(';');
    // print.err("---------------------------------------------------------------------------------");
    return sb.toString();
}
Also used : PageException(lucee.runtime.exp.PageException) TimeFormat(lucee.runtime.format.TimeFormat) ParserString(lucee.commons.lang.ParserString) DateFormat(lucee.runtime.format.DateFormat) DateTime(lucee.runtime.type.dt.DateTime)

Example 8 with ParserString

use of lucee.commons.lang.ParserString in project Lucee by lucee.

the class CFMLExpressionInterpreter method interpret.

public Object interpret(PageContext pc, String str, boolean preciseMath) throws PageException {
    this.cfml = new ParserString(str);
    this.preciseMath = preciseMath;
    init(pc);
    if (LITERAL_ARRAY == null)
        LITERAL_ARRAY = fld.getFunction("_literalArray");
    if (LITERAL_STRUCT == null)
        LITERAL_STRUCT = fld.getFunction("_literalStruct");
    if (JSON_ARRAY == null)
        JSON_ARRAY = fld.getFunction("_jsonArray");
    if (JSON_STRUCT == null)
        JSON_STRUCT = fld.getFunction("_jsonStruct");
    if (LITERAL_ORDERED_STRUCT == null)
        LITERAL_ORDERED_STRUCT = fld.getFunction("_literalOrderedStruct");
    cfml.removeSpace();
    Ref ref = assignOp();
    cfml.removeSpace();
    if (cfml.isAfterLast()) {
        // data.put(str+":"+preciseMath,ref);
        return ref.getValue(pc);
    }
    throw new InterpreterException("Syntax Error, invalid Expression [" + cfml.toString() + "]");
}
Also used : Ref(lucee.runtime.interpreter.ref.Ref) ParserString(lucee.commons.lang.ParserString)

Example 9 with ParserString

use of lucee.commons.lang.ParserString in project Lucee by lucee.

the class VariableInterpreter method parse.

/*
	public static boolean isDefined(PageContext pc,String var) {
        StringList list = parse(pc,new ParserString(var));
        if(list==null) return false;
        
		int scope=scopeString2Int(list.next());
		Object coll =NULL; 
		if(scope==Scope.SCOPE_UNDEFINED) {
		    coll=pc.undefinedScope().get(list.current(),NULL);
		    if(coll==NULL) return false;
		}
		else {
		    try {
                coll=pc.scope(scope);
            } catch (PageException e) {
                return false;
            }
		}
		
		while(list.hasNext()) {
			coll=pc.getVariableUtil().get(pc,coll,list.next(),NULL);
			//print.out(coll);
			if(coll==NULL) return false;
		}
       
		return true;
    }
	 */
/**
 * parse a Literal variable String and return result as String List
 * @param pc Page Context
 * @param ps ParserString to read
 * @return Variable Definition in a String List
 */
private static StringList parse(PageContext pc, ParserString ps, boolean doLowerCase) {
    String id = readIdentifier(ps, doLowerCase);
    if (id == null)
        return null;
    StringList list = new StringList(id);
    CFMLExpressionInterpreter interpreter = null;
    while (true) {
        if (ps.forwardIfCurrent('.')) {
            id = readIdentifier(ps, doLowerCase);
            if (id == null)
                return null;
            list.add(id);
        } else if (ps.forwardIfCurrent('[')) {
            if (interpreter == null)
                interpreter = new CFMLExpressionInterpreter(false);
            try {
                list.add(Caster.toString(interpreter.interpretPart(pc, ps)));
            } catch (PageException e) {
                return null;
            }
            if (!ps.forwardIfCurrent(']'))
                return null;
            ps.removeSpace();
        } else
            break;
    }
    if (ps.isValidIndex())
        return null;
    list.reset();
    return list;
}
Also used : PageException(lucee.runtime.exp.PageException) StringList(lucee.commons.lang.StringList) ParserString(lucee.commons.lang.ParserString)

Example 10 with ParserString

use of lucee.commons.lang.ParserString in project Lucee by lucee.

the class VariableInterpreter method parse.

public static StringList parse(String var, boolean doLowerCase) {
    ParserString ps = new ParserString(var);
    String id = readIdentifier(ps, doLowerCase);
    if (id == null)
        return null;
    StringList list = new StringList(id);
    while (true) {
        if (ps.forwardIfCurrent('.')) {
            id = readIdentifier(ps, doLowerCase);
            if (id == null)
                return null;
            list.add(id);
        } else
            break;
    }
    if (ps.isValidIndex())
        return null;
    list.reset();
    return list;
}
Also used : ParserString(lucee.commons.lang.ParserString) StringList(lucee.commons.lang.StringList) ParserString(lucee.commons.lang.ParserString)

Aggregations

ParserString (lucee.commons.lang.ParserString)23 StringList (lucee.commons.lang.StringList)10 ValueString (lucee.runtime.sql.exp.value.ValueString)7 PageException (lucee.runtime.exp.PageException)5 RefBoolean (lucee.commons.lang.types.RefBoolean)3 RefBooleanImpl (lucee.commons.lang.types.RefBooleanImpl)3 ColumnExpression (lucee.runtime.sql.exp.ColumnExpression)3 Expression (lucee.runtime.sql.exp.Expression)2 ValueNumber (lucee.runtime.sql.exp.value.ValueNumber)2 DateFormat (lucee.runtime.format.DateFormat)1 TimeFormat (lucee.runtime.format.TimeFormat)1 Ref (lucee.runtime.interpreter.ref.Ref)1 Column (lucee.runtime.sql.exp.Column)1 OperationN (lucee.runtime.sql.exp.op.OperationN)1 Collection (lucee.runtime.type.Collection)1 Struct (lucee.runtime.type.Struct)1 StructImpl (lucee.runtime.type.StructImpl)1 DateTime (lucee.runtime.type.dt.DateTime)1 VariableReference (lucee.runtime.type.ref.VariableReference)1 Attribute (lucee.transformer.bytecode.statement.tag.Attribute)1