Flupdo  v1.3-5-ge5a1c59
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
FlupdoBuilder Class Reference

Description

Query builder base class, which provides some magic to build queries.

Inheritance diagram for FlupdoBuilder

Public Member Functions

 __construct ($pdo, $log_query=false, $log_explain=false, $no_parenthesis_in_conditions=false)
 Constructor. More...
 
 __call ($method, $args)
 Call buffer-specific method to process arguments. More...
 
 quoteIdent ($ident)
 Quote identifier, preserve dot. More...
 
 compile ()
 Call compile function in a safe way. More...
 
 uncompile ()
 "Uncompile" the query. More...
 
 debugDump ()
 Fluently dump query to error log. More...
 
 getSqlQuery ()
 Get compiled SQL query, use only for debugging. More...
 
 getSqlParams ()
 Get parameters for compiled SQL query, use only for debugging. More...
 
 quote ($value)
 Quotes a string for use in a query. More...
 
 rawSql ($sql)
 Returns object marking raw SQL statement. More...
 
 exec ()
 Builds and executes an SQL statement, returning the number of affected rows. More...
 
 query ()
 Builds, binds and executes an SQL statement, returning a result set as a PDOStatement object. More...
 
 prepare ($driver_options=array())
 Builds and prepares a statement for execution, returns a statement object. More...
 
 lastInsertId ()
 Proxy to PDO::lastInsertId(). More...
 
 fetchSingleRow ()
 Fetch one row from result and close cursor. More...
 
 fetchSingleValue ()
 Fetch one row from result and close cursor. More...
 
 fetchAll ($key_column=null)
 Fetch everything into array. More...
 
 __toString ()
 Get SQL query as a string. More...
 

Static Public Member Functions

static generateDoxygenDocumentation ()
 Generate documentation for methods defined in $methods array. More...
 

Public Attributes

 $pdo
 PDO driver used to execute query and escape strings. More...
 

Protected Member Functions

 add ($args, $buffer_id)
 Add SQL fragment to buffer. More...
 
 replace ($args, $buffer_id)
 Replace buffer content with SQL fragment. More...
 
 setFlag ($args, $buffer_id, $label)
 Set flag. More...
 
 addJoin ($args, $buffer_id, $label)
 Add join statement to buffer. More...
 
 compileQuery ()
 Process all buffers and build SQL query. More...
 
 explain ()
 Explain the query and dump result to log. More...
 
 sqlStart ()
 Start SQL generating. More...
 
 sqlFinish ()
 Finish SQL generating. More...
 
 sqlBuffer ($buf)
 Add SQL with parameters. More...
 
 sqlRawBuffer ($buf)
 Generate raw SQL fragment. More...
 
 sqlComment ($buffer_id)
 Generate SQL comment. More...
 
 sqlFlag ($buffer_id)
 Generate flag fragment. More...
 
 sqlStatementFlags ($buffer_id, $flag_buffer_ids, $decorations)
 Generate SQL fragment made of flags. More...
 
 sqlList ($buffer_id, $decorations)
 Generate SQL fragment made of list. More...
 
 sqlValuesList ($buffer_id)
 Generate SQL fragment made of list values. More...
 
 sqlJoins ($buffer_id)
 Generate SQL fragment made of joins. More...
 
 sqlConditions ($buffer_id)
 Generate SQL fragment made of conditions in AND statement. More...
 

Protected Attributes

 $log_query
 Log all queries as they are executed. More...
 
 $log_explain
 Explain each query to log. More...
 
 $no_parenthesis_in_conditions
 Sphinx does not like parenthesis in WHERE. More...
 
 $can_explain = false
 Is it possible to explain this query? More...
 
 $indent = "\t"
 Indentation string. More...
 
 $sub_indent = "\t::t"
 Second level indentation string. More...
 
 $query_sql = null
 Built query. More...
 
 $query_params = null
 Parameters for prepared statement (to be bound before query is executed). More...
 
 $buffers = array()
 Buffers containing SQL fragments. More...
 

