-
Notifications
You must be signed in to change notification settings - Fork 0
/
PWDb.cs
214 lines (210 loc) · 8.36 KB
/
PWDb.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
namespace techassessment
{
public class PWDb
{
protected string ConnectionString { get; set; }
System.Data.SqlClient.SqlConnection conn = default(System.Data.SqlClient.SqlConnection);
System.Data.SqlClient.SqlCommand cmd = default(System.Data.SqlClient.SqlCommand);
System.Data.SqlClient.SqlDataAdapter da = default(System.Data.SqlClient.SqlDataAdapter);
System.Data.SqlClient.SqlTransaction trx = default(System.Data.SqlClient.SqlTransaction);
public PWDb() { }
public PWDb(string connectionString)
{
conn = new System.Data.SqlClient.SqlConnection(connectionString);
cmd = new System.Data.SqlClient.SqlCommand();
da = new System.Data.SqlClient.SqlDataAdapter();
cmd.Connection = conn;
}
public void Disconnect()
{
conn.Close();
conn.Dispose();
}
public System.Data.DataSet Execute_FillDataset(string query)
{
System.Data.DataSet? ds = new System.Data.DataSet();
string SQLStr = "";
try
{
if (conn.State == System.Data.ConnectionState.Closed) conn.Open();
SQLStr = query;
cmd.CommandText = SQLStr;
da.SelectCommand = cmd;
da.Fill(ds);
}
catch (Exception ex)
{
Console.WriteLine($"! Exception while execute dataset fill function. {ex.Message}");
ds = null;
}
finally
{
if (conn.State == System.Data.ConnectionState.Open) conn.Close();
}
return ds;
}
public string Execute_NonQuery(string query, bool IsReturnValue = false)
{
string result = "";
try
{
if (conn.State == System.Data.ConnectionState.Closed) conn.Open();
cmd.CommandText = query;
if (IsReturnValue == true)
{
result = Convert.ToString(cmd.ExecuteNonQuery());
}
else
{
cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
Console.WriteLine($"! Exception when executing non query statement. {ex.Message}");
result = "-1";
}
finally
{
if (conn.State == System.Data.ConnectionState.Open) conn.Close();
}
return result;
}
public object Execute_Scalar(string query)
{
object result = null;
try
{
if (conn.State == System.Data.ConnectionState.Closed) conn.Open();
cmd.CommandText = query;
result = cmd.ExecuteScalar();
if (result == null)
{
result = "";
}
}
catch (Exception ex)
{
Console.WriteLine($"Exception executing scalar statement. {ex.Message}");
result = "";
}
finally
{
if (conn.State == System.Data.ConnectionState.Open) conn.Close();
}
return result;
}
public string SQLS(string data)
{
return string.IsNullOrEmpty(data) ? "" : "N'" + data.Replace("'", "''") + "'";
}
public dynamic Dyna(object value)
{
try
{
if (value != null)
{
string valuetype = value.GetType().Name;
switch (valuetype)
{
case "String":
return SQLS(value.ToString().TrimEnd());
case "DateTime":
return SQLS(Convert.ToDateTime(value).ToString("yyyy-MM-ddTHH:mm:ss"));
default:
return value;
}
}
else
{
return null;
}
}
catch (Exception ex)
{
Console.WriteLine($"! Exception while handling platform well record. {ex.Message}");
throw;
}
}
private bool IsRecordExist(object id, object tablename)
{
bool IsExist = false;
try
{
string SQLQuery = $"SELECT COUNT(id) FROM {tablename} WHERE id={id}";
int? cnt = Execute_Scalar(SQLQuery) as int?;
if (cnt != null && cnt > 0)
IsExist = true;
}
catch (Exception ex)
{
Console.WriteLine($"! Exception while verify record is exist. {ex.Message}");
}
return IsExist;
}
public void HandlePlatformWellData(List<Platform> pw)
{
try
{
if (pw != null)
{
if (pw.Count == 0)
{
Console.WriteLine($"! Platform record empty");
}
foreach (Platform p in pw)
{
InsertUpdatePlatformRecord(p);
if (p.well != null)
{
if (p.well.Count == 0)
{
Console.WriteLine($"! No well record under this platform: {p.uniqueName}");
}
foreach (Well w in p.well)
{
InsertUpdateWellRecord(w);
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"! Exception while handling platform well record. {ex.Message}");
}
}
private void InsertUpdatePlatformRecord(Platform p)
{
try
{
string SQLQuery = string.Empty;
if (IsRecordExist(p.id, "platform"))
SQLQuery = $"UPDATE dbo.platform SET uniqueName={Dyna(p.uniqueName)},latitude={Dyna(p.latitude)},longitude={Dyna(p.longitude)}" + (p.createdAt != null ? $",createdAt={Dyna(p.createdAt)}" : "") + (p.updatedAt != null ? $",updatedAt={Dyna(p.updatedAt)}" : "") + $" WHERE id={Dyna(p.id)};";
else
SQLQuery = $"INSERT INTO dbo.platform(id,uniqueName,latitude,longitude{(p.createdAt != null ? ",createdAt" : "")}{(p.updatedAt != null ? ",updatedAt" : "")}) VALUES({ Dyna(p.id)},{Dyna(p.uniqueName)},{Dyna(p.latitude)},{Dyna(p.longitude)}{(p.createdAt != null ? $",{ Dyna(p.createdAt)}" : "")}{(p.updatedAt != null ? $",{ Dyna(p.updatedAt)}" : "")})";
int affected = Convert.ToInt32(Execute_NonQuery(SQLQuery, true));
}
catch (Exception ex)
{
Console.WriteLine($"! Exception while inserting platform record. {ex.Message}");
}
}
private void InsertUpdateWellRecord(Well w)
{
try
{
string SQLQuery = string.Empty;
if (IsRecordExist(w.id, "well"))
SQLQuery = $"UPDATE dbo.well SET platformId = {Dyna(w.platformId)},uniqueName={Dyna(w.uniqueName)},latitude={Dyna(w.latitude)},longitude = {Dyna(w.longitude)}" + (w.createdAt != null ? $",createdAt = {Dyna(w.createdAt)}" : "") + (w.updatedAt != null ? $",updatedAt = {Dyna(w.updatedAt)}" : "") + $"WHERE id = {Dyna(w.id)};";
else
SQLQuery = $"INSERT INTO dbo.well(id,platformId,uniqueName,latitude,longitude{(w.createdAt != null ? ",createdAt" : "")}{(w.updatedAt != null ? ",updatedAt" : "")}) VALUES({ Dyna(w.id)},{Dyna(w.platformId)},{Dyna(w.uniqueName)},{Dyna(w.latitude)},{Dyna(w.longitude)}{(w.createdAt != null ? $",{ Dyna(w.createdAt)}" : "")}{(w.updatedAt != null ? $",{ Dyna(w.updatedAt)}" : "")})";
int affected = Convert.ToInt32(Execute_NonQuery(SQLQuery, true));
}
catch (Exception ex)
{
Console.WriteLine($"! Exception while inserting well record. {ex.Message}");
}
}
}
}