프로젝트

일반

사용자정보

통계
| 개정판:

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

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

1 b18dc619 gaqhf
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6 fab4f207 gaqhf
using System.Globalization;
7
using System.Data.SQLite;
8
using System.Data;
9 8847ea67 gaqhf
using System.Data.SqlClient;
10 224535bb gaqhf
using Newtonsoft.Json;
11 b18dc619 gaqhf
12 171142c5 gaqhf
namespace Converter.BaseModel
13 b18dc619 gaqhf
{
14 171142c5 gaqhf
    public class Project_DB
15 b18dc619 gaqhf
    {
16 bca86986 gaqhf
        const string SPPID_DB_INFO_TABLE = "T_SPPID_CONNECTION_INFO";
17
        const string SPPID_SYMBOL_MAPPING_TABLE = "T_SPPID_SYMBOL_MAPPING";
18
        const string SPPID_ATTRIBUTE_MAPPING_TABLE = "T_SPPID_ATTRIBUTE_MAPPING";
19 7cbb1038 gaqhf
        const string SPPID_SETTING_TABLE = "T_SPPID_SETTING_TABLE";
20 b8bd98aa gaqhf
        const string SPPID_LABEL_INFO_TABLE = "T_SPPID_LABEL_INFO";
21 e8536f2b gaqhf
        const string SPPID_DRAWING_INFO = "T_SPPID_DRAWING_INFO";
22 154d8f43 gaqhf
        const string SPPID_OPC_INFO = "T_SPPID_OPC_INFO";
23 171142c5 gaqhf
24 bca86986 gaqhf
        const string LineProperties_TABLE = "LineProperties";
25
        const string LineTypes_TABLE = "LineTypes";
26
        const string SymbolType_TABLE = "SymbolType";
27
        const string SymbolAttribute_TABLE = "SymbolAttribute";
28
        const string Symbol_TABLE = "Symbol";
29 154d8f43 gaqhf
        const string OPCRelations_TABLE = "OPCRelations";
30 171142c5 gaqhf
31 8847ea67 gaqhf
        private static SqlConnection GetSqlConnection()
32 fab4f207 gaqhf
        {
33
            Project_Info projectInfo = Project_Info.GetInstance();
34 8847ea67 gaqhf
            SqlConnection connection = null;
35 fab4f207 gaqhf
            try
36
            {
37 8847ea67 gaqhf
                connection = new SqlConnection(string.Format(CultureInfo.CurrentCulture,
38
                    @"Data Source = {0},{1}; Initial Catalog = {2}; User ID = {3}; Password = {4};",
39
                    projectInfo.ServerIP,
40
                    projectInfo.Port,
41
                    System.IO.Path.GetFileName(projectInfo.DefaultPath),
42
                    projectInfo.DBUser,
43
                    projectInfo.DBPassword));
44
45 fab4f207 gaqhf
                connection.Open();
46
            }
47
            catch (Exception ex)
48
            {
49
                connection.Dispose();
50 8847ea67 gaqhf
                connection = null;
51 fab4f207 gaqhf
            }
52
53 8847ea67 gaqhf
            return connection;
54 fab4f207 gaqhf
        }
55
56 8847ea67 gaqhf
        public static bool ConnTestAndCreateTable()
57 fab4f207 gaqhf
        {
58 8847ea67 gaqhf
            bool result = false;
59 fab4f207 gaqhf
            Project_Info projectInfo = Project_Info.GetInstance();
60 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
61 fab4f207 gaqhf
            {
62 8847ea67 gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, @"Data Source = {0}", projectInfo.DBFilePath)))
63 fab4f207 gaqhf
                {
64 8847ea67 gaqhf
                    try
65 fab4f207 gaqhf
                    {
66 8847ea67 gaqhf
                        connection.Open();
67
                        if (connection.State == ConnectionState.Open)
68
                        {
69
                            using (SQLiteCommand cmd = connection.CreateCommand())
70
                            {
71
                                cmd.CommandText = "SELECT NAME FROM sqlite_master WHERE type='table'";
72
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
73
                                using (DataTable dt = new DataTable())
74
                                {
75
                                    dt.Load(dr);
76
77
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_DB_INFO_TABLE)).Length == 0)
78
                                    {
79
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString TEXT)", SPPID_DB_INFO_TABLE);
80
                                        cmd.ExecuteNonQuery();
81
                                    }
82
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_SETTING_TABLE)).Length == 0)
83
                                    {
84
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString TEXT, SettingType TEXT)", SPPID_SETTING_TABLE);
85
                                        cmd.ExecuteNonQuery();
86
                                    }
87
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_SYMBOL_MAPPING_TABLE)).Length == 0)
88
                                    {
89
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, NAME TEXT, SPPID_SYMBOL_PATH TEXT, LEADERLINE BOOLEAN)", SPPID_SYMBOL_MAPPING_TABLE);
90
                                        cmd.ExecuteNonQuery();
91
                                    }
92
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_ATTRIBUTE_MAPPING_TABLE)).Length == 0)
93
                                    {
94
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, SPPID_ATTRIBUTE TEXT)", SPPID_ATTRIBUTE_MAPPING_TABLE);
95
                                        cmd.ExecuteNonQuery();
96
                                    }
97
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_LABEL_INFO_TABLE)).Length == 0)
98
                                    {
99
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, LOCATION INT DEFAULT 0, LEADERLINE BOOLEAN)", SPPID_LABEL_INFO_TABLE);
100
                                        cmd.ExecuteNonQuery();
101
                                    }
102
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_DRAWING_INFO)).Length == 0)
103
                                    {
104
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_DRAWING_UID Text PRIMARY KEY, PATH TEXT, DRAWINGNUMBER TEXT, DRAWINGNAME TEXT)", SPPID_DRAWING_INFO);
105
                                        cmd.ExecuteNonQuery();
106
                                    }
107
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_OPC_INFO)).Length == 0)
108
                                    {
109
                                        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);
110
                                        cmd.ExecuteNonQuery();
111
                                    }
112
                                }
113
114
                                #region Check Column 업데이트시 예비용
115
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_SYMBOL_MAPPING_TABLE);
116
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
117
                                using (DataTable dt = new DataTable())
118
                                {
119
                                    dt.Load(dr);
120
                                    if (!dt.Columns.Contains("LEADERLINE"))
121
                                    {
122
                                        cmd.CommandText = string.Format("ALTER TABLE {0} ADD COLUMN LEADERLINE BOOLEAN", SPPID_SYMBOL_MAPPING_TABLE);
123
                                        cmd.ExecuteNonQuery();
124
                                    }
125
                                }
126
127
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_SETTING_TABLE);
128
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
129
                                using (DataTable dt = new DataTable())
130
                                {
131
                                    dt.Load(dr);
132
                                    if (!dt.Columns.Contains("SettingType"))
133
                                    {
134
                                        cmd.CommandText = string.Format("ALTER TABLE {0} ADD COLUMN SettingType Text", SPPID_SETTING_TABLE);
135
                                        cmd.ExecuteNonQuery();
136
                                    }
137
                                }
138 fab4f207 gaqhf
139 8847ea67 gaqhf
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DRAWING_INFO);
140
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
141
                                using (DataTable dt = new DataTable())
142
                                {
143
                                    dt.Load(dr);
144
                                    if (dt.Columns.Contains("DOCUMENT"))
145
                                    {
146
                                        cmd.CommandText = string.Format("DROP TABLE {0}", SPPID_DRAWING_INFO);
147
                                        cmd.ExecuteNonQuery();
148
149
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_DRAWING_UID Text PRIMARY KEY, PATH TEXT, DRAWINGNUMBER TEXT, DRAWINGNAME TEXT)", SPPID_DRAWING_INFO);
150
                                        cmd.ExecuteNonQuery();
151
                                    }
152
                                }
153
                                #endregion
154
                            }
155
                            result = true;
156
                        }
157
                        connection.Close();
158
                    }
159
                    catch (Exception ex)
160
                    {
161
162
                    }
163
                    finally
164
                    {
165
                        connection.Dispose();
166 fab4f207 gaqhf
                    }
167
                }
168
            }
169 8847ea67 gaqhf
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
170 fab4f207 gaqhf
            {
171 8847ea67 gaqhf
                using (SqlConnection connection = GetSqlConnection())
172 fab4f207 gaqhf
                {
173 8847ea67 gaqhf
                    try
174 fab4f207 gaqhf
                    {
175 8847ea67 gaqhf
                        if (connection != null && connection.State == ConnectionState.Open)
176
                        {
177
                            using (SqlCommand cmd = connection.CreateCommand())
178
                            {
179
                                cmd.CommandText = "SELECT TABLE_NAME AS NAME FROM INFORMATION_SCHEMA.TABLES";
180
                                using (SqlDataReader dr = cmd.ExecuteReader())
181
                                using (DataTable dt = new DataTable())
182
                                {
183
                                    dt.Load(dr);
184
185
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_DB_INFO_TABLE)).Length == 0)
186
                                    {
187 d2a7bef1 gaqhf
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString varchar(MAX))", SPPID_DB_INFO_TABLE);
188 8847ea67 gaqhf
                                        cmd.ExecuteNonQuery();
189
                                    }
190
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_SETTING_TABLE)).Length == 0)
191
                                    {
192 d2a7bef1 gaqhf
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString varchar(MAX), SettingType varchar(MAX))", SPPID_SETTING_TABLE);
193 8847ea67 gaqhf
                                        cmd.ExecuteNonQuery();
