Show:

Db.Query.Mysql Class

Class implements MySQL query

Constructor

Db.Query.Mysql

(
  • mysql
  • type
  • query
  • parameters
  • table
)

Parameters:

  • mysql Object

    Db.Mysql

  • type Number

    One of the TYPE_* constants in Db.Query

  • query String | Object

    A sql query (for raw queries) or an associative array of clauses

  • parameters Object | Array

    The parameters to add to the query right away (to be bound when executing). Values corresponding to numeric keys replace question marks, while values corresponding to string keys replace ":key" placeholders, in the SQL.

  • table String

    The table operated with query

Methods

after

(
  • after
  • clause
)
Db.Query.Mysql chainable

Inserts a custom clause after a particular clause

Parameters:

  • after String

    The name of the standard clause to add after, such as FROM or UPDATE

  • clause String

    The text of the clause to add

Returns:

Db.Query.Mysql:

The resulting Db.Query object

andWhere

(
  • criteria
  • or_criteria
)
Db.Query.Mysql chainable

Adds to the WHERE clause, like this: " ... AND (x OR y OR z)", where x, y and z are the arguments to this function.

Parameters:

  • criteria Db.Expression | Object | String

    An associative array of expression: value pairs. The values are automatically turned into placeholders to be escaped later. They can also be arrays, in which case they are placed into an expression of the form "key IN ('val1', 'val2')" Or, this could be a Db.Expression object.

  • or_criteria Db.Expression | Object | String

    You can have any number of these, including zero.

Returns:

Db.Query.Mysql:

The resulting Db.Query object

begin

