프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / APIDConverter / DB / Project_DB.cs @ c8da68ce

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

1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
using System.Globalization;
7
using System.Data.SQLite;
8
using System.Data;
9
using System.Data.SqlClient;
10
using System.IO;
11
using Newtonsoft.Json;
12
using AVEVA.PID.CustomizationUtility;
13
using AVEVA.PID.CustomizationUtility.Model;
14

    
15
namespace AVEVA.PID.CustomizationUtility.DB
16
{
17
    public class Project_DB
18
    {
19
        const string APID_SYMBOL_MAPPING_TABLE = "T_APID_SYMBOL_MAPPING";
20

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

    
28
        private static SqlConnection GetSqlConnection()
29
        {
30
            Project_Info projectInfo = Project_Info.GetInstance();
31
            SqlConnection connection = null;
32
            try
33
            {
34
                connection = new SqlConnection(string.Format(CultureInfo.CurrentCulture,
35
                    @"Data Source = {0},{1}; Initial CataLog = {2}; User ID = {3}; Password = {4};",
36
                    projectInfo.ServerIP,
37
                    projectInfo.Port,
38
                    System.IO.Path.GetFileName(projectInfo.DefaultPath),
39
                    projectInfo.DBUser,
40
                    projectInfo.DBPassword));
41

    
42
                connection.Open();
43
            }
44
            catch (Exception ex)
45
            {
46
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
47
                if (connection != null)
48
                    connection.Dispose();
49
                connection = null;
50
            }
51

    
52
            return connection;
53
        }
54
        public static bool ConnTestAndCreateTable()
55
        {
56
            bool result = false;
57
            Project_Info projectInfo = Project_Info.GetInstance();
58
            if (projectInfo.DBType == ID2DB_Type.SQLite)
59
            {
60
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, @"Data Source = {0}", projectInfo.DBFilePath)))
61
                {
62
                    try
63
                    {
64
                        connection.Open();
65
                        if (connection.State == ConnectionState.Open)
66
                        {
67
                            using (SQLiteCommand cmd = connection.CreateCommand())
68
                            {
69
                                cmd.CommandText = "SELECT NAME FROM sqlite_master WHERE type='table'";
70
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
71
                                using (DataTable dt = new DataTable())
72
                                {
73
                                    dt.Load(dr);
74

    
75
                                    if (dt.Select(string.Format("NAME = '{0}'", APID_SYMBOL_MAPPING_TABLE)).Length == 0)
76
                                    {
77
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, NAME TEXT, APID_SYMBOL TEXT)", APID_SYMBOL_MAPPING_TABLE);
78
                                        cmd.ExecuteNonQuery();
79
                                    }
80
                                }
81
                            }
82
                            result = true;
83
                        }
84
                        connection.Close();
85
                    }
86
                    catch (Exception ex)
87
                    {
88
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
89
                    }
90
                    finally
91
                    {
92
                        connection.Dispose();
93
                    }
94
                }
95
            }
96
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
97
            {
98
                using (SqlConnection connection = GetSqlConnection())
99
                {
100
                    try
101
                    {
102
                        if (connection != null && connection.State == ConnectionState.Open)
103
                        {
104
                            using (SqlCommand cmd = connection.CreateCommand())
105
                            {
106
                                cmd.CommandText = "SELECT TABLE_NAME AS NAME FROM INFORMATION_SCHEMA.TABLES";
107
                                using (SqlDataReader dr = cmd.ExecuteReader())
108
                                using (DataTable dt = new DataTable())
109
                                {
110
                                    dt.Load(dr);
111

    
112
                                    if (dt.Select(string.Format("NAME = '{0}'", APID_SYMBOL_MAPPING_TABLE)).Length == 0)
113
                                    {
114
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID varchar(255) PRIMARY KEY, NAME varchar(MAX), APID_SYMBOL varchar(MAX))", APID_SYMBOL_MAPPING_TABLE);
115
                                        cmd.ExecuteNonQuery();
116
                                    }
117
                                    
118
                                }
119
                            }
120
                            result = true;
121
                        }
122
                    }
123
                    catch (Exception ex)
124
                    {
125
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
126
                    }
127
                    finally
128
                    {
129
                        if (connection != null)
130
                            connection.Dispose();
131
                    }
132
                }
133
            }
134

    
135
            return result;
136
        }
137
        public static DataTable GetSymbolMappingTable()
