public CreateStatement(string sql) { Match match = STATEMENT_REGEX.Match(sql.Trim()); if (!match.Success) { throw new Exception($"Invalid sql '{this.Sql}'"); } this.Sql = sql; // Extract field names this.TableName = match.Groups["tableName"].Value; string createTableDefs = match.Groups["createTableDefs"].Value.Replace("\r", " ").Replace("\n", " "); List <TableIndex> indexes = new List <TableIndex>(); List <TableFieldDef> fieldDefs = new List <TableFieldDef>(); int lastIndex = 0; while (lastIndex < createTableDefs.Length) { string substring = createTableDefs.Substring(lastIndex); Match primaryKeyMatch = PRIMARY_KEY_REGEX.Match(substring); if (primaryKeyMatch.Success) { string[] keyFieldNames = primaryKeyMatch.Groups["fields"].Value.Split(',').Select(x => x.Trim()).ToArray(); indexes.Add(new TableIndex(TableIndexType.Primary, keyFieldNames)); lastIndex += primaryKeyMatch.Length; } else { Match indexMatch = INDEX_REGEX.Match(substring); if (indexMatch.Success) { string[] keyFieldNames = indexMatch.Groups["fields"].Value.Split(',').Select(x => x.Trim()).ToArray(); indexes.Add(new TableIndex(string.IsNullOrEmpty(indexMatch.Groups["unique"].Value) ? TableIndexType.Other : TableIndexType.Unique, keyFieldNames)); lastIndex += indexMatch.Length; } else { Match fieldMatch = FIELD_REGEX.Match(substring); if (fieldMatch.Success) { string fieldName = fieldMatch.Groups["fieldName"].Value; string fieldTypeText = fieldMatch.Groups["fieldType"].Value; (Type fieldType, int maxLength) = BaseDatabase.ConvertMySqlType(fieldTypeText); bool notNull = fieldMatch.Groups["not"].Success && fieldMatch.Groups["null"].Success; bool autoIncrement = fieldMatch.Groups["autoIncrement"].Success; fieldDefs.Add(new TableFieldDef(fieldName, fieldType, maxLength, !notNull, autoIncrement)); lastIndex += fieldMatch.Length; } else { throw new Exception($"Could not parse '{substring}' in {this.Sql}"); } } } } this.FieldDefs = fieldDefs.ToArray(); this.Indexes = indexes.ToArray(); }