1 11 12 package com.nilostep.xlsql.sql; 13 14 import com.nilostep.xlsql.database.*; 15 16 import java.sql.*; 17 import java.util.regex.*; 18 19 24 public abstract class xlSql { 25 27 protected final String DOT = "."; 28 protected final int INSERT = 0; 29 protected final int UPDATE = 1; 30 protected final int DELETE = 2; 31 protected final int CREATE_TABLE = 3; 32 protected final int DROP_TABLE = 4; 33 protected final int RENAME_TABLE = 5; 34 protected final int ALTER_TABLE = 6; 35 protected com.nilostep.xlsql.database.xlDatabase db; 36 protected String [] cmd; 37 38 43 public xlSql(com.nilostep.xlsql.database.xlDatabase database) { 44 if (database == null) { 45 throw new NullPointerException ("xlSQL: database null"); 46 } else { 47 db = database; 48 } 49 } 50 51 58 public xlSqlCommand parseSql(String sql) { 59 if (sql == null) { 60 throw new NullPointerException ("xlSQL: sql string null"); 61 } 62 63 xlSqlCommand command; 64 65 String sqlLine=sql.replaceAll("\\n|\\r"," "); int cmd = getCmd(sqlLine); 67 String [] v = getVars(cmd, sqlLine); 68 69 switch (cmd) { 70 71 case INSERT: 72 command = new xlSqlInsert(db, v[0], v[1]); 73 break; 74 75 case UPDATE: 76 command = new xlSqlUpdate(db, v[0], v[1]); 77 break; 78 79 case DELETE: 80 command = new xlSqlDelete(db, v[0], v[1]); 81 break; 82 83 case CREATE_TABLE: 84 command = new xlSqlCreateTable(db, v[0], v[1]); 85 break; 86 87 case DROP_TABLE: 88 command = new xlSqlDropTable(db, v[0], v[1]); 89 break; 90 91 case RENAME_TABLE: 92 command = new xlSqlRenameTable(db, v[0], v[1], v[2], v[3]); 93 break; 94 95 case ALTER_TABLE: 96 command = new xlSqlAlterTable(db, v[0], v[1]); 97 break; 98 99 default: 100 command = new xlSqlNull(); 101 break; 102 } 103 return command; 104 } 105 106 protected int getCmd(String sql) { 107 int i; 108 for (i = 0; i < cmd.length; i++) { 109 if (Pattern.compile(cmd[i]).matcher(sql).matches()) { 110 break; 111 } 112 } 113 return i; 114 } 115 116 protected abstract String [] getVars(int cmd, String sql); 117 118 } | Popular Tags |