public function query($stmt)
 {
     $c = $this->dbres->prepare($stmt);
     # this needs to be revisisted
     # throw a better exception, use DBIException class
     if (!$c) {
         $msg = join("\n", $this->dbres->errorInfo());
         $e = new DBIException($msg);
         $e->setStatement($stmt);
         throw $e;
     }
     $c->execute();
     return $c;
 }
Example #2
0
 public function execute()
 {
     $numargs = func_num_args();
     if ($numargs) {
         $this->bindings = func_get_args();
     }
     # bindings is initialized to an emtpy array in the constructor
     $stmt = '';
     $success = true;
     if ($this->bindtypes === "positional") {
         if (count($this->sections) > 1) {
             foreach ($this->sections as $s) {
                 $stmt .= $s;
                 if (count($this->bindings) < 1) {
                     $this->success = false;
                     break;
                 }
                 $v = array_shift($this->bindings);
                 $stmt .= $this->dbh->quote($v);
             }
         } else {
             $stmt = $this->sections[0];
         }
     } else {
         $stmt = $this->sql;
         $this->bindings = array_shift($this->bindings);
         if (!is_array($this->bindings)) {
             /* named binding parameters used, but didn't pass an array */
             throw new DBIException('named binding parameters used, but did not pass an array');
         }
         $search1 = array();
         $replace1 = array();
         foreach ($this->bindings as $k => $v) {
             $search1[] = "?:{$k}";
             $v = $this->dbh->quote($v);
             if (preg_match('/^\\w+:join$/', $k)) {
                 if (is_array($v)) {
                     $v = join(',', $v);
                 } else {
                     throw new DBIException(":join named parameter ({$k}) specified for non-array value");
                 }
             }
             if (is_array($v)) {
                 throw new DBIException("query value is an array");
             }
             $replace1[] = $v;
         }
         /* slight chance that the ?:\w+ string could appear in a quoted string */
         /* if there is a binding they didn't specify, let the SQL parser detect it */
         foreach ($search1 as $k => $v) {
             $stmt = preg_replace('/\\?' . $v . '\\b/', $replace1[$k], $stmt);
         }
     }
     if (!$stmt) {
         /* no statement resulted? */
         throw new DBIException('empty statement, this should not happen');
     }
     if (!$success) {
         /* too few binding parameters */
         throw new DBIException("too few binding parameters in query {$stmt}");
     }
     $this->bindings = array();
     $qstart = DBI::getmicrotime();
     $this->cursor_handle = $this->dbh->dbd->query($stmt);
     $qend = DBI::getmicrotime();
     $this->executed_stmt = $stmt;
     if (preg_match('/^\\s*(\\w+)\\b/', $stmt, $m)) {
         @DBI::$statement_types[strtolower($m[1])]++;
     }
     $qlen = sprintf('%0.5f', $qend - $qstart);
     if (!empty($_SERVER['debugsql'])) {
         d($stmt, 'execution time: ' . $qlen . ' sec');
     }
     #error_log($stmt);
     $this->execution_time = $qlen;
     DBI::$query_runtime += $qlen;
     if (!$this->cursor_handle) {
         $e = new DBIException($this->dbh->dbd->error());
         $e->setStatement($stmt);
         throw $e;
     }
     return $this->cursor_handle;
 }