Compare commits
82 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eb36858f1a | ||
|
|
1d797b306e | ||
|
|
7c819bfc24 | ||
|
|
1d147e169a | ||
|
|
391702700d | ||
|
|
37fd169be3 | ||
|
|
7c5c03c576 | ||
|
|
a0a2f702e2 | ||
|
|
2707bcba7d | ||
|
|
7b0da839de | ||
|
|
1d51c2b674 | ||
|
|
de63573e98 | ||
|
|
26fcce650a | ||
|
|
e403c774c8 | ||
|
|
740ca7bc67 | ||
|
|
7563c660ad | ||
|
|
3741291f44 | ||
|
|
7e00296b32 | ||
|
|
e2e95adda2 | ||
|
|
34a3de3f84 | ||
|
|
cc8dbe47eb | ||
|
|
8090f4e7dc | ||
|
|
58e564c3d5 | ||
|
|
253e803160 | ||
|
|
c05a575be0 | ||
|
|
e61d66fdf8 | ||
|
|
e9a55de0b4 | ||
|
|
9e4cbc28c1 | ||
|
|
b6397a719c | ||
|
|
65ec35c591 | ||
|
|
1d61a11098 | ||
|
|
ecd5fe190f | ||
|
|
e94f75fe2a | ||
|
|
ce11e65070 | ||
|
|
6cb757797b | ||
|
|
027e1529ba | ||
|
|
e03ac71b45 | ||
|
|
f89d70e49e | ||
|
|
6babe98b11 | ||
|
|
039468f3a3 | ||
|
|
5e9507181f | ||
|
|
7364df36cf | ||
|
|
7df5c9baa2 | ||
|
|
ed347e841a | ||
|
|
2ffb770f8a | ||
|
|
ed9c29fe49 | ||
|
|
9a41bf34a1 | ||
|
|
24c1b930df | ||
|
|
d40bc57dde | ||
|
|
c62fc187ee | ||
|
|
6b324d1291 | ||
|
|
5b4e5a959a | ||
|
|
7582961ee1 | ||
|
|
2d49f4f350 | ||
|
|
9930fc073a | ||
|
|
788c506f16 | ||
|
|
bbf9534326 | ||
|
|
1d5844bcc0 | ||
|
|
34c0f5e0a7 | ||
|
|
7825f90080 | ||
|
|
a1f01a4f87 | ||
|
|
80cc78edcd | ||
|
|
5050205e58 | ||
|
|
9f3aa571dc | ||
|
|
a2800ef04b | ||
|
|
164c7157a6 | ||
|
|
f09308a3f7 | ||
|
|
69bfe15ebb | ||
|
|
7621b50a02 | ||
|
|
4122fe52c1 | ||
|
|
8072f6ab07 | ||
|
|
860da8d18a | ||
|
|
0ed2837f3d | ||
|
|
65cae974c5 | ||
|
|
96892fa6c2 | ||
|
|
ad4889da09 | ||
|
|
12604d7854 | ||
|
|
8c5299c2e2 | ||
|
|
964a36e4c2 | ||
|
|
68774532e1 | ||
|
|
2985815750 | ||
|
|
4faebb957c |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -0,0 +1 @@
|
||||
simpletest/test_setup.php
|
||||
|
||||
139
README.md
Normal file
139
README.md
Normal file
@@ -0,0 +1,139 @@
|
||||
MeekroDB -- The Simple PHP MySQL Library
|
||||
========
|
||||
Learn more: http://www.meekro.com
|
||||
|
||||
MeekroDB is:
|
||||
|
||||
* A PHP MySQL library that lets you **get more done with fewer lines of code**, and **makes SQL injection 100% impossible**.
|
||||
* Google's #1 search result for "php mysql library" for over 2 years, with **thousands of deployments worldwide**.
|
||||
* A library with a **perfect security track record**. No bugs relating to security or SQL injection have ever been discovered.
|
||||
|
||||
Installation
|
||||
========
|
||||
When you're ready to get started, see the [Quick Start Guide](http://www.meekro.com/quickstart.php) on our website.
|
||||
|
||||
### Manual Setup
|
||||
Include the `db.class.php` file into your project and set it up like this:
|
||||
|
||||
require_once 'db.class.php';
|
||||
DB::$user = 'my_database_user';
|
||||
DB::$password = 'my_database_password';
|
||||
DB::$dbName = 'my_database_name';
|
||||
|
||||
### Composer
|
||||
Add this to your `composer.json`
|
||||
|
||||
{
|
||||
"require": {
|
||||
"sergeytsalkov/meekrodb": "*"
|
||||
}
|
||||
}
|
||||
|
||||
Code Examples
|
||||
========
|
||||
### Grab some rows from the database and print out a field from each row.
|
||||
|
||||
$accounts = DB::query("SELECT * FROM accounts WHERE type = %s AND age > %i", $type, 15);
|
||||
foreach ($accounts as $account) {
|
||||
echo $account['username'] . "\n";
|
||||
}
|
||||
|
||||
### Insert a new row.
|
||||
|
||||
DB::insert('mytable', array(
|
||||
'name' => $name,
|
||||
'rank' => $rank,
|
||||
'location' => $location,
|
||||
'age' => $age,
|
||||
'intelligence' => $intelligence
|
||||
));
|
||||
|
||||
### Grab one row or field
|
||||
|
||||
$account = DB::queryFirstRow("SELECT * FROM accounts WHERE username=%s", 'Joe');
|
||||
$number_accounts = DB::queryFirstField("SELECT COUNT(*) FROM accounts");
|
||||
|
||||
### Use a list in a query
|
||||
DB::query("SELECT * FROM tbl WHERE name IN %ls AND age NOT IN %li", array('John', 'Bob'), array(12, 15));
|
||||
|
||||
### Nested Transactions
|
||||
|
||||
DB::$nested_transactions = true;
|
||||
DB::startTransaction(); // outer transaction
|
||||
// .. some queries..
|
||||
$depth = DB::startTransaction(); // inner transaction
|
||||
echo $depth . 'transactions are currently active'; // 2
|
||||
|
||||
// .. some queries..
|
||||
DB::commit(); // commit inner transaction
|
||||
// .. some queries..
|
||||
DB::commit(); // commit outer transaction
|
||||
|
||||
### Lots More - See: http://www.meekro.com/docs.php
|
||||
|
||||
|
||||
How is MeekroDB better than PDO?
|
||||
========
|
||||
### Optional Static Class Mode
|
||||
Most web apps will only ever talk to one database. This means that
|
||||
passing $db objects to every function of your code just adds unnecessary clutter.
|
||||
The simplest approach is to use static methods such as DB::query(), and that's how
|
||||
MeekroDB works. Still, if you need database objects, MeekroDB can do that too.
|
||||
|
||||
### Do more with fewer lines of code
|
||||
The code below escapes your parameters for safety, runs the query, and grabs
|
||||
the first row of results. Try doing that in one line with PDO.
|
||||
|
||||
$account = DB::queryFirstRow("SELECT * FROM accounts WHERE username=%s", 'Joe');
|
||||
|
||||
### Work with list parameters easily
|
||||
Using MySQL's IN keyword should not be hard. MeekroDB smooths out the syntax for you,
|
||||
PDO does not.
|
||||
|
||||
$accounts = DB::query("SELECT * FROM accounts WHERE username IN %ls", array('Joe', 'Frank'));
|
||||
|
||||
|
||||
### Simple inserts
|
||||
Using MySQL's INSERT should not be more complicated than passing in an
|
||||
associative array. MeekroDB also simplifies many related commands, including
|
||||
the useful and bizarre INSERT .. ON DUPLICATE UPDATE command. PDO does none of this.
|
||||
|
||||
DB::insert('accounts', array('username' => 'John', 'password' => 'whatever'));
|
||||
|
||||
### Focus on the goal, not the task
|
||||
Want to do INSERT yourself rather than relying on DB::insert()?
|
||||
It's dead simple. I don't even want to think about how many lines
|
||||
you'd need to pull this off in PDO.
|
||||
|
||||
// Insert 2 rows at once
|
||||
DB::query("INSERT INTO %b %lb VALUES %?", 'accounts',
|
||||
array('username', 'password', 'last_login_timestamp'),
|
||||
array(
|
||||
array('Joe', 'joes_password', new DateTime('yesterday')),
|
||||
array('Frank', 'franks_password', new DateTime('last Monday'))
|
||||
)
|
||||
);
|
||||
|
||||
### Nested transactions
|
||||
MySQL's SAVEPOINT commands lets you create nested transactions, but only
|
||||
if you keep track of SAVEPOINT ids yourself. MeekroDB does this for you,
|
||||
so you can have nested transactions with no complexity or learning curve.
|
||||
|
||||
DB::$nested_transactions = true;
|
||||
DB::startTransaction(); // outer transaction
|
||||
// .. some queries..
|
||||
$depth = DB::startTransaction(); // inner transaction
|
||||
echo $depth . 'transactions are currently active'; // 2
|
||||
|
||||
// .. some queries..
|
||||
DB::commit(); // commit inner transaction
|
||||
// .. some queries..
|
||||
DB::commit(); // commit outer transaction
|
||||
|
||||
### Flexible error and success handlers
|
||||
Set your own custom function run on errors, or on every query that succeeds.
|
||||
You can easily have separate error handling behavior for the dev and live
|
||||
versions of your application. Want to count up all your queries and their
|
||||
runtime? Just add a new success handler.
|
||||
|
||||
### More about MeekroDB's design philosophy: http://www.meekro.com/beliefs.php
|
||||
29
composer.json
Normal file
29
composer.json
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "SergeyTsalkov/meekrodb",
|
||||
"description": "The Simple PHP/MySQL Library",
|
||||
"homepage": "http://www.meekro.com",
|
||||
"support": {
|
||||
"email": "support@meekro.com"
|
||||
},
|
||||
"keywords": [
|
||||
"mysql",
|
||||
"database",
|
||||
"mysqli",
|
||||
"pdo"
|
||||
],
|
||||
"license": "LGPL-3.0",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Sergey Tsalkov",
|
||||
"email": "stsalkov@gmail.com"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.2.0"
|
||||
},
|
||||
"autoload": {
|
||||
"classmap": [
|
||||
"db.class.php"
|
||||
]
|
||||
}
|
||||
}
|
||||
1077
db.class.php
1077
db.class.php
File diff suppressed because it is too large
Load Diff
@@ -1,37 +1,32 @@
|
||||
<?php
|
||||
function new_error_callback($params) {
|
||||
global $error_callback_worked;
|
||||
|
||||
if (substr_count($params['error'], 'You have an error in your SQL syntax')) $error_callback_worked = 1;
|
||||
}
|
||||
|
||||
function my_debug_handler($params) {
|
||||
global $debug_callback_worked;
|
||||
if (substr_count($params['query'], 'SELECT')) $debug_callback_worked = 1;
|
||||
}
|
||||
|
||||
|
||||
class BasicTest extends SimpleTest {
|
||||
function __construct() {
|
||||
error_reporting(E_ALL);
|
||||
require_once '../db.class.php';
|
||||
DB::$user = 'libdb_user';
|
||||
DB::$password = 'sdf235sklj';
|
||||
DB::$dbName = 'libdb_test';
|
||||
DB::query("DROP DATABASE libdb_test");
|
||||
DB::query("CREATE DATABASE libdb_test");
|
||||
DB::useDB('libdb_test');
|
||||
foreach (DB::tableList() as $table) {
|
||||
DB::query("DROP TABLE %b", $table);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function test_1_create_table() {
|
||||
DB::query("CREATE TABLE `accounts` (
|
||||
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
|
||||
`profile_id` INT NOT NULL,
|
||||
`username` VARCHAR( 255 ) NOT NULL ,
|
||||
`password` VARCHAR( 255 ) NOT NULL ,
|
||||
`password` VARCHAR( 255 ) NULL ,
|
||||
`age` INT NOT NULL DEFAULT '10',
|
||||
`height` DOUBLE NOT NULL DEFAULT '10.0'
|
||||
`height` DOUBLE NOT NULL DEFAULT '10.0',
|
||||
`favorite_word` VARCHAR( 255 ) NULL DEFAULT 'hi',
|
||||
`birthday` TIMESTAMP NOT NULL
|
||||
) ENGINE = InnoDB");
|
||||
|
||||
DB::query("CREATE TABLE `profile` (
|
||||
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
|
||||
`signature` VARCHAR( 255 ) NULL DEFAULT 'donewriting'
|
||||
) ENGINE = InnoDB");
|
||||
|
||||
$mysqli = DB::get();
|
||||
DB::disconnect();
|
||||
@$this->assert($mysqli->server_info === null);
|
||||
}
|
||||
|
||||
function test_1_5_empty_table() {
|
||||
@@ -55,11 +50,12 @@ class BasicTest extends SimpleTest {
|
||||
}
|
||||
|
||||
function test_2_insert_row() {
|
||||
DB::insert('accounts', array(
|
||||
$true = DB::insert('accounts', array(
|
||||
'username' => 'Abe',
|
||||
'password' => 'hello'
|
||||
));
|
||||
|
||||
$this->assert($true === true);
|
||||
$this->assert(DB::affectedRows() === 1);
|
||||
|
||||
$counter = DB::queryFirstField("SELECT COUNT(*) FROM accounts");
|
||||
@@ -73,22 +69,54 @@ class BasicTest extends SimpleTest {
|
||||
'age' => 15,
|
||||
'height' => 10.371
|
||||
));
|
||||
|
||||
DB::insert('`libdb_test`.`accounts`', array(
|
||||
$dbname = DB::$dbName;
|
||||
DB::insert("`$dbname`.`accounts`", array(
|
||||
'username' => 'Charlie\'s Friend',
|
||||
'password' => 'goodbye',
|
||||
'age' => 30,
|
||||
'height' => 155.23
|
||||
'height' => 155.23,
|
||||
'favorite_word' => null,
|
||||
));
|
||||
|
||||
$this->assert(DB::insertId() === 3);
|
||||
|
||||
$counter = DB::queryFirstField("SELECT COUNT(*) FROM accounts");
|
||||
$this->assert($counter === strval(3));
|
||||
|
||||
$bart = DB::queryFirstRow("SELECT * FROM accounts WHERE age IN %li AND height IN %ld AND username IN %ls",
|
||||
DB::insert('`accounts`', array(
|
||||
'username' => 'Deer',
|
||||
'password' => '',
|
||||
'age' => 15,
|
||||
'height' => 10.371
|
||||
));
|
||||
|
||||
$username = DB::queryFirstField("SELECT username FROM accounts WHERE password=%s0", null);
|
||||
$this->assert($username === 'Deer');
|
||||
|
||||
$password = DB::queryFirstField("SELECT password FROM accounts WHERE favorite_word IS NULL");
|
||||
$this->assert($password === 'goodbye');
|
||||
|
||||
DB::$usenull = false;
|
||||
DB::insertUpdate('accounts', array(
|
||||
'id' => 3,
|
||||
'favorite_word' => null,
|
||||
));
|
||||
|
||||
$password = DB::queryFirstField("SELECT password FROM accounts WHERE favorite_word=%s AND favorite_word=%s", null, '');
|
||||
$this->assert($password === 'goodbye');
|
||||
|
||||
DB::$usenull = true;
|
||||
DB::insertUpdate('accounts', array(
|
||||
'id' => 3,
|
||||
'favorite_word' => null,
|
||||
));
|
||||
|
||||
DB::$param_char = '###';
|
||||
$bart = DB::queryFirstRow("SELECT * FROM accounts WHERE age IN ###li AND height IN ###ld AND username IN ###ls",
|
||||
array(15, 25), array(10.371, 150.123), array('Bart', 'Barts'));
|
||||
$this->assert($bart['username'] === 'Bart');
|
||||
DB::insert('accounts', array('username' => 'f_u'));
|
||||
DB::query("DELETE FROM accounts WHERE username=###s", 'f_u');
|
||||
DB::$param_char = '%';
|
||||
|
||||
$charlie_password = DB::queryFirstField("SELECT password FROM accounts WHERE username IN %ls AND username = %s",
|
||||
array('Charlie', 'Charlie\'s Friend'), 'Charlie\'s Friend');
|
||||
@@ -107,62 +135,284 @@ class BasicTest extends SimpleTest {
|
||||
$this->assert($username === 'Bart');
|
||||
$this->assert($password === 'hello');
|
||||
$this->assert($age == 15);
|
||||
|
||||
$mysqli_result = DB::queryRaw("SELECT * FROM accounts WHERE favorite_word IS NULL");
|
||||
$this->assert($mysqli_result instanceof MySQLi_Result);
|
||||
$row = $mysqli_result->fetch_assoc();
|
||||
$this->assert($row['password'] === 'goodbye');
|
||||
$this->assert($mysqli_result->fetch_assoc() === null);
|
||||
}
|
||||
|
||||
function test_4_query() {
|
||||
$results = DB::query("SELECT * FROM accounts WHERE username=%s", 'Charlie\'s Friend');
|
||||
DB::update('accounts', array(
|
||||
'birthday' => new DateTime('10 September 2000 13:13:13')
|
||||
), 'username=%s', 'Charlie\'s Friend');
|
||||
|
||||
$results = DB::query("SELECT * FROM accounts WHERE username=%s AND birthday IN %lt", 'Charlie\'s Friend', array('September 10 2000 13:13:13'));
|
||||
$this->assert(count($results) === 1);
|
||||
$this->assert($results[0]['age'] == 30 && $results[0]['password'] == 'goodbye');
|
||||
$this->assert($results[0]['age'] === '30' && $results[0]['password'] === 'goodbye');
|
||||
$this->assert($results[0]['birthday'] == '2000-09-10 13:13:13');
|
||||
|
||||
$results = DB::query("SELECT * FROM accounts WHERE username!=%s", "Charlie's Friend");
|
||||
$this->assert(count($results) === 2);
|
||||
$this->assert(count($results) === 3);
|
||||
|
||||
$columnlist = DB::columnList('accounts');
|
||||
$this->assert(count($columnlist) === 8);
|
||||
$this->assert($columnlist[0] === 'id');
|
||||
$this->assert($columnlist[5] === 'height');
|
||||
|
||||
$tablelist = DB::tableList();
|
||||
$this->assert(count($tablelist) === 2);
|
||||
$this->assert($tablelist[0] === 'accounts');
|
||||
|
||||
$tablelist = null;
|
||||
$tablelist = DB::tableList(DB::$dbName);
|
||||
$this->assert(count($tablelist) === 2);
|
||||
$this->assert($tablelist[0] === 'accounts');
|
||||
}
|
||||
|
||||
function test_5_error_handler() {
|
||||
global $error_callback_worked;
|
||||
function test_4_1_query() {
|
||||
DB::insert('accounts', array(
|
||||
'username' => 'newguy',
|
||||
'password' => DB::sqleval("REPEAT('blah', %i)", '3'),
|
||||
'age' => DB::sqleval('171+1'),
|
||||
'height' => 111.15
|
||||
));
|
||||
|
||||
DB::$error_handler = 'new_error_callback';
|
||||
DB::query("SELET * FROM accounts");
|
||||
$this->assert($error_callback_worked === 1);
|
||||
$row = DB::queryOneRow("SELECT * FROM accounts WHERE password=%s", 'blahblahblah');
|
||||
$this->assert($row['username'] === 'newguy');
|
||||
$this->assert($row['age'] === '172');
|
||||
|
||||
$true = DB::update('accounts', array(
|
||||
'password' => DB::sqleval("REPEAT('blah', %i)", 4),
|
||||
'favorite_word' => null,
|
||||
), 'username=%s', 'newguy');
|
||||
|
||||
$row = null;
|
||||
$row = DB::queryOneRow("SELECT * FROM accounts WHERE username=%s", 'newguy');
|
||||
$this->assert($true === true);
|
||||
$this->assert($row['password'] === 'blahblahblahblah');
|
||||
$this->assert($row['favorite_word'] === null);
|
||||
|
||||
$row = DB::query("SELECT * FROM accounts WHERE password=%s_mypass AND (password=%s_mypass) AND username=%s_myuser",
|
||||
array('myuser' => 'newguy', 'mypass' => 'blahblahblahblah')
|
||||
);
|
||||
$this->assert(count($row) === 1);
|
||||
$this->assert($row[0]['username'] === 'newguy');
|
||||
$this->assert($row[0]['age'] === '172');
|
||||
|
||||
$true = DB::query("DELETE FROM accounts WHERE password=%s", 'blahblahblahblah');
|
||||
$this->assert($true === true);
|
||||
$this->assert(DB::affectedRows() === 1);
|
||||
}
|
||||
|
||||
function test_6_exception_catch() {
|
||||
DB::$error_handler = '';
|
||||
DB::$throw_exception_on_error = true;
|
||||
try {
|
||||
DB::query("SELET * FROM accounts");
|
||||
} catch(MeekroDBException $e) {
|
||||
$this->assert(substr_count($e->getMessage(), 'You have an error in your SQL syntax'));
|
||||
$this->assert($e->getQuery() === 'SELET * FROM accounts');
|
||||
$exception_was_caught = 1;
|
||||
}
|
||||
$this->assert($exception_was_caught === 1);
|
||||
function test_4_2_delete() {
|
||||
DB::insert('accounts', array(
|
||||
'username' => 'gonesoon',
|
||||
'password' => 'something',
|
||||
'age' => 61,
|
||||
'height' => 199.194
|
||||
));
|
||||
|
||||
try {
|
||||
DB::insert('`libdb_test`.`accounts`', array(
|
||||
'id' => 2,
|
||||
'username' => 'Another Dude\'s \'Mom"',
|
||||
'password' => 'asdfsdse',
|
||||
'age' => 35,
|
||||
'height' => 555.23
|
||||
));
|
||||
} catch(MeekroDBException $e) {
|
||||
$this->assert(substr_count($e->getMessage(), 'Duplicate entry'));
|
||||
$exception_was_caught = 2;
|
||||
}
|
||||
$this->assert($exception_was_caught === 2);
|
||||
$ct = DB::queryFirstField("SELECT COUNT(*) FROM accounts WHERE username=%s AND height=%d", 'gonesoon', 199.194);
|
||||
$this->assert(intval($ct) === 1);
|
||||
|
||||
$ct = DB::queryFirstField("SELECT COUNT(*) FROM accounts WHERE username=%s1 AND height=%d0 AND height=%d", 199.194, 'gonesoon');
|
||||
$this->assert(intval($ct) === 1);
|
||||
|
||||
DB::delete('accounts', 'username=%s AND age=%i AND height=%d', 'gonesoon', '61', '199.194');
|
||||
$this->assert(DB::affectedRows() === 1);
|
||||
|
||||
$ct = DB::queryFirstField("SELECT COUNT(*) FROM accounts WHERE username=%s AND height=%d", 'gonesoon', '199.194');
|
||||
$this->assert(intval($ct) === 0);
|
||||
}
|
||||
|
||||
function test_7_debugmode_handler() {
|
||||
global $debug_callback_worked;
|
||||
function test_4_3_insertmany() {
|
||||
$ins[] = array(
|
||||
'username' => '1ofmany',
|
||||
'password' => 'something',
|
||||
'age' => 23,
|
||||
'height' => 190.194
|
||||
);
|
||||
$ins[] = array(
|
||||
'password' => 'somethingelse',
|
||||
'username' => '2ofmany',
|
||||
'age' => 25,
|
||||
'height' => 190.194
|
||||
);
|
||||
$ins[] = array(
|
||||
'password' => NULL,
|
||||
'username' => '3ofmany',
|
||||
'age' => 15,
|
||||
'height' => 111.951
|
||||
);
|
||||
|
||||
DB::debugMode('my_debug_handler');
|
||||
DB::query("SELECT * FROM accounts WHERE username!=%s", "Charlie's Friend");
|
||||
DB::insert('accounts', $ins);
|
||||
$this->assert(DB::affectedRows() === 3);
|
||||
|
||||
$this->assert($debug_callback_worked === 1);
|
||||
$rows = DB::query("SELECT * FROM accounts WHERE height=%d ORDER BY age ASC", 190.194);
|
||||
$this->assert(count($rows) === 2);
|
||||
$this->assert($rows[0]['username'] === '1ofmany');
|
||||
$this->assert($rows[0]['age'] === '23');
|
||||
$this->assert($rows[1]['age'] === '25');
|
||||
$this->assert($rows[1]['password'] === 'somethingelse');
|
||||
$this->assert($rows[1]['username'] === '2ofmany');
|
||||
|
||||
DB::debugMode(false);
|
||||
$nullrow = DB::queryOneRow("SELECT * FROM accounts WHERE username LIKE %ss", '3ofman');
|
||||
$this->assert($nullrow['password'] === NULL);
|
||||
$this->assert($nullrow['age'] === '15');
|
||||
}
|
||||
|
||||
|
||||
|
||||
function test_5_insert_blobs() {
|
||||
DB::query("CREATE TABLE `store data` (
|
||||
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
|
||||
`picture` BLOB
|
||||
) ENGINE = InnoDB");
|
||||
|
||||
$columns = DB::columnList('store data');
|
||||
$this->assert(count($columns) === 2);
|
||||
$this->assert($columns[1] === 'picture');
|
||||
|
||||
|
||||
$smile = file_get_contents('smile1.jpg');
|
||||
DB::insert('store data', array(
|
||||
'picture' => $smile,
|
||||
));
|
||||
DB::queryOneRow("INSERT INTO %b (picture) VALUES (%s)", 'store data', $smile);
|
||||
|
||||
$getsmile = DB::queryFirstField("SELECT picture FROM %b WHERE id=1", 'store data');
|
||||
$getsmile2 = DB::queryFirstField("SELECT picture FROM %b WHERE id=2", 'store data');
|
||||
$this->assert($smile === $getsmile);
|
||||
$this->assert($smile === $getsmile2);
|
||||
}
|
||||
|
||||
function test_6_insert_ignore() {
|
||||
DB::insertIgnore('accounts', array(
|
||||
'id' => 1, //duplicate primary key
|
||||
'username' => 'gonesoon',
|
||||
'password' => 'something',
|
||||
'age' => 61,
|
||||
'height' => 199.194
|
||||
));
|
||||
}
|
||||
|
||||
function test_7_insert_update() {
|
||||
$true = DB::insertUpdate('accounts', array(
|
||||
'id' => 2, //duplicate primary key
|
||||
'username' => 'gonesoon',
|
||||
'password' => 'something',
|
||||
'age' => 61,
|
||||
'height' => 199.194
|
||||
), 'age = age + %i', 1);
|
||||
|
||||
$this->assert($true === true);
|
||||
$this->assert(DB::affectedRows() === 2); // a quirk of MySQL, even though only 1 row was updated
|
||||
|
||||
$result = DB::query("SELECT * FROM accounts WHERE age = %i", 16);
|
||||
$this->assert(count($result) === 1);
|
||||
$this->assert($result[0]['height'] === '10.371');
|
||||
|
||||
DB::insertUpdate('accounts', array(
|
||||
'id' => 2, //duplicate primary key
|
||||
'username' => 'blahblahdude',
|
||||
'age' => 233,
|
||||
'height' => 199.194
|
||||
));
|
||||
|
||||
$result = DB::query("SELECT * FROM accounts WHERE age = %i", 233);
|
||||
$this->assert(count($result) === 1);
|
||||
$this->assert($result[0]['height'] === '199.194');
|
||||
$this->assert($result[0]['username'] === 'blahblahdude');
|
||||
|
||||
DB::insertUpdate('accounts', array(
|
||||
'id' => 2, //duplicate primary key
|
||||
'username' => 'gonesoon',
|
||||
'password' => 'something',
|
||||
'age' => 61,
|
||||
'height' => 199.194
|
||||
), array(
|
||||
'age' => 74,
|
||||
));
|
||||
|
||||
$result = DB::query("SELECT * FROM accounts WHERE age = %i", 74);
|
||||
$this->assert(count($result) === 1);
|
||||
$this->assert($result[0]['height'] === '199.194');
|
||||
$this->assert($result[0]['username'] === 'blahblahdude');
|
||||
|
||||
$multiples[] = array(
|
||||
'id' => 3, //duplicate primary key
|
||||
'username' => 'gonesoon',
|
||||
'password' => 'something',
|
||||
'age' => 61,
|
||||
'height' => 199.194
|
||||
);
|
||||
$multiples[] = array(
|
||||
'id' => 1, //duplicate primary key
|
||||
'username' => 'gonesoon',
|
||||
'password' => 'something',
|
||||
'age' => 61,
|
||||
'height' => 199.194
|
||||
);
|
||||
|
||||
DB::insertUpdate('accounts', $multiples, array('age' => 914));
|
||||
$this->assert(DB::affectedRows() === 4);
|
||||
|
||||
$result = DB::query("SELECT * FROM accounts WHERE age=914 ORDER BY id ASC");
|
||||
$this->assert(count($result) === 2);
|
||||
$this->assert($result[0]['username'] === 'Abe');
|
||||
$this->assert($result[1]['username'] === 'Charlie\'s Friend');
|
||||
|
||||
$true = DB::query("UPDATE accounts SET age=15, username='Bart' WHERE age=%i", 74);
|
||||
$this->assert($true === true);
|
||||
$this->assert(DB::affectedRows() === 1);
|
||||
}
|
||||
|
||||
function test_8_lb() {
|
||||
$data = array(
|
||||
'username' => 'vookoo',
|
||||
'password' => 'dookoo',
|
||||
);
|
||||
|
||||
$true = DB::query("INSERT into accounts %lb VALUES %ls", array_keys($data), array_values($data));
|
||||
$result = DB::query("SELECT * FROM accounts WHERE username=%s", 'vookoo');
|
||||
$this->assert($true === true);
|
||||
$this->assert(count($result) === 1);
|
||||
$this->assert($result[0]['password'] === 'dookoo');
|
||||
}
|
||||
|
||||
function test_9_fullcolumns() {
|
||||
$true = DB::insert('profile', array(
|
||||
'id' => 1,
|
||||
'signature' => 'u_suck'
|
||||
));
|
||||
DB::query("UPDATE accounts SET profile_id=1 WHERE id=2");
|
||||
|
||||
$r = DB::queryFullColumns("SELECT accounts.*, profile.*, 1+1 FROM accounts
|
||||
INNER JOIN profile ON accounts.profile_id=profile.id");
|
||||
|
||||
$this->assert($true === true);
|
||||
$this->assert(count($r) === 1);
|
||||
$this->assert($r[0]['accounts.id'] === '2');
|
||||
$this->assert($r[0]['profile.id'] === '1');
|
||||
$this->assert($r[0]['profile.signature'] === 'u_suck');
|
||||
$this->assert($r[0]['1+1'] === '2');
|
||||
}
|
||||
|
||||
function test_901_updatewithspecialchar() {
|
||||
$data = 'www.mysite.com/product?s=t-%s-%%3d%%3d%i&RCAID=24322';
|
||||
DB::update('profile', array('signature' => $data), 'id=%i', 1);
|
||||
$signature = DB::queryFirstField("SELECT signature FROM profile WHERE id=%i", 1);
|
||||
$this->assert($signature === $data);
|
||||
|
||||
DB::update('profile',array('signature'=> "%li "),"id = %d",1);
|
||||
$signature = DB::queryFirstField("SELECT signature FROM profile WHERE id=%i", 1);
|
||||
$this->assert($signature === "%li ");
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
17
simpletest/CallTest.php
Normal file
17
simpletest/CallTest.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
class CallTest extends SimpleTest {
|
||||
function test_1_create_procedure() {
|
||||
DB::query("DROP PROCEDURE IF EXISTS myProc");
|
||||
DB::query("CREATE PROCEDURE myProc()
|
||||
BEGIN
|
||||
SELECT * FROM accounts;
|
||||
END");
|
||||
}
|
||||
|
||||
function test_2_run_procedure() {
|
||||
$r = DB::query("CALL myProc()");
|
||||
$this->assert($r[0]['username'] === 'Abe');
|
||||
$this->assert($r[2]['age'] === '914');
|
||||
}
|
||||
|
||||
}
|
||||
83
simpletest/ErrorTest.php
Normal file
83
simpletest/ErrorTest.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
function new_error_callback($params) {
|
||||
global $error_callback_worked;
|
||||
|
||||
if (substr_count($params['error'], 'You have an error in your SQL syntax')) $error_callback_worked = 1;
|
||||
}
|
||||
|
||||
function my_debug_handler($params) {
|
||||
global $debug_callback_worked;
|
||||
if (substr_count($params['query'], 'SELECT')) $debug_callback_worked = 1;
|
||||
}
|
||||
|
||||
class ErrorTest extends SimpleTest {
|
||||
function test_1_error_handler() {
|
||||
global $error_callback_worked, $static_error_callback_worked, $nonstatic_error_callback_worked;
|
||||
|
||||
DB::$error_handler = 'new_error_callback';
|
||||
DB::query("SELET * FROM accounts");
|
||||
$this->assert($error_callback_worked === 1);
|
||||
|
||||
DB::$error_handler = array('ErrorTest', 'static_error_callback');
|
||||
DB::query("SELET * FROM accounts");
|
||||
$this->assert($static_error_callback_worked === 1);
|
||||
|
||||
DB::$error_handler = array($this, 'nonstatic_error_callback');
|
||||
DB::query("SELET * FROM accounts");
|
||||
$this->assert($nonstatic_error_callback_worked === 1);
|
||||
|
||||
}
|
||||
|
||||
public static function static_error_callback($params) {
|
||||
global $static_error_callback_worked;
|
||||
if (substr_count($params['error'], 'You have an error in your SQL syntax')) $static_error_callback_worked = 1;
|
||||
}
|
||||
|
||||
public function nonstatic_error_callback($params) {
|
||||
global $nonstatic_error_callback_worked;
|
||||
if (substr_count($params['error'], 'You have an error in your SQL syntax')) $nonstatic_error_callback_worked = 1;
|
||||
}
|
||||
|
||||
function test_2_exception_catch() {
|
||||
$dbname = DB::$dbName;
|
||||
DB::$error_handler = '';
|
||||
DB::$throw_exception_on_error = true;
|
||||
try {
|
||||
DB::query("SELET * FROM accounts");
|
||||
} catch(MeekroDBException $e) {
|
||||
$this->assert(substr_count($e->getMessage(), 'You have an error in your SQL syntax'));
|
||||
$this->assert($e->getQuery() === 'SELET * FROM accounts');
|
||||
$exception_was_caught = 1;
|
||||
}
|
||||
$this->assert($exception_was_caught === 1);
|
||||
|
||||
try {
|
||||
DB::insert("`$dbname`.`accounts`", array(
|
||||
'id' => 2,
|
||||
'username' => 'Another Dude\'s \'Mom"',
|
||||
'password' => 'asdfsdse',
|
||||
'age' => 35,
|
||||
'height' => 555.23
|
||||
));
|
||||
} catch(MeekroDBException $e) {
|
||||
$this->assert(substr_count($e->getMessage(), 'Duplicate entry'));
|
||||
$exception_was_caught = 2;
|
||||
}
|
||||
$this->assert($exception_was_caught === 2);
|
||||
}
|
||||
|
||||
function test_3_debugmode_handler() {
|
||||
global $debug_callback_worked;
|
||||
|
||||
DB::debugMode('my_debug_handler');
|
||||
DB::query("SELECT * FROM accounts WHERE username!=%s", "Charlie's Friend");
|
||||
|
||||
$this->assert($debug_callback_worked === 1);
|
||||
|
||||
DB::debugMode(false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
19
simpletest/ErrorTest_53.php
Normal file
19
simpletest/ErrorTest_53.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
class ErrorTest_53 extends SimpleTest {
|
||||
function test_1_error_handler() {
|
||||
global $anonymous_error_callback_worked;
|
||||
|
||||
DB::$throw_exception_on_error = false;
|
||||
DB::$error_handler = function($params) {
|
||||
global $anonymous_error_callback_worked;
|
||||
if (substr_count($params['error'], 'You have an error in your SQL syntax')) $anonymous_error_callback_worked = 1;
|
||||
};
|
||||
DB::query("SELET * FROM accounts");
|
||||
$this->assert($anonymous_error_callback_worked === 1);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
51
simpletest/HelperTest.php
Normal file
51
simpletest/HelperTest.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
class HelperTest extends SimpleTest {
|
||||
function test_1_verticalslice() {
|
||||
$all = DB::query("SELECT * FROM accounts ORDER BY id ASC");
|
||||
$names = DBHelper::verticalSlice($all, 'username');
|
||||
$this->assert(count($names) === 5);
|
||||
$this->assert($names[0] === 'Abe');
|
||||
|
||||
$ages = DBHelper::verticalSlice($all, 'age', 'username');
|
||||
$this->assert(count($ages) === 5);
|
||||
$this->assert($ages['Abe'] === '700');
|
||||
}
|
||||
|
||||
function test_2_reindex() {
|
||||
$all = DB::query("SELECT * FROM accounts ORDER BY id ASC");
|
||||
$names = DBHelper::reIndex($all, 'username');
|
||||
$this->assert(count($names) === 5);
|
||||
$this->assert($names['Bart']['username'] === 'Bart');
|
||||
$this->assert($names['Bart']['age'] === '15');
|
||||
|
||||
$names = DBHelper::reIndex($all, 'username', 'age');
|
||||
$this->assert($names['Bart']['15']['username'] === 'Bart');
|
||||
$this->assert($names['Bart']['15']['age'] === '15');
|
||||
}
|
||||
|
||||
function test_3_empty() {
|
||||
$none = DB::query("SELECT * FROM accounts WHERE username=%s", 'doesnotexist');
|
||||
$this->assert(is_array($none) && count($none) === 0);
|
||||
$names = DBHelper::verticalSlice($none, 'username', 'age');
|
||||
$this->assert(is_array($names) && count($names) === 0);
|
||||
|
||||
$names_other = DBHelper::reIndex($none, 'username', 'age');
|
||||
$this->assert(is_array($names_other) && count($names_other) === 0);
|
||||
}
|
||||
|
||||
function test_4_null() {
|
||||
DB::query("UPDATE accounts SET password = NULL WHERE username=%s", 'Bart');
|
||||
|
||||
$all = DB::query("SELECT * FROM accounts ORDER BY id ASC");
|
||||
$ages = DBHelper::verticalSlice($all, 'age', 'password');
|
||||
$this->assert(count($ages) === 5);
|
||||
$this->assert($ages[''] === '15');
|
||||
|
||||
$passwords = DBHelper::reIndex($all, 'password');
|
||||
$this->assert(count($passwords) === 5);
|
||||
$this->assert($passwords['']['username'] === 'Bart');
|
||||
$this->assert($passwords['']['password'] === NULL);
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
||||
312
simpletest/ObjectTest.php
Normal file
312
simpletest/ObjectTest.php
Normal file
@@ -0,0 +1,312 @@
|
||||
<?php
|
||||
class ObjectTest extends SimpleTest {
|
||||
public $mdb;
|
||||
|
||||
function __construct() {
|
||||
$this->mdb = new MeekroDB();
|
||||
|
||||
foreach ($this->mdb->tableList() as $table) {
|
||||
$this->mdb->query("DROP TABLE %b", $table);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function test_1_create_table() {
|
||||
$this->mdb->query("CREATE TABLE `accounts` (
|
||||
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
|
||||
`username` VARCHAR( 255 ) NOT NULL ,
|
||||
`password` VARCHAR( 255 ) NULL ,
|
||||
`age` INT NOT NULL DEFAULT '10',
|
||||
`height` DOUBLE NOT NULL DEFAULT '10.0',
|
||||
`favorite_word` VARCHAR( 255 ) NULL DEFAULT 'hi'
|
||||
) ENGINE = InnoDB");
|
||||
}
|
||||
|
||||
function test_1_5_empty_table() {
|
||||
$counter = $this->mdb->queryFirstField("SELECT COUNT(*) FROM accounts");
|
||||
$this->assert($counter === strval(0));
|
||||
|
||||
$row = $this->mdb->queryFirstRow("SELECT * FROM accounts");
|
||||
$this->assert($row === null);
|
||||
|
||||
$field = $this->mdb->queryFirstField("SELECT * FROM accounts");
|
||||
$this->assert($field === null);
|
||||
|
||||
$field = $this->mdb->queryOneField('nothere', "SELECT * FROM accounts");
|
||||
$this->assert($field === null);
|
||||
|
||||
$column = $this->mdb->queryFirstColumn("SELECT * FROM accounts");
|
||||
$this->assert(is_array($column) && count($column) === 0);
|
||||
|
||||
$column = $this->mdb->queryOneColumn('nothere', "SELECT * FROM accounts"); //TODO: is this what we want?
|
||||
$this->assert(is_array($column) && count($column) === 0);
|
||||
}
|
||||
|
||||
function test_2_insert_row() {
|
||||
$this->mdb->insert('accounts', array(
|
||||
'username' => 'Abe',
|
||||
'password' => 'hello'
|
||||
));
|
||||
|
||||
$this->assert($this->mdb->affectedRows() === 1);
|
||||
|
||||
$counter = $this->mdb->queryFirstField("SELECT COUNT(*) FROM accounts");
|
||||
$this->assert($counter === strval(1));
|
||||
}
|
||||
|
||||
function test_3_more_inserts() {
|
||||
$this->mdb->insert('`accounts`', array(
|
||||
'username' => 'Bart',
|
||||
'password' => 'hello',
|
||||
'age' => 15,
|
||||
'height' => 10.371
|
||||
));
|
||||
$dbname = $this->mdb->dbName;
|
||||
$this->mdb->insert("`$dbname`.`accounts`", array(
|
||||
'username' => 'Charlie\'s Friend',
|
||||
'password' => 'goodbye',
|
||||
'age' => 30,
|
||||
'height' => 155.23,
|
||||
'favorite_word' => null,
|
||||
));
|
||||
|
||||
$this->assert($this->mdb->insertId() === 3);
|
||||
|
||||
$counter = $this->mdb->queryFirstField("SELECT COUNT(*) FROM accounts");
|
||||
$this->assert($counter === strval(3));
|
||||
|
||||
$password = $this->mdb->queryFirstField("SELECT password FROM accounts WHERE favorite_word IS NULL");
|
||||
$this->assert($password === 'goodbye');
|
||||
|
||||
$this->mdb->param_char = '###';
|
||||
$bart = $this->mdb->queryFirstRow("SELECT * FROM accounts WHERE age IN ###li AND height IN ###ld AND username IN ###ls",
|
||||
array(15, 25), array(10.371, 150.123), array('Bart', 'Barts'));
|
||||
$this->assert($bart['username'] === 'Bart');
|
||||
$this->mdb->param_char = '%';
|
||||
|
||||
$charlie_password = $this->mdb->queryFirstField("SELECT password FROM accounts WHERE username IN %ls AND username = %s",
|
||||
array('Charlie', 'Charlie\'s Friend'), 'Charlie\'s Friend');
|
||||
$this->assert($charlie_password === 'goodbye');
|
||||
|
||||
$charlie_password = $this->mdb->queryOneField('password', "SELECT * FROM accounts WHERE username IN %ls AND username = %s",
|
||||
array('Charlie', 'Charlie\'s Friend'), 'Charlie\'s Friend');
|
||||
$this->assert($charlie_password === 'goodbye');
|
||||
|
||||
$passwords = $this->mdb->queryFirstColumn("SELECT password FROM accounts WHERE username=%s", 'Bart');
|
||||
$this->assert(count($passwords) === 1);
|
||||
$this->assert($passwords[0] === 'hello');
|
||||
|
||||
$username = $password = $age = null;
|
||||
list($age, $username, $password) = $this->mdb->queryOneList("SELECT age,username,password FROM accounts WHERE username=%s", 'Bart');
|
||||
$this->assert($username === 'Bart');
|
||||
$this->assert($password === 'hello');
|
||||
$this->assert($age == 15);
|
||||
|
||||
$mysqli_result = $this->mdb->queryRaw("SELECT * FROM accounts WHERE favorite_word IS NULL");
|
||||
$this->assert($mysqli_result instanceof MySQLi_Result);
|
||||
$row = $mysqli_result->fetch_assoc();
|
||||
$this->assert($row['password'] === 'goodbye');
|
||||
$this->assert($mysqli_result->fetch_assoc() === null);
|
||||
}
|
||||
|
||||
function test_4_query() {
|
||||
$results = $this->mdb->query("SELECT * FROM accounts WHERE username=%s", 'Charlie\'s Friend');
|
||||
$this->assert(count($results) === 1);
|
||||
$this->assert($results[0]['age'] == 30 && $results[0]['password'] == 'goodbye');
|
||||
|
||||
$results = $this->mdb->query("SELECT * FROM accounts WHERE username!=%s", "Charlie's Friend");
|
||||
$this->assert(count($results) === 2);
|
||||
|
||||
$columnlist = $this->mdb->columnList('accounts');
|
||||
$this->assert(count($columnlist) === 6);
|
||||
$this->assert($columnlist[0] === 'id');
|
||||
$this->assert($columnlist[4] === 'height');
|
||||
|
||||
$tablelist = $this->mdb->tableList();
|
||||
$this->assert(count($tablelist) === 1);
|
||||
$this->assert($tablelist[0] === 'accounts');
|
||||
|
||||
$tablelist = null;
|
||||
$tablelist = $this->mdb->tableList($this->mdb->dbName);
|
||||
$this->assert(count($tablelist) === 1);
|
||||
$this->assert($tablelist[0] === 'accounts');
|
||||
}
|
||||
|
||||
function test_4_1_query() {
|
||||
$this->mdb->insert('accounts', array(
|
||||
'username' => 'newguy',
|
||||
'password' => $this->mdb->sqleval("REPEAT('blah', %i)", '3'),
|
||||
'age' => $this->mdb->sqleval('171+1'),
|
||||
'height' => 111.15
|
||||
));
|
||||
|
||||
$row = $this->mdb->queryOneRow("SELECT * FROM accounts WHERE password=%s", 'blahblahblah');
|
||||
$this->assert($row['username'] === 'newguy');
|
||||
$this->assert($row['age'] === '172');
|
||||
|
||||
$this->mdb->update('accounts', array(
|
||||
'password' => $this->mdb->sqleval("REPEAT('blah', %i)", 4),
|
||||
'favorite_word' => null,
|
||||
), 'username=%s', 'newguy');
|
||||
|
||||
$row = null;
|
||||
$row = $this->mdb->queryOneRow("SELECT * FROM accounts WHERE username=%s", 'newguy');
|
||||
$this->assert($row['password'] === 'blahblahblahblah');
|
||||
$this->assert($row['favorite_word'] === null);
|
||||
|
||||
$this->mdb->query("DELETE FROM accounts WHERE password=%s", 'blahblahblahblah');
|
||||
$this->assert($this->mdb->affectedRows() === 1);
|
||||
}
|
||||
|
||||
function test_4_2_delete() {
|
||||
$this->mdb->insert('accounts', array(
|
||||
'username' => 'gonesoon',
|
||||
'password' => 'something',
|
||||
'age' => 61,
|
||||
'height' => 199.194
|
||||
));
|
||||
|
||||
$ct = $this->mdb->queryFirstField("SELECT COUNT(*) FROM accounts WHERE username=%s AND height=%d", 'gonesoon', 199.194);
|
||||
$this->assert(intval($ct) === 1);
|
||||
|
||||
$ct = $this->mdb->queryFirstField("SELECT COUNT(*) FROM accounts WHERE username=%s1 AND height=%d0 AND height=%d", 199.194, 'gonesoon');
|
||||
$this->assert(intval($ct) === 1);
|
||||
|
||||
$this->mdb->delete('accounts', 'username=%s AND age=%i AND height=%d', 'gonesoon', '61', '199.194');
|
||||
$this->assert($this->mdb->affectedRows() === 1);
|
||||
|
||||
$ct = $this->mdb->queryFirstField("SELECT COUNT(*) FROM accounts WHERE username=%s AND height=%d", 'gonesoon', '199.194');
|
||||
$this->assert(intval($ct) === 0);
|
||||
}
|
||||
|
||||
function test_4_3_insertmany() {
|
||||
$ins[] = array(
|
||||
'username' => '1ofmany',
|
||||
'password' => 'something',
|
||||
'age' => 23,
|
||||
'height' => 190.194
|
||||
);
|
||||
$ins[] = array(
|
||||
'password' => 'somethingelse',
|
||||
'username' => '2ofmany',
|
||||
'age' => 25,
|
||||
'height' => 190.194
|
||||
);
|
||||
|
||||
$this->mdb->insert('accounts', $ins);
|
||||
$this->assert($this->mdb->affectedRows() === 2);
|
||||
|
||||
$rows = $this->mdb->query("SELECT * FROM accounts WHERE height=%d ORDER BY age ASC", 190.194);
|
||||
$this->assert(count($rows) === 2);
|
||||
$this->assert($rows[0]['username'] === '1ofmany');
|
||||
$this->assert($rows[0]['age'] === '23');
|
||||
$this->assert($rows[1]['age'] === '25');
|
||||
$this->assert($rows[1]['password'] === 'somethingelse');
|
||||
$this->assert($rows[1]['username'] === '2ofmany');
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
function test_5_insert_blobs() {
|
||||
$this->mdb->query("CREATE TABLE `storedata` (
|
||||
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
|
||||
`picture` BLOB
|
||||
) ENGINE = InnoDB");
|
||||
|
||||
|
||||
$smile = file_get_contents('smile1.jpg');
|
||||
$this->mdb->insert('storedata', array(
|
||||
'picture' => $smile,
|
||||
));
|
||||
$this->mdb->query("INSERT INTO storedata (picture) VALUES (%s)", $smile);
|
||||
|
||||
$getsmile = $this->mdb->queryFirstField("SELECT picture FROM storedata WHERE id=1");
|
||||
$getsmile2 = $this->mdb->queryFirstField("SELECT picture FROM storedata WHERE id=2");
|
||||
$this->assert($smile === $getsmile);
|
||||
$this->assert($smile === $getsmile2);
|
||||
}
|
||||
|
||||
function test_6_insert_ignore() {
|
||||
$this->mdb->insertIgnore('accounts', array(
|
||||
'id' => 1, //duplicate primary key
|
||||
'username' => 'gonesoon',
|
||||
'password' => 'something',
|
||||
'age' => 61,
|
||||
'height' => 199.194
|
||||
));
|
||||
}
|
||||
|
||||
function test_7_insert_update() {
|
||||
$this->mdb->insertUpdate('accounts', array(
|
||||
'id' => 2, //duplicate primary key
|
||||
'username' => 'gonesoon',
|
||||
'password' => 'something',
|
||||
'age' => 61,
|
||||
'height' => 199.194
|
||||
), 'age = age + %i', 1);
|
||||
|
||||
$this->assert($this->mdb->affectedRows() === 2); // a quirk of MySQL, even though only 1 row was updated
|
||||
|
||||
$result = $this->mdb->query("SELECT * FROM accounts WHERE age = %i", 16);
|
||||
$this->assert(count($result) === 1);
|
||||
$this->assert($result[0]['height'] === '10.371');
|
||||
|
||||
$this->mdb->insertUpdate('accounts', array(
|
||||
'id' => 2, //duplicate primary key
|
||||
'username' => 'blahblahdude',
|
||||
'age' => 233,
|
||||
'height' => 199.194
|
||||
));
|
||||
|
||||
$result = $this->mdb->query("SELECT * FROM accounts WHERE age = %i", 233);
|
||||
$this->assert(count($result) === 1);
|
||||
$this->assert($result[0]['height'] === '199.194');
|
||||
$this->assert($result[0]['username'] === 'blahblahdude');
|
||||
|
||||
$this->mdb->insertUpdate('accounts', array(
|
||||
'id' => 2, //duplicate primary key
|
||||
'username' => 'gonesoon',
|
||||
'password' => 'something',
|
||||
'age' => 61,
|
||||
'height' => 199.194
|
||||
), array(
|
||||
'age' => 74,
|
||||
));
|
||||
|
||||
$result = $this->mdb->query("SELECT * FROM accounts WHERE age = %i", 74);
|
||||
$this->assert(count($result) === 1);
|
||||
$this->assert($result[0]['height'] === '199.194');
|
||||
$this->assert($result[0]['username'] === 'blahblahdude');
|
||||
|
||||
$multiples[] = array(
|
||||
'id' => 3, //duplicate primary key
|
||||
'username' => 'gonesoon',
|
||||
'password' => 'something',
|
||||
'age' => 61,
|
||||
'height' => 199.194
|
||||
);
|
||||
$multiples[] = array(
|
||||
'id' => 1, //duplicate primary key
|
||||
'username' => 'gonesoon',
|
||||
'password' => 'something',
|
||||
'age' => 61,
|
||||
'height' => 199.194
|
||||
);
|
||||
|
||||
$this->mdb->insertUpdate('accounts', $multiples, array('age' => 914));
|
||||
$this->assert($this->mdb->affectedRows() === 4);
|
||||
|
||||
$result = $this->mdb->query("SELECT * FROM accounts WHERE age=914 ORDER BY id ASC");
|
||||
$this->assert(count($result) === 2);
|
||||
$this->assert($result[0]['username'] === 'Abe');
|
||||
$this->assert($result[1]['username'] === 'Charlie\'s Friend');
|
||||
|
||||
$this->mdb->query("UPDATE accounts SET age=15, username='Bart' WHERE age=%i", 74);
|
||||
$this->assert($this->mdb->affectedRows() === 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
30
simpletest/TransactionTest.php
Normal file
30
simpletest/TransactionTest.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
class TransactionTest extends SimpleTest {
|
||||
function test_1_transactions() {
|
||||
DB::$nested_transactions = false;
|
||||
|
||||
DB::query("UPDATE accounts SET age=%i WHERE username=%s", 600, 'Abe');
|
||||
|
||||
$depth = DB::startTransaction();
|
||||
$this->assert($depth === 1);
|
||||
|
||||
DB::query("UPDATE accounts SET age=%i WHERE username=%s", 700, 'Abe');
|
||||
$depth = DB::startTransaction();
|
||||
$this->assert($depth === 1);
|
||||
|
||||
DB::query("UPDATE accounts SET age=%i WHERE username=%s", 800, 'Abe');
|
||||
$depth = DB::rollback();
|
||||
$this->assert($depth === 0);
|
||||
|
||||
$age = DB::queryFirstField("SELECT age FROM accounts WHERE username=%s", 'Abe');
|
||||
$this->assert($age == 700);
|
||||
|
||||
$depth = DB::rollback();
|
||||
$this->assert($depth === 0);
|
||||
|
||||
$age = DB::queryFirstField("SELECT age FROM accounts WHERE username=%s", 'Abe');
|
||||
$this->assert($age == 700);
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
||||
85
simpletest/TransactionTest_55.php
Normal file
85
simpletest/TransactionTest_55.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
class TransactionTest_55 extends SimpleTest {
|
||||
function test_1_transactions() {
|
||||
DB::$nested_transactions = true;
|
||||
|
||||
$depth = DB::startTransaction();
|
||||
$this->assert($depth === 1);
|
||||
DB::query("UPDATE accounts SET age=%i WHERE username=%s", 700, 'Abe');
|
||||
|
||||
$depth = DB::startTransaction();
|
||||
$this->assert($depth === 2);
|
||||
DB::query("UPDATE accounts SET age=%i WHERE username=%s", 800, 'Abe');
|
||||
|
||||
$depth = DB::startTransaction();
|
||||
$this->assert($depth === 3);
|
||||
$this->assert(DB::transactionDepth() === 3);
|
||||
DB::query("UPDATE accounts SET age=%i WHERE username=%s", 500, 'Abe');
|
||||
$depth = DB::commit();
|
||||
|
||||
$this->assert($depth === 2);
|
||||
|
||||
$age = DB::queryFirstField("SELECT age FROM accounts WHERE username=%s", 'Abe');
|
||||
$this->assert($age == 500);
|
||||
|
||||
$depth = DB::rollback();
|
||||
$this->assert($depth === 1);
|
||||
|
||||
$age = DB::queryFirstField("SELECT age FROM accounts WHERE username=%s", 'Abe');
|
||||
$this->assert($age == 700);
|
||||
|
||||
$depth = DB::commit();
|
||||
$this->assert($depth === 0);
|
||||
|
||||
$age = DB::queryFirstField("SELECT age FROM accounts WHERE username=%s", 'Abe');
|
||||
$this->assert($age == 700);
|
||||
|
||||
|
||||
DB::$nested_transactions = false;
|
||||
}
|
||||
|
||||
function test_2_transactions() {
|
||||
DB::$nested_transactions = true;
|
||||
|
||||
DB::query("UPDATE accounts SET age=%i WHERE username=%s", 600, 'Abe');
|
||||
|
||||
DB::startTransaction();
|
||||
DB::query("UPDATE accounts SET age=%i WHERE username=%s", 700, 'Abe');
|
||||
DB::startTransaction();
|
||||
DB::query("UPDATE accounts SET age=%i WHERE username=%s", 800, 'Abe');
|
||||
DB::rollback();
|
||||
|
||||
$age = DB::queryFirstField("SELECT age FROM accounts WHERE username=%s", 'Abe');
|
||||
$this->assert($age == 700);
|
||||
|
||||
DB::rollback();
|
||||
|
||||
$age = DB::queryFirstField("SELECT age FROM accounts WHERE username=%s", 'Abe');
|
||||
$this->assert($age == 600);
|
||||
|
||||
DB::$nested_transactions = false;
|
||||
}
|
||||
|
||||
function test_3_transaction_rollback_all() {
|
||||
DB::$nested_transactions = true;
|
||||
|
||||
DB::query("UPDATE accounts SET age=%i WHERE username=%s", 200, 'Abe');
|
||||
|
||||
$depth = DB::startTransaction();
|
||||
$this->assert($depth === 1);
|
||||
DB::query("UPDATE accounts SET age=%i WHERE username=%s", 300, 'Abe');
|
||||
$depth = DB::startTransaction();
|
||||
$this->assert($depth === 2);
|
||||
|
||||
DB::query("UPDATE accounts SET age=%i WHERE username=%s", 400, 'Abe');
|
||||
$depth = DB::rollback(true);
|
||||
$this->assert($depth === 0);
|
||||
|
||||
$age = DB::queryFirstField("SELECT age FROM accounts WHERE username=%s", 'Abe');
|
||||
$this->assert($age == 200);
|
||||
|
||||
DB::$nested_transactions = false;
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
||||
73
simpletest/WhereClauseTest.php
Normal file
73
simpletest/WhereClauseTest.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
class WhereClauseTest extends SimpleTest {
|
||||
function test_1_basic_where() {
|
||||
$where = new WhereClause('and');
|
||||
$where->add('username=%s', 'Bart');
|
||||
$where->add('password=%s', 'hello');
|
||||
|
||||
$result = DB::query("SELECT * FROM accounts WHERE %l", $where->text());
|
||||
$this->assert(count($result) === 1);
|
||||
$this->assert($result[0]['age'] === '15');
|
||||
}
|
||||
|
||||
function test_2_simple_grouping() {
|
||||
$where = new WhereClause('and');
|
||||
$where->add('password=%s', 'hello');
|
||||
$subclause = $where->addClause('or');
|
||||
$subclause->add('age=%i', 15);
|
||||
$subclause->add('age=%i', 14);
|
||||
|
||||
$result = DB::query("SELECT * FROM accounts WHERE %l", $where->text());
|
||||
$this->assert(count($result) === 1);
|
||||
$this->assert($result[0]['age'] === '15');
|
||||
}
|
||||
|
||||
function test_3_negate_last() {
|
||||
$where = new WhereClause('and');
|
||||
$where->add('password=%s', 'hello');
|
||||
$subclause = $where->addClause('or');
|
||||
$subclause->add('username!=%s', 'Bart');
|
||||
$subclause->negateLast();
|
||||
|
||||
$result = DB::query("SELECT * FROM accounts WHERE %l", $where->text());
|
||||
$this->assert(count($result) === 1);
|
||||
$this->assert($result[0]['age'] === '15');
|
||||
}
|
||||
|
||||
function test_4_negate_last_query() {
|
||||
$where = new WhereClause('and');
|
||||
$where->add('password=%s', 'hello');
|
||||
$subclause = $where->addClause('or');
|
||||
$subclause->add('username!=%s', 'Bart');
|
||||
$where->negateLast();
|
||||
|
||||
$result = DB::query("SELECT * FROM accounts WHERE %l", $where);
|
||||
$this->assert(count($result) === 1);
|
||||
$this->assert($result[0]['age'] === '15');
|
||||
}
|
||||
|
||||
function test_5_negate() {
|
||||
$where = new WhereClause('and');
|
||||
$where->add('password=%s', 'hello');
|
||||
$subclause = $where->addClause('or');
|
||||
$subclause->add('username!=%s', 'Bart');
|
||||
$subclause->negate();
|
||||
|
||||
$result = DB::query("SELECT * FROM accounts WHERE %l", $where);
|
||||
$this->assert(count($result) === 1);
|
||||
$this->assert($result[0]['age'] === '15');
|
||||
}
|
||||
|
||||
function test_6_or() {
|
||||
$where = new WhereClause('or');
|
||||
$where->add('username=%s', 'Bart');
|
||||
$where->add('username=%s', 'Abe');
|
||||
|
||||
$result = DB::query("SELECT * FROM accounts WHERE %l", $where);
|
||||
$this->assert(count($result) === 2);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
BIN
simpletest/smile1.jpg
Normal file
BIN
simpletest/smile1.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 48 KiB |
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/php
|
||||
<?php
|
||||
class SimpleTest {
|
||||
protected function assert($boolean) {
|
||||
public function assert($boolean) {
|
||||
if (! $boolean) $this->fail();
|
||||
}
|
||||
|
||||
@@ -11,49 +11,76 @@ class SimpleTest {
|
||||
die;
|
||||
}
|
||||
|
||||
public static function __listfiles($dir, $regex, $type='files', $rec = false) {
|
||||
$A = array();
|
||||
|
||||
if (! $dir_handler = @opendir($dir)) return $A;
|
||||
|
||||
while (false !== ($filename = @readdir($dir_handler))) {
|
||||
if ($filename == '.' || $filename == '..') continue;
|
||||
if ($rec && is_dir("$dir/$filename")) $A = array_merge($A, File::listfiles("$dir/$filename", $regex, $type, true));
|
||||
|
||||
if (! preg_match($regex, $filename)) continue;
|
||||
if ($type == 'files' && ! is_file("$dir/$filename")) continue;
|
||||
if ($type == 'dirs' && ! is_dir("$dir/$filename")) continue;
|
||||
if ($type == 'symlinks' && ! is_link("$dir/$filename")) continue;
|
||||
|
||||
$A[] = $dir . DIRECTORY_SEPARATOR . $filename;
|
||||
}
|
||||
return $A;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
$files = SimpleTest::__listfiles(dirname(__FILE__), '/^.*php$/i');
|
||||
|
||||
$classes_to_test = array();
|
||||
foreach ($files as $fullpath) {
|
||||
$filename = basename($fullpath);
|
||||
if ($fullpath == __FILE__) continue;
|
||||
require_once($fullpath);
|
||||
$classes_to_test[] = str_replace('.php', '', $filename);
|
||||
function microtime_float()
|
||||
{
|
||||
list($usec, $sec) = explode(" ", microtime());
|
||||
return ((float)$usec + (float)$sec);
|
||||
}
|
||||
|
||||
if (phpversion() >= '5.3') $is_php_53 = true;
|
||||
else $is_php_53 = false;
|
||||
|
||||
ini_set('date.timezone', 'America/Los_Angeles');
|
||||
|
||||
error_reporting(E_ALL | E_STRICT);
|
||||
require_once '../db.class.php';
|
||||
include 'test_setup.php'; //test config values go here
|
||||
DB::$user = $set_db_user;
|
||||
DB::$password = $set_password;
|
||||
DB::$dbName = $set_db;
|
||||
DB::$host = $set_host;
|
||||
DB::get(); //connect to mysql
|
||||
|
||||
require_once 'BasicTest.php';
|
||||
require_once 'CallTest.php';
|
||||
require_once 'ObjectTest.php';
|
||||
require_once 'WhereClauseTest.php';
|
||||
require_once 'ErrorTest.php';
|
||||
require_once 'TransactionTest.php';
|
||||
require_once 'HelperTest.php';
|
||||
|
||||
$classes_to_test = array(
|
||||
'BasicTest',
|
||||
'CallTest',
|
||||
'WhereClauseTest',
|
||||
'ObjectTest',
|
||||
'ErrorTest',
|
||||
'TransactionTest',
|
||||
'HelperTest',
|
||||
);
|
||||
|
||||
if ($is_php_53) {
|
||||
require_once 'ErrorTest_53.php';
|
||||
$classes_to_test[] = 'ErrorTest_53';
|
||||
} else {
|
||||
echo "PHP 5.3 not detected, skipping 5.3 tests..\n";
|
||||
}
|
||||
|
||||
$mysql_version = DB::serverVersion();
|
||||
if ($mysql_version >= '5.5') {
|
||||
require_once 'TransactionTest_55.php';
|
||||
$classes_to_test[] = 'TransactionTest_55';
|
||||
} else {
|
||||
echo "MySQL 5.5 not available (version is $mysql_version) -- skipping MySQL 5.5 tests\n";
|
||||
}
|
||||
|
||||
$time_start = microtime_float();
|
||||
foreach ($classes_to_test as $class) {
|
||||
$object = new $class();
|
||||
|
||||
foreach (get_class_methods($object) as $method) {
|
||||
if (substr($method, 0, 2) == '__') continue;
|
||||
if (substr($method, 0, 4) != 'test') continue;
|
||||
echo "Running $class::$method..\n";
|
||||
$object->$method();
|
||||
}
|
||||
}
|
||||
$time_end = microtime_float();
|
||||
$time = round($time_end - $time_start, 2);
|
||||
|
||||
echo "Completed in $time seconds\n";
|
||||
|
||||
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user