private static void ThrowException(CrudReturn status) { if (status.ReturnStatus == CrudStatus.Fail) { throw new Exception(status.ReturnMessage); } }
public CrudReturn ExecuteStatement(string sqlStatement, object parameter = null) { var _ret = new CrudReturn { RecordsAffected = -1, ChangeType = SqlStatementsTypes.UnknownStatement, ReturnStatus = CrudStatus.None }; #if !PCL try { using (var _conn = this.Connection) { _ret.RecordsAffected = _conn.Execute(sqlStatement, parameter); } } catch (Exception ex) { _ret.ReturnMessage = string.Format("{0}\r\n{1}", ex.Message, ex.StackTrace); _ret.ReturnStatus = CrudStatus.Fail; } #endif return(_ret); }
public CrudReturn Insert <TDatabaseObject>(TDatabaseObject objectToInsert, bool ignoreAutoIncrementField = true, TransactionObject transaction = null) where TDatabaseObject : IDatabaseObject { var _table = TableDefinition.GetTableDefinition(typeof(TDatabaseObject)); var _ret = new CrudReturn { ReturnStatus = CrudStatus.Ok, RecordsAffected = -1, ChangeType = SqlStatementsTypes.Insert, ReturnMessage = "Dados atualizados com sucesso!" }; var _transaction = transaction ?? new TransactionObject(true); try { if (_transaction.Connection == null) { _transaction.SetConnection(_table.DefaultDataFunction.Connection); } var _sql = ""; var _data = objectToInsert.ToDictionary(); var _backEndField = _table.Fields.Select(f => f.Value).FirstOrDefault(f => f.AutomaticValue != AutomaticValue.None); if (_backEndField != null) { _sql = _table.GetSqlInsert(ignoreAutoIncrementField); int _lastId = this.DoInsert(_table, _transaction.Connection, _sql, _data); objectToInsert.SetObjectFieldValue(_backEndField.Name, _lastId); _ret.RecordsAffected = 1; } else { objectToInsert.SetIdFields(); _sql = _table.GetSqlInsert(ignoreAutoIncrementField); _ret.RecordsAffected = _transaction.Connection.Execute(_sql, _data); } _ret.ReturnData = objectToInsert; 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); }
public CrudReturn Update <TDatabaseObject>(TDatabaseObject objectToUpdate, TransactionObject transaction = null) where TDatabaseObject : IDatabaseObject { var _table = TableDefinition.GetTableDefinition(typeof(TDatabaseObject)); var _ret = new CrudReturn { ReturnStatus = CrudStatus.Ok, RecordsAffected = -1, ChangeType = SqlStatementsTypes.Update, ReturnMessage = "Dados atualizados com sucesso!" }; var _transaction = transaction ?? new TransactionObject(true); try { if (_transaction.Connection == null) { _transaction.SetConnection(_table.DefaultDataFunction.Connection); } var _sql = ""; var _data = objectToUpdate.ToDictionary(); _sql = _table.GetSqlUpdate(); _ret.RecordsAffected = _transaction.Connection.Execute(_sql, _data); _ret.ReturnData = objectToUpdate; 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); }
public CrudReturn DeleteAll <TDatabaseObject>(DboCommand command, TransactionObject transaction = null) { var _table = TableDefinition.GetTableDefinition(typeof(TDatabaseObject)); var _ret = new CrudReturn { ReturnStatus = CrudStatus.Ok, RecordsAffected = -1, ChangeType = SqlStatementsTypes.DeleteAll, ReturnMessage = "Dados excluídos com sucesso!" }; var _transaction = transaction ?? new TransactionObject(true); try { if (_transaction.Connection == null) { _transaction.SetConnection(_table.DefaultDataFunction.Connection); } _ret.RecordsAffected = _transaction.Connection.Execute(command.GetCommandDefinition()); 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); }