1 package org.opensync.importexport;
2
3 import java.sql.Connection;
4 import java.sql.SQLException;
5 import java.sql.SQLWarning;
6 import java.sql.Statement;
7 import java.util.Hashtable;
8 import java.util.Vector;
9
10 import org.opensync.tools.ConnectionPool;
11 import org.opensync.tools.Utils;
12
13 public class Insert extends Object {
14
15 private Hashtable valuesInsert;
16 private View theme;
17
18 private String fileInsert;
19 private Vector fieldsInsert;
20
21 private boolean batchMode = false;
22
23 public Insert(ConnectionPool pool, View thm, Hashtable values) throws SQLException,SQLWarning {
24
25 theme = thm;
26 valuesInsert = values;
27
28 fileInsert = theme.getIdParentFile();
29 fieldsInsert = buildFieldsInsert();
30 lauchQueryInsert(pool);
31 }
32
33 public Insert(Connection conn, View thm, Hashtable values, boolean batchMode) throws SQLException,SQLWarning {
34 this.batchMode = batchMode;
35 theme = thm;
36 valuesInsert = values;
37
38 fileInsert = theme.getIdParentFile();
39 fieldsInsert = buildFieldsInsert();
40 lauchQueryInsert(conn);
41 }
42
43 Vector buildFieldsInsert() {
44 int i,j,indexP,iMax,jMax,indexPMax,x,y;
45 Column e;
46 Column v1[];
47 String dataF,typeF;
48 Vector v;
49
50 v = new Vector();
51 v1 = theme.getElements();
52 iMax = v1.length;
53
54
55 for (i=0;i<iMax;i++) {
56 e = v1[i];
57 String fieldTemp = e.getField();
58 dataF = "";
59 typeF = "";
60 if ( e.getFile().equals(fileInsert) ) {
61 dataF = (String)valuesInsert.get(e.getKeyWord());
62 if (dataF==null || dataF.trim().length()==0) {
63
64 v.addElement(fieldTemp+"|*|"+"NULL");
65 }
66 else {
67
68 typeF = e.getType();
69 if (typeF.equals("N")) {
70 v.addElement(fieldTemp+"|*|"+dataF.trim().toUpperCase());
71 }
72 else {
73 if (typeF.equals("C") || typeF.equals("T")) {
74 dataF = Utils.checkSpecialChar(dataF , '\'' , "''" );
75 v.addElement(fieldTemp+"|*|"+"'"+dataF.trim().toUpperCase()+"'");
76 }
77 else v.addElement(fieldTemp+"|*|"+"'"+dataF.trim().toUpperCase()+"'");
78 }
79 }
80 }
81 }
82
83 if (Utils.debug){
84 for (i=0;i<v.size();i++){
85 System.out.println("Field Insert : " + v.elementAt(i));
86 }
87 }
88
89 return v;
90 }
91
92 private void lauchQueryInsert(ConnectionPool pool) throws SQLException,SQLWarning {
93
94 String queryInsert;
95 StringBuffer s;
96
97 int i,j,indexP,iMax,jMax,indexPMax;
98 boolean found ;
99 String chps;
100
101 s = new StringBuffer("");
102 chps = "";
103 s.append("INSERT INTO ");
104 s.append(fileInsert);
105 s.append(" ( ");
106 iMax = fieldsInsert.size();
107 for (i=0;i<iMax;i++) {
108 chps = (String)fieldsInsert.elementAt(i);
109 chps = chps.substring(0,chps.indexOf("|*|"));
110 s.append(chps);
111 s.append(",");
112 }
113 s.delete(s.length()-1,s.length());
114 s.append(" ) VALUES ( ");
115 for (i=0;i<iMax;i++) {
116 chps = (String)fieldsInsert.elementAt(i);
117 chps = chps.substring(chps.indexOf("|*|")+3);
118 s.append(chps);
119 s.append(",");
120 }
121 s.delete(s.length()-1,s.length());
122 s.append(" )");
123
124 Connection conn = null;
125 Statement stmt = null;
126 try {
127 conn = pool.getConnection();
128 stmt = conn.createStatement();
129 int ok = stmt.executeUpdate(s.toString());
130 if (ok==1 && Utils.debug==true) System.out.println("Insert done.... : ");
131 }
132
133 catch(SQLWarning e1) {
134 throw new SQLWarning("We got a SQLWarning when executing this query: "+s.toString()+"\n"+e1.getMessage());
135 }
136
137 catch(SQLException e) {
138 throw new SQLException("We got a SQLException when executing this query: "+s.toString()+"\n"+e.getMessage());
139 }
140 finally {
141 if (stmt!=null) stmt.close();
142 pool.freeConnection(conn);
143 }
144
145
146
147
148
149
150
151
152
153
154
155 }
156
157 private void lauchQueryInsert(Connection conn) throws SQLException,SQLWarning {
158
159 String queryInsert;
160 StringBuffer s;
161
162 int i,j,indexP,iMax,jMax,indexPMax;
163 boolean found ;
164 String chps;
165
166 s = new StringBuffer("");
167 chps = "";
168 s.append("INSERT INTO ");
169 s.append(fileInsert);
170 s.append(" ( ");
171 iMax = fieldsInsert.size();
172 for (i=0;i<iMax;i++) {
173 chps = (String)fieldsInsert.elementAt(i);
174 chps = chps.substring(0,chps.indexOf("|*|"));
175 s.append(chps);
176 s.append(",");
177 }
178 s.delete(s.length()-1,s.length());
179 s.append(" ) VALUES ( ");
180 for (i=0;i<iMax;i++) {
181 chps = (String)fieldsInsert.elementAt(i);
182 chps = chps.substring(chps.indexOf("|*|")+3);
183 s.append(chps);
184 s.append(",");
185 }
186 s.delete(s.length()-1,s.length());
187 s.append(" )");
188
189 Statement stmt = null;
190 try {
191 stmt = conn.createStatement();
192 if (batchMode) {
193 stmt.addBatch(s.toString());
194 System.out.println("Insert added to batch ...");
195 } else {
196 int ok = stmt.executeUpdate(s.toString());
197 if (ok==1) System.out.println("Insert done.... : ");
198 }
199 }
200
201 catch(SQLWarning e1) {
202 throw new SQLWarning("We got a SQLWarning when executing this query: "+s.toString()+"\n"+e1.getMessage());
203 }
204
205 catch(SQLException e) {
206 throw new SQLException("We got a SQLException when executing this query: "+s.toString()+"\n"+e.getMessage());
207 }
208 finally {
209 if (stmt!=null) stmt.close();
210 }
211
212
213
214
215
216
217
218
219
220
221
222 }
223
224 }