hytos / DTI_PID / BaseModel / Project_DB.cs @ 310aeb31
이력 | 보기 | 이력해설 | 다운로드 (20.9 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 |
DataTable dtClone = dt.Clone(); |
233 |
dtClone.Columns["UID"].DataType = typeof(string); |
234 |
foreach (DataRow row in dt.Rows) |
235 |
{ |
236 |
dtClone.ImportRow(row); |
237 |
} |
238 |
dt.Dispose(); |
239 |
dt = dtClone; |
240 |
} |
241 |
connection.Close(); |
242 |
} |
243 |
catch (Exception ex) |
244 |
{ |
245 |
|
246 |
} |
247 |
finally |
248 |
{ |
249 |
connection.Dispose(); |
250 |
} |
251 |
} |
252 |
|
253 |
return dt; |
254 |
} |
255 |
|
256 |
public static DataTable SelectProjectChildSymbol() |
257 |
{ |
258 |
DataTable result = new DataTable(); |
259 |
result.Columns.Add("UID"); |
260 |
result.Columns.Add("Name"); |
261 |
result.Columns.Add("Type"); |
262 |
result.Columns.Add("SPPID_SYMBOL_PATH"); |
263 |
|
264 |
Project_Info projectInfo = Project_Info.GetInstance(); |
265 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
266 |
using (DataTable dt = new DataTable()) |
267 |
{ |
268 |
try |
269 |
{ |
270 |
connection.Open(); |
271 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
272 |
{ |
273 |
cmd.CommandText = string.Format(@" |
274 |
SELECT AdditionalSymbol FROM Symbol"); |
275 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
276 |
dt.Load(dr); |
277 |
List<string> childList = new List<string>(); |
278 |
foreach (DataRow row in dt.Rows) |
279 |
{ |
280 |
if (!string.IsNullOrEmpty((string)row["AdditionalSymbol"])) |
281 |
{ |
282 |
string[] array = row["AdditionalSymbol"].ToString().Split(new char[] { '/' }); |
283 |
foreach (var childString in array) |
284 |
{ |
285 |
childList.Add(childString.Split(new char[] { ',' })[2]); |
286 |
} |
287 |
} |
288 |
|
289 |
} |
290 |
|
291 |
dt.Clear(); |
292 |
cmd.Reset(); |
293 |
cmd.CommandText = string.Format(@" |
294 |
SELECT * FROM {0}", SPPID_SYMBOL_MAPPING_TABLE); |
295 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
296 |
dt.Load(dr); |
297 |
|
298 |
childList = childList.Distinct().ToList(); |
299 |
foreach (var child in childList) |
300 |
{ |
301 |
string mappingPath = string.Empty; |
302 |
DataRow[] rows = dt.Select(string.Format("UID = '{0}'", child)); |
303 |
if (rows.Length == 1) |
304 |
mappingPath = !DBNull.Value.Equals(rows[0]["SPPID_SYMBOL_PATH"]) ? (string)rows[0]["SPPID_SYMBOL_PATH"] : null; |
305 |
|
306 |
DataRow newRow = result.NewRow(); |
307 |
newRow["UID"] = child; |
308 |
newRow["Name"] = child; |
309 |
newRow["Type"] = "Child Symbol"; |
310 |
newRow["SPPID_SYMBOL_PATH"] = mappingPath; |
311 |
result.Rows.Add(newRow); |
312 |
} |
313 |
} |
314 |
connection.Close(); |
315 |
} |
316 |
catch (Exception ex) |
317 |
{ |
318 |
|
319 |
} |
320 |
finally |
321 |
{ |
322 |
connection.Dispose(); |
323 |
} |
324 |
} |
325 |
|
326 |
return result; |
327 |
} |
328 |
|
329 |
public static DataTable SelectProjectLine() |
330 |
{ |
331 |
DataTable dt = new DataTable(); |
332 |
Project_Info projectInfo = Project_Info.GetInstance(); |
333 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
334 |
{ |
335 |
try |
336 |
{ |
337 |
connection.Open(); |
338 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
339 |
{ |
340 |
cmd.CommandText = string.Format(@" |
341 |
SELECT l.UID, l.Name, sp.SPPID_SYMBOL_PATH FROM {0} as l |
342 |
LEFT OUTER JOIN {1} as sp |
343 |
ON l.UID = SP.UID ;", LineTypes_TABLE, SPPID_SYMBOL_MAPPING_TABLE); |
344 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
345 |
dt.Load(dr); |
346 |
} |
347 |
connection.Close(); |
348 |
} |
349 |
catch (Exception ex) |
350 |
{ |
351 |
|
352 |
} |
353 |
finally |
354 |
{ |
355 |
connection.Dispose(); |
356 |
} |
357 |
} |
358 |
|
359 |
return dt; |
360 |
} |
361 |
|
362 |
public static DataTable SelectProjectLineProperties() |
363 |
{ |
364 |
DataTable dt = new DataTable(); |
365 |
Project_Info projectInfo = Project_Info.GetInstance(); |
366 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
367 |
{ |
368 |
try |
369 |
{ |
370 |
connection.Open(); |
371 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
372 |
{ |
373 |
cmd.CommandText = string.Format(@" |
374 |
SELECT lp.UID, lp.DisplayName, sp.SPPID_SYMBOL_PATH, spa.SPPID_ATTRIBUTE |
375 |
FROM {0} as lp |
376 |
LEFT OUTER JOIN {1} as sp |
377 |
ON lp.UID = sp.UID |
378 |
LEFT OUTER JOIN {2} as spa |
379 |
ON lp.UID = spa.UID;", LineProperties_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE); |
380 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
381 |
dt.Load(dr); |
382 |
} |
383 |
connection.Close(); |
384 |
} |
385 |
catch (Exception ex) |
386 |
{ |
387 |
|
388 |
} |
389 |
finally |
390 |
{ |
391 |
connection.Dispose(); |
392 |
} |
393 |
} |
394 |
|
395 |
return dt; |
396 |
} |
397 |
|
398 |
public static DataTable SelectProjectAttribute() |
399 |
{ |
400 |
DataTable dt = new DataTable(); |
401 |
Project_Info projectInfo = Project_Info.GetInstance(); |
402 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
403 |
{ |
404 |
try |
405 |
{ |
406 |
connection.Open(); |
407 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
408 |
{ |
409 |
cmd.CommandText = string.Format(@" |
410 |
SELECT sa.UID, sa.DisplayAttribute, st.TYPE, spa.SPPID_ATTRIBUTE, sp.SPPID_SYMBOL_PATH |
411 |
FROM {1} as sa, {0} as st |
412 |
LEFT OUTER JOIN {2} as sp |
413 |
ON sa.UID = SP.UID |
414 |
LEFT OUTER JOIN {3} as spa |
415 |
ON sa.UID = spa.UID |
416 |
WHERE sa.SymbolType_UID = st.UID;", SymbolType_TABLE, SymbolAttribute_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE); |
417 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
418 |
dt.Load(dr); |
419 |
} |
420 |
connection.Close(); |
421 |
} |
422 |
catch (Exception ex) |
423 |
{ |
424 |
|
425 |
} |
426 |
finally |
427 |
{ |
428 |
connection.Dispose(); |
429 |
} |
430 |
} |
431 |
|
432 |
return dt; |
433 |
} |
434 |
|
435 |
|
436 |
|
437 |
public static bool InsertSymbolMapping(List<Tuple<string, string, string>> datas) |
438 |
{ |
439 |
Project_Info projectInfo = Project_Info.GetInstance(); |
440 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
441 |
{ |
442 |
try |
443 |
{ |
444 |
connection.Open(); |
445 |
using (SQLiteTransaction transaction = connection.BeginTransaction()) |
446 |
{ |
447 |
try |
448 |
{ |
449 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
450 |
{ |
451 |
foreach (var item in datas) |
452 |
{ |
453 |
cmd.Parameters.Clear(); |
454 |
cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, NAME, SPPID_SYMBOL_PATH) VALUES (@UID, @NAME, @SPPID_SYMBOL_PATH)", SPPID_SYMBOL_MAPPING_TABLE); |
455 |
cmd.Parameters.AddWithValue("@UID", item.Item1); |
456 |
cmd.Parameters.AddWithValue("@NAME", item.Item2); |
457 |
cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", item.Item3); |
458 |
cmd.ExecuteNonQuery(); |
459 |
} |
460 |
} |
461 |
transaction.Commit(); |
462 |
connection.Close(); |
463 |
} |
464 |
catch (Exception ex) |
465 |
{ |
466 |
transaction.Rollback(); |
467 |
} |
468 |
finally |
469 |
{ |
470 |
transaction.Dispose(); |
471 |
} |
472 |
} |
473 |
} |
474 |
catch (Exception ex) |
475 |
{ |
476 |
return false; |
477 |
} |
478 |
finally |
479 |
{ |
480 |
connection.Dispose(); |
481 |
} |
482 |
} |
483 |
|
484 |
return true; |
485 |
} |
486 |
|
487 |
public static bool InsertAttributeMapping(List<Tuple<string, string>> datas) |
488 |
{ |
489 |
Project_Info projectInfo = Project_Info.GetInstance(); |
490 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
491 |
{ |
492 |
try |
493 |
{ |
494 |
connection.Open(); |
495 |
using (SQLiteTransaction transaction = connection.BeginTransaction()) |
496 |
{ |
497 |
try |
498 |
{ |
499 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
500 |
{ |
501 |
foreach (var item in datas) |
502 |
{ |
503 |
cmd.Parameters.Clear(); |
504 |
cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, SPPID_ATTRIBUTE) VALUES (@UID, @SPPID_ATTRIBUTE)", SPPID_ATTRIBUTE_MAPPING_TABLE); |
505 |
cmd.Parameters.AddWithValue("@UID", item.Item1); |
506 |
cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", item.Item2); |
507 |
cmd.ExecuteNonQuery(); |
508 |
} |
509 |
} |
510 |
transaction.Commit(); |
511 |
connection.Close(); |
512 |
} |
513 |
catch (Exception ex) |
514 |
{ |
515 |
transaction.Rollback(); |
516 |
} |
517 |
finally |
518 |
{ |
519 |
transaction.Dispose(); |
520 |
} |
521 |
} |
522 |
} |
523 |
catch (Exception ex) |
524 |
{ |
525 |
return false; |
526 |
} |
527 |
finally |
528 |
{ |
529 |
connection.Dispose(); |
530 |
} |
531 |
} |
532 |
return true; |
533 |
} |
534 |
} |
535 |
} |