Skip to content

Commit

Permalink
Added KeyGenerator info into MapMemberInfo
Browse files Browse the repository at this point in the history
Finished implementation of RetrieveKey parameter
Tests updated
Better arhitecture
  • Loading branch information
lvaleriu committed May 11, 2012
1 parent fc0acda commit a59f274
Show file tree
Hide file tree
Showing 16 changed files with 143 additions and 80 deletions.
2 changes: 1 addition & 1 deletion Demo/Partial.Trust/Asp.Net/Partial.Trust.Asp.Net.csproj
Expand Up @@ -129,7 +129,7 @@
<WebProjectProperties> <WebProjectProperties>
<UseIIS>False</UseIIS> <UseIIS>False</UseIIS>
<AutoAssignPort>True</AutoAssignPort> <AutoAssignPort>True</AutoAssignPort>
<DevelopmentServerPort>41836</DevelopmentServerPort> <DevelopmentServerPort>2132</DevelopmentServerPort>
<DevelopmentServerVPath>/</DevelopmentServerVPath> <DevelopmentServerVPath>/</DevelopmentServerVPath>
<IISUrl> <IISUrl>
</IISUrl> </IISUrl>
Expand Down
Expand Up @@ -116,7 +116,7 @@
<WebProjectProperties> <WebProjectProperties>
<UseIIS>False</UseIIS> <UseIIS>False</UseIIS>
<AutoAssignPort>True</AutoAssignPort> <AutoAssignPort>True</AutoAssignPort>
<DevelopmentServerPort>41846</DevelopmentServerPort> <DevelopmentServerPort>2142</DevelopmentServerPort>
<DevelopmentServerVPath>/</DevelopmentServerVPath> <DevelopmentServerVPath>/</DevelopmentServerVPath>
<IISUrl> <IISUrl>
</IISUrl> </IISUrl>
Expand Down
2 changes: 1 addition & 1 deletion Demo/WebServices/Server/Server.csproj
Expand Up @@ -132,7 +132,7 @@
<WebProjectProperties> <WebProjectProperties>
<UseIIS>False</UseIIS> <UseIIS>False</UseIIS>
<AutoAssignPort>True</AutoAssignPort> <AutoAssignPort>True</AutoAssignPort>
<DevelopmentServerPort>41841</DevelopmentServerPort> <DevelopmentServerPort>2137</DevelopmentServerPort>
<DevelopmentServerVPath>/</DevelopmentServerVPath> <DevelopmentServerVPath>/</DevelopmentServerVPath>
<IISUrl> <IISUrl>
</IISUrl> </IISUrl>
Expand Down
1 change: 1 addition & 0 deletions Source/BLToolkit.4.csproj
Expand Up @@ -203,6 +203,7 @@
<Compile Include="DataAccess\KeyGeneratorAttribute.cs" /> <Compile Include="DataAccess\KeyGeneratorAttribute.cs" />
<Compile Include="DataAccess\ParamTypeNameAttribute.cs" /> <Compile Include="DataAccess\ParamTypeNameAttribute.cs" />
<Compile Include="DataAccess\SequenceAttribute.cs" /> <Compile Include="DataAccess\SequenceAttribute.cs" />
<Compile Include="DataAccess\SequenceKeyGenerator.cs" />
<Compile Include="DataAccess\SqlIgnoreAttribute.cs" /> <Compile Include="DataAccess\SqlIgnoreAttribute.cs" />
<None Include="Data\DataProvider\GenericDataProvider.cs" /> <None Include="Data\DataProvider\GenericDataProvider.cs" />
<Compile Include="Data\DataProvider\FirebirdMappingSchema.cs" /> <Compile Include="Data\DataProvider\FirebirdMappingSchema.cs" />
Expand Down
1 change: 1 addition & 0 deletions Source/BLToolkit.CP.4.csproj
Expand Up @@ -199,6 +199,7 @@
<Compile Include="DataAccess\ParamTypeNameAttribute.cs" /> <Compile Include="DataAccess\ParamTypeNameAttribute.cs" />
<Compile Include="DataAccess\PrimaryKeyGeneratorType.cs" /> <Compile Include="DataAccess\PrimaryKeyGeneratorType.cs" />
<Compile Include="DataAccess\SequenceAttribute.cs" /> <Compile Include="DataAccess\SequenceAttribute.cs" />
<Compile Include="DataAccess\SequenceKeyGenerator.cs" />
<Compile Include="DataAccess\SqlIgnoreAttribute.cs" /> <Compile Include="DataAccess\SqlIgnoreAttribute.cs" />
<Compile Include="Data\DataProvider\FirebirdMappingSchema.cs" /> <Compile Include="Data\DataProvider\FirebirdMappingSchema.cs" />
<Compile Include="Data\DataProvider\Sql2008DataProvider.cs" /> <Compile Include="Data\DataProvider\Sql2008DataProvider.cs" />
Expand Down
1 change: 1 addition & 0 deletions Source/BLToolkit.Data.4.csproj
Expand Up @@ -260,6 +260,7 @@
<Compile Include="DataAccess\ParamTypeNameAttribute.cs" /> <Compile Include="DataAccess\ParamTypeNameAttribute.cs" />
<Compile Include="DataAccess\PrimaryKeyGeneratorType.cs" /> <Compile Include="DataAccess\PrimaryKeyGeneratorType.cs" />
<Compile Include="DataAccess\SequenceAttribute.cs" /> <Compile Include="DataAccess\SequenceAttribute.cs" />
<Compile Include="DataAccess\SequenceKeyGenerator.cs" />
<Compile Include="DataAccess\SqlIgnoreAttribute.cs" /> <Compile Include="DataAccess\SqlIgnoreAttribute.cs" />
<Compile Include="Data\DataProvider\FirebirdMappingSchema.cs" /> <Compile Include="Data\DataProvider\FirebirdMappingSchema.cs" />
<Compile Include="Data\DataProvider\Sql2008DataProvider.cs" /> <Compile Include="Data\DataProvider\Sql2008DataProvider.cs" />
Expand Down
1 change: 1 addition & 0 deletions Source/BLToolkit.SL.4.csproj
Expand Up @@ -113,6 +113,7 @@
<Compile Include="DataAccess\PrimaryKeyAttribute.cs" /> <Compile Include="DataAccess\PrimaryKeyAttribute.cs" />
<Compile Include="DataAccess\PrimaryKeyGeneratorType.cs" /> <Compile Include="DataAccess\PrimaryKeyGeneratorType.cs" />
<Compile Include="DataAccess\SequenceAttribute.cs" /> <Compile Include="DataAccess\SequenceAttribute.cs" />
<Compile Include="DataAccess\SequenceKeyGenerator.cs" />
<Compile Include="DataAccess\SqlIgnoreAttribute.cs" /> <Compile Include="DataAccess\SqlIgnoreAttribute.cs" />
<Compile Include="DataAccess\TableNameAttribute.cs" /> <Compile Include="DataAccess\TableNameAttribute.cs" />
<Compile Include="Data\DataProvider\ConvertType.cs" /> <Compile Include="Data\DataProvider\ConvertType.cs" />
Expand Down
6 changes: 6 additions & 0 deletions Source/Data/DataProvider/DataProviderBase.cs
Expand Up @@ -223,6 +223,12 @@ public virtual string GetSequenceQuery(string sequenceName, string schema)
return null; return null;
} }