138
        {
139
            DataTable dt = new DataTable();
140
            Project_Info projectInfo = Project_Info.GetInstance();
141
            if (projectInfo.DBType == ID2DB_Type.SQLite)
142
            {
143
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
144
                {
145
                    try
146
                    {
147
                        connection.Open();
148
                        using (SQLiteCommand cmd = connection.CreateCommand())
149
                        {
150
                            cmd.CommandText = string.Format(@"
151
                            SELECT s.UID, s.Name, st.Type, sp.APID_SYMBOL FROM {1} as st, {0} as s 
152
                                LEFT OUTER JOIN {2} as sp 
153
                                    ON s.UID = SP.UID 
154
                            WHERE s.SymbolType_UID = st.UID 
155
                            ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, APID_SYMBOL_MAPPING_TABLE);
156
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
157
                                dt.Load(dr);
158

    
159
                            DataTable dtClone = dt.Clone();
160
                            dtClone.Columns["UID"].DataType = typeof(string);
161
                            foreach (DataRow row in dt.Rows)
162
                            {
163
                                dtClone.ImportRow(row);
164
                            }
165
                            dt.Dispose();
166
                            dt = dtClone;
167
                        }
168
                        connection.Close();
169
                    }
170
                    catch (Exception ex)
171
                    {
172
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
173
                    }
174
                    finally
175
                    {
176
                        connection.Dispose();
177
                    }
178
                }
179
            }
180
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
181
            {
182
                using (SqlConnection connection = GetSqlConnection())
183
                {
184
                    try
185
                    {
186
                        if (connection != null && connection.State == ConnectionState.Open)
187
                        {
188
                            using (SqlCommand cmd = connection.CreateCommand())
189
                            {
190
                                cmd.CommandText = string.Format(@"
191
                            SELECT CONVERT(VARCHAR(255), s.UID) AS UID, s.Name, st.Type, sp.APID_SYMBOL FROM {1} as st, {0} as s 
192
                                LEFT OUTER JOIN {2} as sp 
193
                                    ON CONVERT(VARCHAR(255), s.UID) = CONVERT(VARCHAR(255), SP.UID)
194
                            WHERE s.SymbolType_UID = st.UID 
195
                            ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, APID_SYMBOL_MAPPING_TABLE);
196
                                using (SqlDataReader dr = cmd.ExecuteReader())
197
                                    dt.Load(dr);
198
                            }
199
                            connection.Close();
200
                        }
201
                    }
202
                    catch (Exception ex)
203
                    {
204
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
205
                    }
206
                    finally
207
                    {
208
                        if (connection != null)
209
                            connection.Dispose();
210
                    }
211
                }
212
            }
213

    
214
            return dt;
215
        }
216

    
217

    
218

    
219

    
220
        #region Only ID2 DB
221
        public static DataTable SelectID2SymbolTable()
222
        {
223
            DataTable dt = new DataTable();
224
            Project_Info projectInfo = Project_Info.GetInstance();
225
            if (projectInfo.DBType == ID2DB_Type.SQLite)
226
            {
227
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
228
                {
229
                    try
230
                    {
231
                        connection.Open();
232
                        using (SQLiteCommand cmd = connection.CreateCommand())
233
                        {
234
                            cmd.CommandText = @"SELECT * FROM Symbol";
235
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
236
                                dt.Load(dr);
237
                        }
238
                        connection.Close();
239
                    }
240
                    catch (Exception ex)
241
                    {
242
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
243
                    }
244
                    finally
245
                    {
246
                        connection.Dispose();
247
                    }
248
                }
249
            }
250
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
251
            {
252
                using (SqlConnection connection = GetSqlConnection())
253
                {
254
                    try
255
                    {
256
                        if (connection != null && connection.State == ConnectionState.Open)
257
                        {
258
                            using (SqlCommand cmd = connection.CreateCommand())
259
                            {
260
                                cmd.CommandText = @"SELECT * FROM Symbol";
261
                                using (SqlDataReader dr = cmd.ExecuteReader())
262
                                    dt.Load(dr);
263
                            }
264
                            connection.Close();
265
                        }
266
                    }
267
                    catch (Exception ex)
268
                    {
269
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
270
                    }
271
                    finally
272
                    {
273
                        if (connection != null)
274
                            connection.Dispose();
275
                    }
276
                }
277
            }
278

    
279
            return dt;
280
        }
281
        public static DataTable SelectSymbolType()
282
        {
283
            DataTable dt = new DataTable();
284
            Project_Info projectInfo = Project_Info.GetInstance();
285
            if (projectInfo.DBType == ID2DB_Type.SQLite)
286
            {
287
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
288
                {
289
                    try
290
                    {
291
                        connection.Open();
292
                        using (SQLiteCommand cmd = connection.CreateCommand())
293
                        {
294
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SymbolType_TABLE);
295
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
296
                                dt.Load(dr);
297
                        }
298
                        connection.Close();
299
                    }
300
                    catch (Exception ex)
301
                    {
302
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
303
                    }
304
                    finally
305
                    {
306
                        connection.Dispose();
307
                    }
308
                }
309
            }
