classes/bfw/database/Database.php
changeset 23 975b2839f1f3
parent 20 fe950de090e4
child 27 79be7d6a2765
equal deleted inserted replaced
22:16ebd5c503fe 23:975b2839f1f3
   223         return null;
   223         return null;
   224     }
   224     }
   225 
   225 
   226     /**
   226     /**
   227      * @param $table
   227      * @param $table
   228      * @param bool $sys
   228      * @param int $initial_id
   229      * @return array|null
   229      * @return array|null
   230      */
   230      */
   231     public function findAll($table, $sys) {
   231     public function findAll($table, $initial_id = 0) {
   232         $this->logger->info(sprintf('%s(%s, %d) ', __METHOD__, $table, $sys));
   232         $this->logger->info(sprintf('%s(%s, %d) ', __METHOD__, $table, $initial_id));
   233 
   233 
   234         $stmt = $this->pdo->prepare(
   234         $stmt = $this->pdo->prepare(
   235             sprintf('
   235             sprintf('
   236               SELECT
   236               SELECT
   237                 *
   237                 *
   238               FROM
   238               FROM
   239                 %s
   239                 %s
   240               WHERE
   240               WHERE
   241                 id > :id
   241                 id > :id
       
   242               ORDER BY ID ASC
   242             ', $table
   243             ', $table
   243             ));
   244             ));
   244 
   245 
   245         $id = ($sys ? 0 : 1);
   246         $stmt->bindParam(':id', $initial_id);
   246         $stmt->bindParam(':id', $id);
       
   247 
   247 
   248         if ($stmt->execute()) {
   248         if ($stmt->execute()) {
   249             return $stmt->fetchAll(PDO::FETCH_ASSOC);
   249             return $stmt->fetchAll(PDO::FETCH_ASSOC);
   250         }
   250         }
   251 
   251 
   327         return null;
   327         return null;
   328     }
   328     }
   329 
   329 
   330     /**
   330     /**
   331      * @param $sql
   331      * @param $sql
   332      * @return int
   332      * @return bool
   333      */
   333      */
   334     public function execute($sql) {
   334     public function execute($sql) {
   335         return $this->pdo->exec($sql);
   335         return ($this->pdo->exec($sql) !== FALSE);
   336     }
   336     }
   337 
   337 
   338     /**
   338     /**
   339      * @param $table
   339      * @param $table
   340      * @param $array
   340      * @param $array
   341      * @return int
   341      * @return bool
   342      */
   342      */
   343     public function persist($table, $array) {
   343     public function persist($table, $array) {
   344         $this->logger->info(sprintf('%s(%s, %s) ', __METHOD__, $table, print_r($array, true)));
   344         $this->logger->info(sprintf('%s(%s, %s) ', __METHOD__, $table, print_r($array, true)));
   345 
   345 
   346         $keys = array();
   346         $keys = array();
   364         $sql = sprintf("
   364         $sql = sprintf("
   365             INSERT INTO %s
   365             INSERT INTO %s
   366             (id, %s) VALUES (NULL, %s)
   366             (id, %s) VALUES (NULL, %s)
   367         ", $table, $fieldList, $fields);
   367         ", $table, $fieldList, $fields);
   368 
   368 
   369         return $this->pdo->exec($sql);
   369         return ($this->pdo->exec($sql) !== FALSE);
   370     }
   370     }
   371 
   371 
   372     /**
   372     /**
   373      * @param $table
   373      * @param $table
   374      * @param $id
   374      * @param $id
   375      * @param $array
   375      * @param $array
   376      * @return int
   376      * @return bool
   377      */
   377      */
   378     public function store($table, $id, $array) {
   378     public function store($table, $id, $array) {
   379         $this->logger->info(sprintf('%s(%s, %d, %s) ', __METHOD__, $table, $id, print_r($array, true)));
   379         $this->logger->info(sprintf('%s(%s, %d, %s) ', __METHOD__, $table, $id, print_r($array, true)));
   380 
   380 
   381         $list = array();
   381         $list = array();
   391             UPDATE %s
   391             UPDATE %s
   392             SET %s
   392             SET %s
   393             WHERE id = %d
   393             WHERE id = %d
   394         ", $table, $listItems, $id);
   394         ", $table, $listItems, $id);
   395 
   395 
   396         return $this->pdo->exec($sql);
   396         return ($this->pdo->exec($sql) !== FALSE);
   397     }
   397     }
   398 
   398 
   399     /**
   399     /**
   400      * @param $table
   400      * @param $table
   401      * @param $id
   401      * @param $id
   407         $sql = sprintf("
   407         $sql = sprintf("
   408             DELETE FROM %s
   408             DELETE FROM %s
   409             WHERE id = %d;
   409             WHERE id = %d;
   410         ", $table, $id);
   410         ", $table, $id);
   411 
   411 
   412         return $this->pdo->exec($sql);
   412         return ($this->pdo->exec($sql) !== FALSE);
   413     }
   413     }
   414 
   414 
   415     public function getLastInsertedId() {
   415     public function getLastInsertedId() {
   416         return $this->pdo->lastInsertId();
   416         return $this->pdo->lastInsertId();
   417     }
   417     }
       
   418 
       
   419     public function getLastError() {
       
   420         return $this->pdo->errorCode();
       
   421     }
   418 }
   422 }