Пример #1
0
        public override void ExplicitVisit(DefaultConstraintDefinition node)
        {
            _buffer.Append("\talter column ");
            node.Column.Accept(this);
            _buffer.Append(" set default ");

            ScalarExpression expression = node.Expression;

            while (expression is ParenthesisExpression)
            {
                expression = ((ParenthesisExpression)expression).Expression;
            }

            string        columnName = node.Column.Value;
            PgTableColumn column     = _table.Columns[columnName];

            if (expression is IntegerLiteral literal)
            {
                string value = literal.Value;
                if (column.DataType == "bool")
                {
                    if (value == "0")
                    {
                        value = "(\'f\')";
                    }
                    else
                    {
                        value = "(\'t\')";
                    }
                    _buffer.Append(value);
                    return;
                }
            }
            else if (expression is FunctionCall call)
            {
                string functionName = call.FunctionName.Value.ToLower();
                switch (functionName)
                {
                case "getutcdate":
                    _buffer.Append("(current_timestamp at time zone 'UTC')");
                    return;

                case "getdate":
                    _buffer.Append("(localtimestamp)");
                    return;

                case "suser_sname":
                    _buffer.Append("(session_user)");
                    return;

                default:
                    throw new InvalidOperationException($"unsupported function {functionName}");
                }
            }
            _buffer.Append("(");
            expression.Accept(this);
            _buffer.Append(")");
        }
        public override void ExplicitVisit(CreateTableStatement node)
        {
            _buffer.Append("create table ");
            node.SchemaObjectName.Accept(this);
            _table.Name = PgTranslatorVisitor.GetTableName(node.SchemaObjectName);

            _buffer.AppendLine(" (");
            bool needComma = false;

            foreach (ColumnDefinition columnDefinition in node.Definition.ColumnDefinitions)
            {
                if (needComma)
                {
                    _buffer.AppendLine(",");
                }

                _column = new PgTableColumn();
                if (columnDefinition.IdentityOptions != null)
                {
                    _column.IsIdentity = true;
                }

                columnDefinition.Accept(this);
                _table.Columns.Add(_column);
                _column = null;

                needComma = true;
            }
            foreach (ConstraintDefinition constraint in node.Definition.TableConstraints)
            {
                if (needComma)
                {
                    _buffer.AppendLine(",");
                }

                _buffer.Append("\t");
                if (constraint is UniqueConstraintDefinition unique)
                {
                    unique.Accept(new PgExpressionVisitor(_buffer));
                }
                else if (constraint is ForeignKeyConstraintDefinition foreignKey)
                {
                    foreignKey.Accept(new PgExpressionVisitor(_buffer));
                }
                else if (constraint is CheckConstraintDefinition check)
                {
                    check.Accept(new PgExpressionVisitor(_buffer));
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }
            _buffer.AppendLine();
            _buffer.AppendLine(");");
            _buffer.AppendLine();
        }