Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added KeyGenerator info into MapMemberInfo

Finished implementation of RetrieveKey parameter
Tests updated
Better arhitecture
  • Loading branch information...
commit a59f274e044979a9a10c7c0a6a84d200cc0d2da8 1 parent fc0acda
Valeriu Lacatusu authored
2  Demo/Partial.Trust/Asp.Net/Partial.Trust.Asp.Net.csproj
@@ -129,7 +129,7 @@
129 129 <WebProjectProperties>
130 130 <UseIIS>False</UseIIS>
131 131 <AutoAssignPort>True</AutoAssignPort>
132   - <DevelopmentServerPort>41836</DevelopmentServerPort>
  132 + <DevelopmentServerPort>2132</DevelopmentServerPort>
133 133 <DevelopmentServerVPath>/</DevelopmentServerVPath>
134 134 <IISUrl>
135 135 </IISUrl>
2  Demo/WebServices/NorthwindDataService/NorthwindDataService.csproj
@@ -116,7 +116,7 @@
116 116 <WebProjectProperties>
117 117 <UseIIS>False</UseIIS>
118 118 <AutoAssignPort>True</AutoAssignPort>
119   - <DevelopmentServerPort>41846</DevelopmentServerPort>
  119 + <DevelopmentServerPort>2142</DevelopmentServerPort>
120 120 <DevelopmentServerVPath>/</DevelopmentServerVPath>
121 121 <IISUrl>
122 122 </IISUrl>
2  Demo/WebServices/Server/Server.csproj
@@ -132,7 +132,7 @@
132 132 <WebProjectProperties>
133 133 <UseIIS>False</UseIIS>
134 134 <AutoAssignPort>True</AutoAssignPort>
135   - <DevelopmentServerPort>41841</DevelopmentServerPort>
  135 + <DevelopmentServerPort>2137</DevelopmentServerPort>
136 136 <DevelopmentServerVPath>/</DevelopmentServerVPath>
137 137 <IISUrl>
138 138 </IISUrl>
1  Source/BLToolkit.4.csproj
@@ -203,6 +203,7 @@
203 203 <Compile Include="DataAccess\KeyGeneratorAttribute.cs" />
204 204 <Compile Include="DataAccess\ParamTypeNameAttribute.cs" />
205 205 <Compile Include="DataAccess\SequenceAttribute.cs" />
  206 + <Compile Include="DataAccess\SequenceKeyGenerator.cs" />
206 207 <Compile Include="DataAccess\SqlIgnoreAttribute.cs" />
207 208 <None Include="Data\DataProvider\GenericDataProvider.cs" />
208 209 <Compile Include="Data\DataProvider\FirebirdMappingSchema.cs" />
1  Source/BLToolkit.CP.4.csproj
@@ -199,6 +199,7 @@
199 199 <Compile Include="DataAccess\ParamTypeNameAttribute.cs" />
200 200 <Compile Include="DataAccess\PrimaryKeyGeneratorType.cs" />
201 201 <Compile Include="DataAccess\SequenceAttribute.cs" />
  202 + <Compile Include="DataAccess\SequenceKeyGenerator.cs" />
202 203 <Compile Include="DataAccess\SqlIgnoreAttribute.cs" />
203 204 <Compile Include="Data\DataProvider\FirebirdMappingSchema.cs" />
204 205 <Compile Include="Data\DataProvider\Sql2008DataProvider.cs" />
1  Source/BLToolkit.Data.4.csproj
@@ -260,6 +260,7 @@
260 260 <Compile Include="DataAccess\ParamTypeNameAttribute.cs" />
261 261 <Compile Include="DataAccess\PrimaryKeyGeneratorType.cs" />
262 262 <Compile Include="DataAccess\SequenceAttribute.cs" />
  263 + <Compile Include="DataAccess\SequenceKeyGenerator.cs" />
