internal void GetFieldsToChange(ECTable _table, ref Dictionary <string, string> _fieldsToAdd, ref Dictionary <string, string> _fieldsToDelete, ref Dictionary <string, string> _databaseFields) { Type t = _table.GetType(); Dictionary <string, string> tableProperties = new Dictionary <string, string>(); foreach (PropertyInfo p in _table.GetType().GetProperties().Where(x => x.IsDefined(typeof(ECTableFieldAttribute)))) { ECTableFieldAttribute tfa = (ECTableFieldAttribute)p.GetCustomAttribute(typeof(ECTableFieldAttribute)); if (tfa.type == FieldType.VARCHAR) { tableProperties.Add(p.Name, FieldTypeToSqlType(tfa.type) + "(" + tfa.length.ToString() + ")"); } else { tableProperties.Add(p.Name, FieldTypeToSqlType(tfa.type)); } } string sql = "SHOW COLUMNS FROM `" + t.Name + "`;"; MySqlCommand cmd = new MySqlCommand(sql, connection); MySqlDataReader res = cmd.ExecuteReader(); while (res.Read()) { string type = res["Type"].ToString().ToUpper(); if (type.Substring(0, type.IndexOf('(')) != "VARCHAR") { type = type.Substring(0, type.IndexOf('(')); } _databaseFields.Add(res["Field"].ToString(), type); } foreach (KeyValuePair <string, string> kv in tableProperties) { if (!_databaseFields.Contains(kv)) { _fieldsToAdd.Add(kv.Key, kv.Value); } } foreach (KeyValuePair <string, string> kv in _databaseFields) { if (!tableProperties.Contains(kv)) { _fieldsToDelete.Add(kv.Key, kv.Value); } } res.Close(); }
internal void GetFieldsToChange(ECTable _table, ref Dictionary <string, string> _fieldsToAdd, ref Dictionary <string, string> _fieldsToDelete, ref Dictionary <string, string> _databaseFields) { Type t = _table.GetType(); Dictionary <string, string> tableProperties = new Dictionary <string, string>(); foreach (PropertyInfo p in _table.GetType().GetProperties().Where(x => x.IsDefined(typeof(ECTableFieldAttribute)))) { ECTableFieldAttribute tfa = (ECTableFieldAttribute)p.GetCustomAttribute(typeof(ECTableFieldAttribute)); if (tfa.type == FieldType.VARCHAR) { tableProperties.Add(p.Name, FieldTypeToSqlType(tfa.type) + "(" + tfa.length.ToString() + ")"); } else { tableProperties.Add(p.Name, FieldTypeToSqlType(tfa.type)); } } command.Parameters.Clear(); command.CommandText = "PRAGMA table_info(" + t.Name + ");"; SQLiteDataReader res = command.ExecuteReader(); while (res.Read()) { _databaseFields.Add(res["name"].ToString(), res["type"].ToString().Replace(" ", "")); } foreach (KeyValuePair <string, string> kv in tableProperties) { if (!_databaseFields.Contains(kv)) { _fieldsToAdd.Add(kv.Key, kv.Value); } } foreach (KeyValuePair <string, string> kv in _databaseFields) { if (!tableProperties.Contains(kv)) { _fieldsToDelete.Add(kv.Key, kv.Value); } } res.Close(); }