프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / DB / Project_DB.cs @ 401efcff

이력 | 보기 | 이력해설 | 다운로드 (24.4 KB)

1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
using System.Globalization;
7
using System.Data.SQLite;
8
using System.Data;
9

    
10
namespace Converter.BaseModel
11
{
12
    public class Project_DB
13
    {
14
        const string SPPID_DB_INFO_TABLE = "T_SPPID_CONNECTION_INFO";
15
        const string SPPID_SYMBOL_MAPPING_TABLE = "T_SPPID_SYMBOL_MAPPING";
16
        const string SPPID_ATTRIBUTE_MAPPING_TABLE = "T_SPPID_ATTRIBUTE_MAPPING";
17
        const string SPPID_ETC_SETTING_TABLE = "T_SPPID_ETC_SETTING_TABLE";
18
        const string SPPID_LABEL_INFO_TABLE = "T_SPPID_LABEL_INFO";
19

    
20
        const string LineProperties_TABLE = "LineProperties";
21
        const string LineTypes_TABLE = "LineTypes";
22
        const string SymbolType_TABLE = "SymbolType";
23
        const string SymbolAttribute_TABLE = "SymbolAttribute";
24
        const string Symbol_TABLE = "Symbol";
25

    
26
        public static bool ConnTestAndCreateTable()
27
        {
28
            bool result = false;
29
            Project_Info projectInfo = Project_Info.GetInstance();
30
            SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, @"Data Source = {0}", projectInfo.DBFilePath));
31
            try
32
            {
33
                connection.Open();
34
                if (connection.State == ConnectionState.Open)
35
                {
36
                    CreateTable(connection);
37
                    result = true;
38
                }
39
                connection.Close();
40
            }
41
            catch (Exception ex)
42
            {
43

    
44
            }
45
            finally
46
            {
47
                connection.Dispose();
48
            }
49

    
50
            return result;
51
        }
52

    
53
        public static bool SaveSPPID_DB_INFO(string jsonString)
54
        {
55
            Project_Info projectInfo = Project_Info.GetInstance();
56
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
57
            {
58

    
59
                try
60
                {
61
                    connection.Open();
62
                    using (SQLiteCommand cmd = connection.CreateCommand())
63
                    {
64
                        cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_DB_INFO_TABLE);
65
                        cmd.ExecuteNonQuery();
66

    
67
                        cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString)", SPPID_DB_INFO_TABLE);
68
                        cmd.Parameters.AddWithValue("@jsonString", jsonString);
69
                        cmd.ExecuteNonQuery();
70
                    }
71
                    connection.Close();
72
                }
73
                catch (Exception ex)
74
                {
75
                    return false;
76
                }
77
                finally
78
                {
79
                    connection.Dispose();
80
                }
81
            }
82

    
83
            return true;
84
        }
85

    
86
        public static DataTable SelectSPPID_DB_INFO()
87
        {
88
            DataTable dt = new DataTable();
89
            Project_Info projectInfo = Project_Info.GetInstance();
90
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
91
            {
92
                try
93
                {
94
                    connection.Open();
95
                    using (SQLiteCommand cmd = connection.CreateCommand())
96
                    {
97
                        cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DB_INFO_TABLE);
98
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
99
                            dt.Load(dr);
100
                    }
101
                    connection.Close();
102
                }
103
                catch (Exception ex)
104
                {
105

    
106
                }
107
                finally
108
                {
109
                    connection.Dispose();
110
                }
111
            }
112

    
113
            return dt;
114
        }
115

    
116
        public static bool SaveETCSetting(string jsonString)
117
        {
118
            Project_Info projectInfo = Project_Info.GetInstance();
119
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
120
            {
121

    
122
                try
123
                {
124
                    connection.Open();
125
                    using (SQLiteCommand cmd = connection.CreateCommand())
126
                    {
127
                        cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_ETC_SETTING_TABLE);
128
                        cmd.ExecuteNonQuery();
129

    
130
                        cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString)", SPPID_ETC_SETTING_TABLE);
131
                        cmd.Parameters.AddWithValue("@jsonString", jsonString);
132
                        cmd.ExecuteNonQuery();
133
                    }
134
                    connection.Close();
135
                }
136
                catch (Exception ex)
137
                {
138
                    return false;
139
                }
140
                finally
141
                {
142
                    connection.Dispose();
143
                }
144
            }
145

    
146
            return true;
147
        }
148

    
149
        public static DataTable SelectETCSetting()
150
        {
151
            DataTable dt = new DataTable();
152
            Project_Info projectInfo = Project_Info.GetInstance();
153
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
154
            {
155
                try
156
                {
157
                    connection.Open();
158
                    using (SQLiteCommand cmd = connection.CreateCommand())
159
                    {
160
                        cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_ETC_SETTING_TABLE);
161
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
162
                            dt.Load(dr);
163
                    }
164
                    connection.Close();
165
                }
166
                catch (Exception ex)
167
                {
168

    
169
                }
170
                finally
171
                {
172
                    connection.Dispose();
173
                }
174
            }