public virtual string NextSequenceQuery(string sequenceName, string schema)
{
return null;
}

public virtual void SetParameterValue(IDbDataParameter parameter, object value) public virtual void SetParameterValue(IDbDataParameter parameter, object value)
{ {
if (value is System.Data.Linq.Binary) if (value is System.Data.Linq.Binary)
Expand Down
8 changes: 8 additions & 0 deletions Source/Data/DataProvider/OdpDataProvider.cs
Expand Up @@ -559,6 +559,14 @@ public override string GetSequenceQuery(string sequenceName, string schema)
return string.Format("SELECT {0}.NEXTVAL FROM DUAL", sequenceName); 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) public override IDbDataParameter CreateParameterObject(IDbCommand command)
{ {
var parameter = base.CreateParameterObject(command); var parameter = base.CreateParameterObject(command);
Expand Down
33 changes: 33 additions & 0 deletions 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; }
}
}
}
56 changes: 22 additions & 34 deletions Source/DataAccess/SqlQueryBase.cs
Expand Up @@ -236,14 +236,6 @@ protected SqlQueryInfo CreateInsertSqlText(DbManager db, Type type, int nParamet


bool isSet; bool isSet;
var nonUpdatableAttribute = mp.GetNonUpdatableAttribute(type, typeExt, mm.MapMemberInfo.MemberAccessor, out 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) if (nonUpdatableAttribute == null || !isSet || nonUpdatableAttribute.OnInsert == false)
{ {
sb.AppendFormat("\t{0},\n", sb.AppendFormat("\t{0},\n",
Expand All @@ -256,20 +248,29 @@ protected SqlQueryInfo CreateInsertSqlText(DbManager db, Type type, int nParamet


sb.Append(") VALUES (\n"); sb.Append(") VALUES (\n");


foreach (var mm in list) foreach (var mm in list)
{ {
var p = query.AddParameter( var keyGenerator = mm.MapMemberInfo.KeyGenerator as SequenceKeyGenerator;
db.DataProvider.Convert(mm.Name + "_P", ConvertType.NameToQueryParameter).ToString(), if (keyGenerator != null && !keyGenerator.RetrievePkValue)
mm.Name); {

string seqQuery = db.DataProvider.NextSequenceQuery(keyGenerator.Sequence, GetOwnerName(type));
if (nParameter < 0) sb.AppendFormat("\t{0},\n", seqQuery);
sb.AppendFormat("\t{0},\n", p.ParameterName); }
//sb.AppendFormat("\t{0},\n", db.DataProvider.Convert(p.ParameterName, ConvertType.NameToQueryParameter)); else
else {
sb.AppendFormat("\t{{{0}}},\n", nParameter++); 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(")"); sb.Append(")");


Expand Down Expand Up @@ -382,11 +383,6 @@ public virtual SqlQueryInfo GetSqlQueryInfo(DbManager db, Type type, string acti
{ {
query = CreateSqlText(db, type, actionName); query = CreateSqlText(db, type, actionName);
query.OwnerName = GetOwnerName(type); query.OwnerName = GetOwnerName(type);
QueryType queryType;
if (Enum.TryParse(actionName, out queryType))
{
query.QueryType = queryType;
}
_actionSqlQueryInfo[key] = query; _actionSqlQueryInfo[key] = query;
} }


Expand All @@ -395,12 +391,4 @@ public virtual SqlQueryInfo GetSqlQueryInfo(DbManager db, Type type, string acti


#endregion #endregion
} }

public enum QueryType
{
None,
SelectByKey,
SelectAll,
Insert,
}
} }
49 changes: 8 additions & 41 deletions Source/DataAccess/SqlQueryInfo.cs
Expand Up @@ -20,7 +20,6 @@ public SqlQueryInfo(ObjectMapper objectMapper)
ObjectMapper = objectMapper; ObjectMapper = objectMapper;
} }


public QueryType QueryType { get; set; }
public string OwnerName { get; set; } public string OwnerName { get; set; }
public string QueryText { get; set; } public string QueryText { get; set; }
public ObjectMapper ObjectMapper { get; private set; } public ObjectMapper ObjectMapper { get; private set; }
Expand Down Expand Up @@ -73,12 +72,8 @@ public IDbDataParameter[] GetParameters(DbManager db, object obj)
//parameters[i] = db.Parameter(info.ParameterName, info.MemberMapper.GetValue(obj)); //parameters[i] = db.Parameter(info.ParameterName, info.MemberMapper.GetValue(obj));


var mmi = info.MemberMapper.MapMemberInfo; var mmi = info.MemberMapper.MapMemberInfo;

var val = info.MemberMapper.GetValue(obj); 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*/) if (val == null && mmi.Nullable/* && mmi.NullValue == null*/)
{ {
//replace value with DbNull //replace value with DbNull
Expand All @@ -96,41 +91,13 @@ public IDbDataParameter[] GetParameters(DbManager db, object obj)
parameters[i] = db.Parameter(info.ParameterName, val); parameters[i] = db.Parameter(info.ParameterName, val);
} }


bool isPkSet; var keyGenerator = mmi.KeyGenerator as SequenceKeyGenerator;

if (keyGenerator != null && keyGenerator.RetrievePkValue)
mp.GetPrimaryKeyOrder(type, typeExt, mmi.MemberAccessor, out isPkSet); {
if (isPkSet) string seqQuery = db.DataProvider.GetSequenceQuery(keyGenerator.Sequence, OwnerName);
{ val = db.SetCommand(seqQuery).ExecuteScalar();
bool isGeneratorSet; parameters[i] = db.Parameter(info.ParameterName, val);
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);
}
}
}
}
} }