Static Protected Attributes

static $clauses = array()
 List of clauses used to composed result query. More...
 
static $methods = array()
 List of methods used to fill the $buffers. More...
 

Flags for helper methods.

Used by sqlList() and sqlStatementFlags().

const INDENT = 0x01
 List items should be indented. More...
 
const LABEL = 0x02
 SQL fragment has a label. More...
 
const BRACKETS = 0x04
 There are brackets around each item in the list. More...
 
const NO_SEPARATOR = 0x08
 No separator between list items. More...
 
const SUB_INDENT = 0x20
 Indent more the first line. More...
 
const COMMA = 0x40
 Add comma after the SQL fragment. More...
 
const EOL = 0x80
 Add EOL after the SQL fragment. More...
 
const ALL_DECORATIONS = 0xFF
 Make it fancy! More...
 

Constructor & Destructor Documentation

◆ __construct()

__construct (   $pdo,
  $log_query = false,
  $log_explain = false,
  $no_parenthesis_in_conditions = false 
)

Constructor.

Member Function Documentation

◆ __call()

__call (   $method,
  $args 
)

Call buffer-specific method to process arguments.

If the first argument is null, corresponding buffer will be deleted.

◆ quoteIdent()

quoteIdent (   $ident)

Quote identifier, preserve dot.

◆ add()

add (   $args,
  $buffer_id 
)
protected

Add SQL fragment to buffer.

◆ replace()

replace (   $args,
  $buffer_id 
)
protected

Replace buffer content with SQL fragment.

◆ setFlag()

setFlag (   $args,
  $buffer_id,
  $label 
)
protected

Set flag.

Replace buffer with new label of this flag.

◆ addJoin()

addJoin (   $args,
  $buffer_id,
  $label 
)
protected

Add join statement to buffer.

◆ compileQuery()

compileQuery ( )
abstractprotected

Process all buffers and build SQL query.

Side product is array of parameters (stored in $this->args) to bind with query.

This function is called by FlupdoBuilder, do not call it directly.

Example:

$this->sqlStart();
// ...
return $this->sqlFinish();

◆ compile()

compile ( )
final

Call compile function in a safe way.

◆ uncompile()

uncompile ( )

"Uncompile" the query.

This will drop compiled query and allow additional modifications. Once query is compiled additional modifications are not allowed to detect programming errors, but sometimes it is useful to execute the query and then perform additional modifications before second execution.

◆ debugDump()

debugDump ( )

Fluently dump query to error log.

◆ getSqlQuery()

getSqlQuery ( )

Get compiled SQL query, use only for debugging.

◆ getSqlParams()

getSqlParams ( )

Get parameters for compiled SQL query, use only for debugging.

◆ quote()

quote (   $value)

Quotes a string for use in a query.

Proxy to PDO::quote().

◆ rawSql()

rawSql (   $sql)

Returns object marking raw SQL statement.

◆ exec()

exec ( )

Builds and executes an SQL statement, returning the number of affected rows.

Proxy to PDO::exec().

◆ query()

query ( )

Builds, binds and executes an SQL statement, returning a result set as a PDOStatement object.

Proxy to PDOStatement::prepare() & PDOStatement::bindValue() & PDOStatement::query(). But if there is nothing to bind, PDO::query() is called instead.

◆ explain()

explain ( )
protected

Explain the query and dump result to log.

◆ prepare()

prepare (   $driver_options = array())

Builds and prepares a statement for execution, returns a statement object.

Proxy to PDO::prepare().

◆ lastInsertId()

lastInsertId ( )

Proxy to PDO::lastInsertId().

◆ fetchSingleRow()

fetchSingleRow ( )

Fetch one row from result and close cursor.

Returns what PDOStatement::fetch() would return.

◆ fetchSingleValue()

