Esempio n. 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;
 }
Esempio n. 2
0
// since we are on php5 we can use the magic __call() method to:
// - load the manager module: $mdb2->loadModule('Manager', null, true);
// - redirect the method call to the manager module: $mdb2->manager->createTable('sometable', $fields);
$mdb2->mgCreateTable($table, $fields);
$query = "INSERT INTO {$table} (somename, somedate) VALUES (:name, :date)";
// parameters:
// 1) the query (notice we are using named parameters, but we could also use ? instead
// 2) types of the placeholders (either keyed numerically in order or by name)
// 3) MDB2_PREPARE_MANIP denotes a DML statement
$stmt = $mdb2->prepare($query, array('text', 'date'), MDB2_PREPARE_MANIP);
if (PEAR::isError($stmt)) {
    die($stmt->getMessage());
}
// load Date helper class
MDB2::loadFile('Date');
$stmt->execute(array('name' => 'hello', 'date' => MDB2_Date::mdbToday()));
// get the last inserted id
echo 'last insert id: ';
var_dump($mdb2->lastInsertId($table, 'id'));
$stmt->execute(array('name' => 'world', 'date' => '2005-11-11'));
// get the last inserted id
echo 'last insert id: ';
var_dump($mdb2->lastInsertId($table, 'id'));
// load Iterator implementations
MDB2::loadFile('Iterator');
$query = 'SELECT * FROM ' . $table;
// parameters:
// 1) the query
// 2) true means MDB2 tries to determine the result set type automatically
// 3) true is the default and means that internally a MDB2_Result instance should be created
// 4) 'MDB2_BufferedIterator' means the MDB2_Result should be wrapped inside an SeekableIterator
Esempio n. 3
0
 /**
  * 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();
 }
 /**
  * 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();
 }
Esempio n. 7
0
 /**
  * @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();
 }
Esempio n. 8
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();
 }