Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #16 from tomaszkubacki/1216d9ec5cc84534a6944093eb1…

…e1db6be7a4cc7

PostgreSQL dialect support
  • Loading branch information...
commit 19e3b6f06e975d021f8529453fb45d1f0a6d7a7d 2 parents 4791007 + 1216d9e
Demis Bellot mythz authored
BIN  lib/Mono.Security.dll
Binary file not shown
BIN  lib/Npgsql.dll
Binary file not shown
152 src/ServiceStack.OrmLite.PostgreSQL/PostgreSQLDialectProvider.cs
... ... @@ -0,0 +1,152 @@
  1 +using System;
  2 +using System.Collections.Generic;
  3 +using System.Data;
  4 +using System.Text;
  5 +using Npgsql;
  6 +
  7 +namespace ServiceStack.OrmLite.PostgreSQL
  8 +{
  9 + public class PostgreSQLDialectProvider : OrmLiteDialectProviderBase
  10 + {
  11 + public static PostgreSQLDialectProvider Instance = new PostgreSQLDialectProvider();
  12 +
  13 +
  14 + private PostgreSQLDialectProvider()
  15 + {
  16 + base.AutoIncrementDefinition = "";
  17 + base.IntColumnDefinition = "integer";
  18 + base.BoolColumnDefinition = "boolean";
  19 + base.TimeColumnDefinition = "time";
  20 + base.DateTimeColumnDefinition = "timestamp";
  21 + base.DecimalColumnDefinition = "numeric(38,6)";
  22 + base.GuidColumnDefinition = "uuid";
  23 + base.DefaultStringLength = 255;
  24 + base.ParamString = ":";
  25 + base.BlobColumnDefinition = "bytea";
  26 + base.RealColumnDefinition = "double precision";
  27 + base.InitColumnTypeMap();
  28 + DbTypes<TimeSpan>.Set(DbType.Time, "Interval");
  29 + DbTypes<TimeSpan?>.Set(DbType.Time, "Interval");
  30 + }
  31 +
  32 + public override string GetColumnDefinition(
  33 + string fieldName,
  34 + Type fieldType,
  35 + bool isPrimaryKey,
  36 + bool autoIncrement,
  37 + bool isNullable,
  38 + int? fieldLength,
  39 + int? scale,
  40 + string defaultValue)
  41 + {
  42 + string fieldDefinition = null;
  43 +
  44 + if (fieldType == typeof(string))
  45 + {
  46 + fieldDefinition = string.Format(StringLengthColumnDefinitionFormat, fieldLength.GetValueOrDefault(DefaultStringLength));
  47 + }
  48 + else
  49 + {
  50 + if (autoIncrement)
  51 + {
  52 + if (fieldType == typeof(long))
  53 + fieldDefinition = "bigserial";
  54 + else if (fieldType == typeof(int))
  55 + fieldDefinition = "serial";
  56 +
  57 + }
  58 + else if (!DbTypes.ColumnTypeMap.TryGetValue(fieldType, out fieldDefinition))
  59 + {
  60 + fieldDefinition = this.GetUndefinedColumnDefintion(fieldType);
  61 + }
  62 + }
  63 +
  64 + var sql = new StringBuilder();
  65 + sql.AppendFormat("{0} {1}", GetNameDelimited(fieldName), fieldDefinition);
  66 +
  67 + if (isPrimaryKey)
  68 + {
  69 + sql.Append(" PRIMARY KEY");
  70 + }
  71 + else
  72 + {
  73 + if (isNullable)
  74 + {
  75 + sql.Append(" NULL");
  76 + }
  77 + else
  78 + {
  79 + sql.Append(" NOT NULL");
  80 + }
  81 + }
  82 +
  83 + if (!string.IsNullOrEmpty(defaultValue))
  84 + {
  85 + sql.AppendFormat(DefaultValueFormat, defaultValue);
  86 + }
  87 +
  88 + return sql.ToString();
  89 + }
  90 +
  91 + public override string EscapeParam(object paramValue)
  92 + {
  93 + return paramValue.ToString().Replace("'", @"\'");
  94 + }
  95 +
  96 + public override IDbConnection CreateConnection(string connectionString, Dictionary<string, string> options)
  97 + {
  98 + return new NpgsqlConnection(connectionString);
  99 + }
  100 +
  101 + public override string GetQuotedValue(object value, Type fieldType)
  102 + {
  103 + if (value == null) return "NULL";
  104 +
  105 + if (fieldType == typeof(DateTime))
  106 + {
  107 + var dateValue = (DateTime)value;
  108 + const string iso8601Format = "yyyy-MM-dd HH:mm:ss.fff";
  109 + return base.GetQuotedValue(dateValue.ToString(iso8601Format), typeof(string));
  110 + }
  111 + if (fieldType == typeof(Guid))
  112 + {
  113 + var guidValue = (Guid)value;
  114 + return base.GetQuotedValue(guidValue.ToString("N"), typeof(string));
  115 + }
  116 +
  117 + return base.GetQuotedValue(value, fieldType);
  118 + }
  119 +
  120 + public override object ConvertDbValue(object value, Type type)
  121 + {
  122 + if (value == null || value is DBNull) return null;
  123 +
  124 + return base.ConvertDbValue(value, type);
  125 + }
  126 +
  127 + public override string GetTableNameDelimited(ModelDefinition modelDef)
  128 + {
  129 + return string.Format("\"{0}\"", modelDef.ModelName);
  130 + }
  131 +
  132 + public override string GetNameDelimited(string columnName)
  133 + {
  134 + return string.Format("\"{0}\"", columnName);
  135 + }
  136 +
  137 + public override long GetLastInsertId(IDbCommand command)
  138 + {
  139 + command.CommandText = "SELECT LASTVAL()";
  140 + var result = command.ExecuteScalar();
  141 + if (result is DBNull)
  142 + return default(long);
  143 + return Convert.ToInt64(result);
  144 + }
  145 +
  146 +
  147 + public override SqlExpressionVisitor<T> ExpressionVisitor<T>()
  148 + {
  149 + throw new NotImplementedException();
  150 + }
  151 + }
  152 +}
