KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.nilostep.xlsql.sql.mysql;
12
13 import com.nilostep.xlsql.sql.xlSqlWriter;
14
15 import java.sql.*;
16 import java.text.*;
17 import java.util.*;
18
19 /**
20  * @author Jim Caprioli
21  */

22 public class xlMySQLWriter extends xlSqlWriter {
23     /**
24      * TODO: javadoc
25      *
26      * @param s
27      *
28      * @return sql string for 'CREATE SCHEMA'
29      */

30     public String JavaDoc wCreateSchema(String JavaDoc s) {
31         return "CREATE DATABASE " + s + ";";
32     }
33
34     /**
35      * TODO: javadoc
36      *
37      * @param s
38      * @param t
39      * @param co
40      * @param ty
41      *
42      * @return sql string for 'CREATE TABLE'
43      */

44     public String JavaDoc wCreateTable(String JavaDoc s,
45                                String JavaDoc t,
46                                String JavaDoc[] co,
47                                String JavaDoc[] ty) {
48         String JavaDoc sql = "CREATE TABLE " + s + "." + t + " ( ";
49         boolean firstcolumn = true;
50
51         for (int i = 0; i < co.length; i++) {
52             if (firstcolumn) {
53                 firstcolumn = false;
54             } else {
55                 sql = sql + ",";
56             }
57
58             sql = sql + co[i] + " ";
59
60             if (ty[i].equalsIgnoreCase("VARCHAR")) {
61                 sql = sql + "TEXT";
62             } else if (ty[i].equalsIgnoreCase("BIT")) {
63                 sql = sql + "CHAR(1)";
64             } else {
65                 sql = sql + ty[i];
66             }
67         }
68
69         sql = sql + " );";
70
71         return sql;
72     }
73
74     /**
75      * TODO: javadoc
76      *
77      * @param s
78      * @param t
79      *
80      * @return sql string for 'DROP TABLE'
81      */

82     public String JavaDoc wDropTable(String JavaDoc s,
83                              String JavaDoc t) {
84         return "DROP TABLE IF EXISTS " + s + "." + t + ";";
85     }
86
87     /**
88      * TODO: javadoc
89      *
90      * @param s
91      * @param t
92      * @param co
93      * @param ty
94      * @param va
95      *
96      * @return sql string for 'INSERT INTO ... VALUES '
97      */

98     public String JavaDoc wInsert(String JavaDoc s,
99                           String JavaDoc t,
100                           String JavaDoc[] co,
101                           String JavaDoc[] ty,
102                           String JavaDoc[] va) {
103         String JavaDoc sql = "INSERT INTO " + s + "." + t + " VALUES (";
104         boolean firstcolumn = true;
105
106         for (int i = 0; i < co.length; i++) {
107             if (firstcolumn) {
108                 firstcolumn = false;
109             } else {
110                 sql = sql + ",";
111             }
112
113             if (va[i] == null) {
114                 sql = sql + "null";
115
116                 continue;
117             }
118
119             if ("DOUBLE".equals(ty[i])) {
120                 sql = sql + va[i];
121             } else if ("BIT".equals(ty[i])) {
122                 sql = sql + "'Y'";
123             } else if ("DATE".equals(ty[i])) {
124                 try {
125                     SimpleDateFormat dateFormat =
126                             new SimpleDateFormat("dd/MM/yyyy");
127                     java.util.Date JavaDoc d;
128                     d = dateFormat.parse(va[i]);
129                     dateFormat.applyPattern("yyyy-MM-dd");
130                     sql = sql + "'" + dateFormat.format(d) + "'";
131                 } catch (ParseException pe) {
132                     sql = sql + "null";
133                 }
134             } else {
135                 sql = sql + "'" + va[i] + "'";
136             }
137         }
138
139         sql = sql + " );";
140
141         return sql;
142     }
143
144     protected String JavaDoc getTableName(String JavaDoc schema,
145                                   String JavaDoc table) {
146         return null;
147     }
148 }
Popular Tags