fetchSingleValue ( )

Fetch one row from result and close cursor.

Returns what PDOStatement::fetchColumn(0) would return.

◆ fetchAll()

fetchAll (   $key_column = null)

Fetch everything into array.

Returns what PDOStatement::fetchAll(PDO::FETCH_ASSOC) would return.

If $key_column is set, the specified column will be used to index array items.

Note
Keep in mind that this is not effective for larger data sets. It is better to perform the query and iterate over result instead of loading it all at once. However, it may not be possible in all cases.

◆ __toString()

__toString ( )

Get SQL query as a string.

◆ sqlStart()

sqlStart ( )
protected

Start SQL generating.

Uses output buffering to concatenate the query.

◆ sqlFinish()

sqlFinish ( )
protected

Finish SQL generating.

Picks up the query from output buffer.

◆ sqlBuffer()

sqlBuffer (   $buf)
protected

Add SQL with parameters.

Parameters are stored in groups, merge to one array is done at the end (using single array_merge call).

◆ sqlRawBuffer()

sqlRawBuffer (   $buf)
protected

Generate raw SQL fragment.

◆ sqlComment()

sqlComment (   $buffer_id)
protected

Generate SQL comment.

◆ sqlFlag()

sqlFlag (   $buffer_id)
protected

Generate flag fragment.

◆ sqlStatementFlags()

sqlStatementFlags (   $buffer_id,
  $flag_buffer_ids,
  $decorations 
)
protected

Generate SQL fragment made of flags.

◆ sqlList()

sqlList (   $buffer_id,
  $decorations 
)
protected

Generate SQL fragment made of list.

◆ sqlValuesList()

sqlValuesList (   $buffer_id)
protected

Generate SQL fragment made of list values.

◆ sqlJoins()

sqlJoins (   $buffer_id)
protected

Generate SQL fragment made of joins.

◆ sqlConditions()

sqlConditions (   $buffer_id)
protected

Generate SQL fragment made of conditions in AND statement.

◆ generateDoxygenDocumentation()

static generateDoxygenDocumentation ( )
static

Generate documentation for methods defined in $methods array.

Member Data Documentation

◆ $pdo

$pdo

PDO driver used to execute query and escape strings.

◆ $log_query

$log_query
protected

Log all queries as they are executed.

◆ $log_explain

$log_explain
protected

Explain each query to log.

◆ $no_parenthesis_in_conditions

$no_parenthesis_in_conditions
protected

Sphinx does not like parenthesis in WHERE.

◆ $can_explain

$can_explain = false
protected

Is it possible to explain this query?

◆ $indent

$indent = "\t"
protected

Indentation string.

◆ $sub_indent

$sub_indent = "\t::t"
protected

Second level indentation string.

◆ $query_sql

$query_sql = null
protected

Built query.

◆ $query_params

$query_params = null
protected

Parameters for prepared statement (to be bound before query is executed).

◆ $clauses

$clauses = array()
staticprotected

List of clauses used to composed result query.

Shared constant data.

◆ $methods

$methods = array()
staticprotected

List of methods used to fill the $buffers.

Shared constant data.

◆ $buffers

$buffers = array()
protected

Buffers containing SQL fragments.

◆ INDENT

const INDENT = 0x01

List items should be indented.

◆ LABEL

const LABEL = 0x02

SQL fragment has a label.

◆ BRACKETS

const BRACKETS = 0x04

There are brackets around each item in the list.

◆ NO_SEPARATOR

const NO_SEPARATOR = 0x08

No separator between list items.

◆ SUB_INDENT

const SUB_INDENT = 0x20

Indent more the first line.

◆ COMMA

const COMMA = 0x40

Add comma after the SQL fragment.

◆ EOL

const EOL = 0x80

Add EOL after the SQL fragment.

◆ ALL_DECORATIONS

const ALL_DECORATIONS = 0xFF

Make it fancy!