public static string GetColumnName(this ColumnDeclarationContext context)
        {
            var uidContext = context.GetChild <UidContext>();
            var name       = uidContext.GetText().RemoveBackQuote();

            return(name);
        }
예제 #2
0
        public static MySqlColumnDefinition Extract(ColumnDeclarationContext context)
        {
            if (context == null)
            {
                return(null);
            }

            var column = new MySqlColumnDefinition
            {
                // column name
                Name = context.GetColumnName()
            };

            // check column definitions
            var definitionContext = context.GetChild <ColumnDefinitionContext>();

            if (definitionContext == null)
            {
                return(null);
            }

            // BITINT(20): DimensionDataTypeContext
            column.Data = new MySqlColumnDataDefinition();
            (column.Data.DataType, column.Data.Length, column.Data.IsUnsigned) = definitionContext.ExtractColumnDataDefinition();

            // NOTNULL: NullColumnConstraintContext
            var notnull = definitionContext.GetChild <NullColumnConstraintContext>();

            column.Data.IsNullable = notnull != null && notnull.GetText() == "NULL"; // ChildCount = 1 not proove null. string compare is guaranteed.

            // AUTOINCREMENT: AutoIncrementColumnConstraintContext
            var autoincrement = definitionContext.GetChild <AutoIncrementColumnConstraintContext>();

            column.AutoIncrement = autoincrement != null;

            // DEFAULt'0': DefaultColumnConstraintContext
            var defaultValue = definitionContext.GetChild <DefaultColumnConstraintContext>();

            column.HasDefault   = defaultValue != null;
            column.DefaultValue = defaultValue?.GetText()?.RemoveDefaultString();

            //MEMO: GeneratedColumnContext will done via Listener
            //var generatedColumnConstraintContext = definitionContext.GetChild<GeneratedColumnConstraintContext>();
            //columnDefinition.GeneratedColumn = GeneratedColumnDefinition.ExtractGeneratedColumnDefinition(generatedColumnConstraintContext);

            //MEMO: ReferenceColumnContext will done via Listener

            // Special overwrite for SERIAL DataType.
            // SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE.
            if (column.Data.DataType == "SERIAL")
            {
                column.Data.IsUnsigned = true;
                column.Data.IsNullable = false;
                column.AutoIncrement   = true;
                //MEMO: No action for UniqueKey. It's too much special. (should add unique key clause on sql)
            }

            return(column);
        }