Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 05a94a3

Browse files
Change Oracle implementation of InsertAndGetLastInsertId
- do not store value of identity from shared state variable - throw exception from Oracle GetLastInsertId() - fix a test, and an auto comment
1 parent c46a221 commit 05a94a3

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

src/ServiceStack.OrmLite.Oracle.Tests/DateTimeColumnTest.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,10 @@ public void Can_store_and_retrieve_DateTime_Value()
5050

5151
db.Save(obj);
5252

53-
var id = (int)db.LastInsertId();
54-
var target = db.SingleById<Analyze>(id);
53+
var target = db.SingleById<Analyze>(obj.Id);
5554

5655
Assert.IsNotNull(target);
57-
Assert.AreEqual(id, target.Id);
56+
Assert.AreEqual(obj.Id, target.Id);
5857
Assert.AreEqual(obj.Date.ToString("yyyy-MM-dd HH:mm:ss"), target.Date.ToString("yyyy-MM-dd HH:mm:ss"));
5958
Assert.AreEqual(obj.Url, target.Url);
6059
}

src/ServiceStack.OrmLite.Oracle/OracleOrmLiteDialectProvider.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public static OracleOrmLiteDialectProvider Instance
3636
get { return _instance ?? (_instance = new OracleOrmLiteDialectProvider()); }
3737
}
3838

39-
internal long LastInsertId { get; set; }
4039
protected bool CompactGuid;
4140

4241
internal const string StringGuidDefinition = "VARCHAR2(37)";
@@ -148,13 +147,24 @@ public override IDbConnection CreateConnection(string connectionString, Dictiona
148147

149148
public override long GetLastInsertId(IDbCommand dbCmd)
150149
{
151-
return LastInsertId;
150+
throw new NotSupportedException();
152151
}
153152

154153
public override long InsertAndGetLastInsertId<T>(IDbCommand dbCmd)
155154
{
156155
dbCmd.ExecuteScalar();
157-
return GetLastInsertId(dbCmd);
156+
157+
var modelDef = GetModel(typeof(T));
158+
159+
var primaryKey = modelDef.PrimaryKey;
160+
if (primaryKey == null)
161+
return 0;
162+
163+
var identityParameter = (DbParameter)dbCmd.Parameters[this.GetParam(SanitizeFieldNameForParamName(primaryKey.FieldName))];
164+
if (identityParameter == null)
165+
return 0;
166+
167+
return Convert.ToInt64(identityParameter.Value);
158168
}
159169

160170
public override void SetDbValue(FieldDefinition fieldDef, IDataReader reader, int colIndex, object instance)
@@ -897,13 +907,11 @@ private object GetNextValue(IDbConnection connection, IDbTransaction transaction
897907
Object retObj;
898908
if (long.TryParse(value.ToString(), out nv))
899909
{
900-
LastInsertId = nv;
901-
retObj = LastInsertId;
910+
retObj = nv;
902911
}
903912
else
904913
{
905-
LastInsertId = 0;
906-
retObj = value;
914+
retObj = 0;
907915
}
908916
return retObj;
909917
}
@@ -913,7 +921,6 @@ private object GetNextValue(IDbConnection connection, IDbTransaction transaction
913921
dbCmd.Transaction = transaction;
914922
dbCmd.CommandText = string.Format("SELECT {0}.NEXTVAL FROM dual", Quote(sequence));
915923
var result = dbCmd.LongScalar();
916-
LastInsertId = result;
917924
return result;
918925
}
919926
}

src/ServiceStack.OrmLite/OrmLiteWriteConnectionExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Data;
5+
using ServiceStack.Data;
56

67
namespace ServiceStack.OrmLite
78
{
@@ -281,7 +282,7 @@ public static int DeleteById<T>(this IDbConnection dbConn, object id)
281282

282283
/// <summary>
283284
/// Delete 1 row by the PrimaryKey where the rowVersion matches the optimistic concurrency field.
284-
/// Will throw <exception cref="RowModifiedException">RowModefiedExeption</exception> if the
285+
/// Will throw <exception cref="OptimisticConcurrencyException">RowModefiedExeption</exception> if the
285286
/// row does not exist or has a different row version.
286287
/// E.g: <para>db.DeleteById&lt;Person&gt;(1)</para>
287288
/// </summary>

0 commit comments

Comments
 (0)