public CrudReturn SaveChanges <TDatabaseObject>(TDatabaseObject objectToSave, bool ignoreAutoIncrementField = true, bool doNotUpdateWhenExists = false, TransactionObject transaction = null) where TDatabaseObject : IDatabaseObject { var _table = TableDefinition.GetTableDefinition(typeof(TDatabaseObject)); var _ret = new CrudReturn { ReturnStatus = CrudStatus.Ok, RecordsAffected = -1, ChangeType = SqlStatementsTypes.None, ReturnMessage = "Dados atualizados com sucesso!" }; var _transaction = transaction ?? new TransactionObject(true); try { if (_transaction.Connection == null) { _transaction.SetConnection(_table.DefaultDataFunction.Connection); } var _sql = ""; var _backEndField = _table.Fields.Select(f => f.Value).FirstOrDefault(f => f.AutomaticValue != AutomaticValue.None); var _data = objectToSave.ToDictionary(); var _exists = _transaction.Connection.ExecuteScalar <long>(_table.GetSqlSelectForCheck(), _data); if (_exists == 0) { _ret.ChangeType = SqlStatementsTypes.Insert; if (_backEndField != null) { _sql = _table.GetSqlInsert(ignoreAutoIncrementField); int _lastId = this.DoInsert(_table, _transaction.Connection, _sql, _data); objectToSave.SetObjectFieldValue(_backEndField.Name, _lastId); _ret.RecordsAffected = 1; } else { objectToSave.SetIdFields(); _sql = _table.GetSqlInsert(ignoreAutoIncrementField); _ret.RecordsAffected = _transaction.Connection.Execute(_sql, _data); } } else { if (!doNotUpdateWhenExists) { _ret.ChangeType = SqlStatementsTypes.Update; _sql = _table.GetSqlUpdate(); _ret.RecordsAffected = _transaction.Connection.Execute(_sql, _data); } } _ret.ReturnData = objectToSave; if (_transaction.AutoCommit) { _transaction.Commit(); _transaction.Dispose(); } } catch (Exception ex) { _ret.ReturnMessage = string.Format("{0}\r\n{1}", ex.Message, ex.StackTrace); _ret.ReturnStatus = CrudStatus.Fail; } return(_ret); }
private int DoInsert(TableDefinition table, IDbConnection connection, string sql, object objectParameters) { var _ret = connection.Query <int>(sql + "\n" + table.DefaultDataFunction.GetSqlLastIdentity(), objectParameters).First(); return(_ret); }
public void InitFields() { #if !PCL this.ResolveObjectDefAttribute(); this.ResolveDefaultDataFunction(); this.JoinTables = new Dictionary <string, TableDefinition>(); this.Fields = new Dictionary <string, FieldDefAttribute>(); this.Rules = new Dictionary <string, FieldRuleAttribute>(); this.JoinFields = new Dictionary <string, JoinFieldAttribute>(); #if WINDOWS_PHONE_APP var _srcProp = m_Type.GetRuntimeProperties(); #else var _srcProp = m_Type.GetProperties(BindingFlags.Public | BindingFlags.Instance); #endif foreach (var p in _srcProp) { try { if (!p.CanWrite) { continue; } var _p = _srcProp.FirstOrDefault(d => d.Name == p.Name); if (_p == null) { continue; } var _joinAtt = (JoinFieldAttribute)_p.GetCustomAttributes(typeof(JoinFieldAttribute), true).FirstOrDefault(); if (_joinAtt != null) { #if WINDOWS_PHONE_APP if (_p.PropertyType.GetTypeInfo().IsGenericType) #else if (_p.PropertyType.IsGenericType) #endif { _joinAtt.FieldType = _p.PropertyType.GenericTypeArguments[0]; } else { _joinAtt.FieldType = _p.PropertyType; } _joinAtt.Name = _p.Name; _joinAtt.TableName = _joinAtt.FieldType.Name; _joinAtt.SourceTableName = m_Type.Name; #if WINDOWS_PHONE_APP var _props = _joinAtt.FieldType.GetRuntimeProperties().Where(pi => pi.CanWrite); #else var _props = _joinAtt.FieldType.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(pi => pi.CanWrite); #endif var _fieldNamesProps = _props.Where(pi => !pi.GetCustomAttributes <JoinFieldAttribute>().Any()); _joinAtt.FieldNames = _fieldNamesProps.Select(pi => pi.Name).ToArray(); var _fields = new List <string>(); foreach (var _prop in _props) { var _flds = _prop.GetCustomAttributes <FieldDefAttribute>(true).FirstOrDefault(pi => pi.IsPrimaryKey); if (_flds != null) { if (_flds.IgnoreForSave) { continue; } _fields.Add(_prop.Name); } } _joinAtt.PrimaryKeysFields = _fields.ToArray(); this.JoinFields.Add(_p.Name, _joinAtt); this.JoinTables.Add(_joinAtt.Name, TableDefinition.GetTableDefinition(_joinAtt.FieldType, $"{this.TableName}_{_p.Name}")); } else { var _dbAtt = (FieldDefAttribute)_p.GetCustomAttributes(typeof(FieldDefAttribute), true).FirstOrDefault(); var _ignore = _dbAtt != null && _dbAtt.IgnoreForSave; if (!_ignore) { if (_dbAtt != null) { if (_dbAtt.IsInternal) { continue; } this.Fields.Add(_p.Name, new FieldDefAttribute { IsPrimaryKey = _dbAtt.IsPrimaryKey, AutomaticValue = _dbAtt.AutomaticValue, Name = _p.Name, DotNetType = _p.PropertyType, IsNullAble = _dbAtt.IsNullAble, Lenght = _dbAtt.Lenght, Label = _dbAtt.Label, ShortLabel = _dbAtt.ShortLabel, DisplayOnForm = _dbAtt.DisplayOnForm, DisplayOnGrid = _dbAtt.DisplayOnGrid, IgnoreForSave = _dbAtt.IgnoreForSave, Precision = _dbAtt.Precision, EditOnForm = _dbAtt.EditOnForm, SerializeField = _dbAtt.SerializeField, NumberFormat = _dbAtt.NumberFormat, ValidChars = _dbAtt.ValidChars }); } else { this.Fields.Add(_p.Name, new FieldDefAttribute { IsPrimaryKey = false, AutomaticValue = AutomaticValue.None, Name = _p.Name, DotNetType = _p.PropertyType, IsNullAble = true, Lenght = 0, EditOnForm = true, SerializeField = false, ValidChars = "", NumberFormat = "" }); } } var _ruleAtt = (FieldRuleAttribute)_p.GetCustomAttributes(typeof(FieldRuleAttribute), true).FirstOrDefault(); if (_ruleAtt == null) { this.Rules.Add(_p.Name, new FieldRuleAttribute { Name = _p.Name, Type = _p.PropertyType, ValidationText = "", IsForValidate = false }); } else { _ruleAtt.Name = _p.Name; _ruleAtt.Type = _p.PropertyType; _ruleAtt.IsForValidate = true; this.Rules.Add(_p.Name, _ruleAtt); } } } catch (FormatException fex) { } catch (Exception ex) { throw new Exception("Validar erro na propriedade InnerException", ex); } } #endif }