Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 0861b87ddc70376fc715348f378db9a6fce13d20 @gonzalo123 committed May 20, 2012
Showing with 149 additions and 0 deletions.
  1. +61 −0 CPool.php
  2. +14 −0 README
  3. +40 −0 db.php
  4. +12 −0 example1.php
  5. +12 −0 example2.php
  6. +10 −0 example3.php
@@ -0,0 +1,61 @@
+<?php
+class CPoolStatement
+{
+ private $stmt;
+ function __construct($sql=null)
+ {
+ if (!is_null($sql)) {
+ $url = "http://" . NODEPHP . "?" . http_build_query(array(
+ 'action' => 'prepare',
+ 'sql' => $sql
+ ));
+ $this->stmt = file_get_contents($url);
+ }
+ }
+
+ public function getId()
+ {
+ return $this->stmt;
+ }
+
+ public function setId($id)
+ {
+ $this->stmt = $id;
+ }
+
+ public function execute($values=array())
+ {
+ $url = "http://" . NODEPHP . "?" . http_build_query(array(
+ 'action' => 'execute',
+ 'smtId' => $this->stmt,
+ 'values' => $values
+ ));
+ $this->stmt = file_get_contents($url);
+ }
+
+ public function fetchAll()
+ {
+ $url = "http://" . NODEPHP . "?" . http_build_query(array(
+ 'action' => 'fetchAll',
+ 'smtId' => $this->stmt
+ ));
+ return (file_get_contents($url));
+ }
+
+ public function closeCursor()
+ {
+ $url = "http://" . NODEPHP . "?" . http_build_query(array(
+ 'action' => 'closeCursor',
+ 'smtId' => $this->stmt
+ ));
+ return (file_get_contents($url));
+ }
+}
+
+class CPool
+{
+ function prepare($sql)
+ {
+ return new CPoolStatement($sql);
+ }
+}
@@ -0,0 +1,14 @@
+
+We start the server:
+php db.php
+
+Simple example (fetchAll)
+php example1.php
+[{"uid":0,"0":0,"name":"Gonzalo","1":"Gonzalo","surname":"Ayuso","2":"Ayuso"},{"uid":1,"0":1,"name":"Hans","1":"Hans","surname":"Solo","2":"Solo"},{"uid":2,"0":2,"name":"Luke","1":"Luke","surname":"Skywalker","2":"Skywalker"}]gonzalo@gnzl:~/borrame$
+
+We parse the SQL and create a new statement
+php example2.php
+
+We execute our prepared statement
+php example3.php
+[{"uid":0,"0":0,"name":"Gonzalo","1":"Gonzalo","surname":"Ayuso","2":"Ayuso"},{"uid":1,"0":1,"name":"Hans","1":"Hans","surname":"Solo","2":"Solo"},{"uid":2,"0":2,"name":"Luke","1":"Luke","surname":"Skywalker","2":"Skywalker"}]gonzalo@gnzl:~/bor
@@ -0,0 +1,40 @@
+<?php
+require 'vendor/autoload.php';
+
+$conn = new PDO('pgsql:dbname=demo;host=vmserver', 'gonzalo', 'password');
+$stmtIds = 0;
+$stmts = array();
+
+$app = new React\Espresso\Application();
+
+$app->get('/', function ($request, $response) use($conn, &$stmtIds, &$stmts) {
+ $headers = array('Content-Type' => 'application/json');
+ $query = $request->getQuery();
+ $out = null;
+ switch ($query['action']) {
+ case 'prepare':
+ $stmt = $conn->prepare($query['sql']);
+ $stmtIds++;
+ $stmts[$stmtIds] = $stmt;
+ $out = $stmtIds;
+ break;
+ case 'execute':
+ $stmts[$query['smtId']]->execute();
+ $out = $stmtIds;
+ break;
+ case 'fetchAll':
+ $out = $stmts[$query['smtId']]->fetchAll();
+ break;
+ case 'closeCursor':
+ $stmts[$query['smtId']]->closeCursor();
+ unset($stmts[$query['smtId']]);
+ break;
+ }
+
+ $response->writeHead(200, $headers);
+ $response->end(json_encode($out));
+});
+
+$stack = new React\Espresso\Stack($app);
+$stack->listen(1337);
+echo "Server running at http://127.0.0.1:1337\n";
@@ -0,0 +1,12 @@
+<?php
+include "CPool.php";
+define('NODEPHP', '127.0.0.1:1337');
+
+$dbh = new CPool();
+$sql = "SELECT * FROM USERS";
+$stmt = $dbh->prepare($sql);
+$stmt->execute();
+$data = $stmt->fetchAll();
+$stmt->closeCursor();
+print_r($data);
+
@@ -0,0 +1,12 @@
+<?php
+include "CPool.php";
+define('NODEPHP', '127.0.0.1:1337');
+
+
+$dbh = new CPool();
+$sql = "SELECT * FROM USERS";
+$stmt = $dbh->prepare($sql);
+
+echo $stmt->getId();
+
+
@@ -0,0 +1,10 @@
+<?php
+include "CPool.php";
+define('NODEPHP', '127.0.0.1:1337');
+
+$stmt = new CPoolStatement();
+$stmt->setId(1);
+
+$stmt->execute();
+$data = $stmt->fetchAll();
+print_r($data);

0 comments on commit 0861b87

Please sign in to comment.