프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / DB / Project_DB.cs @ b7a29053

이력 | 보기 | 이력해설 | 다운로드 (37.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
        public static DataTable SelectDrawingInfo()
622
        {
623
            DataTable dt = new DataTable();
624
            Project_Info projectInfo = Project_Info.GetInstance();
625
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
626
            {
627
                try
628
                {
629
                    connection.Open();
630
                    using (SQLiteCommand cmd = connection.CreateCommand())
631
                    {
632
                        cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DRAWING_INFO);
633
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
634
                            dt.Load(dr);
635
                    }
636
                    connection.Close();
637
                }
638
                catch (Exception ex)
639
                {
640

    
641
                }
642
                finally
643
                {
644
                    connection.Dispose();
645
                }
646
            }
647

    
648
            return dt;
649
        }
650

    
651
        public static bool InsertSymbolMapping(List<Tuple<string, string, string, bool>> datas)
652
        {
653
            Project_Info projectInfo = Project_Info.GetInstance();
654
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
655
            {
656
                try
657
                {
658
                    connection.Open();
659
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
660
                    {
661
                        try
662
                        {
663
                            using (SQLiteCommand cmd = connection.CreateCommand())
664
                            {
665
                                foreach (var item in datas)
666
                                {
667
                                    cmd.Parameters.Clear();
668
                                    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);
669
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
670
                                    cmd.Parameters.AddWithValue("@NAME", item.Item2);
671
                                    cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", item.Item3);
672
                                    cmd.Parameters.AddWithValue("@LEADERLINE", item.Item4);
673
                                    cmd.ExecuteNonQuery();
674
                                }
675
                            }
676
                            transaction.Commit();
677
                            connection.Close();
678
                        }
679
                        catch (Exception ex)
680
                        {
681
                            transaction.Rollback();
682
                        }
683
                        finally
684
                        {
685
                            transaction.Dispose();
686
                        }
687
                    }
688
                }
689
                catch (Exception ex)
690
                {
691
                    return false;
692
                }
693
                finally
694
                {
695
                    connection.Dispose();
696
                }
697
            }
698

    
699
            return true;
700
        }
701

    
702
        public static bool InsertAttributeMapping(List<Tuple<string, string>> datas)
703
        {
704
            Project_Info projectInfo = Project_Info.GetInstance();
705
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
706
            {
707
                try
708
                {
709
                    connection.Open();
710
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
711
                    {
712
                        try
713
                        {
714
                            using (SQLiteCommand cmd = connection.CreateCommand())
715
                            {
716
                                foreach (var item in datas)
717
                                {
718
                                    cmd.Parameters.Clear();
719
                                    cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, SPPID_ATTRIBUTE) VALUES (@UID, @SPPID_ATTRIBUTE)", SPPID_ATTRIBUTE_MAPPING_TABLE);
720
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
721
                                    cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", item.Item2);
722
                                    cmd.ExecuteNonQuery();
723
                                }
724
                            }
725
                            transaction.Commit();
726
                            connection.Close();
727
                        }
728
                        catch (Exception ex)
729
                        {
730
                            transaction.Rollback();
731
                        }
732
                        finally
733
                        {
734
                            transaction.Dispose();
735
                        }
736
                    }
737
                }
738
                catch (Exception ex)
739
                {
740
                    return false;
741
                }
742
                finally
743
                {
744
                    connection.Dispose();
745
                }
746
            }
747
            return true;
748
        }
749

    
750
        public static bool InsertLabelInfoMapping(List<Tuple<string, int, bool>> datas)
