Esempio n. 1
0
        private TableSchema ParseCreateTable(string sql)
        {
            var parser = new SimpleParser(sql);

            var tableSchema = new TableSchema();

            tableSchema.Columns = new List <ColumnSchema>();

            parser.SkipOverString("create table");
            tableSchema.TableName = parser.TakeWord();
            parser.SkipOverString("(");
            while (!parser.AtEnd() && !parser.Is(')'))
            {
                var column = new ColumnSchema();
                tableSchema.Columns.Add(column);

                column.ColumnName = parser.TakeWord();
                column.DataType   = parser.TakeWord();

                var attributes = parser.TakeToAny(new[] { ',', ')' }).ToUpper();
                if (attributes.Contains("PRIMARY KEY"))
                {
                    column.Attributes = column.Attributes | ColumnAttributes.Primary;
                }
                if (attributes.Contains("NOT NULL"))
                {
                    column.Attributes = column.Attributes | ColumnAttributes.NotNull;
                }
                if (attributes.Contains("UNIQUE"))
                {
                    column.Attributes = column.Attributes | ColumnAttributes.Unique;
                }
                if (attributes.Contains("AUTOINCREMENT"))
                {
                    column.Attributes = column.Attributes | ColumnAttributes.AutoIncrement;
                }
                if (string.Equals(column.DataType, "TEXT", StringComparison.OrdinalIgnoreCase) && !attributes.Contains("COLLATE NOCASE"))
                {
                    column.Attributes = column.Attributes | ColumnAttributes.CaseSensitive;
                }
            }

            return(tableSchema);
        }
Esempio n. 2
0
 private void AddColumnDdl(TableSchema table, ColumnSchema column, StringBuilder sql)
 {
     sql.AppendFormat("ALTER TABLE {0} ADD COLUMN ", table.TableName);
     ColumnDdl(column, sql);
 }