Overview

Namespaces

  • None
  • RedBeanPHP
    • Adapter
    • BeanHelper
    • Cursor
    • Driver
    • Logger
      • RDefault
    • QueryWriter
    • RedException
    • Repository
    • Util

Classes

  • AssociationManager
  • BeanCollection
  • DuplicationManager
  • Facade
  • Finder
  • Jsonable
  • LabelMaker
  • Observable
  • OODB
  • OODBBean
  • R
  • Repository
  • SimpleModel
  • SimpleModelHelper
  • TagManager
  • ToolBox

Interfaces

  • Adapter
  • BeanHelper
  • Cursor
  • Driver
  • Logger
  • Observer
  • Plugin
  • QueryWriter

Exceptions

  • RedException
  • Overview
  • Namespace
  • Class

Interface QueryWriter

QueryWriter Interface for QueryWriters. Describes the API for a QueryWriter.

Terminology:

  • beautified property (a camelCased property, has to be converted first)
  • beautified type (a camelCased type, has to be converted first)
  • type (a bean type, corresponds directly to a table)
  • property (a bean property, corresponds directly to a column)
  • table (a checked and quoted type, ready for use in a query)
  • column (a checked and quoted property, ready for use in query)
  • tableNoQ (same as type, but in context of a database operation)
  • columnNoQ (same as property, but in context of a database operation)

Direct known implementers

RedBeanPHP\QueryWriter\CUBRID, RedBeanPHP\QueryWriter\MySQL, RedBeanPHP\QueryWriter\PostgreSQL, RedBeanPHP\QueryWriter\SQLiteT
Namespace: RedBeanPHP
Copyright:

copyright (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community. This source file is subject to the BSD/GPLv2 License that is bundled with this source code in the file license.txt.


License: BSD/GPLv2
Author: Gabor de Mooij and the RedBeanPHP community
File: RedBeanPHP/QueryWriter.php
Located at QueryWriter.php
Methods summary
public string
# writeJoin( string $type, string $targetType, string $joinType )

Writes an SQL Snippet for a JOIN, returns the SQL snippet string.

Writes an SQL Snippet for a JOIN, returns the SQL snippet string.

Parameters

$type
source type
$targetType
target type (type to join)
$joinType
$leftRight type of join (possible: 'LEFT', 'RIGHT' or 'INNER').

Returns

string
$joinSQLSnippet

Note

A default implementation is available in AQueryWriter unless a database uses very different SQL this should suffice.


public string
# glueSQLCondition( string $sql, integer $glue = NULL )

Glues an SQL snippet to the beginning of a WHERE clause. This ensures users don't have to add WHERE to their query snippets.

Glues an SQL snippet to the beginning of a WHERE clause. This ensures users don't have to add WHERE to their query snippets.

The snippet gets prefixed with WHERE or AND if it starts with a condition.

If the snippet does NOT start with a condition (or this function thinks so) the snippet is returned as-is.

The GLUE type determines the prefix:

  • NONE prefixes with WHERE
  • WHERE prefixes with WHERE and replaces AND if snippets starts with AND
  • AND prefixes with AND

This method will never replace WHERE with AND since a snippet should never begin with WHERE in the first place. OR is not supported.

Only a limited set of clauses will be recognized as non-conditions. For instance beginning a snippet with complex statements like JOIN or UNION will not work. This is too complex for use in a snippet.

Parameters

$sql
SQL Snippet
$glue
the GLUE type - how to glue (C_GLUE_WHERE or C_GLUE_AND)

Returns

string

Note

A default implementation is available in AQueryWriter unless a database uses very different SQL this should suffice.


public string
# glueLimitOne( string $sql )

Determines if there is a LIMIT 1 clause in the SQL. If not, it will add a LIMIT 1. (used for findOne).

Determines if there is a LIMIT 1 clause in the SQL. If not, it will add a LIMIT 1. (used for findOne).

Parameters

$sql
query to scan and adjust

Returns

string

Note

A default implementation is available in AQueryWriter unless a database uses very different SQL this should suffice.


public array
# getTables( )

Returns the tables that are in the database.

Returns the tables that are in the database.

Returns

array
public
# createTable( string $type )

This method will create a table for the bean. This methods accepts a type and infers the corresponding table name.

This method will create a table for the bean. This methods accepts a type and infers the corresponding table name.

Parameters

$type
type of bean you want to create a table for
public array
# getColumns( string $type )

Returns an array containing all the columns of the specified type. The format of the return array looks like this: $field => $type where $field is the name of the column and $type is a database specific description of the datatype.

Returns an array containing all the columns of the specified type. The format of the return array looks like this: $field => $type where $field is the name of the column and $type is a database specific description of the datatype.

This methods accepts a type and infers the corresponding table name.

Parameters

$type
type of bean you want to obtain a column list of

Returns

array
public integer
# scanType( string $value, $alsoScanSpecialForTypes = FALSE )

Returns the Column Type Code (integer) that corresponds to the given value type. This method is used to determine the minimum column type required to represent the given value.

Returns the Column Type Code (integer) that corresponds to the given value type. This method is used to determine the minimum column type required to represent the given value.

Parameters

$value
value
$alsoScanSpecialForTypes

Returns

integer
public
# addColumn( string $type, string $column, integer $field )

This method will add a column to a table. This methods accepts a type and infers the corresponding table name.

This method will add a column to a table. This methods accepts a type and infers the corresponding table name.

Parameters

$type
name of the table
$column
name of the column
$field
data type for field
public integer
# code( string $typedescription, boolean $includeSpecials = FALSE )

Returns the Type Code for a Column Description. Given an SQL column description this method will return the corresponding code for the writer. If the include specials flag is set it will also return codes for special columns. Otherwise special columns will be identified as specified columns.

Returns the Type Code for a Column Description. Given an SQL column description this method will return the corresponding code for the writer. If the include specials flag is set it will also return codes for special columns. Otherwise special columns will be identified as specified columns.

Parameters

$typedescription
description
$includeSpecials
whether you want to get codes for special columns as well

Returns

integer
public
# widenColumn( string $type, string $column, integer $datatype )

This method will widen the column to the specified data type. This methods accepts a type and infers the corresponding table name.

This method will widen the column to the specified data type. This methods accepts a type and infers the corresponding table name.

Parameters

$type
type / table that needs to be adjusted
$column
column that needs to be altered
$datatype
target data type
public array
# queryRecord( string $type, array $conditions = array(), string $addSql = NULL, array $bindings = array() )

Selects records from the database. This methods selects the records from the database that match the specified type, conditions (optional) and additional SQL snippet (optional).

Selects records from the database. This methods selects the records from the database that match the specified type, conditions (optional) and additional SQL snippet (optional).

Parameters

$type
name of the table you want to query
$conditions
criteria ( $column => array( $values ) )
$addSql
additional SQL snippet
$bindings
bindings for SQL snippet

Returns

array
public RedBeanPHP\Cursor
# queryRecordWithCursor( string $type, array $addSql = NULL, string $bindings = array() , array $bindings,… )

Selects records from the database and returns a cursor. This methods selects the records from the database that match the specified type, conditions (optional) and additional SQL snippet (optional).

Selects records from the database and returns a cursor. This methods selects the records from the database that match the specified type, conditions (optional) and additional SQL snippet (optional).

Parameters

$type
name of the table you want to query
$addSql
$conditions criteria ( $column => array( $values ) )
$bindings
$addSQL additional SQL snippet
$bindings,…
bindings for SQL snippet

Returns

RedBeanPHP\Cursor
public array
# queryRecordRelated( string $sourceType, string $destType, mixed $linkID, string $addSql = '', array $bindings = array() )

Returns records through an intermediate type. This method is used to obtain records using a link table and allows the SQL snippets to reference columns in the link table for additional filtering or ordering.

Returns records through an intermediate type. This method is used to obtain records using a link table and allows the SQL snippets to reference columns in the link table for additional filtering or ordering.

Parameters

$sourceType
source type, the reference type you want to use to fetch related items on the other side
$destType
destination type, the target type you want to get beans of
$linkID
ID to use for the link table
$addSql
Additional SQL snippet
$bindings
Bindings for SQL snippet

Returns

array
public array|null
# queryRecordLink( string $sourceType, string $destType, string $sourceID, string $destID )

Returns the row that links $sourceType $sourcID to $destType $destID in an N-M relation.

Returns the row that links $sourceType $sourcID to $destType $destID in an N-M relation.

Parameters

$sourceType
source type, the first part of the link you're looking for
$destType
destination type, the second part of the link you're looking for
$sourceID
ID for the source
$destID
ID for the destination

Returns

array|null
public integer
# queryRecordCount( string $type, array $conditions = array(), string $addSql = NULL, array $bindings = array() )

Counts the number of records in the database that match the conditions and additional SQL.

Counts the number of records in the database that match the conditions and additional SQL.

Parameters

$type
name of the table you want to query
$conditions
criteria ( $column => array( $values ) )
$addSql
additional SQL snippet
$bindings
bindings for SQL snippet

Returns

integer
public integer
# queryRecordCountRelated( string $sourceType, string $targetType, mixed $linkID, string $addSQL = '', array $bindings = array() )

Returns the number of records linked through $linkType and satisfying the SQL in $addSQL/$bindings.

Returns the number of records linked through $linkType and satisfying the SQL in $addSQL/$bindings.

Parameters

$sourceType
source type
$targetType
the thing you want to count
$linkID
the of the source type
$addSQL
additional SQL snippet
$bindings
bindings for SQL snippet

Returns

integer
public array
# queryTagged( string $type, array $tagList, boolean $all = FALSE, string $addSql = '', array $bindings = array() )

Returns all rows of specified type that have been tagged with one of the strings in the specified tag list array.

Returns all rows of specified type that have been tagged with one of the strings in the specified tag list array.

Note that the additional SQL snippet can only be used for pagination, the SQL snippet will be appended to the end of the query.

Parameters

$type
the bean type you want to query
$tagList
an array of strings, each string containing a tag title
$all
if TRUE only return records that have been associated with ALL the tags in the list
$addSql
addition SQL snippet, for pagination
$bindings
parameter bindings for additional SQL snippet

Returns

array
public integer
# updateRecord( string $type, array $updatevalues, integer $id = NULL )

This method should update (or insert a record), it takes a table name, a list of update values ( $field => $value ) and an primary key ID (optional). If no primary key ID is provided, an INSERT will take place. Returns the new ID. This methods accepts a type and infers the corresponding table name.

This method should update (or insert a record), it takes a table name, a list of update values ( $field => $value ) and an primary key ID (optional). If no primary key ID is provided, an INSERT will take place. Returns the new ID. This methods accepts a type and infers the corresponding table name.

Parameters

$type
name of the table to update
$updatevalues
list of update values
$id
optional primary key ID value

Returns

integer
public
# deleteRecord( string $type, array $conditions = array(), string $addSql = '', array $bindings = array() )

Deletes records from the database.

Deletes records from the database.

Parameters

$type
name of the table you want to query
$conditions
criteria ( $column => array( $values ) )
$addSql
$sql additional SQL
$bindings
bindings

Note

$addSql is always prefixed with ' WHERE ' or ' AND .'
public
# deleteRelations( string $sourceType, string $destType, string $sourceID )

Deletes all links between $sourceType and $destType in an N-M relation.

Deletes all links between $sourceType and $destType in an N-M relation.

Parameters

$sourceType
source type
$destType
destination type
$sourceID
source ID
public
# addUniqueIndex( $type, $columns )

See

QueryWriter::addUniqueConstaint
public
# addUniqueConstraint( string $type, array $columns )

This method will add a UNIQUE constraint index to a table on columns $columns. This methods accepts a type and infers the corresponding table name.

This method will add a UNIQUE constraint index to a table on columns $columns. This methods accepts a type and infers the corresponding table name.

Parameters

$type
target bean type
$columns
$columnsPartOfIndex columns to include in index
public boolean
# sqlStateIn( string $state, array $list )

This method will check whether the SQL state is in the list of specified states and returns TRUE if it does appear in this list or FALSE if it does not. The purpose of this method is to translate the database specific state to a one of the constants defined in this class and then check whether it is in the list of standard states provided.

This method will check whether the SQL state is in the list of specified states and returns TRUE if it does appear in this list or FALSE if it does not. The purpose of this method is to translate the database specific state to a one of the constants defined in this class and then check whether it is in the list of standard states provided.

Parameters

$state
SQL state to consider
$list
list of standardized SQL state constants to check against

Returns

boolean
public
# wipe( string $type )

This method will remove all beans of a certain type. This methods accepts a type and infers the corresponding table name.

This method will remove all beans of a certain type. This methods accepts a type and infers the corresponding table name.

Parameters

$type
bean type
public
# addFK( string $type, string $targetType, string $property, string $targetProperty, string $isDep = false )

This method will add a foreign key from type and field to target type and target field. The foreign key is created without an action. On delete/update no action will be triggered. The FK is only used to allow database tools to generate pretty diagrams and to make it easy to add actions later on. This methods accepts a type and infers the corresponding table name.

This method will add a foreign key from type and field to target type and target field. The foreign key is created without an action. On delete/update no action will be triggered. The FK is only used to allow database tools to generate pretty diagrams and to make it easy to add actions later on. This methods accepts a type and infers the corresponding table name.

Parameters

$type
type that will have a foreign key field
$targetType
points to this type
$property
field that contains the foreign key value
$targetProperty
field where the fk points to
$isDep
whether target is dependent and should cascade on update/delete
public
# addIndex( string $type, string $name, string $property )

This method will add an index to a type and field with name $name. This methods accepts a type and infers the corresponding table name.

This method will add an index to a type and field with name $name. This methods accepts a type and infers the corresponding table name.

Parameters

$type
type to add index to
$name
name of the new index
$property
field to index
public string
# esc( string $databaseStructure, boolean $dontQuote = FALSE )

Checks and filters a database structure element like a table of column for safe use in a query. A database structure has to conform to the RedBeanPHP DB security policy which basically means only alphanumeric symbols are allowed. This security policy is more strict than conventional SQL policies and does therefore not require database specific escaping rules.

Checks and filters a database structure element like a table of column for safe use in a query. A database structure has to conform to the RedBeanPHP DB security policy which basically means only alphanumeric symbols are allowed. This security policy is more strict than conventional SQL policies and does therefore not require database specific escaping rules.

Parameters

$databaseStructure
name of the column/table to check
$dontQuote
$noQuotes TRUE to NOT put backticks or quotes around the string

Returns

string
public
# wipeAll( )

Removes all tables and views from the database.

Removes all tables and views from the database.

public
# renameAssocTable( string|array $fromType, string $toType = NULL )

Renames an association. For instance if you would like to refer to album_song as: track you can specify this by calling this method like:

Renames an association. For instance if you would like to refer to album_song as: track you can specify this by calling this method like:

renameAssociation('album_song','track')

This allows:

$album->sharedSong

to add/retrieve beans from track instead of album_song. Also works for exportAll().

This method also accepts a single associative array as its first argument.

Parameters

$fromType
original type name, or array
$toType
new type name (only if 1st argument is string)
public string
# getAssocTable( array $types )

Returns the format for link tables. Given an array containing two type names this method returns the name of the link table to be used to store and retrieve association records. For instance, given two types: person and project, the corresponding link table might be: 'person_project'.

Returns the format for link tables. Given an array containing two type names this method returns the name of the link table to be used to store and retrieve association records. For instance, given two types: person and project, the corresponding link table might be: 'person_project'.

Parameters

$types
two types array($type1, $type2)

Returns

string
public string|null
# inferFetchType( $type, $property )

Given a bean type and a property, this method tries to infer the fetch type using the foreign key definitions in the database. For instance: project, student -> person. If no fetchType can be inferred, this method will return NULL.

Given a bean type and a property, this method tries to infer the fetch type using the foreign key definitions in the database. For instance: project, student -> person. If no fetchType can be inferred, this method will return NULL.

Parameters

$type
source type to fetch a target type for
$property
property to fetch the type of

Returns

string|null

Note

QueryWriters do not have to implement this method, it's optional. A default version is available in AQueryWriter.


Constants summary
string C_SQLFILTER_READ

SQL filter constants

SQL filter constants

# 'r'
string C_SQLFILTER_WRITE
# 'w'
integer C_SQLSTATE_NO_SUCH_TABLE

Query Writer constants.

Query Writer constants.

# 1
integer C_SQLSTATE_NO_SUCH_COLUMN
# 2
integer C_SQLSTATE_INTEGRITY_CONSTRAINT_VIOLATION
# 3
integer C_DATATYPE_RANGE_SPECIAL

Define data type regions

Define data type regions

00 - 80: normal data types 80 - 99: special data types, only scan/code if requested 99 : specified by user, don't change

# 80
integer C_DATATYPE_RANGE_SPECIFIED
# 99
integer C_GLUE_WHERE

Define GLUE types for use with glueSQLCondition methods. Determines how to prefix a snippet of SQL before appending it to other SQL (or integrating it, mixing it otherwise).

Define GLUE types for use with glueSQLCondition methods. Determines how to prefix a snippet of SQL before appending it to other SQL (or integrating it, mixing it otherwise).

WHERE - glue as WHERE condition AND - glue as AND condition

# 1
integer C_GLUE_AND
# 2
API documentation generated by ApiGen