Example #1
0
 /**
  * Will make a query with the mysql server
  * @param string $query
  * @return mixed return query result
  */
 function query($query)
 {
     $this->lastquery = $query;
     $this->query_cache[] = $query;
     $this->querys += 1;
     $res = new db_result($this->CLASS);
     $res->setQuery($query);
     $res->setResult(sqlite_query($query, $this->connection));
     if ($res->getResult() === false) {
         $this->CLASS['error']->log("ERROR IN QUERY: \"{$query}\"", 1, sqlite_errno() . ":" . sqlite_errno());
     }
     return $res;
 }
Example #2
0
 /**
  * Will make a query with the mysql server
  * @param string $query
  * @return mixed return query result
  */
 function query($query)
 {
     $this->lastquery = $query;
     $this->query_cache[] = $query;
     $this->querys += 1;
     $res = new db_result($this->CLASS);
     $res->setQuery($query);
     $res->setResult($this->connection->query($query));
     if ($res->getResult() === false) {
         echo "#" . $query . "#";
         $this->CLASS['error']->log("ERROR IN QUERY: \"{$query}\"", 1, $this->connection->lastErrorMsg() . ":" . $this->connection->lastErrorCode());
     }
     return $res;
 }
Example #3
0
 /**
  * Will make a query with the server
  * @param string $query
  * @return resource return query result
  */
 function query($query)
 {
     $this->lastquery = $query;
     $this->query_cache[] = $query;
     $this->querys += 1;
     $res = new db_result($this->CLASS);
     $res->setQuery($query);
     $res->setResult($this->mdb2->query($query));
     // if error in query
     if (PEAR::isError($res->getResult())) {
         $this->CLASS['error']->log("ERROR IN QUERY: \"{$query}\"", 1, $res->getResult()->getMessage());
     }
     return $res;
 }
Example #4
0
 /**
  * Will make a query with the oracle server
  * @param string $query
  * @return mixed return query result
  */
 function query($query)
 {
     $this->lastquery = $query;
     $this->query_cache[] = $query;
     $this->querys += 1;
     $res = new db_result($this->CLASS);
     $res->setQuery($query);
     $res->setResult(oci_parse($this->connection, $query));
     oci_execute($res->getResult());
     // if error in query
     if ($res->getResult() === false) {
         //$this->CLASS['error']->log("ERROR IN QUERY: \"$query\"",1,oci_last_error($this->connection));
     }
     return $res;
 }
