PhpToolCase
Api Documentation Version 0.9.2
PtcQueryBuilder Class Reference

Public Member Functions

 __construct (PDO $pdo=null)
 Adds the pdo instance to the query builder object. More...
 
 table ($table)
 Adds tables to the query. More...
 
 select ($columns)
 Sets the columns to be selected. More...
 
 rawSelect ($rawClause)
 Adds A raw where clause to the query. More...
 
 sanitize ($value)
 Sanitize unsafe data. More...
 
 raw ($value)
 Adds a raw value to a where clause in the query. More...
 
 addBackTicks ($string)
 Adds backticks to the passed string. More...
 
 join ($table, $first, $operator=null, $second=null, $type= 'inner')
 Creates a join based on the parameters. More...
 
 on ($column, $operator, $value, $type= 'and')
 Joins columns based on values. More...
 
 where ($column, $operator=null, $value=null, $type= 'and')
 Adds where clouses to the query. More...
 
 find ($id)
 returns 1 record from a given table based on the id. More...
 
 run ($query=null, $bind=null, $type=null)
 Runs queries if pdo object is present. More...
 
 row ($column=null)
 Retrieves 1 row from a given table. More...
 
 prepare ()
 Builds the query and returns it as string with place holders. More...
 
 order ($column, $direction= 'asc')
 Adds order to the query. More...
 
 group ($column)
 Adds group by to the query. More...
 
 limit ($start, $results=null)
 Adds limit to the query. More...
 
 countRows ()
 Returns number of affected rows from last query. More...
 
 lastId ()
 Retrieves last inserted id. More...
 
 insert ($array)
 Inserts a record in a given table. More...
 
 update ($array, $id=null)
 Updates records in a given table based on a where clause. More...
 
 delete ($id=null)
 Deletes rows from a given table based on a where clause. More...
 
 reset ()
 Resets the query parameters. More...
 
 setFetchMode ($mode, $class=null)
 Sets fetch mode for the next query. More...
 
 __call ($method, $args)
 Adds where operators and joins to the query. More...
 

Protected Member Functions

 _buildQuery ()
 Builds the query based on the type. More...
 
 _executeSql ($mode=1)
 Executes sql queries. More...
 
 _bind ($pos, $value, $type=null)
 Binds values to the query. More...
 
 _addAndOR ($value)
 adds "and" or "or" to the query More...
 
 _buildWhereClause ($type, $column, $operator=null, $value=null)
 Builds the where clause. More...
 
 _buildInClause ($column, $array, $type= 'in')
 Creates a "where in" statement. More...
 
 _buildBetweenClause ($column, $start, $end, $type= 'between')
 Creates a "where between" statement. More...
 
 _checkRawValue ($value)
 Checks if a rwa value was added. More...
 
 _checkOperator ($operator)
 
 _runClosure (Closure $function, $type)
 Runs closures for "where" and "joins". More...
 
 _checkPdo ()
 Checks if Pdo object was passed to the constructor to run queries. More...
 
 _isTableSet ()
 Checks if a table was set. More...
 
 _generateRandomId ()
 Generates a random numeric string to secure the raw function. More...
 
 _fireEvent ($data)
 Fires "ptc.query" event if PtcEvent class is present. More...
 
 _getEventClass ()
 Retrieves the event class names and it's namespace. More...
 
 _debugQuery ($string, $data)
 Sends queries to the Debugger & Logger if present. More...
 

Static Protected Member Functions

static _debug ($string, $statement=null, $category= 'QueryBuilder')
 Send messsages to the PtcDebug class if present. More...
 
static _debugBuffer ($reference, $type=null, $string=null, $statement=null)
 Adds execution time and query results to the PtcDebug class. More...
 