310
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
311
            {
312
                using (SqlConnection connection = GetSqlConnection())
313
                {
314
                    try
315
                    {
316
                        if (connection != null && connection.State == ConnectionState.Open)
317
                        {
318
                            using (SqlCommand cmd = connection.CreateCommand())
319
                            {
320
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SymbolType_TABLE);
321
                                using (SqlDataReader dr = cmd.ExecuteReader())
322
                                    dt.Load(dr);
323
                            }
324
                            connection.Close();
325
                        }
326
                    }
327
                    catch (Exception ex)
328
                    {
329
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
330
                    }
331
                    finally
332
                    {
333
                        if (connection != null)
334
                            connection.Dispose();
335
                    }
336
                }
337
            }
338

    
339
            return dt;
340
        }
341
        public static DataTable SelectDrawings()
342
        {
343
            DataTable dt = new DataTable();
344
            Project_Info projectInfo = Project_Info.GetInstance();
345
            if (projectInfo.DBType == ID2DB_Type.SQLite)
346
            {
347
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
348
                {
349
                    try
350
                    {
351
                        connection.Open();
352
                        using (SQLiteCommand cmd = connection.CreateCommand())
353
                        {
354
                            cmd.CommandText = string.Format("SELECT * FROM {0}", "Drawings");
355
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
356
                                dt.Load(dr);
357
                        }
358
                        connection.Close();
359
                    }
360
                    catch (Exception ex)
361
                    {
362
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
363
                    }
364
                    finally
365
                    {
366
                        connection.Dispose();
367
                    }
368
                }
369
            }
370
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
371
            {
372
                using (SqlConnection connection = GetSqlConnection())
373
                {
374
                    try
375
                    {
376
                        if (connection != null && connection.State == ConnectionState.Open)
377
                        {
378
                            using (SqlCommand cmd = connection.CreateCommand())
379
                            {
380
                                cmd.CommandText = string.Format("SELECT * FROM {0}", "Drawings");
381
                                using (SqlDataReader dr = cmd.ExecuteReader())
382
                                    dt.Load(dr);
383
                            }
384
                            connection.Close();
385
                        }
386
                    }
387
                    catch (Exception ex)
388
                    {
389
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
390
                    }
391
                    finally
392
                    {
393
                        if (connection != null)
394
                            connection.Dispose();
395
                    }
396
                }
397
            }
398

    
399
            return dt;
400
        }
401
        public static bool InsertSymbolMapping(List<Tuple<string, string, string>> datas)
402
        {
403
            Project_Info projectInfo = Project_Info.GetInstance();
404
            if (projectInfo.DBType == ID2DB_Type.SQLite)
405
            {
406
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
407
                {
408
                    try
409
                    {
410
                        connection.Open();
411
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
412
                        {
413
                            try
414
                            {
415
                                using (SQLiteCommand cmd = connection.CreateCommand())
416
                                {
417
                                    foreach (var item in datas)
418
                                    {
419
                                        cmd.Parameters.Clear();
420
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, NAME, APID_SYMBOL) VALUES (@UID, @NAME, @APID_SYMBOL)", APID_SYMBOL_MAPPING_TABLE);
421
                                        cmd.Parameters.AddWithValue("@UID", item.Item1);
422
                                        cmd.Parameters.AddWithValue("@NAME", item.Item2);
423
                                        cmd.Parameters.AddWithValue("@APID_SYMBOL", item.Item3);
424
                                        cmd.ExecuteNonQuery();
425
                                    }
426
                                }
427
                                transaction.Commit();
428
                                connection.Close();
429
                            }
430
                            catch (Exception ex)
431
                            {
432
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
433
                                transaction.Rollback();
434
                                return false;
435
                            }
436
                            finally
437
                            {
438
                                transaction.Dispose();
439
                            }
440
                        }
441
                    }
442
                    catch (Exception ex)
443
                    {
444
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
445
                        return false;
446
                    }
447
                    finally
448
                    {
449
                        connection.Dispose();
450
                    }
451
                }
452
            }
