Skip to content
Browse files

Added KeyGenerator info into MapMemberInfo

Finished implementation of RetrieveKey parameter
Tests updated
Better arhitecture
  • Loading branch information...
1 parent fc0acda commit a59f274e044979a9a10c7c0a6a84d200cc0d2da8 @lvaleriu committed May 11, 2012
View
2 Demo/Partial.Trust/Asp.Net/Partial.Trust.Asp.Net.csproj
@@ -129,7 +129,7 @@
<WebProjectProperties>
<UseIIS>False</UseIIS>
<AutoAssignPort>True</AutoAssignPort>
- <DevelopmentServerPort>41836</DevelopmentServerPort>
+ <DevelopmentServerPort>2132</DevelopmentServerPort>
<DevelopmentServerVPath>/</DevelopmentServerVPath>
<IISUrl>
</IISUrl>
View
2 Demo/WebServices/NorthwindDataService/NorthwindDataService.csproj
@@ -116,7 +116,7 @@
<WebProjectProperties>
<UseIIS>False</UseIIS>
<AutoAssignPort>True</AutoAssignPort>
- <DevelopmentServerPort>41846</DevelopmentServerPort>
+ <DevelopmentServerPort>2142</DevelopmentServerPort>
<DevelopmentServerVPath>/</DevelopmentServerVPath>
<IISUrl>
</IISUrl>
View
2 Demo/WebServices/Server/Server.csproj
@@ -132,7 +132,7 @@
<WebProjectProperties>
<UseIIS>False</UseIIS>
<AutoAssignPort>True</AutoAssignPort>
- <DevelopmentServerPort>41841</DevelopmentServerPort>
+ <DevelopmentServerPort>2137</DevelopmentServerPort>
<DevelopmentServerVPath>/</DevelopmentServerVPath>
<IISUrl>
</IISUrl>
View
1 Source/BLToolkit.4.csproj
@@ -203,6 +203,7 @@
<Compile Include="DataAccess\KeyGeneratorAttribute.cs" />
<Compile Include="DataAccess\ParamTypeNameAttribute.cs" />
<Compile Include="DataAccess\SequenceAttribute.cs" />
+ <Compile Include="DataAccess\SequenceKeyGenerator.cs" />
<Compile Include="DataAccess\SqlIgnoreAttribute.cs" />
<None Include="Data\DataProvider\GenericDataProvider.cs" />
<Compile Include="Data\DataProvider\FirebirdMappingSchema.cs" />
View
1 Source/BLToolkit.CP.4.csproj
@@ -199,6 +199,7 @@
<Compile Include="DataAccess\ParamTypeNameAttribute.cs" />
<Compile Include="DataAccess\PrimaryKeyGeneratorType.cs" />
<Compile Include="DataAccess\SequenceAttribute.cs" />
+ <Compile Include="DataAccess\SequenceKeyGenerator.cs" />
<Compile Include="DataAccess\SqlIgnoreAttribute.cs" />
<Compile Include="Data\DataProvider\FirebirdMappingSchema.cs" />
<Compile Include="Data\DataProvider\Sql2008DataProvider.cs" />
View
1 Source/BLToolkit.Data.4.csproj
@@ -260,6 +260,7 @@
<Compile Include="DataAccess\ParamTypeNameAttribute.cs" />
<Compile Include="DataAccess\PrimaryKeyGeneratorType.cs" />
<Compile Include="DataAccess\SequenceAttribute.cs" />
+ <Compile Include="DataAccess\SequenceKeyGenerator.cs" />
<Compile Include="DataAccess\SqlIgnoreAttribute.cs" />
<Compile Include="Data\DataProvider\FirebirdMappingSchema.cs" />
<Compile Include="Data\DataProvider\Sql2008DataProvider.cs" />
View
1 Source/BLToolkit.SL.4.csproj
@@ -113,6 +113,7 @@
<Compile Include="DataAccess\PrimaryKeyAttribute.cs" />
<Compile Include="DataAccess\PrimaryKeyGeneratorType.cs" />
<Compile Include="DataAccess\SequenceAttribute.cs" />
+ <Compile Include="DataAccess\SequenceKeyGenerator.cs" />
<Compile Include="DataAccess\SqlIgnoreAttribute.cs" />
<Compile Include="DataAccess\TableNameAttribute.cs" />
<Compile Include="Data\DataProvider\ConvertType.cs" />
View
6 Source/Data/DataProvider/DataProviderBase.cs
@@ -223,6 +223,12 @@ public virtual string GetSequenceQuery(string sequenceName, string schema)
return null;
}
+
+ public virtual string NextSequenceQuery(string sequenceName, string schema)
+ {
+ return null;
+ }
+
public virtual void SetParameterValue(IDbDataParameter parameter, object value)
{
if (value is System.Data.Linq.Binary)
View
8 Source/Data/DataProvider/OdpDataProvider.cs
@@ -559,6 +559,14 @@ public override string GetSequenceQuery(string sequenceName, string schema)
return string.Format("SELECT {0}.NEXTVAL FROM DUAL", sequenceName);
}
+ public override string NextSequenceQuery(string sequenceName, string schema)
+ {
+ if (!string.IsNullOrWhiteSpace(schema))
+ return string.Format("{0}.{1}.NEXTVAL", schema, sequenceName);
+
+ return string.Format("{0}.NEXTVAL", sequenceName);
+ }
+
public override IDbDataParameter CreateParameterObject(IDbCommand command)
{
var parameter = base.CreateParameterObject(command);
View
33 Source/DataAccess/SequenceKeyGenerator.cs
@@ -0,0 +1,33 @@
+namespace BLToolkit.DataAccess
+{
+ public class SequenceKeyGenerator : KeyGenerator
+ {
+ private readonly string _sequence;
+
+ public SequenceKeyGenerator(string sequence, bool retrievePkValue)
+ : base(retrievePkValue)
+ {
+ _sequence = sequence;
+ }
+
+ public string Sequence
+ {
+ get { return _sequence; }
+ }
+ }
+
+ public abstract class KeyGenerator
+ {
+ private readonly bool _retrievePkValue;
+
+ protected KeyGenerator(bool retrievePkValue)
+ {
+ _retrievePkValue = retrievePkValue;
+ }
+
+ public bool RetrievePkValue
+ {
+ get { return _retrievePkValue; }
+ }
+ }
+}
View
56 Source/DataAccess/SqlQueryBase.cs
@@ -236,14 +236,6 @@ protected SqlQueryInfo CreateInsertSqlText(DbManager db, Type type, int nParamet
bool isSet;
var nonUpdatableAttribute = mp.GetNonUpdatableAttribute(type, typeExt, mm.MapMemberInfo.MemberAccessor, out isSet);
-
- bool isGeneratorSet;
- mp.GetGeneratorType(typeExt, mm.MapMemberInfo.MemberAccessor, out isGeneratorSet);
- if (isGeneratorSet && (nonUpdatableAttribute != null && isSet))
- {
- throw new Exception("Cannont set primary key when NonUpdateable attribute is present!");
- }
-
if (nonUpdatableAttribute == null || !isSet || nonUpdatableAttribute.OnInsert == false)
{
sb.AppendFormat("\t{0},\n",
@@ -256,20 +248,29 @@ protected SqlQueryInfo CreateInsertSqlText(DbManager db, Type type, int nParamet
sb.Append(") VALUES (\n");
- foreach (var mm in list)
- {
- var p = query.AddParameter(
- db.DataProvider.Convert(mm.Name + "_P", ConvertType.NameToQueryParameter).ToString(),
- mm.Name);
-
- if (nParameter < 0)
- sb.AppendFormat("\t{0},\n", p.ParameterName);
- //sb.AppendFormat("\t{0},\n", db.DataProvider.Convert(p.ParameterName, ConvertType.NameToQueryParameter));
- else
- sb.AppendFormat("\t{{{0}}},\n", nParameter++);
- }
+ foreach (var mm in list)
+ {
+ var keyGenerator = mm.MapMemberInfo.KeyGenerator as SequenceKeyGenerator;
+ if (keyGenerator != null && !keyGenerator.RetrievePkValue)
+ {
+ string seqQuery = db.DataProvider.NextSequenceQuery(keyGenerator.Sequence, GetOwnerName(type));
+ sb.AppendFormat("\t{0},\n", seqQuery);
+ }
+ else
+ {
+ var p = query.AddParameter(
+ db.DataProvider.Convert(mm.Name + "_P", ConvertType.NameToQueryParameter).ToString(),
+ mm.Name);
+
+ if (nParameter < 0)
+ sb.AppendFormat("\t{0},\n", p.ParameterName);
+ //sb.AppendFormat("\t{0},\n", db.DataProvider.Convert(p.ParameterName, ConvertType.NameToQueryParameter));
+ else
+ sb.AppendFormat("\t{{{0}}},\n", nParameter++);
+ }
+ }
- sb.Remove(sb.Length - 2, 1);
+ sb.Remove(sb.Length - 2, 1);
sb.Append(")");
@@ -382,11 +383,6 @@ public virtual SqlQueryInfo GetSqlQueryInfo(DbManager db, Type type, string acti
{
query = CreateSqlText(db, type, actionName);
query.OwnerName = GetOwnerName(type);
- QueryType queryType;
- if (Enum.TryParse(actionName, out queryType))
- {
- query.QueryType = queryType;
- }
_actionSqlQueryInfo[key] = query;
}
@@ -395,12 +391,4 @@ public virtual SqlQueryInfo GetSqlQueryInfo(DbManager db, Type type, string acti
#endregion
}
-
- public enum QueryType
- {
- None,
- SelectByKey,
- SelectAll,
- Insert,
- }
}
View
49 Source/DataAccess/SqlQueryInfo.cs
@@ -20,7 +20,6 @@ public SqlQueryInfo(ObjectMapper objectMapper)
ObjectMapper = objectMapper;
}
- public QueryType QueryType { get; set; }
public string OwnerName { get; set; }
public string QueryText { get; set; }
public ObjectMapper ObjectMapper { get; private set; }
@@ -73,12 +72,8 @@ public IDbDataParameter[] GetParameters(DbManager db, object obj)
//parameters[i] = db.Parameter(info.ParameterName, info.MemberMapper.GetValue(obj));
var mmi = info.MemberMapper.MapMemberInfo;
-
var val = info.MemberMapper.GetValue(obj);
- var mp = info.MemberMapper.MappingSchema.MetadataProvider;
- var type = info.MemberMapper.Type;
- var typeExt = TypeExtension.GetTypeExtension(type, new ExtensionList());
-
+
if (val == null && mmi.Nullable/* && mmi.NullValue == null*/)
{
//replace value with DbNull
@@ -96,41 +91,13 @@ public IDbDataParameter[] GetParameters(DbManager db, object obj)
parameters[i] = db.Parameter(info.ParameterName, val);
}
- bool isPkSet;
-
- mp.GetPrimaryKeyOrder(type, typeExt, mmi.MemberAccessor, out isPkSet);
- if (isPkSet)
- {
- bool isGeneratorSet;
- KeyGeneratorInfo genInfo = mp.GetGeneratorType(typeExt, mmi.MemberAccessor, out isGeneratorSet);
- if (isGeneratorSet)
- {
- if (genInfo.GeneratorType == PrimaryKeyGeneratorType.Sequence)
- {
- bool isSeqSet;
- string sequenceName = mp.GetSequenceName(typeExt, mmi.MemberAccessor, out isSeqSet);
- if (!isSeqSet)
- throw new Exception("Sequence atribute is not present!");
- if (string.IsNullOrWhiteSpace(sequenceName))
- throw new Exception("SequenceName is empty");
-
- if (!genInfo.RetrievePkValue)
- {
- throw new Exception("Not implemented");
- //TODO Use base provider
- val = string.Format("{0}.NEXTVAL", sequenceName);
-
- parameters[i] = db.Parameter(info.ParameterName, val, DbType.String);
- }
- else
- {
- string seqQuery = db.DataProvider.GetSequenceQuery(sequenceName, OwnerName);
- val = db.SetCommand(seqQuery).ExecuteScalar();
- parameters[i] = db.Parameter(info.ParameterName, val);
- }
- }
- }
- }
+ var keyGenerator = mmi.KeyGenerator as SequenceKeyGenerator;
+ if (keyGenerator != null && keyGenerator.RetrievePkValue)
+ {
+ string seqQuery = db.DataProvider.GetSequenceQuery(keyGenerator.Sequence, OwnerName);
+ val = db.SetCommand(seqQuery).ExecuteScalar();
+ parameters[i] = db.Parameter(info.ParameterName, val);
+ }
}
return parameters;
View
7 Source/Mapping/MapMemberInfo.cs
@@ -1,7 +1,7 @@
using System;
using System.Data;
using System.Diagnostics;
-
+using BLToolkit.DataAccess;
using BLToolkit.Reflection;
using BLToolkit.Reflection.Extension;
@@ -34,5 +34,10 @@ public MapMemberInfo()
public MapValue[] MapValues { get; set; }
public MemberExtension MemberExtension { get; set; }
public DbType DbType { get; set; }
+
+ /// <summary>
+ /// TODO : Create PrimaryKeyMapMemberInfo?
+ /// </summary>
+ public KeyGenerator KeyGenerator { get; set; }
}
}
View
35 Source/Mapping/ObjectMapper.cs
@@ -283,6 +283,7 @@ public virtual void Init(MappingSchema mappingSchema, Type type)
mi.DefaultValue = GetDefaultValue(ma);
mi.Nullable = GetNullable (ma);
mi.NullValue = GetNullValue (ma, mi.Nullable);
+ mi.KeyGenerator = GetKeyGenerator(ma);
Add(CreateMemberMapper(mi));
}
@@ -472,6 +473,40 @@ protected virtual string GetFieldName(MemberAccessor memberAccessor)
return MetadataProvider.GetFieldName(Extension, memberAccessor, out isSet);
}
+ protected virtual KeyGenerator GetKeyGenerator(MemberAccessor memberAccessor)
+ {
+ bool isSet;
+ var nonUpdatableAttribute = MetadataProvider.GetNonUpdatableAttribute(memberAccessor.Type, Extension, memberAccessor, out isSet);
+
+ bool isPkSet;
+ MetadataProvider.GetPrimaryKeyOrder(memberAccessor.Type, Extension, memberAccessor, out isPkSet);
+ if (isPkSet)
+ {
+ bool isGeneratorSet;
+ KeyGeneratorInfo genInfo = MetadataProvider.GetGeneratorType(Extension, memberAccessor, out isGeneratorSet);
+ if (isGeneratorSet)
+ {
+ if (nonUpdatableAttribute != null && isSet)
+ {
+ throw new Exception("Cannont set primary key when NonUpdateable attribute is present!");
+ }
+
+ if (genInfo.GeneratorType == PrimaryKeyGeneratorType.Sequence)
+ {
+ bool isSeqSet;
+ string sequenceName = MetadataProvider.GetSequenceName(Extension, memberAccessor, out isSeqSet);
+ if (!isSeqSet)
+ throw new Exception("Sequence atribute is not present!");
+ if (string.IsNullOrWhiteSpace(sequenceName))
+ throw new Exception("SequenceName is empty");
+
+ return new SequenceKeyGenerator(sequenceName, genInfo.RetrievePkValue);
+ }
+ }
+ }
+ return null;
+ }
+
protected virtual string GetFieldStorage(MemberAccessor memberAccessor)
{
bool isSet;
View
17 UnitTests/CS/JointureTests/Artist.cs
@@ -1,3 +1,4 @@
+using System;
using BLToolkit.DataAccess;
using BLToolkit.Mapping;
@@ -12,4 +13,20 @@ public class Artist
[MapField("ARTIST")]
public string Name { get; set; }
}
+
+
+ [TableName(Name = "LABEL", Owner = Consts.Owner)]
+ public class Label
+ {
+ [MapField("ID_LABEL"), PrimaryKey, KeyGenerator(PrimaryKeyGeneratorType.Sequence, true), Sequence("SEQ_LABEL")]
+ public long Id { get; set; }
+
+ [MapField("LABEL")]
+ public string Name { get; set; }
+
+ public int ID_USER_ { get; set; }
+ public DateTime DATE_CREATION { get; set; }
+ public DateTime DATE_MODIFICATION { get; set; }
+ public int ACTIVATION { get; set; }
+ }
}
View
2 UnitTests/CS/JointureTests/AssociationTests.cs
@@ -56,7 +56,7 @@ public void InsertArtistWithAutoSequence()
using (var db = new MusicDB())
{
var query = new SqlQuery(db);
- var artist = new Artist() {Name = "TEST"};
+ var artist = new Label() {Name = "TEST", DATE_CREATION = DateTime.Now, DATE_MODIFICATION = DateTime.Now, ACTIVATION = 10, ID_USER_ = 200};
query.Insert(artist);
}
}

0 comments on commit a59f274

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