return parameters; return parameters;
Expand Down
7 changes: 6 additions & 1 deletion Source/Mapping/MapMemberInfo.cs
@@ -1,7 +1,7 @@
using System; using System;
using System.Data; using System.Data;
using System.Diagnostics; using System.Diagnostics;

using BLToolkit.DataAccess;
using BLToolkit.Reflection; using BLToolkit.Reflection;
using BLToolkit.Reflection.Extension; using BLToolkit.Reflection.Extension;


Expand Down Expand Up @@ -34,5 +34,10 @@ public MapMemberInfo()
public MapValue[] MapValues { get; set; } public MapValue[] MapValues { get; set; }
public MemberExtension MemberExtension { get; set; } public MemberExtension MemberExtension { get; set; }
public DbType DbType { get; set; } public DbType DbType { get; set; }

/// <summary>
/// TODO : Create PrimaryKeyMapMemberInfo?
/// </summary>
public KeyGenerator KeyGenerator { get; set; }
} }
} }
35 changes: 35 additions & 0 deletions Source/Mapping/ObjectMapper.cs
Expand Up @@ -283,6 +283,7 @@ public virtual void Init(MappingSchema mappingSchema, Type type)
mi.DefaultValue = GetDefaultValue(ma); mi.DefaultValue = GetDefaultValue(ma);
mi.Nullable = GetNullable (ma); mi.Nullable = GetNullable (ma);
mi.NullValue = GetNullValue (ma, mi.Nullable); mi.NullValue = GetNullValue (ma, mi.Nullable);
mi.KeyGenerator = GetKeyGenerator(ma);


