Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Precision and Scale for decimal types #208

Closed
wants to merge 1 commit into from

4 participants

angel ignacio colmenares laguado Demis Bellot Thomas Grassauer Darren Sherwood
angel ignacio colmenares laguado
Collaborator

using DecimalLengthAttribute, precision and scale can be set ;

public decimal SomeDecimal {get set; }
will produce:
for mysql: .... SomeDecimal DECIMAL (38,6) NOT NULL;
for the rest: ... SomeDecimal DECIMAL (18,12) NOT NULL;

[DecimalLength(15,2)]
public decimal SomeDecimalWithPrecisionAndScale {get;set; }
will produce: SomeDecimal DECIMAL (15,2) NOT NULL;

Demis Bellot
Owner

ok, as this deals with MySql field precision, it's best to get @brainless83 to review it.

angel ignacio colmenares laguado
Collaborator

OrmLiteDialectProviderBase.GetColumnDefinition was changed, so it affects all dialects!

Thomas Grassauer
Collaborator

I am kind of busy right now, I will take a look at this within the next few days

angel ignacio colmenares laguado
Collaborator

hi,
@mythz , @brainless83
no news about this request?

Darren Sherwood

@brainless83
Example use that I would like to have this attribute for.

public class GeoLocation
{
    [AutoIncrement]
    public long Id { get; set; }
    //[DecimalLength(9, 6)] //Want to use this.
    public decimal Latitude { get; set; }
    //[DecimalLength(9, 6)] //Want to use this.
    public decimal Longitude { get; set; }
}

@angelcolmenares I am not sure what database you mean by 'for the rest' .. for me (SQLSERVER2012) OrmLite makes DECIMAL (38,6) NOT NULL

Demis Bellot
Owner

can't be merged cleanly, cleaning up.

Demis Bellot mythz closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 23, 2013
  1. angel ignacio colmenares laguado
This page is out of date. Refresh to see the latest.
52 src/ServiceStack.OrmLite.MySql.Tests/DecimalPrecisionScaleTest.cs
View
@@ -0,0 +1,52 @@
+using System;
+using NUnit.Framework;
+using ServiceStack.DataAnnotations;
+using ServiceStack.DesignPatterns.Model;
+
+namespace ServiceStack.OrmLite.MySql.Tests
+{
+ [TestFixture]
+ public class DecimalPrecisionScaleTest
+ : OrmLiteTestBase
+ {
+ [Test]
+ public void CanCreateDecimalWithPrecisionAndScale()
+ {
+ var fielDef = ModelDefinition<DecimalTest>.Definition.GetFieldDefinition<DecimalTest> (f => f.SomeDecimalWithPrecisionAndScale);
+ Assert.AreEqual("ALTER TABLE `DecimalTest` ADD COLUMN `SomeDecimalWithPrecisionAndScale` DECIMAL (15,2) NOT NULL;",
+ OrmLiteConfig.DialectProvider.ToAddColumnStatement (typeof(DecimalTest), fielDef));
+ }
+
+ [Test]
+ public void CanCreateDecimal()
+ {
+ var fielDef = ModelDefinition<DecimalTest>.Definition.GetFieldDefinition<DecimalTest> (f => f.SomeDecimal);
+ Assert.AreEqual("ALTER TABLE `DecimalTest` ADD COLUMN `SomeDecimal` DECIMAL (38,6) NOT NULL;",
+ OrmLiteConfig.DialectProvider.ToAddColumnStatement (typeof(DecimalTest), fielDef));
+ }
+
+ public class DecimalTest
+ {
+
+ public int Id
+ {
+ get;
+ set;
+ }
+
+ public decimal SomeDecimal
+ {
+ get;
+ set;
+ }
+
+ [DecimalLength(15,2)]
+ public decimal SomeDecimalWithPrecisionAndScale
+ {
+ get;
+ set;
+ }
+
+ }
+ }
+}
9 src/ServiceStack.OrmLite.MySql.Tests/ServiceStack.OrmLite.MySql.Tests.csproj
View
@@ -13,9 +13,9 @@
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
- <DebugSymbols>true</DebugSymbols>
+ <DebugSymbols>True</DebugSymbols>
<DebugType>full</DebugType>
- <Optimize>false</Optimize>
+ <Optimize>False</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
@@ -23,7 +23,7 @@
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
- <Optimize>true</Optimize>
+ <Optimize>True</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
@@ -58,7 +58,7 @@
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="MySql.Data">
- <HintPath>..\..\lib\mysql.data.dll</HintPath>
+ <HintPath>..\..\lib\MySql.Data.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
@@ -106,6 +106,7 @@
<Compile Include="UseCase\SimpleUseCase.cs" />
<Compile Include="OrmLiteGetScalarTests.cs" />
<Compile Include="OrmLiteCreateTableWithNamingStrategyTests.cs" />
+ <Compile Include="DecimalPrecisionScaleTest.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ServiceStack.OrmLite.MySql\ServiceStack.OrmLite.MySql.csproj">
4 src/ServiceStack.OrmLite.MySql/MySqlDialectProvider.cs
View
@@ -20,11 +20,13 @@ private MySqlDialectProvider()
base.IntColumnDefinition = "int(11)";
base.BoolColumnDefinition = "tinyint(1)";
base.TimeColumnDefinition = "time";
- base.DecimalColumnDefinition = "decimal(38,6)";
+ //base.DecimalColumnDefinition = "decimal(38,6)";
base.GuidColumnDefinition = "char(32)";
base.DefaultStringLength = 255;
base.InitColumnTypeMap();
base.DefaultValueFormat = " DEFAULT '{0}'";
+ DefaultDecimalPrecision=38;
+ DefaultDecimalScale=6;
}
public override string GetQuotedParam(string paramValue)
8 src/ServiceStack.OrmLite/OrmLiteDialectProviderBase.cs
View
@@ -370,7 +370,13 @@ protected virtual string GetUndefinedColumnDefinition(Type fieldType, int? field
{
fieldDefinition = string.Format(StringLengthColumnDefinitionFormat, fieldLength.GetValueOrDefault(DefaultStringLength));
}
- else
+ else if( fieldType==typeof(decimal) )
+ {
+ fieldDefinition= string.Format("{0} ({1},{2})", DecimalColumnDefinition,
+ fieldLength.GetValueOrDefault(DefaultDecimalPrecision),
+ scale.GetValueOrDefault(DefaultDecimalScale) );
+ }
+ else
{
if (!DbTypeMap.ColumnTypeMap.TryGetValue(fieldType, out fieldDefinition))
{
Something went wrong with that request. Please try again.