프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

hytos / DTI_PID / BaseModel / Project_DB.cs @ bca86986

이력 | 보기 | 이력해설 | 다운로드 (13.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

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

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

    
42
            }
43
            finally
44
            {
45
                connection.Dispose();
46
            }
47

    
48
            return result;
49
        }
50

    
51
        public static bool SaveSPPID_DB_INFO(string jsonString)
52
        {
53
            Project_Info projectInfo = Project_Info.GetInstance();
54
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
55
            {
56
                
57
                try
58
                {
59
                    connection.Open();
60
                    using (SQLiteCommand cmd = connection.CreateCommand())
61
                    {
62
                        cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_DB_INFO_TABLE);
63
                        cmd.ExecuteNonQuery();
64

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

    
81
            return true;
82
        }
83

    
84
        public static DataTable SelectSPPID_DB_INFO()
85
        {
86
            DataTable dt = new DataTable();
87
            Project_Info projectInfo = Project_Info.GetInstance();
88
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
89
            {
90

    
91
                try
92
                {
93
                    connection.Open();
94
                    using (SQLiteCommand cmd = connection.CreateCommand())
95
                    {
96
                        cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DB_INFO_TABLE);
97
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
98
                            dt.Load(dr);
99
                    }
100
                    connection.Close();
101
                }
102
                catch (Exception ex)
103
                {
104

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

    
112
            return dt;
113
        }
114

    
115
        private static void CreateTable(SQLiteConnection connection)
116
        {
117
            using (SQLiteCommand cmd = connection.CreateCommand())
118
            {
119
                cmd.CommandText = "SELECT NAME FROM sqlite_master WHERE type='table'";
120
                using (SQLiteDataReader dr = cmd.ExecuteReader())
121
                using (DataTable dt = new DataTable())
122
                {
123
                    dt.Load(dr);
124

    
125
                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_DB_INFO_TABLE)).Length == 0)
126
                    {
127
                        cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString TEXT)", SPPID_DB_INFO_TABLE);
128
                        cmd.ExecuteNonQuery();
129
                    }
130
                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_SYMBOL_MAPPING_TABLE)).Length == 0)
131
                    {
132
                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, NAME TEXT, SPPID_SYMBOL_PATH TEXT)", SPPID_SYMBOL_MAPPING_TABLE);
133
                        cmd.ExecuteNonQuery();
134
                    }
135
                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_ATTRIBUTE_MAPPING_TABLE)).Length == 0)
136
                    {
137
                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, SPPID_ATTRIBUTE TEXT)", SPPID_ATTRIBUTE_MAPPING_TABLE);
138
                        cmd.ExecuteNonQuery();
139
                    }
140
                }
141
            }
142
        }
143

    
144
        public static DataTable SelectProjectSymbol()