194
                                    }
195
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_SYMBOL_MAPPING_TABLE)).Length == 0)
196
                                    {
197 d2a7bef1 gaqhf
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID varchar(255) PRIMARY KEY, NAME varchar(MAX), SPPID_SYMBOL_PATH varchar(MAX), LEADERLINE BIT)", SPPID_SYMBOL_MAPPING_TABLE);
198 8847ea67 gaqhf
                                        cmd.ExecuteNonQuery();
199
                                    }
200
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_ATTRIBUTE_MAPPING_TABLE)).Length == 0)
201
                                    {
202 d2a7bef1 gaqhf
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID varchar(255) PRIMARY KEY, SPPID_ATTRIBUTE varchar(MAX))", SPPID_ATTRIBUTE_MAPPING_TABLE);
203 8847ea67 gaqhf
                                        cmd.ExecuteNonQuery();
204
                                    }
205
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_LABEL_INFO_TABLE)).Length == 0)
206
                                    {
207
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID varchar(255) PRIMARY KEY, LOCATION INT DEFAULT 0, LEADERLINE BIT)", SPPID_LABEL_INFO_TABLE);
208
                                        cmd.ExecuteNonQuery();
209
                                    }
210
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_DRAWING_INFO)).Length == 0)
211
                                    {
212 d2a7bef1 gaqhf
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_DRAWING_UID varchar(255) PRIMARY KEY, PATH varchar(MAX), DRAWINGNUMBER varchar(MAX), DRAWINGNAME varchar(MAX))", SPPID_DRAWING_INFO);
213 8847ea67 gaqhf
                                        cmd.ExecuteNonQuery();
214
                                    }
215
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_OPC_INFO)).Length == 0)
216
                                    {
217 d2a7bef1 gaqhf
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_OPC_UID varchar(255) PRIMARY KEY, SPPID_OPC_MODELITEM_ID varchar(MAX), ID2_DRAWING_UID varchar(MAX), ATTRIBUTES varchar(MAX), PAIRED BIT)", SPPID_OPC_INFO);
218 8847ea67 gaqhf
                                        cmd.ExecuteNonQuery();
219
                                    }
220
                                }
221
222
                                #region Check Column 업데이트시 예비용
223
                                #endregion
224
                            }
225
                            result = true;
226
                        }
227 fab4f207 gaqhf
                    }
228 8847ea67 gaqhf
                    catch (Exception ex)
229
                    {
230 fab4f207 gaqhf
231 8847ea67 gaqhf
                    }
232
                    finally
233
                    {
234
                        if (connection != null)
235
                            connection.Dispose();
236
                    }
237 fab4f207 gaqhf
                }
238
            }
239
240 8847ea67 gaqhf
            return result;
241 fab4f207 gaqhf
        }
242
243 8847ea67 gaqhf
        public static bool SaveSPPID_DB_INFO(string jsonString)
244 e00e891d gaqhf
        {
245
            Project_Info projectInfo = Project_Info.GetInstance();
246 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
247 e00e891d gaqhf
            {
248 8847ea67 gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
249 e00e891d gaqhf
                {
250
251 8847ea67 gaqhf
                    try
252
                    {
253
                        connection.Open();
254
                        using (SQLiteCommand cmd = connection.CreateCommand())
255 7cbb1038 gaqhf
                        {
256 8847ea67 gaqhf
                            cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_DB_INFO_TABLE);
257
                            cmd.ExecuteNonQuery();
258
259
                            cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString)", SPPID_DB_INFO_TABLE);
260
                            cmd.Parameters.AddWithValue("@jsonString", jsonString);
261 7cbb1038 gaqhf
                            cmd.ExecuteNonQuery();
262
                        }
263 8847ea67 gaqhf
                        connection.Close();
264
                    }
265
                    catch (Exception ex)
266
                    {
267
                        return false;
268
                    }
269
                    finally
270
                    {
271
                        connection.Dispose();
272 e00e891d gaqhf
                    }
273
                }
274 8847ea67 gaqhf
            }
275
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
276
            {
277
                using (SqlConnection connection = GetSqlConnection())
278 e00e891d gaqhf
                {
279 8847ea67 gaqhf
                    try
280
                    {
281
                        if (connection != null && connection.State == ConnectionState.Open)
282
                        {
283 d2a7bef1 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
284
                            {
285
                                cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_DB_INFO_TABLE);
286
                                cmd.ExecuteNonQuery();
287 8847ea67 gaqhf
288 d2a7bef1 gaqhf
                                cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString)", SPPID_DB_INFO_TABLE);
289
                                cmd.Parameters.AddWithValue("@jsonString", jsonString);
290
                                cmd.ExecuteNonQuery();
291
                            }
292
                            connection.Close();
293
                        }
294
                        else
295
                        {
296
                            return false;
297 8847ea67 gaqhf
                        }
298
                    }
299
                    catch (Exception ex)
300
                    {
301 d2a7bef1 gaqhf
                        return false;
302 8847ea67 gaqhf
                    }
303
                    finally
304
                    {
305
                        if (connection != null)
306
                            connection.Dispose();
307
                    }
308 e00e891d gaqhf
                }
309
            }
310
311 8847ea67 gaqhf
312 e00e891d gaqhf
            return true;
313
        }
314
315 8847ea67 gaqhf
        public static DataTable SelectSPPID_DB_INFO()
316 e00e891d gaqhf
        {
317
            DataTable dt = new DataTable();
318
            Project_Info projectInfo = Project_Info.GetInstance();
319 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
320 e00e891d gaqhf
            {
321 8847ea67 gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
322 e00e891d gaqhf
                {
323 8847ea67 gaqhf
                    try
324 e00e891d gaqhf
                    {
325 8847ea67 gaqhf
                        connection.Open();
326
                        using (SQLiteCommand cmd = connection.CreateCommand())
327
                        {
328
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DB_INFO_TABLE);
329
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
330
                                dt.Load(dr);
331
                        }
332
                        connection.Close();
333 e00e891d gaqhf
                    }
334 8847ea67 gaqhf
                    catch (Exception ex)
335
                    {
336 e00e891d gaqhf
337 8847ea67 gaqhf
                    }
338
                    finally
339
                    {
340
                        connection.Dispose();
341
                    }
342 e00e891d gaqhf
                }
343 8847ea67 gaqhf
            }
344
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
345
            {
346
                using (SqlConnection connection = GetSqlConnection())
347 e00e891d gaqhf
                {
348 8847ea67 gaqhf
                    try
349
                    {
350
                        if (connection != null && connection.State == ConnectionState.Open)
351
                        {
352 d2a7bef1 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
353
                            {
354
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DB_INFO_TABLE);
355
                                using (SqlDataReader dr = cmd.ExecuteReader())
356
                                    dt.Load(dr);
357
                            }
358
                            connection.Close();
359 8847ea67 gaqhf
                        }
360
                    }
361
                    catch (Exception ex)
362
                    {
363
364
                    }
365
                    finally
366
                    {
367
                        if (connection != null)
368
                            connection.Dispose();
369
                    }
370 e00e891d gaqhf
                }
371
            }
372
373
            return dt;
374
        }
375
376 8847ea67 gaqhf
        public static bool SaveETCSetting(Dictionary<string,string> dicSetting)
377 fab4f207 gaqhf
        {
378 8847ea67 gaqhf
            Project_Info projectInfo = Project_Info.GetInstance();
379
            if (projectInfo.DBType == ID2DB_Type.SQLite)
380 fab4f207 gaqhf
            {
381 8847ea67 gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
382 fab4f207 gaqhf
                {
383
384 8847ea67 gaqhf
                    try
385 e00e891d gaqhf
                    {
386 8847ea67 gaqhf
                        connection.Open();
387
                        using (SQLiteCommand cmd = connection.CreateCommand())
388
                        {
389
                            cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_SETTING_TABLE);
390
                            cmd.ExecuteNonQuery();
391
392
                            foreach (var item in dicSetting)
393
                            {
394
                                cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString, @SettingType)", SPPID_SETTING_TABLE);
395 f4485671 gaqhf
                                cmd.Parameters.Clear();
396 8847ea67 gaqhf
                                cmd.Parameters.AddWithValue("@jsonString", item.Value);
397
                                cmd.Parameters.AddWithValue("@SettingType", item.Key);
398
                                cmd.ExecuteNonQuery();
399
                            }
400
                        }
401
                        connection.Close();
402 e00e891d gaqhf
                    }
403 8847ea67 gaqhf
                    catch (Exception ex)
404 bca86986 gaqhf
                    {
405 8847ea67 gaqhf
                        return false;
406 bca86986 gaqhf
                    }
407 8847ea67 gaqhf
                    finally
408 bca86986 gaqhf
                    {
409 8847ea67 gaqhf
                        connection.Dispose();
410 bca86986 gaqhf
                    }
411 8847ea67 gaqhf
                }
412
            }
