Db_Interface Class
Interface class for database connection. An adapter must support it to implement the Db class.
Constructor
Db_Interface
-
$conn_name
-
$pdo
Parameters:
-
$conn_name
StringThe name of the connection
-
$pdo
PDO
Item Index
Methods
__call
-
$name
-
$arguments
Forwards all other calls to the PDO object
Parameters:
-
$name
StringThe function name
-
$arguments
ArrayThe arguments
codeForModelBaseClass
-
$table
-
$directory
-
$classname_prefix=''
-
&$class_name=null
-
$prefix=null
Generates a base class for the model
Parameters:
-
$table
StringThe name of the table to generate the code for.
-
$directory
StringThe path of the directory in which to place the model code.
-
$classname_prefix=''
StringPrefix for class name
-
&$class_name=null
StringIf 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:
The generated code for the class.
connection
()
String
Returns the connection with which this Db object was created.
Returns:
connectionName
()
String
Returns the name of the connection with which this Db object was created.
Returns:
dbms
()
String
Returns the lowercase name of the dbms (e.g. "mysql")
Returns:
dbName
()
String
Returns the name of the database used
Returns:
delete
-
$table_from
-
$table_using=null
Creates a query to delete rows.
Parameters:
-
$table_from
StringThe table to delete from
-
$table_using=null
String
Returns:
dsn
()
Array
Returns an associative array representing the dsn
Returns:
fromDateTime
-
$syntax
-
$datetime
Returns a timestamp from a DateTime string
Parameters:
-
$syntax
StringThe format of the date string, see date() function.
-
$datetime
StringThe DateTime string that comes from the db
Returns:
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
StringThe name of a previously registered connection.
-
$directory
StringThe 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
StringThe 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()
Creates a query to insert a row into a table
Parameters:
-
$table_into
StringThe name of the table to insert into
-
$fields=array()
ArrayThe fields as an array of column=>value pairs
Returns:
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
StringThe name of the table to insert into
-
[$rows=array()]
Array optionalThe 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 optionalAn associative array of options, including:
-
[className]
String optionalIf you provide the class name, the system will be able to use any sharding indexes under that class name in the config.
-
[chunkSize]
Integer optionalThe 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 optionalYou 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
StringThe name of the table in the database
-
$pts_field
StringThe name of the field to rank by.
-
$rank_field
StringThe rank field to update in all the rows
-
$chunk_size=1000
IntegerThe 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
IntegerSince 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
StringThe order clause to use when calculating ranks. Default "ORDER BY $pts_field DESC"
rawQuery
-
$sql
-
$bind=array()
Creates a query from raw SQL
Parameters:
-
$sql
StringMay contain more than one SQL statement
-
$bind=array()
ArrayOptional. An array of parameters to bind to the query, using the Db_Query->bind method.
Returns:
reallyConnect
-
[$shardName=null]
Actually makes a connection to the database (by creating a PDO instance)
Parameters:
-
[$shardName=null]
Array optionalA shard name that was added using Db::setShard. This modifies how we connect to the database.
Returns:
The PDO object for connection
scriptToQueries
-
$script
Takes a SQL script and returns an array of queries. When DELIMITER is changed, respects that too.
Parameters:
-
$script
StringThe text of the script
Returns:
An array of the SQL queries.
select
-
[$fields='*']
-
[$tables='']
Creates a query to select fields from a table. Needs to be used with Db_Query::from().
Parameters:
-
[$fields='*']
String | Array optionalThe fields as strings, or array of alias=>field
-
[$tables='']
String | Array optionalThe tables as strings, or array of alias=>table
Returns:
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 optionalin seconds
shardName
()
String
Returns the name of the shard with which this Db object was created.
Returns:
timestamp
()
Integer
Returns the timestamp the db server would have, based on synchronization
Returns:
toDateTime
-
$timestamp
Returns a DateTime string to store in the database
Parameters:
-
$timestamp
StringThe UNIX timestamp, e.g. from strtotime function
Returns:
uniqueId
-
$table
-
$field
-
$where=array()
-
[$options=array()]
Generate an ID that is unique in a table
Parameters:
-
$table
StringThe name of the table
-
$field
StringThe name of the field to check for uniqueness. You should probably have an index starting with this field.
-
$where=array()
ArrayYou 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 optionalOptional array used to override default options:
-
[length=8]
Integer optionalThe length of the ID to generate, after the prefix.
-
[characters='abcdefghijklmnopqrstuvwxyz']
String optionalAll the characters from which to construct the id
-
[prefix='']
String optionalThe prefix to prepend to the unique id.
-
[filter]
Callable optionalThe 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
-