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); }
private IndexSchema ParseCreateIndex(string sql) { var parser = new SimpleParser(sql); var indexSchema = new IndexSchema(); indexSchema.Attributes = IndexAttributes.CaseSensitive; parser.SkipOverString("CREATE"); if (parser.TakeWord().ToUpper() == "UNIQUE") { indexSchema.Attributes = indexSchema.Attributes | IndexAttributes.Unique; parser.SkipOverString("INDEX"); } indexSchema.IndexName = parser.TakeWord(); if (indexSchema.IndexName.ToUpper() == "IF") { parser.SkipOverString("EXISTS"); indexSchema.IndexName = parser.TakeWord(); } parser.SkipOverString("ON"); parser.TakeWord(); // table name parser.SkipOverString("("); var columnNames = new List <string>(); while (!parser.AtEnd() && !parser.Is(')')) { columnNames.Add(parser.TakeWord()); parser.SkipAny(' '); var columnOptions = parser.TakeToAny(',', ')').ToUpper(); if (columnOptions.Contains("COLLATE NOCASE")) { indexSchema.Attributes = indexSchema.Attributes & ~IndexAttributes.CaseSensitive; } parser.Skip(1); } indexSchema.ColumnNames = columnNames.ToArray(); return(indexSchema); }