413
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
414
            {
415
                using (SqlConnection connection = GetSqlConnection())
416
                {
417
                    try
418 1a3a74a8 gaqhf
                    {
419 8847ea67 gaqhf
                        if (connection != null && connection.State == ConnectionState.Open)
420
                        {
421 d2a7bef1 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
422
                            {
423
                                cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_SETTING_TABLE);
424
                                cmd.ExecuteNonQuery();
425 8847ea67 gaqhf
426 d2a7bef1 gaqhf
                                foreach (var item in dicSetting)
427
                                {
428
                                    cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString, @SettingType)", SPPID_SETTING_TABLE);
429 f4485671 gaqhf
                                    cmd.Parameters.Clear();
430 d2a7bef1 gaqhf
                                    cmd.Parameters.AddWithValue("@jsonString", item.Value);
431
                                    cmd.Parameters.AddWithValue("@SettingType", item.Key);
432
                                    cmd.ExecuteNonQuery();
433
                                }
434
                            }
435
                            connection.Close();
436 8847ea67 gaqhf
                        }
437 d2a7bef1 gaqhf
                        else
438
                            return false;
439 1a3a74a8 gaqhf
                    }
440 8847ea67 gaqhf
                    catch (Exception ex)
441 e8536f2b gaqhf
                    {
442 d2a7bef1 gaqhf
                        return false;
443 154d8f43 gaqhf
                    }
444 8847ea67 gaqhf
                    finally
445 154d8f43 gaqhf
                    {
446 8847ea67 gaqhf
                        if (connection != null)
447
                            connection.Dispose();
448 e8536f2b gaqhf
                    }
449 fab4f207 gaqhf
                }
450 8847ea67 gaqhf
            }
451 cf924377 gaqhf
452 8847ea67 gaqhf
            return true;
453
        }
454
455
        public static DataTable SelectSetting()
456
        {
457
            DataTable dt = new DataTable();
458
            Project_Info projectInfo = Project_Info.GetInstance();
459
            if (projectInfo.DBType == ID2DB_Type.SQLite)
460
            {
461
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
462 cf924377 gaqhf
                {
463 8847ea67 gaqhf
                    try
464 cf924377 gaqhf
                    {
465 8847ea67 gaqhf
                        connection.Open();
466
                        using (SQLiteCommand cmd = connection.CreateCommand())
467
                        {
468
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_SETTING_TABLE);
469
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
470
                                dt.Load(dr);
471
                        }
472
                        connection.Close();
473 cf924377 gaqhf
                    }
474 8847ea67 gaqhf
                    catch (Exception ex)
475
                    {
476 7cbb1038 gaqhf
477 8847ea67 gaqhf
                    }
478
                    finally
479 7cbb1038 gaqhf
                    {
480 8847ea67 gaqhf
                        connection.Dispose();
481 7cbb1038 gaqhf
                    }
482
                }
483 8847ea67 gaqhf
            }
484
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
485
            {
486
                using (SqlConnection connection = GetSqlConnection())
487 224535bb gaqhf
                {
488 8847ea67 gaqhf
                    try
489
                    {
490
                        if (connection != null && connection.State == ConnectionState.Open)
491
                        {
492 d2a7bef1 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
493
                            {
494
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_SETTING_TABLE);
495
                                using (SqlDataReader dr = cmd.ExecuteReader())
496
                                    dt.Load(dr);
497
                            }
498
                            connection.Close();
499 8847ea67 gaqhf
                        }
500
                    }
501
                    catch (Exception ex)
502 224535bb gaqhf
                    {
503 154d8f43 gaqhf
504 8847ea67 gaqhf
                    }
505
                    finally
506
                    {
507
                        if (connection != null)
508
                            connection.Dispose();
509 224535bb gaqhf
                    }
510
                }
511 fab4f207 gaqhf
            }
512 8847ea67 gaqhf
513
            return dt;
514 fab4f207 gaqhf
        }
515 bca86986 gaqhf
516
        public static DataTable SelectProjectSymbol()
