1
0
Fork 0
mirror of synced 2024-06-26 10:10:57 +12:00

Added PDO proxy

This commit is contained in:
Eldad Fux 2020-07-02 01:34:05 +03:00
parent 53f0685918
commit 4e09285ca4
6 changed files with 156 additions and 35 deletions

View file

@ -12,18 +12,20 @@ if (\file_exists(__DIR__.'/../vendor/autoload.php')) {
}
use Appwrite\Auth\Auth;
use Utopia\App;
use Utopia\Config\Config;
use Utopia\Locale\Locale;
use Utopia\Registry\Registry;
use Appwrite\Database\Database;
use Appwrite\Database\Adapter\MySQL as MySQLAdapter;
use Appwrite\Database\Adapter\Redis as RedisAdapter;
use Appwrite\Database\Document;
use Appwrite\Database\Validator\Authorization;
use Appwrite\Event\Event;
use PHPMailer\PHPMailer\PHPMailer;
use Appwrite\Extend\PDO;
use Utopia\App;
use Utopia\View;
use Utopia\Config\Config;
use Utopia\Locale\Locale;
use Utopia\Registry\Registry;
use PHPMailer\PHPMailer\PHPMailer;
use PDO as PDONative;
const APP_NAME = 'Appwrite';
const APP_DOMAIN = 'appwrite.io';
@ -87,13 +89,13 @@ $register->set('db', function () { // Register DB connection
$dbScheme = App::getEnv('_APP_DB_SCHEMA', '');
$pdo = new PDO("mysql:host={$dbHost};dbname={$dbScheme};charset=utf8mb4", $dbUser, $dbPass, array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4',
PDO::ATTR_TIMEOUT => 5, // Seconds
PDONative::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4',
PDONative::ATTR_TIMEOUT => 5, // Seconds
));
// Connection settings
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); // Return arrays
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Handle all errors with exceptions
$pdo->setAttribute(PDONative::ATTR_DEFAULT_FETCH_MODE, PDONative::FETCH_ASSOC); // Return arrays
$pdo->setAttribute(PDONative::ATTR_ERRMODE, PDONative::ERRMODE_EXCEPTION); // Handle all errors with exceptions
return $pdo;
});

View file

@ -15,7 +15,7 @@ ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$http = new Server("localhost", 9501);
$http = new Server("localhost", 80);
$http
->set([
@ -49,39 +49,25 @@ $http->on('start', function (Server $http) {
});
});
$data = file_get_contents('../public/test.html');
// $register = new Registry();
// $utopia = new App('Asia/Tel_Aviv');
// /**
// * @var $request Request
// */
// $request &= null;
// $response &= null;
// include 'init.php';
// include 'app.php';
$counter = 0;
include __DIR__ . '/app.php';
$http->on('request', function (SwooleRequest $swooleRequest, SwooleResponse $swooleResponse) use (&$counter) {
$http->on('request', function (SwooleRequest $swooleRequest, SwooleResponse $swooleResponse) {
$request = new Request($swooleRequest);
$response = new Response($swooleResponse);
//$swooleResponse->write($counter++);
$app = new App('Asia/Tel_Aviv');
try {
$app->run($request, $response);
} catch (\Throwable $th) {
var_dump($th->getMessage());
var_dump($th->getFile());
var_dump($th->getLine());
$swooleResponse->end('error: '.$th->getMessage());
if(App::isDevelopment()) {
var_dump($th->getMessage());
var_dump($th->getFile());
var_dump($th->getLine());
$swooleResponse->end('error: '.$th->getMessage());
}
$swooleResponse->end('500: Server Error');
}
});

View file

@ -37,7 +37,7 @@ services:
- VERSION=dev
restart: unless-stopped
ports:
- 9501:9501
- 9501:80
networks:
- appwrite
labels:

View file

@ -906,7 +906,7 @@ class MySQL extends Adapter
*
* @throws Exception
*/
protected function getPDO():PDO
protected function getPDO()
{
return $this->register->get('db');
}

View file

@ -0,0 +1,66 @@
<?php
namespace Appwrite\Extend;
use PDO as PDONative;
class PDO
{
/**
* @var PDONative
*/
protected $pdo;
/**
* @var mixed
*/
protected $dsn;
/**
* @var mixed
*/
protected $username;
/**
* @var mixed
*/
protected $passwd;
/**
* @var mixed
*/
protected $options;
/**
* Create A Proxy PDO Object
*/
public function __construct($dsn, $username = null, $passwd = null, $options = null)
{
$this->dsn = $dsn;
$this->username = $username;
$this->passwd = $passwd;
$this->options = $options;
$this->pdo = new PDONative($dsn, $username, $passwd, $options);
}
public function setAttribute($attribute, $value)
{
return $this->pdo->setAttribute($attribute, $value);
}
public function prepare($statement, array $driver_options = [])
{
return new PDOStatement($this->pdo, $this->pdo->prepare($statement, $driver_options));
}
public function quote($string, $parameter_type = PDONative::PARAM_STR)
{
return $this->pdo->quote($string, $parameter_type);
}
public function reconnect()
{
return $this->pdo = new PDONative($this->dsn, $this->username, $this->passwd, $this->options);
}
}

View file

@ -0,0 +1,67 @@
<?php
namespace Appwrite\Extend;
use PDO as PDONative;
use PDOStatement as PDOStatementNative;
class PDOStatement
{
/**
* @var PDONative
*/
protected $pdo;
/**
* @var PDOStatementNative
*/
protected $PDOStatement;
public function __construct(PDONative &$pdo, PDOStatementNative $PDOStatement)
{
$this->pdo = $pdo;
$this->PDOStatement = $PDOStatement;
}
public function bindValue($parameter, $value, $data_type = PDONative::PARAM_STR)
{
$result = $this->PDOStatement->bindValue($parameter, $value, $data_type);
return $result;
}
public function bindParam($parameter, &$variable, $data_type = PDONative::PARAM_STR, $length = null, $driver_options = null)
{
$result = $this->PDOStatement->bindParam($parameter, $variable, $data_type, $length, $driver_options);
return $result;
}
public function bindColumn($column, &$param, $type = null, $maxlen = null, $driverdata = null)
{
$result = $this->PDOStatement->bindColumn($column, $param, $type, $maxlen, $driverdata);
return $result;
}
public function execute($input_parameters = null)
{
$result = $this->PDOStatement->execute($input_parameters);
return $result;
}
public function fetch($fetch_style = PDONative::FETCH_ASSOC, $cursor_orientation = PDONative::FETCH_ORI_NEXT, $cursor_offset = 0)
{
$result = $this->PDOStatement->fetch($fetch_style, $cursor_orientation, $cursor_offset);
return $result;
}
public function fetchAll()
{
$result = $this->PDOStatement->fetchAll();
return $result;
}
}