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