public static string BuildDeleteCommandContent <T>(T obj, string table, int prefix, out PropertyList properties) { SQLMetaField primaryKey = null; foreach (FieldInfo field in typeof(T).GetFields()) { bool isPrimary = false; string name = field.Name; foreach (Attribute attrib in Attribute.GetCustomAttributes(field)) { if (attrib is SQLIgnore) { break; } else if (attrib is SQLPrimaryKey) { isPrimary = true; } else if (attrib is SQLPropertyName) { name = ((SQLPropertyName)attrib).Name; } } if (isPrimary) { primaryKey = new SQLMetaField(name, -1, field); break; } } if (primaryKey == null) { throw new NoPrimaryKeyException(); } string k1 = $"@{prefix}KEY"; string command = $"DELETE FROM `{table}` WHERE {primaryKey.Name}={k1};"; properties = new PropertyList(); properties.Add(k1, primaryKey.Field.GetValue(obj)); return(command); }
public static MySqlCommand BuildDeleteCommand <T>(T obj, string table, MySqlConnection sqlConnection = null) { SQLMetaField primaryKey = null; foreach (FieldInfo field in typeof(T).GetFields()) { bool isPrimary = false; string name = field.Name; foreach (Attribute attrib in Attribute.GetCustomAttributes(field)) { if (attrib is SQLIgnore) { break; } else if (attrib is SQLPrimaryKey) { isPrimary = true; } else if (attrib is SQLPropertyName) { name = ((SQLPropertyName)attrib).Name; } } if (isPrimary) { primaryKey = new SQLMetaField(name, -1, field); break; } } if (primaryKey == null) { throw new NoPrimaryKeyException(); } string command = $"DELETE FROM `{table}` WHERE {primaryKey.Name}=@KEY;"; MySqlCommand sqlCommand = (sqlConnection != null ? new MySqlCommand(command, sqlConnection) : new MySqlCommand(command)); sqlCommand.Parameters.AddWithValue("@KEY", primaryKey.Field.GetValue(obj)); return(sqlCommand); }
public static string BuildUpdateCommandContent <T>(T obj, string table, int prefix, out PropertyList properties) { List <SQLMetaField> sqlMetas = new List <SQLMetaField>(); SQLMetaField primaryKey = null; foreach (FieldInfo field in typeof(T).GetFields()) { bool include = true; string name = field.Name; bool omitUpdate = false; bool isPrimary = Attribute.IsDefined(field, typeof(SQLPrimaryKey)); foreach (Attribute attrib in Attribute.GetCustomAttributes(field)) { if ((attrib is SQLOmit && !isPrimary) || attrib is SQLIgnore) { include = false; break; } else if (attrib is SQLPropertyName) { name = ((SQLPropertyName)attrib).Name; } else if (attrib is SQLOmitUpdate) { omitUpdate = true; } } if (include) { SQLMetaField meta = new SQLMetaField(name, sqlMetas.Count, field, omitUpdate); if (isPrimary) { primaryKey = meta; foreach (SQLMetaField SubMeta in sqlMetas.Where(x => string.Equals(x.Name, meta.Name, StringComparison.InvariantCultureIgnoreCase))) { sqlMetas.Remove(SubMeta); break; } } else { if (sqlMetas.Where(x => string.Equals(x.Name, name, StringComparison.InvariantCultureIgnoreCase)).Count() != 0) { continue; } sqlMetas.Add(new SQLMetaField(name, sqlMetas.Count, field)); } } } sqlMetas.RemoveAll(x => x.OmitUpdate); if (primaryKey == null) { throw new NoPrimaryKeyException(); } string command = $"UPDATE `{table}` SET {string.Join(", ", sqlMetas.CastEnumeration(x => $"{x.Name}=@{x.Index}"))} WHERE {primaryKey.Name}=@KEY;"; properties = new PropertyList(); properties.Add($"@{prefix}KEY", primaryKey.Field.GetValue(obj)); foreach (SQLMetaField meta in sqlMetas) { properties.Add($"@{prefix}_{meta.Index}", meta.Field.GetValue(obj)); } return(command); }
public static MySqlCommand BuildUpdateCommand <T>(T obj, string table, MySqlConnection connection = null) { List <SQLMetaField> sqlMetas = new List <SQLMetaField>(); SQLMetaField primaryKey = null; foreach (FieldInfo field in typeof(T).GetFields()) { bool include = true; string Name = field.Name; bool omitUpdate = false; bool isPrimary = Attribute.IsDefined(field, typeof(SQLPrimaryKey)); foreach (Attribute Attrib in Attribute.GetCustomAttributes(field)) { if ((Attrib is SQLOmit && !isPrimary) || Attrib is SQLIgnore) { include = false; break; } else if (Attrib is SQLPropertyName) { Name = ((SQLPropertyName)Attrib).Name; } else if (Attrib is SQLOmitUpdate) { omitUpdate = true; } } if (include) { SQLMetaField meta = new SQLMetaField(Name, sqlMetas.Count, field, omitUpdate); if (isPrimary) { primaryKey = meta; foreach (SQLMetaField SubMeta in sqlMetas.Where(x => string.Equals(x.Name, meta.Name, StringComparison.InvariantCultureIgnoreCase))) { sqlMetas.Remove(SubMeta); break; } } else { if (sqlMetas.Where(x => string.Equals(x.Name, Name, StringComparison.InvariantCultureIgnoreCase)).Count() != 0) { continue; } sqlMetas.Add(new SQLMetaField(Name, sqlMetas.Count, field)); } } } sqlMetas.RemoveAll(x => x.OmitUpdate); if (primaryKey == null) { throw new NoPrimaryKeyException(); } string Command = $"UPDATE `{table}` SET {string.Join(", ", sqlMetas.CastEnumeration(x => $"{x.Name}=@{x.Index}"))} WHERE {primaryKey.Name}=@KEY;"; MySqlCommand sqlCommand = (connection != null ? new MySqlCommand(Command, connection) : new MySqlCommand(Command)); sqlCommand.Parameters.AddWithValue("@KEY", primaryKey.Field.GetValue(obj)); foreach (SQLMetaField meta in sqlMetas) { sqlCommand.Parameters.AddWithValue($"@{meta.Index}", meta.Field.GetValue(obj)); } return(sqlCommand); }