protected override List <T1> RawExecuteUpdated() { var sql = this.ToSql(); if (string.IsNullOrEmpty(sql)) { return(new List <T1>()); } var sb = new StringBuilder(); sb.Append(sql).Append(" RETURNING "); var colidx = 0; foreach (var col in _table.Columns.Values) { if (colidx > 0) { sb.Append(", "); } sb.Append(_commonUtils.QuoteReadColumn(col.Attribute.MapType, _commonUtils.QuoteSqlName(col.Attribute.Name))).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName)); ++colidx; } sql = sb.ToString(); var dbParms = _params.Concat(_paramsSource).ToArray(); var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Update, sql, dbParms); _orm.Aop.CurdBefore?.Invoke(this, before); var ret = new List <T1>(); Exception exception = null; try { ret = _orm.Ado.Query <T1>(_connection, _transaction, CommandType.Text, sql, dbParms); ValidateVersionAndThrow(ret.Count); } catch (Exception ex) { exception = ex; throw ex; } finally { var after = new Aop.CurdAfterEventArgs(before, exception, ret); _orm.Aop.CurdAfter?.Invoke(this, after); } return(ret); }
protected override long RawExecuteIdentity() { var sql = this.ToSql(); if (string.IsNullOrEmpty(sql)) { return(0); } var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Insert, string.Concat(sql, "; SELECT @@identity;"), _params); _orm.Aop.CurdBeforeHandler?.Invoke(this, before); long ret = 0; Exception exception = null; var isUseConnection = _connection != null; try { if (isUseConnection == false) { using (var conn = _orm.Ado.MasterPool.Get()) { _connection = conn.Value; _orm.Ado.ExecuteNonQuery(_connection, _transaction, CommandType.Text, sql, _params); long.TryParse(string.Concat(_orm.Ado.ExecuteScalar(_connection, _transaction, CommandType.Text, "SELECT @@identity", _params)), out ret); } } else { _orm.Ado.ExecuteNonQuery(_connection, _transaction, CommandType.Text, sql, _params); long.TryParse(string.Concat(_orm.Ado.ExecuteScalar(_connection, _transaction, CommandType.Text, "SELECT @@identity", _params)), out ret); } } catch (Exception ex) { exception = ex; throw ex; } finally { if (isUseConnection == false) { _connection = null; } var after = new Aop.CurdAfterEventArgs(before, exception, ret); _orm.Aop.CurdAfterHandler?.Invoke(this, after); } return(ret); }
async protected override Task <long> RawExecuteIdentityAsync() { var sql = this.ToSql(); if (string.IsNullOrEmpty(sql)) { return(0); } Object <DbConnection> poolConn = null; var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Insert, string.Concat(sql, $"; {_utils.Adapter.InsertAfterGetIdentitySql}"), _params); _orm.Aop.CurdBeforeHandler?.Invoke(this, before); long ret = 0; Exception exception = null; try { var conn = _connection; if (_transaction != null) { conn = _transaction.Connection; } if (conn == null) { poolConn = _orm.Ado.MasterPool.Get(); conn = poolConn.Value; } await _orm.Ado.ExecuteNonQueryAsync(conn, _transaction, CommandType.Text, sql, _params); ret = long.TryParse(string.Concat(await _orm.Ado.ExecuteScalarAsync(conn, _transaction, CommandType.Text, $" {_utils.Adapter.InsertAfterGetIdentitySql}")), out var trylng) ? trylng : 0; } catch (Exception ex) { exception = ex; throw ex; } finally { if (poolConn != null) { _orm.Ado.MasterPool.Return(poolConn); } var after = new Aop.CurdAfterEventArgs(before, exception, ret); _orm.Aop.CurdAfterHandler?.Invoke(this, after); } return(ret); }
async public override Task <List <T1> > ExecuteDeletedAsync(CancellationToken cancellationToken = default) { var sql = this.ToSql(); if (string.IsNullOrEmpty(sql)) { return(new List <T1>()); } var sb = new StringBuilder(); sb.Append(sql).Append(" RETURNING "); var colidx = 0; foreach (var col in _table.Columns.Values) { if (colidx > 0) { sb.Append(", "); } sb.Append(_commonUtils.QuoteReadColumn(col.CsType, col.Attribute.MapType, _commonUtils.QuoteSqlName(col.Attribute.Name))).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName)); ++colidx; } sql = sb.ToString(); var dbParms = _params.ToArray(); var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Delete, sql, dbParms); _orm.Aop.CurdBeforeHandler?.Invoke(this, before); var ret = new List <T1>(); Exception exception = null; try { ret = await _orm.Ado.QueryAsync <T1>(_table.TypeLazy ?? _table.Type, _connection, _transaction, CommandType.Text, sql, _commandTimeout, dbParms, cancellationToken); } catch (Exception ex) { exception = ex; throw ex; } finally { var after = new Aop.CurdAfterEventArgs(before, exception, ret); _orm.Aop.CurdAfterHandler?.Invoke(this, after); } this.ClearData(); return(ret); }
protected override long RawExecuteIdentity() { var sql = this.ToSql(); if (string.IsNullOrEmpty(sql)) { return(0); } Object <DbConnection> poolConn = null; var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Insert, string.Concat(sql, "; SELECT LAST_INSERT_ID();", _commandTimeout), _params); _orm.Aop.CurdBeforeHandler?.Invoke(this, before); long ret = 0; Exception exception = null; try { var conn = _connection; if (_transaction != null) { conn = _transaction.Connection; } if (conn == null) { poolConn = _orm.Ado.MasterPool.Get(); conn = poolConn.Value; } _orm.Ado.ExecuteNonQuery(conn, _transaction, CommandType.Text, sql, _commandTimeout, _params); ret = long.TryParse(string.Concat(_orm.Ado.ExecuteScalar(conn, _transaction, CommandType.Text, " SELECT LAST_INSERT_ID()", _commandTimeout)), out var trylng) ? trylng : 0; } catch (Exception ex) { exception = ex; throw ex; } finally { if (poolConn != null) { _orm.Ado.MasterPool.Return(poolConn); } var after = new Aop.CurdAfterEventArgs(before, exception, ret); _orm.Aop.CurdAfterHandler?.Invoke(this, after); } return(ret); }
async protected override Task <List <T1> > RawExecuteInsertedAsync() { var sql = this.ToSql(); if (string.IsNullOrEmpty(sql)) { return(new List <T1>()); } var sb = new StringBuilder(); sb.Append(sql).Append(" RETURNING "); var colidx = 0; foreach (var col in _table.Columns.Values) { if (colidx > 0) { sb.Append(", "); } sb.Append(_commonUtils.QuoteReadColumn(col.Attribute.MapType, _commonUtils.QuoteSqlName(col.Attribute.Name))).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName)); ++colidx; } sql = sb.ToString(); var before = new Aop.CurdBeforeEventArgs(_table.Type, Aop.CurdType.Insert, sql, _params); _orm.Aop.CurdBefore?.Invoke(this, before); var ret = new List <T1>(); Exception exception = null; try { ret = await _orm.Ado.QueryAsync <T1>(_connection, _transaction, CommandType.Text, sql, _params); } catch (Exception ex) { exception = ex; throw ex; } finally { var after = new Aop.CurdAfterEventArgs(before, exception, ret); _orm.Aop.CurdAfter?.Invoke(this, after); } this.ClearData(); return(ret); }
async protected override Task <long> RawExecuteIdentityAsync() { var sql = this.ToSql(); if (string.IsNullOrEmpty(sql)) { return(0); } long ret = 0; Exception exception = null; Aop.CurdBeforeEventArgs before = null; var identCols = _table.Columns.Where(a => a.Value.Attribute.IsIdentity == true); if (identCols.Any() == false) { before = new Aop.CurdBeforeEventArgs(_table.Type, Aop.CurdType.Insert, sql, _params); _orm.Aop.CurdBefore?.Invoke(this, before); try { ret = await _orm.Ado.ExecuteNonQueryAsync(_connection, _transaction, CommandType.Text, sql, _params); } catch (Exception ex) { exception = ex; throw ex; } finally { var after = new Aop.CurdAfterEventArgs(before, exception, ret); _orm.Aop.CurdAfter?.Invoke(this, after); } this.ClearData(); return(0); } sql = string.Concat(sql, " RETURNING ", _commonUtils.QuoteSqlName(identCols.First().Value.Attribute.Name)); before = new Aop.CurdBeforeEventArgs(_table.Type, Aop.CurdType.Insert, sql, _params); _orm.Aop.CurdBefore?.Invoke(this, before); try { long.TryParse(string.Concat(await _orm.Ado.ExecuteScalarAsync(_connection, _transaction, CommandType.Text, sql, _params)), out ret); } catch (Exception ex) { exception = ex; throw ex; } finally { var after = new Aop.CurdAfterEventArgs(before, exception, ret); _orm.Aop.CurdAfter?.Invoke(this, after); } this.ClearData(); return(ret); }
async protected override Task<long> RawExecuteIdentityAsync() { var sql = this.ToSql(); if (string.IsNullOrEmpty(sql)) return 0; sql = string.Concat(sql, "; SELECT last_insert_rowid();"); var before = new Aop.CurdBeforeEventArgs(_table.Type, Aop.CurdType.Insert, sql, _params); _orm.Aop.CurdBefore?.Invoke(this, before); long ret = 0; Exception exception = null; try { long.TryParse(string.Concat(await _orm.Ado.ExecuteScalarAsync(_connection, _transaction, CommandType.Text, sql, _params)), out ret); } catch (Exception ex) { exception = ex; throw ex; } finally { var after = new Aop.CurdAfterEventArgs(before, exception, ret); _orm.Aop.CurdAfter?.Invoke(this, after); } this.ClearData(); return ret; }
async protected Task <int> RawExecuteAffrowsAsync() { var sql = ToSql(); var before = new Aop.CurdBeforeEventArgs(_table.Type, Aop.CurdType.Insert, sql, _params); _orm.Aop.CurdBefore?.Invoke(this, before); var affrows = 0; Exception exception = null; try { affrows = await _orm.Ado.ExecuteNonQueryAsync(_connection, _transaction, CommandType.Text, sql, _params); } catch (Exception ex) { exception = ex; throw ex; } finally { var after = new Aop.CurdAfterEventArgs(before, exception, affrows); _orm.Aop.CurdAfter?.Invoke(this, after); } this.ClearData(); return(affrows); }
async public virtual Task <int> ExecuteAffrowsAsync(CancellationToken cancellationToken = default) { var affrows = 0; DbParameter[] dbParms = null; await ToSqlFetchAsync(async sb => { if (dbParms == null) { dbParms = _params.ToArray(); } var sql = sb.ToString(); var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Delete, sql, dbParms); _orm.Aop.CurdBeforeHandler?.Invoke(this, before); Exception exception = null; try { affrows += await _orm.Ado.ExecuteNonQueryAsync(_connection, _transaction, CommandType.Text, sql, _commandTimeout, dbParms, cancellationToken); } catch (Exception ex) { exception = ex; throw; } finally { var after = new Aop.CurdAfterEventArgs(before, exception, affrows); _orm.Aop.CurdAfterHandler?.Invoke(this, after); } }); if (dbParms != null) { this.ClearData(); } return(affrows); }
protected override int RawExecuteAffrows() { var affrows = 0; Exception exception = null; Aop.CurdBeforeEventArgs before = null; if (_source.Count > 1) { try { before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Insert, null, _params); _orm.Aop.CurdBeforeHandler?.Invoke(this, before); using (var conn = _orm.Ado.MasterPool.Get()) { using var bulkCopyInterface = new ClickHouseBulkCopy(conn.Value as ClickHouseConnection) { DestinationTableName = _table.DbName, BatchSize = _source.Count }; var data = ToDataTable(); bulkCopyInterface.WriteToServerAsync(data, default).Wait(); } return(affrows); } catch (Exception ex) { exception = ex; throw ex; } finally { var after = new Aop.CurdAfterEventArgs(before, exception, affrows); _orm.Aop.CurdAfterHandler?.Invoke(this, after); } } return(base.RawExecuteAffrows()); }
public override int ExecuteAffrows() { var affrows = 0; DbParameter[] dbParms = null; ToSqlFetch(sb => { if (dbParms == null) { dbParms = _params.ToArray(); } var sql = ReplaceDeleteSql(sb.ToString()); var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Delete, sql, dbParms); _orm.Aop.CurdBeforeHandler?.Invoke(this, before); Exception exception = null; try { affrows += _orm.Ado.ExecuteNonQuery(_connection, _transaction, CommandType.Text, sql, _commandTimeout, dbParms); } catch (Exception ex) { exception = ex; throw; } finally { var after = new Aop.CurdAfterEventArgs(before, exception, affrows); _orm.Aop.CurdAfterHandler?.Invoke(this, after); } }); if (dbParms != null) { this.ClearData(); } return(affrows); }
async protected Task <int> RawExecuteAffrowsAsync(CancellationToken cancellationToken = default) { var affrows = 0; DbParameter[] dbParms = null; await ToSqlFetchAsync(async sb => { if (dbParms == null) { dbParms = _params.Concat(_paramsSource).ToArray(); } var sql = sb.ToString(); var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Update, sql, dbParms); _orm.Aop.CurdBeforeHandler?.Invoke(this, before); Exception exception = null; try { var affrowstmp = await _orm.Ado.ExecuteNonQueryAsync(_connection, _transaction, CommandType.Text, sql, _commandTimeout, dbParms, cancellationToken); ValidateVersionAndThrow(affrowstmp, sql, dbParms); affrows += affrowstmp; } catch (Exception ex) { exception = ex; throw; } finally { var after = new Aop.CurdAfterEventArgs(before, exception, affrows); _orm.Aop.CurdAfterHandler?.Invoke(this, after); } }); return(affrows); }
async public override Task <List <T1> > ExecuteDeletedAsync(CancellationToken cancellationToken = default) { var ret = new List <T1>(); DbParameter[] dbParms = null; StringBuilder sbret = null; await ToSqlFetchAsync(async sb => { if (dbParms == null) { dbParms = _params.ToArray(); sbret = new StringBuilder(); sbret.Append(" OUTPUT "); var colidx = 0; foreach (var col in _table.Columns.Values) { if (colidx > 0) { sbret.Append(", "); } sbret.Append(_commonUtils.RereadColumn(col, $"DELETED.{_commonUtils.QuoteSqlName(col.Attribute.Name)}")).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName)); ++colidx; } } var sql = sb.ToString(); var validx = sql.IndexOf(" WHERE "); if (validx == -1) { throw new ArgumentException("找不到 WHERE "); } sql = sb.Clear().Append(sql.Substring(0, validx)) .Append(sbret) .Append(sql.Substring(validx)).ToString(); var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Delete, sql, dbParms); _orm.Aop.CurdBeforeHandler?.Invoke(this, before); Exception exception = null; try { ret.AddRange(await _orm.Ado.QueryAsync <T1>(_table.TypeLazy ?? _table.Type, _connection, _transaction, CommandType.Text, sql, _commandTimeout, dbParms, cancellationToken)); } catch (Exception ex) { exception = ex; throw ex; } finally { var after = new Aop.CurdAfterEventArgs(before, exception, ret); _orm.Aop.CurdAfterHandler?.Invoke(this, after); } }); if (dbParms != null) { this.ClearData(); sbret.Clear(); } return(ret); }
protected override List <T1> RawExecuteInserted() { var sql = this.ToSql(); if (string.IsNullOrEmpty(sql)) { return(new List <T1>()); } var sb = new StringBuilder(); sb.Append(" OUTPUT "); var colidx = 0; foreach (var col in _table.Columns.Values) { if (colidx > 0) { sb.Append(", "); } sb.Append(_commonUtils.QuoteReadColumn(col.CsType, col.Attribute.MapType, $"INSERTED.{_commonUtils.QuoteSqlName(col.Attribute.Name)}")).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName)); ++colidx; } if ((_commonUtils as SqlServerUtils).ServerVersion > 10) { var validx = sql.IndexOf(") VALUES"); if (validx == -1) { throw new ArgumentException("找不到 VALUES"); } sb.Insert(0, sql.Substring(0, validx + 1)); sb.Append(sql.Substring(validx + 1)); } else { var validx = sql.IndexOf(") SELECT "); if (validx == -1) { throw new ArgumentException("找不到 SELECT"); } sb.Insert(0, sql.Substring(0, validx + 1)); sb.Append(sql.Substring(validx + 1)); } sql = sb.ToString(); var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Insert, sql, _params); _orm.Aop.CurdBeforeHandler?.Invoke(this, before); var ret = new List <T1>(); Exception exception = null; try { ret = _orm.Ado.Query <T1>(_table.TypeLazy ?? _table.Type, _connection, _transaction, CommandType.Text, sql, _commandTimeout, _params); } catch (Exception ex) { exception = ex; throw; } finally { var after = new Aop.CurdAfterEventArgs(before, exception, ret); _orm.Aop.CurdAfterHandler?.Invoke(this, after); } return(ret); }
async protected override Task <long> RawExecuteIdentityAsync() { var sql = this.ToSql(); if (string.IsNullOrEmpty(sql)) { return(0); } long ret = 0; Exception exception = null; Aop.CurdBeforeEventArgs before = null; if (_identCol == null || _source.Count > 1) { before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Insert, sql, _params); _orm.Aop.CurdBefore?.Invoke(this, before); try { ret = await _orm.Ado.ExecuteNonQueryAsync(_connection, _transaction, CommandType.Text, sql, _params); } catch (Exception ex) { exception = ex; throw ex; } finally { var after = new Aop.CurdAfterEventArgs(before, exception, ret); _orm.Aop.CurdAfter?.Invoke(this, after); } return(0); } var identColName = _commonUtils.QuoteSqlName(_identCol.Attribute.Name); var identParam = _commonUtils.AppendParamter(null, $"{_identCol.CsName}99", _identCol, _identCol.Attribute.MapType, 0) as OracleParameter; identParam.Direction = ParameterDirection.Output; sql = $"{sql} RETURNING {identColName} INTO {identParam.ParameterName}"; var dbParms = _params.Concat(new[] { identParam }).ToArray(); before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Insert, sql, dbParms); _orm.Aop.CurdBefore?.Invoke(this, before); try { await _orm.Ado.ExecuteNonQueryAsync(_connection, _transaction, CommandType.Text, sql, dbParms); long.TryParse(string.Concat(identParam.Value), out ret); } catch (Exception ex) { exception = ex; throw ex; } finally { var after = new Aop.CurdAfterEventArgs(before, exception, ret); _orm.Aop.CurdAfter?.Invoke(this, after); } return(ret); }
protected override long RawExecuteIdentity() { var sql = this.ToSql(); if (string.IsNullOrEmpty(sql)) { return(0); } var identityType = _table.Primarys.Where(a => a.Attribute.IsIdentity).FirstOrDefault()?.CsType.NullableTypeOrThis(); var identitySql = ""; if (identityType != null) { if (identityType == typeof(int) || identityType == typeof(uint)) { identitySql = "SELECT dbinfo('sqlca.sqlerrd1') FROM dual"; } else if (identityType == typeof(long) || identityType == typeof(ulong)) { identitySql = "SELECT dbinfo('serial8') FROM dual"; } } var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Insert, string.Concat(sql, $"; {identitySql};"), _params); _orm.Aop.CurdBeforeHandler?.Invoke(this, before); long ret = 0; Exception exception = null; var isUseConnection = _connection != null; try { if (isUseConnection == false) { using (var conn = _orm.Ado.MasterPool.Get()) { _connection = conn.Value; _orm.Ado.ExecuteNonQuery(_connection, _transaction, CommandType.Text, sql, _commandTimeout, _params); if (string.IsNullOrWhiteSpace(identitySql) == false) { long.TryParse(string.Concat(_orm.Ado.ExecuteScalar(_connection, _transaction, CommandType.Text, identitySql, _commandTimeout, _params)), out ret); } } } else { _orm.Ado.ExecuteNonQuery(_connection, _transaction, CommandType.Text, sql, _commandTimeout, _params); if (string.IsNullOrWhiteSpace(identitySql) == false) { long.TryParse(string.Concat(_orm.Ado.ExecuteScalar(_connection, _transaction, CommandType.Text, identitySql, _commandTimeout, _params)), out ret); } } } catch (Exception ex) { exception = ex; throw ex; } finally { if (isUseConnection == false) { _connection = null; } var after = new Aop.CurdAfterEventArgs(before, exception, ret); _orm.Aop.CurdAfterHandler?.Invoke(this, after); } return(ret); }