Add(CreateMemberMapper(mi)); Add(CreateMemberMapper(mi));
} }
Expand Down Expand Up @@ -472,6 +473,40 @@ protected virtual string GetFieldName(MemberAccessor memberAccessor)
return MetadataProvider.GetFieldName(Extension, memberAccessor, out isSet); 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) protected virtual string GetFieldStorage(MemberAccessor memberAccessor)
{ {
bool isSet; bool isSet;
Expand Down
17 changes: 17 additions & 0 deletions UnitTests/CS/JointureTests/Artist.cs
@@ -1,3 +1,4 @@
using System;
using BLToolkit.DataAccess; using BLToolkit.DataAccess;
using BLToolkit.Mapping; using BLToolkit.Mapping;


Expand All @@ -12,4 +13,20 @@ public class Artist
[MapField("ARTIST")] [MapField("ARTIST")]
public string Name { get; set; } 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; }
}
} }
2 changes: 1 addition & 1 deletion UnitTests/CS/JointureTests/AssociationTests.cs
Expand Up @@ -56,7 +56,7 @@ public void InsertArtistWithAutoSequence()
using (var db = new MusicDB()) using (var db = new MusicDB())
{ {
var query = new SqlQuery(db); 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); query.Insert(artist);
} }
} }
Expand Down

0 comments on commit a59f274

Please sign in to comment.