[ Поиск ] - [ Пользователи ] - [ Календарь ]
Полная Версия: QueryBuilder! Прокатимся?
Страницы: 1, 2
stump
Сводка новостей:
Недавно как в пятницу получилась модель велосипеда QueryBuilder у которой:
колеса
<?php

namespace Database;

/**
* Description of PDO
*
@author stump
*/


require_once 'config.inc.php';

class PDO
{
public $pdo;
public $statement;

function __construct()
{
$this -> connect();
}

public function connect()
{
$this -> pdo = new \PDO(
"mysql:host=" . DB_HOST . " ;dbname=" . DB_NAME . ";",
DB_USER,
DB_PW
);
return $this -> pdo;
}

public function error()
{
if ( $this -> pdo -> errorCode() != '00000' )
{
throw new \Exception( implode( " ", $this -> pdo -> errorInfo() ) );
}

return TRUE;
}

public function prepare( $sql )
{
return empty( $this -> pdo )
?
"Run without pdo connetcion"
: ( $this -> statement = $this -> pdo -> prepare( $sql ) );
}

public function execute( $param )
{
return empty( $this -> statement )
?
"Run without pdo connetcion"
: empty( $param )
?
$this -> statement -> execute()
:
$this -> statement -> execute( $param );
}

public function fetch()
{
return empty( $this -> statement )
?
"Run without pdo connetcion"
: $this -> statement -> fetchAll( \PDO::FETCH_CLASS );
}
}


со спицами
<?php
define('DB_HOST', '127.0.0.1');
define('DB_USER', 'root');
define('DB_PW', '1111');
define('DB_NAME', 'mydb');


педали
<?php

namespace Database;
use Database\QueryBuilder;

/**
* Description of User
*
@author stump
*/

class Database
{
public $pdo;

public $sql = NULL;
public $param = [];

private $Query;

public function __construct()
{
$this -> pdo = new PDO();
}

public function Build()
{
$this -> Query = new QueryBuilder();
return $this -> Query;
}

public function apply( QueryBuilder $Query )
{
$this -> sql = empty( $Query -> apply() )
?
""
: $Query -> sql;
return $this -> sql;
}

public function execute()
{
$this -> pdo -> prepare( $this -> sql );
$execute = $this -> pdo -> execute( $this -> param );
$this -> pdo -> error();
return $execute;
}

public function query()
{
$this -> pdo -> prepare( $this -> sql );
$this -> pdo -> execute( $this -> param );
$this -> pdo -> error();
$fetch = $this -> pdo -> fetch();
return $fetch;
}
}


руль
<?php

namespace Database;

/**
* Description of QueryBuilder
*
@author stump
*/

class QueryBuilder
{
public $sql;
public $field = [];

private static $Instance;

private $key_exclude = ["FIELD"];

function __construct()
{
static :: $Instance = $this;
}

public function addpart( $key, $value = "" )
{
$this -> field[ $key ][] = is_object( $value )
?
array_keys( get_object_vars( $value ) )
: (
array )$value;
return static::$Instance;
}

public function apply()
{
$fields = [];
foreach ( $this -> field as $key => $field )
{
foreach ( $field as $_field )
{
$fields[ $key ] = " " . $key . " ";
$fields[ $key ] .= $this -> prepare( $key, $_field );
}
}


$this -> sql .= implode( " ", $fields );
$this -> sql = str_replace( $this -> key_exclude, "", $this -> sql );

return ( TRUE == $this -> sql );
}

private function prepare( $key, array $array )
{
$query = "";
switch ($key)
{
case 'FIELD':
case 'VALUES':
$query .= "(" . implode( " , ", $array ) . ")";
break;
case 'JOIN':
case 'INNER JOIN':
case 'LEFT JOIN':
case 'RIGHT JOIN':
case 'CROSS JOIN':
$query .= implode( " ON ", $array );
break;
case 'WHERE':
$query .= implode( " AND ", $array );
break;
default:
$query .= implode( " , ", $array );
break;
}
return $query;
}
}


Чтобы ехать нужно крутить педали:
левой и правой ногой попеременно


//Left
$user = new User('id'=>1, 'name'=>'User', 'family'=>'Group', 'address'=>'Home');

$db = new \Database\Database();
$Query = $db -> Build()
->
addpart( 'INSERT INTO', 'user')
->
addpart( 'FIELD', $user)
->
addpart( 'VALUES', ['?','?','?','?'] );
$db -> apply( $Query );
$db -> param = array_values( get_object_vars( $user ) );
$db -> execute();

//right
$Query = $db -> Build()
->
addpart( 'UPDATE', 'user')
->
addpart( 'SET', ['id=?', 'name=?', 'family=?', 'address=?'])
->
addpart( 'WHERE', ['id=1'] );
$db -> apply( $Query );
$db -> param = [2, 'User', 'Group', 'Home'];
$db -> execute();

//left
$Query = $db -> Build()
->
addpart( 'SELECT', $user )
->
addpart( 'FROM', ['user'] )
->
addpart( 'WHERE', ['id > 0'] )
->
addpart( 'LIMIT', [10] );
$db -> apply( $Query );
$res = $db -> query();

//relax
var_dump($res);

//left-right-relax... loop.


На сегодня пока все.
Вам слово!
Студия!?

_____________
Трус не играет в хокей
Быстрый ответ:

 Графические смайлики |  Показывать подпись
Здесь расположена полная версия этой страницы.
Invision Power Board © 2001-2024 Invision Power Services, Inc.