KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nilostep > xlsql > sql > mysql > xlMySQL


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.mysql;
13
14 import com.nilostep.xlsql.sql.xlSql;
15
16
17 /**
18  * xlMySQL class translates MySQL to native xlSQL
19  *
20  * @author Jim Caprioli
21  */

22 public class xlMySQL extends xlSql {
23     //~ Instance variables ·····················································
24

25     private final int USE = 7;
26     private String JavaDoc context;
27
28     /**
29      * Creates a new instance of type xlMySQL.
30      *
31      * @param xlsqldb xlSQL instance
32      * @param mysqldb initial context ( sql.database in xlsql.properties )
33      */

34     public xlMySQL(com.nilostep.xlsql.database.xlDatabase xlsqldb,
35                    String JavaDoc mysqldb) {
36         super(xlsqldb);
37
38         if (mysqldb == null) {
39             throw new NullPointerException JavaDoc("xlSQL: mysql dbname null");
40         }
41
42         context = mysqldb;
43
44         cmd = new String JavaDoc[8];
45         cmd[INSERT] = "\\s*[Ii][Nn][Ss][Ee][Rr][Tt].*[Ii][Nn][Tt][Oo].*[Vv][Aa][Ll][Uu][Ee][Ss].*";
46         cmd[UPDATE] = "\\s*[Uu][Pp][Dd][Aa][Tt][Ee].*";
47         cmd[DELETE] = "\\s*[Dd][Ee][Ll][Ee][Tt][Ee].*[Ff][Rr][Oo][Mm].*";
48         cmd[CREATE_TABLE] = "\\s*[Cc][Rr][Ee][Aa][Tt][eE].*[Tt][Aa][Bb][Ll][Ee].*";
49         cmd[DROP_TABLE] = "\\s*[Dd][Rr][Oo][Pp].*[Tt][Aa][Bb][Ll][Ee].*";
50         cmd[RENAME_TABLE] = "\\s*[Aa][Ll][Tt][Ee][Rr].*[Tt][Aa][Bb][Ll][Ee].*" +
51                  "[Rr][Ee][Nn][Aa][Mm][Ee].*";
52         cmd[ALTER_TABLE] = "\\s*[Aa][Ll][Tt][Ee][Rr].*[Tt][Aa][Bb][Ll][Ee].*" +
53                  "[Cc][Oo][Ll][Uu][Mm][Nn].*";
54         cmd[USE] = "\\s*[Uu][Ss][Ee].*";
55     }
56
57     //~ Methods ································································
58

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