Example #1
0
 function getSampleData($row)
 {
     $data = array();
     $data['user_name'] = 'user_' . $row;
     $data['user_password'] = '******';
     $data['subscribed'] = $row % 2 ? true : false;
     $data['user_id'] = $row;
     $data['quota'] = strval($row / 100);
     $data['weight'] = sqrt($row);
     $data['access_date'] = MDB2_Date::mdbToday();
     $data['access_time'] = MDB2_Date::mdbTime();
     $data['approved'] = MDB2_Date::mdbNow();
     return $data;
 }
 /**
  * http://pear.php.net/bugs/bug.php?id=3146
  */
 function testBug3146()
 {
     $data = array();
     $total_rows = 5;
     $query = 'INSERT INTO users (' . implode(', ', array_keys($this->fields)) . ') VALUES (' . implode(', ', array_fill(0, count($this->fields), '?')) . ')';
     $stmt = $this->db->prepare($query, array_values($this->fields), MDB2_PREPARE_MANIP);
     for ($row = 0; $row < $total_rows; $row++) {
         $data[$row]['user_name'] = "user_{$row}";
         $data[$row]['user_password'] = '******';
         $data[$row]['subscribed'] = (bool) ($row % 2);
         $data[$row]['user_id'] = $row;
         $data[$row]['quota'] = sprintf("%.2f", strval(1 + ($row + 1) / 100));
         $data[$row]['weight'] = sqrt($row);
         $data[$row]['access_date'] = MDB2_Date::mdbToday();
         $data[$row]['access_time'] = MDB2_Date::mdbTime();
         $data[$row]['approved'] = MDB2_Date::mdbNow();
         $result = $stmt->execute(array_values($data[$row]));
         if (PEAR::isError($result)) {
             $this->assertTrue(false, 'Error executing prepared query' . $result->getMessage());
         }
     }
     $stmt->free();
     $query = 'SELECT ' . implode(', ', array_keys($this->fields)) . ' FROM users ORDER BY user_id';
     $result =& $this->db->query($query, $this->fields);
     $numrows = $result->numRows($result);
     $this->verifyFetchedValues($result, 0, $data[0]);
     $this->verifyFetchedValues($result, 2, $data[2]);
     $this->verifyFetchedValues($result, null, $data[3]);
     $this->verifyFetchedValues($result, 1, $data[1]);
     $result->free();
 }
 /**
  * Test replace query
  *
  * The replace method emulates the replace query of mysql
  */
 function testReplace()
 {
     if (!$this->supported('replace')) {
         return;
     }
     $row = 1234;
     $data = $this->getSampleData($row);
     $fields = array('user_name' => array('value' => "user_{$row}", 'type' => 'text'), 'user_password' => array('value' => $data['user_password'], 'type' => 'text'), 'subscribed' => array('value' => $data['subscribed'], 'type' => 'boolean'), 'user_id' => array('value' => $data['user_id'], 'type' => 'integer', 'key' => 1), 'quota' => array('value' => $data['quota'], 'type' => 'decimal'), 'weight' => array('value' => $data['weight'], 'type' => 'float'), 'access_date' => array('value' => $data['access_date'], 'type' => 'date'), 'access_time' => array('value' => $data['access_time'], 'type' => 'time'), 'approved' => array('value' => $data['approved'], 'type' => 'timestamp'));
     $result = $this->db->replace('users', $fields);
     if (PEAR::isError($result)) {
         $this->assertTrue(false, 'Replace failed');
     }
     if ($this->db->supports('affected_rows')) {
         $affected_rows = $result;
         $this->assertEquals(1, $result, "replacing a row in an empty table returned incorrect value");
     }
     $result =& $this->db->query('SELECT ' . implode(', ', array_keys($this->fields)) . ' FROM users', $this->fields);
     if (PEAR::isError($result)) {
         $this->assertTrue(false, 'Error selecting from users' . $result->getMessage());
     }
     $this->verifyFetchedValues($result, 0, $data);
     $row = 4321;
     $fields['user_name']['value'] = $data['user_name'] = 'user_' . $row;
     $fields['user_password']['value'] = $data['user_password'] = '******';
     $fields['subscribed']['value'] = $data['subscribed'] = $row % 2 ? true : false;
     $fields['quota']['value'] = $data['quota'] = strval($row / 100);
     $fields['weight']['value'] = $data['weight'] = sqrt($row);
     $fields['access_date']['value'] = $data['access_date'] = MDB2_Date::mdbToday();
     $fields['access_time']['value'] = $data['access_time'] = MDB2_Date::mdbTime();
     $fields['approved']['value'] = $data['approved'] = MDB2_Date::mdbNow();
     $result = $this->db->replace('users', $fields);
     if (PEAR::isError($result)) {
         $this->assertTrue(false, 'Replace failed');
     }
     if ($this->db->supports('affected_rows')) {
         $this->assertEquals(2, $result, "replacing a row returned incorrect result");
     }
     $result =& $this->db->query('SELECT ' . implode(', ', array_keys($this->fields)) . ' FROM users', $this->fields);
     if (PEAR::isError($result)) {
         $this->assertTrue(false, 'Error selecting from users' . $result->getMessage());
     }
     $this->verifyFetchedValues($result, 0, $data);
     $this->assertTrue(!$result->valid(), 'the query result did not seem to have reached the end of result as expected');
     $result->free();
 }
 /**
  * http://pear.php.net/bugs/bug.php?id=946
  */
 function testBug946()
 {
     $data = array();
     $total_rows = 5;
     $prepared_query = $this->db->prepare('INSERT INTO users (user_name, user_password, subscribed, user_id, quota, weight, access_date, access_time, approved) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', $this->types);
     for ($row = 0; $row < $total_rows; $row++) {
         $data[$row]['user_name'] = "user_{$row}";
         $data[$row]['user_password'] = '******';
         $data[$row]['subscribed'] = (bool) ($row % 2);
         $data[$row]['user_id'] = $row;
         $data[$row]['quota'] = sprintf("%.2f", strval(1 + ($row + 1) / 100));
         $data[$row]['weight'] = sqrt($row);
         $data[$row]['access_date'] = MDB2_Date::mdbToday();
         $data[$row]['access_time'] = MDB2_Date::mdbTime();
         $data[$row]['approved'] = MDB2_Date::mdbNow();
         $this->insertTestValues($prepared_query, $data[$row]);
         $result = $this->db->execute($prepared_query);
         if (MDB2::isError($result)) {
             $this->assertTrue(false, 'Error executing prepared query' . $result->getMessage());
         }
     }
     $this->db->freePrepared($prepared_query);
     $this->db->setLimit(3, 1);
     $result = $this->db->query('SELECT * FROM users');
     $numrows = $result->numRows();
     while ($row = $result->fetchRow()) {
         if (MDB2::isError($row)) {
             $this->assertTrue(false, 'Error fetching a row' . $row->getMessage());
         }
     }
     $result->free();
     $result = $this->db->query('SELECT * FROM users');
     $numrows = $result->numRows();
     while ($row = $result->fetchRow()) {
         if (MDB2::isError($row)) {
             $this->assertTrue(false, 'Error fetching a row' . $row->getMessage());
         }
     }
     $result->free();
 }
Example #5
0
 /**
  * Inserts $message to the currently open database.  Calls open(),
  * if necessary.  Also passes the message along to any Log_observer
  * instances that are observing this Log.
  *
  * @param mixed  $message  String or object containing the message to log.
  * @param string $priority The priority of the message.  Valid
  *                  values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  *                  PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  *                  PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  * @return boolean  True on success or false on failure.
  * @access public
  */
 function log($message, $priority = null)
 {
     /* If a priority hasn't been specified, use the default value. */
     if ($priority === null) {
         $priority = $this->_priority;
     }
     /* Abort early if the priority is above the maximum logging level. */
     if (!$this->_isMasked($priority)) {
         return false;
     }
     /* If the connection isn't open and can't be opened, return failure. */
     if (!$this->_opened && !$this->open()) {
         return false;
     }
     /* If we don't already have a statement object, create one. */
     if (!is_object($this->_statement) && !$this->_prepareStatement()) {
         return false;
     }
     /* Extract the string representation of the message. */
     $message = $this->_extractMessage($message);
     /* Build our set of values for this log entry. */
     $values = array('id' => $this->_db->nextId($this->_sequence), 'logtime' => MDB2_Date::mdbNow(), 'ident' => $this->_ident, 'priority' => $priority, 'message' => $message);
     /* Execute the SQL query for this log entry insertion. */
     $this->_db->expectError(MDB2_ERROR_NOSUCHTABLE);
     $result =& $this->_statement->execute($values);
     $this->_db->popExpect();
     /* Attempt to handle any errors. */
     if (PEAR::isError($result)) {
         /* We can only handle MDB2_ERROR_NOSUCHTABLE errors. */
         if ($result->getCode() != MDB2_ERROR_NOSUCHTABLE) {
             return false;
         }
         /* Attempt to create the target table. */
         if (!$this->_createTable()) {
             return false;
         }
         /* Recreate our prepared statement resource. */
         $this->_statement->free();
         if (!$this->_prepareStatement()) {
             return false;
         }
         /* Attempt to re-execute the insertion query. */
         $result = $this->_statement->execute($values);
         if (PEAR::isError($result)) {
             return false;
         }
     }
     $this->_announce(array('priority' => $priority, 'message' => $message));
     return true;
 }
 /**
  * Testing transaction support
  */
 function testTransactions()
 {
     if (!$this->supported('transactions')) {
         return;
     }
     $this->db->autoCommit(0);
     $row = 0;
     $data = array();
     $data['user_name'] = "user_{$row}";
     $data['user_password'] = '******';
     $data['subscribed'] = $row % 2 ? true : false;
     $data['user_id'] = $row;
     $data['quota'] = strval($row / 100);
     $data['weight'] = sqrt($row);
     $data['access_date'] = MDB2_Date::mdbToday();
     $data['access_time'] = MDB2_Date::mdbTime();
     $data['approved'] = MDB2_Date::mdbNow();
     $prepared_query = $this->db->prepare('INSERT INTO users (user_name, user_password, subscribed, user_id, quota, weight, access_date, access_time, approved) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', $this->types);
     $this->insertTestValues($prepared_query, $data);
     $result = $this->db->execute($prepared_query);
     $this->db->rollback();
     $result =& $this->db->query('SELECT * FROM users');
     if (MDB2::isError($result)) {
         $this->assertTrue(false, 'Error selecting from users' . $result->getMessage());
     }
     $this->assertTrue(!$result->valid(), 'Transaction rollback did not revert the row that was inserted');
     $result->free();
     $this->insertTestValues($prepared_query, $data);
     $result = $this->db->execute($prepared_query);
     $this->db->commit();
     $result =& $this->db->query('SELECT * FROM users');
     if (MDB2::isError($result)) {
         $this->assertTrue(false, 'Error selecting from users' . $result->getMessage());
     }
     $this->assertTrue($result->valid(), 'Transaction commit did not make permanent the row that was inserted');
     $result->free();
     $result =& $this->db->query('DELETE FROM users');
     if (MDB2::isError($result)) {
         $this->assertTrue(false, 'Error deleting from users' . $result->getMessage());
         $this->db->rollback();
     }
     $autocommit = $this->db->autocommit(1);
     $this->assertTrue(!MDB2::isError($autocommit), 'Error autocommiting transactions');
     $this->db->freePrepared($prepared_query);
     $result =& $this->db->query('SELECT * FROM users');
     if (MDB2::isError($result)) {
         $this->assertTrue(false, 'Error selecting from users' . $result->getMessage());
     }
     $this->assertTrue(!$result->valid(), 'Transaction end with implicit commit when re-enabling auto-commit did not make permanent the rows that were deleted');
     $result->free();
 }
 /**
  * @see http://pear.php.net/bugs/bug.php?id=670
  */
 function testBug670()
 {
     $data['user_name'] = null;
     $data['user_password'] = '******';
     $data['subscribed'] = true;
     $data['user_id'] = 1;
     $data['quota'] = sprintf("%.2f", strval(3 / 100));
     $data['weight'] = sqrt(1);
     $data['access_date'] = MDB2_Date::mdbToday();
     $data['access_time'] = MDB2_Date::mdbTime();
     $data['approved'] = MDB2_Date::mdbNow();
     $stmt = $this->db->prepare('INSERT INTO users (' . implode(', ', array_keys($this->fields)) . ') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', array_values($this->fields), MDB2_PREPARE_MANIP);
     $result = $stmt->execute(array_values($data));
     $result =& $this->db->query('SELECT user_name FROM users');
     $col = $result->fetchCol('user_name');
     if (PEAR::isError($col)) {
         $this->fail('Error when fetching column first first row as NULL: ' . $col->getMessage());
     }
     $data['user_name'] = "user_1";
     $data['user_id'] = 2;
     $result = $stmt->execute(array_values($data));
     $result =& $this->db->query('SELECT user_name FROM users');
     $col = $result->fetchCol('user_name');
     if (PEAR::isError($col)) {
         $this->fail('Error when fetching column: ' . $col->getMessage());
     }
     $data['user_name'] = null;
     $stmt->free();
 }
