|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Data; |
| 4 | +using System.Data.SqlClient; |
| 5 | +using System.Diagnostics; |
| 6 | + |
| 7 | +namespace GenericSQLEntityHandler |
| 8 | +{ |
| 9 | + public class SQLTableCreator |
| 10 | + { |
| 11 | + #region Private Members |
| 12 | + private SqlConnection connection = null; |
| 13 | + private SqlTransaction transaction = null; |
| 14 | + |
| 15 | + |
| 16 | + #endregion Private Members |
| 17 | + |
| 18 | + #region Public Members |
| 19 | + |
| 20 | + public SqlConnection SqlConnection { get; set; } |
| 21 | + public SqlTransaction SqlTransaction { get; set; } |
| 22 | + public string DestinationTableName { get; set; } |
| 23 | + |
| 24 | + #endregion Public Members |
| 25 | + |
| 26 | + |
| 27 | + #region Constructor |
| 28 | + public SQLTableCreator() { } |
| 29 | + |
| 30 | + public SQLTableCreator(SqlConnection connection) : this(connection, null) { } |
| 31 | + |
| 32 | + public SQLTableCreator(SqlConnection sqlConnection, SqlTransaction sqlTransaction) |
| 33 | + { |
| 34 | + SqlConnection = sqlConnection; |
| 35 | + SqlTransaction = sqlTransaction; |
| 36 | + } |
| 37 | + |
| 38 | + #endregion Constructor |
| 39 | + |
| 40 | + #region Methods |
| 41 | + public bool Create(DataTable schema, int[] primaryKeys, string destinationTableName, Type entityType) |
| 42 | + { |
| 43 | + try |
| 44 | + { |
| 45 | + |
| 46 | + string sql = GetCreateSql(destinationTableName, schema, primaryKeys, entityType); |
| 47 | + SqlCommand cmd; |
| 48 | + if (transaction != null && transaction.Connection != null) |
| 49 | + cmd = new SqlCommand(sql, connection, transaction); |
| 50 | + else |
| 51 | + cmd = new SqlCommand(sql, connection); |
| 52 | + |
| 53 | + return cmd.ExecuteNonQuery() == 1; |
| 54 | + |
| 55 | + } |
| 56 | + catch (Exception ex) |
| 57 | + { |
| 58 | + Debug.WriteLine(ex.ToString()); |
| 59 | + throw; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + private string GetCreateSql(string tableName, DataTable schema, int[] primaryKeys, Type entityType) |
| 65 | + { |
| 66 | + string sql = "CREATE TABLE [" + tableName + "] (\n"; |
| 67 | + |
| 68 | + // columns |
| 69 | + foreach (DataRow column in schema.Rows) |
| 70 | + { |
| 71 | + |
| 72 | + if (!(schema.Columns.Contains("IsHidden") && column["IsHidden"] != DBNull.Value && (bool)column["IsHidden"]) && entityType.GetProperty(column["ColumnName"].ToString()) != null) |
| 73 | + { |
| 74 | + sql += "\t[" + column["ColumnName"] + "] " + SQLGetType(column); |
| 75 | + if (schema.Columns.Contains("IsIdentity") && column["IsIdentity"] != DBNull.Value && (bool)column["IsIdentity"]) |
| 76 | + sql += " IDENTITY(1,1)"; |
| 77 | + if (schema.Columns.Contains("AllowDBNull") && column["AllowDBNull"] != DBNull.Value && (bool)column["AllowDBNull"] == false) |
| 78 | + sql += " NOT NULL"; |
| 79 | + sql += ",\n"; |
| 80 | + } |
| 81 | + } |
| 82 | + sql = sql.TrimEnd(',', '\n') + "\n"; |
| 83 | + |
| 84 | + // primary keys |
| 85 | + string pk = ", CONSTRAINT PK_" + tableName + " PRIMARY KEY CLUSTERED ("; |
| 86 | + bool hasKeys = (primaryKeys != null && primaryKeys.Length > 0); |
| 87 | + if (hasKeys) |
| 88 | + { |
| 89 | + // user defined keys |
| 90 | + foreach (int key in primaryKeys) |
| 91 | + { |
| 92 | + pk += schema.Rows[key]["ColumnName"] + ", "; |
| 93 | + } |
| 94 | + } |
| 95 | + else |
| 96 | + { |
| 97 | + // check schema for keys |
| 98 | + string keys = string.Join(", ", GetPrimaryKeys(schema)); |
| 99 | + pk += keys; |
| 100 | + hasKeys = keys.Length > 0; |
| 101 | + } |
| 102 | + pk = pk.TrimEnd(',', ' ', '\n') + ")\n"; |
| 103 | + |
| 104 | + if (hasKeys) |
| 105 | + sql += pk; |
| 106 | + |
| 107 | + sql += ")"; |
| 108 | + return sql; |
| 109 | + } |
| 110 | + |
| 111 | + private string[] GetPrimaryKeys(DataTable schema) |
| 112 | + { |
| 113 | + List<string> keys = new List<string>(); |
| 114 | + |
| 115 | + foreach (DataRow column in schema.Rows) |
| 116 | + { |
| 117 | + if (schema.Columns.Contains("IsKey") && column["IsKey"] != DBNull.Value && (bool)column["IsKey"]) |
| 118 | + keys.Add(column["ColumnName"].ToString()); |
| 119 | + } |
| 120 | + |
| 121 | + return keys.ToArray(); |
| 122 | + } |
| 123 | + |
| 124 | + // Overload based on row from schema table |
| 125 | + private string SQLGetType(DataRow schemaRow) |
| 126 | + { |
| 127 | + string type = schemaRow["DataTypeName"].ToString(); |
| 128 | + |
| 129 | + if (schemaRow["DataType"].ToString() == "System.String" || schemaRow["DataType"].ToString() == "System.Byte[]") |
| 130 | + type += "(" + (int.Parse(schemaRow["ColumnSize"].ToString()) == int.MaxValue ? "MAX" : schemaRow["ColumnSize"].ToString()) + ")"; |
| 131 | + return type; |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | + #endregion Methods |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | + } |
| 142 | +} |
0 commit comments