Update Oracle

This commit is contained in:
sunkaixuan
2017-10-14 17:34:30 +08:00
parent a82d174804
commit a3af101bce
2 changed files with 14 additions and 7 deletions

View File

@@ -61,12 +61,12 @@ namespace SqlSugar
RestoreMapping(); RestoreMapping();
return Convert.ToInt64( Ado.GetScalar(sql, InsertBuilder.Parameters == null ? null : InsertBuilder.Parameters.ToArray())); return Convert.ToInt64( Ado.GetScalar(sql, InsertBuilder.Parameters == null ? null : InsertBuilder.Parameters.ToArray()));
} }
public T ExecuteReturnEntity() public virtual T ExecuteReturnEntity()
{ {
ExecuteCommandIdentityIntoEntity(); ExecuteCommandIdentityIntoEntity();
return InsertObjs.First(); return InsertObjs.First();
} }
public bool ExecuteCommandIdentityIntoEntity() public virtual bool ExecuteCommandIdentityIntoEntity()
{ {
var result = InsertObjs.First(); var result = InsertObjs.First();
var identityKeys = GetIdentityKeys(); var identityKeys = GetIdentityKeys();

View File

@@ -7,6 +7,7 @@ namespace SqlSugar
{ {
public class OracleInsertable<T> : InsertableProvider<T> where T : class, new() public class OracleInsertable<T> : InsertableProvider<T> where T : class, new()
{ {
protected override List<string> GetIdentityKeys() protected override List<string> GetIdentityKeys()
{ {
return this.EntityInfo.Columns.Where(it => it.OracleSequenceName.HasValue()).Select(it => it.DbColumnName).ToList(); return this.EntityInfo.Columns.Where(it => it.OracleSequenceName.HasValue()).Select(it => it.DbColumnName).ToList();
@@ -26,13 +27,19 @@ namespace SqlSugar
string sql = InsertBuilder.ToSqlString(); string sql = InsertBuilder.ToSqlString();
RestoreMapping(); RestoreMapping();
var count = Ado.ExecuteCommand(sql, InsertBuilder.Parameters == null ? null : InsertBuilder.Parameters.ToArray()); var count = Ado.ExecuteCommand(sql, InsertBuilder.Parameters == null ? null : InsertBuilder.Parameters.ToArray());
var result = (this.GetIdentityKeys().IsNullOrEmpty() || count == 0) ? 0 : GetSeqValue(GetSeqName() ); var result = (this.GetIdentityKeys().IsNullOrEmpty() || count == 0) ? 0 : GetSeqValue(GetSeqName());
return result; return result;
} }
public override int ExecuteCommand()
{
base.ExecuteCommand();
return base.InsertObjs.Count();
}
private int GetSeqValue(string seqName) private int GetSeqValue(string seqName)
{ {
return Ado.GetInt(" SELECT " + seqName+ ".currval FROM DUAL"); return Ado.GetInt(" SELECT " + seqName + ".currval FROM DUAL");
} }
protected override void PreToSql() protected override void PreToSql()
@@ -40,7 +47,7 @@ namespace SqlSugar
var identities = GetSeqNames(); var identities = GetSeqNames();
var insertCount = InsertObjs.Count(); var insertCount = InsertObjs.Count();
InsertBuilder.OracleSeqInfoList = new Dictionary<string, int>(); InsertBuilder.OracleSeqInfoList = new Dictionary<string, int>();
if (identities.HasValue()&& insertCount > 1) if (identities.HasValue() && insertCount > 1)
{ {
Check.Exception(identities.Count != identities.Distinct().Count(), "The field sequence needs to be unique"); Check.Exception(identities.Count != identities.Distinct().Count(), "The field sequence needs to be unique");
foreach (var seqName in identities) foreach (var seqName in identities)
@@ -49,7 +56,7 @@ namespace SqlSugar
this.Ado.ExecuteCommand("alter sequence " + seqName + " increment by " + insertCount); this.Ado.ExecuteCommand("alter sequence " + seqName + " increment by " + insertCount);
seqBeginValue = this.Ado.GetInt("select " + seqName + ".Nextval from dual") - insertCount; seqBeginValue = this.Ado.GetInt("select " + seqName + ".Nextval from dual") - insertCount;
this.Ado.ExecuteCommand("alter sequence " + seqName + " increment by " + 1); this.Ado.ExecuteCommand("alter sequence " + seqName + " increment by " + 1);
InsertBuilder.OracleSeqInfoList.Add(seqName,seqBeginValue); InsertBuilder.OracleSeqInfoList.Add(seqName, seqBeginValue);
} }
} }
base.PreToSql(); base.PreToSql();