Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #16 from tomaszkubacki/1216d9ec5cc84534a6944093eb1…

…e1db6be7a4cc7

PostgreSQL dialect support
  • Loading branch information...
commit 19e3b6f06e975d021f8529453fb45d1f0a6d7a7d 2 parents 4791007 + 1216d9e
@mythz mythz authored
View
BIN  lib/Mono.Security.dll
Binary file not shown
View
BIN  lib/Npgsql.dll
Binary file not shown
View
152 src/ServiceStack.OrmLite.PostgreSQL/PostgreSQLDialectProvider.cs
@@ -0,0 +1,152 @@
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.Text;
+using Npgsql;
+
+namespace ServiceStack.OrmLite.PostgreSQL
+{
+ public class PostgreSQLDialectProvider : OrmLiteDialectProviderBase
+ {
+ public static PostgreSQLDialectProvider Instance = new PostgreSQLDialectProvider();
+
+
+ private PostgreSQLDialectProvider()
+ {
+ base.AutoIncrementDefinition = "";
+ base.IntColumnDefinition = "integer";
+ base.BoolColumnDefinition = "boolean";
+ base.TimeColumnDefinition = "time";
+ base.DateTimeColumnDefinition = "timestamp";
+ base.DecimalColumnDefinition = "numeric(38,6)";
+ base.GuidColumnDefinition = "uuid";
+ base.DefaultStringLength = 255;
+ base.ParamString = ":";
+ base.BlobColumnDefinition = "bytea";
+ base.RealColumnDefinition = "double precision";
+ base.InitColumnTypeMap();
+ DbTypes<TimeSpan>.Set(DbType.Time, "Interval");
+ DbTypes<TimeSpan?>.Set(DbType.Time, "Interval");
+ }
+
+ public override string GetColumnDefinition(
+ string fieldName,
+ Type fieldType,
+ bool isPrimaryKey,
+ bool autoIncrement,
+ bool isNullable,
+ int? fieldLength,
+ int? scale,
+ string defaultValue)
+ {
+ string fieldDefinition = null;
+
+ if (fieldType == typeof(string))
+ {
+ fieldDefinition = string.Format(StringLengthColumnDefinitionFormat, fieldLength.GetValueOrDefault(DefaultStringLength));
+ }
+ else
+ {
+ if (autoIncrement)
+ {
+ if (fieldType == typeof(long))
+ fieldDefinition = "bigserial";
+ else if (fieldType == typeof(int))
+ fieldDefinition = "serial";
+
+ }
+ else if (!DbTypes.ColumnTypeMap.TryGetValue(fieldType, out fieldDefinition))
+ {
+ fieldDefinition = this.GetUndefinedColumnDefintion(fieldType);
+ }
+ }
+
+ var sql = new StringBuilder();
+ sql.AppendFormat("{0} {1}", GetNameDelimited(fieldName), fieldDefinition);
+
+ if (isPrimaryKey)
+ {
+ sql.Append(" PRIMARY KEY");
+ }
+ else
+ {
+ if (isNullable)
+ {
+ sql.Append(" NULL");
+ }
+ else
+ {
+ sql.Append(" NOT NULL");
+ }
+ }
+
+ if (!string.IsNullOrEmpty(defaultValue))
+ {
+ sql.AppendFormat(DefaultValueFormat, defaultValue);
+ }
+
+ return sql.ToString();
+ }
+
+ public override string EscapeParam(object paramValue)
+ {
+ return paramValue.ToString().Replace("'", @"\'");
+ }
+
+ public override IDbConnection CreateConnection(string connectionString, Dictionary<string, string> options)
+ {
+ return new NpgsqlConnection(connectionString);
+ }
+
+ public override string GetQuotedValue(object value, Type fieldType)
+ {
+ if (value == null) return "NULL";
+
+ if (fieldType == typeof(DateTime))
+ {
+ var dateValue = (DateTime)value;
+ const string iso8601Format = "yyyy-MM-dd HH:mm:ss.fff";
+ return base.GetQuotedValue(dateValue.ToString(iso8601Format), typeof(string));
+ }
+ if (fieldType == typeof(Guid))
+ {
+ var guidValue = (Guid)value;
+ return base.GetQuotedValue(guidValue.ToString("N"), typeof(string));
+ }
+
+ return base.GetQuotedValue(value, fieldType);
+ }
+
+ public override object ConvertDbValue(object value, Type type)
+ {
+ if (value == null || value is DBNull) return null;
+
+ return base.ConvertDbValue(value, type);
+ }
+
+ public override string GetTableNameDelimited(ModelDefinition modelDef)
+ {
+ return string.Format("\"{0}\"", modelDef.ModelName);
+ }
+
+ public override string GetNameDelimited(string columnName)
+ {
+ return string.Format("\"{0}\"", columnName);
+ }
+
+ public override long GetLastInsertId(IDbCommand command)
+ {
+ command.CommandText = "SELECT LASTVAL()";
+ var result = command.ExecuteScalar();
+ if (result is DBNull)
+ return default(long);
+ return Convert.ToInt64(result);
+ }
+
+
+ public override SqlExpressionVisitor<T> ExpressionVisitor<T>()
+ {
+ throw new NotImplementedException();
+ }
+ }
+}
View
36 src/ServiceStack.OrmLite.PostgreSQL/Properties/AssemblyInfo.cs
@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("ServiceStack.OrmLite.PostgreSQL")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("ServiceStack.OrmLite.PostgreSQL")]
+[assembly: AssemblyCopyright("Copyright © Tomasz Kubacki 2012")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible
+// to COM components. If you need to access a type in this assembly from
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("b89c24c2-77c5-843c-f1bf-c33d25af3f16")]
+
+// Version information for an assembly consists of the following four values:
+//
+// Major Version
+// Minor Version
+// Build Number
+// Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
View
67 src/ServiceStack.OrmLite.PostgreSQL/ServiceStack.OrmLite.PostgreSQL.csproj
@@ -0,0 +1,67 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>8.0.30703</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{3220F088-BDD0-6979-AC0C-8C541C2E7DE5}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>ServiceStack.OrmLite.PostgreSQL</RootNamespace>
+ <AssemblyName>ServiceStack.OrmLite.PostgreSQL</AssemblyName>
+ <FileAlignment>512</FileAlignment>
+ <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
+ <TargetFrameworkProfile />
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="Mono.Security">
+ <HintPath>..\..\lib\Mono.Security.dll</HintPath>
+ </Reference>
+ <Reference Include="Npgsql, Version=2.0.11.92, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7, processorArchitecture=MSIL">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\..\lib\Npgsql.dll</HintPath>
+ </Reference>
+ <Reference Include="System" />
+ <Reference Include="System.Core" />
+ <Reference Include="System.Xml.Linq" />
+ <Reference Include="System.Data.DataSetExtensions" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="PostgreSQLDialectProvider.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+ <ItemGroup>
+ <ProjectReference Include="..\ServiceStack.OrmLite\ServiceStack.OrmLite.csproj">
+ <Project>{96179AC6-F6F1-40C3-9FDD-4F6582F54C5C}</Project>
+ <Name>ServiceStack.OrmLite</Name>
+ </ProjectReference>
+ </ItemGroup>
+</Project>
View
6 src/ServiceStack.OrmLite/IOrmLiteDialectProvider.cs
@@ -17,8 +17,10 @@ namespace ServiceStack.OrmLite
{
public interface IOrmLiteDialectProvider
{
- int DefaultStringLength { get; set; }
-
+ int DefaultStringLength { get; set; }
+
+ string ParamString { get; set; }
+
bool UseUnicode { get; set; }
string EscapeParam(object paramValue);
View
11 src/ServiceStack.OrmLite/OrmLiteDialectProviderBase.cs
@@ -143,6 +143,13 @@ public int DefaultStringLength
}
}
+ private string paramString = "@";
+ public string ParamString
+ {
+ get { return paramString; }
+ set { paramString = value; }
+ }
+
private bool useUnicode;
public virtual bool UseUnicode
{
@@ -570,8 +577,8 @@ public virtual string ToCreateTableStatement( Type tableType)
var refModelDef = fieldDef.ReferencesType.GetModelDefinition();
sbConstraints.AppendFormat(
", \n\n CONSTRAINT {0} FOREIGN KEY ({1}) REFERENCES {2} ({3})",
- GetNameDelimited(string.Format("FK_{0}_{1}", modelDef.ModelName,
- refModelDef.ModelName)),
+ GetNameDelimited(string.Format("FK_{0}_{1}_{2}", modelDef.ModelName,
+ refModelDef.ModelName, fieldDef.FieldName)),
GetNameDelimited(fieldDef.FieldName),
GetTableNameDelimited(refModelDef),
GetNameDelimited(refModelDef.PrimaryKey.FieldName));
View
13 src/ServiceStack.OrmLite/OrmLiteReadExtensions.cs
@@ -187,7 +187,7 @@ public static T FirstOrDefault<T>(this IDbCommand dbCmd, string filter)
public static T GetById<T>(this IDbCommand dbCmd, object idValue)
where T : new()
{
- return First<T>(dbCmd, ModelDefinition<T>.PrimaryKeyName + " = {0}".SqlFormat(idValue));
+ return First<T>(dbCmd, OrmLiteConfig.DialectProvider.GetNameDelimited(ModelDefinition<T>.PrimaryKeyName) + " = {0}".SqlFormat(idValue));
}
[ThreadStatic]
@@ -250,7 +250,10 @@ private static string GetFilterSql(IDbCommand dbCmd, ModelDefinition modelDef)
{
sb.Append(i == 0 ? " WHERE " : " AND ");
var p = (IDbDataParameter)dbCmd.Parameters[i];
- sb.AppendLine(p.ParameterName + " = @" + p.ParameterName);
+ sb.AppendFormat("{0} = {1}{2}",
+ OrmLiteConfig.DialectProvider.GetNameDelimited(p.ParameterName),
+ OrmLiteConfig.DialectProvider.ParamString,
+ p.ParameterName);
}
return sb.ToString();
}
@@ -261,7 +264,7 @@ public static T QueryById<T>(this IDbCommand dbCmd, object value)
if (dbCmd.Parameters.Count != 1
|| ((IDbDataParameter)dbCmd.Parameters[0]).ParameterName != ModelDefinition<T>.PrimaryKeyName
|| lastQueryType != typeof(T))
- SetFilter<T>(dbCmd, ModelDefinition<T>.PrimaryKeyName, value);
+ SetFilter<T>(dbCmd, OrmLiteConfig.DialectProvider.GetNameDelimited(ModelDefinition<T>.PrimaryKeyName), value);
((IDbDataParameter)dbCmd.Parameters[0]).Value = value;
@@ -414,7 +417,7 @@ public static IEnumerable<T> EachWhere<T>(this IDbCommand dbCmd, object anonType
public static T GetByIdOrDefault<T>(this IDbCommand dbCmd, object idValue)
where T : new()
{
- return FirstOrDefault<T>(dbCmd, ModelDefinition<T>.PrimaryKeyName + " = {0}".SqlFormat(idValue));
+ return FirstOrDefault<T>(dbCmd, OrmLiteConfig.DialectProvider.GetNameDelimited(ModelDefinition<T>.PrimaryKeyName) + " = {0}".SqlFormat(idValue));
}
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)
var sql = idValues.GetIdsInSql();
return sql == null
? new List<T>()
- : Select<T>(dbCmd, ModelDefinition<T>.PrimaryKeyName + " IN (" + sql + ")");
+ : Select<T>(dbCmd, OrmLiteConfig.DialectProvider.GetNameDelimited(ModelDefinition<T>.PrimaryKeyName) + " IN (" + sql + ")");
}
public static T GetScalar<T>(this IDbCommand dbCmd, string sql, params object[] sqlParams)
Please sign in to comment.
Something went wrong with that request. Please try again.