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

Commit c0bf547

Browse files
committed
Merge pull request #190 from AkosLukacs/master
Small problem with PostgreSQL table generation
2 parents 066c73f + 2c8a597 commit c0bf547

File tree

8 files changed

+328
-145
lines changed

8 files changed

+328
-145
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
4+
using System.Linq;
5+
using System.Text;
6+
using NUnit.Framework;
7+
using ServiceStack.DataAnnotations;
8+
using ServiceStack.OrmLite.Tests;
9+
10+
namespace ServiceStack.OrmLite.PostgreSQL.Tests
11+
{
12+
[TestFixture]
13+
public class CreatePostgreSQLTablesTests : OrmLiteTestBase
14+
{
15+
16+
[Test]
17+
public void can_create_tables_after_UseUnicode_or_DefaultStringLenght_changed()
18+
{
19+
//first one passes
20+
_reCreateTheTable();
21+
22+
//all of these pass now:
23+
OrmLiteConfig.DialectProvider.UseUnicode = true;
24+
_reCreateTheTable();
25+
26+
OrmLiteConfig.DialectProvider.UseUnicode = false;
27+
_reCreateTheTable();
28+
29+
OrmLiteConfig.DialectProvider.DefaultStringLength = 98765;
30+
31+
_reCreateTheTable();
32+
}
33+
34+
private void _reCreateTheTable()
35+
{
36+
using(var db = ConnectionString.OpenDbConnection()) {
37+
db.CreateTable<CreatePostgreSQLTablesTests_dummy_table>(true);
38+
}
39+
}
40+
41+
private class CreatePostgreSQLTablesTests_dummy_table
42+
{
43+
[AutoIncrement]
44+
public int Id { get; set; }
45+
46+
public String StringNoExplicitLength { get; set; }
47+
48+
[StringLength(100)]
49+
public String String100Characters { get; set; }
50+
}
51+
}
52+
}
Lines changed: 113 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,120 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3-
<PropertyGroup>
4-
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5-
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6-
<ProductVersion>8.0.30703</ProductVersion>
7-
<SchemaVersion>2.0</SchemaVersion>
8-
<ProjectGuid>{E692B423-82E9-46DE-AA80-F4E36A4B4D56}</ProjectGuid>
9-
<OutputType>Library</OutputType>
10-
<AppDesignerFolder>Properties</AppDesignerFolder>
11-
<RootNamespace>ServiceStack.OrmLite.PostgreSQL.Tests</RootNamespace>
12-
<AssemblyName>ServiceStack.OrmLite.PostgreSQL.Tests</AssemblyName>
13-
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
14-
<FileAlignment>512</FileAlignment>
15-
</PropertyGroup>
16-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17-
<DebugSymbols>true</DebugSymbols>
18-
<DebugType>full</DebugType>
19-
<Optimize>false</Optimize>
20-
<OutputPath>bin\Debug\</OutputPath>
21-
<DefineConstants>DEBUG;TRACE</DefineConstants>
22-
<ErrorReport>prompt</ErrorReport>
23-
<WarningLevel>4</WarningLevel>
24-
</PropertyGroup>
25-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26-
<DebugType>pdbonly</DebugType>
27-
<Optimize>true</Optimize>
28-
<OutputPath>bin\Release\</OutputPath>
29-
<DefineConstants>TRACE</DefineConstants>
30-
<ErrorReport>prompt</ErrorReport>
31-
<WarningLevel>4</WarningLevel>
32-
</PropertyGroup>
33-
<ItemGroup>
34-
<Reference Include="Northwind.Perf">
35-
<HintPath>..\..\lib\tests\Northwind.Perf.dll</HintPath>
36-
</Reference>
37-
<Reference Include="Npgsql">
38-
<HintPath>..\..\lib\Npgsql.dll</HintPath>
39-
</Reference>
40-
<Reference Include="ServiceStack.Common">
41-
<HintPath>..\..\lib\ServiceStack.Common.dll</HintPath>
42-
</Reference>
43-
<Reference Include="ServiceStack.Common.Tests">
44-
<HintPath>..\..\lib\tests\ServiceStack.Common.Tests.dll</HintPath>
45-
</Reference>
46-
<Reference Include="ServiceStack.Interfaces">
47-
<HintPath>..\..\lib\ServiceStack.Interfaces.dll</HintPath>
48-
</Reference>
49-
<Reference Include="System" />
50-
<Reference Include="System.ComponentModel.DataAnnotations" />
51-
<Reference Include="System.Core" />
52-
<Reference Include="System.Xml.Linq" />
53-
<Reference Include="System.Data.DataSetExtensions" />
54-
<Reference Include="System.Data" />
55-
<Reference Include="System.Xml" />
56-
<Reference Include="Northwind.Common">
57-
<HintPath>..\..\lib\tests\Northwind.Common.dll</HintPath>
58-
</Reference>
59-
<Reference Include="nunit.framework">
60-
<HintPath>..\..\lib\tests\nunit.framework.dll</HintPath>
61-
</Reference>
62-
<Reference Include="ServiceStack.Text">
63-
<HintPath>..\..\lib\ServiceStack.Text.dll</HintPath>
64-
</Reference>
65-
<Reference Include="System.Configuration" />
66-
</ItemGroup>
67-
<ItemGroup>
68-
<Compile Include="EnumTests.cs" />
69-
<Compile Include="Expressions\AdditiveExpressionsTest.cs" />
70-
<Compile Include="Expressions\Author.cs" />
71-
<Compile Include="Expressions\AuthorUseCase.cs" />
72-
<Compile Include="Expressions\ConditionalExpressionTest.cs" />
73-
<Compile Include="Expressions\EqualityExpressionsTest.cs" />
74-
<Compile Include="Expressions\ExpressionsTestBase.cs" />
75-
<Compile Include="Expressions\LogicalExpressionsTest.cs" />
76-
<Compile Include="Expressions\MultiplicativeExpressionsTest.cs" />
77-
<Compile Include="Expressions\OrmLiteCountTests.cs" />
78-
<Compile Include="Expressions\PrimaryExpressionsTest.cs" />
79-
<Compile Include="Expressions\RelationalExpressionsTest.cs" />
80-
<Compile Include="Expressions\StringFunctionTests.cs" />
81-
<Compile Include="Expressions\TestType.cs" />
82-
<Compile Include="Expressions\UnaryExpressionsTest.cs" />
83-
<Compile Include="ForeignKeyAttributeTests.cs" />
84-
<Compile Include="NorthwindPerfTests.cs" />
85-
<Compile Include="OrmLiteBasicPersistenceProviderTests.cs" />
86-
<Compile Include="OrmLiteCreateTableWithNamigStrategyTests.cs" />
87-
<Compile Include="OrmLiteInsertTests.cs" />
88-
<Compile Include="OrmLiteNorthwindTests.cs" />
89-
<Compile Include="OrmLiteSelectTests.cs" />
90-
<Compile Include="OrmLiteTestBase.cs" />
91-
<Compile Include="Properties\AssemblyInfo.cs" />
92-
<Compile Include="OrmLiteGetScalarTests.cs" />
93-
<Compile Include="SchemaTests.cs" />
94-
<Compile Include="TypeWithByteArrayFieldTests.cs" />
95-
</ItemGroup>
96-
<ItemGroup>
97-
<None Include="app.config">
98-
<SubType>Designer</SubType>
99-
</None>
100-
</ItemGroup>
101-
<ItemGroup>
102-
<ProjectReference Include="..\ServiceStack.OrmLite.PostgreSQL\ServiceStack.OrmLite.PostgreSQL.csproj">
103-
<Project>{3220F088-BDD0-6979-AC0C-8C541C2E7DE5}</Project>
104-
<Name>ServiceStack.OrmLite.PostgreSQL</Name>
105-
</ProjectReference>
106-
<ProjectReference Include="..\ServiceStack.OrmLite\ServiceStack.OrmLite.csproj">
107-
<Project>{96179AC6-F6F1-40C3-9FDD-4F6582F54C5C}</Project>
108-
<Name>ServiceStack.OrmLite</Name>
109-
</ProjectReference>
110-
</ItemGroup>
111-
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{E692B423-82E9-46DE-AA80-F4E36A4B4D56}</ProjectGuid>
9+
<OutputType>Library</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>ServiceStack.OrmLite.PostgreSQL.Tests</RootNamespace>
12+
<AssemblyName>ServiceStack.OrmLite.PostgreSQL.Tests</AssemblyName>
13+
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
14+
<FileAlignment>512</FileAlignment>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<DebugType>pdbonly</DebugType>
27+
<Optimize>true</Optimize>
28+
<OutputPath>bin\Release\</OutputPath>
29+
<DefineConstants>TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<ItemGroup>
34+
<Reference Include="Northwind.Perf">
35+
<HintPath>..\..\lib\tests\Northwind.Perf.dll</HintPath>
36+
</Reference>
37+
<Reference Include="Npgsql">
38+
<HintPath>..\..\lib\Npgsql.dll</HintPath>
39+
</Reference>
40+
<Reference Include="ServiceStack.Common">
41+
<HintPath>..\..\lib\ServiceStack.Common.dll</HintPath>
42+
</Reference>
43+
<Reference Include="ServiceStack.Common.Tests">
44+
<HintPath>..\..\lib\tests\ServiceStack.Common.Tests.dll</HintPath>
45+
</Reference>
46+
<Reference Include="ServiceStack.Interfaces">
47+
<HintPath>..\..\lib\ServiceStack.Interfaces.dll</HintPath>
48+
</Reference>
49+
<Reference Include="System" />
50+
<Reference Include="System.ComponentModel.DataAnnotations" />
51+
<Reference Include="System.Core" />
52+
<Reference Include="System.Xml.Linq" />
53+
<Reference Include="System.Data.DataSetExtensions" />
54+
<Reference Include="System.Data" />
55+
<Reference Include="System.Xml" />
56+
<Reference Include="Northwind.Common">
57+
<HintPath>..\..\lib\tests\Northwind.Common.dll</HintPath>
58+
</Reference>
59+
<Reference Include="nunit.framework">
60+
<HintPath>..\..\lib\tests\nunit.framework.dll</HintPath>
61+
</Reference>
62+
<Reference Include="ServiceStack.Text">
63+
<HintPath>..\..\lib\ServiceStack.Text.dll</HintPath>
64+
</Reference>
65+
<Reference Include="System.Configuration" />
66+
</ItemGroup>
67+
<ItemGroup>
68+
<Compile Include="CreatePostgreSQLTablesTests.cs" />
69+
<Compile Include="EnumTests.cs" />
70+
<Compile Include="Expressions\AdditiveExpressionsTest.cs" />
71+
<Compile Include="Expressions\Author.cs" />
72+
<Compile Include="Expressions\AuthorUseCase.cs" />
73+
<Compile Include="Expressions\ConditionalExpressionTest.cs" />
74+
<Compile Include="Expressions\EqualityExpressionsTest.cs" />
75+
<Compile Include="Expressions\ExpressionsTestBase.cs" />
76+
<Compile Include="Expressions\LogicalExpressionsTest.cs" />
77+
<Compile Include="Expressions\MultiplicativeExpressionsTest.cs" />
78+
<Compile Include="Expressions\OrmLiteCountTests.cs" />
79+
<Compile Include="Expressions\PrimaryExpressionsTest.cs" />
80+
<Compile Include="Expressions\RelationalExpressionsTest.cs" />
81+
<Compile Include="Expressions\StringFunctionTests.cs" />
82+
<Compile Include="Expressions\TestType.cs" />
83+
<Compile Include="Expressions\UnaryExpressionsTest.cs" />
84+
<Compile Include="ForeignKeyAttributeTests.cs" />
85+
<Compile Include="NorthwindPerfTests.cs" />
86+
<Compile Include="OrmLiteBasicPersistenceProviderTests.cs" />
87+
<Compile Include="OrmLiteCreateTableWithNamigStrategyTests.cs" />
88+
<Compile Include="OrmLiteInsertTests.cs" />
89+
<Compile Include="OrmLiteNorthwindTests.cs" />
90+
<Compile Include="OrmLiteSelectTests.cs" />
91+
<Compile Include="OrmLiteTestBase.cs" />
92+
<Compile Include="Properties\AssemblyInfo.cs" />
93+
<Compile Include="OrmLiteGetScalarTests.cs" />
94+
<Compile Include="SchemaTests.cs" />
95+
<Compile Include="TypeWithByteArrayFieldTests.cs" />
96+
</ItemGroup>
97+
<ItemGroup>
98+
<None Include="app.config">
99+
<SubType>Designer</SubType>
100+
</None>
101+
</ItemGroup>
102+
<ItemGroup>
103+
<ProjectReference Include="..\ServiceStack.OrmLite.PostgreSQL\ServiceStack.OrmLite.PostgreSQL.csproj">
104+
<Project>{3220F088-BDD0-6979-AC0C-8C541C2E7DE5}</Project>
105+
<Name>ServiceStack.OrmLite.PostgreSQL</Name>
106+
</ProjectReference>
107+
<ProjectReference Include="..\ServiceStack.OrmLite\ServiceStack.OrmLite.csproj">
108+
<Project>{96179AC6-F6F1-40C3-9FDD-4F6582F54C5C}</Project>
109+
<Name>ServiceStack.OrmLite</Name>
110+
</ProjectReference>
111+
</ItemGroup>
112+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
112113
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
113114
Other similar extension points exist, see Microsoft.Common.targets.
114115
<Target Name="BeforeBuild">
115116
</Target>
116117
<Target Name="AfterBuild">
117118
</Target>
118-
-->
119+
-->
119120
</Project>