175

    
176
            return dt;
177
        }
178

    
179
        private static void CreateTable(SQLiteConnection connection)
180
        {
181
            using (SQLiteCommand cmd = connection.CreateCommand())
182
            {
183
                cmd.CommandText = "SELECT NAME FROM sqlite_master WHERE type='table'";
184
                using (SQLiteDataReader dr = cmd.ExecuteReader())
185
                using (DataTable dt = new DataTable())
186
                {
187
                    dt.Load(dr);
188

    
189
                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_DB_INFO_TABLE)).Length == 0)
190
                    {
191
                        cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString TEXT)", SPPID_DB_INFO_TABLE);
192
                        cmd.ExecuteNonQuery();
193
                    }
194
                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_ETC_SETTING_TABLE)).Length == 0)
195
                    {
196
                        cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString TEXT)", SPPID_ETC_SETTING_TABLE);
197
                        cmd.ExecuteNonQuery();
198
                    }
199
                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_SYMBOL_MAPPING_TABLE)).Length == 0)
200
                    {
201
                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, NAME TEXT, SPPID_SYMBOL_PATH TEXT, LEADERLINE BOOLEAN)", SPPID_SYMBOL_MAPPING_TABLE);
202
                        cmd.ExecuteNonQuery();
203
                    }
204
                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_ATTRIBUTE_MAPPING_TABLE)).Length == 0)
205
                    {
206
                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, SPPID_ATTRIBUTE TEXT)", SPPID_ATTRIBUTE_MAPPING_TABLE);
207
                        cmd.ExecuteNonQuery();
208
                    }
209
                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_LABEL_INFO_TABLE)).Length == 0)
210
                    {
211
                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, LOCATION INT DEFAULT 0, LEADERLINE BOOLEAN)", SPPID_LABEL_INFO_TABLE);
212
                        cmd.ExecuteNonQuery();
213
                    }
214
                }
215

    
216
                #region Check Column 업데이트시 예비용
217
                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_SYMBOL_MAPPING_TABLE);
218
                using (SQLiteDataReader dr = cmd.ExecuteReader())
219
                using (DataTable dt = new DataTable())
220
                {
221
                    dt.Load(dr);
222
                    if (!dt.Columns.Contains("LEADERLINE"))
223
                    {
224
                        cmd.CommandText = string.Format("ALTER TABLE {0} ADD COLUMN LEADERLINE BOOLEAN", SPPID_SYMBOL_MAPPING_TABLE);
225
                        cmd.ExecuteNonQuery();
226
                    }
227
                }
228
                #endregion
229
            }
230
        }
231

    
232
        public static DataTable SelectProjectSymbol()
