コード例 #1
0
        public RawQuery GetRawQuery()
        {
            RawQuery rq = new RawQuery();

            rq.Append("UPDATE ")
            .AppendTableName(Table.Name, Table.Schema)
            .Append("\n");

            rq.Append("SET ");
            bool first = true;

            foreach (var(column, typedExpression) in _updateStatementsBuilder.UpdateParts)
            {
                if (!first)
                {
                    rq.Append(", ");
                }

                rq.AppendColumnName(column.Name)
                .Append(" = ")
                .Append(typedExpression.RawQuery);

                first = false;
            }

            rq.Append("\n");

            var whereRawQuery = _whereBuilder.GetRawQuery();

            if (!whereRawQuery.IsEmpty)
            {
                rq.Append("WHERE ")
                .Append(whereRawQuery);
            }

            rq.SkipExplicitColumnTableNames();
            return(rq);
        }
コード例 #2
0
ファイル: InsertQuery.cs プロジェクト: KrystianD/KDPgDriver
        public RawQuery GetRawQuery()
        {
            if (IsEmpty)
            {
                return(RawQuery.Create("SELECT 0"));
            }

            RawQuery rq = new RawQuery();

            var columns = _columns.Count == 0 ? AllColumnsWithoutAutoIncrement : _columns;

            rq.Append("INSERT INTO ");

            rq.AppendTableName(Table.Name, Table.Schema);

            rq.Append("(");
            rq.AppendColumnNames(columns.Select(x => x.columnDescriptor.Name));
            if (_idColumn != null)
            {
                if (columns.Count > 0)
                {
                    rq.Append(",");
                }
                rq.AppendColumnName(_idColumn.Name);
            }

            rq.Append(")");

            rq.Append(" VALUES ");

            var first = true;

            foreach (var obj in _objects)
            {
                if (!first)
                {
                    rq.Append(",");
                }
                rq.Append("(");

                for (int i = 0; i < columns.Count; i++)
                {
                    var column = columns[i];

                    if (i > 0)
                    {
                        rq.Append(",");
                    }

                    if (column.subquery == null)
                    {
                        object val      = ModelsRegistry.GetModelValueByColumn(obj, column.columnDescriptor);
                        var    npgValue = PgTypesConverter.ConvertToPgValue(column.columnDescriptor.Type, val);
                        rq.Append(npgValue);
                    }
                    else
                    {
                        rq.AppendSurround(column.subquery.GetRawQuery());
                    }
                }

                if (_idColumn != null)
                {
                    if (columns.Count > 0)
                    {
                        rq.Append(",");
                    }

                    rq.Append(ExpressionBuilders.CurrSeqValueOfTable(_idRefColumn).RawQuery);
                }

                rq.Append(")");
                first = false;
            }

            if (_onInsertConflict == OnInsertConflict.DoNothing)
            {
                rq.Append(" ON CONFLICT DO NOTHING ");
            }

            if (_onInsertConflict == OnInsertConflict.DoUpdate)
            {
                rq.Append(" ON CONFLICT (");

                var fields = new FieldListBuilder <TModel>();
                _onInsertConflictUpdateFields(fields);
                first = true;
                foreach (var column in fields.Fields)
                {
                    if (!first)
                    {
                        rq.Append(", ");
                    }

                    rq.AppendColumnName(column.Name);

                    first = false;
                }

                rq.Append(") DO UPDATE SET ");

                var updateStatementsBuilder = new UpdateStatementsBuilder <TModel>();
                _onInsertConflictUpdate(updateStatementsBuilder);

                first = true;
                foreach (var(column, typedExpression) in updateStatementsBuilder.UpdateParts)
                {
                    if (!first)
                    {
                        rq.Append(", ");
                    }

                    rq.AppendColumnName(column.Name)
                    .Append(" = ")
                    .Append(typedExpression.RawQuery);

                    first = false;
                }
            }

            if (TableModel.PrimaryKey != null)
            {
                rq.Append(" RETURNING ");
                rq.AppendColumnName(TableModel.PrimaryKey.Name);
            }

            rq.Append(";");

            if (_outputVariable != null)
            {
                rq.Append(" SELECT ");
                rq.Append(ExpressionBuilders.SetConfigText(_outputVariable, ExpressionBuilders.LastVal(), true).RawQuery);
            }

            rq.SkipExplicitColumnTableNames();
            return(rq);
        }