145
        {
146
            DataTable dt = new DataTable();
147
            Project_Info projectInfo = Project_Info.GetInstance();
148
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
149
            {
150
                try
151
                {
152
                    connection.Open();
153
                    using (SQLiteCommand cmd = connection.CreateCommand())
154
                    {
155
                        cmd.CommandText = string.Format(@"
156
                            SELECT s.UID, s.Name, st.Type, sp.SPPID_SYMBOL_PATH FROM {1} as st, {0} as s 
157
                                LEFT OUTER JOIN {2} as sp 
158
                                    ON s.UID = SP.UID 
159
                            WHERE s.SymbolType_UID = st.UID 
160
                            ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, SPPID_SYMBOL_MAPPING_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
        public static DataTable SelectProjectLine()
180
        {
181
            DataTable dt = new DataTable();
182
            Project_Info projectInfo = Project_Info.GetInstance();
183
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
184
            {
185
                try
186
                {
187
                    connection.Open();
188
                    using (SQLiteCommand cmd = connection.CreateCommand())
189
                    {
190
                        cmd.CommandText = string.Format(@"
191
                            SELECT l.UID, l.Name, sp.SPPID_SYMBOL_PATH FROM {0} as l 
192
                                LEFT OUTER JOIN {1} as sp 
193
                                    ON l.UID = SP.UID ;", LineTypes_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
194
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
195
                            dt.Load(dr);
196
                    }
197
                    connection.Close();
198
                }
199
                catch (Exception ex)
200
                {
201

    
202
                }
203
                finally
204
                {
205
                    connection.Dispose();
206
                }
207
            }
208

    
209
            return dt;
210
        }
211

    
212
        public static DataTable SelectProjectLineProperties()
213
        {
214
            DataTable dt = new DataTable();
215
            Project_Info projectInfo = Project_Info.GetInstance();
216
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
217
            {
218
                try
219
                {
220
                    connection.Open();
221
                    using (SQLiteCommand cmd = connection.CreateCommand())
222
                    {
223
                        cmd.CommandText = string.Format(@"
224
                            SELECT lp.UID, lp.DisplayName, sp.SPPID_SYMBOL_PATH, spa.SPPID_ATTRIBUTE
225
                            FROM {0} as lp 
226
                                 LEFT OUTER JOIN {1} as sp 
227
                                      ON lp.UID = sp.UID
228
                                 LEFT OUTER JOIN {2} as spa 
229
                                      ON lp.UID = spa.UID;", LineProperties_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE);
230
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
231
                            dt.Load(dr);
232
                    }
233
                    connection.Close();
234
                }
235
                catch (Exception ex)
236
                {
237

    
238
                }
239
                finally
240
                {
241
                    connection.Dispose();
242
                }
243
            }
244

    
245
            return dt;
246
        }
247

    
248
        public static DataTable SelectProjectAssociation()
249
        {
250
            DataTable dt = new DataTable();
251
            Project_Info projectInfo = Project_Info.GetInstance();
252
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
253
            {
254
                try
255
                {
256
                    connection.Open();
257
                    using (SQLiteCommand cmd = connection.CreateCommand())
258
                    {
259
                        cmd.CommandText = string.Format(@"
260
                            SELECT sa.UID, sa.DisplayAttribute, st.TYPE, spa.SPPID_ATTRIBUTE, sp.SPPID_SYMBOL_PATH 
261
                            FROM {1} as sa, {0} as st 
262
                                 LEFT OUTER JOIN {2} as sp 
263
                                      ON sa.UID = SP.UID 
264
                                LEFT OUTER JOIN {3} as spa 
265
                                     ON sa.UID = spa.UID
266
                            WHERE sa.SymbolType_UID = st.UID;", SymbolType_TABLE, SymbolAttribute_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE);
267
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
268
                            dt.Load(dr);
269
                    }
270
                    connection.Close();
271
                }
272
                catch (Exception ex)
273
                {
274

    
275
                }
276
                finally
277
                {
278
                    connection.Dispose();
279
                }
280
            }
281

    
282
            return dt;
283
        }
284

    
285

    
286

    
287
        public static bool InsertSymbolMapping(List<Tuple<string, string, string>> datas)
288
        {
289
            Project_Info projectInfo = Project_Info.GetInstance();
290
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
291
            {
292
                try
293
                {
294
                    connection.Open();
295
                    using (SQLiteCommand cmd = connection.CreateCommand())
296
                    {
297
                        foreach (var item in datas)
298
                        {
299
                            cmd.Parameters.Clear();
300
                            cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, NAME, SPPID_SYMBOL_PATH) VALUES (@UID, @NAME, @SPPID_SYMBOL_PATH)", SPPID_SYMBOL_MAPPING_TABLE);
301
                            cmd.Parameters.AddWithValue("@UID", item.Item1);
302
                            cmd.Parameters.AddWithValue("@NAME", item.Item2);
303
                            cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", item.Item3);
304
                            cmd.ExecuteNonQuery();
305
                        }
306
                    }
307
                    connection.Close();
308
                }
309
                catch (Exception ex)
310
                {
311
                    return false;
312
                }
313
                finally
314
                {
315
                    connection.Dispose();
316
                }
317
            }
318

    
319
            return true;
320
        }
321

    
322
        public static bool InsertAttributeMapping(List<Tuple<string, string>> datas)
323
        {
324
            Project_Info projectInfo = Project_Info.GetInstance();
325
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
326
            {
327
                try
328
                {
329
                    connection.Open();
330
                    using (SQLiteCommand cmd = connection.CreateCommand())
331
                    {
332
                        foreach (var item in datas)
333
                        {
334
                            cmd.Parameters.Clear();
335
                            cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, SPPID_ATTRIBUTE) VALUES (@UID, @SPPID_ATTRIBUTE)", SPPID_ATTRIBUTE_MAPPING_TABLE);
336
                            cmd.Parameters.AddWithValue("@UID", item.Item1);
337
                            cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", item.Item2);
338
                            cmd.ExecuteNonQuery();
339
                        }
340
                    }
341
                    connection.Close();
342
                }
343
                catch (Exception ex)
344
                {
345
                    return false;
346
                }
347
                finally
348
                {
349
                    connection.Dispose();
350
                }
351
            }
352

    
353
            return true;
354
        }
355
    }
356
}
클립보드 이미지 추가 (최대 크기: 500 MB)