Example #5
0
 /**
  * Execute a query.
  *
  * syntax:
 	%%: literal %
 	%.: auto-detect
 	%s: string with quotes and escaping
 	%c: string with quotes and escaping, embraced by percent signs for
 	    usage with LIKE
 	%i: integer
 	%f: floating point
 	%l: literal (no quoting/escaping)
 	%_: nothing, but do process one argument
 	%A?: array of type ?, comma separated
 	%S:  array of key => ., becomes key=., comma separated
 	%SS: array of key => ., becomes key=., "AND" separated
 
 	query can be prepended with a keyword to change the returned data
 	format:
 	- returnid: for use with INSERT, returns the auto_increment value used
 	  for that row.
 	- returnaffected: return the number of modified rows by this query.
 	- tuple, value: select exactly one row or one value
 	- maybetuple, maybevalue: select zero or one rows or values
 	- column: return a list of a single attribute
 	- table: return complete result in one array
 	- keytable: same as table but arraykey is the field called ARRAYKEY
 	- keyvaluetable: select two columns, and this returns a map from the first
 	  field (key) to the second (exactly one value)
 */
 public function q()
 {
     $this->connect();
     $argv = func_get_args();
     $format = trim(array_shift($argv));
     list($key) = preg_split('/\\s+/', $format, 2);
     $key = strtolower($key);
     $maybe = false;
     switch ($key) {
         // modifying commands; keywords first, then regular
         case 'returnid':
         case 'returnaffected':
             $format = substr($format, strlen($key) + 1);
         case 'insert':
         case 'update':
         case 'replace':
         case 'delete':
         case 'set':
             $type = 'update';
             break;
             // select commandos; keywords, then regular
         // select commandos; keywords, then regular
         case 'maybetuple':
         case 'maybevalue':
             $maybe = true;
             $key = substr($key, 5, 5);
             // ATTENTION: the substr below will use the new key as its
             // keylength, that's why we have to take the length of
             // VALUE/TUPLE from the format. Luckily BOTH are 5 long.
             $format = substr($format, 5);
         case 'column':
         case 'table':
         case 'keytable':
         case 'keyvaluetable':
         case 'tuple':
         case 'value':
             $format = substr($format, strlen($key) + 1);
         case 'select':
         case 'describe':
         case 'show':
             $type = 'select';
             break;
             // transactions
         // transactions
         case 'start':
             // start transaction. Do not support BEGIN, it's deprecated
         // start transaction. Do not support BEGIN, it's deprecated
         case 'commit':
         case 'rollback':
             $type = 'transaction';
             break;
         default:
             throw new InvalidArgumentException("SQL command/lib keyword '{$key}' unknown!");
     }
     $parts = explode('%', $format);
     $literal = false;
     foreach ($parts as $part) {
         if ($literal) {
             $literal = false;
             $query .= $part;
             continue;
         }
         if (!isset($query)) {
             // first part
             $query = $part;
             continue;
         }
         if (!$part) {
             // literal %%
             $query .= '%';
             $literal = true;
             continue;
         }
         if (!$argv) {
             throw new BadMethodCallException("Not enough arguments");
         }
         $val = array_shift($argv);
         switch ($part[0]) {
             case 'A':
                 if (!is_array($val) || !$val) {
                     $backtrace = debug_backtrace();
                     if (DEBUG) {
                         $callsite = 'in file: ' . $backtrace[0]['file'] . ', ' . ' line: ' . $backtrace[0]['line'] . ', ';
                     } else {
                         $callsite = '';
                     }
                     throw new InvalidArgumentException("%A in \$DATABASE->q() has to correspond to an " . "non-empty array, it is" . " now a '{$val}' (Query:" . "'{$key} {$query}')! {$callsite}");
                 }
                 $GLOBALS['MODE'] = $part[1];
                 $query .= implode(', ', array_map(array($this, 'val2sql'), $val));
                 unset($GLOBALS['MODE']);
                 $query .= substr($part, 2);
                 break;
             case 'S':
                 $parts = array();
                 foreach ($val as $field => $value) {
                     $parts[] = '`' . $field . '` = ' . $this->val2sql($value);
                 }
                 $separator = ', ';
                 $skip = 1;
                 if (strlen($part) > 1 && $part[1] == 'S') {
                     $separator = ' AND ';
                     $skip = 2;
                 }
                 $query .= implode($separator, $parts);
                 unset($parts);
                 $query .= substr($part, $skip);
                 break;
             case 's':
             case 'c':
             case 'i':
             case 'f':
             case 'l':
             case '.':
                 $query .= $this->val2sql($val, $part[0]);
                 $query .= substr($part, 1);
                 break;
             case '_':
                 // eat one argument
                 $query .= substr($part, 1);
                 break;
             default:
                 throw new InvalidArgumentException("Unknown %-code: " . $part[0]);
         }
     }
     if ($literal) {
         user_error("Internal error in q()", E_USER_ERROR);
     }
     if ($argv) {
         if (DEBUG) {
             $backtrace = debug_backtrace();
             $callsite = ' in file: ' . $backtrace[0]['file'] . ', ' . ' line: ' . $backtrace[0]['line'] . ', ';
         } else {
             $callsite = '';
         }
         throw new BadMethodCallException("Not all arguments to q() are" . " processed.\n{$callsite}");
     }
     $res = $this->execute($query);
     // nothing left to do if transaction statement...
     if ($type == 'transaction') {
         return null;
     }
     if ($type == 'update') {
         if ($key == 'returnid') {
             return mysqli_insert_id($this->_connection);
         }
         if ($key == 'returnaffected') {
             return mysqli_affected_rows($this->_connection);
         }
         return;
     }
     $res = new db_result($res);
     if ($key == 'tuple' || $key == 'value') {
         if ($res->count() < 1) {
             if ($maybe) {
                 return NULL;
             }
             throw new UnexpectedValueException("{$this->database} query error" . " ({$key} {$query}): Query did not return any rows");
         }
         if ($res->count() > 1) {
             throw new UnexpectedValueException("{$this->database} query error" . "({$key} {$query}): Query returned too many rows" . "(" . $res->count() . ")");
         }
         $row = $res->next();
         if ($key == 'value') {
             return array_shift($row);
         }
         return $row;
     }
     if ($key == 'table') {
         return $res->gettable();
     }
     if ($key == 'keytable') {
         return $res->getkeytable('ARRAYKEY');
     }
     if ($key == 'keyvaluetable') {
         return $res->getkeyvaluetable();
     }
     if ($key == 'column') {
         return $res->getcolumn();
     }
     return $res;
 }
Example #6
0
 /**
  * Will make a query with the postgresql server
  * @param string $query
  * @return mixed return query result
  */
 function query($query)
 {
     $this->lastquery = $query;
     $this->query_cache[] = $query;
     $this->querys += 1;
     $res = new db_result($this->CLASS);
     $res->setQuery($query);
     $res->setResult(pg_query($this->connection, $query));
     // if error in query
     if ($res->getResult() === false) {
         $this->CLASS['error']->log("ERROR IN QUERY: \"{$query}\"", 1, pg_last_error($this->connection));
     }
     return $res;
 }
Example #7
0
 public function query()
 {
     $this->startTime = microtime(true);
     $this->uniqueQueryKey = "{QUERYKEY_" . $this->generateRandomString() . "}";
     // init system if needed
     $this->init();
     $args = func_get_args();
     $query = array_shift($args);
     if (count($args) >= 1) {
         $this->replaceVars($query, $args);
     }
     try {
         //echo $query;
         $this->dbObj->real_query($query);
         if (mysqli_error($this->dbObj)) {
             throw new exception(mysqli_error($this->dbObj), mysqli_errno($this->dbObj));
         }
         $endtime = microtime(true);
         $this->lastQueryTime = $endtime - $this->startTime;
         $res = new db_result($this->dbObj);
         $infoArray = array("query" => $query);
         if ($res) {
             $infoArray["rowsNum"] = $res->getNumRows();
         }
         $this->log(db::SUCCESS, $infoArray);
         $res->runAfterFetchAll = $this->runAfterFetchAll;
         $res->runAfterFetch = $this->runAfterFetch;
         return $res;
     } catch (Exception $e) {
         echo "<b>" . $e->getMessage() . "</b><br />\r\n";
         var_dump($e->getTrace());
         $this->log(db::ERROR, array("query" => $query, "message" => $e->getTrace()));
     }
     return true;
 }