36 src/ServiceStack.OrmLite.PostgreSQL/Properties/AssemblyInfo.cs
... ... @@ -0,0 +1,36 @@
  1 +using System.Reflection;
  2 +using System.Runtime.CompilerServices;
  3 +using System.Runtime.InteropServices;
  4 +
  5 +// General Information about an assembly is controlled through the following
  6 +// set of attributes. Change these attribute values to modify the information
  7 +// associated with an assembly.
  8 +[assembly: AssemblyTitle("ServiceStack.OrmLite.PostgreSQL")]
  9 +[assembly: AssemblyDescription("")]
  10 +[assembly: AssemblyConfiguration("")]
  11 +[assembly: AssemblyCompany("")]
  12 +[assembly: AssemblyProduct("ServiceStack.OrmLite.PostgreSQL")]
  13 +[assembly: AssemblyCopyright("Copyright © Tomasz Kubacki 2012")]
  14 +[assembly: AssemblyTrademark("")]
  15 +[assembly: AssemblyCulture("")]
  16 +
  17 +// Setting ComVisible to false makes the types in this assembly not visible
  18 +// to COM components. If you need to access a type in this assembly from
  19 +// COM, set the ComVisible attribute to true on that type.
  20 +[assembly: ComVisible(false)]
  21 +
  22 +// The following GUID is for the ID of the typelib if this project is exposed to COM
  23 +[assembly: Guid("b89c24c2-77c5-843c-f1bf-c33d25af3f16")]
  24 +
  25 +// Version information for an assembly consists of the following four values:
  26 +//
  27 +// Major Version
  28 +// Minor Version
  29 +// Build Number
  30 +// Revision
  31 +//
  32 +// You can specify all the values or you can default the Build and Revision Numbers
  33 +// by using the '*' as shown below:
  34 +// [assembly: AssemblyVersion("1.0.*")]
  35 +[assembly: AssemblyVersion("1.0.0.0")]
  36 +[assembly: AssemblyFileVersion("1.0.0.0")]
