hytos / DTI_PID / SPPIDConverter / DB / Project_DB.cs @ 7e680366
이력 | 보기 | 이력해설 | 다운로드 (38.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 |
using Newtonsoft.Json; |
10 |
|
11 |
namespace Converter.BaseModel |
12 |
{ |
13 |
public class Project_DB |
14 |
{ |
15 |
const string SPPID_DB_INFO_TABLE = "T_SPPID_CONNECTION_INFO"; |
16 |
const string SPPID_SYMBOL_MAPPING_TABLE = "T_SPPID_SYMBOL_MAPPING"; |
17 |
const string SPPID_ATTRIBUTE_MAPPING_TABLE = "T_SPPID_ATTRIBUTE_MAPPING"; |
18 |
const string SPPID_SETTING_TABLE = "T_SPPID_SETTING_TABLE"; |
19 |
const string SPPID_LABEL_INFO_TABLE = "T_SPPID_LABEL_INFO"; |
20 |
const string SPPID_DRAWING_INFO = "T_SPPID_DRAWING_INFO"; |
21 |
const string SPPID_OPC_INFO = "T_SPPID_OPC_INFO"; |
22 |
|
23 |
const string LineProperties_TABLE = "LineProperties"; |
24 |
const string LineTypes_TABLE = "LineTypes"; |
25 |
const string SymbolType_TABLE = "SymbolType"; |
26 |
const string SymbolAttribute_TABLE = "SymbolAttribute"; |
27 |
const string Symbol_TABLE = "Symbol"; |
28 |
const string OPCRelations_TABLE = "OPCRelations"; |
29 |
|
30 |
public static bool ConnTestAndCreateTable() |
31 |
{ |
32 |
bool result = false; |
33 |
Project_Info projectInfo = Project_Info.GetInstance(); |
34 |
SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, @"Data Source = {0}", projectInfo.DBFilePath)); |
35 |
try |
36 |
{ |
37 |
connection.Open(); |
38 |
if (connection.State == ConnectionState.Open) |
39 |
{ |
40 |
CreateTable(connection); |
41 |
result = true; |
42 |
} |
43 |
connection.Close(); |
44 |
} |
45 |
catch (Exception ex) |
46 |
{ |
47 |
|
48 |
} |
49 |
finally |
50 |
{ |
51 |
connection.Dispose(); |
52 |
} |
53 |
|
54 |
return result; |
55 |
} |
56 |
|
57 |
public static bool SaveSPPID_DB_INFO(string jsonString) |
58 |
{ |
59 |
Project_Info projectInfo = Project_Info.GetInstance(); |
60 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
61 |
{ |
62 |
|
63 |
try |
64 |
{ |
65 |
connection.Open(); |
66 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
67 |
{ |
68 |
cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_DB_INFO_TABLE); |
69 |
cmd.ExecuteNonQuery(); |
70 |
|
71 |
cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString)", SPPID_DB_INFO_TABLE); |
72 |
cmd.Parameters.AddWithValue("@jsonString", jsonString); |
73 |
cmd.ExecuteNonQuery(); |
74 |
} |
75 |
connection.Close(); |
76 |
} |
77 |
catch (Exception ex) |
78 |
{ |
79 |
return false; |
80 |
} |
81 |
finally |
82 |
{ |
83 |
connection.Dispose(); |
84 |
} |
85 |
} |
86 |
|
87 |
return true; |
88 |
} |
89 |
|
90 |
public static DataTable SelectSPPID_DB_INFO() |
91 |
{ |
92 |
DataTable dt = new DataTable(); |
93 |
Project_Info projectInfo = Project_Info.GetInstance(); |
94 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
95 |
{ |
96 |
try |
97 |
{ |
98 |
connection.Open(); |
99 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
100 |
{ |
101 |
cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DB_INFO_TABLE); |
102 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
103 |
dt.Load(dr); |
104 |
} |
105 |
connection.Close(); |
106 |
} |
107 |
catch (Exception ex) |
108 |
{ |
109 |
|
110 |
} |
111 |
finally |
112 |
{ |
113 |
connection.Dispose(); |
114 |
} |
115 |
} |
116 |
|
117 |
return dt; |
118 |
} |
119 |
|
120 |
public static bool SaveETCSetting(Dictionary<string,string> dicSetting) |
121 |
{ |
122 |
Project_Info projectInfo = Project_Info.GetInstance(); |
123 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
124 |
{ |
125 |
|
126 |
try |
127 |
{ |
128 |
connection.Open(); |
129 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
130 |
{ |
131 |
cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_SETTING_TABLE); |
132 |
cmd.ExecuteNonQuery(); |
133 |
|
134 |
foreach (var item in dicSetting) |
135 |
{ |
136 |
cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString, @SettingType)", SPPID_SETTING_TABLE); |
137 |
cmd.Parameters.AddWithValue("@jsonString", item.Value); |
138 |
cmd.Parameters.AddWithValue("@SettingType", item.Key); |
139 |
cmd.ExecuteNonQuery(); |
140 |
} |
141 |
} |
142 |
connection.Close(); |
143 |
} |
144 |
catch (Exception ex) |
145 |
{ |
146 |
return false; |
147 |
} |
148 |
finally |
149 |
{ |
150 |
connection.Dispose(); |
151 |
} |
152 |
} |
153 |
|
154 |
return true; |
155 |
} |
156 |
|
157 |
public static DataTable SelectSetting() |
158 |
{ |
159 |
DataTable dt = new DataTable(); |
160 |
Project_Info projectInfo = Project_Info.GetInstance(); |
161 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
162 |
{ |
163 |
try |
164 |
{ |
165 |
connection.Open(); |
166 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
167 |
{ |
168 |
cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_SETTING_TABLE); |
169 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
170 |
dt.Load(dr); |
171 |
} |
172 |
connection.Close(); |
173 |
} |
174 |
catch (Exception ex) |
175 |
{ |
176 |
|
177 |
} |
178 |
finally |
179 |
{ |
180 |
connection.Dispose(); |
181 |
} |
182 |
} |
183 |
|
184 |
return dt; |
185 |
} |
186 |
|
187 |
private static void CreateTable(SQLiteConnection connection) |
188 |
{ |
189 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
190 |
{ |
191 |
cmd.CommandText = "SELECT NAME FROM sqlite_master WHERE type='table'"; |
192 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
193 |
using (DataTable dt = new DataTable()) |
194 |
{ |
195 |
dt.Load(dr); |
196 |
|
197 |
if (dt.Select(string.Format("NAME = '{0}'", SPPID_DB_INFO_TABLE)).Length == 0) |
198 |
{ |
199 |
cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString TEXT)", SPPID_DB_INFO_TABLE); |
200 |
cmd.ExecuteNonQuery(); |
201 |
} |
202 |
if (dt.Select(string.Format("NAME = '{0}'", SPPID_SETTING_TABLE)).Length == 0) |
203 |
{ |
204 |
cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString TEXT, SettingType TEXT)", SPPID_SETTING_TABLE); |
205 |
cmd.ExecuteNonQuery(); |
206 |
} |
207 |
if (dt.Select(string.Format("NAME = '{0}'", SPPID_SYMBOL_MAPPING_TABLE)).Length == 0) |
208 |
{ |
209 |
cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, NAME TEXT, SPPID_SYMBOL_PATH TEXT, LEADERLINE BOOLEAN)", SPPID_SYMBOL_MAPPING_TABLE); |
210 |
cmd.ExecuteNonQuery(); |
211 |
} |
212 |
if (dt.Select(string.Format("NAME = '{0}'", SPPID_ATTRIBUTE_MAPPING_TABLE)).Length == 0) |
213 |
{ |
214 |
cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, SPPID_ATTRIBUTE TEXT)", SPPID_ATTRIBUTE_MAPPING_TABLE); |
215 |
cmd.ExecuteNonQuery(); |
216 |
} |
217 |
if (dt.Select(string.Format("NAME = '{0}'", SPPID_LABEL_INFO_TABLE)).Length == 0) |
218 |
{ |
219 |
cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, LOCATION INT DEFAULT 0, LEADERLINE BOOLEAN)", SPPID_LABEL_INFO_TABLE); |
220 |
cmd.ExecuteNonQuery(); |
221 |
} |
222 |
if (dt.Select(string.Format("NAME = '{0}'", SPPID_DRAWING_INFO)).Length == 0) |
223 |
{ |
224 |
cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_DRAWING_UID Text PRIMARY KEY, PATH TEXT, DRAWINGNUMBER TEXT, DRAWINGNAME TEXT)", SPPID_DRAWING_INFO); |
225 |
cmd.ExecuteNonQuery(); |
226 |
} |
227 |
if (dt.Select(string.Format("NAME = '{0}'", SPPID_OPC_INFO)).Length == 0) |
228 |
{ |
229 |
cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_OPC_UID TEXT PRIMARY KEY, SPPID_OPC_MODELITEM_ID TEXT, ID2_DRAWING_UID TEXT, ATTRIBUTES TEXT, PAIRED BOOL)", SPPID_OPC_INFO); |
230 |
cmd.ExecuteNonQuery(); |
231 |
} |
232 |
} |
233 |
|
234 |
#region Check Column 업데이트시 예비용 |
235 |
cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_SYMBOL_MAPPING_TABLE); |
236 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
237 |
using (DataTable dt = new DataTable()) |
238 |
{ |
239 |
dt.Load(dr); |
240 |
if (!dt.Columns.Contains("LEADERLINE")) |
241 |
{ |
242 |
cmd.CommandText = string.Format("ALTER TABLE {0} ADD COLUMN LEADERLINE BOOLEAN", SPPID_SYMBOL_MAPPING_TABLE); |
243 |
cmd.ExecuteNonQuery(); |
244 |
} |
245 |
} |
246 |
|
247 |
cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_SETTING_TABLE); |
248 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
249 |
using (DataTable dt = new DataTable()) |
250 |
{ |
251 |
dt.Load(dr); |
252 |
if (!dt.Columns.Contains("SettingType")) |
253 |
{ |
254 |
cmd.CommandText = string.Format("ALTER TABLE {0} ADD COLUMN SettingType Text", SPPID_SETTING_TABLE); |
255 |
cmd.ExecuteNonQuery(); |
256 |
} |
257 |
} |
258 |
|
259 |
cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DRAWING_INFO); |
260 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
261 |
using (DataTable dt = new DataTable()) |
262 |
{ |
263 |
dt.Load(dr); |
264 |
if (dt.Columns.Contains("DOCUMENT")) |
265 |
{ |
266 |
cmd.CommandText = string.Format("DROP TABLE {0}", SPPID_DRAWING_INFO); |
267 |
cmd.ExecuteNonQuery(); |
268 |
|
269 |
cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_DRAWING_UID Text PRIMARY KEY, PATH TEXT, DRAWINGNUMBER TEXT, DRAWINGNAME TEXT)", SPPID_DRAWING_INFO); |
270 |
cmd.ExecuteNonQuery(); |
271 |
} |
272 |
} |
273 |
#endregion |
274 |
} |
275 |
} |
276 |
|
277 |
public static DataTable SelectProjectSymbol() |
278 |
{ |
279 |
DataTable dt = new DataTable(); |
280 |
Project_Info projectInfo = Project_Info.GetInstance(); |
281 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
282 |
{ |
283 |
try |
284 |
{ |
285 |
connection.Open(); |
286 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
287 |
{ |
288 |
cmd.CommandText = string.Format(@" |
289 |
SELECT s.UID, s.Name, st.Type, sp.SPPID_SYMBOL_PATH, sp.LEADERLINE FROM {1} as st, {0} as s |
290 |
LEFT OUTER JOIN {2} as sp |
291 |
ON s.UID = SP.UID |
292 |
WHERE s.SymbolType_UID = st.UID |
293 |
ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, SPPID_SYMBOL_MAPPING_TABLE); |
294 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
295 |
dt.Load(dr); |
296 |
|
297 |
DataTable dtClone = dt.Clone(); |
298 |
dtClone.Columns["UID"].DataType = typeof(string); |
299 |
foreach (DataRow row in dt.Rows) |
300 |
{ |
301 |
dtClone.ImportRow(row); |
302 |
} |
303 |
dt.Dispose(); |
304 |
dt = dtClone; |
305 |
} |
306 |
connection.Close(); |
307 |
} |
308 |
catch (Exception ex) |
309 |
{ |
310 |
|
311 |
} |
312 |
finally |
313 |
{ |
314 |
connection.Dispose(); |
315 |
} |
316 |
} |
317 |
|
318 |
return dt; |
319 |
} |
320 |
|
321 |
public static DataTable SelectProjectChildSymbol() |
322 |
{ |
323 |
DataTable result = new DataTable(); |
324 |
result.Columns.Add("UID"); |
325 |
result.Columns.Add("Name"); |
326 |
result.Columns.Add("Type"); |
327 |
result.Columns.Add("SPPID_SYMBOL_PATH"); |
328 |
|
329 |
Project_Info projectInfo = Project_Info.GetInstance(); |
330 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
331 |
using (DataTable dt = new DataTable()) |
332 |
{ |
333 |
try |
334 |
{ |
335 |
connection.Open(); |
336 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
337 |
{ |
338 |
cmd.CommandText = string.Format(@" |
339 |
SELECT AdditionalSymbol FROM Symbol"); |
340 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
341 |
dt.Load(dr); |
342 |
List<string> childList = new List<string>(); |
343 |
foreach (DataRow row in dt.Rows) |
344 |
{ |
345 |
if (row["AdditionalSymbol"] != null && !DBNull.Value.Equals(row["AdditionalSymbol"]) && !string.IsNullOrEmpty((string)row["AdditionalSymbol"])) |
346 |
{ |
347 |
string[] array = row["AdditionalSymbol"].ToString().Split(new char[] { '/' }); |
348 |
foreach (var childString in array) |
349 |
{ |
350 |
childList.Add(childString.Split(new char[] { ',' })[2]); |
351 |
} |
352 |
} |
353 |
|
354 |
} |
355 |
|
356 |
dt.Clear(); |
357 |
cmd.Reset(); |
358 |
cmd.CommandText = string.Format(@" |
359 |
SELECT * FROM {0}", SPPID_SYMBOL_MAPPING_TABLE); |
360 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
361 |
dt.Load(dr); |
362 |
|
363 |
childList = childList.Distinct().ToList(); |
364 |
foreach (var child in childList) |
365 |
{ |
366 |
string mappingPath = string.Empty; |
367 |
DataRow[] rows = dt.Select(string.Format("UID = '{0}'", child)); |
368 |
if (rows.Length == 1) |
369 |
mappingPath = !DBNull.Value.Equals(rows[0]["SPPID_SYMBOL_PATH"]) ? (string)rows[0]["SPPID_SYMBOL_PATH"] : null; |
370 |
|
371 |
DataRow newRow = result.NewRow(); |
372 |
newRow["UID"] = child; |
373 |
newRow["Name"] = child; |
374 |
newRow["Type"] = "Child Symbol"; |
375 |
newRow["SPPID_SYMBOL_PATH"] = mappingPath; |
376 |
result.Rows.Add(newRow); |
377 |
} |
378 |
} |
379 |
connection.Close(); |
380 |
} |
381 |
catch (Exception ex) |
382 |
{ |
383 |
|
384 |
} |
385 |
finally |
386 |
{ |
387 |
connection.Dispose(); |
388 |
} |
389 |
} |
390 |
|
391 |
return result; |
392 |
} |
393 |
|
394 |
public static DataTable SelectProjectLine() |
395 |
{ |
396 |
DataTable dt = new DataTable(); |
397 |
Project_Info projectInfo = Project_Info.GetInstance(); |
398 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
399 |
{ |
400 |
try |
401 |
{ |
402 |
connection.Open(); |
403 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
404 |
{ |
405 |
cmd.CommandText = string.Format(@" |
406 |
SELECT l.UID, l.Name, sp.SPPID_SYMBOL_PATH FROM {0} as l |
407 |
LEFT OUTER JOIN {1} as sp |
408 |
ON l.UID = SP.UID ;", LineTypes_TABLE, SPPID_SYMBOL_MAPPING_TABLE); |
409 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
410 |
dt.Load(dr); |
411 |
} |
412 |
connection.Close(); |
413 |
} |
414 |
catch (Exception ex) |
415 |
{ |
416 |
|
417 |
} |
418 |
finally |
419 |
{ |
420 |
connection.Dispose(); |
421 |
} |
422 |
} |
423 |
|
424 |
return dt; |
425 |
} |
426 |
|
427 |
public static DataTable SelectProjectLineProperties() |
428 |
{ |
429 |
DataTable dt = new DataTable(); |
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 (SQLiteCommand cmd = connection.CreateCommand()) |
437 |
{ |
438 |
cmd.CommandText = string.Format(@" |
439 |
SELECT lp.UID, lp.DisplayName, sp.SPPID_SYMBOL_PATH, spa.SPPID_ATTRIBUTE |
440 |
FROM {0} as lp |
441 |
LEFT OUTER JOIN {1} as sp |
442 |
ON lp.UID = sp.UID |
443 |
LEFT OUTER JOIN {2} as spa |
444 |
ON lp.UID = spa.UID;", LineProperties_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE); |
445 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
446 |
dt.Load(dr); |
447 |
} |
448 |
connection.Close(); |
449 |
} |
450 |
catch (Exception ex) |
451 |
{ |
452 |
|
453 |
} |
454 |
finally |
455 |
{ |
456 |
connection.Dispose(); |
457 |
} |
458 |
} |
459 |
|
460 |
return dt; |
461 |
} |
462 |
|
463 |
public static DataTable SelectProjectAttribute() |
464 |
{ |
465 |
DataTable dt = new DataTable(); |
466 |
Project_Info projectInfo = Project_Info.GetInstance(); |
467 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
468 |
{ |
469 |
try |
470 |
{ |
471 |
connection.Open(); |
472 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
473 |
{ |
474 |
cmd.CommandText = string.Format(@" |
475 |
SELECT sa.UID, sa.DisplayAttribute, st.TYPE, spa.SPPID_ATTRIBUTE, sp.SPPID_SYMBOL_PATH, spl.LOCATION, spl.LEADERLINE, sa.Property |
476 |
FROM {1} as sa, {0} as st |
477 |
LEFT OUTER JOIN {2} as sp |
478 |
ON sa.UID = SP.UID |
479 |
LEFT OUTER JOIN {3} as spa |
480 |
ON sa.UID = spa.UID |
481 |
LEFT OUTER JOIN {4} as spl |
482 |
ON sa.UID = spl.UID |
483 |
WHERE sa.SymbolType_UID = st.UID AND (sa.Property != 2 OR sa.Property IS NULL);", SymbolType_TABLE, SymbolAttribute_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE, SPPID_LABEL_INFO_TABLE); |
484 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
485 |
dt.Load(dr); |
486 |
} |
487 |
connection.Close(); |
488 |
} |
489 |
catch (Exception ex) |
490 |
{ |
491 |
|
492 |
} |
493 |
finally |
494 |
{ |
495 |
connection.Dispose(); |
496 |
} |
497 |
} |
498 |
|
499 |
return dt; |
500 |
} |
501 |
|
502 |
public static DataTable SelectID2SymbolTable() |
503 |
{ |
504 |
DataTable dt = new DataTable(); |
505 |
Project_Info projectInfo = Project_Info.GetInstance(); |
506 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
507 |
{ |
508 |
try |
509 |
{ |
510 |
connection.Open(); |
511 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
512 |
{ |
513 |
cmd.CommandText = @"SELECT * FROM Symbol"; |
514 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
515 |
dt.Load(dr); |
516 |
} |
517 |
connection.Close(); |
518 |
} |
519 |
catch (Exception ex) |
520 |
{ |
521 |
|
522 |
} |
523 |
finally |
524 |
{ |
525 |
connection.Dispose(); |
526 |
} |
527 |
} |
528 |
|
529 |
return dt; |
530 |
} |
531 |
|
532 |
public static DataTable SelectOPCRelations() |
533 |
{ |
534 |
DataTable dt = new DataTable(); |
535 |
Project_Info projectInfo = Project_Info.GetInstance(); |
536 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
537 |
{ |
538 |
try |
539 |
{ |
540 |
connection.Open(); |
541 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
542 |
{ |
543 |
cmd.CommandText = string.Format("SELECT * FROM {0}", OPCRelations_TABLE); |
544 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
545 |
dt.Load(dr); |
546 |
} |
547 |
connection.Close(); |
548 |
} |
549 |
catch (Exception ex) |
550 |
{ |
551 |
|
552 |
} |
553 |
finally |
554 |
{ |
555 |
connection.Dispose(); |
556 |
} |
557 |
} |
558 |
|
559 |
return dt; |
560 |
} |
561 |
|
562 |
public static DataTable SelectDrawings() |
563 |
{ |
564 |
DataTable dt = new DataTable(); |
565 |
Project_Info projectInfo = Project_Info.GetInstance(); |
566 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
567 |
{ |
568 |
try |
569 |
{ |
570 |
connection.Open(); |
571 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
572 |
{ |
573 |
cmd.CommandText = string.Format("SELECT * FROM {0}", "Drawings"); |
574 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
575 |
dt.Load(dr); |
576 |
} |
577 |
connection.Close(); |
578 |
} |
579 |
catch (Exception ex) |
580 |
{ |
581 |
|
582 |
} |
583 |
finally |
584 |
{ |
585 |
connection.Dispose(); |
586 |
} |
587 |
} |
588 |
|
589 |
return dt; |
590 |
} |
591 |
|
592 |
public static DataTable SelectOPCInfo() |
593 |
{ |
594 |
DataTable dt = new DataTable(); |
595 |
Project_Info projectInfo = Project_Info.GetInstance(); |
596 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
597 |
{ |
598 |
try |
599 |
{ |
600 |
connection.Open(); |
601 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
602 |
{ |
603 |
cmd.CommandText = string.Format("SELECT * FROM {0} WHERE SPPID_OPC_MODELITEM_ID IS NOT NULL AND PAIRED = False", SPPID_OPC_INFO); |
604 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
605 |
dt.Load(dr); |
606 |
} |
607 |
connection.Close(); |
608 |
} |
609 |
catch (Exception ex) |
610 |
{ |
611 |
|
612 |
} |
613 |
finally |
614 |
{ |
615 |
connection.Dispose(); |
616 |
} |
617 |
} |
618 |
|
619 |
return dt; |
620 |
} |
621 |
|
622 |
public static DataTable SelectSymbolType() |
623 |
{ |
624 |
DataTable dt = new DataTable(); |
625 |
Project_Info projectInfo = Project_Info.GetInstance(); |
626 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
627 |
{ |
628 |
try |
629 |
{ |
630 |
connection.Open(); |
631 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
632 |
{ |
633 |
cmd.CommandText = string.Format("SELECT * FROM {0}", SymbolType_TABLE); |
634 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
635 |
dt.Load(dr); |
636 |
} |
637 |
connection.Close(); |
638 |
} |
639 |
catch (Exception ex) |
640 |
{ |
641 |
|
642 |
} |
643 |
finally |
644 |
{ |
645 |
connection.Dispose(); |
646 |
} |
647 |
} |
648 |
|
649 |
return dt; |
650 |
} |
651 |
|
652 |
public static DataTable SelectDrawingInfo() |
653 |
{ |
654 |
DataTable dt = new DataTable(); |
655 |
Project_Info projectInfo = Project_Info.GetInstance(); |
656 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
657 |
{ |
658 |
try |
659 |
{ |
660 |
connection.Open(); |
661 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
662 |
{ |
663 |
cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DRAWING_INFO); |
664 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
665 |
dt.Load(dr); |
666 |
} |
667 |
connection.Close(); |
668 |
} |
669 |
catch (Exception ex) |
670 |
{ |
671 |
|
672 |
} |
673 |
finally |
674 |
{ |
675 |
connection.Dispose(); |
676 |
} |
677 |
} |
678 |
|
679 |
return dt; |
680 |
} |
681 |
|
682 |
public static bool InsertSymbolMapping(List<Tuple<string, string, string, bool>> datas) |
683 |
{ |
684 |
Project_Info projectInfo = Project_Info.GetInstance(); |
685 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
686 |
{ |
687 |
try |
688 |
{ |
689 |
connection.Open(); |
690 |
using (SQLiteTransaction transaction = connection.BeginTransaction()) |
691 |
{ |
692 |
try |
693 |
{ |
694 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
695 |
{ |
696 |
foreach (var item in datas) |
697 |
{ |
698 |
cmd.Parameters.Clear(); |
699 |
cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, NAME, SPPID_SYMBOL_PATH, LEADERLINE) VALUES (@UID, @NAME, @SPPID_SYMBOL_PATH, @LEADERLINE)", SPPID_SYMBOL_MAPPING_TABLE); |
700 |
cmd.Parameters.AddWithValue("@UID", item.Item1); |
701 |
cmd.Parameters.AddWithValue("@NAME", item.Item2); |
702 |
cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", item.Item3); |
703 |
cmd.Parameters.AddWithValue("@LEADERLINE", item.Item4); |
704 |
cmd.ExecuteNonQuery(); |
705 |
} |
706 |
} |
707 |
transaction.Commit(); |
708 |
connection.Close(); |
709 |
} |
710 |
catch (Exception ex) |
711 |
{ |
712 |
transaction.Rollback(); |
713 |
} |
714 |
finally |
715 |
{ |
716 |
transaction.Dispose(); |
717 |
} |
718 |
} |
719 |
} |
720 |
catch (Exception ex) |
721 |
{ |
722 |
return false; |
723 |
} |
724 |
finally |
725 |
{ |
726 |
connection.Dispose(); |
727 |
} |
728 |
} |
729 |
|
730 |
return true; |
731 |
} |
732 |
|
733 |
public static bool InsertAttributeMapping(List<Tuple<string, string>> datas) |
734 |
{ |
735 |
Project_Info projectInfo = Project_Info.GetInstance(); |
736 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
737 |
{ |
738 |
try |
739 |
{ |
740 |
connection.Open(); |
741 |
using (SQLiteTransaction transaction = connection.BeginTransaction()) |
742 |
{ |
743 |
try |
744 |
{ |
745 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
746 |
{ |
747 |
foreach (var item in datas) |
748 |
{ |
749 |
cmd.Parameters.Clear(); |
750 |
cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, SPPID_ATTRIBUTE) VALUES (@UID, @SPPID_ATTRIBUTE)", SPPID_ATTRIBUTE_MAPPING_TABLE); |
751 |
cmd.Parameters.AddWithValue("@UID", item.Item1); |
752 |
cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", item.Item2); |
753 |
cmd.ExecuteNonQuery(); |
754 |
} |
755 |
} |
756 |
transaction.Commit(); |
757 |
connection.Close(); |
758 |
} |
759 |
catch (Exception ex) |
760 |
{ |
761 |
transaction.Rollback(); |
762 |
} |
763 |
finally |
764 |
{ |
765 |
transaction.Dispose(); |
766 |
} |
767 |
} |
768 |
} |
769 |
catch (Exception ex) |
770 |
{ |
771 |
return false; |
772 |
} |
773 |
finally |
774 |
{ |
775 |
connection.Dispose(); |
776 |
} |
777 |
} |
778 |
return true; |
779 |
} |
780 |
|
781 |
public static bool InsertLabelInfoMapping(List<Tuple<string, int, bool>> datas) |
782 |
{ |
783 |
Project_Info projectInfo = Project_Info.GetInstance(); |
784 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
785 |
{ |
786 |
try |
787 |
{ |
788 |
connection.Open(); |
789 |
using (SQLiteTransaction transaction = connection.BeginTransaction()) |
790 |
{ |
791 |
try |
792 |
{ |
793 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
794 |
{ |
795 |
foreach (var item in datas) |
796 |
{ |
797 |
cmd.Parameters.Clear(); |
798 |
cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, LOCATION, LEADERLINE) VALUES (@UID, @LOCATION, @LEADERLINE)", SPPID_LABEL_INFO_TABLE); |
799 |
cmd.Parameters.AddWithValue("@UID", item.Item1); |
800 |
cmd.Parameters.AddWithValue("@LOCATION", item.Item2); |
801 |
cmd.Parameters.AddWithValue("@LEADERLINE", item.Item3); |
802 |
cmd.ExecuteNonQuery(); |
803 |
} |
804 |
} |
805 |
transaction.Commit(); |
806 |
connection.Close(); |
807 |
} |
808 |
catch (Exception ex) |
809 |
{ |
810 |
transaction.Rollback(); |
811 |
} |
812 |
finally |
813 |
{ |
814 |
transaction.Dispose(); |
815 |
} |
816 |
} |
817 |
} |
818 |
catch (Exception ex) |
819 |
{ |
820 |
return false; |
821 |
} |
822 |
finally |
823 |
{ |
824 |
connection.Dispose(); |
825 |
} |
826 |
} |
827 |
return true; |
828 |
} |
829 |
|
830 |
public static bool InsertDrawingInfoAndOPCInfo(string path, string drawingNumber, string drawingName, SPPID.Model.SPPID_Document document) |
831 |
{ |
832 |
Project_Info projectInfo = Project_Info.GetInstance(); |
833 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
834 |
{ |
835 |
try |
836 |
{ |
837 |
connection.Open(); |
838 |
using (SQLiteTransaction transaction = connection.BeginTransaction()) |
839 |
{ |
840 |
try |
841 |
{ |
842 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
843 |
{ |
844 |
cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (ID2_DRAWING_UID, PATH, DRAWINGNUMBER, DRAWINGNAME) VALUES (@ID2_DRAWING_UID, @PATH, @DRAWINGNUMBER, @DRAWINGNAME)", SPPID_DRAWING_INFO); |
845 |
cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID); |
846 |
cmd.Parameters.AddWithValue("@PATH", path); |
847 |
cmd.Parameters.AddWithValue("@DRAWINGNUMBER", drawingNumber); |
848 |
cmd.Parameters.AddWithValue("@DRAWINGNAME", drawingName); |
849 |
cmd.ExecuteNonQuery(); |
850 |
} |
851 |
|
852 |
List<Symbol> OPCs = document.SYMBOLS.FindAll(x => x.TYPE == "Piping OPC's" || x.TYPE == "Instrument OPC's"); |
853 |
foreach (var item in OPCs) |
854 |
{ |
855 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
856 |
{ |
857 |
cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (ID2_OPC_UID, SPPID_OPC_MODELITEM_ID, ID2_DRAWING_UID, ATTRIBUTES, PAIRED) VALUES (@ID2_OPC_UID, @SPPID_OPC_MODELITEM_ID, @ID2_DRAWING_UID, @ATTRIBUTES, @PAIRED)", SPPID_OPC_INFO); |
858 |
cmd.Parameters.AddWithValue("@ID2_OPC_UID", item.UID); |
859 |
cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", item.SPPID.ModelItemID); |
860 |
cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID); |
861 |
cmd.Parameters.AddWithValue("@ATTRIBUTES", JsonConvert.SerializeObject(item.SPPID.Attributes)); |
862 |
cmd.Parameters.AddWithValue("@PAIRED", false); |
863 |
cmd.ExecuteNonQuery(); |
864 |
} |
865 |
} |
866 |
|
867 |
transaction.Commit(); |
868 |
connection.Close(); |
869 |
} |
870 |
catch (Exception ex) |
871 |
{ |
872 |
transaction.Rollback(); |
873 |
} |
874 |
finally |
875 |
{ |
876 |
transaction.Dispose(); |
877 |
} |
878 |
} |
879 |
} |
880 |
catch (Exception ex) |
881 |
{ |
882 |
return false; |
883 |
} |
884 |
finally |
885 |
{ |
886 |
connection.Dispose(); |
887 |
} |
888 |
} |
889 |
return true; |
890 |
} |
891 |
|
892 |
public static bool InsertOPCInfo(string UID, string ModelItemID, string drawingUID, bool Paired) |
893 |
{ |
894 |
Project_Info projectInfo = Project_Info.GetInstance(); |
895 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
896 |
{ |
897 |
try |
898 |
{ |
899 |
connection.Open(); |
900 |
using (SQLiteTransaction transaction = connection.BeginTransaction()) |
901 |
{ |
902 |
try |
903 |
{ |
904 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
905 |
{ |
906 |
cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (ID2_OPC_UID, SPPID_OPC_MODELITEM_ID, ID2_DRAWING_UID, PAIRED) VALUES (@ID2_OPC_UID, @SPPID_OPC_MODELITEM_ID, @ID2_DRAWING_UID, @PAIRED)", SPPID_OPC_INFO); |
907 |
cmd.Parameters.AddWithValue("@ID2_OPC_UID", UID); |
908 |
cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", ModelItemID); |
909 |
cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", drawingUID); |
910 |
cmd.Parameters.AddWithValue("@PAIRED", Paired); |
911 |
cmd.ExecuteNonQuery(); |
912 |
} |
913 |
|
914 |
transaction.Commit(); |
915 |
connection.Close(); |
916 |
} |
917 |
catch (Exception ex) |
918 |
{ |
919 |
transaction.Rollback(); |
920 |
} |
921 |
finally |
922 |
{ |
923 |
transaction.Dispose(); |
924 |
} |
925 |
} |
926 |
} |
927 |
catch (Exception ex) |
928 |
{ |
929 |
return false; |
930 |
} |
931 |
finally |
932 |
{ |
933 |
connection.Dispose(); |
934 |
} |
935 |
} |
936 |
return true; |
937 |
} |
938 |
} |
939 |
} |