KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nilostep > xlsql > sql > mckoi > xlMcKoi


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.mckoi;
13
14 import com.nilostep.xlsql.sql.xlSql;
15 import com.nilostep.xlsql.database.*;
16 import com.nilostep.xlsql.sql.*;
17
18 import java.sql.*;
19 import java.util.regex.*;
20
21 /**
22  * xlMcKoi class translates McKoi to native xlSQL
23  *
24  * @author Jim Caprioli
25  */

26 public class xlMcKoi extends xlSql {
27     //~ Instance variables ·····················································
28

29     private final int SET_SCHEMA = 7;
30     private String JavaDoc context;
31
32     /**
33      * Creates a new instance of type xlMcKoi.
34      *
35      * @param xlsqldb xlSQL instance
36      * @param mckoischema initial context for McKoi always APP
37      */

38     public xlMcKoi(com.nilostep.xlsql.database.xlDatabase xlsqldb,
39                    String JavaDoc mckoischema) {
40         super(xlsqldb);
41
42         if (mckoischema == null) {
43             throw new NullPointerException JavaDoc("xlSQL: mckoi schema null");
44         }
45
46         context = mckoischema;
47     }
48
49     //~ Methods ································································
50

51     protected int getCmd(String JavaDoc sql) {
52         return 0;
53     }
54
55     protected String JavaDoc[] getVars(int cmd, String JavaDoc sql) {
56         String JavaDoc[] v = new String JavaDoc[4];
57         String JavaDoc tmp = "";
58         
59         switch (cmd) {
60         case INSERT:
61             tmp = sql.replaceAll(
62                           "\\s*[Ii][Nn][Ss][Ee][Rr][Tt].*[Ii][Nn][Tt][Oo]\\s*",
63                           "");
64             tmp = tmp.substring(0, tmp.indexOf("("));
65             tmp = tmp.replaceAll("\\s*[Vv][Aa][Ll][Uu][Ee][Ss].*", "");
66             break;
67
68         case UPDATE:
69             tmp = sql.replaceAll("\\s*[Uu][Pp][Dd][Aa][Tt][Ee]\\s*", "");
70             tmp = tmp.replaceAll("\\s*[Ss][Ee][Tt].*", "");
71             break;
72
73         case DELETE:
74             tmp = sql.replaceAll(
75                           "\\s*[Dd][Ee][Ll][Ee][Tt][Ee].*[Ff][Rr][Oo][Mm]\\s*",
76                           "");
77             tmp = tmp.replaceAll("\\s*[Ww][Hh][Ee][Rr][Ee].*", "");
78             break;
79
80         case CREATE_TABLE:
81             tmp = sql.replaceAll("\\s*[Cc][Rr][Ee][Aa][Tt][Ee]\\s*", "");
82             tmp = tmp.substring(6);
83             tmp = tmp.replaceAll("\\s*[(].*", "");
84             break;
85
86         case DROP_TABLE:
87             tmp = sql.replaceAll("\\s*[Dd][Rr][Oo][Pp]\\s*", "");
88             tmp = tmp.substring(6);
89             tmp = tmp.replaceAll("\\s*[Ii][Ff].*[Ee][Xx][Ii][Ss][Tt][Ss]\\s*", "");
90             break;
91
92         case RENAME_TABLE:
93             tmp = sql.replaceAll("\\s*[Aa][Ll][Tt][Ee][Rr]\\s*", "");
94             tmp = tmp.trim();
95             tmp = tmp.substring(6);
96             String JavaDoc[] s = tmp.split("[Rr][Ee][Nn][Aa][Mm][Ee].*[Tt][Oo]");
97             if (s.length < 2) {
98                 s = tmp.split("[Rr][Ee][Nn][Aa][Mm][Ee]");
99             }
100             String JavaDoc tmp_old = s[0].trim();
101             tmp_old = tmp_old.replaceAll("[\"]", "");
102             tmp = s[1].trim();
103             tmp = tmp.replaceAll("[\"]", "");
104             if (tmp_old.indexOf(DOT) == -1) {
105                 v[2] = context;
106                 v[3] = tmp_old;
107             } else {
108                 v[2] = tmp_old.substring(0, tmp_old.indexOf(DOT));
109                 v[3] = tmp_old.substring(1 + tmp_old.indexOf(DOT),
110                                                 tmp_old.length());
111             }
112             break;
113
114         case ALTER_TABLE:
115             tmp = sql.replaceAll("\\s*[Aa][Ll][Tt][Ee][Rr]\\s*", "");
116             tmp = tmp.substring(5);
117             tmp = tmp.substring(0, tmp.indexOf(" "));
118             break;
119
120         case SET_SCHEMA:
121             tmp = sql.replaceAll("\\s*[Ss][Ee][Tt]\\s*", "");
122             tmp = tmp.substring(7);
123             tmp = tmp.replaceAll("[;]", "");
124             tmp = tmp.trim();
125             context = tmp;
126
127         default:
128             break;
129         }
130
131         tmp = tmp.trim();
132         tmp = tmp.replaceAll("[;]", "");
133         tmp = tmp.replaceAll("[\"]", "");
134         if (tmp.indexOf(DOT) == -1) {
135             v[0] = context;
136             v[1] = tmp;
137         } else {
138             v[0] = tmp.substring(0, tmp.indexOf(DOT));
139             v[1] = tmp.substring(1 + tmp.indexOf(DOT), tmp.length());
140         }
141         return v;
142     }
143 }
Popular Tags