Skip to content
This repository has been archived by the owner on Oct 8, 2020. It is now read-only.

Commit

Permalink
Merge branch 'fix-sql2005' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
danielwertheim committed Jun 20, 2012
2 parents 5aacf8e + 744ee3e commit 193a25b
Show file tree
Hide file tree
Showing 20 changed files with 255 additions and 87 deletions.
65 changes: 6 additions & 59 deletions Source/Projects/SisoDb.Sql2005/Resources/Sql2005Statements.resx
Expand Up @@ -155,14 +155,14 @@ alter table [{0}] with nocheck add constraint FK_{0}_{1} foreign key (StructureI
</data>
<data name="CreateDatabase" xml:space="preserve">
<value>create database [{0}];
ALTER DATABASE [{0}] SET COMPATIBILITY_LEVEL = 90;</value>
EXEC sp_dbcmptlevel [{0}], 90;</value>
</data>
<data name="CreateDatesIndexesGuid" xml:space="preserve">
<value>create table [{0}] (
[RowId] [bigint] identity not null constraint [PK_{0}] primary key clustered ([RowId]),
[StructureId] [uniqueidentifier] not null,
[MemberPath] [varchar](250) not null,
[Value] [datetime2] null,
[Value] [datetime] null,
[StringValue] [varchar](33) null);

create nonclustered index [IX_{0}_Q] on [{0}] ([StructureId],[MemberPath],[Value]);
Expand All @@ -174,7 +174,7 @@ alter table [{0}] with nocheck add constraint FK_{0}_{1} foreign key (StructureI
[RowId] [bigint] identity not null constraint [PK_{0}] primary key clustered ([RowId]),
[StructureId] [bigint] not null,
[MemberPath] [varchar](250) not null,
[Value] [datetime2] null,
[Value] [datetime] null,
[StringValue] [varchar](33) null);

create nonclustered index [IX_{0}_Q] on [{0}] ([StructureId],[MemberPath],[Value]);
Expand All @@ -186,7 +186,7 @@ alter table [{0}] with nocheck add constraint FK_{0}_{1} foreign key (StructureI
[RowId] [bigint] identity not null constraint [PK_{0}] primary key clustered ([RowId]),
[StructureId] [nvarchar](16) not null,
[MemberPath] [varchar](250) not null,
[Value] [datetime2] null,
[Value] [datetime] null,
[StringValue] [varchar](33) null);

create nonclustered index [IX_{0}_Q] on [{0}] ([StructureId],[MemberPath],[Value]);
Expand Down Expand Up @@ -438,7 +438,7 @@ delete from [{0}] where StructureId = @id;</value>
</data>
<data name="DeleteByIds" xml:space="preserve">
<value>set nocount on;
delete from [{0}] where StructureId in (select Id from @ids);</value>
delete from [{0}] where StructureId in ({1});</value>
</data>
<data name="DeleteByQuery" xml:space="preserve">
<value>set nocount on;
Expand Down Expand Up @@ -484,7 +484,7 @@ select Json from [{0}] where StructureId =@id;</value>
</data>
<data name="GetJsonByIds" xml:space="preserve">
<value>set nocount on;
select Json from [{0}] where StructureId in (select Id from @ids);</value>
select Json from [{0}] where StructureId in ({1});</value>
</data>
<data name="GetJsonByIdWithLock" xml:space="preserve">
<value>set nocount on;
Expand Down Expand Up @@ -548,59 +548,6 @@ begin
[CurrentId] [bigint] NOT NULL default(0),
constraint [PK_SisoDbIdentities] primary key([EntityName])
);
end</value>
</data>
<data name="Sys_Types_CreateIfNotExists" xml:space="preserve">
<value>use [{0}];
set nocount on;
if(select 1 from sys.table_types where is_table_type = 1 and is_user_defined = 1 and name = 'SisoGuidIds') is null
begin
create type SisoGuidIds as table (Id uniqueidentifier primary key not null);
end

if(select 1 from sys.table_types where is_table_type = 1 and is_user_defined = 1 and name = 'SisoIdentityIds') is null
begin
create type SisoIdentityIds as table (Id bigint primary key not null);
end

