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

Commit 01bbd35

Browse files
committed
Add support for custom Decimal precision in all RDBMS providers
1 parent 35c66f4 commit 01bbd35

File tree

8 files changed

+85
-24
lines changed

8 files changed

+85
-24
lines changed

src/ServiceStack.OrmLite.MySql/MySqlDialectProvider.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -195,31 +195,34 @@ public override string ToCreateTableStatement(Type tableType)
195195
return sql.ToString();
196196
}
197197

198-
public string GetColumnDefinition(FieldDefinition fieldDefinition)
198+
public string GetColumnDefinition(FieldDefinition fieldDef)
199199
{
200-
if (fieldDefinition.PropertyInfo.FirstAttribute<TextAttribute>() != null)
200+
if (fieldDef.PropertyInfo.FirstAttribute<TextAttribute>() != null)
201201
{
202202
var sql = new StringBuilder();
203-
sql.AppendFormat("{0} {1}", GetQuotedColumnName(fieldDefinition.FieldName), TextColumnDefinition);
204-
sql.Append(fieldDefinition.IsNullable ? " NULL" : " NOT NULL");
203+
sql.AppendFormat("{0} {1}", GetQuotedColumnName(fieldDef.FieldName), TextColumnDefinition);
204+
sql.Append(fieldDef.IsNullable ? " NULL" : " NOT NULL");
205205
return sql.ToString();
206206
}
207207

208208
var ret = base.GetColumnDefinition(
209-
fieldDefinition.FieldName,
210-
fieldDefinition.ColumnType,
211-
fieldDefinition.IsPrimaryKey,
212-
fieldDefinition.AutoIncrement,
213-
fieldDefinition.IsNullable,
214-
fieldDefinition.IsRowVersion,
215-
fieldDefinition.FieldLength,
209+
fieldDef.FieldName,
210+
fieldDef.ColumnType,
211+
fieldDef.IsPrimaryKey,
212+
fieldDef.AutoIncrement,
213+
fieldDef.IsNullable,
214+
fieldDef.IsRowVersion,
215+
fieldDef.FieldLength,
216216
null,
217-
fieldDefinition.DefaultValue,
218-
fieldDefinition.CustomFieldDefinition);
217+
fieldDef.DefaultValue,
218+
fieldDef.CustomFieldDefinition);
219219

220-
if (fieldDefinition.IsRowVersion)
220+
if (fieldDef.IsRowVersion)
221221
return ret + " DEFAULT 1";
222222

223+
if (fieldDef.ColumnType == typeof(Decimal))
224+
return base.ReplaceDecimalColumnDefinition(ret, fieldDef.FieldLength, fieldDef.Scale);
225+
223226
return ret;
224227
}
225228

src/ServiceStack.OrmLite.Oracle/OracleOrmLiteDialectProvider.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,12 @@ public override string GetColumnDefinition(string fieldName, Type fieldType,
774774

775775
sql.Append(isNullable ? " NULL" : " NOT NULL");
776776

777-
return sql.ToString();
777+
var definition = sql.ToString();
778+
779+
if (fieldType == typeof(Decimal))
780+
return base.ReplaceDecimalColumnDefinition(definition, fieldLength, scale);
781+
782+
return definition;
778783
}
779784

780785

src/ServiceStack.OrmLite.PostgreSQL/PostgreSQLDialectProvider.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,12 @@ public override string GetColumnDefinition(
123123
sql.AppendFormat(DefaultValueFormat, defaultValue);
124124
}
125125

126-
return sql.ToString();
126+
var definition = sql.ToString();
127+
128+
if (fieldType == typeof(Decimal))
129+
return base.ReplaceDecimalColumnDefinition(definition, fieldLength, scale);
130+
131+
return definition;
127132
}
128133

129134
//Convert xmin into an integer so it can be used in comparisons