(
  • [lockType='FOR
)
chainable

Begins a transaction right before executing this query. The reason this method is part of the query class is because you often need the "where" clauses to figure out which database to send it to, if sharding is being used.

Parameters:

  • [lockType='FOR String

    UPDATE'] Defaults to 'FOR UPDATE', but can also be 'LOCK IN SHARE MODE', or set it to null to avoid adding a "LOCK" clause

build

(
  • [options={}]
)
String

Builds the query from the clauses

Parameters:

  • [options={}] Object optional

Returns:

commit

() Db.Query.Mysql chainable

Commits transaction when query is executed Use only with MySQL.

Returns:

Db.Query.Mysql:

The resulting Db.Query object

execute

(
  • callback
  • [options={}]
)

Executes the query against a database connection Connects to one or more shard(s) as necessary.

Parameters:

  • callback Function

    This function is called when the queries have all completed. It is passed the following arguments:

    • errors: an Object. If there were any errors, it will contain shardName: error pairs
    • results: an array of results merged from all the shards (for SELECT queries) for INSERT queries results contains the value of LAST_INSERT_ID()
    • fields: an array of fields merged from all the shards (for SELECT queries) It is passed an object whose keys are the shard names and the values are arrays of [err, results, fields] as returned from the mysql module.
  • [options={}] Object optional

    You can override the following options:

    • [plain=false] Boolean optional

      If true, returns array of plain object instead of Db.Row instances

    • [raw=false] Boolean optional

      If true, or if the query type is Db.Query.TYPE_RAW, the callback will be passed an object of pairs representing the results returned from the mysql query on each shard. Note that the results array will contain raw objects of the form "{fieldName: fieldValue};", and not objects which have Db.Row mixed in.

    • [shards] String | Array | Object optional

      This option will bypass the usual sharding calculations. You can pass a string here, which will be used to run the query on this shard. Or pass an array of shard names. Or you can specify custom query objects as {shardName: query}.

getClause

(
  • clause
  • with_after
)
String | Array

Gets a clause from the query

Parameters:

  • clause String

    Name of the clause

  • with_after Boolean

    Also get the sql after the clause, if any

Returns:

String | Array:

If with_after is true, returns [clause, after] Otherwise just returns clause

getSQL

(
  • callback
  • [shardName='']
)
Db.Query | String

Gets the SQL that would be executed with the execute() method.

Parameters:

  • callback Function

    This callback is passed the resulting SQL string.

  • [shardName=''] String optional

    The name of the shard on which to execute getSQL.

Returns:

Db.Query | String:

Returns the db query again, for chainable interface. If "callback" is not defined returns string representation of the query

groupBy

(
  • expression
)
Db.Query.Mysql chainable

Adds a GROUP BY clause to a query

Parameters:

  • expression Db.Expression | String

    A string or Db.Expression with the expression to group the results by.

Returns:

Db.Query.Mysql:

The resulting Db.Query object

having

(
  • criteria
)
Db.Query.Mysql chainable

Adds a HAVING clause to a query

Parameters:

  • criteria Db.Expression | Object | String

    An associative array of expression => value pairs. The values are automatically escaped using PDO placeholders. Or, this could be a Db.Expression object.

Returns:

Db.Query.Mysql:

The resulting Db.Query object

join

(
  • table
  • condition
  • [join_type='INNER']
)
Db.Query.Mysql chainable

Joins another table to use in the query

Parameters:

  • table String

    The name of the table. May also be "name AS alias".

  • condition Db.Expression | Object | String

    The condition to join on. Thus, JOIN table ON (condition)

  • [join_type='INNER'] String optional

    The string to prepend to JOIN, such as 'INNER', 'LEFT OUTER', etc.

Returns:

Db.Query.Mysql:

The resulting Db.Query object

limit

(
  • limit
  • [offset=0]
)
Db.Query.Mysql chainable

Adds optional LIMIT and OFFSET clauses to the query

Parameters:

  • limit Number

    A non-negative integer showing how many rows to return

  • [offset=0] Number optional

    A non-negative integer showing what row to start the result set with.

Returns:

Db.Query.Mysql:

The resulting Db.Query object

lock

(
  • [type='FOR
)
Db.Query.Mysql chainable

Works with SELECT queries to lock the selected rows. Use only with MySQL.

Parameters:

  • [type='FOR String

    UPDATE'] Defaults to 'FOR UPDATE', but can also be 'LOCK IN SHARE MODE'

Returns:

Db.Query.Mysql:

The resulting Db.Query object

onDuplicateKeyUpdate

(
  • updates
)
Db.Query.Mysql chainable

Adds an ON DUPLICATE KEY UPDATE clause to an INSERT statement. Use only with MySQL.

Parameters:

  • updates Object

    An associative array of {column: value} pairs. The values are automatically escaped using PDO placeholders.

Returns:

Db.Query.Mysql:

The resulting Db.Query object

options

(
  • options
)
Db.Query.Mysql chainable

This function provides an easy way to provide additional clauses to the query.

Parameters:

  • options Object

    An associative array of {key: value} pairs, where the key is the name of the method to call, and the value is the array of arguments. If the value is not an array, it is wrapped in one.

Returns:

Db.Query.Mysql:

The resulting Db.Query object

orderBy

(
  • expression
  • [ascending=false]
)
Db.Query.Mysql chainable

Adds a ORDER BY clause to a query

Parameters:

  • expression Db.Expression | String

    A string or Db.Expression with the expression to order the results by.

  • [ascending=false] Object optional

    Boolean If false, sorts results as ascending, otherwise descending.

Returns:

Db.Query.Mysql:

The resulting Db.Query object

orWhere

(
  • criteria
  • and_criteria
)
Db.Query.Mysql chainable

Adds to the WHERE clause, like this: " ... OR (x AND y AND z)", where x, y and z are the arguments to this function.

Parameters:

  • criteria Db.Expression | Object | String

    An associative array of expression: value pairs. The values are automatically turned into placeholders to be escaped later. They can also be arrays, in which case they are placed into an expression of the form "key IN ('val1', 'val2')" Or, this could be a Db.Expression object.

  • and_criteria Db.Expression | Object | String

Returns:

Db.Query.Mysql:

The resulting Db.Query object

reallyConnect

(
  • callback
  • [shardName='']
  • modifications={}
)

Create mysql.Connection and connects to the database table

Parameters:

  • callback Function

    The callback is fired after connection is complete. mysql.Connection is passed as argument

  • [shardName=''] String optional

    The name of the shard to connect

  • modifications={} Object

    Additional modifications to table information. If supplied override shard modifications

rollback

(
  • [criteria=null]
)
Db.Query.Mysql chainable

Rolls back transaction when query is executed Use only with MySQL.

Parameters:

  • [criteria=null] String optional

    Pass this to target the rollback to the right shard.

Returns:

Db.Query.Mysql:

The resulting Db.Query object

SELECT

(
  • fields
  • tables
  • [repeat=false]
)
Db.Query.Mysql chainable

Creates a query to select fields from one or more tables.

Parameters:

  • fields String | Object

    The fields as strings, or associative array of {alias: field};

  • tables String | Object

    The tables as strings, or associative array of {alias: table};

  • [repeat=false] Boolean optional

    If tables is an array, and select() has already been called with the exact table name and alias as one of the tables in that array, then this table is not appended to the tables list if repeat is false. Otherwise it is. This is really just for using in your hooks.

Returns:

Db.Query.Mysql:

The resulting query object.

set

(
  • updates
)
Db.Query.Mysql chainable

Adds a SET clause to an UPDATE statement

Parameters:

  • updates Object

    An associative array of column: value pairs. The values are automatically escaped using PDO placeholders.

Returns:

Db.Query.Mysql:

The resulting Db.Query object

toString

() String

Get string representation of the query

Returns:

String:

SQL statement

valueOf

() String

Get string representation of the query

Returns:

String:

SQL statement

where

(
  • criteria
)
Db.Query.Mysql chainable

Adds a WHERE clause to a query

Parameters:

  • criteria Db.Expression | Object | String

    An associative array of expression: value pairs. The values are automatically turned into placeholders to be escaped later. They can also be arrays, in which case they are placed into an expression of the form "key IN ('val1', 'val2')" Or, this could be a Db.Expression object.

Returns:

Db.Query.Mysql:

The resulting Db.Query

Properties

criteria

Object

Criteria used for sharding the query

table

String

The table operated with query

Events

error

Database error

Event Payload: