Skip to content

Commit

Permalink
Fixing: no sequence support before SQL 2012
Browse files Browse the repository at this point in the history
  • Loading branch information
jesuslpm committed Jan 13, 2015
1 parent 0f4fed7 commit 6666e07
Show file tree
Hide file tree
Showing 41 changed files with 146 additions and 60 deletions.
Binary file modified EntityLite.v12.suo
Binary file not shown.
24 changes: 20 additions & 4 deletions Samples/EntityLite.ttinclude/DataLayerGeneration.ttinclude
Original file line number Diff line number Diff line change
Expand Up @@ -603,12 +603,28 @@ FROM
if (commandText != null)
{
using (var cn = OpenConnection())
using (var cmd = cn.CreateCommand())
{
cmd.CommandText = commandText;
using (var reader = cmd.ExecuteReader())
var areSequencesSupported = true;
int version = 0;
if (ProviderName == "System.Data.SqlClient") {
version = int.Parse(cn.ServerVersion.Split('.')[0]);
if (version < 11) areSequencesSupported = false;
}

if (areSequencesSupported)
{
using (var cmd = cn.CreateCommand())
{
cmd.CommandText = commandText;
using (var reader = cmd.ExecuteReader())
{
Sequences = reader.ToEnumerable<Sequence>().ToDictionary(x => x.FullSequenceName, StringComparer.InvariantCultureIgnoreCase);
}
}
}
else
{
Sequences = reader.ToEnumerable<Sequence>().ToDictionary(x => x.FullSequenceName, StringComparer.InvariantCultureIgnoreCase);
Sequences = new Dictionary<string, Sequence>();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Samples/EntityLite.ttinclude/EntityLite.ttinclude
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<#@ assembly name="Microsoft.CSharp" #>
<#@ assembly name="System.Data" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="$(SolutionDir)packages\EntityLite.Core.1.7.0\lib\net35-client\inercya.EntityLite.dll" #>
<#@ assembly name="$(SolutionDir)packages\EntityLite.Core.1.8.2\lib\net35-client\inercya.EntityLite.dll" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="System.Data" #>
Expand Down
Binary file modified Samples/Northwind.mdf
Binary file not shown.
Binary file modified Samples/Northwind_log.ldf
Binary file not shown.
10 changes: 4 additions & 6 deletions Samples/Samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
<Reference Include="Common.Logging.Core">
<HintPath>..\packages\Common.Logging.Core.2.2.0\lib\net40\Common.Logging.Core.dll</HintPath>
</Reference>
<Reference Include="inercya.EntityLite, Version=1.8.1.0, Culture=neutral, PublicKeyToken=8ba7eec58c815de9, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\EntityLite.Core.1.8.2\lib\net35-client\inercya.EntityLite.dll</HintPath>
</Reference>
<Reference Include="Microsoft.SqlServer.Types, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.SqlServer.Types.10.50.1600.1\lib\Net20\Microsoft.SqlServer.Types.dll</HintPath>
Expand Down Expand Up @@ -214,12 +218,6 @@
<Install>true</Install>
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\inercya.EntityLite\inercya.EntityLite.csproj">
<Project>{b74df500-8654-40d1-a50c-7262a2095b1e}</Project>
<Name>inercya.EntityLite</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>REM COPY "$(ProjectDir)EntityLite.ttinclude\*.ttinclude" "$(SolutionDir)Nuget\Generators\Content\EntityLite.ttinclude\" /Y</PostBuildEvent>
Expand Down
6 changes: 6 additions & 0 deletions Samples/Samples.csproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectView>ShowAllFiles</ProjectView>
</PropertyGroup>
</Project>
4 changes: 2 additions & 2 deletions Samples/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<packages>
<package id="Common.Logging" version="2.2.0" targetFramework="net40" />
<package id="Common.Logging.Core" version="2.2.0" targetFramework="net40" />
<package id="EntityLite" version="1.7.0" targetFramework="net40" />
<package id="EntityLite.Core" version="1.7.0" targetFramework="net40" />
<package id="EntityLite" version="1.8.2" targetFramework="net40" />
<package id="EntityLite.Core" version="1.8.2" targetFramework="net40" />
<package id="Microsoft.SqlServer.Types" version="10.50.1600.1" targetFramework="net40" />
<package id="MySql.Data" version="6.7.4" targetFramework="net40" />
<package id="NLog" version="2.1.0" targetFramework="net40" />
Expand Down
4 changes: 2 additions & 2 deletions inercya.EntityLite/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ limitations under the License.
// Build Number
// Revision
//
[assembly: AssemblyVersion("1.8.0.0")]
[assembly: AssemblyFileVersion("1.8.0.0")]
[assembly: AssemblyVersion("1.8.2.0")]
[assembly: AssemblyFileVersion("1.8.2.0")]
18 changes: 3 additions & 15 deletions inercya.EntityLite/DataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -446,12 +446,6 @@ protected internal virtual bool Delete(object entity)
return affectedRecords > 0;
}

private bool SetAuditDate(string fieldName, object entity, out object previousValue)
{
Type entityType = entity.GetType();
return SetAuditDate(fieldName, entity, EntityMetadata.GetEntityMetadata(entityType), out previousValue);
}

private bool SetAuditDate(string fieldName, object entity, EntityMetadata metadata, out object previousValue)
{
if (entity == null) throw new ArgumentNullException("entity");
Expand Down Expand Up @@ -489,12 +483,6 @@ private bool SetAuditDate(string fieldName, object entity, EntityMetadata metada
return false;
}

private bool SetAuditUser(string fieldName, object entity, out object previousValue)
{
Type entityType = entity.GetType();
return SetAuditUser(fieldName, entity, EntityMetadata.GetEntityMetadata(entityType), out previousValue);
}

private bool SetAuditUser(string fieldName, object entity, EntityMetadata entityMetadata, out object previousValue)
{
if (entity == null) throw new ArgumentNullException("entity");
Expand All @@ -503,7 +491,7 @@ private bool SetAuditUser(string fieldName, object entity, EntityMetadata entity
PropertyGetter getter;
if (getters.TryGetValue(fieldName, out getter))
{
previousValue = getter(fieldName);
previousValue = getter(entity);
}
else
{
Expand Down Expand Up @@ -645,11 +633,11 @@ protected internal virtual int Update(object entity, List<string> sortedFields)

if (IsAutomaticAuditUserFieldsEnabled)
{
isModifiedBySet = SetAuditUser(this.SpecialFieldNames.ModifiedByFieldName, entity, out previousModifiedBy);
isModifiedBySet = SetAuditUser(this.SpecialFieldNames.ModifiedByFieldName, entity, metadata, out previousModifiedBy);
}
if (IsAutomaticAuditDateFieldsEnabled)
{
isModifiedDateSet = SetAuditDate(this.SpecialFieldNames.ModifiedDateFieldName, entity, out previousModifiedDate);
isModifiedDateSet = SetAuditDate(this.SpecialFieldNames.ModifiedDateFieldName, entity, metadata, out previousModifiedDate);
}
var cmd = new CommandExecutor(this, false)
{
Expand Down
Binary file removed nuget/Core/EntityLite.Core.1.7.1.nupkg
Binary file not shown.
Binary file removed nuget/Core/EntityLite.Core.1.8.0.nupkg
Binary file not shown.
Binary file added nuget/Core/EntityLite.Core.1.8.2.nupkg
Binary file not shown.
2 changes: 1 addition & 1 deletion nuget/Core/EntityLite.Core.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>EntityLite.Core</id>
<version>1.8.0</version>
<version>1.8.2</version>
<title>i-nercya EntityLite Core</title>
<authors>Jesús López</authors>
<owners>i-nercya intelligent software</owners>
Expand Down
2 changes: 1 addition & 1 deletion nuget/Core/Publish.cmd
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
%~dp0..\NuGet.exe Update -self
%~dp0..\NuGet.exe Push EntityLite.Core.1.8.0.nupkg
%~dp0..\NuGet.exe Push EntityLite.Core.1.8.2.nupkg
pause
Binary file modified nuget/Core/lib/net35-client/inercya.EntityLite.dll
Binary file not shown.
Binary file removed nuget/Generators/EntityLite.1.7.1.nupkg
Binary file not shown.
Binary file not shown.
4 changes: 2 additions & 2 deletions nuget/Generators/EntityLite.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>EntityLite</id>
<version>1.8.0</version>
<version>1.8.2</version>
<title>i-nercya EntityLite</title>
<authors>Jesús López</authors>
<owners>i-nercya intelligent software</owners>
Expand All @@ -24,7 +24,7 @@
<copyright>Copyright 2013</copyright>
<dependencies>
<group targetFramework="net35-client">
<dependency id="EntityLite.Core" version="[1.8.0]" />
<dependency id="EntityLite.Core" version="[1.8.2]" />
</group>
</dependencies>
<references></references>
Expand Down
2 changes: 1 addition & 1 deletion nuget/Generators/Publish.cmd
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
%~dp0..\NuGet.exe Update -self
%~dp0..\NuGet.exe Push EntityLite.1.8.0.nupkg
%~dp0..\NuGet.exe Push EntityLite.1.8.2.nupkg
pause
Original file line number Diff line number Diff line change
Expand Up @@ -603,12 +603,28 @@ FROM
if (commandText != null)
{
using (var cn = OpenConnection())
using (var cmd = cn.CreateCommand())
{
cmd.CommandText = commandText;
using (var reader = cmd.ExecuteReader())
var areSequencesSupported = true;
int version = 0;
if (ProviderName == "System.Data.SqlClient") {
version = int.Parse(cn.ServerVersion.Split('.')[0]);
if (version < 11) areSequencesSupported = false;
}

if (areSequencesSupported)
{
using (var cmd = cn.CreateCommand())
{
cmd.CommandText = commandText;
using (var reader = cmd.ExecuteReader())
{
Sequences = reader.ToEnumerable<Sequence>().ToDictionary(x => x.FullSequenceName, StringComparer.InvariantCultureIgnoreCase);
}
}
}
else
{
Sequences = reader.ToEnumerable<Sequence>().ToDictionary(x => x.FullSequenceName, StringComparer.InvariantCultureIgnoreCase);
Sequences = new Dictionary<string, Sequence>();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<#@ assembly name="Microsoft.CSharp" #>
<#@ assembly name="System.Data" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="$(SolutionDir)packages\EntityLite.Core.1.8.0\lib\net35-client\inercya.EntityLite.dll" #>
<#@ assembly name="$(SolutionDir)packages\EntityLite.Core.1.8.2\lib\net35-client\inercya.EntityLite.dll" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="System.Data" #>
Expand Down
Binary file removed packages/EntityLite.1.7.0/EntityLite.1.7.0.nupkg
Binary file not shown.
Binary file added packages/EntityLite.1.8.2/EntityLite.1.8.2.nupkg
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
var generation = new DataLayerGeneration
{
ProviderName = "System.Data.SqlClient",
// Please, change the connection string
// Please, change the connection string. This is the connection string used at design time
// to generate de data layer
ConnectionString = @"Data Source=(local);Integrated Security=SSPI;Initial Catalog=Northwind",
DefaultSchema = "dbo",
RootNamespace = "Samples.Entities",
DataServiceName = "NorhtwindDataService",
// you should include a connection string with this name in the configuration file
// this is the connection that will be used at runtime
ConnectionStringName= "Northwind",
EntitySettings = new List<EntitySetting>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ public class DataLayerGeneration : IDisposable
return cn;
}

private DbCommandBuilder _commandBuilder;
public DbCommandBuilder CommandBuilder
{
get
{
if (_commandBuilder == null)
{
_commandBuilder = Factory.CreateCommandBuilder();
}
return _commandBuilder;
}
}


public static Type GetTypeFromDbType(DbType dbType)
{
Expand Down Expand Up @@ -340,12 +353,28 @@ public class DataLayerGeneration : IDisposable
{
var pkfieldMetadata = primaryKey[0];
string schemaName = GetSchemaName(entity);
string fullSequenceName = (string.IsNullOrEmpty(schemaName) ? pkfieldMetadata.BaseColumnName : schemaName + "." + pkfieldMetadata.BaseColumnName) + "_SEQ";
string fullSequenceName = null;
if (!string.IsNullOrEmpty(entity.SequenceName))
{
fullSequenceName = string.IsNullOrEmpty(schemaName) ? entity.SequenceName : schemaName + "." + entity.SequenceName;
}
else if (ProviderName == "Oracle.DataAccess.Client")
{
fullSequenceName = (string.IsNullOrEmpty(schemaName) ? pkfieldMetadata.BaseColumnName : schemaName + "." + pkfieldMetadata.BaseColumnName) + "_SEQ";
}
else
{
fullSequenceName = (string.IsNullOrEmpty(schemaName) ? pkfieldMetadata.BaseTableName + "_" + pkfieldMetadata.BaseColumnName : schemaName + "." + pkfieldMetadata.BaseTableName + "_" + pkfieldMetadata.BaseColumnName) + "_SEQ";
}
Sequence seq = null;
if (Sequences.TryGetValue(fullSequenceName, out seq))
{
pkfieldMetadata.SequenceName = seq.SequenceName;
}
else if (!string.IsNullOrEmpty(entity.SequenceName))
{
throw new ArgumentException("The sequence " + fullSequenceName + " doesn't exist in the database");
}
}
}
}
Expand Down Expand Up @@ -406,14 +435,15 @@ public class DataLayerGeneration : IDisposable
}
}


private DataTable GetSchemaUnion(EntitySetting entitySetting, DbConnection cn)
{
DataTable schemaUnion = null;

DataTable baseTableSchema = null;
if (!string.IsNullOrEmpty(entitySetting.BaseTableName))
{
baseTableSchema = GetSchema(entitySetting.GetFullTableName(DefaultSchema), cn);
baseTableSchema = GetSchema(entitySetting.GetFullTableName(DefaultSchema, CommandBuilder.QuotePrefix, CommandBuilder.QuoteSuffix), cn);
if ((entitySetting.FieldGeneration & FieldGeneration.ForBaseTable) == FieldGeneration.ForBaseTable)
{
schemaUnion = baseTableSchema.Copy();
Expand Down Expand Up @@ -501,10 +531,10 @@ public class DataLayerGeneration : IDisposable
public List<FieldMetadata> GetPrimaryKey(EntitySetting entity)
{

var fullTableName = entity.GetFullTableName(DefaultSchema);
var fullTableName = entity.GetFullTableName(DefaultSchema, CommandBuilder.QuotePrefix , CommandBuilder.QuoteSuffix);
return GetFieldsMetadata(entity).Where(x => x.IsKey
&& ! string.IsNullOrEmpty(entity.BaseTableName)
&& string.Equals(fullTableName, x.FullTableName, StringComparison.InvariantCultureIgnoreCase) ).ToList();
&& string.Equals(fullTableName, x.FullTableName(CommandBuilder.QuotePrefix , CommandBuilder.QuoteSuffix), StringComparison.InvariantCultureIgnoreCase) ).ToList();

}

Expand Down Expand Up @@ -556,22 +586,53 @@ public class DataLayerGeneration : IDisposable

private void LoadSequences()
{
string commandText = null;
if (ProviderName == "Oracle.DataAccess.Client")
{
commandText = "SELECT SEQUENCE_OWNER AS SEQUENCE_SCHEMA, SEQUENCE_NAME FROM ALL_SEQUENCES";
}
else if (ProviderName == "System.Data.SqlClient")
{
commandText = @"
SELECT
sh.name AS SEQUENCE_SCHEMA, seq.name AS SEQUENCE_NAME
FROM
sys.sequences seq
INNER JOIN sys.schemas sh on seq.schema_id = sh.schema_id";
}
if (commandText != null)
{
using (var cn = OpenConnection())
using (var cmd = cn.CreateCommand())
{
cmd.CommandText = "SELECT SEQUENCE_OWNER AS SEQUENCE_SCHEMA, SEQUENCE_NAME FROM ALL_SEQUENCES";
using (var reader = cmd.ExecuteReader())
var areSequencesSupported = true;
int version = 0;
if (ProviderName == "System.Data.SqlClient") {
version = int.Parse(cn.ServerVersion.Split('.')[0]);
if (version < 11) areSequencesSupported = false;
}

if (areSequencesSupported)
{
Sequences = reader.ToEnumerable<Sequence>().ToDictionary(x => x.FullSequenceName, StringComparer.InvariantCultureIgnoreCase);
using (var cmd = cn.CreateCommand())
{
cmd.CommandText = commandText;
using (var reader = cmd.ExecuteReader())
{
Sequences = reader.ToEnumerable<Sequence>().ToDictionary(x => x.FullSequenceName, StringComparer.InvariantCultureIgnoreCase);
}
}
}
else
{
Sequences = new Dictionary<string, Sequence>();
}
}
}
else
else
{
Sequences = new Dictionary<string, Sequence>();
}

}

private Dictionary<string, Sequence> Sequences;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<#@ assembly name="Microsoft.CSharp" #>
<#@ assembly name="System.Data" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="$(SolutionDir)packages\EntityLite.Core.1.7.0\lib\net35-client\inercya.EntityLite.dll" #>
<#@ assembly name="$(SolutionDir)packages\EntityLite.Core.1.8.2\lib\net35-client\inercya.EntityLite.dll" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="System.Data" #>
Expand Down
Loading

0 comments on commit 6666e07

Please sign in to comment.