public IEnumerable <TDatabaseObject> Query <TDatabaseObject>(DboCommand command, bool useFieldNames = true)
        {
            try
            {
                var _ret = new List <TDatabaseObject>();

                if (useFieldNames)
                {
                    var _rows = GetDataReader(command.GetCommandDefinition());

                    _ret = ReadRowsFromReader <TDatabaseObject>(_rows);
                }
                else
                {
                    using (var _conn = this.Connection)
                    {
                        _ret = _conn.Query <TDatabaseObject>(command.GetCommandDefinition()).ToList();
                        _conn.Close();
                    }
                }

                return(_ret);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
        private static DboCommand GetDboCommand(Expression <Func <T, bool> > predicate = null, bool useFieldNames = true, SqlStatementsTypes sqlType = SqlStatementsTypes.Select, object obj = null)
        {
            var _tableDef     = TableDefinition.GetTableDefinition(typeof(T));
            var _selectFields = _tableDef.GetStatementSelect(sqlType == SqlStatementsTypes.SelectReload);

            DboCommand _sqlCommand = null;

            switch (sqlType)
            {
            case SqlStatementsTypes.Select:
                _sqlCommand = GetCommandSelect(_selectFields, predicate, _tableDef);
                break;

            case SqlStatementsTypes.DeleteAll:
                _sqlCommand = GetCommandDelete(predicate);
                break;

            case SqlStatementsTypes.SelectReload:
                _sqlCommand = GetCommandSelectReloadMe(_selectFields, obj);
                break;

            default:
                throw new NotImplementedException();
            }

            return(_sqlCommand);
        }
        public TDatabaseObject ReloadMe <TDatabaseObject>(DboCommand command)
        {
            var _rows = GetDataReader(command.GetCommandDefinition());

            var _ret = ReadRowsFromReader <TDatabaseObject>(_rows);

            return(_ret.FirstOrDefault());
        }
 public bool Exists(DboCommand command)
 {
     using (var _db = this.Connection)
     {
         var _exists = _db.ExecuteScalar <long>(command.GetCommandDefinition());
         _db.Close();
         return(_exists != 0);
     }
 }
        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);
        }
 public async Task <bool> ExistsAsync(DboCommand command)
 {
     return(await Task.Run(() => this.Exists(command)));
 }
 public async Task <CrudReturn> DeleteAllAsync <TDatabaseObject>(DboCommand command, TransactionObject transaction = null)
 {
     return(await Task.Run(() => this.DeleteAll <TDatabaseObject>(command, transaction)));
 }
 public async Task <TDatabaseObject> ReloadMeAsync <TDatabaseObject>(DboCommand command)
 {
     return(await Task.Run(() => this.ReloadMe <TDatabaseObject>(command)));
 }
 public async Task <IEnumerable <TDatabaseObject> > QueryAsync <TDatabaseObject>(DboCommand command, bool useFieldNames = true)
 {
     return(await Task.Run(() => this.Query <TDatabaseObject>(command, useFieldNames)));
 }
 public TDatabaseObject Load <TDatabaseObject>(DboCommand command)
 {
     return(this.Query <TDatabaseObject>(command).FirstOrDefault());
 }
 public async Task <TDatabaseObject> LoadAsync <TDatabaseObject>(DboCommand command)
 {
     return(await Task.Run(() => this.Load <TDatabaseObject>(command)));
 }