internal static SqlGenerator Delete(SqlSession sqlSession, DbSelectStatement statement) { var result = new SqlGenerator(sqlSession.SqlVersion); var sqlBuilder = result.SqlBuilder; var model = statement.Model; var selectList = statement.Select; var from = statement.From ?? model.GetDbTableClause(); var where = statement.Where; result.ModelAliasManager = SqlServer.ModelAliasManager.Create(from); sqlBuilder.Append("DELETE "); sqlBuilder.AppendLine(result.ModelAliasManager[model].ToQuotedIdentifier()); result.VisitingQueryStatement(); result.GenerateFromClause(from); if (where != null) { result.GenerateWhereClause(where); } result.VisitedQueryStatement(statement); return(result); }
protected SessionTransaction(SqlSession sqlSession, SqlTransaction sqlTransaction, string name) { SqlSession = sqlSession; SqlTransaction = sqlTransaction; _level = sqlSession.TransactionCount; _name = GetName(name); Transactions.Push(this); }
internal static SqlGenerator Select(SqlSession sqlSession, DbQueryStatement query) { return(sqlSession._sqlGeneratorCache.GetValue(query, (DbQueryStatement x) => { var result = new SqlGenerator(sqlSession.SqlVersion); x.Accept(result); return result; })); }
public static SessionTransaction Create(SqlSession sqlSession, IsolationLevel?isolation, string name) { if (sqlSession.TransactionCount == 0) { var connection = sqlSession.Connection; var transaction = isolation.HasValue ? connection.BeginTransaction(isolation.Value, name) : connection.BeginTransaction(name); return(new Transaction(sqlSession, transaction, name)); } else { return(new SavePoint(sqlSession, sqlSession.GetCurrentTransaction().SqlTransaction, name)); } }
public static DbSet <T> CreateJsonRowSet <T>(this SqlSession sqlSession, string json, string ordinalColumnName) where T : Model, new() { var _ = new T(); if (!string.IsNullOrEmpty(ordinalColumnName)) { var dataSetOrdinalColumn = new _Int32(); _.AddSystemColumn(dataSetOrdinalColumn, ordinalColumnName); } var sourceJsonParam = _String.Param(json).AsSqlNVarCharMax(); s_jsonRowSets.Add(_, sourceJsonParam); return(_.CreateDbTable(sqlSession, ModelAliasManager.GetDbAlias(_))); }
internal static SqlGenerator Update(SqlSession sqlSession, DbSelectStatement statement) { var result = new SqlGenerator(sqlSession.SqlVersion); var sqlBuilder = result.SqlBuilder; var model = statement.Model; var selectList = statement.Select; var from = statement.From ?? model.GetDbTableClause(); var where = statement.Where; result.ModelAliasManager = SqlServer.ModelAliasManager.Create(from); sqlBuilder.Append("UPDATE "); sqlBuilder.Append(result.ModelAliasManager[model].ToQuotedIdentifier()); sqlBuilder.AppendLine(" SET"); sqlBuilder.IndentLevel++; for (int i = 0; i < selectList.Count; i++) { var select = selectList[i]; sqlBuilder.Append(select.Target.DbColumnName.ToQuotedIdentifier()); sqlBuilder.Append(" = "); select.SourceExpression.Accept(result._expressionGenerator); if (i != selectList.Count - 1) { sqlBuilder.Append(','); } sqlBuilder.AppendLine(); } sqlBuilder.IndentLevel--; result.VisitingQueryStatement(); result.GenerateFromClause(from); if (where != null) { result.GenerateWhereClause(where); } result.VisitedQueryStatement(statement); return(result); }
internal static SqlGenerator Insert(SqlSession sqlSession, DbSelectStatement statement, IDbTable identityOutput) { var result = new SqlGenerator(sqlSession.SqlVersion); var model = statement.Model; var sqlBuilder = result.SqlBuilder; if (model.IsIdentitySuspended()) { SetIdentityInsert(sqlBuilder, model, true); } BuildInsertIntoClause(sqlBuilder, statement.Model, statement.Select); if (identityOutput != null) { var identityColumn = model.GetIdentity(false).Column; var identityOutputNewValue = ((IIdentityOutput)identityOutput.Model).NewValue; sqlBuilder.AppendLine(string.Format("OUTPUT INSERTED.{0} INTO {1} ({2})", identityColumn.DbColumnName.ToQuotedIdentifier(), identityOutput.Name.ToQuotedIdentifier(), identityOutputNewValue.DbColumnName.ToQuotedIdentifier())); } if (statement.Select == null) { statement.From.Accept(result); } else if (statement.Select.Count > 0) { statement.Accept(result); } if (model.IsIdentitySuspended()) { SetIdentityInsert(sqlBuilder, model, false); } return(result); }
internal static SqlGenerator InsertScalar(SqlSession sqlSession, DbSelectStatement statement, bool outputIdentity, out SqlParameter scopeIdentityParam) { var result = new SqlGenerator(sqlSession.SqlVersion); var model = statement.Model; var sqlBuilder = result.SqlBuilder; if (model.IsIdentitySuspended()) { SetIdentityInsert(sqlBuilder, model, true); } BuildInsertIntoClause(sqlBuilder, model, statement.Select); if (statement.Select.Count > 0) { statement.Accept(result); } if (model.IsIdentitySuspended()) { SetIdentityInsert(sqlBuilder, model, false); } if (outputIdentity) { var sqlType = SqlType.BigInt(new _Int64()); scopeIdentityParam = sqlType.CreateSqlParameter("@scopeIdentity", ParameterDirection.Output, null, sqlSession.SqlVersion); sqlBuilder.AppendLine(); sqlBuilder.Append("SET @scopeIdentity = CAST(SCOPE_IDENTITY() AS BIGINT);"); } else { scopeIdentityParam = null; } return(result); }
public SavePoint(SqlSession sqlSession, SqlTransaction sqlTransaction, string name) : base(sqlSession, sqlTransaction, name) { sqlTransaction.Save(Name); }
public Transaction(SqlSession sqlSession, SqlTransaction sqlTransaction, string name) : base(sqlSession, sqlTransaction, name) { }
internal SqlReaderInvoker(SqlSession sqlSession, Model model, SqlCommand sqlCommand) : base(sqlSession, model, sqlCommand) { }