if(select 1 from sys.table_types where is_table_type = 1 and is_user_defined = 1 and name = 'SisoStringIds') is null
begin
create type SisoStringIds as table (Id nvarchar(16) primary key not null);
end

if(select 1 from sys.table_types where is_table_type = 1 and is_user_defined = 1 and name = 'SisoIntegers') is null
begin
create type SisoIntegers as table ([Value] bigint null);
end

if(select 1 from sys.table_types where is_table_type = 1 and is_user_defined = 1 and name = 'SisoFractals') is null
begin
create type SisoFractals as table ([Value] real null);
end

if(select 1 from sys.table_types where is_table_type = 1 and is_user_defined = 1 and name = 'SisoDates') is null
begin
create type SisoDates as table ([Value] datetime2 null);
end

if(select 1 from sys.table_types where is_table_type = 1 and is_user_defined = 1 and name = 'SisoBooleans') is null
begin
create type SisoBooleans as table ([Value] bit null);
end

if(select 1 from sys.table_types where is_table_type = 1 and is_user_defined = 1 and name = 'SisoGuids') is null
begin
create type SisoGuids as table ([Value] uniqueidentifier null);
end

if(select 1 from sys.table_types where is_table_type = 1 and is_user_defined = 1 and name = 'SisoStrings') is null
begin
create type SisoStrings as table ([Value] nvarchar(50) null);
end

if(select 1 from sys.table_types where is_table_type = 1 and is_user_defined = 1 and name = 'SisoTexts') is null
begin
create type SisoTexts as table ([Value] nvarchar(50) null);
end</value>
</data>
<data name="TableExists" xml:space="preserve">
Expand Down
4 changes: 4 additions & 0 deletions Source/Projects/SisoDb.Sql2005/SisoDb.Sql2005.csproj
Expand Up @@ -55,13 +55,17 @@
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SisoDb2005Extensions.cs" />
<Compile Include="Sql2005AdoDriver.cs" />
<Compile Include="Sql2005ConnectionInfo.cs" />
<Compile Include="Sql2005Database.cs" />
<Compile Include="Sql2005DbClient.cs" />
<Compile Include="Sql2005DbFactory.cs" />
<Compile Include="Sql2005ProviderFactory.cs" />
<Compile Include="Sql2005QueryGenerator.cs" />
<Compile Include="Sql2005ServerClient.cs" />
<Compile Include="Sql2005Session.cs" />
<Compile Include="Sql2005Statements.cs" />
<Compile Include="Sql2005WhereCriteriaBuilder.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SisoDb.SqlServer\SisoDb.SqlServer.csproj">
Expand Down
43 changes: 43 additions & 0 deletions Source/Projects/SisoDb.Sql2005/Sql2005AdoDriver.cs
@@ -0,0 +1,43 @@
using System.Data;
using System.Data.SqlClient;
using NCore;
using SisoDb.Dac;
using SisoDb.DbSchema;
using SisoDb.SqlServer;

namespace SisoDb.Sql2005
{
public class Sql2005AdoDriver : SqlServerAdoDriver
{
protected override IDbDataParameter OnParameterCreated(IDbDataParameter parameter, IDacParameter dacParameter)
{
var dbParam = (SqlParameter)parameter;
var setSize = false;

if (DbSchemas.Parameters.ShouldBeDateTime(dacParameter))
{
dbParam.DbType = DbType.DateTime;
return dbParam;
}

if (DbSchemas.Parameters.ShouldBeNonUnicodeString(dacParameter))
{
dbParam.SqlDbType = SqlDbType.VarChar;
setSize = true;
}
else if (DbSchemas.Parameters.ShouldBeUnicodeString(dacParameter))
{
dbParam.SqlDbType = SqlDbType.NVarChar;
setSize = true;
}

if (setSize)
{
dbParam.Size = (dacParameter.Value.ToStringOrNull() ?? string.Empty).Length;
return dbParam;
}

return dbParam;
}
}
}
71 changes: 71 additions & 0 deletions Source/Projects/SisoDb.Sql2005/Sql2005DbClient.cs
@@ -0,0 +1,71 @@
using System.Collections.Generic;
using System.Data;
using System.Linq;
using EnsureThat;
using NCore;
using NCore.Collections;
using PineCone.Structures;
using PineCone.Structures.Schemas;
using SisoDb.Dac;
using SisoDb.DbSchema;
using SisoDb.SqlServer;

