프로젝트

일반

사용자정보

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

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

이력 | 보기 | 이력해설 | 다운로드 (19.5 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.Data;
7
using System.Globalization;
8
using System.IO;
9
using Oracle.ManagedDataAccess.Client;
10

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

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

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

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

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

    
51

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

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

    
89
                }
90
            }
91
            catch (Exception ex)
92
            {
93

    
94
            }
95

    
96
            return bResult;
97
        }
98

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

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

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

    
158
                }
159
            }
160
            catch (Exception ex)
161
            {
162

    
163
            }
164

    
165
            return bResult;
166
        }
167

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

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

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

    
203
                }
204
            }
205
            catch (Exception ex)
206
            {
207

    
208
            }
209

    
210
            return plantList;
211
        }
212

    
213
        public static DataTable GetUnitTree()
214
        {
215
            DataTable dt = new DataTable();
216
            try
217
            {
218
                SPPIDInfo dbInfo = SPPIDInfo.GetInstance();
219

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

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

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

    
248
                }
249
            }
250
            catch (Exception ex)
251
            {
252

    
253
            }
254

    
255
            return dt;
256
        }
257

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

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

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

    
294
                }
295

    
296
            }
297
            catch (Exception ex)
298
            {
299

    
300
            }
301

    
302
            return TemplatePath;
303
        }
304

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

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

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

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

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

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

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

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

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

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

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

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

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

    
407
                            }
408
                        }
409
                    }
410
                }
411
                else
412
                {
413

    
414
                }
415
            }
416
            catch (Exception ex)
417
            {
418

    
419
            }
420

    
421

    
422

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