src/ServiceStack.OrmLite.PostgreSQL/PostgreSQLDialectProvider.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace ServiceStack.OrmLite.PostgreSQL
99
public class PostgreSQLDialectProvider : OrmLiteDialectProviderBase<PostgreSQLDialectProvider>
1010
{
1111
public static PostgreSQLDialectProvider Instance = new PostgreSQLDialectProvider();
12+
const string textColumnDefinition = "text";
1213

1314
private PostgreSQLDialectProvider()
1415
{
@@ -22,7 +23,10 @@ private PostgreSQLDialectProvider()
2223
base.ParamString = ":";
2324
base.BlobColumnDefinition = "bytea";
2425
base.RealColumnDefinition = "double precision";
25-
base.StringLengthColumnDefinitionFormat = "text";
26+
base.StringLengthColumnDefinitionFormat = textColumnDefinition;
27+
//there is no "n"varchar in postgres. All strings are either unicode or non-unicode, inherited from the database.
28+
base.StringLengthUnicodeColumnDefinitionFormat = "character varying({0})";
29+
base.StringLengthNonUnicodeColumnDefinitionFormat = "character varying({0})";
2630
base.InitColumnTypeMap();
2731

2832
DbTypeMap.Set<TimeSpan>(DbType.Time, "Interval");
@@ -44,13 +48,11 @@ public override string GetColumnDefinition(
4448
{
4549
if (fieldLength != null)
4650
{
47-
fieldDefinition = UseUnicode
48-
? string.Format(base.StringLengthUnicodeColumnDefinitionFormat, fieldLength)
49-
: string.Format(base.StringLengthNonUnicodeColumnDefinitionFormat, fieldLength);
51+
fieldDefinition = string.Format(base.StringLengthColumnDefinitionFormat, fieldLength);
5052
}
5153
else
5254
{
53-
fieldDefinition = StringLengthColumnDefinitionFormat;
55+
fieldDefinition = textColumnDefinition;
5456
}
5557
}
5658
else

src/ServiceStack.OrmLite.SqlServer/SqlServerOrmLiteDialectProvider.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,31 @@ public override string GetQuotedValue(object value, Type fieldType)
143143
return base.GetQuotedValue(value, fieldType);
144144

145145

146+
}
147+
148+
protected bool _useDateTime2;
149+
public void UseDatetime2(bool shouldUseDatetime2)
150+
{
151+
_useDateTime2 = shouldUseDatetime2;
152+
DateTimeColumnDefinition = shouldUseDatetime2 ? "datetime2" : "datetime";
153+
InitColumnTypeMap();
154+
}
155+
156+
protected override void AddParameterForFieldToCommand(IDbCommand command, FieldDefinition fieldDef, object objWithProperties)
157+
{
158+
//have to override, because DbTypeMap.Set<T> expects DbType, and SqlDbType is not a DbType...
159+
if(_useDateTime2 && (fieldDef.FieldType == typeof(DateTime) || fieldDef.FieldType == typeof(DateTime?))) {
160+
var sqlCmd = (SqlCommand)command;//should be SqlCommand...
161+
var p = sqlCmd.CreateParameter();
162+
p.ParameterName = string.Format("{0}{1}", ParamString, fieldDef.FieldName);
163+
164+
p.SqlDbType = SqlDbType.DateTime2;
165+
p.Value = GetValueOrDbNull(fieldDef, objWithProperties);
166+
167+
command.Parameters.Add(p);
168+
} else {
169+
base.AddParameterForFieldToCommand(command, fieldDef, objWithProperties);
170+
}
146171
}
147172

148173
public override long GetLastInsertId(IDbCommand dbCmd)

0 commit comments

Comments
 (0)