src/ServiceStack.OrmLite.SqlServer/SqlServerOrmLiteDialectProvider.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -344,14 +344,8 @@ public override string GetColumnDefinition(string fieldName, Type fieldType, boo
344344
var definition = base.GetColumnDefinition(fieldName, fieldType, isPrimaryKey, autoIncrement,
345345
isNullable, isRowVersion, fieldLength, scale, defaultValue, customFieldDefinition);
346346

347-
if (fieldType == typeof(Decimal)
348-
&& (fieldLength != DefaultDecimalPrecision || scale != DefaultDecimalScale))
349-
{
350-
string validDecimal = String.Format("DECIMAL({0},{1})",
351-
fieldLength.GetValueOrDefault(DefaultDecimalPrecision),
352-
scale.GetValueOrDefault(DefaultDecimalScale));
353-
definition = definition.Replace(DecimalColumnDefinition, validDecimal);
354-
}
347+
if (fieldType == typeof(Decimal))
348+
return base.ReplaceDecimalColumnDefinition(definition, fieldLength, scale);
355349

356350
return definition;
357351
}

src/ServiceStack.OrmLite.Sqlite/SqliteOrmLiteDialectProviderBase.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ public override string GetColumnDefinition(string fieldName, Type fieldType, boo
272272
if (isRowVersion)
273273
return ret + " DEFAULT 1";
274274

275+
if (fieldType == typeof(Decimal))
276+
return base.ReplaceDecimalColumnDefinition(ret, fieldLength, scale);
277+
275278
return ret;
276279
}
277280
}

src/ServiceStack.OrmLite/OrmLiteDialectProviderBase.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,23 @@ protected virtual string GetUndefinedColumnDefinition(Type fieldType, int? field
340340
: MaxStringColumnDefinition;
341341
}
342342

343+
protected string ReplaceDecimalColumnDefinition(string definition, int? fieldLength, int? scale)
344+
{
345+
if (fieldLength == null && scale == null)
346+
return definition;
347+
348+
if (fieldLength != DefaultDecimalPrecision || scale != DefaultDecimalScale)
349+
{
350+
var customDecimal = string.Format("DECIMAL({0},{1})",
351+
fieldLength.GetValueOrDefault(DefaultDecimalPrecision),
352+
scale.GetValueOrDefault(DefaultDecimalScale));
353+
354+
return definition.Replace(DecimalColumnDefinition, customDecimal);
355+
}
356+
357+
return definition;
358+
}
359+
343360
public virtual string GetColumnDefinition(string fieldName, Type fieldType,
344361
bool isPrimaryKey, bool autoIncrement, bool isNullable, bool isRowVersion,
345362
int? fieldLength, int? scale, string defaultValue, string customFieldDefinition)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using NUnit.Framework;
2+
using ServiceStack.DataAnnotations;
3+
using ServiceStack.Text;
4+
5+
namespace ServiceStack.OrmLite.Tests.Issues
6+
{
7+
public class ModelWithCustomFields
8+
{
9+
public int Id { get; set; }
10+
11+
[DecimalLength(12,2)]
12+
public decimal? Decimal { get; set; }
13+
}
14+
15+
[TestFixture]
16+
public class CustomFieldTests : OrmLiteTestBase
17+
{
18+
[Test]
19+
public void Can_create_custom_Decimal_field()
20+
{
21+
using (var db = OpenDbConnection())
22+
{
23+
db.DropAndCreateTable<ModelWithCustomFields>();
24+
25+
var sql = db.GetLastSql();
26+
27+
sql.Print();
28+
29+
Assert.That(sql, Is.StringContaining("DECIMAL(12,2)"));
30+
}
31+
}
32+
}
33+
}

tests/ServiceStack.OrmLite.Tests/ServiceStack.OrmLite.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
<Compile Include="Expression\SqlExpressionTests.cs" />
130130
<Compile Include="Expression\SelectExpressionTests.cs" />
131131
<Compile Include="Issues\ComplexJoinWithAlias.cs" />
132+
<Compile Include="Issues\CustomFieldTests.cs" />
132133
<Compile Include="Issues\UtcDateTimeIssueTests.cs" />
133134
<Compile Include="Issues\JoinsWithSchemas.cs" />
134135
<Compile Include="Issues\MappingFieldsFromStoredProcedureTests.cs" />

0 commit comments

Comments
 (0)