1 | |
package org.jtheque.core.managers.schema; |
2 | |
|
3 | |
import org.jtheque.core.managers.Managers; |
4 | |
import org.jtheque.core.managers.log.ILoggingManager; |
5 | |
import org.jtheque.utils.StringUtils; |
6 | |
import org.jtheque.utils.io.FileUtils; |
7 | |
|
8 | |
import java.io.File; |
9 | |
import java.io.FileInputStream; |
10 | |
import java.io.FileNotFoundException; |
11 | |
import java.nio.channels.FileChannel; |
12 | |
import java.util.ArrayList; |
13 | |
import java.util.Collection; |
14 | |
import java.util.Scanner; |
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
final class HSQLFileReader { |
38 | |
|
39 | |
|
40 | |
|
41 | |
private HSQLFileReader() { |
42 | 0 | super(); |
43 | 0 | } |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public static Collection<Insert> readFile(File file) { |
52 | 0 | Collection<Insert> inserts = new ArrayList<Insert>(100); |
53 | |
|
54 | 0 | FileChannel in = null; |
55 | |
try { |
56 | 0 | in = new FileInputStream(file).getChannel(); |
57 | |
|
58 | 0 | Scanner scanner = new Scanner(in); |
59 | |
|
60 | 0 | while (scanner.hasNextLine()) { |
61 | 0 | String line = scanner.nextLine().trim(); |
62 | |
|
63 | 0 | if (line.startsWith("INSERT INTO")) { |
64 | 0 | inserts.add(readInsert(line)); |
65 | |
} |
66 | 0 | } |
67 | 0 | } catch (FileNotFoundException e) { |
68 | 0 | Managers.getManager(ILoggingManager.class).getLogger(HSQLFileReader.class).error(e); |
69 | |
} finally { |
70 | 0 | FileUtils.close(in); |
71 | 0 | } |
72 | |
|
73 | 0 | return inserts; |
74 | |
} |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
private static Insert readInsert(String line) { |
83 | 0 | Insert insert = new Insert(); |
84 | |
|
85 | 0 | insert.setTable(line.substring(12, line.indexOf(" VALUES("))); |
86 | |
|
87 | 0 | insert.setValues(extractValues(line)); |
88 | |
|
89 | 0 | return insert; |
90 | |
} |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
private static Collection<String> extractValues(String line) { |
99 | 0 | String valuesStr = line.substring(line.indexOf('(') + 1, line.lastIndexOf(')') + 1); |
100 | |
|
101 | 0 | valuesStr = valuesStr.replace(",'',", ",NULL,"); |
102 | |
|
103 | 0 | return parseStrValues(valuesStr); |
104 | |
} |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
private static Collection<String> parseStrValues(String valuesStr) { |
113 | 0 | Collection<String> values = new ArrayList<String>(10); |
114 | |
|
115 | 0 | StringBuilder current = new StringBuilder(100); |
116 | |
|
117 | 0 | boolean surroundStart = false; |
118 | 0 | char last = ' '; |
119 | |
|
120 | 0 | for (char c : valuesStr.toCharArray()) { |
121 | 0 | if (isEndOfValue(surroundStart, c)) { |
122 | 0 | values.add(StringUtils.removeUnicode(current.toString())); |
123 | |
|
124 | 0 | current.setLength(0); |
125 | 0 | } else if (isEscapedChar(last, c)) { |
126 | 0 | current.append('\''); |
127 | 0 | surroundStart ^= true; |
128 | 0 | } else if (c == '\'') { |
129 | 0 | surroundStart ^= true; |
130 | |
} else { |
131 | 0 | current.append(c); |
132 | |
} |
133 | |
|
134 | 0 | last = c; |
135 | |
} |
136 | |
|
137 | 0 | return values; |
138 | |
} |
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
private static boolean isEscapedChar(char last, char c) { |
148 | 0 | return last == '\'' && c == '\''; |
149 | |
} |
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
private static boolean isEndOfValue(boolean surroundStart, char c) { |
159 | 0 | return !surroundStart && (c == ',' || c == ')'); |
160 | |
} |
161 | |
} |