Skip to content

Commit c2d8d32

Browse files
committed
Merge pull request #1 from dustingraham/0.1.0
0.1.0
2 parents 00ff633 + b645e80 commit c2d8d32

18 files changed

+921
-682
lines changed

README.md

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,61 @@
33
Non-blocking MySQLi database access with PHP.
44
Designed to work with [reactphp/react](https://github.com/reactphp/react).
55

6+
[![Build Status](https://travis-ci.org/dustingraham/react-mysql.svg?branch=master)](https://travis-ci.org/dustingraham/react-mysql)
7+
8+
## Quickstart
9+
10+
$db = new \DustinGraham\ReactMysql\Database(
11+
['localhost', 'apache', 'apache', 'react_mysql_test']
12+
);
13+
14+
$db->statement('SELECT * FROM simple_table WHERE id = :test', [':test' => 2])
15+
->then(function(\mysqli_result $result)
16+
{
17+
$rows = $result->fetch_all(MYSQLI_ASSOC);
18+
});
19+
20+
$db->shuttingDown = true;
21+
$db->loop->run();
22+
23+
Setting `shuttingDown` to true will allow the loop to exit once the query has resolved.
624

725
## Working
826

9-
This __is__ working. But it is nowhere near complete.
27+
This __is__ working. But it is nowhere near complete. Check out the example file
28+
as well as the unit tests for more examples.
1029

11-
$ ./run
12-
Starting loop...
13-
DB Created.
30+
$ ./example
31+
Creating database....done!
1432
Run Query: 0
1533
Found rows: 0
1634
Run Query: 1
1735
Found rows: 1
18-
Current memory usage: 735.117K
36+
Current memory usage: 868.164K
1937
Run Query: 2
20-
Found rows: 0
38+
Found rows: 1
2139
Run Query: 3
2240
Found rows: 1
2341
Run Query: 4
24-
Found rows: 1
25-
Current memory usage: 735.117K
42+
Found rows: 0
43+
Current memory usage: 868.164K
2644
Run Query: 5
2745
Found rows: 0
28-
Current memory usage: 733.602K
29-
Current memory usage: 733.602K
30-
Current memory usage: 733.602K
46+
Current memory usage: 865.719K
47+
Current memory usage: 865.719K
48+
Current memory usage: 865.719K
3149
Loop finished, all timers halted.
3250

3351
This won't work out of the box without the database configured.
34-
As of this point, database configuration is hard coded.
35-
Still need to pull out the configs. You will also need to
36-
set up a database with some data to query. Check back later
37-
for more!
52+
You will also need to set up a database with some data to query.
3853

39-
## TODO
54+
## Unit Tests
4055

41-
A lot.
56+
The example and unit tests expect a database called `react_mysql_test` which it
57+
will populate with the proper tables each time it runs. It also expects `localhost`
58+
and a user `apache` with password `apache`.
59+
60+
## TODO
4261

4362
This is not production ready. Still tons to do on the query builder.
4463
While I hate to reinvent the wheel, I have not found a lightweight
@@ -52,23 +71,20 @@ These are just plans for now. It may change wildly as we develop.
5271

5372
Here is an example of what is currently working for the most part.
5473

55-
$loop = React\EventLoop\Factory::create();
56-
57-
ConnectionFactory::init($loop, ['db_host', 'db_user', 'db_pass', 'db_name']);
58-
59-
$db = new \DustinGraham\ReactMysql\Database();
74+
$db = new \DustinGraham\ReactMysql\Database(
75+
['localhost', 'apache', 'apache', 'react_mysql_test']
76+
);
6077

61-
$db->createCommand("SELECT * FROM `table` WHERE id = :id;", [':id' => $id])
62-
->execute()->then(
63-
function($result)
78+
$db->statement('SELECT * FROM simple_table WHERE id = :test', [':test' => 2])
79+
->then(function(\mysqli_result $result)
6480
{
6581
$rows = $result->fetch_all(MYSQLI_ASSOC);
66-
$result->close();
6782
6883
// Do something with $rows.
69-
}
70-
);
84+
});
7185

86+
$db->shuttingDown = true;
87+
$db->loop->run();
7288

7389
### Original Big Picture Plans
7490

example

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env php
2+
<?php
3+
error_reporting(-1);
4+
ini_set("display_errors", 1);
5+
6+
require __DIR__.'/vendor/autoload.php';
7+
8+
echo 'Creating database..';
9+
10+
$db = new \DustinGraham\ReactMysql\Database(
11+
['localhost', 'apache', 'apache', 'react_mysql_test']
12+
);
13+
14+
echo '..done!'.PHP_EOL;
15+
16+
$j = 0;
17+
$db->loop->addPeriodicTimer(0.3, function (\React\EventLoop\Timer\TimerInterface $timer) use (&$j)
18+
{
19+
$memory = memory_get_usage() / 1024;
20+
$formatted = number_format($memory, 3).'K';
21+
echo "Current memory usage: {$formatted}\n";
22+
23+
if ($j++ > 3) $timer->cancel();
24+
});
25+
26+
$i = 0;
27+
$db->loop->addPeriodicTimer(0.1, function (\React\EventLoop\Timer\TimerInterface $timer) use (&$i, $db)
28+
{
29+
echo "Run Query: $i\n";
30+
31+
$db->statement(
32+
'SELECT * FROM `simple_table` WHERE id = :test',
33+
[':test' => $i]
34+
)->then(function(\mysqli_result $result)
35+
{
36+
$rows = $result->fetch_all(MYSQLI_ASSOC);
37+
echo 'Found rows: '.count($rows).PHP_EOL;
38+
})->done();
39+
40+
if ($i++ >= 5)
41+
{
42+
// All queries added.
43+
$db->shuttingDown = true;
44+
$timer->cancel();
45+
}
46+
});
47+
48+
$db->loop->run();
49+
50+
echo 'Loop finished, all timers halted.'.PHP_EOL;

run

Lines changed: 0 additions & 59 deletions
This file was deleted.

src/Command.php

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22

33
class Command
44
{
5-
/**
6-
* @var Database the command is associated with.
7-
*/
8-
public $db;
9-
105
/**
116
* @var string
127
*/
@@ -18,16 +13,18 @@ class Command
1813
protected $params = [];
1914

2015
/**
16+
* TODO: Find all of these
17+
*
2118
* @var array
2219
*/
2320
protected $reserved_words = [
2421
'NOW()',
2522
];
2623

27-
public function __construct(Database $database, $sql = null)
24+
public function __construct($sql = null, $params = null)
2825
{
29-
$this->db = $database;
3026
$this->sql = $sql;
27+
$this->bind($params);
3128
}
3229

3330
/**
@@ -39,44 +36,28 @@ public function bind($key, $value = null)
3936
{
4037
if (is_array($key))
4138
{
42-
// TODO: Is this cludgy?
43-
$this->bindValues($key);
39+
foreach ($key as $k => $v)
40+
{
41+
$this->params[$k] = $v;
42+
}
4443
}
45-
else
44+
else if (!is_null($key))
4645
{
4746
$this->params[$key] = $value;
4847
}
4948

5049
return $this;
5150
}
5251

53-
/**
54-
* @param $params
55-
* @return $this
56-
*/
57-
public function bindValues($params)
58-
{
59-
foreach ($params as $k => $v)
60-
{
61-
$this->params[$k] = $v;
62-
}
63-
64-
return $this;
65-
}
66-
6752
/**
6853
* @param Connection $connection
6954
* @return string
7055
*/
7156
public function getPreparedQuery(Connection $connection)
7257
{
73-
$quotedSql = $this->quoteIntoSql($connection);
74-
75-
return $quotedSql;
58+
return $this->quoteIntoSql($connection);
7659
}
7760

78-
// TODO: Find all of these...
79-
8061
/**
8162
* TODO: This is exactly what I don't want to do. "Roll my own" SQL handler.
8263
* However, the requirements for this package have led to this point for now.
@@ -111,14 +92,4 @@ protected function quoteIntoSql(Connection $connection)
11192

11293
return strtr($quotedSql, $quotedParams);
11394
}
114-
115-
/**
116-
* @return \React\Promise\Promise
117-
*/
118-
public function execute()
119-
{
120-
$thing = $this->db->executeCommand($this);
121-
122-
return $thing;
123-
}
12495
}

0 commit comments

Comments
 (0)