453
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
454
            {
455
                using (SqlConnection connection = GetSqlConnection())
456
                {
457
                    try
458
                    {
459
                        if (connection != null && connection.State == ConnectionState.Open)
460
                        {
461
                            using (SqlCommand cmd = connection.CreateCommand())
462
                            {
463
                                foreach (var item in datas)
464
                                {
465
                                    cmd.Parameters.Clear();
466
                                    cmd.CommandText = string.Format(@"
467
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
468
                                        UPDATE {0} SET NAME = @NAME, APID_SYMBOL = @APID_SYMBOL WHERE UID = @UID
469
                                    ELSE
470
                                        INSERT INTO {0} (UID, NAME, APID_SYMBOL) VALUES (@UID, @NAME, @APID_SYMBOL)", APID_SYMBOL_MAPPING_TABLE, item.Item1);
471
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
472
                                    if (string.IsNullOrEmpty(item.Item2))
473
                                        cmd.Parameters.AddWithValue("@NAME", DBNull.Value);
474
                                    else
475
                                        cmd.Parameters.AddWithValue("@NAME", item.Item2);
476
                                    if (string.IsNullOrEmpty(item.Item3))
477
                                        cmd.Parameters.AddWithValue("@APID_SYMBOL", DBNull.Value);
478
                                    else
479
                                        cmd.Parameters.AddWithValue("@APID_SYMBOL", item.Item3);
480
                                    cmd.ExecuteNonQuery();
481
                                }
482
                            }
483
                            connection.Close();
484
                        }
485
                    }
486
                    catch (Exception ex)
487
                    {
488
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
489
                        return false;
490
                    }
491
                    finally
492
                    {
493
                        if (connection != null)
494
                            connection.Dispose();
495
                    }
496
                }
497
            }
498

    
499
            return true;
500
        }
501
        #endregion
502

    
503
        #region AVEVA
504
        public static string GetAvevaConnectionString()
505
        {
506
            string strConn = string.Empty;
507
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
508
            {
509
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLDatabaseName);
510
            }
511
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
512
            {
513
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
514
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLDatabaseName + ";" + strAccessString);
515
            }
516

    
517
            return strConn;
518
        }
519

    
520
        public static string GetAvevaConnectionString_Reports()
521
        {
522
            string strConn = string.Empty;
523
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
524
            {
525
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLReportsDatabaseName);
526
            }
527
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
528
            {
529
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
530
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLReportsDatabaseName + ";" + strAccessString);
531
            }
532

    
533
            return strConn;
534
        }
535

    
536
        public static string GetAvevaConnectionString_Admin()
537
        {
538
            string strConn = string.Empty;
539
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
540
            {
541
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLAdminDatabaseName);
542
            }
543
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
544
            {
545
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
546
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLAdminDatabaseName + ";" + strAccessString);
547
            }
548

    
549
            return strConn;
550
        }
551

    
552
        public static DataTable SelectStandardSymbolTable()
553
        {
554
            DataTable dt = new DataTable();
555

    
556
            using (SqlConnection connection = new SqlConnection())
557
            {
558
                connection.ConnectionString = GetAvevaConnectionString_Admin();
559
                connection.Open();
560
                using (SqlCommand cmd = connection.CreateCommand())
561
                {
562
                    cmd.CommandText = string.Format(@"
563
                    SELECT ss.ID, ss.Symbol_Name, sps.Relative_Path FROM {0} as ss
564
                    LEFT OUTER JOIN {1} as sps
565
                    ON ss.Relative_Path = sps.ID
566
                    ORDER BY sps.ID ASC
567
                    ;", "stdSymbols", "StdPrjFolStructure");
568

    
569
                    using (SqlDataReader dr = cmd.ExecuteReader())
570
                        dt.Load(dr);
571
                }
572
                connection.Close();
573
            }
574

    
575
            return dt;
576
        }
577
        public static DataTable SelectStandardSymbolStructureTable()
578
        {
579
            DataTable dt = new DataTable();
580

    
581
            using (SqlConnection connection = new SqlConnection())
582
            {
583
                connection.ConnectionString = GetAvevaConnectionString_Admin();
584
                connection.Open();
585
                using (SqlCommand cmd = connection.CreateCommand())
586
                {
587
                    cmd.CommandText = string.Format(@"
588
                    SELECT * FROM {0}
589
                    ;", "StdPrjFolStructure");
590

    
591
                    using (SqlDataReader dr = cmd.ExecuteReader())
592
                        dt.Load(dr);
593
                }
594
                connection.Close();
595
            }
596

    
597
            return dt;
598
        }
599
        public static DataTable SelectStandardSymbolImageTable()
600
        {
601
            DataTable dt = new DataTable();
602

    
603
            using (SqlConnection connection = new SqlConnection())
604
            {
605
                connection.ConnectionString = GetAvevaConnectionString_Admin();
606
                connection.Open();
607
                using (SqlCommand cmd = connection.CreateCommand())
608
                {
609
                    cmd.CommandText = string.Format(@"
610
                    SELECT ss.Symbol_Name, ss.Pic_Binary, sps.Relative_Path FROM {0} as ss
611
                    LEFT OUTER JOIN {1} as sps
612
                    ON ss.Relative_Path = sps.ID
613
                    ORDER BY sps.ID ASC
614
                    ;", "stdSymbols", "StdPrjFolStructure");
615

    
616
                    using (SqlDataReader dr = cmd.ExecuteReader())
617
                        dt.Load(dr);
618
                }
619
                connection.Close();
620
            }
621

    
622
            return dt;
623
        }
624

    
625
        #endregion
626

    
627
    }
628
}
클립보드 이미지 추가 (최대 크기: 500 MB)