PHP 클래스 Bluz\Db\Db

저자: Anton Shevchuk
상속: use trait Bluz\Common\Options
파일 보기 프로젝트 열기: bluzphp/framework 1 사용 예제들

보호된 프로퍼티들

프로퍼티 타입 설명
$attributes array PDO connection flags
$connect array PDO connection settings
$handler PDO instance

공개 메소드들

메소드 설명
connect ( ) : Db Connect to Db
delete ( string $table ) : Bluz\Db\Query\Delete Create new query update builder
disconnect ( ) : void Disconnect PDO and clean default adapter
fetchAll ( string $sql, array $params = [] ) : array[] Returns an array containing all of the result set rows
fetchColumn ( string $sql, array $params = [] ) : array Returns an array containing one column from the result set rows
fetchColumnGroup ( string $sql, array $params = [] ) : array Returns an array containing all of the result set rows
fetchGroup ( string $sql, array $params = [], mixed $object = null ) : array Returns an array containing all of the result set rows
fetchObject ( string $sql, array $params = [], mixed $object = 'stdClass' ) : array Returns an object containing first row from the result set
fetchObjects ( string $sql, array $params = [], mixed $object = null ) : array Returns an array of objects containing the result set
fetchOne ( string $sql, array $params = [] ) : string Return first field from first element from the result set
fetchPairs ( string $sql, array $params = [] ) : array Returns a key-value array
fetchRelations ( string $sql, array $params = [] ) : array Returns an array of linked objects containing the result set
fetchRow ( string $sql, array $params = [] ) : array Returns an array containing first row from the result set
handler ( ) : PDO Return PDO handler
insert ( string $table ) : Insert Create new query insert builder
query ( string $sql, array $params = [], array $types = [] ) : integer Execute SQL query
quote ( string $value ) : string Quotes a string for use in a query
quoteIdentifier ( string $identifier ) : string Quote a string so it can be safely used as a table or column name
select ( $select ) : Select Create new query select builder
setAttributes ( array $attributes ) : Db Setup attributes for PDO connect
setConnect ( array $connect ) : Db Setup connection
transaction ( callable $process ) : boolean Transaction wrapper
update ( string $table ) : Bluz\Db\Query\Update Create new query update builder

보호된 메소드들

메소드 설명
log ( string $sql, array $context = [] ) : void Log queries by Application
ok ( ) : void Setup timer
prepare ( string $sql, array $params ) : PDOStatement Prepare SQL query and return PDO Statement

비공개 메소드들

메소드 설명
checkConnect ( ) : void Check connection options

메소드 상세

connect() 공개 메소드

Connect to Db
public connect ( ) : Db
리턴 Db

delete() 공개 메소드

Create new query update builder
public delete ( string $table ) : Bluz\Db\Query\Delete
$table string
리턴 Bluz\Db\Query\Delete

disconnect() 공개 메소드

Disconnect PDO and clean default adapter
public disconnect ( ) : void
리턴 void

fetchAll() 공개 메소드

Example of usage $db->fetchAll("SELECT * FROM users WHERE ip = ?", ['192.168.1.1']);
public fetchAll ( string $sql, array $params = [] ) : array[]
$sql string SQL query with placeholders "SELECT * FROM users WHERE ip = :ip"
$params array params for query placeholders (optional) array (':ip' => '127.0.0.1')
리턴 array[]

fetchColumn() 공개 메소드

Returns an array containing one column from the result set rows
public fetchColumn ( string $sql, array $params = [] ) : array
$sql string SQL query with placeholders "SELECT id FROM users WHERE ip = :ip"
$params array params for query placeholders (optional) array (':ip' => '127.0.0.1')
리턴 array

fetchColumnGroup() 공개 메소드

Group by first column
public fetchColumnGroup ( string $sql, array $params = [] ) : array
$sql string SQL query with placeholders "SELECT ip, id FROM users"
$params array params for query placeholders (optional)
리턴 array

fetchGroup() 공개 메소드

Group by first column $db->fetchGroup("SELECT ip, COUNT(id) FROM users GROUP BY ip", []);
public fetchGroup ( string $sql, array $params = [], mixed $object = null ) : array
$sql string SQL query with placeholders "SELECT ip, id FROM users"
$params array params for query placeholders (optional)
$object mixed
리턴 array

fetchObject() 공개 메소드

Example of usage Fetch object to stdClass $stdClass = $db->fetchObject('SELECT * FROM some_table WHERE id = ?', [$id]); Fetch object to new Some object $someClass = $db->fetchObject('SELECT * FROM some_table WHERE id = ?', [$id], 'Some'); Fetch object to exists instance of Some object $someClass = $db->fetchObject('SELECT * FROM some_table WHERE id = ?', [$id], $someClass);
public fetchObject ( string $sql, array $params = [], mixed $object = 'stdClass' ) : array
$sql string SQL query with placeholders "SELECT * FROM users WHERE name = :name AND pass = :pass"
$params array params for query placeholders (optional) array (':name' => 'John', ':pass' => '123456')
$object mixed
리턴 array

