-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableCreator_Ext.cs
More file actions
272 lines (226 loc) · 8.93 KB
/
Copy pathTableCreator_Ext.cs
File metadata and controls
272 lines (226 loc) · 8.93 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InfinityInfo.DataEntities;
using System.Data.OleDb;
using System.Data;
namespace Entities.InstallExtensions
{
public static class TableCreator
{
public static int CreateDefinition(this DataEntity instance)
{
return CreateDefinition(instance, SchemaUpdateOptions.UpdateAll);
}
public static int CreateDefinition(this DataEntity instance, SchemaUpdateOptions mode)
{
StringBuilder sb1 = new StringBuilder();
//CREATE TABLE
sb1.Append(@"CREATE TABLE ");
//[ database_name.[ owner ] . | owner. ] table_name
sb1.Append(instance.EntityTableName);
//( { < column_definition >
// | column_name AS computed_column_expression
// | < table_constraint > } [ ,...n ]
//)
sb1.Append(@" ( ");
//sb1.Append(
// String.Join(
// " ",
// new string[] { instance.EntityPrimaryKeyFieldName, "VARCHAR(50)" })
// );
string comma = String.Empty;
foreach (DataField field in instance.FieldMappings)
{
sb1.Append(comma);
comma = ", ";
string fieldTypeDef = "VARCHAR(50)";
switch (field.DataType.Name)
{
case "String":
StringDataField stringField = field as StringDataField;
if (stringField is StringDataField)
{
if (stringField.MaxLength > 0)
{
fieldTypeDef = "VARCHAR(" + stringField.MaxLength + ")";
}
}
break;
case "DateTime":
fieldTypeDef = "DATETIME";
break;
case "Double":
fieldTypeDef = "float";
break;
case "Int32":
fieldTypeDef = "int";
break;
case "Decimal":
fieldTypeDef = "decimal";
break;
default:
break;
}
string primaryKeyIndicator = String.Empty;
if (field.IsPrimaryKey) { primaryKeyIndicator = @"PRIMARY KEY CLUSTERED"; }
sb1.Append(
String.Join(
" ",
new string[] { field.FieldName, fieldTypeDef, primaryKeyIndicator })
);
}
sb1.Append(@") ");
try
{
using (OleDbConnection cn = new OleDbConnection(instance.ActiveConnectionString))
{
cn.Open();
using (OleDbCommand cmd1 = cn.CreateCommand())
{
cmd1.CommandText = sb1.ToString();
cmd1.ExecuteNonQuery();
}
cn.Close();
}
}
catch (OleDbException ex)
{
string exMessage = ex.Message;
string formattedMessage = String.Format(@"There is already an object named '{0}' in the database.", instance.EntityTableName);
if (exMessage.Equals(formattedMessage))
{
instance.UpdateDefinition(SchemaUpdateOptions.UpdateFields);
}
else
{
throw ex;
}
}
return 0;
}
}
public static class TableUpdater
{
public static int UpdateDefinition(this DataEntity instance)
{
return UpdateDefinition(instance, SchemaUpdateOptions.UpdateAll);
}
[Obsolete("This method is obselete", false)]
public static int UpdateDefinition(this DataEntity instance, SchemaUpdateOptions mode)
{
if ((mode & SchemaUpdateOptions.DropTable) == SchemaUpdateOptions.DropTable) { return RemoveDefinition(instance, mode); }
List<string> existingColNames = new List<string>();
string checkExistsSelectQuery = String.Format("SELECT TOP 1 * FROM {1}", instance.EntityPrimaryKeyFieldName, instance.EntityTableName);
try
{
using (DataTable dt1 = GetResultsAsDataTable(instance.ActiveConnectionString, checkExistsSelectQuery))
{
DataColumnCollection cols = dt1.Columns;
foreach (DataColumn item in cols)
{
existingColNames.Add(item.ColumnName);
Console.WriteLine(item.ColumnName);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return -1;
}
// take list from select statement and cross-reference with items in the entity definition
string[] columnsColection = new string[] { "ADD GlennTestCol1 VarChar(100)" }; //, "ADD COLUMN GlennTestCol2 DateTime"
string updateSqlStarter = String.Format(@"ALTER TABLE {0} ", instance.EntityTableName);
StringBuilder sb1 = new StringBuilder(updateSqlStarter);
//sb1.Append("( ");
sb1.Append(String.Join(", ", columnsColection));
//sb1.Append(") ");
ExecuteSql(instance.ActiveConnectionString, sb1.ToString());
//ALTER TABLE [ database_name . [ schema_name ] . | schema_name . ] table_name
//{
// ALTER COLUMN column_name
// {
// [ type_schema_name. ] type_name [ ( { precision [ , scale ]
// | max | xml_schema_collection } ) ]
// [ COLLATE collation_name ]
// [ SPARSE | NULL | NOT NULL ]
// | {ADD | DROP }
// { ROWGUIDCOL | PERSISTED | NOT FOR REPLICATION | SPARSE }
// }
// | [ WITH { CHECK | NOCHECK } ]
// | ADD
// {
// <column_definition>
// | <computed_column_definition>
// | <table_constraint>
// | <column_set_definition>
// } [ ,...n ]
// | DROP
// {
// [ CONSTRAINT ] constraint_name
// [ WITH ( <drop_clustered_constraint_option> [ ,...n ] ) ]
// | COLUMN column_name
// } [ ,...n ]
return 0;
}
private static int RemoveDefinition(this DataEntity instance, SchemaUpdateOptions mode)
{
throw new NotImplementedException();
}
private static void ExecuteSql(string connectionString, string query)
{
using (OleDbConnection cn1 = new OleDbConnection(connectionString))
{
cn1.Open();
using (OleDbCommand cmd1 = cn1.CreateCommand())
{
cmd1.CommandText = query;
try
{
cmd1.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
cn1.Close();
}
}
private static DataTable GetResultsAsDataTable(string connectionString, string query)
{
DataTable dt1 = new DataTable();
try
{
using (OleDbConnection cn1 = new OleDbConnection(connectionString))
{
cn1.Open();
using (OleDbCommand cmd1 = cn1.CreateCommand())
{
cmd1.CommandText = query;
using (OleDbDataAdapter da1 = new OleDbDataAdapter(cmd1))
{
da1.Fill(dt1);
}
}
cn1.Close();
}
return dt1;
}
catch (Exception)
{
return new DataTable();
}
}
}
#region random thought
// ICM: remove MergeResultItem and its derivatives.
// Instead, add Attribute to fields (DataEntity!DataField) in the EntityModel
// that should be included in the result set rendering.
// Notes: This removes at least 1 type per data model
// that we define and further enables the framework for
// configuration via an UI.
#endregion
}