22 if ( $pdo ){ $this->_pdo = $pdo; }
32 $table = ( is_array( $table ) ) ? $table : array( $table );
33 foreach ( $table as $v )
37 $this->_table .=
' ' . $val.
',';
40 $divider = ( strpos( $v ,
' as ' ) ) ?
' as ' :
' AS ';
41 $table = explode( $divider , $v );
43 if ( array_key_exists( 1 , $table ) )
47 $this->_table .= $this->
sanitize( $t ) .
',';
50 $this->_table = substr( $this->_table , 0 , strlen( $this->_table ) - 1 );
51 $this->_currentQueryType =
'select';
61 $columns = is_array( $columns ) ? $columns : array( $columns );
62 foreach ( $columns as $v )
66 $this->_columns .=
' ' . $val.
',';
69 $divider = ( strpos( $v ,
' as ' ) ) ?
' as ' :
' AS ';
70 $column = explode( $divider , $v );
71 if ( preg_match(
'/\(([^\)]*)\)/' , $column[ 0 ] , $matches ) )
73 $col = str_replace( $matches[ 0 ] ,
'(' .
74 $this->
addBackTicks( $matches[ 1 ] ) .
')' , $column[ 0 ] );
77 $col .= ( array_key_exists( 1, $column ) ) ?
' as ' .
79 $this->_columns .= $this->
sanitize( $col ) .
',';
81 $this->_columns = substr( $this->_columns , 0 , strlen( $this->_columns ) - 1 ).
' ';
90 $this->_where = $this->_where .
' ' . $rawClause;
102 return addslashes( $value );
109 public function raw( $value ){
return $this->_randomId .
'RAW{' .$value .
'}'; }
118 $raw = explode(
'.' , $string );
119 $string = ( $raw[ 0 ] ===
'*' ) ? $raw[ 0 ] :
'`' . $raw[ 0 ] .
'`';
120 return $string .= ( @$raw[ 1 ] ) ?
'.`' . $raw[ 1 ] .
'`':
'';
130 public function join( $table , $first , $operator = null , $second = null , $type =
'inner' )
133 $this->_join .=
' ' . strtoupper( $type ) .
' JOIN ' . $this->
addBackTicks( $table );
134 $this->_isClosure =
true;
135 if ( $first instanceof Closure ){ $this->
_runClosure( $first ,
'join' ); }
136 else{ $this->
on( $first, $operator, $second ); }
146 public function on( $column, $operator, $value , $type =
'and' )
149 $this->_join .= (
$this->_isClosure ) ?
' ON ' :
' ' . strtoupper( $type ) .
' ';
150 $this->_isClosure =
false;
151 $this->_join .= $this->
addBackTicks( $column ) .
' ' . $operator .
' ' .
162 public function where( $column , $operator = null , $value = null , $type =
'and' )
165 if ( $column instanceof Closure )
170 if ( $this->_isClosure )
172 $this->_isClosure =
false;
185 $this->_where = null;
187 $query =
'SELECT ' . $this->_columns .
' FROM ' . $this->_table .
$this->_where;
188 $this->_currentQuery = $query;
190 return ( !empty( $result ) ) ? $result : null;
199 public function run( $query = null , $bind = null , $type = null )
201 if ( $this->_currentQueryType ==
'select' )
206 $this->_bindings = ( $bind ) ? $bind : $this->_bindings;
207 $this->_currentQuery = ( $query ) ? $query : $this->_currentQuery;
212 foreach ( $this->_returnStatements as $statement )
214 if ( strpos( trim( strtoupper( $this->_currentQuery ) ) , $statement ) === 0 )
228 public function row( $column = null )
237 $this->_currentQueryType =
'select';
240 if ( empty( $result ) ){
return null; }
243 if ( array_key_exists( $column , $result ) ){
return $result[ $column ]; }
244 trigger_error(
'Could not find column "' . $column .
'"!' , E_USER_WARNING );
247 else{
return $result; }
256 foreach ( $this->_bindings as $k => $v )
258 if ( is_string( $v ) )
260 if (
false === strpos( $v,
':' ) ) {
continue; }
261 $query = preg_replace(
'/\?/' , $v , $query , 1 );
272 public function order( $column , $direction =
'asc' )
274 $direction = strtoupper( $direction );
275 $this->_orderBy = (
$this->_orderBy ) ? $this->_orderBy .
', ' :
' ORDER BY ';
285 $this->_groupBy = (
$this->_groupBy ) ? $this->_groupBy .
', ' :
' GROUP BY ';
294 public function limit( $start , $results = null )
296 $start = is_numeric( $start ) ? ( int ) $start : $start;
297 $results = is_numeric( $results ) ? ( int ) $results : $results;
298 $this->_bindings[ ] = $start;
299 $this->_limit =
' LIMIT ?';
302 $this->_bindings[ ] = $results;
303 $this->_limit .=
',?';
313 if ( !$this->_lastQuery )
315 trigger_error(
'No queries Found to countRows!' , E_USER_NOTICE );
318 return $this->_lastQuery->rowCount( );
324 public function lastId( ) {
return $this->_pdo->lastInsertId( ); }
332 $this->_currentQueryType =
'insert';
333 $this->_values = $array;
342 public function update( $array , $id = null )
345 if ( !$this->_where && !$id )
347 trigger_error(
'No id or where clause was specified for the update!' ,
351 $this->_currentQueryType =
'update';
352 $this->_values = $array;
355 $this->_where = null;
365 public function delete( $id = null )
368 if ( !$this->_where && !$id )
370 trigger_error(
'No id or where clause was specified for the delete!' ,
374 $this->_currentQueryType =
'delete';
377 $this->_where = null;
378 $this->_bindings = null;
389 $this->_table = null;
390 $this->_where = null;
391 $this->_columns =
'*';
392 $this->_orderBy = null;
393 $this->_groupBy = null;
394 $this->_limit = null;
395 $this->_query = null;
396 $this->_currentQueryType = null;
397 $this->_currentQuery = null;
398 $this->_bindings = array( );
408 if ( $class && !class_exists( $class ) )
410 trigger_error(
'Class ' . $class .
' does not exists!' , E_USER_ERROR );
413 $this->_fetchMode = ( $class ) ? array( $mode , $class ) : array( $mode );
421 public function __call( $method , $args )
423 if (
false !== strpos( $method ,
'where' ) )
425 $type = str_replace(
'where' ,
'' , $method );
426 if (
false !== strpos( $type ,
'_between' ) )
432 else if (
false !== strpos( $type ,
'_in' ) )
440 $type = str_replace(
'_' ,
' ' , $type );
441 $this->
where( $args[ 0 ] , @$args[ 1 ] , @$args[ 2 ] , $type );
445 else if (
false !== strpos( $method ,
'_join' ) )
447 $type = str_replace(
'_join' ,
'' , $method );
448 $this->
join( $args[ 0 ] , @$args[ 1 ] , @$args[ 2 ] , @$args[ 3 ] , $type );
451 else if (
false !== strpos( $method ,
'_on' ) )
453 $type = str_replace(
'_on' ,
'' , $method );
454 $this->
on( $args[ 0 ] , @$args[ 1 ] , @$args[ 2 ] , $type );
457 trigger_error(
'Called to undefined method "' . get_called_class( ) .
'::' .
458 $method .
'( )"!' , E_USER_ERROR );
519 protected $_operators = array(
'=' ,
'<' ,
'>' ,
'<=' ,
'>=' ,
'<>' ,
'!=' ,
520 'like' ,
'not like' ,
'between' ,
'ilike' );
550 switch( $this->_currentQueryType )
553 $this->_bindings = array_values( $this->_values );
554 foreach ( $this->_values as $k => $v )
559 $fields = substr( $fields , 0 , strlen( $fields ) - 1 );
560 $values = substr( $values , 0 , strlen( $values ) - 1 );
561 $query =
'INSERT INTO ' . $this->_table .
' (' . $fields .
') VALUES (' . $values .
')';
564 $bind = array_values( $this->_values );
565 $this->_bindings = array_merge( $bind , $this->_bindings );
566 foreach ( $this->_values as $k => $v )
570 $values = substr( $values , 0 , strlen( $values ) - 1 );
571 $query =
"UPDATE " . $this->_table .
" SET " . $values .
$this->_where;
573 case 'delete' : $query =
'DELETE FROM ' . $this->_table .
$this->_where;
577 $query =
'SELECT ' . $this->_columns .
' FROM ' . $this->_table . $this->_join .
578 $this->_where . $this->_groupBy . $this->_orderBy .
$this->_limit;
589 if ( !$this->
_checkPdo( ) ) {
return false; }
591 self::_debug(
'' ,
' - ' . $query .
' - ' );
592 $this->_query = $this->_pdo->prepare( $query );
594 if ( !empty( $this->_bindings ) )
596 foreach ( $this->_bindings as $k => $v ) { $this->
_bind( $k , $v ); }
598 $this->_query->execute( );
599 if ( !empty( $this->_fetchMode ) )
601 if ( array_key_exists( 1 , $this->_fetchMode ) )
603 $this->_query->setFetchMode( $this->_fetchMode[ 0 ] , $this->_fetchMode[ 1 ] );
605 else{ $this->_query->setFetchMode( $this->_fetchMode[ 0 ] ); }
606 $this->_fetchMode = array( );
612 case 2 : $result = $this->_query->fetch( );
615 default : $result = $this->_query->fetchAll( );
617 self::_debugBuffer(
' - ' . $query .
' - ' );
618 $final_query = $this->
_debugQuery( $this->_lastQuery->queryString , $this->_bindings );
619 $this->
_fireEvent( array( $final_query , $this->_currentQuery , $this->_bindings ) );
622 self::_debugBuffer(
' - ' . $query .
' - ' ,
'attach' , $result ,
' - ' . $final_query .
' - ' );
631 protected function _bind( $pos , $value , $type = null )
633 if ( is_numeric( $pos ) ) { $pos = ( $pos + 1 ); }
634 if ( is_null( $type ) )
638 case is_int( $value ): $type = PDO::PARAM_INT;
640 case is_bool( $value ): $type = PDO::PARAM_BOOL;
642 case is_null( $value ): $type = PDO::PARAM_NULL;
644 default: $type = PDO::PARAM_STR;
647 $this->_query->bindValue( $pos , $value , $type );
655 if (
false !== strpos( $value ,
'or_' ) )
657 $value = str_replace(
'or_' ,
'' , $value );
662 $value = str_replace(
'and_' ,
'' , $value );
677 $this->_where .= (
$this->_where ) ?
' ' . strtoupper( $type ) .
' ' :
' WHERE ';
678 $this->_where .= $this->
addBackTicks( $column ) .
' ' . $operator;
679 if ( preg_match(
'|' . $this->_randomId .
'RAW{(.*?)}|', $value , $matches ) )
681 $this->_where .=
' ' . $matches[ 1 ];
686 $this->_where .=
' ' . $val;
689 $this->_where .=
' ?';
690 $this->_bindings[ ] = ( @get_magic_quotes_gpc( ) ) ? @stripslashes( $value ) : $value;
699 $this->_where .= strtoupper( str_replace(
'_' ,
' ' , $type ) ) .
' ';
700 $this->_where .=
' (';
701 foreach ( $array as $v )
705 $this->_where .=
' ' . $val;
708 $this->_where .=
'?,';
709 $this->_bindings[ ] = ( @get_magic_quotes_gpc( ) ) ? @stripslashes( $v ) : $v;
712 $this->_where = substr( $this->_where , 0 , strlen( $this->_where ) - 1 );
713 $this->_where .=
') ';
721 $needle = (
false !== strpos( $type ,
'_not' ) ) ?
' NOT BETWEEN ' :
' BETWEEN ';
722 $type = str_replace(
'_not' ,
' ' , $type );
723 $type = strtoupper( $type );
724 $type = str_replace(
'_BETWEEN' , $this->
addBackTicks( $column ) . $needle , $type );
725 $this->_where .= $type .
' ';
726 if ( $val = $this->
_checkRawValue( $start ) ){ $this->_where .=
' ' . $val; }
729 $this->_where .=
' ? ';
730 $this->_bindings[ ] = ( get_magic_quotes_gpc( ) ) ? stripslashes( $start ) : $start;
732 $this->_where .=
' AND ';
733 if ( $val = $this->
_checkRawValue( $end ) ){ $this->_where .=
' ' . $val; }
736 $this->_where .=
' ? ';
737 $this->_bindings[ ] = ( get_magic_quotes_gpc( ) ) ? stripslashes( $end ) : $end;
746 if ( preg_match(
'|' . $this->_randomId .
'RAW{(.*?)}|', $value , $matches ) )
748 return $matches[ 1 ];
757 if ( !in_array( strtolower( $operator ) , $this->_operators ) )
759 trigger_error(
'Invalid query operator "' . $operator .
'"!' , E_USER_ERROR );
769 $this->_isClosure =
true;
770 if (
'join' === $type ){
return call_user_func_array( $function , array( $this ) ); }
771 $this->_where .=
' ' . strtoupper( str_replace(
'_' ,
' ' , strtolower( $type ) ) );
772 $this->_where .=
' ( ';
773 call_user_func_array( $function , array( $this ) );
774 $this->_where .=
' ) ';
775 $this->_isClosure =
false;
784 trigger_error(
'Pdo was not set to execute queries with query builder!' ,
795 if ( !$this->_table )
797 trigger_error(
'No table set for query, use table( )!' , E_USER_ERROR );
807 return mt_rand( 1000000 , 9999999 ) .
'_';
814 if ( !class_exists( $event_class = $this->
_getEventClass( ) ) ){
return; }
815 $listeners = $event_class::getEvents(
'ptc' );
816 if ( @$listeners[
'query' ] ){ $event_class::fire(
'ptc.query' , $data ); }
832 protected static function _debug( $string , $statement = null , $category =
'QueryBuilder' )
834 if ( !defined(
'_PTCDEBUG_NAMESPACE_' ) ){
return false; }
835 return @call_user_func_array( array( _PTCDEBUG_NAMESPACE_ ,
'bufferSql' ) ,
836 array( $string , $statement , $category ) );
843 $indexed=$data==array_values( $data );
844 foreach ( $data as $k => $v )
846 $v = ( is_string( $v ) ) ?
"'$v'" : $v;
847 if ( $indexed ){ $string = preg_replace(
'/\?/' , $v , $string , 1 ); }
848 else{ $string = preg_replace(
'/' . $k .
'/' , $v , $string , 1 ); }
859 protected static function _debugBuffer( $reference , $type = null , $string = null , $statement = null )
861 if ( !defined(
'_PTCDEBUG_NAMESPACE_' ) ){
return false; }
862 if ( $type ==
'attach' )
864 return @call_user_func_array( array(
'\\' . _PTCDEBUG_NAMESPACE_ ,
865 'addToBuffer' ) , array( $reference , $string , $statement ) );
869 return @call_user_func_array( array(
'\\' .
870 _PTCDEBUG_NAMESPACE_ ,
'stopTimer' ) , array( $reference ) );
sanitize($value)
Sanitize unsafe data.
setFetchMode($mode, $class=null)
Sets fetch mode for the next query.
on($column, $operator, $value, $type= 'and')
Joins columns based on values.
$_where
Where property for the query.
_checkPdo()
Checks if Pdo object was passed to the constructor to run queries.
$_isClosure
Property that checks if class is running a closure.
__call($method, $args)
Adds where operators and joins to the query.
$_limit
Limit property for the query.
$_returnStatements
Queries that need a return result propeerty.
_runClosure(Closure $function, $type)
Runs closures for "where" and "joins".
_generateRandomId()
Generates a random numeric string to secure the raw function.
lastId()
Retrieves last inserted id.
_debugQuery($string, $data)
Sends queries to the Debugger & Logger if present.
$_columns
Columns property for the query.
$_lastQuery
Last query property.
prepare()
Builds the query and returns it as string with place holders.
addBackTicks($string)
Adds backticks to the passed string.
_checkOperator($operator)
$_bindLimit
Limit property for the query.
$_eventClass
Event class name property.
_executeSql($mode=1)
Executes sql queries.
_addAndOR($value)
adds "and" or "or" to the query
_buildBetweenClause($column, $start, $end, $type= 'between')
Creates a "where between" statement.
_isTableSet()
Checks if a table was set.
$_orderBy
Order by property for the query.
$_currentQuery
Current query property.
_fireEvent($data)
Fires "ptc.query" event if PtcEvent class is present.
order($column, $direction= 'asc')
Adds order to the query.
find($id)
returns 1 record from a given table based on the id.
select($columns)
Sets the columns to be selected.
run($query=null, $bind=null, $type=null)
Runs queries if pdo object is present.
_buildQuery()
Builds the query based on the type.
__construct(PDO $pdo=null)
Adds the pdo instance to the query builder object.
_getEventClass()
Retrieves the event class names and it's namespace.
rawSelect($rawClause)
Adds A raw where clause to the query.
$_fetchMode
Fecth mode pdo property for current query.
limit($start, $results=null)
Adds limit to the query.
$_bindings
Place holders property.
group($column)
Adds group by to the query.
_buildWhereClause($type, $column, $operator=null, $value=null)
Builds the where clause.
$_operators
Operator for where and join clauses property.
$_table
Table property for the query.
update($array, $id=null)
Updates records in a given table based on a where clause.
_buildInClause($column, $array, $type= 'in')
Creates a "where in" statement.
$_randomId
Random Id property for raw values.
row($column=null)
Retrieves 1 row from a given table.
static _debugBuffer($reference, $type=null, $string=null, $statement=null)
Adds execution time and query results to the PtcDebug class.
countRows()
Returns number of affected rows from last query.
_bind($pos, $value, $type=null)
Binds values to the query.
$_currentQueryType
Current query type property.
insert($array)
Inserts a record in a given table.
$_query
Prepared query property.
$_values
Bind values property.
table($table)
Adds tables to the query.
$_pdo
Pdo object prperty to run queries.
raw($value)
Adds a raw value to a where clause in the query.
reset()
Resets the query parameters.
static _debug($string, $statement=null, $category= 'QueryBuilder')
Send messsages to the PtcDebug class if present.
join($table, $first, $operator=null, $second=null, $type= 'inner')
Creates a join based on the parameters.
where($column, $operator=null, $value=null, $type= 'and')
Adds where clouses to the query.
_checkRawValue($value)
Checks if a rwa value was added.
$_groupBy
Group by property for the query.