263 264 <Compile Include="DataAccess\SqlIgnoreAttribute.cs" />
264 265 <Compile Include="Data\DataProvider\FirebirdMappingSchema.cs" />
265 266 <Compile Include="Data\DataProvider\Sql2008DataProvider.cs" />
1  Source/BLToolkit.SL.4.csproj
@@ -113,6 +113,7 @@
113 113 <Compile Include="DataAccess\PrimaryKeyAttribute.cs" />
114 114 <Compile Include="DataAccess\PrimaryKeyGeneratorType.cs" />
115 115 <Compile Include="DataAccess\SequenceAttribute.cs" />
  116 + <Compile Include="DataAccess\SequenceKeyGenerator.cs" />
116 117 <Compile Include="DataAccess\SqlIgnoreAttribute.cs" />
117 118 <Compile Include="DataAccess\TableNameAttribute.cs" />
118 119 <Compile Include="Data\DataProvider\ConvertType.cs" />
6 Source/Data/DataProvider/DataProviderBase.cs
@@ -223,6 +223,12 @@ public virtual string GetSequenceQuery(string sequenceName, string schema)
223 223 return null;
224 224 }
225 225
  226 +
  227 + public virtual string NextSequenceQuery(string sequenceName, string schema)
  228 + {
  229 + return null;
  230 + }
  231 +
226 232 public virtual void SetParameterValue(IDbDataParameter parameter, object value)
227 233 {
228 234 if (value is System.Data.Linq.Binary)
8 Source/Data/DataProvider/OdpDataProvider.cs
@@ -559,6 +559,14 @@ public override string GetSequenceQuery(string sequenceName, string schema)
559 559 return string.Format("SELECT {0}.NEXTVAL FROM DUAL", sequenceName);
560 560 }
561 561
  562 + public override string NextSequenceQuery(string sequenceName, string schema)
  563 + {
  564 + if (!string.IsNullOrWhiteSpace(schema))
  565 + return string.Format("{0}.{1}.NEXTVAL", schema, sequenceName);
  566 +
  567 + return string.Format("{0}.NEXTVAL", sequenceName);
  568 + }
  569 +
