프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter_AutoModeling / DB / DB.cs @ aa826cf3

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

1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
using Oracle.ManagedDataAccess.Client;
7
using Oracle.ManagedDataAccess.Types;
8
using SPPID.Model;
9
using System.Data;
10
using System.Globalization;
11
using System.IO;
12

    
13
namespace SPPID.DB
14
{
15
    public class DB
16
    {
17
        private const string oConnString = "(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST ={0}" + @")(PORT = {1})) ) (CONNECT_DATA = (SERVICE_NAME = {2})))";
18

    
19
        public static bool CheckAndSetDBInformation()
20
        {
21
            bool bResult = false;
22
            DBInformation dbInfo = DBInformation.GetInstance();
23
            try
24
            {
25
                if (dbInfo.DBType == "ORACLE")
26
                {
27
                    string connString = string.Format(oConnString, dbInfo.ServerIP, dbInfo.Port, dbInfo.Service);
28
                    connString = "Data Source=" + connString + ";User Id=" + dbInfo.DBUser + ";Password=" + dbInfo.DBPassword;
29

    
30
                    using (OracleConnection conn = new OracleConnection(connString))
31
                    {
32
                        conn.Open();
33
                        if (conn.State == System.Data.ConnectionState.Open)
34
                        {
35
                            using (OracleCommand cmd = new OracleCommand())
36
                            {
37
                                cmd.Connection = conn;
38
                                cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT NAME, PATH FROM {0}.T_ROOTITEM", dbInfo.Site);
39

    
40
                                using (OracleDataReader reader = cmd.ExecuteReader())
41
                                {
42
                                    while (reader.Read())
43
                                    {
44
                                        dbInfo.Plant = reader["NAME"].ToString();
45
                                        dbInfo.PlantPath = reader["PATH"].ToString();
46
                                    }
47

    
48
                                    cmd.CommandText = string.Format(CultureInfo.CurrentCulture, @"SELECT SP_SCHEMA_TYPE, USERNAME
49
                                                FROM {0}.T_DB_DATA DB_DATA 
50
                                                INNER JOIN {0}.T_ROOTITEM ROOTITEM ON DB_DATA.SP_ROOTITEMID = ROOTITEM.SP_ID 
51
                                                WHERE ROOTITEM.NAME = '{1}'", dbInfo.Site, dbInfo.Plant);
52

    
53

    
54
                                    using (OracleDataAdapter adapter = new OracleDataAdapter())
55
                                    {
56
                                        DataTable dt = new DataTable();
57
                                        adapter.SelectCommand = cmd;
58
                                        adapter.Fill(dt);
59

    
60
                                        foreach (DataRow row in dt.Rows)
61
                                        {
62
                                            string sType = row["SP_SCHEMA_TYPE"].ToString();
63
                                            switch (sType)
64
                                            {
65
                                                case "SPPIDDATA_DICTIONARY":
66
                                                    dbInfo.PlantPIDDic = row["USERNAME"].ToString();
67
                                                    break;
68
                                                case "DATA_DICTIONARY":
69
                                                    dbInfo.PlantDic = row["USERNAME"].ToString();
70
                                                    break;
71
                                                case "SPAPLANT":
72
                                                    dbInfo.Plant = row["USERNAME"].ToString();
73
                                                    break;
74
                                                case "SPPID":
75
                                                    dbInfo.PlantPID = row["USERNAME"].ToString();
76
                                                    break;
77
                                                default:
78
                                                    break;
79
                                            }
80
                                        }
81
                                        bResult = true;
82
                                    }
83
                                }
84
                            }
85
                        }
86
                    }
87
                }
88
                else
89
                {
90

    
91
                }
92
            }
93
            catch (Exception ex)
94
            {
95

    
96
            }
97

    
98
            return bResult;
99
        }
100

    
101
        public static bool SetPlantDBSetting()
102
        {
103
            bool bResult = false;
104
            DBInformation dbInfo = DBInformation.GetInstance();
105
            try
106
            {
107
                if (dbInfo.DBType == "ORACLE")
108
                {
109
                    string connString = string.Format(oConnString, dbInfo.ServerIP, dbInfo.Port, dbInfo.Service);
110
                    connString = "Data Source=" + connString + ";User Id=" + dbInfo.DBUser + ";Password=" + dbInfo.DBPassword;
111

    
112
                    using (OracleConnection conn = new OracleConnection(connString))
113
                    {
114
                        conn.Open();
115
                        if (conn.State == System.Data.ConnectionState.Open)
116
                        {
117
                            using (OracleCommand cmd = new OracleCommand())
118
                            {
119
                                cmd.Connection = conn;
120
                                cmd.CommandText = string.Format(CultureInfo.CurrentCulture, @"SELECT SP_SCHEMA_TYPE, USERNAME
121
                                                FROM {0}.T_DB_DATA DB_DATA 
122
                                                INNER JOIN {0}.T_ROOTITEM ROOTITEM ON DB_DATA.SP_ROOTITEMID = ROOTITEM.SP_ID 
123
                                                WHERE ROOTITEM.NAME = '{1}'", dbInfo.Site, dbInfo.Plant);
124
                                using (OracleDataAdapter adapter = new OracleDataAdapter())
125
                                {
126
                                    DataTable dt = new DataTable();
127
                                    adapter.SelectCommand = cmd;
128
                                    adapter.Fill(dt);
129

    
130
                                    foreach (DataRow row in dt.Rows)
131
                                    {
132
                                        string sType = row["SP_SCHEMA_TYPE"].ToString();
133
                                        switch (sType)
134
                                        {
135
                                            case "SPPIDDATA_DICTIONARY":
136
                                                dbInfo.PlantPIDDic = row["USERNAME"].ToString();
137
                                                break;
138
                                            case "DATA_DICTIONARY":
139
                                                dbInfo.PlantDic = row["USERNAME"].ToString();
140
                                                break;
141
                                            case "SPAPLANT":
142
                                                dbInfo.Plant = row["USERNAME"].ToString();
143
                                                break;
144
                                            case "SPPID":
145
                                                dbInfo.PlantPID = row["USERNAME"].ToString();
146
                                                break;
147
                                            default:
148
                                                break;
149
                                        }
150
                                    }
151
                                    bResult = true;
152
                                }
153
                            }
154
                        }
155
                    }
156
                }
157
                else
158
                {
159

    
160
                }
161
            }
162
            catch (Exception ex)
163
            {
164

    
165
            }
166

    
167
            return bResult;
168
        }
169

    
170
        public static List<string> GetPlantList()
171
        {
172
            DBInformation dbInfo = DBInformation.GetInstance();
173
            List<string> plantList = new List<string>();
174
            try
175
            {
176
                if (dbInfo.DBType == "ORACLE")
177
                {
178
                    string connString = string.Format(oConnString, dbInfo.ServerIP, dbInfo.Port, dbInfo.Service);
179
                    connString = "Data Source=" + connString + ";User Id=" + dbInfo.DBUser + ";Password=" + dbInfo.DBPassword;
180

    
181
                    using (OracleConnection conn = new OracleConnection(connString))
182
                    {
183
                        conn.Open();
184
                        if (conn.State == System.Data.ConnectionState.Open)
185
                        {
186
                            using (OracleCommand cmd = new OracleCommand())
187
                            {
188
                                cmd.Connection = conn;
189
                                cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT NAME, PATH FROM {0}.T_ROOTITEM", dbInfo.Site);
190

    
191
                                using (OracleDataReader reader = cmd.ExecuteReader())
192
                                {
193
                                    while (reader.Read())
194
                                    {
195
                                        plantList.Add(reader["NAME"].ToString());
196
                                    }
197
                                }
198
                            }
199
                        }
200
                    }
201
                }
202
                else
203
                {
204

    
205
                }
206
            }
207
            catch (Exception ex)
208
            {
209

    
210
            }
211

    
212
            return plantList;
213
        }
214

    
215
        public static DataTable GetUnitTree()
216
        {
217
            DataTable dt = new DataTable();
218
            try
219
            {
220
                DBInformation dbInfo = DBInformation.GetInstance();
221

    
222
                if (dbInfo.DBType == "ORACLE")
223
                {
224
                    string connString = string.Format(oConnString, dbInfo.ServerIP, dbInfo.Port, dbInfo.Service);
225
                    connString = "Data Source=" + connString + ";User Id=" + dbInfo.DBUser + ";Password=" + dbInfo.DBPassword;
226

    
227
                    using (OracleConnection conn = new OracleConnection(connString))
228
                    {
229
                        conn.Open();
230
                        if (conn.State == System.Data.ConnectionState.Open)
231
                        {
232
                            string sQuery = string.Format(CultureInfo.CurrentCulture,
233
                                @"SELECT SP_ID, PARENTID, NAME
234
                                    FROM {0}.T_PLANTGROUP 
235
                                    START WITH PARENTID='-1'
236
                                    CONNECT BY PRIOR SP_ID=PARENTID", dbInfo.Plant);
237

    
238
                            using (OracleCommand cmd = new OracleCommand(sQuery, conn))
239
                            using (OracleDataAdapter adapter = new OracleDataAdapter()) 
240
                            {
241
                                adapter.SelectCommand = cmd;
242
                                adapter.Fill(dt);
243
                            }
244
                        }
245
                    }
246
                }
247
                else
248
                {
249

    
250
                }
251
            }
252
            catch (Exception ex)
253
            {
254

    
255
            }
256

    
257
            return dt;
258
        }
259

    
260
        public static string GetPlantPID_T_OPTIONSETTING_Value(string name)
261
        {
262
            string TemplatePath = string.Empty;
263
            DBInformation dbInfo = DBInformation.GetInstance();
264
            try
265
            {
266
                if (dbInfo.DBType == "ORACLE")
267
                {
268
                    string connString = string.Format(oConnString, dbInfo.ServerIP, dbInfo.Port, dbInfo.Service);
269
                    connString = "Data Source=" + connString + ";User Id=" + dbInfo.DBUser + ";Password=" + dbInfo.DBPassword;
270

    
271
                    using (OracleConnection conn = new OracleConnection(connString))
272
                    {
273
                        conn.Open();
274
                        if (conn.State == System.Data.ConnectionState.Open)
275
                        {
276
                            using (OracleCommand cmd = new OracleCommand())
277
                            {
278
                                cmd.Connection = conn;
279
                                cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT VALUE FROM {0}.T_OPTIONSETTING WHERE NAME = '{1}'", dbInfo.PlantPID, name);
280

    
281
                                using (OracleDataReader reader = cmd.ExecuteReader())
282
                                {
283
                                    while (reader.Read())
284
                                    {
285
                                        TemplatePath = reader["VALUE"].ToString();
286
                                        break;
287
                                    }
288
                                }
289
                            }
290
                        }
291
                    }
292
                }
293
                else
294
                {
295

    
296
                }
297

    
298
            }
299
            catch (Exception ex)
300
            {
301

    
302
            }
303

    
304
            return TemplatePath;
305
        }
306

    
307
        public static List<string> GetSPPIDAttribute()
308
        {
309
            List<string> attributes = new List<string>();
310
            DBInformation dbInfo = DBInformation.GetInstance();
311
            try
312
            {
313
                if (dbInfo.DBType == "ORACLE")
314
                {
315
                    string connString = string.Format(oConnString, dbInfo.ServerIP, dbInfo.Port, dbInfo.Service);
316
                    connString = "Data Source=" + connString + ";User Id=" + dbInfo.DBUser + ";Password=" + dbInfo.DBPassword;
317

    
318
                    using (OracleConnection conn = new OracleConnection(connString))
319
                    {
320
                        conn.Open();
321
                        if (conn.State == System.Data.ConnectionState.Open)
322
                        {
323
                            using (OracleCommand cmd = new OracleCommand())
324
                            {
325
                                cmd.Connection = conn;
326

    
327
                                // 정리 필요
328
                                cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT * FROM {0}.{1}", dbInfo.PlantPID, "T_EQUIPMENT");
329
                                using (OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly))
330
                                {
331
                                    DataTable dt = reader.GetSchemaTable();
332
                                    foreach (DataRow row in dt.Rows)
333
                                        attributes.Add(row["ColumnName"].ToString());
334
                                }
335

    
336
                                cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT * FROM {0}.{1}", dbInfo.PlantPID, "T_NOZZLE");
337
                                using (OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly))
338
                                {
339
                                    DataTable dt = reader.GetSchemaTable();
340
                                    foreach (DataRow row in dt.Rows)
341
                                        attributes.Add(row["ColumnName"].ToString());
342
                                }
343

    
344
                                cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT * FROM {0}.{1}", dbInfo.PlantPID, "T_PIPINGCOMP");
345
                                using (OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly))
346
                                {
347
                                    DataTable dt = reader.GetSchemaTable();
348
                                    foreach (DataRow row in dt.Rows)
349
                                        attributes.Add(row["ColumnName"].ToString());
350
                                }
351

    
352
                                cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT * FROM {0}.{1}", dbInfo.PlantPID, "T_PIPERUN");
353
                                using (OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly))
354
                                {
355

    
356
                                    DataTable dt = reader.GetSchemaTable();
357
                                    foreach (DataRow row in dt.Rows)
358
                                        attributes.Add(row["ColumnName"].ToString());
359
                                }
360

    
361
                                cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT * FROM {0}.{1}", dbInfo.PlantPID, "T_INSTRUMENT");
362
                                using (OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly))
363
                                {
364
                                    DataTable dt = reader.GetSchemaTable();
365
                                    foreach (DataRow row in dt.Rows)
366
                                        attributes.Add(row["ColumnName"].ToString());
367
                                }
368

    
369
                                cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT * FROM {0}.{1}", dbInfo.PlantPID, "T_INLINECOMP");
370
                                using (OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly))
371
                                {
372
                                    DataTable dt = reader.GetSchemaTable();
373
                                    foreach (DataRow row in dt.Rows)
374
                                        attributes.Add(row["ColumnName"].ToString());
375
                                }
376

    
377
                                cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT * FROM {0}.{1}", dbInfo.PlantPID, "T_VESSEL");
378
                                using (OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly))
379
                                {
380
                                    DataTable dt = reader.GetSchemaTable();
381
                                    foreach (DataRow row in dt.Rows)
382
                                        attributes.Add(row["ColumnName"].ToString());
383
                                }
384

    
385
                                cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT * FROM {0}.{1}", dbInfo.PlantPID, "T_EXCHANGER");
386
                                using (OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly))
387
                                {
388
                                    DataTable dt = reader.GetSchemaTable();
389
                                    foreach (DataRow row in dt.Rows)
390
                                        attributes.Add(row["ColumnName"].ToString());
391
                                }
392

    
393
                                cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT * FROM {0}.{1}", dbInfo.PlantPID, "T_MECHANICAL");
394
                                using (OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly))
395
                                {
396
                                    DataTable dt = reader.GetSchemaTable();
397
                                    foreach (DataRow row in dt.Rows)
398
                                        attributes.Add(row["ColumnName"].ToString());
399
                                }
400

    
401
                                cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT * FROM {0}.{1}", dbInfo.PlantPID, "T_EQUIPCOMPONENT");
402
                                using (OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly))
403
                                {
404
                                    DataTable dt = reader.GetSchemaTable();
405
                                    foreach (DataRow row in dt.Rows)
406
                                        attributes.Add(row["ColumnName"].ToString());
407
                                }
408

    
409
                            }
410
                        }
411
                    }
412
                }
413
                else
414
                {
415

    
416
                }
417
            }
418
            catch (Exception ex)
419
            {
420

    
421
            }
422

    
423

    
424

    
425
            return attributes.Distinct().ToList();
426
        }
427
    }
428
}
클립보드 이미지 추가 (최대 크기: 500 MB)