Skip to content

Commit

Permalink
Merge pull request #1 from dustingraham/0.1.0
Browse files Browse the repository at this point in the history
0.1.0
  • Loading branch information
dustingraham committed May 8, 2016
2 parents 00ff633 + b645e80 commit c2d8d32
Show file tree
Hide file tree
Showing 18 changed files with 921 additions and 682 deletions.
72 changes: 44 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,61 @@
Non-blocking MySQLi database access with PHP.
Designed to work with [reactphp/react](https://github.com/reactphp/react).

[![Build Status](https://travis-ci.org/dustingraham/react-mysql.svg?branch=master)](https://travis-ci.org/dustingraham/react-mysql)

## Quickstart

$db = new \DustinGraham\ReactMysql\Database(
['localhost', 'apache', 'apache', 'react_mysql_test']
);

$db->statement('SELECT * FROM simple_table WHERE id = :test', [':test' => 2])
->then(function(\mysqli_result $result)
{
$rows = $result->fetch_all(MYSQLI_ASSOC);
});

$db->shuttingDown = true;
$db->loop->run();

Setting `shuttingDown` to true will allow the loop to exit once the query has resolved.

## Working

This __is__ working. But it is nowhere near complete.
This __is__ working. But it is nowhere near complete. Check out the example file
as well as the unit tests for more examples.

$ ./run
Starting loop...
DB Created.
$ ./example
Creating database....done!
Run Query: 0
Found rows: 0
Run Query: 1
Found rows: 1
Current memory usage: 735.117K
Current memory usage: 868.164K
Run Query: 2
Found rows: 0
Found rows: 1
Run Query: 3
Found rows: 1
Run Query: 4
Found rows: 1
Current memory usage: 735.117K
Found rows: 0
Current memory usage: 868.164K
Run Query: 5
Found rows: 0
Current memory usage: 733.602K
Current memory usage: 733.602K
Current memory usage: 733.602K
Current memory usage: 865.719K
Current memory usage: 865.719K
Current memory usage: 865.719K
Loop finished, all timers halted.

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

## TODO
## Unit Tests

A lot.
The example and unit tests expect a database called `react_mysql_test` which it
will populate with the proper tables each time it runs. It also expects `localhost`
and a user `apache` with password `apache`.

## TODO

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

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

$loop = React\EventLoop\Factory::create();

ConnectionFactory::init($loop, ['db_host', 'db_user', 'db_pass', 'db_name']);

$db = new \DustinGraham\ReactMysql\Database();
$db = new \DustinGraham\ReactMysql\Database(
['localhost', 'apache', 'apache', 'react_mysql_test']
);

$db->createCommand("SELECT * FROM `table` WHERE id = :id;", [':id' => $id])
->execute()->then(
function($result)
$db->statement('SELECT * FROM simple_table WHERE id = :test', [':test' => 2])
->then(function(\mysqli_result $result)
{
$rows = $result->fetch_all(MYSQLI_ASSOC);
$result->close();
// Do something with $rows.
}
);
});

$db->shuttingDown = true;
$db->loop->run();

### Original Big Picture Plans

Expand Down
50 changes: 50 additions & 0 deletions example
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env php
<?php
error_reporting(-1);
ini_set("display_errors", 1);

require __DIR__.'/vendor/autoload.php';

echo 'Creating database..';

$db = new \DustinGraham\ReactMysql\Database(
['localhost', 'apache', 'apache', 'react_mysql_test']
);

echo '..done!'.PHP_EOL;

$j = 0;
$db->loop->addPeriodicTimer(0.3, function (\React\EventLoop\Timer\TimerInterface $timer) use (&$j)
{
$memory = memory_get_usage() / 1024;
$formatted = number_format($memory, 3).'K';
echo "Current memory usage: {$formatted}\n";

if ($j++ > 3) $timer->cancel();
});

$i = 0;
$db->loop->addPeriodicTimer(0.1, function (\React\EventLoop\Timer\TimerInterface $timer) use (&$i, $db)
{
echo "Run Query: $i\n";

$db->statement(
'SELECT * FROM `simple_table` WHERE id = :test',
[':test' => $i]
)->then(function(\mysqli_result $result)
{
$rows = $result->fetch_all(MYSQLI_ASSOC);
echo 'Found rows: '.count($rows).PHP_EOL;
})->done();

if ($i++ >= 5)
{
// All queries added.
$db->shuttingDown = true;
$timer->cancel();
}
});

$db->loop->run();

echo 'Loop finished, all timers halted.'.PHP_EOL;
59 changes: 0 additions & 59 deletions run

This file was deleted.

49 changes: 10 additions & 39 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

class Command
{
/**
* @var Database the command is associated with.
*/
public $db;

/**
* @var string
*/
Expand All @@ -18,16 +13,18 @@ class Command
protected $params = [];

/**
* TODO: Find all of these
*
* @var array
*/
protected $reserved_words = [
'NOW()',
];

public function __construct(Database $database, $sql = null)
public function __construct($sql = null, $params = null)
{
$this->db = $database;
$this->sql = $sql;
$this->bind($params);
}

/**
Expand All @@ -39,44 +36,28 @@ public function bind($key, $value = null)
{
if (is_array($key))
{
// TODO: Is this cludgy?
$this->bindValues($key);
foreach ($key as $k => $v)
{
$this->params[$k] = $v;
}
}
else
else if (!is_null($key))
{
$this->params[$key] = $value;
}

return $this;
}

/**
* @param $params
* @return $this
*/
public function bindValues($params)
{
foreach ($params as $k => $v)
{
$this->params[$k] = $v;
}

return $this;
}

/**
* @param Connection $connection
* @return string
*/
public function getPreparedQuery(Connection $connection)
{
$quotedSql = $this->quoteIntoSql($connection);

return $quotedSql;
return $this->quoteIntoSql($connection);
}

// TODO: Find all of these...

/**
* TODO: This is exactly what I don't want to do. "Roll my own" SQL handler.
* However, the requirements for this package have led to this point for now.
Expand Down Expand Up @@ -111,14 +92,4 @@ protected function quoteIntoSql(Connection $connection)

return strtr($quotedSql, $quotedParams);
}

/**
* @return \React\Promise\Promise
*/
public function execute()
{
$thing = $this->db->executeCommand($this);

return $thing;
}
}
Loading

0 comments on commit c2d8d32

Please sign in to comment.