public virtual void Delete(DbTransaction transaction) { if (IsNew) { return; } TableNameAttribute tableNameAttribute = (TableNameAttribute)Table.GetType().GetCustomAttributes(typeof(TableNameAttribute), false).FirstOrDefault(); ColumnAttribute idAttribute = (ColumnAttribute)GetType().GetProperty("Id").GetCustomAttributes(typeof(ColumnAttribute), false).First(); if (tableNameAttribute != null && !string.IsNullOrWhiteSpace(tableNameAttribute.Name) && !string.IsNullOrWhiteSpace(idAttribute.Name)) { var command = new FbCommand(string.Format("delete from \"{0}\" where \"{1}\"={2}", tableNameAttribute.Name, idAttribute.Name, Id), Connector.Connection); command.Transaction = (FbTransaction)transaction; command.ExecuteNonQuery(); } else { throw new ApplicationException("No table name provided to delete record"); } }
public virtual bool Save(DbTransaction transaction) { TableNameAttribute tableNameAttribute = (TableNameAttribute)Table.GetType().GetCustomAttributes(typeof(TableNameAttribute), false).FirstOrDefault(); GeneratorNameAttribute generatorNameAttribute = (GeneratorNameAttribute)Table.GetType().GetCustomAttributes(typeof(GeneratorNameAttribute), false).FirstOrDefault(); ColumnAttribute idAttribute = (ColumnAttribute)GetType().GetProperty("Id").GetCustomAttributes(typeof(ColumnAttribute), false).First(); bool result = false; lock (FieldsPreviousValue) { if (FieldsPreviousValue.Count() > 0) { PropertyInfo lastField = FieldsPreviousValue.Last().Key; if (tableNameAttribute != null && !string.IsNullOrWhiteSpace(tableNameAttribute.Name)) { StringBuilder cb = new StringBuilder(); bool inserting = Id == NoId; long id = Id; if (inserting) { if (generatorNameAttribute != null && !string.IsNullOrWhiteSpace(generatorNameAttribute.Name)) { var vb = new StringBuilder(" (@ID, "); cb.Append("insert into \"").Append(tableNameAttribute.Name).Append("\" (").Append(idAttribute.Name).Append(", "); id = Connector.GenNextGenValue(generatorNameAttribute.Name, (FbTransaction)transaction); foreach (PropertyInfo field in FieldsPreviousValue.Keys) { ColumnAttribute an = (ColumnAttribute)(field.GetCustomAttributes(typeof(ColumnAttribute), true).FirstOrDefault()); string fieldName = (an == null) ? field.Name.ToUpperInvariant() : an.Name; cb.Append(fieldName); vb.Append("@").Append(fieldName); if (field != lastField) { cb.Append(", "); vb.Append(", "); } else { cb.Append(")"); vb.Append(")"); } } cb.Append(" values").Append(vb); } else { throw new ApplicationException("No generator name provided to insert data"); } Id = id; } else { cb.Append("update \"").Append(tableNameAttribute.Name).Append("\" set "); foreach (PropertyInfo field in FieldsPreviousValue.Keys) { ColumnAttribute an = (ColumnAttribute)(field.GetCustomAttributes(typeof(ColumnAttribute), true).FirstOrDefault()); string fieldName = (an == null) ? field.Name.ToUpperInvariant() : an.Name; cb.Append(fieldName).Append("=@").Append(fieldName); if (field != lastField) { cb.Append(", "); } else { cb.Append(" "); } } cb.Append("where ").Append(idAttribute.Name).Append("=@ID"); } var command = new FbCommand(cb.ToString(), Connector.Connection); command.Transaction = (FbTransaction)transaction; command.Parameters.Add("@ID", id); PropertyInfo[] fields = this.GetType().GetProperties(); foreach (var field in FieldsPreviousValue) { ColumnAttribute an = (ColumnAttribute)(field.Key.GetCustomAttributes(typeof(ColumnAttribute), true).FirstOrDefault()); string fieldName = (an == null) ? field.Key.Name.ToUpperInvariant() : an.Name; command.Parameters.Add("@" + fieldName, field.Key.GetValue(this, null) ?? DBNull.Value); } command.ExecuteNonQuery(); FieldsPreviousValue.Clear(); RefreshAutoUpdatedFields(transaction, inserting); result = true; } else { throw new ApplicationException("No table name provided"); } } Modified = false; } return(result); }