562 570 public override IDbDataParameter CreateParameterObject(IDbCommand command)
563 571 {
564 572 var parameter = base.CreateParameterObject(command);
33 Source/DataAccess/SequenceKeyGenerator.cs
... ... @@ -0,0 +1,33 @@
  1 +namespace BLToolkit.DataAccess
  2 +{
  3 + public class SequenceKeyGenerator : KeyGenerator
  4 + {
  5 + private readonly string _sequence;
  6 +
  7 + public SequenceKeyGenerator(string sequence, bool retrievePkValue)
  8 + : base(retrievePkValue)
  9 + {
  10 + _sequence = sequence;
  11 + }
  12 +
  13 + public string Sequence
  14 + {
  15 + get { return _sequence; }
  16 + }
  17 + }
  18 +
  19 + public abstract class KeyGenerator
  20 + {
  21 + private readonly bool _retrievePkValue;
  22 +
  23 + protected KeyGenerator(bool retrievePkValue)
  24 + {
  25 + _retrievePkValue = retrievePkValue;
  26 + }
  27 +
  28 + public bool RetrievePkValue
  29 + {
  30 + get { return _retrievePkValue; }
  31 + }
  32 + }
  33 +}
56 Source/DataAccess/SqlQueryBase.cs
@@ -236,14 +236,6 @@ protected SqlQueryInfo CreateInsertSqlText(DbManager db, Type type, int nParamet
236 236
237 237 bool isSet;
238 238 var nonUpdatableAttribute = mp.GetNonUpdatableAttribute(type, typeExt, mm.MapMemberInfo.MemberAccessor, out isSet);
239   -
240   - bool isGeneratorSet;
241   - mp.GetGeneratorType(typeExt, mm.MapMemberInfo.MemberAccessor, out isGeneratorSet);
242   - if (isGeneratorSet && (nonUpdatableAttribute != null && isSet))
243   - {
244   - throw new Exception("Cannont set primary key when NonUpdateable attribute is present!");
245   - }
246   -
247 239 if (nonUpdatableAttribute == null || !isSet || nonUpdatableAttribute.OnInsert == false)
248 240 {
249 241 sb.AppendFormat("\t{0},\n",
@@ -256,20 +248,29 @@ protected SqlQueryInfo CreateInsertSqlText(DbManager db, Type type, int nParamet
256 248
257 249 sb.Append(") VALUES (\n");
258 250
259   - foreach (var mm in list)
260   - {
261   - var p = query.AddParameter(
262   - db.DataProvider.Convert(mm.Name + "_P", ConvertType.NameToQueryParameter).ToString(),
263   - mm.Name);
264   -
265   - if (nParameter < 0)
266   - sb.AppendFormat("\t{0},\n", p.ParameterName);
267   - //sb.AppendFormat("\t{0},\n", db.DataProvider.Convert(p.ParameterName, ConvertType.NameToQueryParameter));
268   - else
269   - sb.AppendFormat("\t{{{0}}},\n", nParameter++);
270   - }
  251 + foreach (var mm in list)
  252 + {
  253 + var keyGenerator = mm.MapMemberInfo.KeyGenerator as SequenceKeyGenerator;
  254 + if (keyGenerator != null && !keyGenerator.RetrievePkValue)
  255 + {
  256 + string seqQuery = db.DataProvider.NextSequenceQuery(keyGenerator.Sequence, GetOwnerName(type));
  257 + sb.AppendFormat("\t{0},\n", seqQuery);
  258 + }
  259 + else
  260 + {
  261 + var p = query.AddParameter(
  262 + db.DataProvider.Convert(mm.Name + "_P", ConvertType.NameToQueryParameter).ToString(),
  263 + mm.Name);
  264 +
  265 + if (nParameter < 0)
  266 + sb.AppendFormat("\t{0},\n", p.ParameterName);
  267 + //sb.AppendFormat("\t{0},\n", db.DataProvider.Convert(p.ParameterName, ConvertType.NameToQueryParameter));
  268 + else
  269 + sb.AppendFormat("\t{{{0}}},\n", nParameter++);
  270 + }
  271 + }
271 272
272   - sb.Remove(sb.Length - 2, 1);
  273 + sb.Remove(sb.Length - 2, 1);
273 274
274 275 sb.Append(")");
275 276
@@ -382,11 +383,6 @@ public virtual SqlQueryInfo GetSqlQueryInfo(DbManager db, Type type, string acti
382 383 {
383 384 query = CreateSqlText(db, type, actionName);
384 385 query.OwnerName = GetOwnerName(type);
385   - QueryType queryType;
386   - if (Enum.TryParse(actionName, out queryType))
387   - {
388   - query.QueryType = queryType;
389   - }
390 386 _actionSqlQueryInfo[key] = query;
391 387 }
392 388
@@ -395,12 +391,4 @@ public virtual SqlQueryInfo GetSqlQueryInfo(DbManager db, Type type, string acti
395 391
396 392 #endregion
397 393 }
398   -
399   - public enum QueryType
400   - {
401   - None,
402   - SelectByKey,
403   - SelectAll,
404   - Insert,
405   - }
406 394 }
49 Source/DataAccess/SqlQueryInfo.cs
@@ -20,7 +20,6 @@ public SqlQueryInfo(ObjectMapper objectMapper)
20 20 ObjectMapper = objectMapper;
21 21 }
22 22
23   - public QueryType QueryType { get; set; }
24 23 public string OwnerName { get; set; }
25 24 public string QueryText { get; set; }
26 25 public ObjectMapper ObjectMapper { get; private set; }
@@ -73,12 +72,8 @@ public IDbDataParameter[] GetParameters(DbManager db, object obj)
73 72 //parameters[i] = db.Parameter(info.ParameterName, info.MemberMapper.GetValue(obj));
74 73
75 74 var mmi = info.MemberMapper.MapMemberInfo;
76   -
77 75 var val = info.MemberMapper.GetValue(obj);
78   - var mp = info.MemberMapper.MappingSchema.MetadataProvider;
79   - var type = info.MemberMapper.Type;
80   - var typeExt = TypeExtension.GetTypeExtension(type, new ExtensionList());
81   -
  76 +
82 77 if (val == null && mmi.Nullable/* && mmi.NullValue == null*/)
83 78 {
84 79 //replace value with DbNull
@@ -96,41 +91,13 @@ public IDbDataParameter[] GetParameters(DbManager db, object obj)
96 91 parameters[i] = db.Parameter(info.ParameterName, val);
97 92 }
98 93
99   - bool isPkSet;
100   -
101   - mp.GetPrimaryKeyOrder(type, typeExt, mmi.MemberAccessor, out isPkSet);
102   - if (isPkSet)
103   - {
104   - bool isGeneratorSet;
105   - KeyGeneratorInfo genInfo = mp.GetGeneratorType(typeExt, mmi.MemberAccessor, out isGeneratorSet);
106   - if (isGeneratorSet)
107   - {
108   - if (genInfo.GeneratorType == PrimaryKeyGeneratorType.Sequence)
109   - {
110   - bool isSeqSet;
111   - string sequenceName = mp.GetSequenceName(typeExt, mmi.MemberAccessor, out isSeqSet);
112   - if (!isSeqSet)
113   - throw new Exception("Sequence atribute is not present!");
114   - if (string.IsNullOrWhiteSpace(sequenceName))
115   - throw new Exception("SequenceName is empty");
116   -
117   - if (!genInfo.RetrievePkValue)
118   - {
119   - throw new Exception("Not implemented");
120   - //TODO Use base provider
121   - val = string.Format("{0}.NEXTVAL", sequenceName);
122   -
123   - parameters[i] = db.Parameter(info.ParameterName, val, DbType.String);
124   - }
125   - else
126   - {
127   - string seqQuery = db.DataProvider.GetSequenceQuery(sequenceName, OwnerName);
128   - val = db.SetCommand(seqQuery).ExecuteScalar();
129   - parameters[i] = db.Parameter(info.ParameterName, val);
130   - }
131   - }
132   - }
133   - }
  94 + var keyGenerator = mmi.KeyGenerator as SequenceKeyGenerator;
  95 + if (keyGenerator != null && keyGenerator.RetrievePkValue)
  96 + {
  97 + string seqQuery = db.DataProvider.GetSequenceQuery(keyGenerator.Sequence, OwnerName);
  98 + val = db.SetCommand(seqQuery).ExecuteScalar();
  99 + parameters[i] = db.Parameter(info.ParameterName, val);
  100 + }
134 101 }
135 102
136 103 return parameters;
7 Source/Mapping/MapMemberInfo.cs
... ... @@ -1,7 +1,7 @@
1 1 using System;
2 2 using System.Data;
3 3 using System.Diagnostics;
4   -
  4 +using BLToolkit.DataAccess;
5 5 using BLToolkit.Reflection;
6 6 using BLToolkit.Reflection.Extension;
7 7
@@ -34,5 +34,10 @@ public MapMemberInfo()
34 34 public MapValue[] MapValues { get; set; }
35 35 public MemberExtension MemberExtension { get; set; }
36 36 public DbType DbType { get; set; }
  37 +
  38 + /// <summary>
  39 + /// TODO : Create PrimaryKeyMapMemberInfo?
  40 + /// </summary>
  41 + public KeyGenerator KeyGenerator { get; set; }
37 42 }
38 43 }
35 Source/Mapping/ObjectMapper.cs
@@ -283,6 +283,7 @@ public virtual void Init(MappingSchema mappingSchema, Type type)
283 283 mi.DefaultValue = GetDefaultValue(ma);
284 284 mi.Nullable = GetNullable (ma);
285 285 mi.NullValue = GetNullValue (ma, mi.Nullable);
  286 + mi.KeyGenerator = GetKeyGenerator(ma);
286 287
287 288 Add(CreateMemberMapper(mi));
288 289 }
@@ -472,6 +473,40 @@ protected virtual string GetFieldName(MemberAccessor memberAccessor)
472 473 return MetadataProvider.GetFieldName(Extension, memberAccessor, out isSet);
473 474 }
474 475
  476 + protected virtual KeyGenerator GetKeyGenerator(MemberAccessor memberAccessor)
  477 + {
  478 + bool isSet;
  479 + var nonUpdatableAttribute = MetadataProvider.GetNonUpdatableAttribute(memberAccessor.Type, Extension, memberAccessor, out isSet);
  480 +
  481 + bool isPkSet;
  482 + MetadataProvider.GetPrimaryKeyOrder(memberAccessor.Type, Extension, memberAccessor, out isPkSet);
  483 + if (isPkSet)
  484 + {
  485 + bool isGeneratorSet;
  486 + KeyGeneratorInfo genInfo = MetadataProvider.GetGeneratorType(Extension, memberAccessor, out isGeneratorSet);
  487 + if (isGeneratorSet)
  488 + {
  489 + if (nonUpdatableAttribute != null && isSet)
  490 + {
  491 + throw new Exception("Cannont set primary key when NonUpdateable attribute is present!");
  492 + }
  493 +
  494 + if (genInfo.GeneratorType == PrimaryKeyGeneratorType.Sequence)
  495 + {
  496 + bool isSeqSet;
  497 + string sequenceName = MetadataProvider.GetSequenceName(Extension, memberAccessor, out isSeqSet);
  498 + if (!isSeqSet)
  499 + throw new Exception("Sequence atribute is not present!");
  500 + if (string.IsNullOrWhiteSpace(sequenceName))
  501 + throw new Exception("SequenceName is empty");
  502 +
  503 + return new SequenceKeyGenerator(sequenceName, genInfo.RetrievePkValue);
  504 + }
  505 + }
  506 + }
  507 + return null;
  508 + }
  509 +
