リビジョン | 36 (tree) |
---|---|
日時 | 2013-12-26 15:00:53 |
作者 | y-moriguchi |
Release for version 0.1.0
@@ -95,7 +95,7 @@ | ||
95 | 95 | public void testExprParser006() { |
96 | 96 | okp("count(*)"); |
97 | 97 | okp("rand()"); |
98 | - okp("aaaa(*, 1, 2 + 3)"); | |
98 | + okp("aaaa(A, 1, 2 + 3)"); | |
99 | 99 | } |
100 | 100 | |
101 | 101 | public void testExprParser007() { |
@@ -154,9 +154,9 @@ | ||
154 | 154 | } |
155 | 155 | |
156 | 156 | public void testExprParser106() { |
157 | - eqp("count(*)", "(COUNT *)"); | |
157 | + eqp("count(A)", "(COUNT A)"); | |
158 | 158 | eqp("rand()", "(RAND)"); |
159 | - eqp("aaaa(*, 1, 2 + 3)", "(AAAA * 1 (+ 2 3))"); | |
159 | + eqp("aaaa(A, 1, 2 + 3)", "(AAAA A 1 (+ 2 3))"); | |
160 | 160 | } |
161 | 161 | |
162 | 162 | public void testExprParser107() { |
@@ -20,6 +20,8 @@ | ||
20 | 20 | import java.math.BigDecimal; |
21 | 21 | import java.sql.SQLException; |
22 | 22 | |
23 | +import net.morilib.db.misc.Rational; | |
24 | + | |
23 | 25 | import junit.framework.TestCase; |
24 | 26 | |
25 | 27 | /** |
@@ -65,7 +67,7 @@ | ||
65 | 67 | |
66 | 68 | void eqn(int t) { |
67 | 69 | try { |
68 | - assertEquals(BigDecimal.valueOf(t), lexer.get()); | |
70 | + assertEquals(Rational.valueOf(t), lexer.get()); | |
69 | 71 | lexer.next(); |
70 | 72 | } catch (IOException e) { |
71 | 73 | throw new RuntimeException(e); |
@@ -76,7 +78,7 @@ | ||
76 | 78 | |
77 | 79 | void eqn(String t) { |
78 | 80 | try { |
79 | - assertEquals(new BigDecimal(t), lexer.get()); | |
81 | + assertEquals(Rational.valueOf(new BigDecimal(t)), lexer.get()); | |
80 | 82 | lexer.next(); |
81 | 83 | } catch (IOException e) { |
82 | 84 | throw new RuntimeException(e); |
@@ -87,13 +87,13 @@ | ||
87 | 87 | okp("SELECT 1 FROM b INNER JOIN c ON a = 1"); |
88 | 88 | okp("SELECT 1 FROM b LEFT JOIN c ON a = 1"); |
89 | 89 | okp("SELECT 1 FROM b LEFT OUTER JOIN c ON a = 1"); |
90 | - okp("SELECT 1 FROM b RIGHT JOIN c ON a = 1"); | |
91 | - okp("SELECT 1 FROM b RIGHT OUTER JOIN c ON a = 1"); | |
92 | - okp("SELECT 1 FROM b FULL JOIN c ON a = 1"); | |
93 | - okp("SELECT 1 FROM b FULL OUTER JOIN c ON a = 1"); | |
90 | +// okp("SELECT 1 FROM b RIGHT JOIN c ON a = 1"); | |
91 | +// okp("SELECT 1 FROM b RIGHT OUTER JOIN c ON a = 1"); | |
92 | +// okp("SELECT 1 FROM b FULL JOIN c ON a = 1"); | |
93 | +// okp("SELECT 1 FROM b FULL OUTER JOIN c ON a = 1"); | |
94 | 94 | okp("SELECT 1 FROM b OUTER JOIN c ON a = 1"); |
95 | - okp("SELECT 1 FROM b NATURAL JOIN c"); | |
96 | - okp("SELECT 1 FROM b CROSS JOIN c"); | |
95 | +// okp("SELECT 1 FROM b NATURAL JOIN c"); | |
96 | +// okp("SELECT 1 FROM b CROSS JOIN c"); | |
97 | 97 | } |
98 | 98 | |
99 | 99 | public void testSelectParse003() { |
@@ -166,20 +166,20 @@ | ||
166 | 166 | "(select ((1 . #f)) ((join left-outer B C (= A 1))) #t #f #t #f)"); |
167 | 167 | eqs("SELECT 1 FROM b LEFT OUTER JOIN c ON a = 1", |
168 | 168 | "(select ((1 . #f)) ((join left-outer B C (= A 1))) #t #f #t #f)"); |
169 | - eqs("SELECT 1 FROM b RIGHT JOIN c ON a = 1", | |
170 | - "(select ((1 . #f)) ((join right-outer B C (= A 1))) #t #f #t #f)"); | |
171 | - eqs("SELECT 1 FROM b RIGHT OUTER JOIN c ON a = 1", | |
172 | - "(select ((1 . #f)) ((join right-outer B C (= A 1))) #t #f #t #f)"); | |
173 | - eqs("SELECT 1 FROM b FULL JOIN c ON a = 1", | |
174 | - "(select ((1 . #f)) ((join full-outer B C (= A 1))) #t #f #t #f)"); | |
175 | - eqs("SELECT 1 FROM b FULL OUTER JOIN c ON a = 1", | |
176 | - "(select ((1 . #f)) ((join full-outer B C (= A 1))) #t #f #t #f)"); | |
169 | +// eqs("SELECT 1 FROM b RIGHT JOIN c ON a = 1", | |
170 | +// "(select ((1 . #f)) ((join right-outer B C (= A 1))) #t #f #t #f)"); | |
171 | +// eqs("SELECT 1 FROM b RIGHT OUTER JOIN c ON a = 1", | |
172 | +// "(select ((1 . #f)) ((join right-outer B C (= A 1))) #t #f #t #f)"); | |
173 | +// eqs("SELECT 1 FROM b FULL JOIN c ON a = 1", | |
174 | +// "(select ((1 . #f)) ((join full-outer B C (= A 1))) #t #f #t #f)"); | |
175 | +// eqs("SELECT 1 FROM b FULL OUTER JOIN c ON a = 1", | |
176 | +// "(select ((1 . #f)) ((join full-outer B C (= A 1))) #t #f #t #f)"); | |
177 | 177 | eqs("SELECT 1 FROM b OUTER JOIN c ON a = 1", |
178 | 178 | "(select ((1 . #f)) ((join left-outer B C (= A 1))) #t #f #t #f)"); |
179 | - eqs("SELECT 1 FROM b NATURAL JOIN c", | |
180 | - "(select ((1 . #f)) ((join natural B C)) #t #f #t #f)"); | |
181 | - eqs("SELECT 1 FROM b CROSS JOIN c", | |
182 | - "(select ((1 . #f)) ((join cross B C)) #t #f #t #f)"); | |
179 | +// eqs("SELECT 1 FROM b NATURAL JOIN c", | |
180 | +// "(select ((1 . #f)) ((join natural B C)) #t #f #t #f)"); | |
181 | +// eqs("SELECT 1 FROM b CROSS JOIN c", | |
182 | +// "(select ((1 . #f)) ((join cross B C)) #t #f #t #f)"); | |
183 | 183 | } |
184 | 184 | |
185 | 185 | public void testSelect003() { |
@@ -60,20 +60,20 @@ | ||
60 | 60 | public void testTable101() { |
61 | 61 | eqs("CREATE TABLE A ( NAME VARCHAR )", |
62 | 62 | "CREATE TABLE A (\n" + |
63 | - " NAME VARCHAR\n)"); | |
63 | + " NAME VARCHAR(0)\n)"); | |
64 | 64 | eqs("CREATE TABLE A ( NAME NUMERIC )", |
65 | 65 | "CREATE TABLE A (\n" + |
66 | - " NAME NUMERIC\n)"); | |
66 | + " NAME NUMERIC(0)\n)"); | |
67 | 67 | eqs("CREATE TABLE A ( NAME VARCHAR NOT NULL )", |
68 | 68 | "CREATE TABLE A (\n" + |
69 | - " NAME VARCHAR NOT NULL\n)"); | |
69 | + " NAME VARCHAR(0) NOT NULL\n)"); | |
70 | 70 | eqs("CREATE TABLE A ( NAME NUMERIC PRIMARY KEY )", |
71 | 71 | "CREATE TABLE A (\n" + |
72 | - " NAME NUMERIC NOT NULL PRIMARY KEY\n)"); | |
72 | + " NAME NUMERIC(0) NOT NULL PRIMARY KEY\n)"); | |
73 | 73 | eqs("CREATE TABLE A ( NAME VARCHAR, B NUMERIC )", |
74 | 74 | "CREATE TABLE A (\n" + |
75 | - " NAME VARCHAR\n" + | |
76 | - " B NUMERIC\n)"); | |
75 | + " NAME VARCHAR(0),\n" + | |
76 | + " B NUMERIC(0)\n)"); | |
77 | 77 | } |
78 | 78 | |
79 | 79 | } |
@@ -37,8 +37,8 @@ | ||
37 | 37 | import javax.swing.JTextField; |
38 | 38 | import javax.swing.JToolBar; |
39 | 39 | |
40 | +import net.morilib.db.RePlus; | |
40 | 41 | import net.morilib.db.schema.SqlSchema; |
41 | -import net.morilib.db.sql.RePlus; | |
42 | 42 | |
43 | 43 | public class ZRelagent extends JFrame { |
44 | 44 |
@@ -0,0 +1,500 @@ | ||
1 | +/* | |
2 | + * Copyright 2013 Yuichiro Moriguchi | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package net.morilib.db; | |
17 | + | |
18 | +import java.io.BufferedReader; | |
19 | +import java.io.File; | |
20 | +import java.io.IOException; | |
21 | +import java.io.InputStreamReader; | |
22 | +import java.sql.SQLException; | |
23 | +import java.text.SimpleDateFormat; | |
24 | +import java.util.ArrayList; | |
25 | +import java.util.Collection; | |
26 | +import java.util.EnumSet; | |
27 | +import java.util.LinkedHashMap; | |
28 | +import java.util.List; | |
29 | +import java.util.Map; | |
30 | +import java.util.regex.Matcher; | |
31 | +import java.util.regex.Pattern; | |
32 | + | |
33 | +import net.morilib.db.engine.SqlEngine; | |
34 | +import net.morilib.db.engine.SqlEngineFactory; | |
35 | +import net.morilib.db.fichier.FabriqueDeFichier; | |
36 | +import net.morilib.db.jdbc.RelationsJDBCUtils; | |
37 | +import net.morilib.db.misc.ErrorBundle; | |
38 | +import net.morilib.db.misc.Rational; | |
39 | +import net.morilib.db.misc.SqlResponse; | |
40 | +import net.morilib.db.relations.Relation; | |
41 | +import net.morilib.db.relations.RelationCursor; | |
42 | +import net.morilib.db.relations.RelationTuple; | |
43 | +import net.morilib.db.schema.FileSqlSchema; | |
44 | +import net.morilib.db.schema.HTMLSqlSchema; | |
45 | +import net.morilib.db.schema.MemorySqlSchema; | |
46 | +import net.morilib.db.schema.SqlSchema; | |
47 | +import net.morilib.db.schema.XlsxSqlSchema; | |
48 | +import net.morilib.db.sqlcs.ddl.SqlColumnAttribute; | |
49 | +import net.morilib.db.sqlcs.ddl.SqlColumnDefinition; | |
50 | +import net.morilib.db.sqlcs.ddl.SqlCreateTable; | |
51 | +import net.morilib.db.transact.RelationsTransaction; | |
52 | + | |
53 | +public class RePlus { | |
54 | + | |
55 | + /** | |
56 | + * | |
57 | + */ | |
58 | + public static final String VERSION = "0.1.0"; | |
59 | + | |
60 | + /** | |
61 | + * | |
62 | + */ | |
63 | + public static final int MAJOR_VERSION = 0; | |
64 | + | |
65 | + /** | |
66 | + * | |
67 | + */ | |
68 | + public static final int MINOR_VERSION = 0; | |
69 | + | |
70 | + private static final String PS1 = "rel>"; | |
71 | + private static final String PS2 = "%3d>"; | |
72 | + private static final String DESC_N = "NAME"; | |
73 | + private static final String DESC_T = "TYPE"; | |
74 | + private static final String DESC_A = "ATTRIBUTE"; | |
75 | + | |
76 | + private static final SimpleDateFormat FM1 = | |
77 | + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); | |
78 | + | |
79 | + private static final Pattern PT1 = Pattern.compile( | |
80 | + "show\\p{Space}+tables;*", | |
81 | + Pattern.CASE_INSENSITIVE); | |
82 | + private static final Pattern PT2 = Pattern.compile( | |
83 | + "d(esc(ribe)?)?\\p{Space}+([^\\p{Space};]+|\".*\");*", | |
84 | + Pattern.CASE_INSENSITIVE); | |
85 | + private static final Pattern PT3 = Pattern.compile( | |
86 | + "commit;*", | |
87 | + Pattern.CASE_INSENSITIVE); | |
88 | + private static final Pattern PT4 = Pattern.compile( | |
89 | + "rollback;*", | |
90 | + Pattern.CASE_INSENSITIVE); | |
91 | + | |
92 | + private static void perror(int code, Object... args) { | |
93 | + System.err.println( | |
94 | + ErrorBundle.getDefaultMessage(code, args)); | |
95 | + } | |
96 | + | |
97 | + private static void pout(int code, Object... args) { | |
98 | + System.out.println( | |
99 | + ErrorBundle.getDefaultMessage(code, args)); | |
100 | + } | |
101 | + | |
102 | + private static boolean isquit(String s) { | |
103 | + return (s.equalsIgnoreCase("exit") || | |
104 | + s.equalsIgnoreCase("quit")); | |
105 | + } | |
106 | + | |
107 | + private static void strrep(int x, char c) { | |
108 | + for(int i = 0; i < x; i++) System.out.print(c); | |
109 | + System.out.print(' '); | |
110 | + } | |
111 | + | |
112 | + private static int len(String s) { | |
113 | + Character.UnicodeBlock b; | |
114 | + int r = 0; | |
115 | + char c; | |
116 | + | |
117 | + for(int i = 0; i < s.length(); i++) { | |
118 | + c = s.charAt(i); | |
119 | + b = Character.UnicodeBlock.of(c); | |
120 | + if(b == null) { | |
121 | + r++; | |
122 | + } else if(b.equals(Character.UnicodeBlock.CJK_COMPATIBILITY) || | |
123 | + b.equals(Character.UnicodeBlock.CJK_COMPATIBILITY_FORMS) || | |
124 | + b.equals(Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS) || | |
125 | + b.equals(Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS_SUPPLEMENT) || | |
126 | + b.equals(Character.UnicodeBlock.CJK_RADICALS_SUPPLEMENT) || | |
127 | +// b.equals(Character.UnicodeBlock.CJK_STROKES) || | |
128 | + b.equals(Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION) || | |
129 | + b.equals(Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS) || | |
130 | + b.equals(Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A) || | |
131 | + b.equals(Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B) || | |
132 | + b.equals(Character.UnicodeBlock.KATAKANA) || | |
133 | + b.equals(Character.UnicodeBlock.HIRAGANA) || | |
134 | + b.equals(Character.UnicodeBlock.HANGUL_SYLLABLES)) { | |
135 | + r += 2; | |
136 | + } else { | |
137 | + r++; | |
138 | + } | |
139 | + } | |
140 | + return r; | |
141 | + } | |
142 | + | |
143 | + private static void lft(String s, int l) { | |
144 | + Character.UnicodeBlock b; | |
145 | + int r = 0; | |
146 | + char c; | |
147 | + | |
148 | + for(int i = 0; i < s.length() && r < l; i++) { | |
149 | + c = s.charAt(i); | |
150 | + b = Character.UnicodeBlock.of(c); | |
151 | + if(b == null) { | |
152 | + r++; | |
153 | + } else if(b.equals(Character.UnicodeBlock.CJK_COMPATIBILITY) || | |
154 | + b.equals(Character.UnicodeBlock.CJK_COMPATIBILITY_FORMS) || | |
155 | + b.equals(Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS) || | |
156 | + b.equals(Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS_SUPPLEMENT) || | |
157 | + b.equals(Character.UnicodeBlock.CJK_RADICALS_SUPPLEMENT) || | |
158 | +// b.equals(Character.UnicodeBlock.CJK_STROKES) || | |
159 | + b.equals(Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION) || | |
160 | + b.equals(Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS) || | |
161 | + b.equals(Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A) || | |
162 | + b.equals(Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B) || | |
163 | + b.equals(Character.UnicodeBlock.KATAKANA) || | |
164 | + b.equals(Character.UnicodeBlock.HIRAGANA) || | |
165 | + b.equals(Character.UnicodeBlock.HANGUL_SYLLABLES)) { | |
166 | + r += 2; | |
167 | + } else { | |
168 | + r++; | |
169 | + } | |
170 | + System.out.print(c); | |
171 | + } | |
172 | + strrep(l - r, ' '); | |
173 | + } | |
174 | + | |
175 | + private static void _print( | |
176 | + Object o) throws IOException, SQLException { | |
177 | + List<RelationTuple> l = new ArrayList<RelationTuple>(); | |
178 | + Map<String, Integer> m, r; | |
179 | + RelationCursor c; | |
180 | + RelationTuple t; | |
181 | + Integer n; | |
182 | + String v; | |
183 | + Object q; | |
184 | + int z; | |
185 | + | |
186 | + if(o instanceof Integer) { | |
187 | + pout(99903, ((Number)o).longValue()); | |
188 | + } else if(o instanceof SqlResponse) { | |
189 | + System.out.println(((SqlResponse)o).toString()); | |
190 | + } else if(!(o instanceof Relation)) { | |
191 | + System.out.println(o); | |
192 | + } else if((c = ((Relation)o).iterator()).hasNext()) { | |
193 | + m = new LinkedHashMap<String, Integer>(); | |
194 | + r = new LinkedHashMap<String, Integer>(); | |
195 | + while(c.hasNext()) { | |
196 | + t = c.next(); | |
197 | + for(String s : t.toMap().keySet()) { | |
198 | + q = t.get(s); | |
199 | + if(!(q instanceof Rational)) { | |
200 | + z = 0; | |
201 | + } else if(r.get(s) == null) { | |
202 | + z = ((Rational)q).getScale(); | |
203 | + } else { | |
204 | + z = Math.max(r.get(s), | |
205 | + ((Rational)q).getScale()); | |
206 | + } | |
207 | + z = z < 4 ? z : 4; | |
208 | + r.put(s, z); | |
209 | + } | |
210 | + } | |
211 | + | |
212 | + c = ((Relation)o).iterator(); | |
213 | + while(c.hasNext()) { | |
214 | + t = c.next(); | |
215 | + l.add(t); | |
216 | + for(String s : t.toMap().keySet()) { | |
217 | + q = t.get(s); | |
218 | + if(q instanceof Rational) { | |
219 | + z = r.get(s); | |
220 | + v = ((Rational)q).toBigDecimal(z).toString(); | |
221 | + } else if(q instanceof java.util.Date) { | |
222 | + v = FM1.format((java.util.Date)q); | |
223 | + } else { | |
224 | + v = q.toString(); | |
225 | + } | |
226 | + | |
227 | + z = len(v); | |
228 | + if((n = m.get(s)) == null || z > n.intValue()) { | |
229 | + m.put(s, len(v)); | |
230 | + } | |
231 | + } | |
232 | + } | |
233 | + | |
234 | + for(String s : m.keySet()) { | |
235 | + if(m.get(s) == 0) m.put(s, 5); | |
236 | + } | |
237 | + | |
238 | + for(String s : m.keySet()) { | |
239 | + z = m.get(s); | |
240 | + lft(s, z); | |
241 | + } | |
242 | + System.out.println(); | |
243 | + | |
244 | + for(String s : m.keySet()) { | |
245 | + z = m.get(s); | |
246 | + strrep(z, '-'); | |
247 | + } | |
248 | + System.out.println(); | |
249 | + | |
250 | + for(RelationTuple p : l) { | |
251 | + for(String s : m.keySet()) { | |
252 | + q = p.get(s); | |
253 | + if(q instanceof Rational) { | |
254 | + lft(((Rational)q).toBigDecimal(4).toString(), | |
255 | + m.get(s)); | |
256 | + } else if(q instanceof java.util.Date) { | |
257 | + lft(FM1.format((java.util.Date)q), m.get(s)); | |
258 | + } else { | |
259 | + lft(q.toString(), m.get(s)); | |
260 | + } | |
261 | + } | |
262 | + System.out.println(); | |
263 | + } | |
264 | + System.out.println(); | |
265 | + } else { | |
266 | + pout(99907); | |
267 | + } | |
268 | + } | |
269 | + | |
270 | + private static void _showtables(Collection<String> c) { | |
271 | + int m = -1; | |
272 | + | |
273 | + if(c.isEmpty()) { | |
274 | + pout(99908); | |
275 | + } else { | |
276 | + for(String s : c) { | |
277 | + m = s.length() > m ? s.length() : m; | |
278 | + } | |
279 | + System.out.println("NAME"); | |
280 | + strrep(m, '-'); | |
281 | + System.out.println(); | |
282 | + for(String s : c){ | |
283 | + System.out.println(s.toUpperCase()); | |
284 | + } | |
285 | + System.out.println(); | |
286 | + } | |
287 | + } | |
288 | + | |
289 | + private static String attrtostr(EnumSet<SqlColumnAttribute> a) { | |
290 | + StringBuffer b = new StringBuffer(); | |
291 | + String d = ""; | |
292 | + | |
293 | + for(SqlColumnAttribute x : a) { | |
294 | + b.append(d).append(x.toString()); | |
295 | + d = ","; | |
296 | + } | |
297 | + return b.toString(); | |
298 | + } | |
299 | + | |
300 | + static void _desc(SqlSchema f, | |
301 | + String name) throws IOException, SQLException { | |
302 | + SqlCreateTable c; | |
303 | + List<SqlColumnDefinition> l; | |
304 | + int l1, l2, l3, j; | |
305 | + | |
306 | + c = f.getCreateTable(name); | |
307 | + l = c.getColumnDefinitions(); | |
308 | + l1 = DESC_N.length(); | |
309 | + l2 = DESC_T.length(); | |
310 | + l3 = DESC_A.length(); | |
311 | + for(SqlColumnDefinition x : l) { | |
312 | + j = len(x.getName().toUpperCase()); | |
313 | + l1 = j > l1 ? j : l1; | |
314 | + j = len(x.getType().toString()); | |
315 | + l2 = j > l2 ? j : l2; | |
316 | + j = len(attrtostr(x.getAttributes())); | |
317 | + l3 = j > l3 ? j : l3; | |
318 | + } | |
319 | + | |
320 | + lft(DESC_N, l1); System.out.print(' '); | |
321 | + lft(DESC_T, l2); System.out.print(' '); | |
322 | + lft(DESC_A, l3); System.out.println(); | |
323 | + strrep(l1, '-'); System.out.print(' '); | |
324 | + strrep(l2, '-'); System.out.print(' '); | |
325 | + strrep(l3, '-'); System.out.println(); | |
326 | + | |
327 | + for(SqlColumnDefinition x : l) { | |
328 | + lft(x.getName().toUpperCase(), l1); System.out.print(' '); | |
329 | + lft(x.getType().toString(), l2); System.out.print(' '); | |
330 | + lft(attrtostr(x.getAttributes()), l3); | |
331 | + System.out.println(); | |
332 | + } | |
333 | + System.out.println(); | |
334 | + } | |
335 | + | |
336 | + /** | |
337 | + * | |
338 | + * @param f | |
339 | + * @param args | |
340 | + * @return | |
341 | + * @throws IOException | |
342 | + * @throws SQLException | |
343 | + */ | |
344 | + public static SqlSchema findSchema(File f, int k, | |
345 | + String... args) throws IOException, SQLException { | |
346 | + FabriqueDeFichier fb = FabriqueDeFichier.getDefault(); | |
347 | + SqlSchema fs = null; | |
348 | + | |
349 | + if(f.isFile() && f.getName().matches( | |
350 | + ".*\\.[Cc][Ss][Vv]")) { | |
351 | + fs = MemorySqlSchema.readCSVs(k, args); | |
352 | + } else if(f.isFile() && f.getName().matches( | |
353 | + ".*\\.[Hh][Tt][Mm][Ll]?")) { | |
354 | + fs = new HTMLSqlSchema(fb.newInstance(f)); | |
355 | + } else if(f.isFile() && f.getName().matches( | |
356 | + ".*\\.[Xx][Ll][Ss][Xx]?")) { | |
357 | + fs = new XlsxSqlSchema(fb.newInstance(f)); | |
358 | + } else if(f.isDirectory()) { | |
359 | + fs = new FileSqlSchema(fb, fb.newInstance(f)); | |
360 | + } | |
361 | + return fs; | |
362 | + } | |
363 | + | |
364 | + private static void putex(boolean dg, SQLException e) { | |
365 | + if(dg) e.printStackTrace(); | |
366 | + System.err.format( | |
367 | + "ERROR REL-%d: %s\n", | |
368 | + e.getErrorCode(), | |
369 | + e.getMessage()); | |
370 | + } | |
371 | + | |
372 | + /** | |
373 | + * | |
374 | + * @param args | |
375 | + */ | |
376 | + public static void main(String[] args) { | |
377 | + StringBuffer b = new StringBuffer(); | |
378 | + final boolean[] dg = new boolean[1]; | |
379 | + boolean tf = true; | |
380 | + final SqlEngine en; | |
381 | + BufferedReader rd; | |
382 | + SqlSchema fs; | |
383 | + int l, k = 0; | |
384 | + Matcher m; | |
385 | + Object o; | |
386 | + String s; | |
387 | + Thread t; | |
388 | + File f; | |
389 | + | |
390 | + dg[0] = false; | |
391 | + for(;; k++) { | |
392 | + if(args.length <= k) { | |
393 | + break; | |
394 | + } else if(args[k].equals("--transaction")) { | |
395 | + tf = true; | |
396 | + } else if(args[k].equals("--autocommit")) { | |
397 | + tf = false; | |
398 | + } else if(args[k].equals("--debug")) { | |
399 | + dg[0] = true; | |
400 | + RelationsJDBCUtils.logger = RelationsJDBCUtils.DEBUG; | |
401 | + } else { | |
402 | + break; | |
403 | + } | |
404 | + } | |
405 | + | |
406 | + if(args.length > k) { | |
407 | + f = new File(args[k]); | |
408 | + } else { | |
409 | + perror(99901, "(unspecified)"); | |
410 | + System.exit(86); | |
411 | + return; | |
412 | + } | |
413 | + | |
414 | + try { | |
415 | + if((fs = findSchema(f, k, args)) == null) { | |
416 | + perror(99902, f.toString()); | |
417 | + System.exit(86); | |
418 | + return; | |
419 | + } | |
420 | + | |
421 | + if(tf) { | |
422 | + en = new RelationsTransaction(fs); | |
423 | + } else { | |
424 | + en = SqlEngineFactory.getInstance().getEngine(fs); | |
425 | + } | |
426 | + | |
427 | + Runtime.getRuntime().addShutdownHook(t = new Thread() { | |
428 | + | |
429 | + public void run() { | |
430 | + try { | |
431 | + pout(99906); | |
432 | + en.rollback(); | |
433 | + } catch(IOException e) { | |
434 | + throw new RuntimeException(e); | |
435 | + } catch(SQLException e) { | |
436 | + putex(dg[0], e); | |
437 | + } | |
438 | + } | |
439 | + | |
440 | + }); | |
441 | + | |
442 | + rd = new BufferedReader(new InputStreamReader(System.in)); | |
443 | + System.out.print(PS1); l = 1; | |
444 | + while((s = rd.readLine()) != null) { | |
445 | + try { | |
446 | + if(b.length() == 0 && isquit(s)) { | |
447 | + Runtime.getRuntime().removeShutdownHook(t); | |
448 | + en.commit(); | |
449 | + System.exit(0); | |
450 | + } else if(b.length() == 0 && | |
451 | + PT1.matcher(s).matches()) { | |
452 | + _showtables(fs.getTableNames()); | |
453 | + b = new StringBuffer(); l = 1; | |
454 | + System.out.print(PS1); | |
455 | + } else if(b.length() == 0 && | |
456 | + (m = PT2.matcher(s)).matches()) { | |
457 | + s = m.group(3); | |
458 | + s = s.replaceFirst("^\"", ""); | |
459 | + s = s.replaceFirst("\"$", ""); | |
460 | + _desc(fs, s); | |
461 | + b = new StringBuffer(); l = 1; | |
462 | + System.out.print(PS1); | |
463 | + } else if(b.length() == 0 && | |
464 | + PT3.matcher(s).matches()) { | |
465 | + en.commit(); | |
466 | + pout(99904); | |
467 | + System.out.print(PS1); | |
468 | + } else if(b.length() == 0 && | |
469 | + PT4.matcher(s).matches()) { | |
470 | + en.rollback(); | |
471 | + pout(99905); | |
472 | + System.out.print(PS1); | |
473 | + } else if(s.length() == 0) { | |
474 | + b = new StringBuffer(); l = 1; | |
475 | + System.out.print(PS1); | |
476 | + } else if(!s.endsWith(";")) { | |
477 | + b.append(s).append('\n'); | |
478 | + System.out.format(PS2, ++l); | |
479 | + } else { | |
480 | + b.append(s).deleteCharAt(b.length() - 1); | |
481 | + o = en.execute(b.toString()); | |
482 | + _print(o); | |
483 | + b = new StringBuffer(); l = 1; | |
484 | + System.out.print(PS1); | |
485 | + } | |
486 | + } catch(SQLException e) { | |
487 | + putex(dg[0], e); | |
488 | + b = new StringBuffer(); l = 1; | |
489 | + System.out.print(PS1); | |
490 | + } | |
491 | + } | |
492 | + System.exit(0); | |
493 | + } catch(IOException e) { | |
494 | + throw new RuntimeException(e); | |
495 | + } catch(SQLException e) { | |
496 | + throw new RuntimeException(e); | |
497 | + } | |
498 | + } | |
499 | + | |
500 | +} |
@@ -1,500 +0,0 @@ | ||
1 | -/* | |
2 | - * Copyright 2013 Yuichiro Moriguchi | |
3 | - * | |
4 | - * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | - * you may not use this file except in compliance with the License. | |
6 | - * You may obtain a copy of the License at | |
7 | - * | |
8 | - * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | - * | |
10 | - * Unless required by applicable law or agreed to in writing, software | |
11 | - * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | - * See the License for the specific language governing permissions and | |
14 | - * limitations under the License. | |
15 | - */ | |
16 | -package net.morilib.db.sql; | |
17 | - | |
18 | -import java.io.BufferedReader; | |
19 | -import java.io.File; | |
20 | -import java.io.IOException; | |
21 | -import java.io.InputStreamReader; | |
22 | -import java.sql.SQLException; | |
23 | -import java.text.SimpleDateFormat; | |
24 | -import java.util.ArrayList; | |
25 | -import java.util.Collection; | |
26 | -import java.util.EnumSet; | |
27 | -import java.util.LinkedHashMap; | |
28 | -import java.util.List; | |
29 | -import java.util.Map; | |
30 | -import java.util.regex.Matcher; | |
31 | -import java.util.regex.Pattern; | |
32 | - | |
33 | -import net.morilib.db.engine.SqlEngine; | |
34 | -import net.morilib.db.engine.SqlEngineFactory; | |
35 | -import net.morilib.db.fichier.FabriqueDeFichier; | |
36 | -import net.morilib.db.jdbc.RelationsJDBCUtils; | |
37 | -import net.morilib.db.misc.ErrorBundle; | |
38 | -import net.morilib.db.misc.Rational; | |
39 | -import net.morilib.db.misc.SqlResponse; | |
40 | -import net.morilib.db.relations.Relation; | |
41 | -import net.morilib.db.relations.RelationCursor; | |
42 | -import net.morilib.db.relations.RelationTuple; | |
43 | -import net.morilib.db.schema.FileSqlSchema; | |
44 | -import net.morilib.db.schema.HTMLSqlSchema; | |
45 | -import net.morilib.db.schema.MemorySqlSchema; | |
46 | -import net.morilib.db.schema.SqlSchema; | |
47 | -import net.morilib.db.schema.XlsxSqlSchema; | |
48 | -import net.morilib.db.sqlcs.ddl.SqlColumnAttribute; | |
49 | -import net.morilib.db.sqlcs.ddl.SqlColumnDefinition; | |
50 | -import net.morilib.db.sqlcs.ddl.SqlCreateTable; | |
51 | -import net.morilib.db.transact.RelationsTransaction; | |
52 | - | |
53 | -public class RePlus { | |
54 | - | |
55 | - /** | |
56 | - * | |
57 | - */ | |
58 | - public static final String VERSION = "0.0.2"; | |
59 | - | |
60 | - /** | |
61 | - * | |
62 | - */ | |
63 | - public static final int MAJOR_VERSION = 0; | |
64 | - | |
65 | - /** | |
66 | - * | |
67 | - */ | |
68 | - public static final int MINOR_VERSION = 0; | |
69 | - | |
70 | - private static final String PS1 = "rel>"; | |
71 | - private static final String PS2 = "%3d>"; | |
72 | - private static final String DESC_N = "NAME"; | |
73 | - private static final String DESC_T = "TYPE"; | |
74 | - private static final String DESC_A = "ATTRIBUTE"; | |
75 | - | |
76 | - private static final SimpleDateFormat FM1 = | |
77 | - new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); | |
78 | - | |
79 | - private static final Pattern PT1 = Pattern.compile( | |
80 | - "show\\p{Space}+tables;*", | |
81 | - Pattern.CASE_INSENSITIVE); | |
82 | - private static final Pattern PT2 = Pattern.compile( | |
83 | - "d(esc(ribe)?)?\\p{Space}+([^\\p{Space};]+|\".*\");*", | |
84 | - Pattern.CASE_INSENSITIVE); | |
85 | - private static final Pattern PT3 = Pattern.compile( | |
86 | - "commit;*", | |
87 | - Pattern.CASE_INSENSITIVE); | |
88 | - private static final Pattern PT4 = Pattern.compile( | |
89 | - "rollback;*", | |
90 | - Pattern.CASE_INSENSITIVE); | |
91 | - | |
92 | - private static void perror(int code, Object... args) { | |
93 | - System.err.println( | |
94 | - ErrorBundle.getDefaultMessage(code, args)); | |
95 | - } | |
96 | - | |
97 | - private static void pout(int code, Object... args) { | |
98 | - System.out.println( | |
99 | - ErrorBundle.getDefaultMessage(code, args)); | |
100 | - } | |
101 | - | |
102 | - private static boolean isquit(String s) { | |
103 | - return (s.equalsIgnoreCase("exit") || | |
104 | - s.equalsIgnoreCase("quit")); | |
105 | - } | |
106 | - | |
107 | - private static void strrep(int x, char c) { | |
108 | - for(int i = 0; i < x; i++) System.out.print(c); | |
109 | - System.out.print(' '); | |
110 | - } | |
111 | - | |
112 | - private static int len(String s) { | |
113 | - Character.UnicodeBlock b; | |
114 | - int r = 0; | |
115 | - char c; | |
116 | - | |
117 | - for(int i = 0; i < s.length(); i++) { | |
118 | - c = s.charAt(i); | |
119 | - b = Character.UnicodeBlock.of(c); | |
120 | - if(b == null) { | |
121 | - r++; | |
122 | - } else if(b.equals(Character.UnicodeBlock.CJK_COMPATIBILITY) || | |
123 | - b.equals(Character.UnicodeBlock.CJK_COMPATIBILITY_FORMS) || | |
124 | - b.equals(Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS) || | |
125 | - b.equals(Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS_SUPPLEMENT) || | |
126 | - b.equals(Character.UnicodeBlock.CJK_RADICALS_SUPPLEMENT) || | |
127 | -// b.equals(Character.UnicodeBlock.CJK_STROKES) || | |
128 | - b.equals(Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION) || | |
129 | - b.equals(Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS) || | |
130 | - b.equals(Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A) || | |
131 | - b.equals(Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B) || | |
132 | - b.equals(Character.UnicodeBlock.KATAKANA) || | |
133 | - b.equals(Character.UnicodeBlock.HIRAGANA) || | |
134 | - b.equals(Character.UnicodeBlock.HANGUL_SYLLABLES)) { | |
135 | - r += 2; | |
136 | - } else { | |
137 | - r++; | |
138 | - } | |
139 | - } | |
140 | - return r; | |
141 | - } | |
142 | - | |
143 | - private static void lft(String s, int l) { | |
144 | - Character.UnicodeBlock b; | |
145 | - int r = 0; | |
146 | - char c; | |
147 | - | |
148 | - for(int i = 0; i < s.length() && r < l; i++) { | |
149 | - c = s.charAt(i); | |
150 | - b = Character.UnicodeBlock.of(c); | |
151 | - if(b == null) { | |
152 | - r++; | |
153 | - } else if(b.equals(Character.UnicodeBlock.CJK_COMPATIBILITY) || | |
154 | - b.equals(Character.UnicodeBlock.CJK_COMPATIBILITY_FORMS) || | |
155 | - b.equals(Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS) || | |
156 | - b.equals(Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS_SUPPLEMENT) || | |
157 | - b.equals(Character.UnicodeBlock.CJK_RADICALS_SUPPLEMENT) || | |
158 | -// b.equals(Character.UnicodeBlock.CJK_STROKES) || | |
159 | - b.equals(Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION) || | |
160 | - b.equals(Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS) || | |
161 | - b.equals(Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A) || | |
162 | - b.equals(Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B) || | |
163 | - b.equals(Character.UnicodeBlock.KATAKANA) || | |
164 | - b.equals(Character.UnicodeBlock.HIRAGANA) || | |
165 | - b.equals(Character.UnicodeBlock.HANGUL_SYLLABLES)) { | |
166 | - r += 2; | |
167 | - } else { | |
168 | - r++; | |
169 | - } | |
170 | - System.out.print(c); | |
171 | - } | |
172 | - strrep(l - r, ' '); | |
173 | - } | |
174 | - | |
175 | - private static void _print( | |
176 | - Object o) throws IOException, SQLException { | |
177 | - List<RelationTuple> l = new ArrayList<RelationTuple>(); | |
178 | - Map<String, Integer> m, r; | |
179 | - RelationCursor c; | |
180 | - RelationTuple t; | |
181 | - Integer n; | |
182 | - String v; | |
183 | - Object q; | |
184 | - int z; | |
185 | - | |
186 | - if(o instanceof Integer) { | |
187 | - pout(99903, ((Number)o).longValue()); | |
188 | - } else if(o instanceof SqlResponse) { | |
189 | - System.out.println(((SqlResponse)o).toString()); | |
190 | - } else if(!(o instanceof Relation)) { | |
191 | - System.out.println(o); | |
192 | - } else if((c = ((Relation)o).iterator()).hasNext()) { | |
193 | - m = new LinkedHashMap<String, Integer>(); | |
194 | - r = new LinkedHashMap<String, Integer>(); | |
195 | - while(c.hasNext()) { | |
196 | - t = c.next(); | |
197 | - for(String s : t.toMap().keySet()) { | |
198 | - q = t.get(s); | |
199 | - if(!(q instanceof Rational)) { | |
200 | - z = 0; | |
201 | - } else if(r.get(s) == null) { | |
202 | - z = ((Rational)q).getScale(); | |
203 | - } else { | |
204 | - z = Math.max(r.get(s), | |
205 | - ((Rational)q).getScale()); | |
206 | - } | |
207 | - z = z < 4 ? z : 4; | |
208 | - r.put(s, z); | |
209 | - } | |
210 | - } | |
211 | - | |
212 | - c = ((Relation)o).iterator(); | |
213 | - while(c.hasNext()) { | |
214 | - t = c.next(); | |
215 | - l.add(t); | |
216 | - for(String s : t.toMap().keySet()) { | |
217 | - q = t.get(s); | |
218 | - if(q instanceof Rational) { | |
219 | - z = r.get(s); | |
220 | - v = ((Rational)q).toBigDecimal(z).toString(); | |
221 | - } else if(q instanceof java.util.Date) { | |
222 | - v = FM1.format((java.util.Date)q); | |
223 | - } else { | |
224 | - v = q.toString(); | |
225 | - } | |
226 | - | |
227 | - z = len(v); | |
228 | - if((n = m.get(s)) == null || z > n.intValue()) { | |
229 | - m.put(s, len(v)); | |
230 | - } | |
231 | - } | |
232 | - } | |
233 | - | |
234 | - for(String s : m.keySet()) { | |
235 | - if(m.get(s) == 0) m.put(s, 5); | |
236 | - } | |
237 | - | |
238 | - for(String s : m.keySet()) { | |
239 | - z = m.get(s); | |
240 | - lft(s, z); | |
241 | - } | |
242 | - System.out.println(); | |
243 | - | |
244 | - for(String s : m.keySet()) { | |
245 | - z = m.get(s); | |
246 | - strrep(z, '-'); | |
247 | - } | |
248 | - System.out.println(); | |
249 | - | |
250 | - for(RelationTuple p : l) { | |
251 | - for(String s : m.keySet()) { | |
252 | - q = p.get(s); | |
253 | - if(q instanceof Rational) { | |
254 | - lft(((Rational)q).toBigDecimal(4).toString(), | |
255 | - m.get(s)); | |
256 | - } else if(q instanceof java.util.Date) { | |
257 | - lft(FM1.format((java.util.Date)q), m.get(s)); | |
258 | - } else { | |
259 | - lft(q.toString(), m.get(s)); | |
260 | - } | |
261 | - } | |
262 | - System.out.println(); | |
263 | - } | |
264 | - System.out.println(); | |
265 | - } else { | |
266 | - pout(99907); | |
267 | - } | |
268 | - } | |
269 | - | |
270 | - private static void _showtables(Collection<String> c) { | |
271 | - int m = -1; | |
272 | - | |
273 | - if(c.isEmpty()) { | |
274 | - pout(99908); | |
275 | - } else { | |
276 | - for(String s : c) { | |
277 | - m = s.length() > m ? s.length() : m; | |
278 | - } | |
279 | - System.out.println("NAME"); | |
280 | - strrep(m, '-'); | |
281 | - System.out.println(); | |
282 | - for(String s : c){ | |
283 | - System.out.println(s.toUpperCase()); | |
284 | - } | |
285 | - System.out.println(); | |
286 | - } | |
287 | - } | |
288 | - | |
289 | - private static String attrtostr(EnumSet<SqlColumnAttribute> a) { | |
290 | - StringBuffer b = new StringBuffer(); | |
291 | - String d = ""; | |
292 | - | |
293 | - for(SqlColumnAttribute x : a) { | |
294 | - b.append(d).append(x.toString()); | |
295 | - d = ","; | |
296 | - } | |
297 | - return b.toString(); | |
298 | - } | |
299 | - | |
300 | - static void _desc(SqlSchema f, | |
301 | - String name) throws IOException, SQLException { | |
302 | - SqlCreateTable c; | |
303 | - List<SqlColumnDefinition> l; | |
304 | - int l1, l2, l3, j; | |
305 | - | |
306 | - c = f.getCreateTable(name); | |
307 | - l = c.getColumnDefinitions(); | |
308 | - l1 = DESC_N.length(); | |
309 | - l2 = DESC_T.length(); | |
310 | - l3 = DESC_A.length(); | |
311 | - for(SqlColumnDefinition x : l) { | |
312 | - j = len(x.getName().toUpperCase()); | |
313 | - l1 = j > l1 ? j : l1; | |
314 | - j = len(x.getType().toString()); | |
315 | - l2 = j > l2 ? j : l2; | |
316 | - j = len(attrtostr(x.getAttributes())); | |
317 | - l3 = j > l3 ? j : l3; | |
318 | - } | |
319 | - | |
320 | - lft(DESC_N, l1); System.out.print(' '); | |
321 | - lft(DESC_T, l2); System.out.print(' '); | |
322 | - lft(DESC_A, l3); System.out.println(); | |
323 | - strrep(l1, '-'); System.out.print(' '); | |
324 | - strrep(l2, '-'); System.out.print(' '); | |
325 | - strrep(l3, '-'); System.out.println(); | |
326 | - | |
327 | - for(SqlColumnDefinition x : l) { | |
328 | - lft(x.getName().toUpperCase(), l1); System.out.print(' '); | |
329 | - lft(x.getType().toString(), l2); System.out.print(' '); | |
330 | - lft(attrtostr(x.getAttributes()), l3); | |
331 | - System.out.println(); | |
332 | - } | |
333 | - System.out.println(); | |
334 | - } | |
335 | - | |
336 | - /** | |
337 | - * | |
338 | - * @param f | |
339 | - * @param args | |
340 | - * @return | |
341 | - * @throws IOException | |
342 | - * @throws SQLException | |
343 | - */ | |
344 | - public static SqlSchema findSchema(File f, int k, | |
345 | - String... args) throws IOException, SQLException { | |
346 | - FabriqueDeFichier fb = FabriqueDeFichier.getDefault(); | |
347 | - SqlSchema fs = null; | |
348 | - | |
349 | - if(f.isFile() && f.getName().matches( | |
350 | - ".*\\.[Cc][Ss][Vv]")) { | |
351 | - fs = MemorySqlSchema.readCSVs(k, args); | |
352 | - } else if(f.isFile() && f.getName().matches( | |
353 | - ".*\\.[Hh][Tt][Mm][Ll]?")) { | |
354 | - fs = new HTMLSqlSchema(fb.newInstance(f)); | |
355 | - } else if(f.isFile() && f.getName().matches( | |
356 | - ".*\\.[Xx][Ll][Ss][Xx]?")) { | |
357 | - fs = new XlsxSqlSchema(fb.newInstance(f)); | |
358 | - } else if(f.isDirectory()) { | |
359 | - fs = new FileSqlSchema(fb, fb.newInstance(f)); | |
360 | - } | |
361 | - return fs; | |
362 | - } | |
363 | - | |
364 | - private static void putex(boolean dg, SQLException e) { | |
365 | - if(dg) e.printStackTrace(); | |
366 | - System.err.format( | |
367 | - "ERROR REL-%d: %s\n", | |
368 | - e.getErrorCode(), | |
369 | - e.getMessage()); | |
370 | - } | |
371 | - | |
372 | - /** | |
373 | - * | |
374 | - * @param args | |
375 | - */ | |
376 | - public static void main(String[] args) { | |
377 | - StringBuffer b = new StringBuffer(); | |
378 | - final boolean[] dg = new boolean[1]; | |
379 | - boolean tf = true; | |
380 | - final SqlEngine en; | |
381 | - BufferedReader rd; | |
382 | - SqlSchema fs; | |
383 | - int l, k = 0; | |
384 | - Matcher m; | |
385 | - Object o; | |
386 | - String s; | |
387 | - Thread t; | |
388 | - File f; | |
389 | - | |
390 | - dg[0] = false; | |
391 | - for(;; k++) { | |
392 | - if(args.length <= k) { | |
393 | - break; | |
394 | - } else if(args[k].equals("--transaction")) { | |
395 | - tf = true; | |
396 | - } else if(args[k].equals("--autocommit")) { | |
397 | - tf = false; | |
398 | - } else if(args[k].equals("--debug")) { | |
399 | - dg[0] = true; | |
400 | - RelationsJDBCUtils.logger = RelationsJDBCUtils.DEBUG; | |
401 | - } else { | |
402 | - break; | |
403 | - } | |
404 | - } | |
405 | - | |
406 | - if(args.length > k) { | |
407 | - f = new File(args[k]); | |
408 | - } else { | |
409 | - perror(99901, "(unspecified)"); | |
410 | - System.exit(86); | |
411 | - return; | |
412 | - } | |
413 | - | |
414 | - try { | |
415 | - if((fs = findSchema(f, k, args)) == null) { | |
416 | - perror(99902, f.toString()); | |
417 | - System.exit(86); | |
418 | - return; | |
419 | - } | |
420 | - | |
421 | - if(tf) { | |
422 | - en = new RelationsTransaction(fs); | |
423 | - } else { | |
424 | - en = SqlEngineFactory.getInstance().getEngine(fs); | |
425 | - } | |
426 | - | |
427 | - Runtime.getRuntime().addShutdownHook(t = new Thread() { | |
428 | - | |
429 | - public void run() { | |
430 | - try { | |
431 | - pout(99906); | |
432 | - en.rollback(); | |
433 | - } catch(IOException e) { | |
434 | - throw new RuntimeException(e); | |
435 | - } catch(SQLException e) { | |
436 | - putex(dg[0], e); | |
437 | - } | |
438 | - } | |
439 | - | |
440 | - }); | |
441 | - | |
442 | - rd = new BufferedReader(new InputStreamReader(System.in)); | |
443 | - System.out.print(PS1); l = 1; | |
444 | - while((s = rd.readLine()) != null) { | |
445 | - try { | |
446 | - if(b.length() == 0 && isquit(s)) { | |
447 | - Runtime.getRuntime().removeShutdownHook(t); | |
448 | - en.commit(); | |
449 | - System.exit(0); | |
450 | - } else if(b.length() == 0 && | |
451 | - PT1.matcher(s).matches()) { | |
452 | - _showtables(fs.getTableNames()); | |
453 | - b = new StringBuffer(); l = 1; | |
454 | - System.out.print(PS1); | |
455 | - } else if(b.length() == 0 && | |
456 | - (m = PT2.matcher(s)).matches()) { | |
457 | - s = m.group(3); | |
458 | - s = s.replaceFirst("^\"", ""); | |
459 | - s = s.replaceFirst("\"$", ""); | |
460 | - _desc(fs, s); | |
461 | - b = new StringBuffer(); l = 1; | |
462 | - System.out.print(PS1); | |
463 | - } else if(b.length() == 0 && | |
464 | - PT3.matcher(s).matches()) { | |
465 | - en.commit(); | |
466 | - pout(99904); | |
467 | - System.out.print(PS1); | |
468 | - } else if(b.length() == 0 && | |
469 | - PT4.matcher(s).matches()) { | |
470 | - en.rollback(); | |
471 | - pout(99905); | |
472 | - System.out.print(PS1); | |
473 | - } else if(s.length() == 0) { | |
474 | - b = new StringBuffer(); l = 1; | |
475 | - System.out.print(PS1); | |
476 | - } else if(!s.endsWith(";")) { | |
477 | - b.append(s).append('\n'); | |
478 | - System.out.format(PS2, ++l); | |
479 | - } else { | |
480 | - b.append(s).deleteCharAt(b.length() - 1); | |
481 | - o = en.execute(b.toString()); | |
482 | - _print(o); | |
483 | - b = new StringBuffer(); l = 1; | |
484 | - System.out.print(PS1); | |
485 | - } | |
486 | - } catch(SQLException e) { | |
487 | - putex(dg[0], e); | |
488 | - b = new StringBuffer(); l = 1; | |
489 | - System.out.print(PS1); | |
490 | - } | |
491 | - } | |
492 | - System.exit(0); | |
493 | - } catch(IOException e) { | |
494 | - throw new RuntimeException(e); | |
495 | - } catch(SQLException e) { | |
496 | - throw new RuntimeException(e); | |
497 | - } | |
498 | - } | |
499 | - | |
500 | -} |
@@ -213,6 +213,8 @@ | ||
213 | 213 | for(int i = 0; i < n.size(); i++) { |
214 | 214 | if(i >= y.length) { |
215 | 215 | // do nothing |
216 | + } else if(y[i] == null || y[i].equals("")) { | |
217 | + // do nothing | |
216 | 218 | } else if((t[i] == null || t[i] instanceof SqlTypeNumeric) && |
217 | 219 | (w = PT1.matcher(y[i])).matches()) { |
218 | 220 | t[i] = getlen((SqlTypeNumeric)t[i], w); |
@@ -9,6 +9,7 @@ | ||
9 | 9 | import java.sql.SQLFeatureNotSupportedException; |
10 | 10 | import java.util.Arrays; |
11 | 11 | |
12 | +import net.morilib.db.RePlus; | |
12 | 13 | import net.morilib.db.expr.RelationFunction; |
13 | 14 | import net.morilib.db.functions.FunctionType; |
14 | 15 | import net.morilib.db.info.ColumnsInfo; |
@@ -18,7 +19,6 @@ | ||
18 | 19 | import net.morilib.db.info.TypeInfoInfo; |
19 | 20 | import net.morilib.db.misc.ErrorBundle; |
20 | 21 | import net.morilib.db.misc.RelationsLogger; |
21 | -import net.morilib.db.sql.RePlus; | |
22 | 22 | |
23 | 23 | public class RelationsDatabaseMetadata implements DatabaseMetaData { |
24 | 24 |