Example #1
0
        internal ColumnDefNode CreateColumnDefNode()
        {
            var ret = new ColumnDefNode {
                ColumnName = this.ColumnName, ColumnConstraints = new List <ColumnConstraintNode>()
            };

            //dialect converts DbType.Int16-64 to "INT" not "INTEGER" and only INTEGER columns can be autoincremented. This fixes that.
            string correctType = this.IsIdentity ? "INTEGER" : this.DbType;

            ret.TypeNameNode = SQLiteParseVisitor.ParseString <TypeNameNode>(correctType, i => i.type_name());

            //not quite right but should work

            if (this.IsIdentity || this.IsPrimaryKey)
            {
                var primKey = new PrimaryKeyConstraintNode();
                if (this.IsIdentity)
                {
                    primKey.AutoIncrement = true;
                }
                ret.ColumnConstraints.Add(primKey);
            }

            if (this.Default != null)
            {
                ret.ColumnConstraints.Add(new DefaultConstraintNode {
                    Value = DbUtils.ConvertToSqlValue(Default)
                });
            }

            if (this.IsNotNull)
            {
                ret.ColumnConstraints.Add(new NotNullConstraintNode());
            }
            else if (this.Default == null && !this.IsPrimaryKey && !this.IsUnique)
            {
                ret.ColumnConstraints.Add(new DefaultConstraintNode {
                    Value = "NULL"
                });
            }

            if (this.IsUnique)
            {
                ret.ColumnConstraints.Add(new UniqueConstraintNode());
            }

            return(ret);
        }
Example #2
0
        public StringBuilder Visit(PrimaryKeyConstraintNode primaryKeyColumnConstraintNode)
        {
            var sb = new StringBuilder();

            sb.Append("PRIMARY KEY ");
            if (primaryKeyColumnConstraintNode.Order != null)
            {
                sb.Append(primaryKeyColumnConstraintNode.Order.Value.ToString());
                sb.Append(" ");
            }
            if (primaryKeyColumnConstraintNode.ConflictClause != null)
            {
                sb.Append(primaryKeyColumnConstraintNode.ConflictClause.Accept(this));
            }
            if (primaryKeyColumnConstraintNode.AutoIncrement)
            {
                sb.Append(" AUTOINCREMENT");
            }

            return(sb);
        }
Example #3
0
        public override SQLiteParseTreeNode VisitColumn_constraint__postfix(SQLiteParserSimpleParser.Column_constraint__postfixContext context)
        {
            if (context.NOT() != null)
            {
                return(new NotNullConstraintNode(context)
                {
                    ConflictClause = context.conflict_clause().Accept(this) as ConflictClauseNode
                });
            }
            if (context.PRIMARY() != null)
            {
                var ret = new PrimaryKeyConstraintNode(context);
                if (context.ASC() != null)
                {
                    ret.Order = SortOrder.Asc;
                }
                else if (context.DESC() != null)
                {
                    ret.Order = SortOrder.Desc;
                }

                ret.ConflictClause = context.conflict_clause().Accept(this) as ConflictClauseNode;

                if (context.AUTOINCREMENT() != null)
                {
                    ret.AutoIncrement = true;
                }

                return(ret);
            }
            if (context.UNIQUE() != null)
            {
                return(new UniqueConstraintNode(context)
                {
                    ConflictClause = context.conflict_clause().Accept(this) as ConflictClauseNode
                });
            }
            if (context.CHECK_C() != null)
            {
                return(new CheckConstraintNode(context)
                {
                    Expr = context.expr().GetText()
                });
            }
            if (context.DEFAULT() != null)
            {
                var ret = new DefaultConstraintNode(context);
                if (context.signed_number() != null)
                {
                    ret.Value = context.signed_number().GetText();
                }
                else if (context.literal_value() != null)
                {
                    ret.Value = context.literal_value().GetText();
                }

                else if (context.expr() != null)
                {
                    ret.Value = "( " + context.expr().GetText() + " )";
                }


                return(ret);
            }
            if (context.NULL() != null)
            {
                //it's a default null but sqlite allows it and says it's null
                var ret = new DefaultConstraintNode(context)
                {
                    Value = "NULL"
                };
                return(ret);
            }

            if (context.COLLATE() != null)
            {
                return(new CollateConstraintNode(context)
                {
                    CollationName = context.collation_name().GetText()
                });
            }
            if (context.foreign_key_clause() != null)
            {
                return(context.foreign_key_clause().Accept(this) as ColumnConstraintNode);
            }

            throw new Exception("we should never get here.");
        }