Example #1
0
 /**
  * Fetch the entire result set of a query and return it as an
  * associative array using the first column as the key.
  * 
  * Note: column names are not preserved when using this function.
  * 
  * <code>
  * For example, if the table 'mytable' contains:
  *
  *   ID      TEXT       DATE
  * --------------------------------
  *   1       'one'      944679408
  *   2       'two'      944679408
  *   3       'three'    944679408
  *
  * $q = new Query("SELECT id, text FROM mytable") 
  * $q->getAssoc() returns:
  *    array(
  *      '1' => array('one'),
  *      '2' => array('two'),
  *      '3' => array('three'),
  *    )
  * 
  * ... or call $q->getAssoc($scalar=true) to avoid wrapping results in an array (only
  * applies if only 2 cols are returned):
  *  array(
  *      '1' => 'one',
  *      '2' => 'two',
  *      '3' => 'three',
  *    )
  * </code>
  * Keep in mind that database functions in PHP usually return string
  * values for results regardless of the database's internal type.
  *
  * @param boolean $scalar Used only when the query returns
  * exactly two columns.  If TRUE, the values of second column are not
  * wrapped in an array.  Default here is false, in order to assure expected
  * behavior.
  * 
  * @return array Associative array with results from the query.
  * @author Lukas Smith <*****@*****.**> (MDB)
  */
 public function getAssoc($scalar = false)
 {
     $stmt = $this->conn->createStatement();
     if ($this->max) {
         $stmt->setLimit($this->max);
     }
     if ($this->start) {
         $stmt->setOffset($this->start);
     }
     $rs = $stmt->executeQuery($this->sql);
     $numcols = null;
     $results = array();
     while ($rs->next()) {
         $fields = $rs->getRow();
         if ($numcols === null) {
             $numcols = count($fields);
         }
         if (!$scalar || $numcols > 2) {
             $results[array_shift($fields)] = array_values($fields);
         } else {
             $results[array_shift($fields)] = array_shift($fields);
         }
     }
     $rs->close();
     $stmt->close();
     return $results;
 }
 public function testExecute()
 {
     $exch = DriverTestManager::getExchange('StatementTest.executeUpdate');
     $stmt = $this->conn->createStatement();
     $res = $stmt->execute($exch->getSql());
     $this->assertFalse($res, "Expected resulst of execute() to be FALSE because an update statement was executed (this is to match JDBC return values).");
     $this->assertEquals(1, $stmt->getUpdateCount());
     $exch = DriverTestManager::getExchange('StatementTest.executeQuery');
     $stmt = $this->conn->createStatement();
     $res = $stmt->execute($exch->getSql());
     $this->assertTrue($res, "Expected resulst of execute() to be TRUE because a select query was executed (this is to match JDBC return values).");
     $this->assertTrue($stmt->getResultSet() instanceof ResultSet, "Expected to be able to getResultSet() after call to execute() w/ SELECT query.");
     $stmt->close();
 }
 /**
  * Unlocks the specified table.
  *
  * @param      Connection $con The Creole connection to use.
  * @param      string $table The name of the table to unlock.
  * @throws     SQLException No Statement could be created or
  * executed.
  */
 public function unlockTable(Connection $con, $table)
 {
     $statement = $con->createStatement();
     $statement->executeUpdate("UNLOCK TABLES");
 }
 /**
  * Locks the specified table.
  *
  * @param      Connection $con The Creole connection to use.
  * @param      string $table The name of the table to lock.
  * @throws     SQLException No Statement could be created or executed.
  */
 public function lockTable(Connection $con, $table)
 {
     $statement = $con->createStatement();
     $sql = "SELECT next_id FROM " . $table . " FOR UPDATE";
     $statement->executeQuery($sql);
 }
 /**
  * @see Connection::createStatement()
  */
 public function createStatement()
 {
     $obj = $this->childConnection->createStatement();
     $objClass = get_class($obj);
     return new $objClass($this);
 }
 /**
  * @see Connection::createStatement()
  */
 public function createStatement()
 {
     return new DBArrayPreparedStatement($this, null);
     return new DBArrayPreparedStatement($this, $dataSql['sql']);
     krumo('DBArrayConnection createStatement ');
     die;
     $obj = $this->childConnection->createStatement();
     $objClass = get_class($obj);
     return new $objClass($this);
 }
 /**
  * Executes the passed SQL statements.
  */
 protected static function runStatements($statements, Connection $conn)
 {
     $stmt = $conn->createStatement();
     foreach ($statements as $sql) {
         // print "Executing : $sql \n";
         try {
             $stmt->execute($sql);
         } catch (Exception $e) {
             if (!stripos($e->getMessage(), "drop table")) {
                 print "Error executing SQL: " . $sql . "\n";
                 print $e->getMessage() . "\n";
                 print "=== Attempting to continue === \n";
             }
         }
     }
 }