private string GetTableName(ModelDefinition modelDef) { return(modelDef.IsInSchema ? NamingStrategy.ApplyNameRestrictions(modelDef.Schema + "_" + NamingStrategy.GetTableName(modelDef.ModelName)) : NamingStrategy.GetTableName(modelDef.ModelName)); }
public override string ToCreateTableStatement(Type tableType) { var sbColumns = StringBuilderCache.Allocate(); var sbConstraints = StringBuilderCacheAlt.Allocate(); var sbPk = new StringBuilder(); var modelDef = GetModel(tableType); foreach (var fieldDef in CreateTableFieldsStrategy(modelDef)) { if (fieldDef.CustomSelect != null) { continue; } if (fieldDef.IsPrimaryKey) { sbPk.AppendFormat(sbPk.Length != 0 ? ",{0}" : "{0}", GetQuotedColumnName(fieldDef.FieldName)); } if (sbColumns.Length != 0) { sbColumns.Append(", \n "); } var columnDefinition = GetColumnDefinition(fieldDef); sbColumns.Append(columnDefinition); if (fieldDef.ForeignKey == null || OrmLiteConfig.SkipForeignKeys) { continue; } var refModelDef = GetModel(fieldDef.ForeignKey.ReferenceType); var fkName = NamingStrategy.ApplyNameRestrictions(fieldDef.ForeignKey.GetForeignKeyName(modelDef, refModelDef, NamingStrategy, fieldDef)).ToLower(); sbConstraints.AppendFormat(", \n\n CONSTRAINT {0} FOREIGN KEY ({1}) REFERENCES {2} ({3})", GetQuotedName(fkName), GetQuotedColumnName(fieldDef.FieldName), GetQuotedTableName(refModelDef), GetQuotedColumnName(refModelDef.PrimaryKey.FieldName)); sbConstraints.Append(GetForeignKeyOnDeleteClause(fieldDef.ForeignKey)); sbConstraints.Append(GetForeignKeyOnUpdateClause(fieldDef.ForeignKey)); } if (sbPk.Length != 0) { sbColumns.AppendFormat(", \n PRIMARY KEY({0})", sbPk); } var sql = string.Format( "CREATE TABLE {0} \n(\n {1}{2} \n); \n", GetQuotedTableName(modelDef), StringBuilderCache.ReturnAndFree(sbColumns), StringBuilderCacheAlt.ReturnAndFree(sbConstraints)); return(sql); }
private string Sequence(string modelName, string fieldName, string sequence) { //TODO used to return Quote(sequence) if (!sequence.IsNullOrEmpty()) { return(sequence); } var seqName = NamingStrategy.ApplyNameRestrictions(modelName + "_" + fieldName + "_GEN"); return(seqName); }
public override string ToPostCreateTableStatement(ModelDefinition modelDef) { if (modelDef.RowVersion != null) { var triggerName = NamingStrategy.ApplyNameRestrictions( RowVersionTriggerFormat.Fmt(modelDef.ModelName)); var triggerBody = ":NEW.{0} := :OLD.{0}+1;".Fmt( modelDef.RowVersion.FieldName.SqlColumn()); var sql = "CREATE TRIGGER {0} BEFORE UPDATE ON {1} FOR EACH ROW BEGIN {2} END;".Fmt( Quote(triggerName), modelDef.ModelName, triggerBody); return(sql); } return(null); }
public override string ToPostCreateTableStatement(ModelDefinition modelDef) { if (modelDef.RowVersion != null) { var triggerName = NamingStrategy.ApplyNameRestrictions( RowVersionTriggerFormat.Fmt(modelDef.ModelName)); var triggerBody = "new.{0} = old.{0}+1;".Fmt( modelDef.RowVersion.FieldName.SqlColumn(this)); var sql = "CREATE OR ALTER TRIGGER {0} BEFORE UPDATE ON {1} AS BEGIN {2} END;".Fmt( Quote(triggerName), GetTableName(modelDef.ModelName, modelDef.Schema), triggerBody); return(sql); } return(null); }
public override List <string> ToCreateIndexStatements(Type tableType) { var sqlIndexes = new List <string>(); var modelDef = GetModel(tableType); foreach (var fieldDef in modelDef.FieldDefinitions) { if (!fieldDef.IsIndexed) { continue; } var indexName = GetIndexName( fieldDef.IsUnique, (modelDef.IsInSchema ? modelDef.Schema + "_" + modelDef.ModelName : modelDef.ModelName).SafeVarName(), fieldDef.FieldName); indexName = NamingStrategy.ApplyNameRestrictions(indexName); sqlIndexes.Add( ToCreateIndexStatement(fieldDef.IsUnique, indexName, modelDef, fieldDef.FieldName)); } foreach (var compositeIndex in modelDef.CompositeIndexes) { var indexName = GetCompositeIndexNameWithSchema(compositeIndex, modelDef); indexName = NamingStrategy.ApplyNameRestrictions(indexName); var indexNames = string.Join(",", compositeIndex.FieldNames.ToArray()); sqlIndexes.Add( ToCreateIndexStatement(compositeIndex.Unique, indexName, modelDef, indexNames)); } return(sqlIndexes); }
protected override string GetIndexName(bool isUnique, string modelName, string fieldName) { return(NamingStrategy.ApplyNameRestrictions( string.Format("{0}idx_{1}_{2}", isUnique ? "u" : "", modelName, fieldName).ToLower())); }
public override string ToCreateTableStatement(Type tableType) { var sbColumns = new StringBuilder(); var sbConstraints = new StringBuilder(); var sbPk = new StringBuilder(); var modelDef = GetModel(tableType); foreach (var fieldDef in modelDef.FieldDefinitions) { if (fieldDef.IsPrimaryKey) { sbPk.AppendFormat(sbPk.Length != 0 ? ",{0}" : "{0}", GetQuotedColumnName(fieldDef.FieldName)); } if (sbColumns.Length != 0) { sbColumns.Append(", \n "); } var columnDefinition = GetColumnDefinition( fieldDef.FieldName, fieldDef.ColumnType, fieldDef.IsPrimaryKey, fieldDef.AutoIncrement, fieldDef.IsNullable, fieldDef.IsRowVersion, fieldDef.FieldLength, fieldDef.Scale, GetDefaultValue(fieldDef), fieldDef.CustomFieldDefinition); sbColumns.Append(columnDefinition); if (fieldDef.ForeignKey == null) { continue; } var refModelDef = GetModel(fieldDef.ForeignKey.ReferenceType); var fkName = NamingStrategy.ApplyNameRestrictions(fieldDef.ForeignKey.GetForeignKeyName(modelDef, refModelDef, NamingStrategy, fieldDef)).ToLower(); sbConstraints.AppendFormat(", \n\n CONSTRAINT {0} FOREIGN KEY ({1}) REFERENCES {2} ({3})", GetQuotedName(fkName), GetQuotedColumnName(fieldDef.FieldName), GetQuotedTableName(refModelDef), GetQuotedColumnName(refModelDef.PrimaryKey.FieldName)); sbConstraints.Append(GetForeignKeyOnDeleteClause(fieldDef.ForeignKey)); sbConstraints.Append(GetForeignKeyOnUpdateClause(fieldDef.ForeignKey)); } if (sbPk.Length != 0) { sbColumns.AppendFormat(", \n PRIMARY KEY({0})", sbPk); } var sql = new StringBuilder(string.Format( "CREATE TABLE {0} \n(\n {1}{2} \n); \n", GetQuotedTableName(modelDef), sbColumns, sbConstraints)); return(sql.ToString()); }
protected override string GetIndexName(bool isUnique, string modelName, string fieldName) { return(NamingStrategy.ApplyNameRestrictions( $"{(isUnique ? "u" : "")}idx_{modelName}_{fieldName}".ToLower())); }