Example #8
0
 function insertSessionDataToDatabase()
 {
     // Stores session data to database.
     $sql = "INSERT INTO ilmo_users (ilmo_id, id_string, confirmed, time) VALUES ({$this->newSignupid}, '{$this->sessionId}', 0, '" . MDB2_Date::mdbNow() . "')";
     $this->database->doQuery($sql);
 }
<?php

require_once "../DBInterface.php";
require_once "../ErrorReportEnabler.php";
MDB2::loadFile("Date");
$sql = "SELECT (" . MDB2_Date::mdbNow() . " - " . MDB2_Date::unix2Mdbstamp(strtotime('Jan 18, 2007')) . ")";
print "<p>{$sql}</p>";
$result = query($conn, $sql);
print_r($result->fetchRow());
Example #10
0
 /**
  * Test replace query
  *
  * The replace method emulates the replace query of mysql
  *
  * @dataProvider provider
  */
 public function testReplace($ci)
 {
     $this->manualSetUp($ci);
     if (!$this->supported('replace')) {
         $this->markTestSkipped('REPLACE not supported');
     }
     $row = 1234;
     $data = $this->getSampleData($row);
     $fields = array('user_name' => array('value' => "user_{$row}", 'type' => 'text'), 'user_password' => array('value' => $data['user_password'], 'type' => 'text'), 'subscribed' => array('value' => $data['subscribed'], 'type' => 'boolean'), 'user_id' => array('value' => $data['user_id'], 'type' => 'integer', 'key' => 1), 'quota' => array('value' => $data['quota'], 'type' => 'decimal'), 'weight' => array('value' => $data['weight'], 'type' => 'float'), 'access_date' => array('value' => $data['access_date'], 'type' => 'date'), 'access_time' => array('value' => $data['access_time'], 'type' => 'time'), 'approved' => array('value' => $data['approved'], 'type' => 'timestamp'));
     $result = $this->db->replace($this->table_users, $fields);
     if (MDB2::isError($result)) {
         $this->fail('Replace failed');
     }
     if ($this->db->supports('affected_rows')) {
         $this->assertEquals(1, $result, "replacing a row in an empty table returned incorrect value");
     }
     $query = 'SELECT ' . implode(', ', array_keys($this->fields)) . ' FROM ' . $this->table_users;
     $result = $this->db->query($query, $this->fields);
     if (MDB2::isError($result)) {
         $this->fail('Error selecting from users' . $result->getUserInfo());
     }
     $this->verifyFetchedValues($result, 0, $data);
     $row = 4321;
     $fields['user_name']['value'] = $data['user_name'] = 'user_' . $row;
     $fields['user_password']['value'] = $data['user_password'] = '******';
     $fields['subscribed']['value'] = $data['subscribed'] = $row % 2 ? true : false;
     $fields['quota']['value'] = $data['quota'] = strval($row / 100);
     $fields['weight']['value'] = $data['weight'] = sqrt($row);
     $fields['access_date']['value'] = $data['access_date'] = MDB2_Date::mdbToday();
     $fields['access_time']['value'] = $data['access_time'] = MDB2_Date::mdbTime();
     $fields['approved']['value'] = $data['approved'] = MDB2_Date::mdbNow();
     $result = $this->db->replace($this->table_users, $fields);
     if (MDB2::isError($result)) {
         $this->fail('Replace failed');
     }
     if ($this->db->supports('affected_rows')) {
         switch ($this->db->phptype) {
             case 'sqlite':
                 $expect = 1;
                 break;
             default:
                 $expect = 2;
         }
         $this->assertEquals($expect, $result, "replacing a row returned incorrect result");
     }
     $query = 'SELECT ' . implode(', ', array_keys($this->fields)) . ' FROM ' . $this->table_users;
     $result = $this->db->query($query, $this->fields);
     if (MDB2::isError($result)) {
         $this->fail('Error selecting from users' . $result->getUserInfo());
     }
     $this->verifyFetchedValues($result, 0, $data);
     $this->assertTrue(!$result->valid(), 'the query result did not seem to have reached the end of result as expected');
     $result->free();
 }
Example #11
0
 /**
  * Return string to call a variable with the current timestamp inside an SQL statement
  * There are three special variables for current date and time:
  * - CURRENT_TIMESTAMP (date and time, TIMESTAMP type)
  * - CURRENT_DATE (date, DATE type)
  * - CURRENT_TIME (time, TIME type)
  *
  * @return string to call a variable with the current timestamp
  * @access public
  */
 public function now()
 {
     if ($this->dbLayer === 'MDB2') {
         return MDB2_Date::mdbNow();
     } elseif ($this->dbLayer === 'PDO') {
         return date('Y-m-d H:i:s');
     }
 }