001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.runtime.db; 020 021import java.util.Locale; 022 023import lucee.commons.lang.ParserString; 024import lucee.runtime.exp.PageException; 025import lucee.runtime.format.DateFormat; 026import lucee.runtime.format.TimeFormat; 027import lucee.runtime.op.date.DateCaster; 028import lucee.runtime.type.dt.DateTime; 029 030public class SQLPrettyfier { 031 032 public static final String PLACEHOLDER_COUNT="placeholder_count"; 033 public static final String PLACEHOLDER_ASTERIX="placeholder_asterix"; 034 public static final String PLACEHOLDER_QUESTION="QUESTION_MARK_SIGN"; 035 036 037 038 public static String prettyfie(String sql){ 039 return prettyfie(sql,false); 040 } 041 042 public static String prettyfie(String sql, boolean validZql){ 043 044 ParserString ps=new ParserString(sql.trim()); 045 boolean insideString=false; 046 //short insideKlammer=0; 047 StringBuilder sb=new StringBuilder(sql.length()); 048 //char last=0; 049 050 outer:while(!ps.isAfterLast()) { 051 if(insideString) { 052 if(ps.isCurrent('\'')) { 053 if(!ps.hasNext() || !ps.isNext('\''))insideString=false; 054 } 055 } 056 else { 057 if(ps.isCurrent('\''))insideString=true; 058 else if(ps.isCurrent('?')) { 059 sb.append(" "+PLACEHOLDER_QUESTION+" "); 060 ps.next(); 061 continue; 062 } 063 else if(ps.isCurrent('{')) { 064 StringBuilder date=new StringBuilder(); 065 int pos=ps.getPos(); 066 while(true) { 067 if(ps.isAfterLast()){ 068 ps.setPos(pos); 069 break; 070 } 071 else if(ps.isCurrent('}')) { 072 date.append('}'); 073 DateTime d; 074 try { 075 d = DateCaster.toDateAdvanced(date.toString(), null); 076 } 077 catch (PageException e) { 078 ps.setPos(pos); 079 break; 080 } 081 sb.append('\''); 082 sb.append(new DateFormat(Locale.US).format(d,"yyyy-mm-dd")); 083 sb.append(' '); 084 sb.append(new TimeFormat(Locale.US).format(d,"HH:mm:ss")); 085 sb.append('\''); 086 ps.next(); 087 continue outer; 088 } 089 else { 090 date.append(ps.getCurrent()); 091 ps.next(); 092 } 093 } 094 } 095 else if(ps.isCurrent('*')) { 096 sb.append(" "+PLACEHOLDER_ASTERIX+" "); 097 ps.next(); 098 //last=ps.getCurrent(); 099 continue; 100 } 101 else if(validZql && ps.isCurrent('a')) { 102 if(ps.isPreviousWhiteSpace() && ps.isNext('s') && ps.isNextNextWhiteSpace()) { 103 ps.next(); 104 ps.next(); 105 ps.removeSpace(); 106 107 continue; 108 } 109 110 } 111 /*for(int i=0;i<reseved_words.length;i++) { 112 if(ps.isCurrent(reseved_words[i])) { 113 int pos=ps.getPos(); 114 ps.setPos(pos+4); 115 if(ps.isCurrentWhiteSpace()) { 116 sb.append(" placeholder_"+reseved_words[i]+" "); 117 continue; 118 } 119 if(ps.isCurrent(',')) { 120 sb.append(" placeholder_"+reseved_words[i]+","); 121 continue; 122 } 123 ps.setPos(pos); 124 } 125 }*/ 126 /*if(ps.isCurrent("char")) { 127 int pos=ps.getPos(); 128 ps.setPos(pos+4); 129 if(ps.isCurrentWhiteSpace()) { 130 sb.append(" "+PLACEHOLDER_CHAR+" "); 131 continue; 132 } 133 if(ps.isCurrent(',')) { 134 sb.append(" "+PLACEHOLDER_CHAR+","); 135 continue; 136 } 137 ps.setPos(pos); 138 }*/ 139 } 140 sb.append(ps.getCurrent()); 141 ps.next(); 142 } 143 ; 144 if(!ps.isLast(';'))sb.append(';'); 145 146 //print.err(sb.toString()); 147 //print.err("---------------------------------------------------------------------------------"); 148 return sb.toString(); 149 } 150 151 152}