475 510 protected virtual string GetFieldStorage(MemberAccessor memberAccessor)
476 511 {
477 512 bool isSet;
17 UnitTests/CS/JointureTests/Artist.cs
... ... @@ -1,3 +1,4 @@
  1 +using System;
1 2 using BLToolkit.DataAccess;
2 3 using BLToolkit.Mapping;
3 4
@@ -12,4 +13,20 @@ public class Artist
12 13 [MapField("ARTIST")]
13 14 public string Name { get; set; }
14 15 }
  16 +
  17 +
  18 + [TableName(Name = "LABEL", Owner = Consts.Owner)]
  19 + public class Label
  20 + {
  21 + [MapField("ID_LABEL"), PrimaryKey, KeyGenerator(PrimaryKeyGeneratorType.Sequence, true), Sequence("SEQ_LABEL")]
  22 + public long Id { get; set; }
  23 +
  24 + [MapField("LABEL")]
  25 + public string Name { get; set; }
  26 +
  27 + public int ID_USER_ { get; set; }
  28 + public DateTime DATE_CREATION { get; set; }
  29 + public DateTime DATE_MODIFICATION { get; set; }
  30 + public int ACTIVATION { get; set; }
  31 + }
15 32 }
2  UnitTests/CS/JointureTests/AssociationTests.cs
@@ -56,7 +56,7 @@ public void InsertArtistWithAutoSequence()
56 56 using (var db = new MusicDB())
57 57 {
58 58 var query = new SqlQuery(db);
59   - var artist = new Artist() {Name = "TEST"};
  59 + var artist = new Label() {Name = "TEST", DATE_CREATION = DateTime.Now, DATE_MODIFICATION = DateTime.Now, ACTIVATION = 10, ID_USER_ = 200};
60 60 query.Insert(artist);
61 61 }
62 62 }

0 comments on commit a59f274

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