Protected Attributes

 $_bindLimit = array( )
 Limit property for the query. More...
 
 $_columns = '*'
 Columns property for the query. More...
 
 $_table = null
 Table property for the query. More...
 
 $_where = null
 Where property for the query. More...
 
 $_orderBy = null
 Order by property for the query. More...
 
 $_groupBy = null
 Group by property for the query. More...
 
 $_limit = null
 Limit property for the query. More...
 
 $_pdo = null
 Pdo object prperty to run queries. More...
 
 $_query = null
 Prepared query property. More...
 
 $_lastQuery = null
 Last query property. More...
 
 $_currentQuery = null
 Current query property. More...
 
 $_currentQueryType = null
 Current query type property. More...
 
 $_values = null
 Bind values property. More...
 
 $_bindings = array( )
 Place holders property. More...
 
 $_operators
 Operator for where and join clauses property. More...
 
 $_returnStatements = array( 'SHOW' , 'SELECT' )
 Queries that need a return result propeerty. More...
 
 $_join = null
 Join Property. More...
 
 $_randomId = null
 Random Id property for raw values. More...
 
 $_isClosure = false
 Property that checks if class is running a closure. More...
 
 $_fetchMode = array( )
 Fecth mode pdo property for current query. More...
 
 $_eventClass = '\PtcEvent'
 Event class name property. More...
 

Constructor & Destructor Documentation

__construct ( PDO  $pdo = null)

Adds the pdo instance to the query builder object.

See Getting Started

Parameters
object$pdothe pdo object

Definition at line 19 of file PtcQueryBuilder.php.

Member Function Documentation

__call (   $method,
  $args 
)

Adds where operators and joins to the query.

Parameters
string$methodthe method to call
array$argsarguments used by the called methods

Definition at line 421 of file PtcQueryBuilder.php.

_addAndOR (   $value)
protected

adds "and" or "or" to the query

Parameters
string$valuethe value to check

Definition at line 653 of file PtcQueryBuilder.php.

_bind (   $pos,
  $value,
  $type = null 
)
protected

Binds values to the query.

Parameters
mixed$posthe param position if numeric
mixed$valuethe value to bind the place holder to
contants$typea pdo constant to bind values

Definition at line 631 of file PtcQueryBuilder.php.

_buildBetweenClause (   $column,
  $start,
  $end,
  $type = 'between' 
)
protected

Creates a "where between" statement.

Definition at line 719 of file PtcQueryBuilder.php.

_buildInClause (   $column,
  $array,
  $type = 'in' 
)
protected

Creates a "where in" statement.

Definition at line 696 of file PtcQueryBuilder.php.

_buildQuery ( )
protected

Builds the query based on the type.

Definition at line 548 of file PtcQueryBuilder.php.

_buildWhereClause (   $type,
  $column,
  $operator = null,
  $value = null 
)
protected

Builds the where clause.

Parameters
string$typespecifies "and"/"or"
string$columnthe table column
string$operatorthe operator to use
mixed$valuethe value to check

Definition at line 674 of file PtcQueryBuilder.php.

_checkOperator (   $operator)
protected

Definition at line 755 of file PtcQueryBuilder.php.

_checkPdo ( )
protected

Checks if Pdo object was passed to the constructor to run queries.

Definition at line 780 of file PtcQueryBuilder.php.

_checkRawValue (   $value)
protected

Checks if a rwa value was added.

Definition at line 744 of file PtcQueryBuilder.php.

static _debug (   $string,
  $statement = null,
  $category = 'QueryBuilder' 
)
staticprotected

Send messsages to the PtcDebug class if present.

Parameters
mixed$stringthe string to pass
mixed$statementsome statement if required
string$categorya category for the messages panel

Definition at line 832 of file PtcQueryBuilder.php.

static _debugBuffer (   $reference,
  $type = null,
  $string = null,
  $statement = null 
)
staticprotected

Adds execution time and query results to the PtcDebug class.

Parameters
string$referencea reference to look for ("$statement")
string$typethe type of debug (timer, attach)
mixed$stringthe string to pass
mixed$statementsome new statement if required

Definition at line 859 of file PtcQueryBuilder.php.

_debugQuery (   $string,
  $data 
)
protected

Sends queries to the Debugger & Logger if present.

Definition at line 841 of file PtcQueryBuilder.php.

_executeSql (   $mode = 1)
protected

Executes sql queries.

See Specifying The Return Type

Parameters
numeric$modethe type of query (1,2,3)
Returns
the result if query was select, the affected rows otherwise

Definition at line 587 of file PtcQueryBuilder.php.

_fireEvent (   $data)
protected

Fires "ptc.query" event if PtcEvent class is present.

See Observer Events

Definition at line 812 of file PtcQueryBuilder.php.

_generateRandomId ( )
protected

Generates a random numeric string to secure the raw function.

Definition at line 805 of file PtcQueryBuilder.php.

_getEventClass ( )
protected

Retrieves the event class names and it's namespace.

Returns
the event class name defined in the "$_eventclass" property and the namespace

Definition at line 822 of file PtcQueryBuilder.php.

_isTableSet ( )
protected

Checks if a table was set.

Definition at line 793 of file PtcQueryBuilder.php.

_runClosure ( Closure  $function,
  $type 
)
protected

Runs closures for "where" and "joins".

Definition at line 767 of file PtcQueryBuilder.php.

addBackTicks (   $string)

Adds backticks to the passed string.

Parameters
string$stringthe column or table name
Returns
the string with backticks added,

Definition at line 115 of file PtcQueryBuilder.php.

countRows ( )

Returns number of affected rows from last query.

See Counting Rows

Returns
the affected rows by the last query

Definition at line 311 of file PtcQueryBuilder.php.

delete (   $id = null)

Deletes rows from a given table based on a where clause.

See Delete Records

Parameters
numeric$ida row id

Definition at line 365 of file PtcQueryBuilder.php.

find (   $id)

returns 1 record from a given table based on the id.

See Retrieving 1 Record From A Table

Parameters
numeric$idthe record id

Definition at line 182 of file PtcQueryBuilder.php.

group (   $column)

Adds group by to the query.

See Group, Order And Limit

Parameters
string$columnthe column names

Definition at line 283 of file PtcQueryBuilder.php.

insert (   $array)

Inserts a record in a given table.

See Insert Recods

Parameters
array$arrayan associative array , Ex.: array( column => value )

Definition at line 329 of file PtcQueryBuilder.php.

join (   $table,
  $first,
  $operator = null,
  $second = null,
  $type = 'inner' 
)

Creates a join based on the parameters.

See Joins

Parameters
string$tablethe name of the table to join
string$firstthe first column
string$operatorthe operator to use for the join
string$secondthe second column
string$typethe type of join

Definition at line 130 of file PtcQueryBuilder.php.

lastId ( )

Retrieves last inserted id.

See Last Inserted Id

Returns
the last inserted id

Definition at line 324 of file PtcQueryBuilder.php.

limit (   $start,
  $results = null 
)

Adds limit to the query.

See Group, Order And Limit

Parameters
string | int$startan integer value or a place holder
string | int$resultsan integer value or a place holder

Definition at line 294 of file PtcQueryBuilder.php.

on (   $column,
  $operator,
  $value,
  $type = 'and' 
)

Joins columns based on values.

See Joins

Parameters
string$columnthe first column for the join
string$operatorthe operator to use
string$valuethe second column for the join
string$typeadds "and" , "or" to multiple joins statements

Definition at line 146 of file PtcQueryBuilder.php.

order (   $column,
  $direction = 'asc' 
)

Adds order to the query.

See Group, Order And Limit

Parameters
string$columnthe column names
string$directionasc or desc

Definition at line 272 of file PtcQueryBuilder.php.

prepare ( )

Builds the query and returns it as string with place holders.

See Preparing Queries

Definition at line 252 of file PtcQueryBuilder.php.

raw (   $value)

Adds a raw value to a where clause in the query.

See Raw Values

Parameters
string$valuea raw value for the sql query
Returns
the raw value to be added with a unique identifier string

Definition at line 109 of file PtcQueryBuilder.php.

rawSelect (   $rawClause)

Adds A raw where clause to the query.

See Raw Statements

Parameters
string$rawClausethe where clause

Definition at line 88 of file PtcQueryBuilder.php.

reset ( )

Resets the query parameters.

Definition at line 387 of file PtcQueryBuilder.php.

row (   $column = null)

Retrieves 1 row from a given table.

See Retrieving 1 Record From A Table

Parameters
string$columncolumn name to return only 1 value as string
Returns
single column value if "$column" argument is set, the full row otherwise

Definition at line 228 of file PtcQueryBuilder.php.

run (   $query = null,
  $bind = null,
  $type = null 
)

Runs queries if pdo object is present.

See Getting Started

Parameters
string$querythe query to run
array$bindthe values to bind to the query
numeric$typethe query type ( 1,2,3)
Returns
the query result if select, otherwise the number of affected rows

Definition at line 199 of file PtcQueryBuilder.php.

sanitize (   $value)

Sanitize unsafe data.

Parameters
string$valuethe value to sanitize
Returns
the value with back slashes added

Definition at line 98 of file PtcQueryBuilder.php.

select (   $columns)

Sets the columns to be selected.

See Specifying Columns To Be Selected

Parameters
array | string$columnsthe columns to be selected

Definition at line 58 of file PtcQueryBuilder.php.

setFetchMode (   $mode,
  $class = null 
)

Sets fetch mode for the next query.

See Setting The Fetch Mode

Parameters
constant$modea pdo constant
mixed$classa class name if needed

Definition at line 406 of file PtcQueryBuilder.php.

table (   $table)

Adds tables to the query.

See Adding Multiple Tables

Parameters
array | string$tablethe name of the table

Definition at line 29 of file PtcQueryBuilder.php.

update (   $array,
  $id = null 
)

Updates records in a given table based on a where clause.

See Update Records

Parameters
array$arrayassociative array of values, Ex.: array( column => value )
numeric$ida row id

Definition at line 342 of file PtcQueryBuilder.php.

where (   $column,
  $operator = null,
  $value = null,
  $type = 'and' 
)

Adds where clouses to the query.

See Where Operators

Parameters
mixed$columnthe column name or a closure function
string$operatorthe operator to use for the where clause
string$valuethe value to look for in the column
string$typethe type of where clasuse

Definition at line 162 of file PtcQueryBuilder.php.

Field Documentation

$_bindings = array( )
protected

Place holders property.

Definition at line 515 of file PtcQueryBuilder.php.

$_bindLimit = array( )
protected

Limit property for the query.

Definition at line 463 of file PtcQueryBuilder.php.

$_columns = '*'
protected

Columns property for the query.

Definition at line 467 of file PtcQueryBuilder.php.

$_currentQuery = null
protected

Current query property.

Definition at line 503 of file PtcQueryBuilder.php.

$_currentQueryType = null
protected

Current query type property.

Definition at line 507 of file PtcQueryBuilder.php.

$_eventClass = '\PtcEvent'
protected

Event class name property.

Definition at line 544 of file PtcQueryBuilder.php.

$_fetchMode = array( )
protected

Fecth mode pdo property for current query.

Definition at line 540 of file PtcQueryBuilder.php.

$_groupBy = null
protected

Group by property for the query.

Definition at line 483 of file PtcQueryBuilder.php.

$_isClosure = false
protected

Property that checks if class is running a closure.

Definition at line 536 of file PtcQueryBuilder.php.

$_join = null
protected

Join Property.

Definition at line 528 of file PtcQueryBuilder.php.

$_lastQuery = null
protected

Last query property.

Definition at line 499 of file PtcQueryBuilder.php.

$_limit = null
protected

Limit property for the query.

Definition at line 487 of file PtcQueryBuilder.php.

$_operators
protected
Initial value:
= array( '=' , '<' , '>' , '<=' , '>=' , '<>' , '!=' ,
'like' , 'not like' , 'between' , 'ilike' )

Operator for where and join clauses property.

Definition at line 519 of file PtcQueryBuilder.php.

$_orderBy = null
protected

Order by property for the query.

Definition at line 479 of file PtcQueryBuilder.php.

$_pdo = null
protected

Pdo object prperty to run queries.

Definition at line 491 of file PtcQueryBuilder.php.

$_query = null
protected

Prepared query property.

Definition at line 495 of file PtcQueryBuilder.php.

$_randomId = null
protected

Random Id property for raw values.

Definition at line 532 of file PtcQueryBuilder.php.

$_returnStatements = array( 'SHOW' , 'SELECT' )
protected

Queries that need a return result propeerty.

Definition at line 524 of file PtcQueryBuilder.php.

$_table = null
protected

Table property for the query.

Definition at line 471 of file PtcQueryBuilder.php.

$_values = null
protected

Bind values property.

Definition at line 511 of file PtcQueryBuilder.php.

$_where = null
protected

Where property for the query.

Definition at line 475 of file PtcQueryBuilder.php.


The documentation for this class was generated from the following file: