public int Execute() { int i = 0; DataParameter dp; StringBuilder wheres = new StringBuilder(); KeyValuePair <string, FieldInfo>[] pks = TDbTable <T> .PrimaryKeys; List <DataParameter> list = new List <DataParameter>(pks.Length); foreach (KeyValuePair <string, FieldInfo> key in pks) { if (i++ > 0) { wheres.Append(" AND "); } wheres.Append(_query.Provider.EscapeName(key.Key)); wheres.Append('='); dp = _query.BuildParameter(key.Value.GetValue(_instance)); wheres.Append(dp.GetParameterName()); list.Add(dp); } StringBuilder sb = new StringBuilder(); sb.Append("DELETE FROM "); sb.Append(_query.Provider.EscapeName(DbTable.GetTableName <T>())); sb.Append(" WHERE "); sb.Append(wheres.ToString()); sb.Append(';'); return(_query.DataSource.ExecuteNonQuery(sb.ToString(), list.ToArray())); }
public bool Execute() { int i = 0; DataParameter dp; StringBuilder names = new StringBuilder(); StringBuilder values = new StringBuilder(); KeyValuePair <FieldInfo, DataColumnAttribute> pair; Dictionary <string, KeyValuePair <FieldInfo, DataColumnAttribute> > fields = TAllNameGetAttFields <T, DataColumnAttribute> .Fields; List <DataParameter> list = new List <DataParameter>(fields.Count); foreach (string key in fields.Keys) { pair = fields[key]; if (pair.Value == null || !pair.Value.IsIdentity) { if (i++ > 0) { names.Append(','); values.Append(','); } names.Append(_query.Provider.EscapeName(key)); dp = _query.BuildParameter(pair.Key.GetValue(_instance)); values.Append(dp.GetParameterName()); list.Add(dp); } } string id = null; KeyValuePair <string, bool>[] pks = TDbTable <T> .IdentityKeyEx; if (pks.Length == 1) { KeyValuePair <string, bool> kvp = pks[0]; if (kvp.Value) { id = kvp.Key; } } StringBuilder sb = new StringBuilder(); sb.Append("INSERT INTO "); sb.Append(_query.Provider.EscapeName(DbTable.GetTableName <T>())); sb.Append(" ("); sb.Append(names.ToString()); sb.Append(") VALUES ("); sb.Append(values.ToString()); sb.Append(')'); sb.Append(_query.Provider.GetInsertSqlEnd(id)); sb.Append(';'); return(DbTable.InsertImpl(_query.DataSource, sb.ToString(), _instance, list.ToArray())); }
public int Execute() { int i = 0; DataParameter dp; StringBuilder sets = new StringBuilder(); StringBuilder wheres = new StringBuilder(); Dictionary <string, KeyValuePair <FieldInfo, DataColumnAttribute> > fields = TAllNameGetAttFields <T, DataColumnAttribute> .Fields; List <DataParameter> list = new List <DataParameter>(fields.Count); if (_columns == null) { foreach (KeyValuePair <string, KeyValuePair <FieldInfo, DataColumnAttribute> > field in fields) { if (field.Value.Value == null || (!field.Value.Value.IsPrimaryKey && !field.Value.Value.IsIdentity)) { if (i++ > 0) { sets.Append(','); } sets.Append(_query.Provider.EscapeName(field.Key)); sets.Append('='); dp = _query.BuildParameter(field.Value.Key.GetValue(_instance)); sets.Append(dp.GetParameterName()); list.Add(dp); } } } else { if (_mode == ColumnMode.Include) { KeyValuePair <FieldInfo, DataColumnAttribute> pair; foreach (DbColumn column in _columns) { if (fields.TryGetValue(column.Column, out pair)) { if (pair.Value == null || (!pair.Value.IsPrimaryKey && !pair.Value.IsIdentity)) { if (i++ > 0) { sets.Append(','); } sets.Append(column.Build(_query.DataSource)); sets.Append('='); dp = _query.BuildParameter(pair.Key.GetValue(_instance)); sets.Append(dp.GetParameterName()); list.Add(dp); } } else { throw new ArgumentException(string.Concat("column \"", column.Column, "\" is not in data table \"", DbTable.GetTableName <T>(), "\"")); } } } else { Dictionary <string, KeyValuePair <FieldInfo, DataColumnAttribute> > keys = new Dictionary <string, KeyValuePair <FieldInfo, DataColumnAttribute> >(fields); foreach (DbColumn column in _columns) { keys.Remove(column.Column); } foreach (KeyValuePair <string, KeyValuePair <FieldInfo, DataColumnAttribute> > field in fields) { if (field.Value.Value == null || (!field.Value.Value.IsPrimaryKey && !field.Value.Value.IsIdentity)) { if (i++ > 0) { sets.Append(','); } sets.Append(_query.Provider.EscapeName(field.Key)); sets.Append('='); dp = _query.BuildParameter(field.Value.Key.GetValue(_instance)); sets.Append(dp.GetParameterName()); list.Add(dp); } } } } i = 0; KeyValuePair <string, FieldInfo>[] pks = TDbTable <T> .PrimaryKeys; foreach (KeyValuePair <string, FieldInfo> key in pks) { if (i++ > 0) { wheres.Append(" AND "); } wheres.Append(_query.Provider.EscapeName(key.Key)); wheres.Append('='); dp = _query.BuildParameter(key.Value.GetValue(_instance)); wheres.Append(dp.GetParameterName()); list.Add(dp); } StringBuilder sb = new StringBuilder(); sb.Append("UPDATE "); sb.Append(_query.Provider.EscapeName(DbTable.GetTableName <T>())); sb.Append(" SET "); sb.Append(sets.ToString()); sb.Append(" WHERE "); sb.Append(wheres.ToString()); sb.Append(';'); return(_query.DataSource.ExecuteNonQuery(sb.ToString(), list.ToArray())); }
private DataParameter GetParameter(DataSource ds, object value) { return(DbQuery.BuildParameter(ds, value)); }