fetchObjects() 공개 메소드

Returns an array of objects containing the result set
public fetchObjects ( string $sql, array $params = [], mixed $object = null ) : array
$sql string SQL query with placeholders "SELECT * FROM users WHERE name = :name AND pass = :pass"
$params array params for query placeholders (optional) array (':name' => 'John', ':pass' => '123456')
$object mixed Class name or instance
리턴 array

fetchOne() 공개 메소드

Example of usage $db->fetchOne("SELECT COUNT(*) FROM users");
public fetchOne ( string $sql, array $params = [] ) : string
$sql string SQL query with placeholders "SELECT * FROM users WHERE name = :name AND pass = :pass"
$params array params for query placeholders (optional) array (':name' => 'John', ':pass' => '123456')
리턴 string

fetchPairs() 공개 메소드

Returns a key-value array
public fetchPairs ( string $sql, array $params = [] ) : array
$sql string SQL query with placeholders "SELECT id, username FROM users WHERE ip = :ip"
$params array params for query placeholders (optional) array (':ip' => '127.0.0.1')
리턴 array

fetchRelations() 공개 메소드

Returns an array of linked objects containing the result set
public fetchRelations ( string $sql, array $params = [] ) : array
$sql string SQL query with placeholders "SELECT '__users', u.*, '__users_profile', up.* FROM users u LEFT JOIN users_profile up ON up.userId = u.id WHERE u.name = :name"
$params array params for query placeholders (optional) array (':name' => 'John')
리턴 array

fetchRow() 공개 메소드

Example of usage $db->fetchRow("SELECT name, email FROM users WHERE id = ". $db->quote($id)); $db->fetchRow("SELECT name, email FROM users WHERE id = ?", [$id]); $db->fetchRow("SELECT name, email FROM users WHERE id = :id", [':id'=>$id]);
public fetchRow ( string $sql, array $params = [] ) : array
$sql string SQL query with placeholders "SELECT * FROM users WHERE name = :name AND pass = :pass"
$params array params for query placeholders (optional) array (':name' => 'John', ':pass' => '123456')
리턴 array array ('name' => 'John', 'email' => '[email protected]')

handler() 공개 메소드

Return PDO handler
public handler ( ) : PDO
리턴 PDO

insert() 공개 메소드

Create new query insert builder
public insert ( string $table ) : Insert
$table string
리턴 Bluz\Db\Query\Insert

log() 보호된 메소드

Log queries by Application
protected log ( string $sql, array $context = [] ) : void
$sql string SQL query for logs
$context array
리턴 void

ok() 보호된 메소드

Setup timer
protected ok ( ) : void
리턴 void

prepare() 보호된 메소드

Prepare SQL query and return PDO Statement
protected prepare ( string $sql, array $params ) : PDOStatement
$sql string SQL query with placeholders
$params array params for query placeholders
리턴 PDOStatement

query() 공개 메소드

Example of usage $db->query("SET NAMES 'utf8'");
public query ( string $sql, array $params = [], array $types = [] ) : integer
$sql string SQL query with placeholders "UPDATE users SET name = :name WHERE id = :id"
$params array params for query placeholders (optional) array (':name' => 'John', ':id' => '123')
$types array Types of params (optional) array (':name' => \PDO::PARAM_STR, ':id' => \PDO::PARAM_INT)
리턴 integer the number of rows

quote() 공개 메소드

Example of usage $db->quote($_GET['id'])
public quote ( string $value ) : string
$value string
리턴 string

quoteIdentifier() 공개 메소드

Quote a string so it can be safely used as a table or column name
public quoteIdentifier ( string $identifier ) : string
$identifier string
리턴 string

select() 공개 메소드

Create new query select builder
public select ( $select ) : Select
$select The selection expressions
리턴 Bluz\Db\Query\Select

setAttributes() 공개 메소드

Setup attributes for PDO connect
public setAttributes ( array $attributes ) : Db
$attributes array
리턴 Db

setConnect() 공개 메소드

Just save connection settings $db->setConnect([ 'type' => 'mysql', 'host' => 'localhost', 'name' => 'db name', 'user' => 'root', 'pass' => '' ]);
public setConnect ( array $connect ) : Db
$connect array options
리턴 Db

transaction() 공개 메소드

Example of usage $db->transaction(function() use ($db) { $db->query("INSERT INTO table ..."); $db->query("UPDATE table ..."); $db->query("DELETE FROM table ..."); })
public transaction ( callable $process ) : boolean
$process callable callable structure - closure function or class with __invoke() method
리턴 boolean

update() 공개 메소드

Create new query update builder
public update ( string $table ) : Bluz\Db\Query\Update
$table string
리턴 Bluz\Db\Query\Update

프로퍼티 상세

$attributes 보호되어 있는 프로퍼티

PDO connection flags
protected array $attributes
리턴 array

$connect 보호되어 있는 프로퍼티

PDO connection settings
protected array $connect
리턴 array

$handler 보호되어 있는 프로퍼티

PDO instance
protected $handler