Search in sources :

Example 1 with DateFormat

use of lucee.runtime.format.DateFormat 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)

Aggregations

ParserString (lucee.commons.lang.ParserString)1 PageException (lucee.runtime.exp.PageException)1 DateFormat (lucee.runtime.format.DateFormat)1 TimeFormat (lucee.runtime.format.TimeFormat)1 DateTime (lucee.runtime.type.dt.DateTime)1