private void ConnectionsCombo_SelectionChanged(object sender, SelectionChangedEventArgs e) { this._tables.Clear(); if (this.ConnectionsCombo.SelectedItem != null) { var conn = (GeneratorConfig.Connection) this.ConnectionsCombo.SelectedItem; try { using (var connection = SqlConnections.New(conn.ConnectionString, conn.ProviderName)) { connection.Open(); foreach (var t in SqlSchemaInfo.GetTableNames(connection)) { _tables.Add(((t.Item1 != null) ? (t.Item1 + ".") : "") + t.Item2); } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } }
private void ConnectionsCombo_SelectionChanged(object sender, SelectionChangedEventArgs e) { this._tables.Clear(); if (this.ConnectionsCombo.SelectedItem != null) { string connStr = (string)this.ConnectionsCombo.SelectedItem; try { using (var connection = CreateConnection(connStr)) { connection.Open(); foreach (var t in SqlSchemaInfo.GetTableNames(connection)) { _tables.Add(((t.Item1 != null && t.Item1 != "dbo") ? (t.Item1 + ".") : "") + t.Item2); } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } }
private static EntityField ToEntityField(SqlSchemaInfo.FieldInfo fieldInfo, int prefixLength) { string flags; if (fieldInfo.IsIdentity) { flags = "Identity"; } else if (fieldInfo.IsPrimaryKey) { flags = "PrimaryKey"; } else if (fieldInfo.DataType == "timestamp" || fieldInfo.DataType == "rowversion") { flags = "Insertable(false), Updatable(false), NotNull"; } else if (!fieldInfo.IsNullable) { flags = "NotNull"; } else { flags = null; } string dataType; var fieldType = SqlSchemaInfo.SqlTypeNameToFieldType(fieldInfo.DataType, fieldInfo.Size, out dataType); dataType = dataType ?? fieldType; return(new EntityField { FieldType = fieldType, DataType = dataType, IsValueType = fieldType != "String" && fieldType != "Stream" && fieldType != "ByteArray", TSType = FieldTypeToTS(fieldType), Ident = GenerateVariableName(fieldInfo.FieldName.Substring(prefixLength)), Title = Inflector.Inflector.Titleize(fieldInfo.FieldName.Substring(prefixLength)), Flags = flags, Name = fieldInfo.FieldName, Size = fieldInfo.Size == 0 ? (Int32?)null : fieldInfo.Size, Scale = fieldInfo.Scale }); }
public static EntityCodeGenerationModel GenerateModel(IDbConnection connection, string tableSchema, string table, string module, string connectionKey, string entityClass, string permission, GeneratorConfig config) { var model = new EntityCodeGenerationModel(); model.Module = module; if (connection.GetDialect() is MySqlDialect) { model.Schema = null; } else { model.Schema = tableSchema; } model.Permission = permission; model.ConnectionKey = connectionKey; model.RootNamespace = config.RootNamespace; var className = entityClass ?? ClassNameFromTableName(table); model.ClassName = className; model.RowClassName = className + "Row"; model.Tablename = table; model.Fields = new List <EntityCodeField>(); model.Joins = new List <EntityCodeJoin>(); model.Instance = true; var fields = SqlSchemaInfo.GetTableFieldInfos(connection, tableSchema, table); var foreigns = SqlSchemaInfo.GetTableSingleFieldForeignKeys(connection, tableSchema, table); var prefix = DeterminePrefixLength(fields, x => x.FieldName); model.FieldPrefix = fields.First().FieldName.Substring(0, prefix); var identity = fields.Find(f => f.IsIdentity == true); if (identity == null) { identity = fields.Find(f => f.IsPrimaryKey == true); } if (identity != null) { model.Identity = GenerateVariableName(identity.FieldName.Substring(prefix)); } else { identity = fields.Find(f => f.IsPrimaryKey == true); if (identity != null) { model.Identity = GenerateVariableName(identity.FieldName.Substring(prefix)); } } string baseRowMatch = null; HashSet <string> baseRowFieldset = null; List <string> baseRowFieldList = new List <string>(); foreach (var k in config.BaseRowClasses ?? new List <GeneratorConfig.BaseRowClass>()) { var b = k.ClassName; var f = new HashSet <string>(StringComparer.OrdinalIgnoreCase); var fl = new List <string>(); bool skip = false; foreach (var s in k.Fields ?? new List <string>()) { string n = s.TrimToNull(); if (n == null || !fields.Exists(z => z.FieldName.Substring(prefix) == n)) { skip = true; break; } f.Add(n); fl.Add(n); } if (skip) { continue; } if (baseRowFieldset == null || f.Count > baseRowFieldset.Count) { baseRowFieldset = f; baseRowFieldList = fl; baseRowMatch = b; } } var removeForeignFields = new HashSet <string>(StringComparer.OrdinalIgnoreCase); foreach (var s in config.RemoveForeignFields ?? new List <string>()) { string n = s.TrimToNull(); if (n != null) { removeForeignFields.Add(n); } } if (baseRowFieldset != null && baseRowFieldset.Count > 0) { model.RowBaseClass = baseRowMatch; model.FieldsBaseClass = baseRowMatch + "Fields"; model.RowBaseFields = new List <EntityCodeField>(); fields.RemoveAll(f => { if (baseRowFieldset.Contains(f.FieldName.Substring(prefix))) { var ft = SqlSchemaInfo.SqlTypeNameToFieldType(f.DataType); model.RowBaseFields.Add(new EntityCodeField { Type = ft, TSType = FieldTypeToTS(ft), Ident = GenerateVariableName(f.FieldName.Substring(prefix)), Title = Inflector.Inflector.Titleize(f.FieldName.Substring(prefix)), Name = f.FieldName, IsValueType = ft != "String" && ft != "Stream", Size = f.Size == 0 ? (Int32?)null : f.Size, Scale = f.Scale }); return(true); } return(false); }); } else { model.RowBaseClass = "Row"; model.RowBaseFields = new List <EntityCodeField>(); model.FieldsBaseClass = "RowFieldsBase"; } foreach (var field in fields) { string flags; if (field.IsIdentity) { flags = "Identity"; } else if (field.IsPrimaryKey) { flags = "PrimaryKey"; } else if (!field.IsNullable) { flags = "NotNull"; } else { flags = null; } var fieldType = SqlSchemaInfo.SqlTypeNameToFieldType(field.DataType); var f = new EntityCodeField() { Type = fieldType, TSType = FieldTypeToTS(fieldType), Ident = GenerateVariableName(field.FieldName.Substring(prefix)), Title = Inflector.Inflector.Titleize(field.FieldName.Substring(prefix)), Name = field.FieldName, Flags = flags, IsValueType = fieldType != "String" && fieldType != "Stream", Size = field.Size == 0 ? (Int32?)null : field.Size, Scale = field.Scale }; if (f.Name == className && fieldType == "String") { model.NameField = f.Name; } var foreign = foreigns.Find((k) => k.FKColumn.Equals(field.FieldName, StringComparison.InvariantCultureIgnoreCase)); if (foreign != null) { if (f.Title.EndsWith(" Id") && f.Title.Length > 3) { f.Title = f.Title.SafeSubstring(0, f.Title.Length - 3); } f.PKSchema = foreign.PKSchema; f.PKTable = foreign.PKTable; f.PKColumn = foreign.PKColumn; var frgfld = SqlSchemaInfo.GetTableFieldInfos(connection, foreign.PKSchema, foreign.PKTable); int frgPrefix = RowGenerator.DeterminePrefixLength(frgfld, z => z.FieldName); var j = new EntityCodeJoin(); j.Fields = new List <EntityCodeField>(); j.Name = GenerateVariableName(f.Name.Substring(prefix)); if (j.Name.EndsWith("Id") || j.Name.EndsWith("ID")) { j.Name = j.Name.Substring(0, j.Name.Length - 2); } f.ForeignJoinAlias = j.Name; j.SourceField = f.Ident; frgfld.RemoveAll(y => removeForeignFields.Contains(y.FieldName)); if (frgfld.Find(y => y.FieldName.Substring(frgPrefix) == "SonGuncelleyenID") != null) { //frgfld.RemoveAll(y => LoggingBaseFields.Contains(y.FieldName.Substring(frgPrefix))); } foreach (var frg in frgfld) { if (frg.FieldName.Equals(foreign.PKColumn, StringComparison.InvariantCultureIgnoreCase)) { continue; } var kType = SqlSchemaInfo.SqlTypeNameToFieldType(frg.DataType); var k = new EntityCodeField() { Type = kType, TSType = FieldTypeToTS(kType), Ident = GenerateVariableName(frg.FieldName.Substring(frgPrefix)), Title = Inflector.Inflector.Titleize(JU(j.Name, frg.FieldName.Substring(frgPrefix))), Name = frg.FieldName, Flags = flags, IsValueType = kType != "String" && kType != "Stream", Size = frg.Size == 0 ? (Int32?)null : frg.Size, Scale = frg.Scale }; if (f.TextualField == null && kType == "String") { f.TextualField = JI(j.Name, k.Ident); } j.Fields.Add(k); } model.Joins.Add(j); } model.Fields.Add(f); } if (model.NameField == null) { var fld = model.Fields.FirstOrDefault(z => z.Type == "String"); if (fld != null) { model.NameField = fld.Ident; } } return(model); }
private void ConnectionsCombo_SelectionChanged(object sender, SelectionChangedEventArgs e) { this._tables.Clear(); GenerateCodeButton.IsEnabled = false; if (this.ConnectionsCombo.SelectedItem != null) { var conn = (GeneratorConfig.Connection) this.ConnectionsCombo.SelectedItem; try { using (var connection = SqlConnections.New(conn.ConnectionString, conn.ProviderName)) { connection.Open(); foreach (var t in SqlSchemaInfo.GetTableNames(connection)) { var table = conn != null?conn.Tables.FirstOrDefault(x => x.Tablename == t.Tablename) : null; var identifier = (table == null || table.Identifier.IsEmptyOrNull()) ? RowGenerator.ClassNameFromTableName(t.Table) : table.Identifier; var permission = table == null ? "Administration:General" : table.PermissionKey; var connectionKey = (table != null && !table.ConnectionKey.IsEmptyOrNull()) ? table.ConnectionKey : conn.Key; var module = (table != null && table.Module != null) ? table.Module : Inflector.Inflector.Capitalize(connectionKey); var tableItem = new TableItem { IsChecked = false, ConnectionKey = conn.Key, Module = module, Identifier = identifier, PermissionKey = permission, FullName = t.Tablename }; _tables.Add(tableItem); tableItem.PropertyChanged += (s, e2) => { var t2 = conn.Tables.FirstOrDefault(x => x.Tablename == tableItem.FullName); if (t2 == null) { t2 = new GeneratorConfig.Table(); t2.Tablename = tableItem.FullName; conn.Tables.Add(t2); } t2.Identifier = tableItem.Identifier; t2.Module = tableItem.Module; t2.ConnectionKey = tableItem.ConnectionKey; t2.PermissionKey = tableItem.PermissionKey; this.config.Save(); GenerateCodeButton.IsEnabled = _tables.Any(x => x.IsChecked); }; } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } }
public static EntityModel GenerateModel(IDbConnection connection, string tableSchema, string table, string module, string connectionKey, string entityClass, string permission, GeneratorConfig config) { var model = new EntityModel(); model.Module = module; if (connection.GetDialect().ServerType.StartsWith("MySql", StringComparison.OrdinalIgnoreCase)) { model.Schema = null; } else { model.Schema = tableSchema; } model.Permission = permission; model.ConnectionKey = connectionKey; model.RootNamespace = config.RootNamespace; var className = entityClass ?? ClassNameFromTableName(table); model.ClassName = className; model.RowClassName = className + "Row"; model.Tablename = table; model.Fields = new List <EntityField>(); model.Joins = new List <EntityJoin>(); model.Instance = true; var fields = SqlSchemaInfo.GetTableFieldInfos(connection, tableSchema, table); var foreigns = SqlSchemaInfo.GetTableSingleFieldForeignKeys(connection, tableSchema, table); var prefix = DeterminePrefixLength(fields, x => x.FieldName); model.FieldPrefix = fields.First().FieldName.Substring(0, prefix); var identity = fields.Find(f => f.IsIdentity == true); if (identity == null) { identity = fields.Find(f => f.IsPrimaryKey == true); } if (identity != null) { model.Identity = GenerateVariableName(identity.FieldName.Substring(prefix)); } else { identity = fields.Find(f => f.IsPrimaryKey == true); if (identity != null) { model.Identity = GenerateVariableName(identity.FieldName.Substring(prefix)); } } string baseRowMatch = null; HashSet <string> baseRowFieldset = null; List <string> baseRowFieldList = new List <string>(); foreach (var k in config.BaseRowClasses ?? new List <GeneratorConfig.BaseRowClass>()) { var b = k.ClassName; var f = new HashSet <string>(StringComparer.OrdinalIgnoreCase); var fl = new List <string>(); bool skip = false; foreach (var s in k.Fields ?? new List <string>()) { string n = s.TrimToNull(); if (n == null || !fields.Exists(z => z.FieldName.Substring(prefix) == n)) { skip = true; break; } f.Add(n); fl.Add(n); } if (skip) { continue; } if (baseRowFieldset == null || f.Count > baseRowFieldset.Count) { baseRowFieldset = f; baseRowFieldList = fl; baseRowMatch = b; } } var removeForeignFields = new HashSet <string>(StringComparer.OrdinalIgnoreCase); foreach (var s in config.RemoveForeignFields ?? new List <string>()) { string n = s.TrimToNull(); if (n != null) { removeForeignFields.Add(n); } } if (baseRowFieldset != null && baseRowFieldset.Count > 0) { model.RowBaseClass = baseRowMatch; model.FieldsBaseClass = baseRowMatch + "Fields"; model.RowBaseFields = new List <EntityField>(); fields.RemoveAll(f => { if (baseRowFieldset.Contains(f.FieldName.Substring(prefix))) { var ef = ToEntityField(f, prefix); ef.Flags = null; model.RowBaseFields.Add(ef); return(true); } return(false); }); } else { model.RowBaseClass = "Row"; model.RowBaseFields = new List <EntityField>(); model.FieldsBaseClass = "RowFieldsBase"; } foreach (var field in fields) { var f = ToEntityField(field, prefix); if (f.Name == className && f.FieldType == "String") { model.NameField = f.Name; } var foreign = foreigns.Find((k) => k.FKColumn.Equals(field.FieldName, StringComparison.OrdinalIgnoreCase)); if (foreign != null) { if (f.Title.EndsWith(" Id") && f.Title.Length > 3) { f.Title = f.Title.SafeSubstring(0, f.Title.Length - 3); } f.PKSchema = foreign.PKSchema; f.PKTable = foreign.PKTable; f.PKColumn = foreign.PKColumn; var frgfld = SqlSchemaInfo.GetTableFieldInfos(connection, foreign.PKSchema, foreign.PKTable); int frgPrefix = RowGenerator.DeterminePrefixLength(frgfld, z => z.FieldName); var j = new EntityJoin(); j.Fields = new List <EntityField>(); j.Name = GenerateVariableName(f.Name.Substring(prefix)); if (j.Name.EndsWith("Id") || j.Name.EndsWith("ID")) { j.Name = j.Name.Substring(0, j.Name.Length - 2); } f.ForeignJoinAlias = j.Name; j.SourceField = f.Ident; frgfld.RemoveAll(y => removeForeignFields.Contains(y.FieldName)); foreach (var frg in frgfld) { if (frg.FieldName.Equals(foreign.PKColumn, StringComparison.OrdinalIgnoreCase)) { continue; } var k = ToEntityField(frg, frgPrefix); k.Flags = null; k.Title = Inflector.Inflector.Titleize(JU(j.Name, frg.FieldName.Substring(frgPrefix))); if (f.TextualField == null && k.FieldType == "String") { f.TextualField = JI(j.Name, k.Ident); } j.Fields.Add(k); } model.Joins.Add(j); } model.Fields.Add(f); } if (model.NameField == null) { var fld = model.Fields.FirstOrDefault(z => z.FieldType == "String"); if (fld != null) { model.NameField = fld.Ident; } } return(model); }