Révision | 8dd566916e71760f4d5ca727c40d3aa245d475ab (tree) |
---|---|
l'heure | 2013-05-25 09:07:14 |
Auteur | argius <argius.net@gmai...> |
Commiter | argius |
refactoring: separate addCommas from generateUpdateOrInsertPhrase (and apply to where phrase)
@@ -96,7 +96,7 @@ final class DatabaseInfoTree extends JTree implements AnyActionListener, TextSea | ||
96 | 96 | } |
97 | 97 | final String phrase = generateEquivalentJoinClause(columnNodes); |
98 | 98 | if (phrase.length() > 0) { |
99 | - insertTextIntoTextArea(phrase); | |
99 | + insertTextIntoTextArea(addCommas(phrase)); | |
100 | 100 | } |
101 | 101 | } else if (ev.isAnyOf(generateSelectPhrase)) { |
102 | 102 | final String phrase = generateSelectPhrase(getSelectionNodes()); |
@@ -109,7 +109,7 @@ final class DatabaseInfoTree extends JTree implements AnyActionListener, TextSea | ||
109 | 109 | final String phrase = generateUpdateOrInsertPhrase(getSelectionNodes(), isInsert); |
110 | 110 | if (phrase.length() > 0) { |
111 | 111 | if (isInsert) { |
112 | - insertTextIntoTextArea(phrase); | |
112 | + insertTextIntoTextArea(addCommas(phrase)); | |
113 | 113 | } else { |
114 | 114 | insertTextIntoTextArea(phrase + " WHERE "); |
115 | 115 | } |
@@ -208,6 +208,19 @@ final class DatabaseInfoTree extends JTree implements AnyActionListener, TextSea | ||
208 | 208 | } |
209 | 209 | } |
210 | 210 | |
211 | + private static String addCommas(String phrase) { | |
212 | + int c = 0; | |
213 | + for (final char ch : phrase.toCharArray()) { | |
214 | + if (ch == '?') { | |
215 | + ++c; | |
216 | + } | |
217 | + } | |
218 | + if (c >= 2) { | |
219 | + return String.format("%s;%s", phrase, join(",", nCopies(c, ""))); | |
220 | + } | |
221 | + return phrase; | |
222 | + } | |
223 | + | |
211 | 224 | static String generateEquivalentJoinClause(List<ColumnNode> nodes) { |
212 | 225 | if (nodes.isEmpty()) { |
213 | 226 | return ""; |
@@ -243,7 +256,7 @@ final class DatabaseInfoTree extends JTree implements AnyActionListener, TextSea | ||
243 | 256 | } |
244 | 257 | expressions.addAll(expressions2); |
245 | 258 | } |
246 | - return String.format("%s;", join(" AND ", expressions)); | |
259 | + return String.format("%s", join(" AND ", expressions)); | |
247 | 260 | } |
248 | 261 | |
249 | 262 | static String generateSelectPhrase(List<TreeNode> nodes) { |
@@ -338,11 +351,10 @@ final class DatabaseInfoTree extends JTree implements AnyActionListener, TextSea | ||
338 | 351 | final String phrase; |
339 | 352 | if (isInsert) { |
340 | 353 | final int columnCount = columnsInTable.size(); |
341 | - phrase = String.format("INSERT INTO %s (%s) VALUES (%s);%s", | |
354 | + phrase = String.format("INSERT INTO %s (%s) VALUES (%s)", | |
342 | 355 | tableName, |
343 | 356 | join(",", columnsInTable), |
344 | - join(",", nCopies(columnCount, "?")), | |
345 | - join(",", nCopies(columnCount, ""))); | |
357 | + join(",", nCopies(columnCount, "?"))); | |
346 | 358 | } else { |
347 | 359 | List<String> columnExpressions = new ArrayList<String>(); |
348 | 360 | for (final String columnName : columnsInTable) { |