517
        {
518
            DataTable dt = new DataTable();
519
            Project_Info projectInfo = Project_Info.GetInstance();
520 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
521 bca86986 gaqhf
            {
522 8847ea67 gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
523 bca86986 gaqhf
                {
524 8847ea67 gaqhf
                    try
525 bca86986 gaqhf
                    {
526 8847ea67 gaqhf
                        connection.Open();
527
                        using (SQLiteCommand cmd = connection.CreateCommand())
528
                        {
529
                            cmd.CommandText = string.Format(@"
530 cf924377 gaqhf
                            SELECT s.UID, s.Name, st.Type, sp.SPPID_SYMBOL_PATH, sp.LEADERLINE FROM {1} as st, {0} as s 
531 bca86986 gaqhf
                                LEFT OUTER JOIN {2} as sp 
532
                                    ON s.UID = SP.UID 
533
                            WHERE s.SymbolType_UID = st.UID 
534
                            ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
535 8847ea67 gaqhf
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
536
                                dt.Load(dr);
537 310aeb31 gaqhf
538 8847ea67 gaqhf
                            DataTable dtClone = dt.Clone();
539
                            dtClone.Columns["UID"].DataType = typeof(string);
540
                            foreach (DataRow row in dt.Rows)
541
                            {
542
                                dtClone.ImportRow(row);
543
                            }
544
                            dt.Dispose();
545
                            dt = dtClone;
546 310aeb31 gaqhf
                        }
547 8847ea67 gaqhf
                        connection.Close();
548 bca86986 gaqhf
                    }
549 8847ea67 gaqhf
                    catch (Exception ex)
550
                    {
551 bca86986 gaqhf
552 8847ea67 gaqhf
                    }
553
                    finally
554
                    {
555
                        connection.Dispose();
556
                    }
557 bca86986 gaqhf
                }
558 8847ea67 gaqhf
            }
559
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
560
            {
561
                using (SqlConnection connection = GetSqlConnection())
562 bca86986 gaqhf
                {
563 8847ea67 gaqhf
                    try
564
                    {
565
                        if (connection != null && connection.State == ConnectionState.Open)
566
                        {
567 d2a7bef1 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
568
                            {
569
                                cmd.CommandText = string.Format(@"
570 a2c803a0 gaqhf
                            SELECT CONVERT(VARCHAR(255), s.UID) AS UID, s.Name, st.Type, sp.SPPID_SYMBOL_PATH, sp.LEADERLINE FROM {1} as st, {0} as s 
571 d2a7bef1 gaqhf
                                LEFT OUTER JOIN {2} as sp 
572 a2c803a0 gaqhf
                                    ON CONVERT(VARCHAR(255), s.UID) = CONVERT(VARCHAR(255), SP.UID)
573 d2a7bef1 gaqhf
                            WHERE s.SymbolType_UID = st.UID 
574
                            ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
575
                                using (SqlDataReader dr = cmd.ExecuteReader())
576
                                    dt.Load(dr);
577
                            }
578
                            connection.Close();
579 8847ea67 gaqhf
                        }
580
                    }
581
                    catch (Exception ex)
582
                    {
583
584
                    }
585
                    finally
586
                    {
587
                        if (connection != null)
588
                            connection.Dispose();
589
                    }
590 bca86986 gaqhf
                }
591
            }
592
593
            return dt;
594
        }
595
596 4b4dbca9 gaqhf
        public static DataTable SelectProjectChildSymbol()
597
        {
598
            DataTable result = new DataTable();
599
            result.Columns.Add("UID");
600
            result.Columns.Add("Name");
601
            result.Columns.Add("Type");
602
            result.Columns.Add("SPPID_SYMBOL_PATH");
603
            
604
            Project_Info projectInfo = Project_Info.GetInstance();
605 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
606 4b4dbca9 gaqhf
            {
607 8847ea67 gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
608
                using (DataTable dt = new DataTable())
609 4b4dbca9 gaqhf
                {
610 8847ea67 gaqhf
                    try
611 4b4dbca9 gaqhf
                    {
612 8847ea67 gaqhf
                        connection.Open();
613
                        using (SQLiteCommand cmd = connection.CreateCommand())
614
                        {
615
                            cmd.CommandText = string.Format(@"
616 4b4dbca9 gaqhf
                            SELECT AdditionalSymbol FROM Symbol");
617 8847ea67 gaqhf
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
618
                                dt.Load(dr);
619
                            List<string> childList = new List<string>();
620
                            foreach (DataRow row in dt.Rows)
621 4b4dbca9 gaqhf
                            {
622 8847ea67 gaqhf
                                if (row["AdditionalSymbol"] != null && !DBNull.Value.Equals(row["AdditionalSymbol"]) && !string.IsNullOrEmpty((string)row["AdditionalSymbol"]))
623 4b4dbca9 gaqhf
                                {
624 8847ea67 gaqhf
                                    string[] array = row["AdditionalSymbol"].ToString().Split(new char[] { '/' });
625
                                    foreach (var childString in array)
626
                                    {
627
                                        childList.Add(childString.Split(new char[] { ',' })[2]);
628
                                    }
629 4b4dbca9 gaqhf
                                }
630
631 8847ea67 gaqhf
                            }
632 4b4dbca9 gaqhf
633 8847ea67 gaqhf
                            dt.Clear();
634
                            cmd.Reset();
635
                            cmd.CommandText = string.Format(@"
636 4b4dbca9 gaqhf
                            SELECT * FROM {0}", SPPID_SYMBOL_MAPPING_TABLE);
637 8847ea67 gaqhf
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
638
                                dt.Load(dr);
639 4b4dbca9 gaqhf
640 8847ea67 gaqhf
                            childList = childList.Distinct().ToList();
641
                            foreach (var child in childList)
642
                            {
643
                                string mappingPath = string.Empty;
644
                                DataRow[] rows = dt.Select(string.Format("UID = '{0}'", child));
645
                                if (rows.Length == 1)
646
                                    mappingPath = !DBNull.Value.Equals(rows[0]["SPPID_SYMBOL_PATH"]) ? (string)rows[0]["SPPID_SYMBOL_PATH"] : null;
647
648
                                DataRow newRow = result.NewRow();
649
                                newRow["UID"] = child;
650
                                newRow["Name"] = child;
651
                                newRow["Type"] = "Child Symbol";
652
                                newRow["SPPID_SYMBOL_PATH"] = mappingPath;
653
                                result.Rows.Add(newRow);
654
                            }
655 4b4dbca9 gaqhf
                        }
656 8847ea67 gaqhf
                        connection.Close();
657 4b4dbca9 gaqhf
                    }
658 8847ea67 gaqhf
                    catch (Exception ex)
659
                    {
660 4b4dbca9 gaqhf
661 8847ea67 gaqhf
                    }
662
                    finally
663
                    {
664
                        connection.Dispose();
665
                    }
666 4b4dbca9 gaqhf
                }
667 8847ea67 gaqhf
            }
668
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
669
            {
670
                using (SqlConnection connection = GetSqlConnection())
671 d2a7bef1 gaqhf
                using (DataTable dt = new DataTable())
672 4b4dbca9 gaqhf
                {
673 8847ea67 gaqhf
                    try
674
                    {
675
                        if (connection != null && connection.State == ConnectionState.Open)
676
                        {
677 d2a7bef1 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
678
                            {
679
                                cmd.CommandText = string.Format(@"
680
                            SELECT AdditionalSymbol FROM Symbol");
681
                                using (SqlDataReader dr = cmd.ExecuteReader())
682
                                    dt.Load(dr);
683
                                List<string> childList = new List<string>();
684
                                foreach (DataRow row in dt.Rows)
685
                                {
686
                                    if (row["AdditionalSymbol"] != null && !DBNull.Value.Equals(row["AdditionalSymbol"]) && !string.IsNullOrEmpty((string)row["AdditionalSymbol"]))
687
                                    {
688
                                        string[] array = row["AdditionalSymbol"].ToString().Split(new char[] { '/' });
689
                                        foreach (var childString in array)
690
                                        {
691
                                            childList.Add(childString.Split(new char[] { ',' })[2]);
692
                                        }
693
                                    }
694
695
                                }
696
697
                                dt.Clear();
698
                                cmd.CommandText = string.Format(@"
699
                            SELECT * FROM {0}", SPPID_SYMBOL_MAPPING_TABLE);
700
                                using (SqlDataReader dr = cmd.ExecuteReader())
701
                                    dt.Load(dr);
702 8847ea67 gaqhf
703 d2a7bef1 gaqhf
                                childList = childList.Distinct().ToList();
704
                                foreach (var child in childList)
705
                                {
706
                                    string mappingPath = string.Empty;
707
                                    DataRow[] rows = dt.Select(string.Format("UID = '{0}'", child));
708
                                    if (rows.Length == 1)
709
                                        mappingPath = !DBNull.Value.Equals(rows[0]["SPPID_SYMBOL_PATH"]) ? (string)rows[0]["SPPID_SYMBOL_PATH"] : null;
710
711
                                    DataRow newRow = result.NewRow();
712
                                    newRow["UID"] = child;
713
                                    newRow["Name"] = child;
714
                                    newRow["Type"] = "Child Symbol";
715
                                    newRow["SPPID_SYMBOL_PATH"] = mappingPath;
716
                                    result.Rows.Add(newRow);
717
                                }
718
                            }
719
                            connection.Close();
720 8847ea67 gaqhf
                        }
721
                    }
722
                    catch (Exception ex)
723
                    {
724
725
                    }
726
                    finally
727
                    {
728
                        if (connection != null)
729
                            connection.Dispose();
730
                    }
731 4b4dbca9 gaqhf
                }
732
            }
733
            return result;
734
        }
735
736 bca86986 gaqhf
        public static DataTable SelectProjectLine()
737
        {
738
            DataTable dt = new DataTable();
739
            Project_Info projectInfo = Project_Info.GetInstance();
740 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
741 bca86986 gaqhf
            {
742 8847ea67 gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
743 bca86986 gaqhf
                {
744 8847ea67 gaqhf
                    try
745 bca86986 gaqhf
                    {
746 8847ea67 gaqhf
                        connection.Open();
747
                        using (SQLiteCommand cmd = connection.CreateCommand())
748
                        {
749
                            cmd.CommandText = string.Format(@"
750 bca86986 gaqhf
                            SELECT l.UID, l.Name, sp.SPPID_SYMBOL_PATH FROM {0} as l 
751
                                LEFT OUTER JOIN {1} as sp 
752
                                    ON l.UID = SP.UID ;", LineTypes_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
753 8847ea67 gaqhf
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
754
                                dt.Load(dr);
755
                        }
756
                        connection.Close();
757 bca86986 gaqhf
                    }
758 8847ea67 gaqhf
                    catch (Exception ex)
759
                    {
760 bca86986 gaqhf
761 8847ea67 gaqhf
                    }
762
                    finally
763
                    {
764
                        connection.Dispose();
765
                    }
766 bca86986 gaqhf
                }
767 8847ea67 gaqhf
            }
768
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
769
            {
770
                using (SqlConnection connection = GetSqlConnection())
771 bca86986 gaqhf
                {
772 8847ea67 gaqhf
                    try
773
                    {
774
                        if (connection != null && connection.State == ConnectionState.Open)
775
                        {
776 d2a7bef1 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
777
                            {
778
                                cmd.CommandText = string.Format(@"
779
                            SELECT l.UID, l.Name, sp.SPPID_SYMBOL_PATH FROM {0} as l 
780
                                LEFT OUTER JOIN {1} as sp 
781
                                    ON l.UID = SP.UID ;", LineTypes_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
782
                                using (SqlDataReader dr = cmd.ExecuteReader())
783
                                    dt.Load(dr);
784
                            }
785
                            connection.Close();
786 8847ea67 gaqhf
                        }
787
                    }
788
                    catch (Exception ex)
789
                    {
790
791
                    }
792
                    finally
793
                    {
794
                        if (connection != null)
795
                            connection.Dispose();
796
                    }
797 bca86986 gaqhf
                }
798
            }
799
800
            return dt;
801
        }
802
803
        public static DataTable SelectProjectLineProperties()
804
        {
805
            DataTable dt = new DataTable();
806
            Project_Info projectInfo = Project_Info.GetInstance();
807 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
808 bca86986 gaqhf
            {
809 8847ea67 gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
810 bca86986 gaqhf
                {
811 8847ea67 gaqhf
                    try
812 bca86986 gaqhf
                    {
813 8847ea67 gaqhf
                        connection.Open();
814
                        using (SQLiteCommand cmd = connection.CreateCommand())
815
                        {
816
                            cmd.CommandText = string.Format(@"
817 bca86986 gaqhf
                            SELECT lp.UID, lp.DisplayName, sp.SPPID_SYMBOL_PATH, spa.SPPID_ATTRIBUTE
818
                            FROM {0} as lp 
819
                                 LEFT OUTER JOIN {1} as sp 
820
                                      ON lp.UID = sp.UID
821
                                 LEFT OUTER JOIN {2} as spa 
822
                                      ON lp.UID = spa.UID;", LineProperties_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE);
823 8847ea67 gaqhf
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
824
                                dt.Load(dr);
825
                        }
826
                        connection.Close();
827 bca86986 gaqhf
                    }
828 8847ea67 gaqhf
                    catch (Exception ex)
829
                    {
830 bca86986 gaqhf
831 8847ea67 gaqhf
                    }
832
                    finally
833
                    {
834
                        connection.Dispose();
835
                    }
836 bca86986 gaqhf
                }
837 8847ea67 gaqhf
            }
838
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
839
            {
840
                using (SqlConnection connection = GetSqlConnection())
841 bca86986 gaqhf
                {
842 8847ea67 gaqhf
                    try
843
                    {
844
                        if (connection != null && connection.State == ConnectionState.Open)
845
                        {
846 d2a7bef1 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
847
                            {
848
                                cmd.CommandText = string.Format(@"
849
                            SELECT lp.UID, lp.DisplayName, sp.SPPID_SYMBOL_PATH, spa.SPPID_ATTRIBUTE
850
                            FROM {0} as lp 
851
                                 LEFT OUTER JOIN {1} as sp 
852
                                      ON lp.UID = sp.UID
853
                                 LEFT OUTER JOIN {2} as spa 
854
                                      ON lp.UID = spa.UID;", LineProperties_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE);
855
                                using (SqlDataReader dr = cmd.ExecuteReader())
856
                                    dt.Load(dr);
857
                            }
858
                            connection.Close();
859 8847ea67 gaqhf
                        }
860
                    }
861
                    catch (Exception ex)
862
                    {
863
864
                    }
865
                    finally
866
                    {
867
                        if (connection != null)
868
                            connection.Dispose();
869
                    }
870 bca86986 gaqhf
                }
871
            }
872
873
            return dt;
874
        }
875
876 1efc25a3 gaqhf
        public static DataTable SelectProjectAttribute()
877 bca86986 gaqhf
        {
878
            DataTable dt = new DataTable();
879
            Project_Info projectInfo = Project_Info.GetInstance();
880 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
881 bca86986 gaqhf
            {
882 8847ea67 gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
883 bca86986 gaqhf
                {
884 8847ea67 gaqhf
                    try
885 bca86986 gaqhf
                    {
886 8847ea67 gaqhf
                        connection.Open();
887
                        using (SQLiteCommand cmd = connection.CreateCommand())
888
                        {
889
                            cmd.CommandText = string.Format(@"
890 401efcff gaqhf
                            SELECT sa.UID, sa.DisplayAttribute, st.TYPE, spa.SPPID_ATTRIBUTE, sp.SPPID_SYMBOL_PATH, spl.LOCATION, spl.LEADERLINE, sa.Property
891 bca86986 gaqhf
                            FROM {1} as sa, {0} as st 
892
                                 LEFT OUTER JOIN {2} as sp 
893
                                      ON sa.UID = SP.UID 
894
                                LEFT OUTER JOIN {3} as spa 
895
                                     ON sa.UID = spa.UID
896 1a3a74a8 gaqhf
                                LEFT OUTER JOIN {4} as spl 
897
                                     ON sa.UID = spl.UID
898 401efcff gaqhf
                            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);
899 8847ea67 gaqhf
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
900
                                dt.Load(dr);
901
                        }
902
                        connection.Close();
903 bca86986 gaqhf
                    }
904 8847ea67 gaqhf
                    catch (Exception ex)
905
                    {
906 bca86986 gaqhf
907 8847ea67 gaqhf
                    }
908
                    finally
909
                    {
910
                        connection.Dispose();
911
                    }
912
                }
913
            }
914
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
915
            {
916
                using (SqlConnection connection = GetSqlConnection())
917 bca86986 gaqhf
                {
918 8847ea67 gaqhf
                    try
919
                    {
920
                        if (connection != null && connection.State == ConnectionState.Open)
921
                        {
922 d2a7bef1 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
923
                            {
924
                                cmd.CommandText = string.Format(@"
925
                            SELECT sa.UID, sa.DisplayAttribute, st.Type, spa.SPPID_ATTRIBUTE, sp.SPPID_SYMBOL_PATH, spl.LOCATION, spl.LEADERLINE, sa.Property
926
                            FROM {1} as sa
927
                                 LEFT OUTER JOIN {2} as sp 
928
                                      ON sa.UID = SP.UID 
929
                                LEFT OUTER JOIN {3} as spa 
930
                                     ON sa.UID = spa.UID
931
                                LEFT OUTER JOIN {4} as spl 
932
                                     ON sa.UID = spl.UID
933
                                LEFT OUTER JOIN {0} as st 
934
                                     ON sa.SymbolType_UID = st.UID 
935
                            WHERE (sa.Property != 2 OR sa.Property IS NULL);", SymbolType_TABLE, SymbolAttribute_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE, SPPID_LABEL_INFO_TABLE);
936
                                using (SqlDataReader dr = cmd.ExecuteReader())
937
                                    dt.Load(dr);
938
                            }
939
                            connection.Close();
940 8847ea67 gaqhf
                        }
941
                    }
942
                    catch (Exception ex)
943
                    {
944
945
                    }
946
                    finally
947
                    {
948
                        if (connection != null)
949
                            connection.Dispose();
950
                    }
951 bca86986 gaqhf
                }
952
            }
953
954 8847ea67 gaqhf
955 bca86986 gaqhf
            return dt;
956
        }
957
958 4d2571ab gaqhf
        public static DataTable SelectID2SymbolTable()
959
        {
960
            DataTable dt = new DataTable();
961
            Project_Info projectInfo = Project_Info.GetInstance();
962 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
963 4d2571ab gaqhf
            {
964 8847ea67 gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
965 4d2571ab gaqhf
                {
966 8847ea67 gaqhf
                    try
967 4d2571ab gaqhf
                    {
968 8847ea67 gaqhf
                        connection.Open();
969
                        using (SQLiteCommand cmd = connection.CreateCommand())
970
                        {
971
                            cmd.CommandText = @"SELECT * FROM Symbol";
972
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
973
                                dt.Load(dr);
974
                        }
975
                        connection.Close();
976 4d2571ab gaqhf
                    }
977 8847ea67 gaqhf
                    catch (Exception ex)
978
                    {
979 4d2571ab gaqhf
980 8847ea67 gaqhf
                    }
981
                    finally
982
                    {
983
                        connection.Dispose();
984
                    }
985 4d2571ab gaqhf
                }
986 8847ea67 gaqhf
            }
987
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
988
            {
989
                using (SqlConnection connection = GetSqlConnection())
990 4d2571ab gaqhf
                {
991 8847ea67 gaqhf
                    try
992
                    {
993
                        if (connection != null && connection.State == ConnectionState.Open)
994
                        {
995 d2a7bef1 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
996
                            {
997
                                cmd.CommandText = @"SELECT * FROM Symbol";
998
                                using (SqlDataReader dr = cmd.ExecuteReader())
999
                                    dt.Load(dr);
1000
                            }
1001
                            connection.Close();
1002 8847ea67 gaqhf
                        }
1003
                    }
1004
                    catch (Exception ex)
1005
                    {
1006
1007
                    }
1008
                    finally
1009
                    {
1010
                        if (connection != null)
1011
                            connection.Dispose();
1012
                    }
1013 4d2571ab gaqhf
                }
1014
            }
1015
1016
            return dt;
1017
        }
1018
1019 154d8f43 gaqhf
        public static DataTable SelectOPCRelations()
1020
        {
1021
            DataTable dt = new DataTable();
1022
            Project_Info projectInfo = Project_Info.GetInstance();
1023 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1024 154d8f43 gaqhf
            {
1025 8847ea67 gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1026 154d8f43 gaqhf
                {
1027 8847ea67 gaqhf
                    try
1028 154d8f43 gaqhf
                    {
1029 8847ea67 gaqhf
                        connection.Open();
1030
                        using (SQLiteCommand cmd = connection.CreateCommand())
1031
                        {
1032
                            cmd.CommandText = string.Format("SELECT * FROM {0}", OPCRelations_TABLE);
1033
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1034
                                dt.Load(dr);
1035
                        }
1036
                        connection.Close();
1037 154d8f43 gaqhf
                    }
1038 8847ea67 gaqhf
                    catch (Exception ex)
1039
                    {
1040 154d8f43 gaqhf
1041 8847ea67 gaqhf
                    }
1042
                    finally
1043
                    {
1044
                        connection.Dispose();
1045
                    }
1046 154d8f43 gaqhf
                }
1047 8847ea67 gaqhf
            }
1048
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1049
            {
1050
                using (SqlConnection connection = GetSqlConnection())
1051 154d8f43 gaqhf
                {
1052 8847ea67 gaqhf
                    try
1053
                    {
1054
                        if (connection != null && connection.State == ConnectionState.Open)
1055
                        {
1056 d2a7bef1 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
1057
                            {
1058
                                cmd.CommandText = string.Format("SELECT * FROM {0}", OPCRelations_TABLE);
1059
                                using (SqlDataReader dr = cmd.ExecuteReader())
1060
                                    dt.Load(dr);
1061
                            }
1062
                            connection.Close();
1063 8847ea67 gaqhf
                        }
1064
                    }
1065
                    catch (Exception ex)
1066
                    {
1067
1068
                    }
1069
                    finally
1070
                    {
1071
                        if (connection != null)
1072
                            connection.Dispose();
1073
                    }
1074 154d8f43 gaqhf
                }
1075
            }
1076
1077
            return dt;
1078
        }
1079
1080
        public static DataTable SelectDrawings()
1081
        {
1082
            DataTable dt = new DataTable();
1083
            Project_Info projectInfo = Project_Info.GetInstance();
1084 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1085 154d8f43 gaqhf
            {
1086 8847ea67 gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1087 154d8f43 gaqhf
                {
1088 8847ea67 gaqhf
                    try
1089 154d8f43 gaqhf
                    {
1090 8847ea67 gaqhf
                        connection.Open();
1091
                        using (SQLiteCommand cmd = connection.CreateCommand())
1092
                        {
1093
                            cmd.CommandText = string.Format("SELECT * FROM {0}", "Drawings");
1094
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1095
                                dt.Load(dr);
1096
                        }
1097
                        connection.Close();
1098 154d8f43 gaqhf
                    }
1099 8847ea67 gaqhf
                    catch (Exception ex)
1100
                    {
1101 154d8f43 gaqhf
1102 8847ea67 gaqhf
                    }
1103
                    finally
1104
                    {
1105
                        connection.Dispose();
1106
                    }
1107 154d8f43 gaqhf
                }
1108 8847ea67 gaqhf
            }
1109
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1110
            {
1111
                using (SqlConnection connection = GetSqlConnection())
1112 154d8f43 gaqhf
                {
1113 8847ea67 gaqhf
                    try
1114
                    {
1115
                        if (connection != null && connection.State == ConnectionState.Open)
1116
                        {
1117 d2a7bef1 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
1118
                            {
1119
                                cmd.CommandText = string.Format("SELECT * FROM {0}", "Drawings");
1120
                                using (SqlDataReader dr = cmd.ExecuteReader())
1121
                                    dt.Load(dr);
1122
                            }
1123
                            connection.Close();
1124 8847ea67 gaqhf
                        }
1125
                    }
1126
                    catch (Exception ex)
1127
                    {
1128
1129
                    }
1130
                    finally
1131
                    {
1132
                        if (connection != null)
1133
                            connection.Dispose();
1134
                    }
1135 154d8f43 gaqhf
                }
1136
            }
1137
1138
            return dt;
1139
        }
1140
1141
        public static DataTable SelectOPCInfo()
1142
        {
1143
            DataTable dt = new DataTable();
1144
            Project_Info projectInfo = Project_Info.GetInstance();
1145 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1146 154d8f43 gaqhf
            {
1147 8847ea67 gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1148 154d8f43 gaqhf
                {
1149 8847ea67 gaqhf
                    try
1150 154d8f43 gaqhf
                    {
1151 8847ea67 gaqhf
                        connection.Open();
1152
                        using (SQLiteCommand cmd = connection.CreateCommand())
1153
                        {
1154
                            cmd.CommandText = string.Format("SELECT * FROM {0} WHERE SPPID_OPC_MODELITEM_ID IS NOT NULL AND PAIRED = False", SPPID_OPC_INFO);
1155
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1156
                                dt.Load(dr);
1157
                        }
1158
                        connection.Close();
1159 154d8f43 gaqhf
                    }
1160 8847ea67 gaqhf
                    catch (Exception ex)
1161
                    {
1162 154d8f43 gaqhf
1163 8847ea67 gaqhf
                    }
1164
                    finally
1165
                    {
1166
                        connection.Dispose();
1167
                    }
1168 154d8f43 gaqhf
                }
1169 8847ea67 gaqhf
            }
1170
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1171
            {
1172
                using (SqlConnection connection = GetSqlConnection())
1173 154d8f43 gaqhf
                {
1174 8847ea67 gaqhf
                    try
1175
                    {
1176
                        if (connection != null && connection.State == ConnectionState.Open)
1177
                        {
1178 d2a7bef1 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
1179
                            {
1180
                                cmd.CommandText = string.Format("SELECT * FROM {0} WHERE SPPID_OPC_MODELITEM_ID IS NOT NULL AND PAIRED = False", SPPID_OPC_INFO);
1181
                                using (SqlDataReader dr = cmd.ExecuteReader())
1182
                                    dt.Load(dr);
1183
                            }
1184
                            connection.Close();
1185 8847ea67 gaqhf
                        }
1186
                    }
1187
                    catch (Exception ex)
1188
                    {
1189
1190
                    }
1191
                    finally
1192
                    {
1193
                        if (connection != null)
1194
                            connection.Dispose();
1195
                    }
1196 154d8f43 gaqhf
                }
1197
            }
1198
1199
            return dt;
1200
        }
1201 7e680366 gaqhf
1202
        public static DataTable SelectSymbolType()
1203
        {
1204
            DataTable dt = new DataTable();
1205
            Project_Info projectInfo = Project_Info.GetInstance();
1206 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1207 7e680366 gaqhf
            {
1208 8847ea67 gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1209 7e680366 gaqhf
                {
1210 8847ea67 gaqhf
                    try
1211 7e680366 gaqhf
                    {
1212 8847ea67 gaqhf
                        connection.Open();
1213
                        using (SQLiteCommand cmd = connection.CreateCommand())
1214
                        {
1215
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SymbolType_TABLE);
1216
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1217
                                dt.Load(dr);
1218
                        }
1219
                        connection.Close();
1220 7e680366 gaqhf
                    }
1221 8847ea67 gaqhf
                    catch (Exception ex)
1222
                    {
1223 7e680366 gaqhf
1224 8847ea67 gaqhf
                    }
1225
                    finally
1226
                    {
1227
                        connection.Dispose();
1228
                    }
1229 7e680366 gaqhf
                }
1230 8847ea67 gaqhf
            }
1231
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1232
            {
1233
                using (SqlConnection connection = GetSqlConnection())
1234 7e680366 gaqhf
                {
1235 8847ea67 gaqhf
                    try
1236
                    {
1237
                        if (connection != null && connection.State == ConnectionState.Open)
1238
                        {
1239 d2a7bef1 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
1240
                            {
1241
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SymbolType_TABLE);
1242
                                using (SqlDataReader dr = cmd.ExecuteReader())
1243
                                    dt.Load(dr);
1244
                            }
1245
                            connection.Close();
1246 8847ea67 gaqhf
                        }
1247
                    }
1248
                    catch (Exception ex)
1249
                    {
1250
1251
                    }
1252
                    finally
1253
                    {
1254
                        if (connection != null)
1255
                            connection.Dispose();
1256
                    }
1257 7e680366 gaqhf
                }
1258
            }
1259
1260
            return dt;
1261
        }
1262
1263 4fb0f8d5 gaqhf
        public static DataTable SelectDrawingInfo()
1264
        {
1265 154d8f43 gaqhf
            DataTable dt = new DataTable();
1266 4fb0f8d5 gaqhf
            Project_Info projectInfo = Project_Info.GetInstance();
1267 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1268 4fb0f8d5 gaqhf
            {
1269 8847ea67 gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1270 4fb0f8d5 gaqhf
                {
1271 8847ea67 gaqhf
                    try
1272 4fb0f8d5 gaqhf
                    {
1273 8847ea67 gaqhf
                        connection.Open();
1274
                        using (SQLiteCommand cmd = connection.CreateCommand())
1275
                        {
1276
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DRAWING_INFO);
1277
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1278
                                dt.Load(dr);
1279
                        }
1280
                        connection.Close();
1281 4fb0f8d5 gaqhf
                    }
1282 8847ea67 gaqhf
                    catch (Exception ex)
1283
                    {
1284 4fb0f8d5 gaqhf
1285 8847ea67 gaqhf
                    }
1286
                    finally
1287
                    {
1288
                        connection.Dispose();
1289
                    }
1290 4fb0f8d5 gaqhf
                }
1291 8847ea67 gaqhf
            }
1292
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1293
            {
1294
                using (SqlConnection connection = GetSqlConnection())
1295 4fb0f8d5 gaqhf
                {
1296 8847ea67 gaqhf
                    try
1297
                    {
1298
                        if (connection != null && connection.State == ConnectionState.Open)
1299
                        {
1300 d2a7bef1 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
1301
                            {
1302
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DRAWING_INFO);
1303
                                using (SqlDataReader dr = cmd.ExecuteReader())
1304
                                    dt.Load(dr);
1305
                            }
1306
                            connection.Close();
1307 8847ea67 gaqhf
                        }
1308
                    }
1309
                    catch (Exception ex)
1310
                    {
1311
1312
                    }
1313
                    finally
1314
                    {
1315
                        if (connection != null)
1316
                            connection.Dispose();
1317
                    }
1318 4fb0f8d5 gaqhf
                }
1319
            }
1320
1321
            return dt;
1322
        }
1323
1324 cf924377 gaqhf
        public static bool InsertSymbolMapping(List<Tuple<string, string, string, bool>> datas)
1325 bca86986 gaqhf
        {
1326
            Project_Info projectInfo = Project_Info.GetInstance();
1327 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1328 bca86986 gaqhf
            {
1329 8847ea67 gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1330 bca86986 gaqhf
                {
1331 8847ea67 gaqhf
                    try
1332 bca86986 gaqhf
                    {
1333 8847ea67 gaqhf
                        connection.Open();
1334
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
1335 1421f1d6 gaqhf
                        {
1336 8847ea67 gaqhf
                            try
1337 1421f1d6 gaqhf
                            {
1338 8847ea67 gaqhf
                                using (SQLiteCommand cmd = connection.CreateCommand())
1339 1421f1d6 gaqhf
                                {
1340 8847ea67 gaqhf
                                    foreach (var item in datas)
1341
                                    {
1342
                                        cmd.Parameters.Clear();
1343
                                        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);
1344
                                        cmd.Parameters.AddWithValue("@UID", item.Item1);
1345
                                        cmd.Parameters.AddWithValue("@NAME", item.Item2);
1346
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", item.Item3);
1347
                                        cmd.Parameters.AddWithValue("@LEADERLINE", item.Item4);
1348
                                        cmd.ExecuteNonQuery();
1349
                                    }
1350 1421f1d6 gaqhf
                                }
1351 8847ea67 gaqhf
                                transaction.Commit();
1352
                                connection.Close();
1353
                            }
1354
                            catch (Exception ex)
1355
                            {
1356
                                transaction.Rollback();
1357 a2c803a0 gaqhf
                                return false;
1358 8847ea67 gaqhf
                            }
1359
                            finally
1360
                            {
1361
                                transaction.Dispose();
1362 1421f1d6 gaqhf
                            }
1363 bca86986 gaqhf
                        }
1364
                    }
1365 8847ea67 gaqhf
                    catch (Exception ex)
1366
                    {
1367
                        return false;
1368
                    }
1369
                    finally
1370
                    {
1371
                        connection.Dispose();
1372
                    }
1373 bca86986 gaqhf
                }
1374 8847ea67 gaqhf
            }
1375
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1376
            {
1377
                using (SqlConnection connection = GetSqlConnection())
1378 bca86986 gaqhf
                {
1379 8847ea67 gaqhf
                    try
1380
                    {
1381
                        if (connection != null && connection.State == ConnectionState.Open)
1382
                        {
1383 a2c803a0 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
1384
                            {
1385
                                foreach (var item in datas)
1386
                                {
1387
                                    cmd.Parameters.Clear();
1388
                                    cmd.CommandText = string.Format(@"
1389
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
1390
                                        UPDATE {0} SET NAME = @NAME, SPPID_SYMBOL_PATH = @SPPID_SYMBOL_PATH, LEADERLINE = @LEADERLINE WHERE UID = @UID
1391
                                    ELSE
1392
                                        INSERT INTO {0} (UID, NAME, SPPID_SYMBOL_PATH, LEADERLINE) VALUES (@UID, @NAME, @SPPID_SYMBOL_PATH, @LEADERLINE)", SPPID_SYMBOL_MAPPING_TABLE, item.Item1);
1393
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
1394 f4485671 gaqhf
                                    if (string.IsNullOrEmpty(item.Item2))
1395
                                        cmd.Parameters.AddWithValue("@NAME", DBNull.Value);
1396
                                    else
1397
                                        cmd.Parameters.AddWithValue("@NAME", item.Item2);
1398 a2c803a0 gaqhf
                                    if (string.IsNullOrEmpty(item.Item3))
1399
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", DBNull.Value);
1400
                                    else
1401
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", item.Item3);
1402
                                    cmd.Parameters.AddWithValue("@LEADERLINE", item.Item4);
1403
                                    cmd.ExecuteNonQuery();
1404
                                }
1405
                            }
1406
                            connection.Close();
1407 8847ea67 gaqhf
                        }
1408
                    }
1409
                    catch (Exception ex)
1410
                    {
1411 a2c803a0 gaqhf
                        return false;
1412 8847ea67 gaqhf
                    }
1413
                    finally
1414
                    {
1415
                        if (connection != null)
1416
                            connection.Dispose();
1417
                    }
1418 bca86986 gaqhf
                }
1419
            }
1420
1421
            return true;
1422
        }
1423
1424
        public static bool InsertAttributeMapping(List<Tuple<string, string>> datas)
1425
        {
1426
            Project_Info projectInfo = Project_Info.GetInstance();
1427 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1428 bca86986 gaqhf
            {
1429 8847ea67 gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1430 bca86986 gaqhf
                {
1431 8847ea67 gaqhf
                    try
1432 bca86986 gaqhf
                    {
1433 8847ea67 gaqhf
                        connection.Open();
1434
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
1435 1421f1d6 gaqhf
                        {
1436 8847ea67 gaqhf
                            try
1437 1421f1d6 gaqhf
                            {
1438 8847ea67 gaqhf
                                using (SQLiteCommand cmd = connection.CreateCommand())
1439 1421f1d6 gaqhf
                                {
1440 8847ea67 gaqhf
                                    foreach (var item in datas)
1441
                                    {
1442
                                        cmd.Parameters.Clear();
1443
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, SPPID_ATTRIBUTE) VALUES (@UID, @SPPID_ATTRIBUTE)", SPPID_ATTRIBUTE_MAPPING_TABLE);
1444
                                        cmd.Parameters.AddWithValue("@UID", item.Item1);
1445
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", item.Item2);
1446
                                        cmd.ExecuteNonQuery();
1447
                                    }
1448 1421f1d6 gaqhf
                                }
1449 8847ea67 gaqhf
                                transaction.Commit();
1450
                                connection.Close();
1451
                            }
1452
                            catch (Exception ex)
1453
                            {
1454
                                transaction.Rollback();
1455
                            }
1456
                            finally
1457
                            {
1458
                                transaction.Dispose();
1459 1421f1d6 gaqhf
                            }
1460 bca86986 gaqhf
                        }
1461
                    }
1462 8847ea67 gaqhf
                    catch (Exception ex)
1463
                    {
1464
                        return false;
1465
                    }
1466
                    finally
1467
                    {
1468
                        connection.Dispose();
1469
                    }
1470 bca86986 gaqhf
                }
1471 8847ea67 gaqhf
            }
1472
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1473
            {
1474
                using (SqlConnection connection = GetSqlConnection())
1475 bca86986 gaqhf
                {
1476 8847ea67 gaqhf
                    try
1477
                    {
1478
                        if (connection != null && connection.State == ConnectionState.Open)
1479
                        {
1480 f4485671 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
1481 a2c803a0 gaqhf
                            {
1482 f4485671 gaqhf
                                foreach (var item in datas)
1483 a2c803a0 gaqhf
                                {
1484 f4485671 gaqhf
                                    cmd.Parameters.Clear();
1485
                                    cmd.CommandText = string.Format(@"
1486
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
1487
                                        UPDATE {0} SET SPPID_ATTRIBUTE = @SPPID_ATTRIBUTE WHERE UID = @UID
1488
                                    ELSE
1489
                                        INSERT INTO {0} (UID, SPPID_ATTRIBUTE) VALUES (@UID, @SPPID_ATTRIBUTE)", SPPID_ATTRIBUTE_MAPPING_TABLE, item.Item1);
1490
1491
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
1492
                                    if (string.IsNullOrEmpty(item.Item2))
1493
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", DBNull.Value);
1494
                                    else
1495 a2c803a0 gaqhf
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", item.Item2);
1496 f4485671 gaqhf
                                    cmd.ExecuteNonQuery();
1497 a2c803a0 gaqhf
                                }
1498
                            }
1499 f4485671 gaqhf
                            connection.Close();
1500 8847ea67 gaqhf
                        }
1501
                    }
1502
                    catch (Exception ex)
1503
                    {
1504 a2c803a0 gaqhf
                        return false;
1505 8847ea67 gaqhf
                    }
1506
                    finally
1507
                    {
1508
                        if (connection != null)
1509
                            connection.Dispose();
1510
                    }
1511 bca86986 gaqhf
                }
1512
            }
1513
            return true;
1514
        }
1515 1a3a74a8 gaqhf
1516 cf924377 gaqhf
        public static bool InsertLabelInfoMapping(List<Tuple<string, int, bool>> datas)
1517 1a3a74a8 gaqhf
        {
1518
            Project_Info projectInfo = Project_Info.GetInstance();
1519 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1520 1a3a74a8 gaqhf
            {
1521 8847ea67 gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1522 1a3a74a8 gaqhf
                {
1523 8847ea67 gaqhf
                    try
1524 1a3a74a8 gaqhf
                    {
1525 8847ea67 gaqhf
                        connection.Open();
1526
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
1527 1a3a74a8 gaqhf
                        {
1528 8847ea67 gaqhf
                            try
1529 1a3a74a8 gaqhf
                            {
1530 8847ea67 gaqhf
                                using (SQLiteCommand cmd = connection.CreateCommand())
1531 1a3a74a8 gaqhf
                                {
1532 8847ea67 gaqhf
                                    foreach (var item in datas)
1533
                                    {
1534
                                        cmd.Parameters.Clear();
1535
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, LOCATION, LEADERLINE) VALUES (@UID, @LOCATION, @LEADERLINE)", SPPID_LABEL_INFO_TABLE);
1536
                                        cmd.Parameters.AddWithValue("@UID", item.Item1);
1537
                                        cmd.Parameters.AddWithValue("@LOCATION", item.Item2);
1538
                                        cmd.Parameters.AddWithValue("@LEADERLINE", item.Item3);
1539
                                        cmd.ExecuteNonQuery();
1540
                                    }
1541 1a3a74a8 gaqhf
                                }
1542 8847ea67 gaqhf
                                transaction.Commit();
1543
                                connection.Close();
1544
                            }
1545
                            catch (Exception ex)
1546
                            {
1547
                                transaction.Rollback();
1548 a2c803a0 gaqhf
                                return false;
1549 8847ea67 gaqhf
                            }
1550
                            finally
1551
                            {
1552
                                transaction.Dispose();
1553 1a3a74a8 gaqhf
                            }
1554
                        }
1555
                    }
1556 8847ea67 gaqhf
                    catch (Exception ex)
1557
                    {
1558
                        return false;
1559
                    }
1560
                    finally
1561
                    {
1562
                        connection.Dispose();
1563
                    }
1564 1a3a74a8 gaqhf
                }
1565 8847ea67 gaqhf
            }
1566
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1567
            {
1568
                using (SqlConnection connection = GetSqlConnection())
1569 1a3a74a8 gaqhf
                {
1570 8847ea67 gaqhf
                    try
1571
                    {
1572
                        if (connection != null && connection.State == ConnectionState.Open)
1573
                        {
1574 a2c803a0 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
1575
                            {
1576
                                foreach (var item in datas)
1577
                                {
1578
                                    cmd.Parameters.Clear();
1579 f4485671 gaqhf
                                    cmd.CommandText = string.Format(@"
1580
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
1581
                                        UPDATE {0} SET LOCATION = @LOCATION, LEADERLINE = @LEADERLINE WHERE UID = @UID
1582
                                    ELSE
1583
                                        INSERT INTO {0} (UID, LOCATION, LEADERLINE) VALUES (@UID, @LOCATION, @LEADERLINE)", SPPID_LABEL_INFO_TABLE, item.Item1);
1584
1585 a2c803a0 gaqhf
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
1586
                                    cmd.Parameters.AddWithValue("@LOCATION", item.Item2);
1587
                                    cmd.Parameters.AddWithValue("@LEADERLINE", item.Item3);
1588
                                    cmd.ExecuteNonQuery();
1589
                                }
1590
                            }
1591
                            connection.Close();
1592 8847ea67 gaqhf
                        }
1593
                    }
1594
                    catch (Exception ex)
1595
                    {
1596 a2c803a0 gaqhf
                        return false;
1597 8847ea67 gaqhf
                    }
1598
                    finally
1599
                    {
1600
                        if (connection != null)
1601
                            connection.Dispose();
1602
                    }
1603 1a3a74a8 gaqhf
                }
1604
            }
1605
            return true;
1606
        }
1607 69b7387a gaqhf
1608 154d8f43 gaqhf
        public static bool InsertDrawingInfoAndOPCInfo(string path, string drawingNumber, string drawingName, SPPID.Model.SPPID_Document document)
1609 69b7387a gaqhf
        {
1610
            Project_Info projectInfo = Project_Info.GetInstance();
1611 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1612 69b7387a gaqhf
            {
1613 8847ea67 gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1614 69b7387a gaqhf
                {
1615 8847ea67 gaqhf
                    try
1616 69b7387a gaqhf
                    {
1617 8847ea67 gaqhf
                        connection.Open();
1618
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
1619 69b7387a gaqhf
                        {
1620 8847ea67 gaqhf
                            try
1621 154d8f43 gaqhf
                            {
1622
                                using (SQLiteCommand cmd = connection.CreateCommand())
1623
                                {
1624 8847ea67 gaqhf
                                    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);
1625 154d8f43 gaqhf
                                    cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
1626 8847ea67 gaqhf
                                    cmd.Parameters.AddWithValue("@PATH", path);
1627
                                    cmd.Parameters.AddWithValue("@DRAWINGNUMBER", drawingNumber);
1628
                                    cmd.Parameters.AddWithValue("@DRAWINGNAME", drawingName);
1629 154d8f43 gaqhf
                                    cmd.ExecuteNonQuery();
1630
                                }
1631
1632 8847ea67 gaqhf
                                List<Symbol> OPCs = document.SYMBOLS.FindAll(x => x.TYPE == "Piping OPC's" || x.TYPE == "Instrument OPC's");
1633
                                foreach (var item in OPCs)
1634
                                {
1635
                                    using (SQLiteCommand cmd = connection.CreateCommand())
1636
                                    {
1637
                                        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);
1638
                                        cmd.Parameters.AddWithValue("@ID2_OPC_UID", item.UID);
1639
                                        cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", item.SPPID.ModelItemID);
1640
                                        cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
1641
                                        cmd.Parameters.AddWithValue("@ATTRIBUTES", JsonConvert.SerializeObject(item.SPPID.Attributes));
1642
                                        cmd.Parameters.AddWithValue("@PAIRED", false);
1643
                                        cmd.ExecuteNonQuery();
1644
                                    }
1645
                                }
1646
1647
                                transaction.Commit();
1648
                                connection.Close();
1649
                            }
1650
                            catch (Exception ex)
1651
                            {
1652
                                transaction.Rollback();
1653 a2c803a0 gaqhf
                                return false;
1654 8847ea67 gaqhf
                            }
1655
                            finally
1656
                            {
1657
                                transaction.Dispose();
1658
                            }
1659 154d8f43 gaqhf
                        }
1660
                    }
1661 8847ea67 gaqhf
                    catch (Exception ex)
1662
                    {
1663
                        return false;
1664
                    }
1665
                    finally
1666
                    {
1667
                        connection.Dispose();
1668
                    }
1669 154d8f43 gaqhf
                }
1670 8847ea67 gaqhf
            }
1671
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1672
            {
1673
                using (SqlConnection connection = GetSqlConnection())
1674 154d8f43 gaqhf
                {
1675 8847ea67 gaqhf
                    try
1676
                    {
1677
                        if (connection != null && connection.State == ConnectionState.Open)
1678
                        {
1679 a2c803a0 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
1680
                            {
1681
                                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);
1682
                                cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
1683
                                cmd.Parameters.AddWithValue("@PATH", path);
1684
                                cmd.Parameters.AddWithValue("@DRAWINGNUMBER", drawingNumber);
1685
                                cmd.Parameters.AddWithValue("@DRAWINGNAME", drawingName);
1686
                                cmd.ExecuteNonQuery();
1687
                            }
1688 8847ea67 gaqhf
1689 a2c803a0 gaqhf
                            List<Symbol> OPCs = document.SYMBOLS.FindAll(x => x.TYPE == "Piping OPC's" || x.TYPE == "Instrument OPC's");
1690
                            foreach (var item in OPCs)
1691
                            {
1692
                                using (SqlCommand cmd = connection.CreateCommand())
1693
                                {
1694
                                    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);
1695
                                    cmd.Parameters.AddWithValue("@ID2_OPC_UID", item.UID);
1696
                                    cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", item.SPPID.ModelItemID);
1697
                                    cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
1698
                                    cmd.Parameters.AddWithValue("@ATTRIBUTES", JsonConvert.SerializeObject(item.SPPID.Attributes));
1699
                                    cmd.Parameters.AddWithValue("@PAIRED", false);
1700
                                    cmd.ExecuteNonQuery();
1701
                                }
1702
                            }
1703
                            connection.Close();
1704 8847ea67 gaqhf
                        }
1705
                    }
1706
                    catch (Exception ex)
1707
                    {
1708 a2c803a0 gaqhf
                        return false;
1709 8847ea67 gaqhf
                    }
1710
                    finally
1711
                    {
1712
                        if (connection != null)
1713
                            connection.Dispose();
1714
                    }
1715 154d8f43 gaqhf
                }
1716
            }
1717
            return true;
1718
        }
1719
1720
        public static bool InsertOPCInfo(string UID, string ModelItemID, string drawingUID, bool Paired)
1721
        {
1722
            Project_Info projectInfo = Project_Info.GetInstance();
1723 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1724 154d8f43 gaqhf
            {
1725 8847ea67 gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1726 154d8f43 gaqhf
                {
1727 8847ea67 gaqhf
                    try
1728 154d8f43 gaqhf
                    {
1729 8847ea67 gaqhf
                        connection.Open();
1730
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
1731 154d8f43 gaqhf
                        {
1732 8847ea67 gaqhf
                            try
1733 154d8f43 gaqhf
                            {
1734 8847ea67 gaqhf
                                using (SQLiteCommand cmd = connection.CreateCommand())
1735
                                {
1736
                                    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);
1737
                                    cmd.Parameters.AddWithValue("@ID2_OPC_UID", UID);
1738
                                    cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", ModelItemID);
1739
                                    cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", drawingUID);
1740
                                    cmd.Parameters.AddWithValue("@PAIRED", Paired);
1741
                                    cmd.ExecuteNonQuery();
1742
                                }
1743 154d8f43 gaqhf
1744 8847ea67 gaqhf
                                transaction.Commit();
1745
                                connection.Close();
1746
                            }
1747
                            catch (Exception ex)
1748
                            {
1749
                                transaction.Rollback();
1750 a2c803a0 gaqhf
                                return false;
1751 8847ea67 gaqhf
                            }
1752
                            finally
1753
                            {
1754
                                transaction.Dispose();
1755
                            }
1756 69b7387a gaqhf
                        }
1757
                    }
1758 8847ea67 gaqhf
                    catch (Exception ex)
1759
                    {
1760
                        return false;
1761
                    }
1762
                    finally
1763
                    {
1764
                        connection.Dispose();
1765
                    }
1766 69b7387a gaqhf
                }
1767 8847ea67 gaqhf
            }
1768
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1769
            {
1770
                using (SqlConnection connection = GetSqlConnection())
1771 69b7387a gaqhf
                {
1772 8847ea67 gaqhf
                    try
1773
                    {
1774
                        if (connection != null && connection.State == ConnectionState.Open)
1775
                        {
1776 a2c803a0 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
1777
                            {
1778
                                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);
1779
                                cmd.Parameters.AddWithValue("@ID2_OPC_UID", UID);
1780
                                cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", ModelItemID);
1781
                                cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", drawingUID);
1782
                                cmd.Parameters.AddWithValue("@PAIRED", Paired);
1783
                                cmd.ExecuteNonQuery();
1784
                            }
1785
                            connection.Close();
1786 8847ea67 gaqhf
                        }
1787
                    }
1788
                    catch (Exception ex)
1789
                    {
1790 a2c803a0 gaqhf
                        return false;
1791 8847ea67 gaqhf
                    }
1792
                    finally
1793
                    {
1794
                        if (connection != null)
1795
                            connection.Dispose();
1796
                    }
1797 69b7387a gaqhf
                }
1798
            }
1799
            return true;
1800
        }
1801 b18dc619 gaqhf
    }
1802
}
클립보드 이미지 추가 (최대 크기: 500 MB)