StringBuffer selsb = new StringBuffer(sels);
selsb = SharedMethods.replaceSubstringBuffer(selsb,"\"","");
selsb = SharedMethods.replaceSubstringBuffer(selsb,"'","");
selsb = SharedMethods.replaceSubstringBuffer(selsb,"`","");
selectList = new EZArrayList(new StringTokenizer(selsb.toString(),","));
////();
if (sels.toLowerCase().startsWith("count(") && sels.endsWith(")"))
{
count = true;
selectList = new EZArrayList();
selectList.add("*");
//("SC 1" + this);
}
//origSelectL = (Vector)selectList.clone();
setOriginalSelectList(selectList);
tok = tok.substring(si + " from ".length(),tok.length());
tok = tok.trim();
tok2 = tok.toLowerCase();
int i = tok.indexOf(" ");
if (i < 0)
{
table = tok.substring(0,tok.length());
table = parseTable(table);
setTable(table);
parseSelectAS();
return;
}
String jtl = "";
while (true){
tok = parseTableAlias(tok);
int i3 = tok.toLowerCase().indexOf(" as ");
if (i3 > 0){
jtl = jtl + tok.substring(0,i3 + " as ".length());
tok = tok.substring(i3 + " as ".length(),tok.length());
tok = StringUtil.getTrimmedValue(tok);
}
i = tok.indexOf(" ");
int i2 = tok.indexOf(" ,");
if (i2 > 0 && i2 <= i)
i = i+ 1;
i2 = tok.indexOf(",");
if (i2 > 0 && i2 < i)
i = i2;
if (i < 1)
{
jtl = jtl + tok;
break;
}
table = tok.substring(0,i + 1);
jtl = jtl + table;
int ji = table.indexOf(",");
if (ji > 0){
//jtl = jtl + table;
tok = tok.substring(i + 1,tok.length());
tok = tok.trim();
}
else break;
}
table = jtl;
int ji = table.indexOf(",");
//(ji + " PSTBL2 : " + table);
if (ji > 0){
getUnion().parseTables(table.substring(ji + 1,table.length()).trim());
table = table.substring(0,ji).trim();
}
table = table.trim();
table = parseTableAlias(table);
int tas = table.toLowerCase().indexOf(" as ");
if (tas > 0)
{
String al = table.substring(tas + " as ".length(),table.length());
table = table.substring(0,tas);
al = StringUtil.getTrimmedValue(al);
table = StringUtil.getTrimmedValue(table);
if (StringUtil.isRealString(al))
aliases.put(al,table);
}
setTable(table);
parseSelectAS();
tok = tok.substring(i + 1,tok.length());
tok = tok.trim();
tok = groupby.parse(tok);
tok2 = tok.toLowerCase();
//i = tok2.indexOf(" ");
//("OBY b " + tok2);
//if (i > -1)
i = tok2.indexOf(" order by ");
if (i < 0)
i = tok2.indexOf("order by ");
if (i > -1)
{
String oby = tok.substring(i + "order by ".length(),tok.length() );
tok = tok.substring(0,i);
tok = tok.trim();
tok2 = oby.toLowerCase();
i = tok2.indexOf(" desc");
if (i > 0){
oby = oby.substring(0,i);
desc = true;
}
i = tok2.indexOf(" asc");
if (i > 0){
oby = oby.substring(0,i);
}
oby = oby.trim();
//("OBY a " + oby);
sortList = new EZArrayList(new StringTokenizer(oby,","));
tok2 = tok.toLowerCase();
}
if (tok2.startsWith("where "))
{
tok2 = " " + tok2;
tok = " " + tok;
}
//("HELLO " + tok);
i = tok2.indexOf(" where ");
if (i > -1){
String al = tok.substring(0,i);
al = al.trim();
if (al.toLowerCase().startsWith("as "))
{
al = al.substring(3,al.length());
al = al.trim();
}
ji = al.indexOf(",");
//(ji + " PSTBL3 : " + al);
if (ji > 0){
getUnion().parseTables(al.substring(ji + 1,al.length()).trim());
al = al.substring(0,ji).trim();
}
if (StringUtil.isRealString(al))
aliases.put(al,table);
tok = tok.substring(i + 1,tok.length());
tok = tok.trim();
tok2 = tok.toLowerCase();
if (selectList.size() > 0)
for (int ct = 0;ct < selectList.size();ct++)
{
String se = selectList.elementAt(ct).toString();
if (se.equals(al))
selectList.setElementAt("*",ct);
else if (se.startsWith(al + ".") && StringUtil.isRealString(se.substring(al.length() + 1,se.length()))){
String cn = se.substring(al.length() + 1,se.length());
//(se + " SELECTSSS " + cn);
updateSelectAS(se,cn);
selectList.setElementAt(cn,ct);
}
}
}else if (tok2.trim().length() > 0)
{
tok = tok.trim();
String al = tok;
al = al.trim();
if (al.toLowerCase().startsWith("as "))
{
al = al.substring(3,al.length());
al = al.trim();
}
if (StringUtil.isRealString(al))
aliases.put(al,table);
}
i = tok2.indexOf("where ");
if (i > -1){
tok = tok.substring(i + "where ".length(),tok.length());
tok = tok.trim();
tok2 = tok.toLowerCase();
String lc = null;
//("SEL 1 " + tok);
while (true){
if (tok.length() < 1)break;
i = tok2.indexOf(" and ");
if (i > -1){
tok2 = tok.substring(0,i);
tok2 = tok2.trim();
Criteria cr = parseCriteria(tok2);
if (lc == null || lc.equals("and")){
if (!getUnion().addToIncludeList(table,getTAlias(table),getSAlias(cr.getName()),getSAlias(cr.getValueString()),cr))
includealllist.add(cr);
}
else{
if (!getUnion().addToEitherOrList(table,getTAlias(table),getSAlias(cr.getName()),getSAlias(cr.getValueString()),cr))
eitheroralllist.add(cr);
}
tok = tok.substring(i + " and ".length(),tok.length());
tok = tok.trim();
tok2 = tok.toLowerCase();
lc = "and";
}
else if( tok2.indexOf(" or ") > -1){
i = tok2.indexOf(" or ");
tok2 = tok.substring(0,i);
tok2 = tok2.trim();
//eitheroralllist.add(parseCriteria(tok2));
Criteria cr = parseCriteria(tok2);
if (lc == null || lc.equals("or"))
{
if (!getUnion().addToEitherOrList(table,getTAlias(table),getSAlias(cr.getName()),getSAlias(cr.getValueString()),cr))
eitheroralllist.add(cr);
} else
{
if (!getUnion().addToIncludeList(table,getTAlias(table),getSAlias(cr.getName()),getSAlias(cr.getValueString()),cr))
includealllist.add(cr);
}
tok = tok.substring(i + " or ".length(),tok.length());
tok = tok.trim();
tok2 = tok.toLowerCase();
lc = "or";
}
else{
Criteria cr = parseCriteria(tok);
if (lc == null || lc.equals("and"))
{
if (!getUnion().addToIncludeList(table,getTAlias(table),getSAlias(cr.getName()),getSAlias(cr.getValueString()),cr))
includealllist.add(cr);
} else
{
if (!getUnion().addToEitherOrList(table,getTAlias(table),getSAlias(cr.getName()),getSAlias(cr.getValueString()),cr))
eitheroralllist.add(cr);
} break;
}
}
}
return;
}
else if (tok2.startsWith("delete ")){
action = "delete";
tok = tok.substring("delete ".length(),tok.length());
tok = tok.trim();
tok2 = tok.toLowerCase();
int si = tok2.indexOf("from ");
tok = tok.substring(si + "from ".length(),tok.length());
tok = tok.trim();
tok2 = tok.toLowerCase();
int i = tok.indexOf(" ");
if (i < 0)
{
table = tok.substring(0,tok.length());
setTable(table);
return;
}
table = tok.substring(0,i);
setTable(table);
tok = tok.substring(i + 1,tok.length());
tok = tok.trim();
tok2 = tok.toLowerCase();
i = tok2.indexOf("where ");
if (i > -1){
tok = tok.substring(i + "where ".length(),tok.length());
tok = tok.trim();
tok2 = tok.toLowerCase();
while (true){
if (tok.length() < 1)break;
i = tok2.indexOf(" and ");
if (i > -1){
tok2 = tok.substring(0,i);
tok2 = tok2.trim();
Criteria cr = parseCriteria(tok2);
if (!getUnion().addToIncludeList(table,getTAlias(table),getSAlias(cr.getName()),getSAlias(cr.getValueString()),cr))
includealllist.add(cr);
tok = tok.substring(i + " and ".length(),tok.length());
tok = tok.trim();
tok2 = tok.toLowerCase();
}
else if( tok2.indexOf(" or ") > -1){
}
else{
Criteria cr = parseCriteria(tok);
if (!getUnion().addToIncludeList(table,getTAlias(table),getSAlias(cr.getName()),getSAlias(cr.getValueString()),cr))
includealllist.add(cr);
break;
}
}
}
return;
}
else if (tok2.startsWith("update ")){
action = "update";
tok = tok.substring("update ".length(),tok.length());
tok = tok.trim();
tok2 = tok.toLowerCase();
int i = tok.indexOf(" ");
if (i < 0)
i = tok.length();
table = tok.substring(0,i);
setTable(table);
tok = tok.substring(i + 1,tok.length());
tok = tok.trim();
tok2 = tok.toLowerCase();
tok = tok.substring("set ".length(),tok.length());
tok = tok.trim();
tok2 = tok.toLowerCase();
int si = tok2.indexOf(" where ");
if (si < 0)
si = tok2.length();
String ups = tok.substring(0,si);
ups = ups.trim();
//tok = encode(ups);
EZArrayList uv = new EZArrayList(new StringTokenizer(ups,","));
String upstr = null;
for (int ct = 0;ct < uv.size();ct++){
upstr = uv.elementAt(ct).toString();
updatelist.add(parseUpdate(upstr));
}
si = tok2.indexOf(" where ");
if (si < 0)return;
tok = tok.substring(si + " where ".length(),tok.length());
tok = tok.trim();
tok2 = tok.toLowerCase();
/*int i = tok.indexOf(" ");
table = tok.substring(0,i);
tok = tok.substring(i + 1,tok.length());
tok = tok.trim();
tok2 = tok.toLowerCase();*/
// i = tok2.indexOf("where ");
// if (i > -1){
//tok = tok.substring(i + "where ".length(),tok.length());
//tok = tok.trim();
//tok2 = tok.toLowerCase();
while (true){
i = tok2.indexOf(" and ");
if (i > -1){
tok2 = tok.substring(0,i);
tok2 = tok2.trim();
Criteria cr = parseCriteria(tok2);
if (!getUnion().addToIncludeList(table,getTAlias(table),getSAlias(cr.getName()),getSAlias(cr.getValueString()),cr))
includealllist.add(cr);
tok = tok.substring(i + " and ".length(),tok.length());
tok = tok.trim();
tok2 = tok.toLowerCase();
}
else if( tok2.indexOf(" or ") > -1){
}
else{
Criteria cr = parseCriteria(tok);
if (!getUnion().addToIncludeList(table,getTAlias(table),getSAlias(cr.getName()),getSAlias(cr.getValueString()),cr))
includealllist.add(cr);
break;
}
}
// }
return;
}else if (SpecialCommandParser.parse(this,firstToken)){
return;
} else if (tok2.startsWith("show tables")){
action = "showTables";
return;
}
else if (tok2.startsWith("describe ")){
action = "describeTable";
tok2 = tok.substring("describe ".length(),tok2.length()).trim();
setTable(tok2);
return;
}
else if (tok2.startsWith("getcolumns ")){
action = "getColumns";
tok2 = tok.substring("getcolumns ".length(),tok2.length()).trim();
setTable(tok2);
return;
}
else if (tok2.startsWith("getprimarykeys ")){
action = "getPrimaryKeys";
tok2 = tok.substring("getprimarykeys ".length(),tok2.length()).trim();
setTable(tok2);
return;
}//special = true;
else if (tok2.startsWith("getindex ")){
action = "getIndex";
tok2 = tok.substring("getindex ".length(),tok2.length()).trim();
setTable(tok2);
special = true;
return;
}
else if (tok2.startsWith("getexportedkeys ")){
action = "getExportedKeys";
tok2 = tok.substring("getexportedkeys ".length(),tok2.length()).trim();
setTable(tok2);
return;
}
else if (tok2.startsWith("gettypeinfo")){
special = true;
setAction("getTypeInfo");
//local = true;
return;
}
else if (tok2.startsWith("getimportedkeys ")){
action = "getImportedKeys";
tok2 = tok.substring("getimportedkeys ".length(),tok2.length()).trim();
setTable(tok2);
return;
}
else if (tok2.startsWith("jiqldescribe ")){
action = "jiqldescribeTable";
tok2 = tok.substring("jiqldescribe ".length(),tok2.length()).trim();
setTable(tok2);
return;
}
ti = tok2.indexOf(" index ");
if (tok2.startsWith("create ") && ti > 0)
{
action = "createIndex";
return;
}
ti = tok2.indexOf(" table ");
if (tok2.startsWith("create ") && ti > 0){
action = "createTable";
tok = tok.substring(ti + " table ".length(),tok.length());
tok = tok.trim();
if (tok.toUpperCase().startsWith("IF NOT EXISTS"))
{
tok = tok.substring(14,tok.length());
tok = tok.trim();
getCreateParser().setIfNotExists(true);
}
int i = tok.indexOf(" ");
if (i < 0)
i = tok.length();
table = tok.substring(0,i);
setTable(table);
if (!AlphaNumeric.isAlphaNumeric(table,"_"))
throw JGException.get("invalid_table_name", table + " Invalid Create Table name");
tok = tok.substring(i + 1,tok.length());
tok = tok.trim();
//(tok + ":" + table + ":REDBEAN");
if (tok.startsWith("("))
{
tok = tok.substring(1,tok.length());
tok = tok.trim();
if (tok.endsWith(")"))
{
tok = tok.substring(0,tok.length() - 1);
tok = tok.trim();
}
}
parseCreateTableParams();
return;
}
else if (tok2.startsWith("drop ") && ti > 0){
action = "dropTable";
tok = tok.substring(ti + " table ".length(),tok.length());
tok = tok.trim();
int i = tok.indexOf(" ");
if (i < 0)
i = tok.length();
table = tok.substring(0,i);
setTable(table);
return;
}
else if (tok2.startsWith("set "))
{
action = "setOperation";
return;
}
else if (tok2.startsWith("load ")){
action = "loadTable";
getLoadTable();
return;
}
else if (tok2.startsWith("insert ")){
action = "sqlInsert";
getInsertIntoTable();
return;
}
else if (tok2.startsWith("alter ") && ti > 0){
parseAlter(ti);
/*action = "dropTable";
tok = tok.substring(ti + " table ".length(),tok.length());
tok = tok.trim();
int i = tok.indexOf(" ");
if (i < 0)
i = tok.length();
table = tok.substring(0,i);
return;*/
}
ti = tok2.indexOf(" user ");
if (tok2.startsWith("drop ") && ti > 0)
{
action = "dropUser";
tok = tok.substring(ti + " user ".length(),tok.length());
tok = tok.trim();
return;
}
else if (tok2.startsWith("create ") && ti > 0)
{
/*action = "createUser";
tok = tok.substring(ti + " user ".length(),tok.length());
tok = tok.trim();
//create user DB_NAME identified by PASSWORD role user|admin
int i = tok.indexOf(" ");
String nuser = tok.substring(0,i);
tok = tok.substring(i + 1,tok.length());
tok = tok.trim();
//(tok + ":" + nuser + ":create suser");
("nuser",nuser);
//identified by PASSWORD role user|admin
//PASSWORD role user|admin
tok = tok.substring("identified".length(),tok.length());
tok = tok.trim();
tok = tok.substring("by".length(),tok.length());
tok = tok.trim();
String d = JGProperties.get().getProperty("digest");
("nupassword",tok);
if (StringUtil.isRealString(d) && !d.equals("PLAIN"))
("nupassword",Crypto.digest(tok,d));
i = tok.indexOf(" ");
if (i > 0)
{
if (StringUtil.isRealString(d) && !d.equals("PLAIN"))
("nupassword",Crypto.digest(tok.substring(0,i).trim(),d));
else
("nupassword",tok.substring(0,i).trim());
tok = tok.substring(i+1,tok.length());
tok = tok.trim();
if (tok.startsWith("role "))
{
tok = tok.substring("role ".length(),tok.length());
tok = tok.trim();
if (!tok.equalsIgnoreCase("admin") && !tok.equalsIgnoreCase("user"))
throw JGException.get("invalid_role Invalid Role! Should be admin or user");
("role",tok);
}
}
return;*/
}
ti = tok2.indexOf(" into ");
if (tok2.startsWith("jiqlinsert ") && ti > 0)
{
action = "writeTableRow";
tok = tok.substring(ti + " into ".length(),tok.length());
tok = tok.trim();
int i = tok.indexOf(" ");
int i2 = tok.indexOf("(");
if (i > i2)
i = i2;
if (i < 0)
i = tok.length();
table = tok.substring(0,i);
setTable(table);
if (i == i2)
tok = tok.substring(i,tok.length());
else
tok = tok.substring(i + 1,tok.length());
tok = tok.trim();
boolean checkfl = false;
if (tok.toLowerCase().startsWith("valjiqlues"))
{
checkfl = true;
StringBuffer sbf = new StringBuffer("(");
EZArrayList fv = getJiqlTableInfo().getFieldList();
sbf.append(fv.toDelimitedString(","));
sbf.append(") values ");
//tok = tok.substring("values".length(),tok.length());
//tok = tok.trim();
tok = sbf.toString() + tok.substring(10,tok.length());
}
if (tok.toLowerCase().startsWith("values"))
{
/*checkfl = true;
StringBuffer sbf = new StringBuffer("(");
EZArrayList fv = getJiqlTableInfo().getFieldList();
sbf.append(fv.toDelimitedString(","));
sbf.append(") ");*/
tok = tok.substring("values".length(),tok.length());
tok = tok.trim();
//tok = sbf.toString() + tok;
}
//("INSERT 1 " + tok);
tok = tok.substring(1,tok.length());
tok = tok.trim();
//tok = encode(tok);
i = tok.indexOf(")");
String tokf = tok.substring(0,i);
tok = tok.substring(i+ 1,tok.length());
tok = tok.trim();
Vector v = null;
if (tok.toLowerCase().startsWith("values"))
{
tokf = tokf.trim();
tok = tok.substring("values".length(),tok.length());
tok = tok.trim();
tok = tok.substring(1,tok.length());
tok = tok.trim();
tok = tok.substring(0,tok.length() -1);
tok = tok.trim();
v = new EZArrayList(new StringTokenizer(tokf,","));
}
else
{
tok = tokf;
v = getJiqlTableInfo().getFieldList();
/*Vector r = Gateway.get(getProperties()).describeTable(this);
v = new Vector();
NameValuePairs row= null;
for (int ct = 0;ct < r.size();ct++){
row = (NameValuePairs)r.elementAt(ct);
v.add(row.getString("Field"));
}*/
}
EZArrayList vv = new EZArrayList(new StringTokenizer(tok,","));
String n = null;
String va = null;
for (int ct = 0;ct < v.size();ct++)
{
n = v.elementAt(ct).toString();
n = n.trim();
n= StringUtil.getTrimmedValue(n);
try{
va = vv.elementAt(ct).toString();
}catch (ArrayIndexOutOfBoundsException e){
throw JGException.get("missing_column_value",n + " Missing Column Value! ");
}
//(n + " TOKO b " + va);