KickJava   Java API By Example, From Geeks To Geeks.

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


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;
12
13 import java.text.*;
14
15 /**
16  * @author Jim Caprioli
17  */

18 public abstract class xlSqlWriter {
19     /**
20      * TODO: javadoc
21      *
22      * @param schema
23      *
24      * @return sql string for 'CREATE SCHEMA'
25      */

26     abstract public String JavaDoc wCreateSchema(String JavaDoc schema);
27
28     /**
29      * TODO: javadoc
30      *
31      * @param s
32      * @param t
33      * @param co
34      * @param ty
35      *
36      * @return sql string for 'CREATE TABLE'
37      */

38     public String JavaDoc wCreateTable(String JavaDoc s,
39                                String JavaDoc t,
40                                String JavaDoc[] co,
41                                String JavaDoc[] ty) {
42         String JavaDoc sql;
43         sql = "CREATE TABLE " + getTableName(s, t) + " ( ";
44
45         boolean firstcolumn = true;
46
47         for (int i = 0; i < co.length; i++) {
48             if (firstcolumn) {
49                 firstcolumn = false;
50             } else {
51                 sql = sql + ",";
52             }
53
54             sql = sql + co[i] + " " + ty[i];
55         }
56
57         sql = sql + " );";
58
59         return sql;
60     }
61
62     /**
63      * TODO: javadoc
64      *
65      * @param schema
66      * @param table
67      *
68      * @return sql string for 'DROP TABLE'
69      */

70     abstract public String JavaDoc wDropTable(String JavaDoc schema,
71                                       String JavaDoc table);
72
73     /**
74      * TODO: javadoc
75      *
76      * @param s
77      * @param t
78      * @param co
79      * @param ty
80      * @param va
81      *
82      * @return sql string for 'INSERT INTO ... VALUES '
83      */

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