namespace SisoDb.Sql2005
{
public class Sql2005DbClient : SqlServerDbClient
{
protected override int MaxBatchedIdsSize
{
get { return 32; }
}

public Sql2005DbClient(IAdoDriver driver, ISisoConnectionInfo connectionInfo, IDbConnection connection, IDbTransaction transaction, IConnectionManager connectionManager, ISqlStatements sqlStatements)
: base(driver, connectionInfo, connection, transaction, connectionManager, sqlStatements) {}

public override void DeleteByIds(IEnumerable<IStructureId> ids, IStructureSchema structureSchema)
{
Ensure.That(structureSchema, "structureSchema").IsNotNull();
var sqlFormat = SqlStatements.GetSql("DeleteByIds").Inject(structureSchema.GetStructureTableName(), "{0}");

using (var cmd = CreateCommand(string.Empty))
{
foreach (var batchedIds in ids.Batch<IStructureId, IDacParameter>(MaxBatchedIdsSize, (id, batchCount) => new DacParameter(string.Concat("id", batchCount), id.Value)))
{
cmd.Parameters.Clear();
Driver.AddCommandParametersTo(cmd, batchedIds);

var paramsString = string.Join(",", batchedIds.Select(p => p.Name));
cmd.CommandText = sqlFormat.Inject(paramsString);
cmd.ExecuteNonQuery();
}
}
}

public override IEnumerable<string> GetJsonByIds(IEnumerable<IStructureId> ids, IStructureSchema structureSchema)
{
Ensure.That(structureSchema, "structureSchema").IsNotNull();
var sqlFormat = SqlStatements.GetSql("GetJsonByIds").Inject(structureSchema.GetStructureTableName(), "{0}");

using (var cmd = CreateCommand(string.Empty))
{
foreach (var batchedIds in ids.Batch<IStructureId, IDacParameter>(MaxBatchedIdsSize, (id, batchCount) => new DacParameter(string.Concat("id", batchCount), id.Value)))
{
cmd.Parameters.Clear();
Driver.AddCommandParametersTo(cmd, batchedIds);

var paramsString = string.Join(",", batchedIds.Select(p => p.Name));
cmd.CommandText = sqlFormat.Inject(paramsString);

using (var reader = cmd.ExecuteReader(CommandBehavior.SingleResult | CommandBehavior.SequentialAccess))
{
while (reader.Read())
{
yield return reader.GetString(0);
}
reader.Close();
}
}
}
}
}
}
51 changes: 50 additions & 1 deletion Source/Projects/SisoDb.Sql2005/Sql2005ProviderFactory.cs
@@ -1,4 +1,7 @@
using SisoDb.Querying;
using System.Data;
using SisoDb.Dac;
using SisoDb.Querying;
using SisoDb.Querying.Sql;
using SisoDb.SqlServer;

namespace SisoDb.Sql2005
Expand All @@ -14,9 +17,55 @@ public override StorageProviders ProviderType
get { return StorageProviders.Sql2005; }
}

public override IServerClient GetServerClient(ISisoConnectionInfo connectionInfo)
{
return new Sql2005ServerClient(GetAdoDriver(), connectionInfo, ConnectionManager, SqlStatements);
}

public override ITransactionalDbClient GetTransactionalDbClient(ISisoConnectionInfo connectionInfo)
{
var connection = ConnectionManager.OpenClientDbConnection(connectionInfo);
var transaction = Transactions.ActiveTransactionExists ? null : connection.BeginTransaction(IsolationLevel.ReadCommitted);

return new Sql2005DbClient(
GetAdoDriver(),
connectionInfo,
connection,
transaction,
ConnectionManager,
SqlStatements);
}

