public static ModelData Create(CacheFile file) { var ret = new ModelData {Name = ShortenName(file.Name)}; var rows = file.GetRows(); ret.Rows = rows; if (rows.Count() == 0) throw new InvalidDataException("Cache is empty"); var singleRow = rows.First(); ret.SingleRow = singleRow; // we need to go through all rows to find the maximum size type of all columns var fieldTypes = new FieldType[singleRow.Columns.Count]; for (int i = 0; i < singleRow.Columns.Count; i++) fieldTypes[i] = singleRow.Columns[i].Type; foreach (var row in rows) for (int i = 0; i < row.Columns.Count; i++) if (FieldTypeHelper.GetTypeBits(row.Columns[i].Type) > FieldTypeHelper.GetTypeBits(fieldTypes[i])) fieldTypes[i] = row.Columns[i].Type; ret.FieldTypes = fieldTypes; // now build the creation query - fields, primary key, indices, cleanup var query = new StringBuilder(); query.AppendLine("DROP TABLE IF EXISTS " + ret.Name + ";"); query.AppendLine("CREATE TABLE " + ret.Name + " ("); bool createAutoIncrement = !IsColumnUnique(rows, 0); string autoIncrementName = createAutoIncrement ? (singleRow.Columns.Any(c => c.Name == "id") ? "index" : "id") : ""; if (createAutoIncrement) query.AppendLine(Indention + autoIncrementName + " INT AUTO_INCREMENT,"); for (int i = 0; i < singleRow.Columns.Count; i++) query.AppendLine(Indention + singleRow.Columns[i].Name + " " + GetSQLType(fieldTypes[i]) + ","); if (createAutoIncrement) query.AppendLine(Indention + "PRIMARY KEY (" + autoIncrementName + "),"); else query.AppendLine(Indention + "PRIMARY KEY (" + singleRow.Columns[0].Name + "),"); for (int i = 1; i < singleRow.Columns.Count; i++) if (singleRow.Columns[i].Name.EndsWith("ID")) query.AppendLine(Indention + "INDEX " + singleRow.Columns[i].Name + " (" + singleRow.Columns[i].Name + "),"); if (query[query.Length - 1] == '\n' && query[query.Length - 2] == '\r' && query[query.Length - 3] == ',') query.Remove(query.Length - 3, 1); query.AppendLine(");"); ret.CreationQuery = query.ToString(); return ret; }
public static ModelData Create(CacheFile file) { var ret = new ModelData { Name = ShortenName(file.Name) }; var rows = file.GetRows(); ret.Rows = rows; if (rows.Count() == 0) { throw new InvalidDataException("Cache is empty"); } var singleRow = rows.First(); ret.SingleRow = singleRow; // we need to go through all rows to find the maximum size type of all columns var fieldTypes = new FieldType[singleRow.Columns.Count]; for (int i = 0; i < singleRow.Columns.Count; i++) { fieldTypes[i] = singleRow.Columns[i].Type; } foreach (var row in rows) { for (int i = 0; i < row.Columns.Count; i++) { if (FieldTypeHelper.GetTypeBits(row.Columns[i].Type) > FieldTypeHelper.GetTypeBits(fieldTypes[i])) { fieldTypes[i] = row.Columns[i].Type; } } } ret.FieldTypes = fieldTypes; // now build the creation query - fields, primary key, indices, cleanup var query = new StringBuilder(); query.AppendLine("DROP TABLE IF EXISTS " + ret.Name + ";"); query.AppendLine("CREATE TABLE " + ret.Name + " ("); bool createAutoIncrement = !IsColumnUnique(rows, 0); string autoIncrementName = createAutoIncrement ? (singleRow.Columns.Any(c => c.Name == "id") ? "index" : "id") : ""; if (createAutoIncrement) { query.AppendLine(Indention + autoIncrementName + " INT AUTO_INCREMENT,"); } for (int i = 0; i < singleRow.Columns.Count; i++) { query.AppendLine(Indention + singleRow.Columns[i].Name + " " + GetSQLType(fieldTypes[i]) + ","); } if (createAutoIncrement) { query.AppendLine(Indention + "PRIMARY KEY (" + autoIncrementName + "),"); } else { query.AppendLine(Indention + "PRIMARY KEY (" + singleRow.Columns[0].Name + "),"); } for (int i = 1; i < singleRow.Columns.Count; i++) { if (singleRow.Columns[i].Name.EndsWith("ID")) { query.AppendLine(Indention + "INDEX " + singleRow.Columns[i].Name + " (" + singleRow.Columns[i].Name + "),"); } } if (query[query.Length - 1] == '\n' && query[query.Length - 2] == '\r' && query[query.Length - 3] == ',') { query.Remove(query.Length - 3, 1); } query.AppendLine(");"); ret.CreationQuery = query.ToString(); return(ret); }