public function query(\Maphper\Lib\Query $query)
 {
     $stmt = $this->getCachedStmt($query->getSql());
     $args = $query->getArgs();
     foreach ($args as &$arg) {
         if ($arg instanceof \DateTime) {
             $arg = $arg->format('Y-m-d H:i:s');
         }
     }
     $res = $stmt->execute($args);
     if (strpos(trim($query->getSql()), 'SELECT') === 0) {
         return $stmt->fetchAll(\PDO::FETCH_OBJ);
     } else {
         return $stmt;
     }
 }
Example #2
0
 public function query(\Maphper\Lib\Query $query)
 {
     $queryId = md5($query->getSql());
     if (isset($this->queryCache[$queryId])) {
         $stmt = $this->queryCache[$queryId];
     } else {
         $stmt = $this->pdo->prepare($query->getSql(), [\PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY]);
         if ($stmt) {
             $this->queryCache[$queryId] = $stmt;
         }
     }
     $args = $query->getArgs();
     foreach ($args as &$arg) {
         if ($arg instanceof \DateTime) {
             if ($arg->format('H:i:s') == '00:00:00') {
                 $arg = $arg->format('Y-m-d');
             } else {
                 $arg = $arg->format('Y-m-d H:i:s');
             }
         }
     }
     if ($stmt !== false) {
         try {
             if (count($args) > 0) {
                 $res = $stmt->execute($args);
             } else {
                 $res = $stmt->execute();
             }
             if (substr($query->getSql(), 0, 6) === 'SELECT') {
                 return $stmt->fetchAll(\PDO::FETCH_OBJ);
             } else {
                 return $stmt;
             }
         } catch (\Exception $e) {
             //SQLite causes an error if when the DB schema changes, rebuild $stmt and try again.
             if ($e->getMessage() == 'SQLSTATE[HY000]: General error: 17 database schema has changed') {
                 unset($this->queryCache[$queryId]);
                 return $this->query($query);
             } else {
                 return $stmt;
             }
         }
     }
 }