public override IDbClient GetNonTransactionalDbClient(ISisoConnectionInfo connectionInfo)
{
IDbConnection connection = null;
if (Transactions.ActiveTransactionExists)
Transactions.SuppressOngoingTransactionWhile(() => connection = ConnectionManager.OpenClientDbConnection(connectionInfo));
else
connection = ConnectionManager.OpenClientDbConnection(connectionInfo);

return new Sql2005DbClient(
GetAdoDriver(),
connectionInfo,
connection,
null,
ConnectionManager,
SqlStatements);
}

public override IAdoDriver GetAdoDriver()
{
return new Sql2005AdoDriver();
}

public override IDbQueryGenerator GetDbQueryGenerator()
{
return new Sql2005QueryGenerator(SqlStatements, GetSqlExpressionBuilder());
}

public override ISqlWhereCriteriaBuilder GetWhereCriteriaBuilder()
{
return new Sql2005WhereCriteriaBuilder();
}
}
}
16 changes: 16 additions & 0 deletions Source/Projects/SisoDb.Sql2005/Sql2005ServerClient.cs
@@ -0,0 +1,16 @@
using System.Data;
using SisoDb.Dac;
using SisoDb.SqlServer;

namespace SisoDb.Sql2005
{
public class Sql2005ServerClient : SqlServerClient
{
public Sql2005ServerClient(IAdoDriver driver, ISisoConnectionInfo connectionInfo, IConnectionManager connectionManager, ISqlStatements sqlStatements)
: base(driver, connectionInfo, connectionManager, sqlStatements) {}

protected override void OnInitializeSysTypes(IDbConnection cn)
{
}
}
}
26 changes: 26 additions & 0 deletions Source/Projects/SisoDb.Sql2005/Sql2005WhereCriteriaBuilder.cs
@@ -0,0 +1,26 @@
using System.Text;
using SisoDb.Dac;
using SisoDb.Querying.Lambdas.Nodes;
using SisoDb.Querying.Sql;

namespace SisoDb.Sql2005
{
public class Sql2005WhereCriteriaBuilder : SqlWhereCriteriaBuilder
{
public override void AddSetOfValues(ArrayValueNode valueNode)
{
var tmp = new StringBuilder();
foreach (var value in valueNode.Value)
{
if(tmp.Length > 0) tmp.Append(",");

var param = new DacParameter(GetNextParameterName(), value);
Params.Add(param);

tmp.Append(param.Name);
}

AddValue(string.Format("({0})", tmp));
}
}
}
3 changes: 2 additions & 1 deletion Source/Projects/SisoDb.SqlCe4/SqlCe4ServerClient.cs
Expand Up @@ -3,10 +3,11 @@
using SisoDb.Core.Io;
using SisoDb.Dac;
using SisoDb.Resources;
using SisoDb.SqlServer;

namespace SisoDb.SqlCe4
{
public class SqlCe4ServerClient : DbServerClient
public class SqlCe4ServerClient : SqlServerClient
{
private readonly SqlCe4ConnectionInfo _connectionInfo;

Expand Down
3 changes: 1 addition & 2 deletions Source/Projects/SisoDb.SqlCe4/SqlCe4WhereCriteriaBuilder.cs
@@ -1,5 +1,4 @@
using System;
using System.Text;
using System.Text;
using SisoDb.Dac;
using SisoDb.Querying.Lambdas.Nodes;
using SisoDb.Querying.Sql;
Expand Down
1 change: 1 addition & 0 deletions Source/Projects/SisoDb.SqlServer/SisoDb.SqlServer.csproj
Expand Up @@ -52,6 +52,7 @@
<Compile Include="..\..\SharedAssemblyInfo.cs">
<Link>Properties\SharedAssemblyInfo.cs</Link>
</Compile>
<Compile Include="SqlServerClient.cs" />
<Compile Include="SqlServerConnectionManager.cs" />
<Compile Include="SqlServerDatabase.cs" />
<Compile Include="SqlServerSession.cs" />
Expand Down

0 comments on commit 193a25b

Please sign in to comment.