751
        {
752
            Project_Info projectInfo = Project_Info.GetInstance();
753
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
754
            {
755
                try
756
                {
757
                    connection.Open();
758
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
759
                    {
760
                        try
761
                        {
762
                            using (SQLiteCommand cmd = connection.CreateCommand())
763
                            {
764
                                foreach (var item in datas)
765
                                {
766
                                    cmd.Parameters.Clear();
767
                                    cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, LOCATION, LEADERLINE) VALUES (@UID, @LOCATION, @LEADERLINE)", SPPID_LABEL_INFO_TABLE);
768
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
769
                                    cmd.Parameters.AddWithValue("@LOCATION", item.Item2);
770
                                    cmd.Parameters.AddWithValue("@LEADERLINE", item.Item3);
771
                                    cmd.ExecuteNonQuery();
772
                                }
773
                            }
774
                            transaction.Commit();
775
                            connection.Close();
776
                        }
777
                        catch (Exception ex)
778
                        {
779
                            transaction.Rollback();
780
                        }
781
                        finally
782
                        {
783
                            transaction.Dispose();
784
                        }
785
                    }
786
                }
787
                catch (Exception ex)
788
                {
789
                    return false;
790
                }
791
                finally
792
                {
793
                    connection.Dispose();
794
                }
795
            }
796
            return true;
797
        }
798

    
799
        public static bool InsertDrawingInfoAndOPCInfo(string path, string drawingNumber, string drawingName, SPPID.Model.SPPID_Document document)
800
        {
801
            Project_Info projectInfo = Project_Info.GetInstance();
802
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
803
            {
804
                try
805
                {
806
                    connection.Open();
807
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
808
                    {
809
                        try
810
                        {
811
                            using (SQLiteCommand cmd = connection.CreateCommand())
812
                            {
813
                                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);
814
                                cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
815
                                cmd.Parameters.AddWithValue("@PATH", path);
816
                                cmd.Parameters.AddWithValue("@DRAWINGNUMBER", drawingNumber);
817
                                cmd.Parameters.AddWithValue("@DRAWINGNAME", drawingName);
818
                                cmd.ExecuteNonQuery();
819
                            }
820

    
821
                            List<Symbol> OPCs = document.SYMBOLS.FindAll(x => x.TYPE == "Piping OPC's" || x.TYPE == "Instrument OPC's");
822
                            foreach (var item in OPCs)
823
                            {
824
                                using (SQLiteCommand cmd = connection.CreateCommand())
825
                                {
826
                                    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);
827
                                    cmd.Parameters.AddWithValue("@ID2_OPC_UID", item.UID);
828
                                    cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", item.SPPID.ModelItemID);
829
                                    cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
830
                                    cmd.Parameters.AddWithValue("@ATTRIBUTES", JsonConvert.SerializeObject(item.SPPID.Attributes));
831
                                    cmd.Parameters.AddWithValue("@PAIRED", false);
832
                                    cmd.ExecuteNonQuery();
833
                                }
834
                            }
835

    
836
                            transaction.Commit();
837
                            connection.Close();
838
                        }
839
                        catch (Exception ex)
840
                        {
841
                            transaction.Rollback();
842
                        }
843
                        finally
844
                        {
845
                            transaction.Dispose();
846
                        }
847
                    }
848
                }
849
                catch (Exception ex)
850
                {
851
                    return false;
852
                }
853
                finally
854
                {
855
                    connection.Dispose();
856
                }
857
            }
858
            return true;
859
        }
860

    
861
        public static bool InsertOPCInfo(string UID, string ModelItemID, string drawingUID, bool Paired)
862
        {
863
            Project_Info projectInfo = Project_Info.GetInstance();
864
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
865
            {
866
                try
867
                {
868
                    connection.Open();
869
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
870
                    {
871
                        try
872
                        {
873
                            using (SQLiteCommand cmd = connection.CreateCommand())
874
                            {
875
                                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);
876
                                cmd.Parameters.AddWithValue("@ID2_OPC_UID", UID);
877
                                cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", ModelItemID);
878
                                cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", drawingUID);
879
                                cmd.Parameters.AddWithValue("@PAIRED", Paired);
880
                                cmd.ExecuteNonQuery();
881
                            }
882

    
883
                            transaction.Commit();
884
                            connection.Close();
885
                        }
886
                        catch (Exception ex)
887
                        {
888
                            transaction.Rollback();
889
                        }
890
                        finally
891
                        {
892
                            transaction.Dispose();
893
                        }
894
                    }
895
                }
896
                catch (Exception ex)
897
                {
898
                    return false;
899
                }
900
                finally
901
                {
902
                    connection.Dispose();
903
                }
904
            }
905
            return true;
906
        }
907
    }
908
}
클립보드 이미지 추가 (최대 크기: 500 MB)