Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 71339f5

Browse files
committed
Add Post Drop/Create table support for each Dialect provider and RowVersion Trigger to Sqlite
1 parent 77588ee commit 71339f5

File tree

14 files changed

+146
-11
lines changed

14 files changed

+146
-11
lines changed

src/ServiceStack.OrmLite.Firebird/FirebirdOrmLiteDialectProvider.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ public override string ToCreateTableStatement(Type tableType)
338338
fieldDef.IsPrimaryKey,
339339
fieldDef.AutoIncrement,
340340
fieldDef.IsNullable,
341+
fieldDef.IsRowVersion,
341342
fieldDef.FieldLength,
342343
fieldDef.Scale,
343344
fieldDef.DefaultValue,
@@ -390,7 +391,7 @@ public override List<string> ToCreateSequenceStatements(Type tableType)
390391
}
391392

392393
public override string GetColumnDefinition(string fieldName, Type fieldType,
393-
bool isPrimaryKey, bool autoIncrement, bool isNullable,
394+
bool isPrimaryKey, bool autoIncrement, bool isNullable, bool isRowVersion,
394395
int? fieldLength, int? scale, string defaultValue, string customFieldDefinition)
395396
{
396397
string fieldDefinition;
@@ -767,6 +768,7 @@ public override string ToAddColumnStatement(Type modelType, FieldDefinition fiel
767768
fieldDef.IsPrimaryKey,
768769
fieldDef.AutoIncrement,
769770
fieldDef.IsNullable,
771+
fieldDef.IsRowVersion,
770772
fieldDef.FieldLength,
771773
fieldDef.Scale,
772774
fieldDef.DefaultValue,
@@ -784,6 +786,7 @@ public override string ToAlterColumnStatement(Type modelType, FieldDefinition fi
784786
fieldDef.IsPrimaryKey,
785787
fieldDef.AutoIncrement,
786788
fieldDef.IsNullable,
789+
fieldDef.IsRowVersion,
787790
fieldDef.FieldLength,
788791
fieldDef.Scale,
789792
fieldDef.DefaultValue,

src/ServiceStack.OrmLite.MySql/MySqlDialectProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ public string GetColumnDefinition(FieldDefinition fieldDefinition)
181181
fieldDefinition.IsPrimaryKey,
182182
fieldDefinition.AutoIncrement,
183183
fieldDefinition.IsNullable,
184+
fieldDefinition.IsRowVersion,
184185
fieldDefinition.FieldLength,
185186
null,
186187
fieldDefinition.DefaultValue,

src/ServiceStack.OrmLite.Oracle/OracleOrmLiteDialectProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@ public override string ToCreateTableStatement(Type tableType)
633633
fieldDef.IsPrimaryKey,
634634
fieldDef.AutoIncrement,
635635
fieldDef.IsNullable,
636+
fieldDef.IsRowVersion,
636637
fieldDef.FieldLength,
637638
fieldDef.Scale,
638639
fieldDef.DefaultValue,
@@ -712,7 +713,7 @@ public override List<string> SequenceList(Type tableType)
712713
}
713714

714715
public override string GetColumnDefinition(string fieldName, Type fieldType,
715-
bool isPrimaryKey, bool autoIncrement, bool isNullable,
716+
bool isPrimaryKey, bool autoIncrement, bool isNullable, bool isRowVersion,
716717
int? fieldLength, int? scale, string defaultValue, string customFieldDefinition)
717718
{
718719
string fieldDefinition;

src/ServiceStack.OrmLite.PostgreSQL/PostgreSQLDialectProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public override string GetColumnDefinition(
5757
Type fieldType,
5858
bool isPrimaryKey,
5959
bool autoIncrement,
60-
bool isNullable,
60+
bool isNullable,
61+
bool isRowVersion,
6162
int? fieldLength,
6263
int? scale,
6364
string defaultValue,

src/ServiceStack.OrmLite.SqlServer/SqlServerOrmLiteDialectProvider.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ public override string ToAddColumnStatement(Type modelType, FieldDefinition fiel
280280
fieldDef.IsPrimaryKey,
281281
fieldDef.AutoIncrement,
282282
fieldDef.IsNullable,
283+
fieldDef.IsRowVersion,
283284
fieldDef.FieldLength,
284285
fieldDef.Scale,
285286
fieldDef.DefaultValue,
@@ -297,6 +298,7 @@ public override string ToAlterColumnStatement(Type modelType, FieldDefinition fi
297298
fieldDef.IsPrimaryKey,
298299
fieldDef.AutoIncrement,
299300
fieldDef.IsNullable,
301+
fieldDef.IsRowVersion,
300302
fieldDef.FieldLength,
301303
fieldDef.Scale,
302304
fieldDef.DefaultValue,
@@ -320,10 +322,10 @@ public override string ToChangeColumnNameStatement(Type modelType, FieldDefiniti
320322
}
321323

322324
public override string GetColumnDefinition(string fieldName, Type fieldType, bool isPrimaryKey, bool autoIncrement,
323-
bool isNullable, int? fieldLength, int? scale, string defaultValue, string customFieldDefinition)
325+
bool isNullable, bool isRowVersion, int? fieldLength, int? scale, string defaultValue, string customFieldDefinition)
324326
{
325327
var definition = base.GetColumnDefinition(fieldName, fieldType, isPrimaryKey, autoIncrement,
326-
isNullable, fieldLength, scale, defaultValue, customFieldDefinition);
328+
isNullable, isRowVersion, fieldLength, scale, defaultValue, customFieldDefinition);
327329

328330
if (fieldType == typeof(Decimal) && fieldLength != DefaultDecimalPrecision && scale != DefaultDecimalScale)
329331
{

src/ServiceStack.OrmLite.Sqlite/SqliteOrmLiteDialectProviderBase.cs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,38 @@ public override void OnAfterInitColumnTypeMap()
3434
public static bool UTF8Encoded { get; set; }
3535
public static bool ParseViaFramework { get; set; }
3636

37+
public static string RowVersionTriggerFormat = "{0}RowVersionUpdateTrigger";
38+
39+
public override string ToPostDropTableStatement(ModelDefinition modelDef)
40+
{
41+
if (modelDef.RowVersion != null)
42+
{
43+
var triggerName = RowVersionTriggerFormat.Fmt(modelDef.ModelName);
44+
return "DROP TRIGGER IF EXISTS {0}".Fmt(GetQuotedTableName(triggerName));
45+
}
46+
47+
return null;
48+
}
49+
50+
public override string ToPostCreateTableStatement(ModelDefinition modelDef)
51+
{
52+
if (modelDef.RowVersion != null)
53+
{
54+
var triggerName = RowVersionTriggerFormat.Fmt(modelDef.ModelName);
55+
var triggerBody = "UPDATE {0} SET {1} = OLD.{1} + 1 WHERE {2} = NEW.{2};".Fmt(
56+
modelDef.ModelName,
57+
modelDef.RowVersion.FieldName.SqlColumn(),
58+
modelDef.PrimaryKey.FieldName.SqlColumn());
59+
60+
var sql = "CREATE TRIGGER {0} AFTER UPDATE ON {1} FOR EACH ROW BEGIN {2} END;".Fmt(
61+
triggerName, modelDef.ModelName, triggerBody);
62+
63+
return sql;
64+
}
65+
66+
return null;
67+
}
68+
3769
public static string CreateFullTextCreateTableStatement(object objectWithProperties)
3870
{
3971
var sbColumns = new StringBuilder();
@@ -205,11 +237,11 @@ public override bool DoesTableExist(IDbCommand dbCmd, string tableName)
205237
return result > 0;
206238
}
207239

208-
public override string GetColumnDefinition(string fieldName, Type fieldType, bool isPrimaryKey, bool autoIncrement,
209-
bool isNullable, int? fieldLength, int? scale, string defaultValue, string customFieldDefinition)
240+
public override string GetColumnDefinition(string fieldName, Type fieldType, bool isPrimaryKey, bool autoIncrement,
241+
bool isNullable, bool isRowVersion, int? fieldLength, int? scale, string defaultValue, string customFieldDefinition)
210242
{
211243
// http://www.sqlite.org/lang_createtable.html#rowid
212-
var ret = base.GetColumnDefinition(fieldName, fieldType, isPrimaryKey, autoIncrement, isNullable, fieldLength, scale, defaultValue, customFieldDefinition);
244+
var ret = base.GetColumnDefinition(fieldName, fieldType, isPrimaryKey, autoIncrement, isNullable, isRowVersion, fieldLength, scale, defaultValue, customFieldDefinition);
213245
if (isPrimaryKey)
214246
return ret.Replace(" BIGINT ", " INTEGER ");
215247
return ret;

src/ServiceStack.OrmLite/FieldDefinition.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public Type ColumnType
5050

5151
public bool IsNonClustered { get; set; }
5252

53+
public bool IsRowVersion { get; set; }
54+
5355
public int? FieldLength { get; set; } // Precision for Decimal Type
5456

5557
public int? Scale { get; set; } // for decimal type

src/ServiceStack.OrmLite/IOrmLiteDialectProvider.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,11 @@ public interface IOrmLiteDialectProvider
6161

6262
string GetColumnDefinition(
6363
string fieldName, Type fieldType, bool isPrimaryKey, bool autoIncrement,
64-
bool isNullable, int? fieldLength,
65-
int? scale, string defaultValue,
64+
bool isNullable,
65+
bool isRowVersion,
66+
int? fieldLength,
67+
int? scale,
68+
string defaultValue,
6669
string customFieldDefinition);
6770

6871
long GetLastInsertId(IDbCommand command);
@@ -103,6 +106,8 @@ string ToSelectFromProcedureStatement(object fromObjWithProperties,
103106
string ToExecuteProcedureStatement(object objWithProperties);
104107

105108
string ToCreateTableStatement(Type tableType);
109+
string ToPostCreateTableStatement(ModelDefinition modelDef);
110+
string ToPostDropTableStatement(ModelDefinition modelDef);
106111

107112
List<string> ToCreateIndexStatements(Type tableType);
108113
List<string> ToCreateSequenceStatements(Type tableType);

src/ServiceStack.OrmLite/ModelDefinition.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public bool HasAutoIncrementId
4747
get { return PrimaryKey != null && PrimaryKey.AutoIncrement; }
4848
}
4949

50+
public FieldDefinition RowVersion { get; set; }
51+
5052
public string ModelName
5153
{
5254
get { return this.Alias ?? this.Name; }

src/ServiceStack.OrmLite/OrmLiteConfigExtensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ internal static ModelDefinition GetModelDefinition(this Type modelType)
105105
var isPrimaryKey = (!hasPkAttr && (propertyInfo.Name == OrmLiteConfig.IdField || (!hasIdField && isFirst)))
106106
|| propertyInfo.HasAttributeNamed(typeof(PrimaryKeyAttribute).Name);
107107

108+
var isRowVersion = propertyInfo.Name == "RowVersion"
109+
&& propertyInfo.PropertyType == typeof(ulong);
110+
108111
var isNullableType = IsNullableType(propertyInfo.PropertyType);
109112

110113
var isNullable = (!propertyInfo.PropertyType.IsValueType
@@ -157,6 +160,7 @@ internal static ModelDefinition GetModelDefinition(this Type modelType)
157160
IsUnique = isUnique,
158161
IsClustered = indexAttr != null && indexAttr.Clustered,
159162
IsNonClustered = indexAttr != null && indexAttr.NonClustered,
163+
IsRowVersion = isRowVersion,
160164
FieldLength = stringLengthAttr != null
161165
? stringLengthAttr.MaximumLength
162166
: (int?)null,
@@ -185,6 +189,9 @@ internal static ModelDefinition GetModelDefinition(this Type modelType)
185189
modelDef.IgnoredFieldDefinitions.Add(fieldDefinition);
186190
else
187191
modelDef.FieldDefinitions.Add(fieldDefinition);
192+
193+
if (isRowVersion)
194+
modelDef.RowVersion = fieldDefinition;
188195
}
189196

190197
modelDef.SqlSelectAllFromTable = "SELECT {0} FROM {1} "

0 commit comments

Comments
 (0)