프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / Project_DB.cs @ a0c69b1c

이력 | 보기 | 이력해설 | 다운로드 (23.6 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)", 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
        }
217

    
218
        public static DataTable SelectProjectSymbol()
219
        {
220
            DataTable dt = new DataTable();
221
            Project_Info projectInfo = Project_Info.GetInstance();
222
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
223
            {
224
                try
225
                {
226
                    connection.Open();
227
                    using (SQLiteCommand cmd = connection.CreateCommand())
228
                    {
229
                        cmd.CommandText = string.Format(@"
230
                            SELECT s.UID, s.Name, st.Type, sp.SPPID_SYMBOL_PATH FROM {1} as st, {0} as s 
231
                                LEFT OUTER JOIN {2} as sp 
232
                                    ON s.UID = SP.UID 
233
                            WHERE s.SymbolType_UID = st.UID 
234
                            ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
235
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
236
                            dt.Load(dr);
237

    
238
                        DataTable dtClone = dt.Clone();
239
                        dtClone.Columns["UID"].DataType = typeof(string);
240
                        foreach (DataRow row in dt.Rows)
241
                        {
242
                            dtClone.ImportRow(row);
243
                        }
244
                        dt.Dispose();
245
                        dt = dtClone;
246
                    }
247
                    connection.Close();
248
                }
249
                catch (Exception ex)
250
                {
251

    
252
                }
253
                finally
254
                {
255
                    connection.Dispose();
256
                }
257
            }
258

    
259
            return dt;
260
        }
261

    
262
        public static DataTable SelectProjectChildSymbol()
263
        {
264
            DataTable result = new DataTable();
265
            result.Columns.Add("UID");
266
            result.Columns.Add("Name");
267
            result.Columns.Add("Type");
268
            result.Columns.Add("SPPID_SYMBOL_PATH");
269
            
270
            Project_Info projectInfo = Project_Info.GetInstance();
271
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
272
            using (DataTable dt = new DataTable())
273
            {
274
                try
275
                {
276
                    connection.Open();
277
                    using (SQLiteCommand cmd = connection.CreateCommand())
278
                    {
279
                        cmd.CommandText = string.Format(@"
280
                            SELECT AdditionalSymbol FROM Symbol");
281
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
282
                            dt.Load(dr);
283
                        List<string> childList = new List<string>();
284
                        foreach (DataRow row in dt.Rows)
285
                        {
286
                            if (!string.IsNullOrEmpty((string)row["AdditionalSymbol"]))
287
                            {
288
                                string[] array = row["AdditionalSymbol"].ToString().Split(new char[] { '/' });
289
                                foreach (var childString in array)
290
                                {
291
                                    childList.Add(childString.Split(new char[] { ',' })[2]);
292
                                }
293
                            }
294

    
295
                        }
296

    
297
                        dt.Clear();
298
                        cmd.Reset();
299
                        cmd.CommandText = string.Format(@"
300
                            SELECT * FROM {0}", SPPID_SYMBOL_MAPPING_TABLE);
301
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
302
                            dt.Load(dr);
303

    
304
                        childList = childList.Distinct().ToList();
305
                        foreach (var child in childList)
306
                        {
307
                            string mappingPath = string.Empty;
308
                            DataRow[] rows = dt.Select(string.Format("UID = '{0}'", child));
309
                            if (rows.Length == 1)
310
                                mappingPath = !DBNull.Value.Equals(rows[0]["SPPID_SYMBOL_PATH"]) ? (string)rows[0]["SPPID_SYMBOL_PATH"] : null;
311

    
312
                            DataRow newRow = result.NewRow();
313
                            newRow["UID"] = child;
314
                            newRow["Name"] = child;
315
                            newRow["Type"] = "Child Symbol";
316
                            newRow["SPPID_SYMBOL_PATH"] = mappingPath;
317
                            result.Rows.Add(newRow);
318
                        }
319
                    }
320
                    connection.Close();
321
                }
322
                catch (Exception ex)
323
                {
324

    
325
                }
326
                finally
327
                {
328
                    connection.Dispose();
329
                }
330
            }
331

    
332
            return result;
333
        }
334

    
335
        public static DataTable SelectProjectLine()
336
        {
337
            DataTable dt = new DataTable();
338
            Project_Info projectInfo = Project_Info.GetInstance();
339
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
340
            {
341
                try
342
                {
343
                    connection.Open();
344
                    using (SQLiteCommand cmd = connection.CreateCommand())
345
                    {
346
                        cmd.CommandText = string.Format(@"
347
                            SELECT l.UID, l.Name, sp.SPPID_SYMBOL_PATH FROM {0} as l 
348
                                LEFT OUTER JOIN {1} as sp 
349
                                    ON l.UID = SP.UID ;", LineTypes_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
350
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
351
                            dt.Load(dr);
352
                    }
353
                    connection.Close();
354
                }
355
                catch (Exception ex)
356
                {
357

    
358
                }
359
                finally
360
                {
361
                    connection.Dispose();
362
                }
363
            }
364

    
365
            return dt;
366
        }
367

    
368
        public static DataTable SelectProjectLineProperties()
369
        {
370
            DataTable dt = new DataTable();
371
            Project_Info projectInfo = Project_Info.GetInstance();
372
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
373
            {
374
                try
375
                {
376
                    connection.Open();
377
                    using (SQLiteCommand cmd = connection.CreateCommand())
378
                    {
379
                        cmd.CommandText = string.Format(@"
380
                            SELECT lp.UID, lp.DisplayName, sp.SPPID_SYMBOL_PATH, spa.SPPID_ATTRIBUTE
381
                            FROM {0} as lp 
382
                                 LEFT OUTER JOIN {1} as sp 
383
                                      ON lp.UID = sp.UID
384
                                 LEFT OUTER JOIN {2} as spa 
385
                                      ON lp.UID = spa.UID;", LineProperties_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE);
386
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
387
                            dt.Load(dr);
388
                    }
389
                    connection.Close();
390
                }
391
                catch (Exception ex)
392
                {
393

    
394
                }
395
                finally
396
                {
397
                    connection.Dispose();
398
                }
399
            }
400

    
401
            return dt;
402
        }
403

    
404
        public static DataTable SelectProjectAttribute()
405
        {
406
            DataTable dt = new DataTable();
407
            Project_Info projectInfo = Project_Info.GetInstance();
408
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
409
            {
410
                try
411
                {
412
                    connection.Open();
413
                    using (SQLiteCommand cmd = connection.CreateCommand())
414
                    {
415
                        cmd.CommandText = string.Format(@"
416
                            SELECT sa.UID, sa.DisplayAttribute, st.TYPE, spa.SPPID_ATTRIBUTE, sp.SPPID_SYMBOL_PATH, spl.LOCATION, spl.LEADERLINE 
417
                            FROM {1} as sa, {0} as st 
418
                                 LEFT OUTER JOIN {2} as sp 
419
                                      ON sa.UID = SP.UID 
420
                                LEFT OUTER JOIN {3} as spa 
421
                                     ON sa.UID = spa.UID
422
                                LEFT OUTER JOIN {4} as spl 
423
                                     ON sa.UID = spl.UID
424
                            WHERE sa.SymbolType_UID = st.UID;", SymbolType_TABLE, SymbolAttribute_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE, SPPID_LABEL_INFO_TABLE);
425
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
426
                            dt.Load(dr);
427
                    }
428
                    connection.Close();
429
                }
430
                catch (Exception ex)
431
                {
432

    
433
                }
434
                finally
435
                {
436
                    connection.Dispose();
437
                }
438
            }
439

    
440
            return dt;
441
        }
442

    
443

    
444

    
445
        public static bool InsertSymbolMapping(List<Tuple<string, string, string>> datas)
446
        {
447
            Project_Info projectInfo = Project_Info.GetInstance();
448
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
449
            {
450
                try
451
                {
452
                    connection.Open();
453
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
454
                    {
455
                        try
456
                        {
457
                            using (SQLiteCommand cmd = connection.CreateCommand())
458
                            {
459
                                foreach (var item in datas)
460
                                {
461
                                    cmd.Parameters.Clear();
462
                                    cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, NAME, SPPID_SYMBOL_PATH) VALUES (@UID, @NAME, @SPPID_SYMBOL_PATH)", SPPID_SYMBOL_MAPPING_TABLE);
463
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
464
                                    cmd.Parameters.AddWithValue("@NAME", item.Item2);
465
                                    cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", item.Item3);
466
                                    cmd.ExecuteNonQuery();
467
                                }
468
                            }
469
                            transaction.Commit();
470
                            connection.Close();
471
                        }
472
                        catch (Exception ex)
473
                        {
474
                            transaction.Rollback();
475
                        }
476
                        finally
477
                        {
478
                            transaction.Dispose();
479
                        }
480
                    }
481
                }
482
                catch (Exception ex)
483
                {
484
                    return false;
485
                }
486
                finally
487
                {
488
                    connection.Dispose();
489
                }
490
            }
491

    
492
            return true;
493
        }
494

    
495
        public static bool InsertAttributeMapping(List<Tuple<string, string>> datas)
496
        {
497
            Project_Info projectInfo = Project_Info.GetInstance();
498
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
499
            {
500
                try
501
                {
502
                    connection.Open();
503
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
504
                    {
505
                        try
506
                        {
507
                            using (SQLiteCommand cmd = connection.CreateCommand())
508
                            {
509
                                foreach (var item in datas)
510
                                {
511
                                    cmd.Parameters.Clear();
512
                                    cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, SPPID_ATTRIBUTE) VALUES (@UID, @SPPID_ATTRIBUTE)", SPPID_ATTRIBUTE_MAPPING_TABLE);
513
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
514
                                    cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", item.Item2);
515
                                    cmd.ExecuteNonQuery();
516
                                }
517
                            }
518
                            transaction.Commit();
519
                            connection.Close();
520
                        }
521
                        catch (Exception ex)
522
                        {
523
                            transaction.Rollback();
524
                        }
525
                        finally
526
                        {
527
                            transaction.Dispose();
528
                        }
529
                    }
530
                }
531
                catch (Exception ex)
532
                {
533
                    return false;
534
                }
535
                finally
536
                {
537
                    connection.Dispose();
538
                }
539
            }
540
            return true;
541
        }
542

    
543
        public static bool InsertLocationMapping(List<Tuple<string, int, bool>> datas)
544
        {
545
            Project_Info projectInfo = Project_Info.GetInstance();
546
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
547
            {
548
                try
549
                {
550
                    connection.Open();
551
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
552
                    {
553
                        try
554
                        {
555
                            using (SQLiteCommand cmd = connection.CreateCommand())
556
                            {
557
                                foreach (var item in datas)
558
                                {
559
                                    cmd.Parameters.Clear();
560
                                    cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, LOCATION, LEADERLINE) VALUES (@UID, @LOCATION, @LEADERLINE)", SPPID_LABEL_INFO_TABLE);
561
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
562
                                    cmd.Parameters.AddWithValue("@LOCATION", item.Item2);
563
                                    cmd.Parameters.AddWithValue("@LEADERLINE", item.Item3);
564
                                    cmd.ExecuteNonQuery();
565
                                }
566
                            }
567
                            transaction.Commit();
568
                            connection.Close();
569
                        }
570
                        catch (Exception ex)
571
                        {
572
                            transaction.Rollback();
573
                        }
574
                        finally
575
                        {
576
                            transaction.Dispose();
577
                        }
578
                    }
579
                }
580
                catch (Exception ex)
581
                {
582
                    return false;
583
                }
584
                finally
585
                {
586
                    connection.Dispose();
587
                }
588
            }
589
            return true;
590
        }
591
    }
592
}
클립보드 이미지 추가 (최대 크기: 500 MB)