67 src/ServiceStack.OrmLite.PostgreSQL/ServiceStack.OrmLite.PostgreSQL.csproj
... ... @@ -0,0 +1,67 @@
  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>{3220F088-BDD0-6979-AC0C-8C541C2E7DE5}</ProjectGuid>
  9 + <OutputType>Library</OutputType>
  10 + <AppDesignerFolder>Properties</AppDesignerFolder>
  11 + <RootNamespace>ServiceStack.OrmLite.PostgreSQL</RootNamespace>
  12 + <AssemblyName>ServiceStack.OrmLite.PostgreSQL</AssemblyName>
  13 + <FileAlignment>512</FileAlignment>
  14 + <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
  15 + <TargetFrameworkProfile />
  16 + </PropertyGroup>
  17 + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
  18 + <DebugSymbols>true</DebugSymbols>
  19 + <DebugType>full</DebugType>
  20 + <Optimize>false</Optimize>
  21 + <OutputPath>bin\Debug\</OutputPath>
  22 + <DefineConstants>DEBUG;TRACE</DefineConstants>
  23 + <ErrorReport>prompt</ErrorReport>
  24 + <WarningLevel>4</WarningLevel>
  25 + </PropertyGroup>
  26 + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
  27 + <DebugType>pdbonly</DebugType>
  28 + <Optimize>true</Optimize>
  29 + <OutputPath>bin\Release\</OutputPath>
  30 + <DefineConstants>TRACE</DefineConstants>
  31 + <ErrorReport>prompt</ErrorReport>
  32 + <WarningLevel>4</WarningLevel>
  33 + </PropertyGroup>
  34 + <ItemGroup>
  35 + <Reference Include="Mono.Security">
  36 + <HintPath>..\..\lib\Mono.Security.dll</HintPath>
  37 + </Reference>
  38 + <Reference Include="Npgsql, Version=2.0.11.92, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7, processorArchitecture=MSIL">
  39 + <SpecificVersion>False</SpecificVersion>
  40 + <HintPath>..\..\lib\Npgsql.dll</HintPath>
  41 + </Reference>
  42 + <Reference Include="System" />
  43 + <Reference Include="System.Core" />
  44 + <Reference Include="System.Xml.Linq" />
  45 + <Reference Include="System.Data.DataSetExtensions" />
  46 + <Reference Include="System.Data" />
  47 + <Reference Include="System.Xml" />
  48 + </ItemGroup>
  49 + <ItemGroup>
  50 + <Compile Include="PostgreSQLDialectProvider.cs" />
  51 + <Compile Include="Properties\AssemblyInfo.cs" />
  52 + </ItemGroup>
  53 + <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  54 + <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
  55 + Other similar extension points exist, see Microsoft.Common.targets.
  56 + <Target Name="BeforeBuild">
  57 + </Target>
  58 + <Target Name="AfterBuild">
  59 + </Target>
  60 + -->
  61 + <ItemGroup>
  62 + <ProjectReference Include="..\ServiceStack.OrmLite\ServiceStack.OrmLite.csproj">
  63 + <Project>{96179AC6-F6F1-40C3-9FDD-4F6582F54C5C}</Project>
  64 + <Name>ServiceStack.OrmLite</Name>
  65 + </ProjectReference>
  66 + </ItemGroup>
  67 +</Project>
6 src/ServiceStack.OrmLite/IOrmLiteDialectProvider.cs
@@ -17,8 +17,10 @@ namespace ServiceStack.OrmLite
17 17 {
18 18 public interface IOrmLiteDialectProvider
19 19 {
20   - int DefaultStringLength { get; set; }
21   -
  20 + int DefaultStringLength { get; set; }
  21 +
  22 + string ParamString { get; set; }
  23 +
22 24 bool UseUnicode { get; set; }
23 25
24 26 string EscapeParam(object paramValue);
11 src/ServiceStack.OrmLite/OrmLiteDialectProviderBase.cs
@@ -143,6 +143,13 @@ public int DefaultStringLength
143 143 }
144 144 }
145 145
  146 + private string paramString = "@";
  147 + public string ParamString
  148 + {
  149 + get { return paramString; }
  150 + set { paramString = value; }
  151 + }
  152 +
