|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Data; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +namespace ParquetFileViewer |
| 10 | +{ |
| 11 | + public class CustomScriptBasedSchemaAdapter |
| 12 | + { |
| 13 | + internal readonly static Hashtable TypeMap; |
| 14 | + |
| 15 | + public string TablePrefix { get; set; } |
| 16 | + public bool CascadeDeletes { get; set; } |
| 17 | + |
| 18 | + static CustomScriptBasedSchemaAdapter() |
| 19 | + { |
| 20 | + Hashtable hashtable = new Hashtable(); |
| 21 | + hashtable.Add(typeof(ulong), "bigint {1}NULL"); |
| 22 | + hashtable.Add(typeof(long), "bigint {1}NULL"); |
| 23 | + hashtable.Add(typeof(bool), "bit {1}NULL"); |
| 24 | + hashtable.Add(typeof(char), "char {1}NULL"); |
| 25 | + hashtable.Add(typeof(DateTime), "datetime {1}NULL"); |
| 26 | + hashtable.Add(typeof(double), "float {1}NULL"); |
| 27 | + hashtable.Add(typeof(uint), "int {1}NULL"); |
| 28 | + hashtable.Add(typeof(int), "int {1}NULL"); |
| 29 | + hashtable.Add(typeof(Guid), "uniqueidentifier {1}NULL"); |
| 30 | + hashtable.Add(typeof(ushort), "smallint {1}NULL"); |
| 31 | + hashtable.Add(typeof(short), "smallint {1}NULL"); |
| 32 | + hashtable.Add(typeof(decimal), "real {1}NULL"); |
| 33 | + hashtable.Add(typeof(byte), "tinyint {1}NULL"); |
| 34 | + hashtable.Add(typeof(sbyte), "tinyint {1}NULL"); |
| 35 | + hashtable.Add(typeof(string), "nvarchar({0}) {1}NULL"); |
| 36 | + hashtable.Add(typeof(TimeSpan), "int {1}NULL"); |
| 37 | + hashtable.Add(typeof(byte[]), "varbinary {1}NULL"); |
| 38 | + TypeMap = hashtable; |
| 39 | + } |
| 40 | + |
| 41 | + public string GetCreateScript(string databaseName) |
| 42 | + { |
| 43 | + if (databaseName == null || databaseName.Trim().Length == 0) |
| 44 | + { |
| 45 | + throw new ArgumentException(string.Format("The database name passed is {0}", (databaseName == null ? "null" : "empty")), "databaseName"); |
| 46 | + } |
| 47 | + |
| 48 | + return string.Format("IF NOT EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'{0}') BEGIN CREATE DATABASE {1};\n END\n", databaseName, this.MakeSafe(databaseName)); |
| 49 | + } |
| 50 | + |
| 51 | + public string GetSchemaScript(DataSet dataSet, bool markTablesAsLocalTemp) |
| 52 | + { |
| 53 | + if (dataSet == null) |
| 54 | + { |
| 55 | + throw new ArgumentException("null is not a valid parameter value", "dataSet"); |
| 56 | + } |
| 57 | + StringBuilder stringBuilder = new StringBuilder(); |
| 58 | + foreach (DataTable table in dataSet.Tables) |
| 59 | + { |
| 60 | + try |
| 61 | + { |
| 62 | + stringBuilder.Append(this.MakeTable(table, markTablesAsLocalTemp)); |
| 63 | + } |
| 64 | + catch (ArgumentException argumentException) |
| 65 | + { |
| 66 | + throw new ArgumentException("Table does not contain any columns", table.TableName, argumentException); |
| 67 | + } |
| 68 | + } |
| 69 | + foreach (DataTable dataTable in dataSet.Tables) |
| 70 | + { |
| 71 | + if ((int)dataTable.PrimaryKey.Length <= 0) |
| 72 | + { |
| 73 | + continue; |
| 74 | + } |
| 75 | + string str = this.MakeSafe(string.Concat(this.TablePrefix, dataTable.TableName)); |
| 76 | + string str1 = this.MakeSafe(string.Concat("PK_", this.TablePrefix, dataTable.TableName)); |
| 77 | + string str2 = this.MakeList(dataTable.PrimaryKey); |
| 78 | + stringBuilder.AppendFormat("IF OBJECT_ID('{1}', 'PK') IS NULL BEGIN ALTER TABLE {0} WITH NOCHECK ADD CONSTRAINT {1} PRIMARY KEY CLUSTERED ({2}); END\n", str, str1, str2); |
| 79 | + } |
| 80 | + foreach (DataRelation relation in dataSet.Relations) |
| 81 | + { |
| 82 | + try |
| 83 | + { |
| 84 | + stringBuilder.Append(this.MakeRelation(relation)); |
| 85 | + } |
| 86 | + catch (ArgumentException argumentException1) |
| 87 | + { |
| 88 | + throw new ArgumentException("Relationship has an empty column list", relation.RelationName, argumentException1); |
| 89 | + } |
| 90 | + } |
| 91 | + return stringBuilder.ToString(); |
| 92 | + } |
| 93 | + |
| 94 | + protected string GetTypeFor(DataColumn column) |
| 95 | + { |
| 96 | + string item = (string)TypeMap[column.DataType]; |
| 97 | + if (item == null) |
| 98 | + { |
| 99 | + throw new NotSupportedException(string.Format("No type mapping is provided for {0}", column.DataType.Name)); |
| 100 | + } |
| 101 | + return string.Format(item, column.DataType == typeof(string) ? "MAX" : column.MaxLength.ToString(), (column.AllowDBNull ? string.Empty : "NOT ")); |
| 102 | + } |
| 103 | + |
| 104 | + private string MakeList(DataColumn[] columns) |
| 105 | + { |
| 106 | + if (columns == null || (int)columns.Length < 1) |
| 107 | + { |
| 108 | + throw new ArgumentException("Invalid column list!", "columns"); |
| 109 | + } |
| 110 | + StringBuilder stringBuilder = new StringBuilder(); |
| 111 | + bool flag = true; |
| 112 | + DataColumn[] dataColumnArray = columns; |
| 113 | + for (int i = 0; i < (int)dataColumnArray.Length; i++) |
| 114 | + { |
| 115 | + DataColumn dataColumn = dataColumnArray[i]; |
| 116 | + if (!flag) |
| 117 | + { |
| 118 | + stringBuilder.Append(", "); |
| 119 | + } |
| 120 | + stringBuilder.Append(this.MakeSafe(dataColumn.ColumnName)); |
| 121 | + flag = false; |
| 122 | + } |
| 123 | + return stringBuilder.ToString(); |
| 124 | + } |
| 125 | + |
| 126 | + private string MakeList(DataColumnCollection columns) |
| 127 | + { |
| 128 | + if (columns == null || columns.Count < 1) |
| 129 | + { |
| 130 | + throw new ArgumentException("Invalid column list!", "columns"); |
| 131 | + } |
| 132 | + StringBuilder stringBuilder = new StringBuilder(); |
| 133 | + bool flag = true; |
| 134 | + foreach (DataColumn column in columns) |
| 135 | + { |
| 136 | + if (!flag) |
| 137 | + { |
| 138 | + stringBuilder.Append(", "); |
| 139 | + } |
| 140 | + string str = this.MakeSafe(column.ColumnName); |
| 141 | + string typeFor = this.GetTypeFor(column); |
| 142 | + stringBuilder.AppendFormat("{0} {1}", str, typeFor); |
| 143 | + flag = false; |
| 144 | + } |
| 145 | + return stringBuilder.ToString(); |
| 146 | + } |
| 147 | + |
| 148 | + private string MakeRelation(DataRelation relation) |
| 149 | + { |
| 150 | + if (relation == null) |
| 151 | + { |
| 152 | + throw new ArgumentException("Invalid argument value (null)", "relation"); |
| 153 | + } |
| 154 | + |
| 155 | + string childTable = this.MakeSafe(string.Concat(this.TablePrefix, relation.ChildTable.TableName)); |
| 156 | + string parentTable = this.MakeSafe(string.Concat(this.TablePrefix, relation.ParentTable.TableName)); |
| 157 | + string fkRelationName = this.MakeSafe(string.Concat(this.TablePrefix, relation.RelationName)); //Add prefix so same tables can be created using different prefixes. Otherwise collisions occur |
| 158 | + string childTableFKColumns = this.MakeList(relation.ChildColumns); |
| 159 | + string parentTableFKColumns = this.MakeList(relation.ParentColumns); |
| 160 | + |
| 161 | + return $"IF OBJECT_ID('{fkRelationName}', 'F') IS NULL BEGIN ALTER TABLE {childTable} " + |
| 162 | + $"ADD CONSTRAINT {fkRelationName} FOREIGN KEY ({childTableFKColumns}) REFERENCES {parentTable} ({parentTableFKColumns})" + |
| 163 | + $"{(this.CascadeDeletes ? " ON DELETE CASCADE" : string.Empty)}; END\n"; |
| 164 | + } |
| 165 | + |
| 166 | + protected string MakeSafe(string inputValue) |
| 167 | + { |
| 168 | + string str = inputValue.Trim(); |
| 169 | + string str1 = string.Format("[{0}]", str.Substring(0, Math.Min(128, str.Length))); |
| 170 | + return str1; |
| 171 | + } |
| 172 | + |
| 173 | + private string MakeTable(DataTable table, bool markTablesAsLocalTemp) |
| 174 | + { |
| 175 | + StringBuilder stringBuilder = new StringBuilder(); |
| 176 | + string str = this.MakeSafe(string.Concat(markTablesAsLocalTemp ? "#" : string.Empty, this.TablePrefix, table.TableName)); |
| 177 | + string str1 = this.MakeList(table.Columns); |
| 178 | + stringBuilder.AppendFormat("CREATE TABLE {0} ({1});\n", str, str1); |
| 179 | + return stringBuilder.ToString(); |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments