Db_Mysql Class
This class lets you create and use PDO database connections.
Constructor
Db_Mysql
-
$connectionName
-
[$pdo=null]
Parameters:
-
$connectionName
StringThe name of the connection out of the connections added with Db::setConnection() This is required for actually connecting to the database.
-
[$pdo=null]
PDO optionalExisting PDO connection. Only accepts connections to MySQL.
Item Index
Methods
Properties
Events
Methods
__call
-
$name
-
$arguments
Forwards all other calls to the PDO object
Parameters:
-
$name
StringThe function name
-
$arguments
ArrayThe arguments
Returns:
The result of method call
codeForModelBaseClass
-
$table
-
$directory
-
[$classname_prefix='']
-
[$class_name_base=null]
-
[$prefix=null]
-
[$js_code=null]
-
[$table_comment='']
Generates code for 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='']
String optionalThe prefix to prepend to the generated class names
-
[$class_name_base=null]
&string optionalIf 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, without the $classname_prefix
-
[$prefix=null]
String optionalDefaults to the prefix of the tables, as specified in the connection. Pass null here to use the default, or a string to override it.
-
[$js_code=null]
&string optionalThe javascript code for the base class
-
[$table_comment='']
&string optionalThe comment from the MySQL table if any
Returns:
The generated code for the class.
connection
()
String
Returns the connection info 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 optionalIf set, adds a USING clause with this table. You can then use ->join() with the resulting Db_Query.
Returns:
dsn
()
Array
Returns an associative array representing the dsn
Returns:
fromDate
-
$datetime
Returns a timestamp from a Date string
Parameters:
-
$datetime
StringThe Date string that comes from the db
Returns:
The timestamp
fromDateTime
-
$datetime
Returns a timestamp from a DateTime string
Parameters:
-
$datetime
StringThe DateTime string that comes from the db
Returns:
The timestamp
generateModels
-
$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:
-
$directory
StringThe directory in which to generate the files. If the files already exist, they are not overwritten, unless they are inside the "Base" subdirectory. If the "Base" subdirectory does not exist, it is created.
-
[$classname_prefix=null]
String optionalThe prefix to prepend to the Base class names. If not specified, prefix becomes "connectionName_", where connectionName is the name of the connection.
Returns:
$filenames The array of filenames for files that were saved.
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_Mysql 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
-
[$start=1]
-
[$chunk_size=1000]
-
[$rank_level2=0]
-
[$order_by]
-
[$where=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
-
[$start=1]
Integer optionalThe value of the first rank
-
[$chunk_size=1000]
Integer optionalThe number of rows to process at a time. Default is 1000. This is so the queries don't tie up the database server for very long, letting it service website requests and other things.
-
[$rank_level2=0]
Integer optionalSince 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 sets the rank_field to 0 in all the rows, before starting the ranking process. (That might be a time consuming operation.) Otherwise, if $rank is a nonzero integer, then the function alternates between the ranges $start to $rank_level2, and $rank_level2 + $start 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]
Array optionalThe order clause to use when calculating ranks. Default is array($pts_field, false)
-
[$where=null]
Array optionalAny additional criteria to filter the table by. The ranking algorithm will do its work within the results that match this criteria. If your table is sharded, then all the work must be done within one shard.
rawQuery
-
$sql
-
[$bind=array()]
Creates a query from raw SQL
Parameters:
-
$sql
String | NullMay contain one or more SQL statements. Pass null here for an empty query that you can add other clauses to, e.g. ->commit().
-
[$bind=array()]
Array optionalAn array of parameters to bind to the query, using the Db_Query_Mysql->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
-
[$callback=null]
Takes a MySQL script and returns an array of queries. When DELIMITER is changed, respects that too.
Parameters:
-
$script
StringThe text of the script
-
[$callback=null]
Callable optionalOptional callback to call for each query.
Returns:
An array of the SQL queries.
scriptToQueries_internal
-
$script
-
[$callback=null]
Takes stripped MySQL script and returns an array of queries. When DELIMITER is changed, respects that too.
Parameters:
-
$script
StringThe text of the script
-
[$callback=null]
Callable optionalOptional callback to call for each query.
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 "*", 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()]
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 currently selected with reallyConnect, if any
Returns:
timestamp
()
Integer
Returns the timestamp the db server would have, based on synchronization
Returns:
toDate
-
$timestamp
Returns a Date string to store in the database
Parameters:
-
$timestamp
StringThe UNIX timestamp, e.g. from a strtotime function
Returns:
toDateTime
-
$timestamp
Returns a DateTime string to store in the database
Parameters:
-
$timestamp
StringThe UNIX timestamp, e.g. from a 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()]
Array optionalYou 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
-
update
-
$table
Creates a query to update rows. Needs to be used with {@link Db_Query::set}
Parameters:
-
$table
StringThe table to update
Returns:
The resulting Db_Query object
update
-
$criteria
Creates a query to rollback a previously started transaction.
Parameters:
-
$criteria
ArrayThe criteria to use, for sharding
Returns:
The resulting Db_Query object
Properties
$connectionName
String
protected
The name of the connection
$dbname
String
The database name of the shard currently selected with reallyConnect, if any
$maxCheckStrlen
String
The cutoff after which strlen gets too expensive to check automatically
$pdo
PDO
The PDO connection that this object uses
$prefix
String
The prefix of the shard currently selected with reallyConnect, if any
$shardName
String
protected
The name of the shard currently selected with reallyConnect, if any