146 153 private bool useUnicode;
147 154 public virtual bool UseUnicode
148 155 {
@@ -570,8 +577,8 @@ public virtual string ToCreateTableStatement( Type tableType)
570 577 var refModelDef = fieldDef.ReferencesType.GetModelDefinition();
571 578 sbConstraints.AppendFormat(
572 579 ", \n\n CONSTRAINT {0} FOREIGN KEY ({1}) REFERENCES {2} ({3})",
573   - GetNameDelimited(string.Format("FK_{0}_{1}", modelDef.ModelName,
574   - refModelDef.ModelName)),
  580 + GetNameDelimited(string.Format("FK_{0}_{1}_{2}", modelDef.ModelName,
  581 + refModelDef.ModelName, fieldDef.FieldName)),
575 582 GetNameDelimited(fieldDef.FieldName),
576 583 GetTableNameDelimited(refModelDef),
577 584 GetNameDelimited(refModelDef.PrimaryKey.FieldName));
13 src/ServiceStack.OrmLite/OrmLiteReadExtensions.cs
@@ -187,7 +187,7 @@ public static T FirstOrDefault<T>(this IDbCommand dbCmd, string filter)
187 187 public static T GetById<T>(this IDbCommand dbCmd, object idValue)
188 188 where T : new()
189 189 {
190   - return First<T>(dbCmd, ModelDefinition<T>.PrimaryKeyName + " = {0}".SqlFormat(idValue));
  190 + return First<T>(dbCmd, OrmLiteConfig.DialectProvider.GetNameDelimited(ModelDefinition<T>.PrimaryKeyName) + " = {0}".SqlFormat(idValue));
191 191 }
192 192
193 193 [ThreadStatic]
@@ -250,7 +250,10 @@ private static string GetFilterSql(IDbCommand dbCmd, ModelDefinition modelDef)
250 250 {
251 251 sb.Append(i == 0 ? " WHERE " : " AND ");
252 252 var p = (IDbDataParameter)dbCmd.Parameters[i];
253   - sb.AppendLine(p.ParameterName + " = @" + p.ParameterName);
  253 + sb.AppendFormat("{0} = {1}{2}",
  254 + OrmLiteConfig.DialectProvider.GetNameDelimited(p.ParameterName),
  255 + OrmLiteConfig.DialectProvider.ParamString,
  256 + p.ParameterName);
254 257 }
255 258 return sb.ToString();
256 259 }
@@ -261,7 +264,7 @@ public static T QueryById<T>(this IDbCommand dbCmd, object value)
261 264 if (dbCmd.Parameters.Count != 1
262 265 || ((IDbDataParameter)dbCmd.Parameters[0]).ParameterName != ModelDefinition<T>.PrimaryKeyName
263 266 || lastQueryType != typeof(T))
264   - SetFilter<T>(dbCmd, ModelDefinition<T>.PrimaryKeyName, value);
  267 + SetFilter<T>(dbCmd, OrmLiteConfig.DialectProvider.GetNameDelimited(ModelDefinition<T>.PrimaryKeyName), value);
265 268
266 269 ((IDbDataParameter)dbCmd.Parameters[0]).Value = value;
267 270
@@ -414,7 +417,7 @@ public static IEnumerable<T> EachWhere<T>(this IDbCommand dbCmd, object anonType
414 417 public static T GetByIdOrDefault<T>(this IDbCommand dbCmd, object idValue)
415 418 where T : new()
416 419 {
417   - return FirstOrDefault<T>(dbCmd, ModelDefinition<T>.PrimaryKeyName + " = {0}".SqlFormat(idValue));
  420 + return FirstOrDefault<T>(dbCmd, OrmLiteConfig.DialectProvider.GetNameDelimited(ModelDefinition<T>.PrimaryKeyName) + " = {0}".SqlFormat(idValue));
418 421 }
419 422
420 423 public static List<T> GetByIds<T>(this IDbCommand dbCmd, IEnumerable idValues)
@@ -423,7 +426,7 @@ public static List<T> GetByIds<T>(this IDbCommand dbCmd, IEnumerable idValues)
423 426 var sql = idValues.GetIdsInSql();
424 427 return sql == null
425 428 ? new List<T>()
426   - : Select<T>(dbCmd, ModelDefinition<T>.PrimaryKeyName + " IN (" + sql + ")");
  429 + : Select<T>(dbCmd, OrmLiteConfig.DialectProvider.GetNameDelimited(ModelDefinition<T>.PrimaryKeyName) + " IN (" + sql + ")");
427 430 }
428 431
429 432 public static T GetScalar<T>(this IDbCommand dbCmd, string sql, params object[] sqlParams)

0 comments on commit 19e3b6f

Please sign in to comment.
Something went wrong with that request. Please try again.