KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nilostep > xlsql > sql > xlSql


1 /*(Header: NiLOSTEP / xlSQL)
2
3     Copyright (C) 2004 NiLOSTEP Information Sciences, all
4     rights reserved.
5     
6     This program is licensed under the terms of the GNU
7     General Public License.You should have received a copy
8     of the GNU General Public License along with this
9     program;
10 */

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 /**
20  * xlSql class translates SQL to native xlSQL
21  *
22  * @author Jim Caprioli
23  */

24 public abstract class xlSql {
25     //~ Instance variables ·····················································
26

27     protected final String JavaDoc 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 JavaDoc[] cmd;
37     
38     /**
39      * Creates a new instance of type xlSql.
40      *
41      * @param database xlSQL instance
42      */

43     public xlSql(com.nilostep.xlsql.database.xlDatabase database) {
44         if (database == null) {
45             throw new NullPointerException JavaDoc("xlSQL: database null");
46         } else {
47             db = database;
48         }
49     }
50
51     /**
52      * Translate foreign SQL string to xlSQL Command
53      *
54      * @param sql foreign SQL string
55      *
56      * @return native xlSQL command object
57      */

58     public xlSqlCommand parseSql(String JavaDoc sql) {
59         if (sql == null) {
60             throw new NullPointerException JavaDoc("xlSQL: sql string null");
61         }
62
63         xlSqlCommand command;
64
65         String JavaDoc sqlLine=sql.replaceAll("\\n|\\r"," "); // add [Tab] \t ?
66
int cmd = getCmd(sqlLine);
67         String JavaDoc[] 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 JavaDoc 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 JavaDoc[] getVars(int cmd, String JavaDoc sql);
117     
118 }
Popular Tags