hytos / DTI_PID / BaseModel / Project_DB.cs @ edc78461
이력 | 보기 | 이력해설 | 다운로드 (17.4 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Text; |
5 |
using System.Threading.Tasks; |
6 |
using System.Globalization; |
7 |
using System.Data.SQLite; |
8 |
using System.Data; |
9 |
|
10 |
namespace Converter.BaseModel |
11 |
{ |
12 |
public class Project_DB |
13 |
{ |
14 |
const string SPPID_DB_INFO_TABLE = "T_SPPID_CONNECTION_INFO"; |
15 |
const string SPPID_SYMBOL_MAPPING_TABLE = "T_SPPID_SYMBOL_MAPPING"; |
16 |
const string SPPID_ATTRIBUTE_MAPPING_TABLE = "T_SPPID_ATTRIBUTE_MAPPING"; |
17 |
const string SPPID_ETC_SETTING_TABLE = "T_SPPID_ETC_SETTING_TABLE"; |
18 |
|
19 |
const string LineProperties_TABLE = "LineProperties"; |
20 |
const string LineTypes_TABLE = "LineTypes"; |
21 |
const string SymbolType_TABLE = "SymbolType"; |
22 |
const string SymbolAttribute_TABLE = "SymbolAttribute"; |
23 |
const string Symbol_TABLE = "Symbol"; |
24 |
|
25 |
public static bool ConnTestAndCreateTable() |
26 |
{ |
27 |
bool result = false; |
28 |
Project_Info projectInfo = Project_Info.GetInstance(); |
29 |
SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, @"Data Source = {0}", projectInfo.DBFilePath)); |
30 |
try |
31 |
{ |
32 |
connection.Open(); |
33 |
if (connection.State == ConnectionState.Open) |
34 |
{ |
35 |
CreateTable(connection); |
36 |
result = true; |
37 |
} |
38 |
connection.Close(); |
39 |
} |
40 |
catch (Exception ex) |
41 |
{ |
42 |
|
43 |
} |
44 |
finally |
45 |
{ |
46 |
connection.Dispose(); |
47 |
} |
48 |
|
49 |
return result; |
50 |
} |
51 |
|
52 |
public static bool SaveSPPID_DB_INFO(string jsonString) |
53 |
{ |
54 |
Project_Info projectInfo = Project_Info.GetInstance(); |
55 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
56 |
{ |
57 |
|
58 |
try |
59 |
{ |
60 |
connection.Open(); |
61 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
62 |
{ |
63 |
cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_DB_INFO_TABLE); |
64 |
cmd.ExecuteNonQuery(); |
65 |
|
66 |
cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString)", SPPID_DB_INFO_TABLE); |
67 |
cmd.Parameters.AddWithValue("@jsonString", jsonString); |
68 |
cmd.ExecuteNonQuery(); |
69 |
} |
70 |
connection.Close(); |
71 |
} |
72 |
catch (Exception ex) |
73 |
{ |
74 |
return false; |
75 |
} |
76 |
finally |
77 |
{ |
78 |
connection.Dispose(); |
79 |
} |
80 |
} |
81 |
|
82 |
return true; |
83 |
} |
84 |
|
85 |
public static DataTable SelectSPPID_DB_INFO() |
86 |
{ |
87 |
DataTable dt = new DataTable(); |
88 |
Project_Info projectInfo = Project_Info.GetInstance(); |
89 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
90 |
{ |
91 |
try |
92 |
{ |
93 |
connection.Open(); |
94 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
95 |
{ |
96 |
cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DB_INFO_TABLE); |
97 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
98 |
dt.Load(dr); |
99 |
} |
100 |
connection.Close(); |
101 |
} |
102 |
catch (Exception ex) |
103 |
{ |
104 |
|
105 |
} |
106 |
finally |
107 |
{ |
108 |
connection.Dispose(); |
109 |
} |
110 |
} |
111 |
|
112 |
return dt; |
113 |
} |
114 |
|
115 |
public static bool SaveETCSetting(string jsonString) |
116 |
{ |
117 |
Project_Info projectInfo = Project_Info.GetInstance(); |
118 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
119 |
{ |
120 |
|
121 |
try |
122 |
{ |
123 |
connection.Open(); |
124 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
125 |
{ |
126 |
cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_ETC_SETTING_TABLE); |
127 |
cmd.ExecuteNonQuery(); |
128 |
|
129 |
cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString)", SPPID_ETC_SETTING_TABLE); |
130 |
cmd.Parameters.AddWithValue("@jsonString", jsonString); |
131 |
cmd.ExecuteNonQuery(); |
132 |
} |
133 |
connection.Close(); |
134 |
} |
135 |
catch (Exception ex) |
136 |
{ |
137 |
return false; |
138 |
} |
139 |
finally |
140 |
{ |
141 |
connection.Dispose(); |
142 |
} |
143 |
} |
144 |
|
145 |
return true; |
146 |
} |
147 |
|
148 |
public static DataTable SelectETCSetting() |
149 |
{ |
150 |
DataTable dt = new DataTable(); |
151 |
Project_Info projectInfo = Project_Info.GetInstance(); |
152 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
153 |
{ |
154 |
try |
155 |
{ |
156 |
connection.Open(); |
157 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
158 |
{ |
159 |
cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_ETC_SETTING_TABLE); |
160 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
161 |
dt.Load(dr); |
162 |
} |
163 |
connection.Close(); |
164 |
} |
165 |
catch (Exception ex) |
166 |
{ |
167 |
|
168 |
} |
169 |
finally |
170 |
{ |
171 |
connection.Dispose(); |
172 |
} |
173 |
} |
174 |
|
175 |
return dt; |
176 |
} |
177 |
|
178 |
private static void CreateTable(SQLiteConnection connection) |
179 |
{ |
180 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
181 |
{ |
182 |
cmd.CommandText = "SELECT NAME FROM sqlite_master WHERE type='table'"; |
183 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
184 |
using (DataTable dt = new DataTable()) |
185 |
{ |
186 |
dt.Load(dr); |
187 |
|
188 |
if (dt.Select(string.Format("NAME = '{0}'", SPPID_DB_INFO_TABLE)).Length == 0) |
189 |
{ |
190 |
cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString TEXT)", SPPID_DB_INFO_TABLE); |
191 |
cmd.ExecuteNonQuery(); |
192 |
} |
193 |
if (dt.Select(string.Format("NAME = '{0}'", SPPID_ETC_SETTING_TABLE)).Length == 0) |
194 |
{ |
195 |
cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString TEXT)", SPPID_ETC_SETTING_TABLE); |
196 |
cmd.ExecuteNonQuery(); |
197 |
} |
198 |
if (dt.Select(string.Format("NAME = '{0}'", SPPID_SYMBOL_MAPPING_TABLE)).Length == 0) |
199 |
{ |
200 |
cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, NAME TEXT, SPPID_SYMBOL_PATH TEXT)", SPPID_SYMBOL_MAPPING_TABLE); |
201 |
cmd.ExecuteNonQuery(); |
202 |
} |
203 |
if (dt.Select(string.Format("NAME = '{0}'", SPPID_ATTRIBUTE_MAPPING_TABLE)).Length == 0) |
204 |
{ |
205 |
cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, SPPID_ATTRIBUTE TEXT)", SPPID_ATTRIBUTE_MAPPING_TABLE); |
206 |
cmd.ExecuteNonQuery(); |
207 |
} |
208 |
} |
209 |
} |
210 |
} |
211 |
|
212 |
public static DataTable SelectProjectSymbol() |
213 |
{ |
214 |
DataTable dt = new DataTable(); |
215 |
Project_Info projectInfo = Project_Info.GetInstance(); |
216 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
217 |
{ |
218 |
try |
219 |
{ |
220 |
connection.Open(); |
221 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
222 |
{ |
223 |
cmd.CommandText = string.Format(@" |
224 |
SELECT s.UID, s.Name, st.Type, sp.SPPID_SYMBOL_PATH FROM {1} as st, {0} as s |
225 |
LEFT OUTER JOIN {2} as sp |
226 |
ON s.UID = SP.UID |
227 |
WHERE s.SymbolType_UID = st.UID |
228 |
ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, SPPID_SYMBOL_MAPPING_TABLE); |
229 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
230 |
dt.Load(dr); |
231 |
} |
232 |
connection.Close(); |
233 |
} |
234 |
catch (Exception ex) |
235 |
{ |
236 |
|
237 |
} |
238 |
finally |
239 |
{ |
240 |
connection.Dispose(); |
241 |
} |
242 |
} |
243 |
|
244 |
return dt; |
245 |
} |
246 |
|
247 |
public static DataTable SelectProjectLine() |
248 |
{ |
249 |
DataTable dt = new DataTable(); |
250 |
Project_Info projectInfo = Project_Info.GetInstance(); |
251 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
252 |
{ |
253 |
try |
254 |
{ |
255 |
connection.Open(); |
256 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
257 |
{ |
258 |
cmd.CommandText = string.Format(@" |
259 |
SELECT l.UID, l.Name, sp.SPPID_SYMBOL_PATH FROM {0} as l |
260 |
LEFT OUTER JOIN {1} as sp |
261 |
ON l.UID = SP.UID ;", LineTypes_TABLE, SPPID_SYMBOL_MAPPING_TABLE); |
262 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
263 |
dt.Load(dr); |
264 |
} |
265 |
connection.Close(); |
266 |
} |
267 |
catch (Exception ex) |
268 |
{ |
269 |
|
270 |
} |
271 |
finally |
272 |
{ |
273 |
connection.Dispose(); |
274 |
} |
275 |
} |
276 |
|
277 |
return dt; |
278 |
} |
279 |
|
280 |
public static DataTable SelectProjectLineProperties() |
281 |
{ |
282 |
DataTable dt = new DataTable(); |
283 |
Project_Info projectInfo = Project_Info.GetInstance(); |
284 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
285 |
{ |
286 |
try |
287 |
{ |
288 |
connection.Open(); |
289 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
290 |
{ |
291 |
cmd.CommandText = string.Format(@" |
292 |
SELECT lp.UID, lp.DisplayName, sp.SPPID_SYMBOL_PATH, spa.SPPID_ATTRIBUTE |
293 |
FROM {0} as lp |
294 |
LEFT OUTER JOIN {1} as sp |
295 |
ON lp.UID = sp.UID |
296 |
LEFT OUTER JOIN {2} as spa |
297 |
ON lp.UID = spa.UID;", LineProperties_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE); |
298 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
299 |
dt.Load(dr); |
300 |
} |
301 |
connection.Close(); |
302 |
} |
303 |
catch (Exception ex) |
304 |
{ |
305 |
|
306 |
} |
307 |
finally |
308 |
{ |
309 |
connection.Dispose(); |
310 |
} |
311 |
} |
312 |
|
313 |
return dt; |
314 |
} |
315 |
|
316 |
public static DataTable SelectProjectAssociation() |
317 |
{ |
318 |
DataTable dt = new DataTable(); |
319 |
Project_Info projectInfo = Project_Info.GetInstance(); |
320 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
321 |
{ |
322 |
try |
323 |
{ |
324 |
connection.Open(); |
325 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
326 |
{ |
327 |
cmd.CommandText = string.Format(@" |
328 |
SELECT sa.UID, sa.DisplayAttribute, st.TYPE, spa.SPPID_ATTRIBUTE, sp.SPPID_SYMBOL_PATH |
329 |
FROM {1} as sa, {0} as st |
330 |
LEFT OUTER JOIN {2} as sp |
331 |
ON sa.UID = SP.UID |
332 |
LEFT OUTER JOIN {3} as spa |
333 |
ON sa.UID = spa.UID |
334 |
WHERE sa.SymbolType_UID = st.UID;", SymbolType_TABLE, SymbolAttribute_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE); |
335 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
336 |
dt.Load(dr); |
337 |
} |
338 |
connection.Close(); |
339 |
} |
340 |
catch (Exception ex) |
341 |
{ |
342 |
|
343 |
} |
344 |
finally |
345 |
{ |
346 |
connection.Dispose(); |
347 |
} |
348 |
} |
349 |
|
350 |
return dt; |
351 |
} |
352 |
|
353 |
|
354 |
|
355 |
public static bool InsertSymbolMapping(List<Tuple<string, string, string>> datas) |
356 |
{ |
357 |
Project_Info projectInfo = Project_Info.GetInstance(); |
358 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
359 |
{ |
360 |
try |
361 |
{ |
362 |
connection.Open(); |
363 |
using (SQLiteTransaction transaction = connection.BeginTransaction()) |
364 |
{ |
365 |
try |
366 |
{ |
367 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
368 |
{ |
369 |
foreach (var item in datas) |
370 |
{ |
371 |
cmd.Parameters.Clear(); |
372 |
cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, NAME, SPPID_SYMBOL_PATH) VALUES (@UID, @NAME, @SPPID_SYMBOL_PATH)", SPPID_SYMBOL_MAPPING_TABLE); |
373 |
cmd.Parameters.AddWithValue("@UID", item.Item1); |
374 |
cmd.Parameters.AddWithValue("@NAME", item.Item2); |
375 |
cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", item.Item3); |
376 |
cmd.ExecuteNonQuery(); |
377 |
} |
378 |
} |
379 |
transaction.Commit(); |
380 |
connection.Close(); |
381 |
} |
382 |
catch (Exception ex) |
383 |
{ |
384 |
transaction.Rollback(); |
385 |
} |
386 |
finally |
387 |
{ |
388 |
transaction.Dispose(); |
389 |
} |
390 |
} |
391 |
} |
392 |
catch (Exception ex) |
393 |
{ |
394 |
return false; |
395 |
} |
396 |
finally |
397 |
{ |
398 |
connection.Dispose(); |
399 |
} |
400 |
} |
401 |
|
402 |
return true; |
403 |
} |
404 |
|
405 |
public static bool InsertAttributeMapping(List<Tuple<string, string>> datas) |
406 |
{ |
407 |
Project_Info projectInfo = Project_Info.GetInstance(); |
408 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
409 |
{ |
410 |
try |
411 |
{ |
412 |
connection.Open(); |
413 |
using (SQLiteTransaction transaction = connection.BeginTransaction()) |
414 |
{ |
415 |
try |
416 |
{ |
417 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
418 |
{ |
419 |
foreach (var item in datas) |
420 |
{ |
421 |
cmd.Parameters.Clear(); |
422 |
cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, SPPID_ATTRIBUTE) VALUES (@UID, @SPPID_ATTRIBUTE)", SPPID_ATTRIBUTE_MAPPING_TABLE); |
423 |
cmd.Parameters.AddWithValue("@UID", item.Item1); |
424 |
cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", item.Item2); |
425 |
cmd.ExecuteNonQuery(); |
426 |
} |
427 |
} |
428 |
transaction.Commit(); |
429 |
connection.Close(); |
430 |
} |
431 |
catch (Exception ex) |
432 |
{ |
433 |
transaction.Rollback(); |
434 |
} |
435 |
finally |
436 |
{ |
437 |
transaction.Dispose(); |
438 |
} |
439 |
} |
440 |
} |
441 |
catch (Exception ex) |
442 |
{ |
443 |
return false; |
444 |
} |
445 |
finally |
446 |
{ |
447 |
connection.Dispose(); |
448 |
} |
449 |
} |
450 |
return true; |
451 |
} |
452 |
} |
453 |
} |