233
        {
234
            DataTable dt = new DataTable();
235
            Project_Info projectInfo = Project_Info.GetInstance();
236
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
237
            {
238
                try
239
                {
240
                    connection.Open();
241
                    using (SQLiteCommand cmd = connection.CreateCommand())
242
                    {
243
                        cmd.CommandText = string.Format(@"
244
                            SELECT s.UID, s.Name, st.Type, sp.SPPID_SYMBOL_PATH, sp.LEADERLINE FROM {1} as st, {0} as s 
245
                                LEFT OUTER JOIN {2} as sp 
246
                                    ON s.UID = SP.UID 
247
                            WHERE s.SymbolType_UID = st.UID 
248
                            ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
249
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
250
                            dt.Load(dr);
251

    
252
                        DataTable dtClone = dt.Clone();
253
                        dtClone.Columns["UID"].DataType = typeof(string);
254
                        foreach (DataRow row in dt.Rows)
255
                        {
256
                            dtClone.ImportRow(row);
257
                        }
258
                        dt.Dispose();
259
                        dt = dtClone;
260
                    }
261
                    connection.Close();
262
                }
263
                catch (Exception ex)
264
                {
265

    
266
                }
267
                finally
268
                {
269
                    connection.Dispose();
270
                }
271
            }
272

    
273
            return dt;
274
        }
275

    
276
        public static DataTable SelectProjectChildSymbol()
277
        {
278
            DataTable result = new DataTable();
279
            result.Columns.Add("UID");
280
            result.Columns.Add("Name");
281
            result.Columns.Add("Type");
282
            result.Columns.Add("SPPID_SYMBOL_PATH");
283
            
284
            Project_Info projectInfo = Project_Info.GetInstance();
285
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
286
            using (DataTable dt = new DataTable())
287
            {
288
                try
289
                {
290
                    connection.Open();
291
                    using (SQLiteCommand cmd = connection.CreateCommand())
292
                    {
293
                        cmd.CommandText = string.Format(@"
294
                            SELECT AdditionalSymbol FROM Symbol");
295
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
296
                            dt.Load(dr);
297
                        List<string> childList = new List<string>();
298
                        foreach (DataRow row in dt.Rows)
299
                        {
300
                            if (!string.IsNullOrEmpty((string)row["AdditionalSymbol"]))
301
                            {
302
                                string[] array = row["AdditionalSymbol"].ToString().Split(new char[] { '/' });
303
                                foreach (var childString in array)
304
                                {
305
                                    childList.Add(childString.Split(new char[] { ',' })[2]);
306
                                }
307
                            }
308

    
309
                        }
310

    
311
                        dt.Clear();
312
                        cmd.Reset();
313
                        cmd.CommandText = string.Format(@"
314
                            SELECT * FROM {0}", SPPID_SYMBOL_MAPPING_TABLE);
315
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
316
                            dt.Load(dr);
317

    
318
                        childList = childList.Distinct().ToList();
319
                        foreach (var child in childList)
320
                        {
321
                            string mappingPath = string.Empty;
322
                            DataRow[] rows = dt.Select(string.Format("UID = '{0}'", child));
323
                            if (rows.Length == 1)
324
                                mappingPath = !DBNull.Value.Equals(rows[0]["SPPID_SYMBOL_PATH"]) ? (string)rows[0]["SPPID_SYMBOL_PATH"] : null;
325

    
326
                            DataRow newRow = result.NewRow();
327
                            newRow["UID"] = child;
328
                            newRow["Name"] = child;
329
                            newRow["Type"] = "Child Symbol";
330
                            newRow["SPPID_SYMBOL_PATH"] = mappingPath;
331
                            result.Rows.Add(newRow);
332
                        }
333
                    }
334
                    connection.Close();
335
                }
336
                catch (Exception ex)
337
                {
338

    
339
                }
340
                finally
341
                {
342
                    connection.Dispose();
343
                }
344
            }
345

    
346
            return result;
347
        }
348

    
349
        public static DataTable SelectProjectLine()
350
        {
351
            DataTable dt = new DataTable();
352
            Project_Info projectInfo = Project_Info.GetInstance();
353
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
354
            {
355
                try
356
                {
357
                    connection.Open();
358
                    using (SQLiteCommand cmd = connection.CreateCommand())
359
                    {
360
                        cmd.CommandText = string.Format(@"
361
                            SELECT l.UID, l.Name, sp.SPPID_SYMBOL_PATH FROM {0} as l 
362
                                LEFT OUTER JOIN {1} as sp 
363
                                    ON l.UID = SP.UID ;", LineTypes_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
364
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
365
                            dt.Load(dr);
366
                    }
367
                    connection.Close();
368
                }
369
                catch (Exception ex)
370
                {
371

    
372
                }
373
                finally
374
                {
375
                    connection.Dispose();
376
                }
377
            }
378

    
379
            return dt;
380
        }
381

    
382
        public static DataTable SelectProjectLineProperties()
383
        {
384
            DataTable dt = new DataTable();
385
            Project_Info projectInfo = Project_Info.GetInstance();
386
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
387
            {
388
                try
389
                {
390
                    connection.Open();
391
                    using (SQLiteCommand cmd = connection.CreateCommand())
392
                    {
393
                        cmd.CommandText = string.Format(@"
394
                            SELECT lp.UID, lp.DisplayName, sp.SPPID_SYMBOL_PATH, spa.SPPID_ATTRIBUTE
395
                            FROM {0} as lp 
396
                                 LEFT OUTER JOIN {1} as sp 
397
                                      ON lp.UID = sp.UID
398
                                 LEFT OUTER JOIN {2} as spa 
399
                                      ON lp.UID = spa.UID;", LineProperties_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE);
400
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
401
                            dt.Load(dr);
402
                    }
403
                    connection.Close();
404
                }
405
                catch (Exception ex)
406
                {
407

    
408
                }
409
                finally
410
                {
411
                    connection.Dispose();
412
                }
413
            }
414

    
415
            return dt;
416
        }
417

    
418
        public static DataTable SelectProjectAttribute()
419
        {
420
            DataTable dt = new DataTable();
421
            Project_Info projectInfo = Project_Info.GetInstance();
422
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
423
            {
424
                try
425
                {
426
                    connection.Open();
427
                    using (SQLiteCommand cmd = connection.CreateCommand())
428
                    {
429
                        cmd.CommandText = string.Format(@"
430
                            SELECT sa.UID, sa.DisplayAttribute, st.TYPE, spa.SPPID_ATTRIBUTE, sp.SPPID_SYMBOL_PATH, spl.LOCATION, spl.LEADERLINE, sa.Property
431
                            FROM {1} as sa, {0} as st 
432
                                 LEFT OUTER JOIN {2} as sp 
433
                                      ON sa.UID = SP.UID 
434
                                LEFT OUTER JOIN {3} as spa 
435
                                     ON sa.UID = spa.UID
436
                                LEFT OUTER JOIN {4} as spl 
437
                                     ON sa.UID = spl.UID
438
                            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);
439
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
440
                            dt.Load(dr);
441
                    }
442
                    connection.Close();
443
                }
444
                catch (Exception ex)
445
                {
446

    
447
                }
448
                finally
449
                {
450
                    connection.Dispose();
451
                }
452
            }
453

    
454
            return dt;
455
        }
456

    
457

    
458

    
459
        public static bool InsertSymbolMapping(List<Tuple<string, string, string, bool>> datas)
460
        {
461
            Project_Info projectInfo = Project_Info.GetInstance();
462
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
463
            {
464
                try
465
                {
466
                    connection.Open();
467
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
468
                    {
469
                        try
470
                        {
471
                            using (SQLiteCommand cmd = connection.CreateCommand())
472
                            {
473
                                foreach (var item in datas)
474
                                {
475
                                    cmd.Parameters.Clear();
476
                                    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);
477
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
478
                                    cmd.Parameters.AddWithValue("@NAME", item.Item2);
479
                                    cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", item.Item3);
480
                                    cmd.Parameters.AddWithValue("@LEADERLINE", item.Item4);
481
                                    cmd.ExecuteNonQuery();
482
                                }
483
                            }
484
                            transaction.Commit();
485
                            connection.Close();
486
                        }
487
                        catch (Exception ex)
488
                        {
489
                            transaction.Rollback();
490
                        }
491
                        finally
492
                        {
493
                            transaction.Dispose();
494
                        }
495
                    }
496
                }
497
                catch (Exception ex)
498
                {
499
                    return false;
500
                }
501
                finally
502
                {
503
                    connection.Dispose();
504
                }
505
            }
506

    
507
            return true;
508
        }
509

    
510
        public static bool InsertAttributeMapping(List<Tuple<string, string>> datas)
511
        {
512
            Project_Info projectInfo = Project_Info.GetInstance();
513
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
514
            {
515
                try
516
                {
517
                    connection.Open();
518
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
519
                    {
520
                        try
521
                        {
522
                            using (SQLiteCommand cmd = connection.CreateCommand())
523
                            {
524
                                foreach (var item in datas)
525
                                {
526
                                    cmd.Parameters.Clear();
527
                                    cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, SPPID_ATTRIBUTE) VALUES (@UID, @SPPID_ATTRIBUTE)", SPPID_ATTRIBUTE_MAPPING_TABLE);
528
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
529
                                    cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", item.Item2);
530
                                    cmd.ExecuteNonQuery();
531
                                }
532
                            }
533
                            transaction.Commit();
534
                            connection.Close();
535
                        }
536
                        catch (Exception ex)
537
                        {
538
                            transaction.Rollback();
539
                        }
540
                        finally
541
                        {
542
                            transaction.Dispose();
543
                        }
544
                    }
545
                }
546
                catch (Exception ex)
547
                {
548
                    return false;
549
                }
550
                finally
551
                {
552
                    connection.Dispose();
553
                }
554
            }
555
            return true;
556
        }
557

    
558
        public static bool InsertLabelInfoMapping(List<Tuple<string, int, bool>> datas)
559
        {
560
            Project_Info projectInfo = Project_Info.GetInstance();
561
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
562
            {
563
                try
564
                {
565
                    connection.Open();
566
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
567
                    {
568
                        try
569
                        {
570
                            using (SQLiteCommand cmd = connection.CreateCommand())
571
                            {
572
                                foreach (var item in datas)
573
                                {
574
                                    cmd.Parameters.Clear();
575
                                    cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, LOCATION, LEADERLINE) VALUES (@UID, @LOCATION, @LEADERLINE)", SPPID_LABEL_INFO_TABLE);
576
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
577
                                    cmd.Parameters.AddWithValue("@LOCATION", item.Item2);
578
                                    cmd.Parameters.AddWithValue("@LEADERLINE", item.Item3);
579
                                    cmd.ExecuteNonQuery();
580
                                }
581
                            }
582
                            transaction.Commit();
583
                            connection.Close();
584
                        }
585
                        catch (Exception ex)
586
                        {
587
                            transaction.Rollback();
588
                        }
589
                        finally
590
                        {
591
                            transaction.Dispose();
592
                        }
593
                    }
594
                }
595
                catch (Exception ex)
596
                {
597
                    return false;
598
                }
599
                finally
600
                {
601
                    connection.Dispose();
602
                }
603
            }
604
            return true;
605
        }
606
    }
607
}
클립보드 이미지 추가 (최대 크기: 500 MB)