mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 02:32:50 +08:00
-增加RestAPI适配
This commit is contained in:
parent
7306372aa6
commit
a8a11dd38d
@ -1,6 +1,8 @@
|
|||||||
using FreeSql.Internal;
|
using FreeSql.Internal;
|
||||||
using FreeSql.Internal.Model;
|
using FreeSql.Internal.Model;
|
||||||
|
using Newtonsoft.Json;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Data.Common;
|
using System.Data.Common;
|
||||||
@ -11,10 +13,8 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace FreeSql.QuestDb.Curd
|
namespace FreeSql.QuestDb.Curd
|
||||||
{
|
{
|
||||||
|
|
||||||
class QuestDbUpdate<T1> : Internal.CommonProvider.UpdateProvider<T1>
|
class QuestDbUpdate<T1> : Internal.CommonProvider.UpdateProvider<T1>
|
||||||
{
|
{
|
||||||
|
|
||||||
public QuestDbUpdate(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere)
|
public QuestDbUpdate(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere)
|
||||||
: base(orm, commonUtils, commonExpression, dywhere)
|
: base(orm, commonUtils, commonExpression, dywhere)
|
||||||
{
|
{
|
||||||
@ -25,11 +25,46 @@ namespace FreeSql.QuestDb.Curd
|
|||||||
internal StringBuilder InternalSbSetIncr => _setIncr;
|
internal StringBuilder InternalSbSetIncr => _setIncr;
|
||||||
internal Dictionary<string, bool> InternalIgnore => _ignore;
|
internal Dictionary<string, bool> InternalIgnore => _ignore;
|
||||||
internal void InternalResetSource(List<T1> source) => _source = source;
|
internal void InternalResetSource(List<T1> source) => _source = source;
|
||||||
internal string InternalWhereCaseSource(string CsName, Func<string, string> thenValue) => WhereCaseSource(CsName, thenValue);
|
|
||||||
|
internal string InternalWhereCaseSource(string CsName, Func<string, string> thenValue) =>
|
||||||
|
WhereCaseSource(CsName, thenValue);
|
||||||
|
|
||||||
internal void InternalToSqlCaseWhenEnd(StringBuilder sb, ColumnInfo col) => ToSqlCaseWhenEnd(sb, col);
|
internal void InternalToSqlCaseWhenEnd(StringBuilder sb, ColumnInfo col) => ToSqlCaseWhenEnd(sb, col);
|
||||||
|
|
||||||
public override int ExecuteAffrows() => base.SplitExecuteAffrows(_batchRowsLimit > 0 ? _batchRowsLimit : 500, _batchParameterLimit > 0 ? _batchParameterLimit : 3000);
|
private int InternelExecuteAffrows()
|
||||||
public override List<T1> ExecuteUpdated() => base.SplitExecuteUpdated(_batchRowsLimit > 0 ? _batchRowsLimit : 500, _batchParameterLimit > 0 ? _batchParameterLimit : 3000);
|
{
|
||||||
|
//如果设置了RestAPI的Url则走HTTP
|
||||||
|
var sql = ToSql();
|
||||||
|
var execAsync = RestAPIExtension.ExecAsync(sql).GetAwaiter().GetResult();
|
||||||
|
var resultHash = new Hashtable();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
resultHash = JsonConvert.DeserializeObject<Hashtable>(execAsync);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
if (execAsync.Contains("401"))
|
||||||
|
{
|
||||||
|
throw new Exception("请确认QuestDb设置的RestAPI账号是否正确.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var ddl = resultHash["ddl"]?.ToString();
|
||||||
|
var updated = Convert.ToInt32(resultHash["updated"]);
|
||||||
|
return ddl?.ToLower() == "ok" ? updated : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override int ExecuteAffrows()
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(RestAPIExtension.BaseUrl))
|
||||||
|
{
|
||||||
|
return base.SplitExecuteAffrows(_batchRowsLimit > 0 ? _batchRowsLimit : 500,
|
||||||
|
_batchParameterLimit > 0 ? _batchParameterLimit : 3000);
|
||||||
|
}
|
||||||
|
return InternelExecuteAffrows();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override List<T1> ExecuteUpdated() => base.SplitExecuteUpdated(
|
||||||
|
_batchRowsLimit > 0 ? _batchRowsLimit : 500, _batchParameterLimit > 0 ? _batchParameterLimit : 3000);
|
||||||
|
|
||||||
protected override List<T1> RawExecuteUpdated()
|
protected override List<T1> RawExecuteUpdated()
|
||||||
{
|
{
|
||||||
@ -48,10 +83,12 @@ namespace FreeSql.QuestDb.Curd
|
|||||||
foreach (var col in _table.Columns.Values)
|
foreach (var col in _table.Columns.Values)
|
||||||
{
|
{
|
||||||
if (colidx > 0) sbret.Append(", ");
|
if (colidx > 0) sbret.Append(", ");
|
||||||
sbret.Append(_commonUtils.RereadColumn(col, _commonUtils.QuoteSqlName(col.Attribute.Name))).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
|
sbret.Append(_commonUtils.RereadColumn(col, _commonUtils.QuoteSqlName(col.Attribute.Name)))
|
||||||
|
.Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
|
||||||
++colidx;
|
++colidx;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var sql = sb.Append(sbret).ToString();
|
var sql = sb.Append(sbret).ToString();
|
||||||
var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Update, sql, dbParms);
|
var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Update, sql, dbParms);
|
||||||
_orm.Aop.CurdBeforeHandler?.Invoke(this, before);
|
_orm.Aop.CurdBeforeHandler?.Invoke(this, before);
|
||||||
@ -59,7 +96,8 @@ namespace FreeSql.QuestDb.Curd
|
|||||||
Exception exception = null;
|
Exception exception = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var rettmp = _orm.Ado.Query<T1>(_table.TypeLazy ?? _table.Type, _connection, _transaction, CommandType.Text, sql, _commandTimeout, dbParms);
|
var rettmp = _orm.Ado.Query<T1>(_table.TypeLazy ?? _table.Type, _connection, _transaction,
|
||||||
|
CommandType.Text, sql, _commandTimeout, dbParms);
|
||||||
ValidateVersionAndThrow(rettmp.Count, sql, dbParms);
|
ValidateVersionAndThrow(rettmp.Count, sql, dbParms);
|
||||||
ret.AddRange(rettmp);
|
ret.AddRange(rettmp);
|
||||||
}
|
}
|
||||||
@ -87,15 +125,18 @@ namespace FreeSql.QuestDb.Curd
|
|||||||
caseWhen.Append(_commonUtils.RereadColumn(pk, _commonUtils.QuoteSqlName(pk.Attribute.Name)));
|
caseWhen.Append(_commonUtils.RereadColumn(pk, _commonUtils.QuoteSqlName(pk.Attribute.Name)));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
caseWhen.Append("(");
|
caseWhen.Append("(");
|
||||||
var pkidx = 0;
|
var pkidx = 0;
|
||||||
foreach (var pk in primarys)
|
foreach (var pk in primarys)
|
||||||
{
|
{
|
||||||
if (pkidx > 0) caseWhen.Append(" || '+' || ");
|
if (pkidx > 0) caseWhen.Append(" || '+' || ");
|
||||||
if (string.IsNullOrEmpty(InternalTableAlias) == false) caseWhen.Append(InternalTableAlias).Append(".");
|
if (string.IsNullOrEmpty(InternalTableAlias) == false) caseWhen.Append(InternalTableAlias).Append(".");
|
||||||
caseWhen.Append(_commonUtils.RereadColumn(pk, _commonUtils.QuoteSqlName(pk.Attribute.Name))).Append("::text");
|
caseWhen.Append(_commonUtils.RereadColumn(pk, _commonUtils.QuoteSqlName(pk.Attribute.Name)))
|
||||||
|
.Append("::text");
|
||||||
++pkidx;
|
++pkidx;
|
||||||
}
|
}
|
||||||
|
|
||||||
caseWhen.Append(")");
|
caseWhen.Append(")");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,6 +147,7 @@ namespace FreeSql.QuestDb.Curd
|
|||||||
sb.Append(_commonUtils.FormatSql("{0}", primarys[0].GetDbValue(d)));
|
sb.Append(_commonUtils.FormatSql("{0}", primarys[0].GetDbValue(d)));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
sb.Append("(");
|
sb.Append("(");
|
||||||
var pkidx = 0;
|
var pkidx = 0;
|
||||||
foreach (var pk in primarys)
|
foreach (var pk in primarys)
|
||||||
@ -114,6 +156,7 @@ namespace FreeSql.QuestDb.Curd
|
|||||||
sb.Append(_commonUtils.FormatSql("{0}", pk.GetDbValue(d))).Append("::text");
|
sb.Append(_commonUtils.FormatSql("{0}", pk.GetDbValue(d))).Append("::text");
|
||||||
++pkidx;
|
++pkidx;
|
||||||
}
|
}
|
||||||
|
|
||||||
sb.Append(")");
|
sb.Append(")");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,6 +168,7 @@ namespace FreeSql.QuestDb.Curd
|
|||||||
sb.Append("::text");
|
sb.Append("::text");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var dbtype = _commonUtils.CodeFirst.GetDbInfo(col.Attribute.MapType)?.dbtype;
|
var dbtype = _commonUtils.CodeFirst.GetDbInfo(col.Attribute.MapType)?.dbtype;
|
||||||
if (dbtype == null) return;
|
if (dbtype == null) return;
|
||||||
|
|
||||||
@ -133,9 +177,22 @@ namespace FreeSql.QuestDb.Curd
|
|||||||
|
|
||||||
#if net40
|
#if net40
|
||||||
#else
|
#else
|
||||||
public override Task<int> ExecuteAffrowsAsync(CancellationToken cancellationToken = default) => base.SplitExecuteAffrowsAsync(_batchRowsLimit > 0 ? _batchRowsLimit : 500, _batchParameterLimit > 0 ? _batchParameterLimit : 3000, cancellationToken);
|
public override Task<int> ExecuteAffrowsAsync(CancellationToken cancellationToken = default)
|
||||||
public override Task<List<T1>> ExecuteUpdatedAsync(CancellationToken cancellationToken = default) => base.SplitExecuteUpdatedAsync(_batchRowsLimit > 0 ? _batchRowsLimit : 500, _batchParameterLimit > 0 ? _batchParameterLimit : 3000, cancellationToken);
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(RestAPIExtension.BaseUrl))
|
||||||
|
{
|
||||||
|
return base.SplitExecuteAffrowsAsync(_batchRowsLimit > 0 ? _batchRowsLimit : 500,
|
||||||
|
_batchParameterLimit > 0 ? _batchParameterLimit : 3000, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Task.FromResult(InternelExecuteAffrows());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public override Task<List<T1>> ExecuteUpdatedAsync(CancellationToken cancellationToken = default) =>
|
||||||
|
base.SplitExecuteUpdatedAsync(_batchRowsLimit > 0 ? _batchRowsLimit : 500,
|
||||||
|
_batchParameterLimit > 0 ? _batchParameterLimit : 3000, cancellationToken);
|
||||||
|
|
||||||
async protected override Task<List<T1>> RawExecuteUpdatedAsync(CancellationToken cancellationToken = default)
|
async protected override Task<List<T1>> RawExecuteUpdatedAsync(CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var ret = new List<T1>();
|
var ret = new List<T1>();
|
||||||
@ -153,10 +210,12 @@ namespace FreeSql.QuestDb.Curd
|
|||||||
foreach (var col in _table.Columns.Values)
|
foreach (var col in _table.Columns.Values)
|
||||||
{
|
{
|
||||||
if (colidx > 0) sbret.Append(", ");
|
if (colidx > 0) sbret.Append(", ");
|
||||||
sbret.Append(_commonUtils.RereadColumn(col, _commonUtils.QuoteSqlName(col.Attribute.Name))).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
|
sbret.Append(_commonUtils.RereadColumn(col, _commonUtils.QuoteSqlName(col.Attribute.Name)))
|
||||||
|
.Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
|
||||||
++colidx;
|
++colidx;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var sql = sb.Append(sbret).ToString();
|
var sql = sb.Append(sbret).ToString();
|
||||||
var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Update, sql, dbParms);
|
var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Update, sql, dbParms);
|
||||||
_orm.Aop.CurdBeforeHandler?.Invoke(this, before);
|
_orm.Aop.CurdBeforeHandler?.Invoke(this, before);
|
||||||
@ -164,7 +223,8 @@ namespace FreeSql.QuestDb.Curd
|
|||||||
Exception exception = null;
|
Exception exception = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var rettmp = await _orm.Ado.QueryAsync<T1>(_table.TypeLazy ?? _table.Type, _connection, _transaction, CommandType.Text, sql, _commandTimeout, dbParms, cancellationToken);
|
var rettmp = await _orm.Ado.QueryAsync<T1>(_table.TypeLazy ?? _table.Type, _connection,
|
||||||
|
_transaction, CommandType.Text, sql, _commandTimeout, dbParms, cancellationToken);
|
||||||
ValidateVersionAndThrow(rettmp.Count, sql, dbParms);
|
ValidateVersionAndThrow(rettmp.Count, sql, dbParms);
|
||||||
ret.AddRange(rettmp);
|
ret.AddRange(rettmp);
|
||||||
}
|
}
|
||||||
@ -184,4 +244,4 @@ namespace FreeSql.QuestDb.Curd
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user