Created
February 16, 2012 08:10
-
-
Save christiansparre/1843193 to your computer and use it in GitHub Desktop.
Sql Server Compact dialect provider for OrmLite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Collections.Generic; | |
using System.Data.SqlServerCe; | |
using ServiceStack.OrmLite.SqlServer; | |
namespace Slyngelstat.OrmLite.SqlCe | |
{ | |
public class SqlCeOrmLiteDialectProvider : SqlServerOrmLiteDialectProvider | |
{ | |
public SqlCeOrmLiteDialectProvider() | |
{ | |
// Sql Ce only supports NVARCHAR's with a length of up to 4000 (VARCHAR not supported) | |
// "GetColumnDefinition" will change this to NTEXT when creating a table with a string column larger that 4000 | |
base.DefaultStringLength = 4000; | |
base.StringColumnDefinition = "NVARCHAR(4000)"; | |
base.StringLengthColumnDefinitionFormat = "NVARCHAR({0})"; | |
base.StringLengthNonUnicodeColumnDefinitionFormat = "NVARCHAR({0})"; | |
base.StringLengthUnicodeColumnDefinitionFormat = "NVARCHAR({0})"; | |
// Sql Ce only supports blob with a length up to 8000 | |
// "GetColumnDefinition" will change this to IMAGE when creating a table with a blob column larger that 8000 | |
base.BlobColumnDefinition = "VARBINARY(8000)"; | |
base.InitColumnTypeMap(); | |
} | |
public override System.Data.IDbConnection CreateConnection(string connectionString, Dictionary<string, string> options) | |
{ | |
if (options != null) | |
{ | |
SqlCeConnectionStringBuilder builder = new SqlCeConnectionStringBuilder(connectionString); | |
foreach (var option in options) | |
{ | |
if (option.Key.ToLower() == "read only") | |
{ | |
if (option.Value.ToLower() == "true") | |
{ | |
builder.FileMode = "Read Only"; | |
// Sql Ce requires a temp path when in read only mode. | |
// But OrmLite can not provide the temp path so we need to roll our own, | |
// setting it to the path where the db file is | |
builder.TempFilePath = new FileInfo(builder.DataSource).Directory.FullName; | |
} | |
} | |
else | |
{ | |
builder.Add(option.Key, option.Value); | |
} | |
} | |
return new SqlCeConnection(builder.ConnectionString); | |
} | |
return new SqlCeConnection(connectionString); | |
} | |
public override string GetColumnDefinition(string fieldName, Type fieldType, bool isPrimaryKey, bool autoIncrement, bool isNullable, int? fieldLength, int? scale, string defaultValue) | |
{ | |
string defaultColumnDefinition = base.GetColumnDefinition(fieldName, fieldType, isPrimaryKey, autoIncrement, isNullable, fieldLength, scale, defaultValue); | |
if (fieldLength.HasValue) | |
{ | |
if (fieldType == typeof(string) && fieldLength > 4000) | |
{ | |
// NVARCHAR greater than 4000 is not supported in Sql Ce so use NTEXT | |
return defaultColumnDefinition.Replace(string.Format(base.StringLengthColumnDefinitionFormat, fieldLength), "NTEXT"); | |
} | |
if (fieldType == typeof(byte[]) && fieldLength > 8000) | |
{ | |
// VARBINARY greater than 8000 is not supported in Sql Ce so use IMAGE | |
return defaultColumnDefinition.Replace("VARBINARY(8000)", "IMAGE"); | |
} | |
} | |
return defaultColumnDefinition; | |
} | |
public override long GetLastInsertId(System.Data.IDbCommand dbCmd) | |
{ | |
// Sql Ce does not support SCOPE_IDENTITY(); | |
dbCmd.CommandText = "SELECT @@IDENTITY"; | |
object result = dbCmd.ExecuteScalar(); | |
if (result is DBNull) | |
{ | |
return 0L; | |
} | |
if (result is decimal) | |
{ | |
return Convert.ToInt64((decimal)result); | |
} | |
return (long)result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment