Beispiel #1
0
        public override SqlStatementKind BuildSql(StringBuilder b, SqlQueryContext context)
        {
            SqlStatementKind kind = Target.BuildSql(b, context);
            SqlStatementKind resultKind;

            switch (kind)
            {
            case SqlStatementKind.Select:
                b.Append(" WHERE ");
                resultKind = SqlStatementKind.SelectWhere;
                break;

            case SqlStatementKind.SelectGroupBy:
                b.Append(" HAVING ");
                resultKind = SqlStatementKind.SelectGroupByHaving;
                break;

            default:
                WrapSqlIntoNestedStatement(b, context);
                b.Append(" WHERE ");
                resultKind = SqlStatementKind.SelectWhere;
                break;
            }
            for (int i = 0; i < Conditions.Count; i++)
            {
                if (i > 0)
                {
                    b.Append(" AND ");
                }
                BuildSqlForCondition(b, context, Conditions[i]);
            }
            b.AppendLine();
            return(resultKind);
        }
Beispiel #2
0
        public override SqlStatementKind BuildSql(StringBuilder b, SqlQueryContext context)
        {
            SqlStatementKind kind = Target.BuildSql(b, context);

            if (kind == SqlStatementKind.SelectLimit)
            {
                WrapSqlIntoNestedStatement(b, context);
            }

            b.Append(" LIMIT " + Length);
            b.AppendLine(" OFFSET " + Start);

            return(SqlStatementKind.SelectLimit);
        }
Beispiel #3
0
        public override SqlStatementKind BuildSql(StringBuilder b, SqlQueryContext context)
        {
            SqlStatementKind kind = Target.BuildSql(b, context);

            if (kind == SqlStatementKind.SelectOrderBy)
            {
                WrapSqlIntoNestedStatement(b, context);
            }

            b.Append(" ORDER BY ");

            for (int i = 0; i < arguments.Count; i++)
            {
                StringWriter        w      = new StringWriter();
                ExpressionSqlWriter writer = new ExpressionSqlWriter(w, context, arguments[i].Argument.Parameters[0]);
                writer.Write(arguments[i].Argument.Body);

                if (i == 0)
                {
                    b.Append(w.ToString());
                }
                else
                {
                    b.Append(", " + w.ToString());
                }

                if (arguments[i].Descending)
                {
                    b.Append(" DESC");
                }
                else
                {
                    b.Append(" ASC");
                }
            }

            return(SqlStatementKind.SelectOrderBy);
        }
Beispiel #4
0
        public IRelationalCommand Build <TEntity>(SqlStatementKind statementKind, IEntityModel model, TEntity entity, IEntityMapping entityMapping)
        {
            switch (statementKind)
            {
            case SqlStatementKind.CreateTable:

                ISqlStatementBuilder <CreateTableStatement> statementBuilder = new CreateTableStatementBuilder(model, entityMapping);

                ISqlStatement statement = statementBuilder.BuildStatement();

                return(new RelationalCommand(statement.GetText(), null));

            case SqlStatementKind.Insert:

                ISqlStatementBuilder <InsertStatement> insertStatementBuilder = new InsertStatementBuilder(entity, entityMapping);
                InsertStatement insertStatement = insertStatementBuilder.BuildStatement();

                return(new RelationalCommand(insertStatement.GetText(), insertStatement.Parameters));

            case SqlStatementKind.Update:

                ISqlStatementBuilder <UpdateStatement> updateStatementBuilder = new UpdateStatementBuilder(entity, entityMapping);
                UpdateStatement updateStatement = updateStatementBuilder.BuildStatement();

                return(new RelationalCommand(updateStatement.GetText(), updateStatement.Parameters));

            case SqlStatementKind.Delete:

                ISqlStatementBuilder <DeleteStatement> deleteStatementBuilder = new DeleteStatementBuilder(entity, entityMapping);
                DeleteStatement deleteStatement = deleteStatementBuilder.BuildStatement();

                return(new RelationalCommand(deleteStatement.GetText(), deleteStatement.Parameters));

            default:

                throw new NotImplementedException();
            }
        }
Beispiel #5
0
        public int ExecuteNonQuery <TEntity>(SqlStatementKind statementKind, IEntityModel model, TEntity entity, IEntityMapping entityMapping) where TEntity : class
        {
            var command = _commandBuilder.Build(statementKind, model, entity, entityMapping);

            return(Connection.ExecuteNonQuery(command.Sql, command.Parameters.ToDictionary()));
        }