|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Data; |
| 4 | +using System.Reflection; |
| 5 | +using System.Text; |
| 6 | +using System.Linq; |
| 7 | +using FirebirdSql.Data.FirebirdClient; |
| 8 | +using ServiceStack.DataAnnotations; |
| 9 | +using ServiceStack.OrmLite; |
| 10 | +using ServiceStack.OrmLite.Firebird.Converters; |
| 11 | +using ServiceStack.Text; |
| 12 | + |
| 13 | +namespace ServiceStack.OrmLite.Firebird |
| 14 | +{ |
| 15 | + public class Firebird4OrmLiteDialectProvider : FirebirdOrmLiteDialectProvider |
| 16 | + { |
| 17 | + private readonly bool usesCompactGuid; |
| 18 | + |
| 19 | + public static List<string> RESERVED = new List<string>(new[] { |
| 20 | + "USER","ORDER","PASSWORD", "ACTIVE","LEFT","DOUBLE", "FLOAT", "DECIMAL","STRING", "DATE","DATETIME", "TYPE","TIMESTAMP", |
| 21 | + "INDEX","UNIQUE", "PRIMARY", "KEY", "ALTER", "DROP", "CREATE", "DELETE", "VALUES", "FUNCTION", "INT", "LONG", "CHAR", "VALUE", "TIME" |
| 22 | + }); |
| 23 | + |
| 24 | + public new static Firebird4OrmLiteDialectProvider Instance = new Firebird4OrmLiteDialectProvider(); |
| 25 | + |
| 26 | + public Firebird4OrmLiteDialectProvider() : this(true) { } |
| 27 | + |
| 28 | + public Firebird4OrmLiteDialectProvider(bool compactGuid): base(compactGuid) |
| 29 | + { |
| 30 | + usesCompactGuid = compactGuid; |
| 31 | + |
| 32 | + // FB4 now has identity columns |
| 33 | + base.AutoIncrementDefinition = " GENERATED BY DEFAULT AS IDENTITY "; |
| 34 | + NamingStrategy = new Firebird4NamingStrategy(); |
| 35 | + |
| 36 | + base.RemoveConverter<bool>(); |
| 37 | + |
| 38 | + this.Variables = new Dictionary<string, string> |
| 39 | + { |
| 40 | + { OrmLiteVariables.SystemUtc, "CURRENT_TIMESTAMP" }, |
| 41 | + { OrmLiteVariables.MaxText, "VARCHAR(1000)" }, |
| 42 | + { OrmLiteVariables.MaxTextUnicode, "VARCHAR(2048)" }, |
| 43 | + { OrmLiteVariables.True, SqlBool(true) }, |
| 44 | + { OrmLiteVariables.False, SqlBool(false) }, |
| 45 | + }; |
| 46 | + } |
| 47 | + |
| 48 | + public override string ToCreateTableStatement(Type tableType) |
| 49 | + { |
| 50 | + var sbColumns = StringBuilderCache.Allocate(); |
| 51 | + var sbConstraints = StringBuilderCacheAlt.Allocate(); |
| 52 | + |
| 53 | + var sbPk = new StringBuilder(); |
| 54 | + |
| 55 | + var modelDef = GetModel(tableType); |
| 56 | + foreach (var fieldDef in CreateTableFieldsStrategy(modelDef)) |
| 57 | + { |
| 58 | + if (fieldDef.CustomSelect != null) |
| 59 | + continue; |
| 60 | + |
| 61 | + if (fieldDef.IsPrimaryKey) |
| 62 | + sbPk.AppendFormat(sbPk.Length != 0 ? ",{0}" : "{0}", GetQuotedColumnName(fieldDef.FieldName)); |
| 63 | + |
| 64 | + if (sbColumns.Length != 0) |
| 65 | + sbColumns.Append(", \n "); |
| 66 | + |
| 67 | + var columnDefinition = GetColumnDefinition(fieldDef); |
| 68 | + sbColumns.Append(columnDefinition); |
| 69 | + |
| 70 | + if (fieldDef.ForeignKey == null || OrmLiteConfig.SkipForeignKeys) |
| 71 | + continue; |
| 72 | + |
| 73 | + var refModelDef = GetModel(fieldDef.ForeignKey.ReferenceType); |
| 74 | + |
| 75 | + var fkName = NamingStrategy.ApplyNameRestrictions(fieldDef.ForeignKey.GetForeignKeyName(modelDef, refModelDef, NamingStrategy, fieldDef)).ToLower(); |
| 76 | + sbConstraints.AppendFormat(", \n\n CONSTRAINT {0} FOREIGN KEY ({1}) REFERENCES {2} ({3})", |
| 77 | + GetQuotedName(fkName), |
| 78 | + GetQuotedColumnName(fieldDef.FieldName), |
| 79 | + GetQuotedTableName(refModelDef), |
| 80 | + GetQuotedColumnName(refModelDef.PrimaryKey.FieldName)); |
| 81 | + |
| 82 | + sbConstraints.Append(GetForeignKeyOnDeleteClause(fieldDef.ForeignKey)); |
| 83 | + sbConstraints.Append(GetForeignKeyOnUpdateClause(fieldDef.ForeignKey)); |
| 84 | + } |
| 85 | + |
| 86 | + if (sbPk.Length != 0) |
| 87 | + sbColumns.AppendFormat(", \n PRIMARY KEY({0})", sbPk); |
| 88 | + |
| 89 | + var sql = $"RECREATE TABLE {GetQuotedTableName(modelDef)} \n(\n {StringBuilderCache.ReturnAndFree(sbColumns)}{StringBuilderCacheAlt.ReturnAndFree(sbConstraints)} \n); \n"; |
| 90 | + |
| 91 | + return sql; |
| 92 | + } |
| 93 | + |
| 94 | + public override List<string> ToCreateSequenceStatements(Type tableType) |
| 95 | + { |
| 96 | + var gens = new List<string>(); |
| 97 | + var modelDef = GetModel(tableType); |
| 98 | + |
| 99 | + foreach (var fieldDef in modelDef.FieldDefinitions) |
| 100 | + { |
| 101 | + if (!fieldDef.AutoIncrement || fieldDef.Sequence.IsNullOrEmpty()) continue; |
| 102 | + |
| 103 | + // https://firebirdsql.org/refdocs/langrefupd21-ddl-sequence.html |
| 104 | + var sequence = Sequence(modelDef.ModelName, fieldDef.FieldName, fieldDef.Sequence).ToUpper(); |
| 105 | + gens.Add(GetCreateSequenceSql(sequence)); |
| 106 | + } |
| 107 | + return gens; |
| 108 | + } |
| 109 | + |
| 110 | + protected override void EnsureAutoIncrementSequence(ModelDefinition modelDef, FieldDefinition fieldDef) |
| 111 | + { |
| 112 | + if (fieldDef.AutoIncrement && !string.IsNullOrEmpty(fieldDef.Sequence)) |
| 113 | + { |
| 114 | + fieldDef.Sequence = Sequence(modelDef.ModelName, fieldDef.FieldName, fieldDef.Sequence); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + public override string GetColumnDefinition(FieldDefinition fieldDef) |
| 119 | + { |
| 120 | + var fieldDefinition = ResolveFragment(fieldDef.CustomFieldDefinition) |
| 121 | + ?? GetColumnTypeDefinition(fieldDef.ColumnType, fieldDef.FieldLength, fieldDef.Scale); |
| 122 | + |
| 123 | + var sql = StringBuilderCache.Allocate(); |
| 124 | + sql.AppendFormat("{0} {1}", GetQuotedColumnName(fieldDef.FieldName), fieldDefinition); |
| 125 | + |
| 126 | + var defaultValue = GetDefaultValue(fieldDef); |
| 127 | + if (fieldDef.IsRowVersion) |
| 128 | + { |
| 129 | + sql.AppendFormat(DefaultValueFormat, 1L); |
| 130 | + } |
| 131 | + else if (!string.IsNullOrEmpty(defaultValue)) |
| 132 | + { |
| 133 | + sql.AppendFormat(DefaultValueFormat, defaultValue); |
| 134 | + } |
| 135 | + |
| 136 | + if (fieldDef.AutoIncrement && string.IsNullOrEmpty(fieldDef.Sequence)) |
| 137 | + { |
| 138 | + sql.Append(AutoIncrementDefinition); |
| 139 | + } |
| 140 | + else |
| 141 | + // Identity columns must accept null to generate a new value. |
| 142 | + if (!fieldDef.IsNullable) |
| 143 | + { |
| 144 | + sql.Append(" NOT NULL"); |
| 145 | + } |
| 146 | + if (fieldDef.IsUniqueConstraint) |
| 147 | + { |
| 148 | + sql.Append(" UNIQUE"); |
| 149 | + } |
| 150 | + |
| 151 | + return StringBuilderCacheAlt.ReturnAndFree(sql); |
| 152 | + } |
| 153 | + |
| 154 | + public override void PrepareParameterizedInsertStatement<T>(IDbCommand cmd, ICollection<string> insertFields = null) |
| 155 | + { |
| 156 | + var sbColumnNames = StringBuilderCache.Allocate(); |
| 157 | + var sbColumnValues = StringBuilderCacheAlt.Allocate(); |
| 158 | + var sbReturningColumns = StringBuilderCacheAlt.Allocate(); |
| 159 | + var modelDef = OrmLiteUtils.GetModelDefinition(typeof(T)); |
| 160 | + |
| 161 | + cmd.Parameters.Clear(); |
| 162 | + cmd.CommandTimeout = OrmLiteConfig.CommandTimeout; |
| 163 | + |
| 164 | + var fieldDefs = GetInsertFieldDefinitions(modelDef, insertFields); |
| 165 | + foreach (var fieldDef in fieldDefs) |
| 166 | + { |
| 167 | + if (ShouldReturnOnInsert(modelDef, fieldDef)) |
| 168 | + { |
| 169 | + if (sbReturningColumns.Length > 0) |
| 170 | + sbReturningColumns.Append(","); |
| 171 | + sbReturningColumns.Append(GetQuotedColumnName(fieldDef.FieldName)); |
| 172 | + } |
| 173 | + |
| 174 | + if (ShouldSkipInsert(fieldDef) || (fieldDef.AutoIncrement && string.IsNullOrEmpty(fieldDef.Sequence))) |
| 175 | + continue; |
| 176 | + |
| 177 | + if (sbColumnNames.Length > 0) |
| 178 | + sbColumnNames.Append(","); |
| 179 | + if (sbColumnValues.Length > 0) |
| 180 | + sbColumnValues.Append(","); |
| 181 | + |
| 182 | + try |
| 183 | + { |
| 184 | + sbColumnNames.Append(GetQuotedColumnName(fieldDef.FieldName)); |
| 185 | + |
| 186 | + // in FB4 only use 'next value for' if the fielddef has a sequence explicitly. |
| 187 | + if (fieldDef.AutoIncrement && !string.IsNullOrEmpty(fieldDef.Sequence)) |
| 188 | + { |
| 189 | + EnsureAutoIncrementSequence(modelDef, fieldDef); |
| 190 | + sbColumnValues.Append("NEXT VALUE FOR " + fieldDef.Sequence); |
| 191 | + } |
| 192 | + if (fieldDef.AutoId && usesCompactGuid) |
| 193 | + { |
| 194 | + sbColumnValues.Append("GEN_UUID()"); |
| 195 | + } |
| 196 | + else |
| 197 | + { |
| 198 | + sbColumnValues.Append(this.GetParam(SanitizeFieldNameForParamName(fieldDef.FieldName))); |
| 199 | + AddParameter(cmd, fieldDef); |
| 200 | + } |
| 201 | + } |
| 202 | + catch (Exception ex) |
| 203 | + { |
| 204 | + Log.Error("ERROR in PrepareParameterizedInsertStatement(): " + ex.Message, ex); |
| 205 | + throw; |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + var strReturning = StringBuilderCacheAlt.ReturnAndFree(sbReturningColumns); |
| 210 | + cmd.CommandText = string.Format("INSERT INTO {0} ({1}) VALUES ({2}) {3}", |
| 211 | + GetQuotedTableName(modelDef), |
| 212 | + StringBuilderCache.ReturnAndFree(sbColumnNames), |
| 213 | + StringBuilderCacheAlt.ReturnAndFree(sbColumnValues), |
| 214 | + strReturning.Length > 0 ? "RETURNING " + strReturning : ""); |
| 215 | + } |
| 216 | + } |
| 217 | +} |
| 218 | + |
0 commit comments