Show:

Db_Interface Class

Module: Db

Interface class for database connection. An adapter must support it to implement the Db class.

Constructor

Db_Interface

(
  • $conn_name
  • $pdo
)
static

Parameters:

  • $conn_name String

    The name of the connection

  • $pdo PDO

Methods

__call

(
  • $name
  • $arguments
)

Forwards all other calls to the PDO object

Parameters:

  • $name String

    The function name

  • $arguments Array

    The arguments

codeForModelBaseClass

(
  • $table
  • $directory
  • $classname_prefix=''
  • &$class_name=null
  • $prefix=null
)
String

Generates a base class for the model

Parameters:

  • $table String

    The name of the table to generate the code for.

  • $directory String

    The path of the directory in which to place the model code.

  • $classname_prefix='' String

    Prefix for class name

  • &$class_name=null String

    If set, this is the class name that is used. If an unset variable is passed, it is filled with the class name that is ultimately chosen from the $classname_prefix and $table_name.

  • $prefix=null String

Returns:

String:

The generated code for the class.

connection

() String

Returns the connection with which this Db object was created.

Returns:

String:

connectionName

() String

Returns the name of the connection with which this Db object was created.

Returns:

String:

dbms

() String

Returns the lowercase name of the dbms (e.g. "mysql")

Returns:

String:

dbName

() String

Returns the name of the database used

Returns:

String:

delete

(
  • $table_from
  • $table_using=null
)
Db_Query

Creates a query to delete rows.

Parameters:

  • $table_from String

    The table to delete from

  • $table_using=null String

Returns:

dsn

() Array

Returns an associative array representing the dsn

Returns:

Array:

fromDateTime

(
  • $syntax
  • $datetime
)
String

Returns a timestamp from a DateTime string

Parameters:

  • $syntax String

    The format of the date string, see date() function.

  • $datetime String

    The DateTime string that comes from the db

Returns:

String:

The timestamp

generateModels

(
  • $conn_name
  • $directory
  • $classname_prefix=null
)

Generates base classes of the models, and if they don't exist, skeleton code for the models themselves. Use it only after you have made changes to the database schema. You shouldn't be using it on every request.

Parameters:

  • $conn_name String

    The name of a previously registered connection.

  • $directory String

    The directory in which to generate the files. If the files already exist, they are not overwritten, unless they are inside the "generated" subdirectory. If the "generated" subdirectory does not exist, it is created.

  • $classname_prefix=null String

    The prefix to prepend to the generated class names. If not specified, prefix becomes "ConnName", where conn_name is the name of the connection.

insert

(
  • $table_into
  • $fields=array()
)
Db_Query

Creates a query to insert a row into a table

Parameters:

  • $table_into String

    The name of the table to insert into

  • $fields=array() Array

    The fields as an array of column=>value pairs

Returns:

Db_Query:

The resulting Db_Query object

insertManyAndExecute

(
  • $table_into
  • [$rows=array()]
  • [$options=array()]
)

Inserts multiple rows into a single table, preparing the statement only once, and executes all the queries.

Parameters:

  • $table_into String

    The name of the table to insert into

  • [$rows=array()] Array optional

    The array of rows to insert. Each row should be an array of ($field => $value) pairs, with the exact same set of keys (field names) in each array. It can also be a Db_Row.

  • [$options=array()] Array optional

    An associative array of options, including:

    • [className] String optional

      If you provide the class name, the system will be able to use any sharding indexes under that class name in the config.

    • [chunkSize] Integer optional

      The number of rows to insert at a time. Defaults to 20. You can also put 0 here, which means unlimited chunks, but it's not recommended.

    • [onDuplicateKeyUpdate] Array optional

      You can put an array of fieldname => value pairs here, which will add an ON DUPLICATE KEY UPDATE clause to the query.

rank

(
  • $table
  • $pts_field
  • $rank_field
  • $chunk_size=1000
  • $rank_level2=0
  • $order_by_clause=null
)

Sorts a table in chunks

Parameters:

  • $table String

    The name of the table in the database

  • $pts_field String

    The name of the field to rank by.

  • $rank_field String

    The rank field to update in all the rows

  • $chunk_size=1000 Integer

    The number of rows to process at a time. This is so the queries don't tie up the database server for very long, letting it service website requests and other things. Defaults to 1000

  • $rank_level2=0 Integer

    Since the ranking is done in chunks, the function must know which rows have not been processed yet. If this field is empty (default) then the function first sets the rank_field to 0 in all the rows. (That might be a time consuming operation.) Otherwise, if $rank is a nonzero integer, then the function alternates between the ranges 0 to $rank_level2, and $rank_level2 to $rank_level2 * 2. That is, after it is finished, all the ratings will be in one of these two ranges. If not empty, this should be a very large number, like a billion.

  • $order_by_clause=null String

    The order clause to use when calculating ranks. Default "ORDER BY $pts_field DESC"

rawQuery

(
  • $sql
  • $bind=array()
)
Db_Query

Creates a query from raw SQL

Parameters:

  • $sql String

    May contain more than one SQL statement

  • $bind=array() Array

    Optional. An array of parameters to bind to the query, using the Db_Query->bind method.

Returns:

reallyConnect

(
  • [$shardName=null]
)
PDO

Actually makes a connection to the database (by creating a PDO instance)

Parameters:

  • [$shardName=null] Array optional

    A shard name that was added using Db::setShard. This modifies how we connect to the database.

Returns:

PDO:

The PDO object for connection

scriptToQueries

(
  • $script
)
Array

Takes a SQL script and returns an array of queries. When DELIMITER is changed, respects that too.

Parameters:

  • $script String

    The text of the script

Returns:

Array:

An array of the SQL queries.

select

(
  • [$fields='*']
  • [$tables='']
)
Db_Query_Mysql

Creates a query to select fields from a table. Needs to be used with Db_Query::from().

Parameters:

  • [$fields='*'] String | Array optional

    The fields as strings, or array of alias=>field

  • [$tables=''] String | Array optional

    The tables as strings, or array of alias=>table

Returns:

Db_Query_Mysql:

The resulting Db_Query object

setTimezone

(
  • [$offset=timezone_offset_get()]
)

If connected, sets the timezone in the database to match the one in PHP.

Parameters:

  • [$offset=timezone_offset_get()] Integer optional

    in seconds

shardName

() String

Returns the name of the shard with which this Db object was created.

Returns:

String:

timestamp

() Integer

Returns the timestamp the db server would have, based on synchronization

Returns:

Integer:

toDateTime

(
  • $timestamp
)
String

Returns a DateTime string to store in the database

Parameters:

  • $timestamp String

    The UNIX timestamp, e.g. from strtotime function

Returns:

String:

uniqueId

(
  • $table
  • $field
  • $where=array()
  • [$options=array()]
)

Generate an ID that is unique in a table

Parameters:

  • $table String

    The name of the table

  • $field String

    The name of the field to check for uniqueness. You should probably have an index starting with this field.

  • $where=array() Array

    You can indicate conditions here to limit the search for an existing value. The result is an id that is unique within a certain partition.

  • [$options=array()] Array optional

    Optional array used to override default options:

    • [length=8] Integer optional

      The length of the ID to generate, after the prefix.

    • [characters='abcdefghijklmnopqrstuvwxyz'] String optional

      All the characters from which to construct the id

    • [prefix=''] String optional

      The prefix to prepend to the unique id.

    • [filter] Callable optional

      The name of a function that will take the generated string and check it. The filter function can modify the string by returning another string, or simply reject the string by returning false, in which another string will be

update

(
  • $table
)
Db_Query

Creates a query to update rows. Needs to be used with Db_Query::set()

Parameters:

  • $table String

    The table to update

Returns:

Db_Query:

The resulting Db_Query object