Package base :: Package includes :: Module database_mysqli
[hide private]

Module database_mysqli

source code

Database interface code for MySQL database servers using the mysqli client libraries. mysqli is included in PHP 5 by default and allows developers to use the advanced features of MySQL 4.1.x, 5.0.x and beyond.
See Also:
Drupy Homepage, Drupal Homepage
Notes:

Author: Brendon Crawford

Copyright: 2008 Brendon Crawford

Contact: message144 at users dot sourceforge dot net

Version: 0.1

Functions [hide private]
 
status_report(phase)
Report database status.
source code
 
version()
Returns the version of the database server currently in use.
source code
 
connect(url)
Initialise a database connection.
source code
 
_query(query_, debug=0)
Helper function for db_query().
source code
 
fetch_object(result)
Fetch one result row from the previous query as an object.
source code
 
fetch_array(result)
Fetch one result row from the previous query as an array.
source code
 
result(result)
Return an individual result field from the previous query.
source code
 
error()
Determine whether the previous query caused an error.
source code
 
affected_rows()
Determine the number of rows changed by the preceding query.
source code
 
query_range(query)
Runs a limited-range query in the active database.
source code
 
query_temporary(query)
Runs a SELECT query and stores its results in a temporary table.
source code
 
encode_blob(data)
Returns a properly formatted Binary Large Object value.
source code
 
decode_blob(data)
Returns text from a Binary Large OBject value.
source code
 
lock_table(table)
Lock a table.
source code
 
unlock_tables()
Unlock all locked tables.
source code
 
table_exists(table)
Check if a table exists.
source code
 
column_exists(table, column)
Check if a column exists in the given table.
source code
 
distinct_field(table, field, query)
Wraps the given table.field entry with a DISTINCT().
source code
 
query(query_, *args)
Runs a basic query in the active database.
source code
 
create_table_sql(name, table)
Generate SQL to create a new table from a Drupal schema definition.
source code
 
_create_keys_sql(spec) source code
 
_create_key_sql(fields) source code
 
_process_field(field)
Set database-engine specific properties for a field.
source code
 
_create_field_sql(name, spec)
Create an SQL string for a field to be used in table creation or alteration.
source code
 
type_map()
This maps a generic data type in combination with its data size to the engine-specific data type.
source code
 
rename_table(ret, table, new_name)
Rename a table.
source code
 
drop_table(ret, table)
Drop a table.
source code
 
add_field(ret, table, field, spec, keys_new=[])
Add a new field to a table.
source code
 
drop_field(ret, table, field)
Drop a field.
source code
 
field_set_default(ret, table, field, default)
Set the default value for a field.
source code
 
field_set_no_default(ret, table, field)
Set a field to have no default value.
source code
 
add_primary_key(ret, table, fields)
Add a primary key.
source code
 
drop_primary_key(ret, table)
Drop the primary key.
source code
 
add_unique_key(ret, table, name, fields)
Add a unique key.
source code
 
drop_unique_key(ret, table, name)
Drop a unique key.
source code
 
add_index(ret, table, name, fields)
Add an index.
source code
 
drop_index(ret, table, name)
Drop an index.
source code
 
change_field(ret, table, field, field_new, spec, keys_new=[])
Change a field definition.
source code
 
last_insert_id(table, field)
Returns the last insert id.
source code
 
escape_string(data)
Wrapper to escape a string
source code
 
fetch_assoc(result)
Fetch one result row from the previous query as an array.
source code
Variables [hide private]
  __version__ = '$Revision: 1 $'
Function Details [hide private]

version()

source code 
Returns the version of the database server currently in use.
Returns:
Database server version

connect(url)

source code 
Initialise a database connection. Note that mysqli does not support persistent connections.

fetch_object(result)

source code 
Fetch one result row from the previous query as an object.
Parameters:
  • result - A database query result resource, as returned from db_query().
Returns:
An object representing the next row of the result, or False. The attributes of this object are the table fields selected by the query.

fetch_array(result)

source code 
Fetch one result row from the previous query as an array.
Parameters:
  • result - A database query result resource, as returned from db_query().
Returns:
An associative array representing the next row of the result, or False. The keys of this object are the names of the table fields selected by the query, and the values are the field values for this result row.

result(result)

source code 
Return an individual result field from the previous query. Only use this function if exactly one field is being selected; otherwise, use db_fetch_object() or db_fetch_array().
Parameters:
  • result - A database query result resource, as returned from db_query().
Returns:
The resulting field or False.

query_range(query)

source code 
Runs a limited-range query in the active database. Use this as a substitute for db_query() when a subset of the query is to be returned. User-supplied arguments to the query should be passed in as separate parameters so that they can be properly escaped to avoid SQL injection attacks.
Parameters:
  • query - A string containing an SQL query.
  • ... - A variable number of arguments which are substituted into the query using printf() syntax. The query arguments can be enclosed in one array instead. Valid %-modifiers are: %s, %d, %f, %b (binary data, do not enclose in '') and %%. NOTE: using this syntax will cast None and False values to decimal 0, and True values to decimal 1.
  • from - The first result row to return.
  • count - The maximum number of result rows to return.
Returns:
A database query result resource, or False if the query was not executed correctly.

query_temporary(query)

source code 
Runs a SELECT query and stores its results in a temporary table. Use this as a substitute for db_query() when the results need to stored in a temporary table. Temporary tables exist for the duration of the page request. User-supplied arguments to the query should be passed in as separate parameters so that they can be properly escaped to avoid SQL injection attacks. Note that if you need to know how many results were returned, you should do a SELECT COUNT(*) on the temporary table afterwards. db_affected_rows() does not give consistent result across different database types in this case.
Parameters:
  • query - A string containing a normal SELECT SQL query.
  • ... - A variable number of arguments which are substituted into the query using printf() syntax. The query arguments can be enclosed in one array instead. Valid %-modifiers are: %s, %d, %f, %b (binary data, do not enclose in '') and %%. NOTE: using this syntax will cast None and False values to decimal 0, and True values to decimal 1.
  • table - The name of the temporary table to select into. This name will not be prefixed as there is no risk of collision.
Returns:
A database query result resource, or False if the query was not executed correctly.

encode_blob(data)

source code 
Returns a properly formatted Binary Large Object value.
Parameters:
  • data - Data to encode.
Returns:
Encoded data.

decode_blob(data)

source code 
Returns text from a Binary Large OBject value.
Parameters:
  • data - Data to decode.
Returns:
Decoded data.

distinct_field(table, field, query)

source code 
Wraps the given table.field entry with a DISTINCT(). The wrapper is added to the SELECT list entry of the given query and the resulting query is returned. This function only applies the wrapper if a DISTINCT doesn't already exist in the query.
Parameters:
  • table - Table containing the field to set as DISTINCT
  • field - Field to set as DISTINCT
  • query - Query to apply the wrapper to
Returns:
SQL query with the DISTINCT wrapper surrounding the given table.field.

query(query_, *args)

source code 
Runs a basic query in the active database. User-supplied arguments to the query should be passed in as separate parameters so that they can be properly escaped to avoid SQL injection attacks.
Parameters:
  • query - A string containing an SQL query.
  • ... - A variable number of arguments which are substituted into the query using printf() syntax. Instead of a variable number of query arguments, you may also pass a single array containing the query arguments. Valid %-modifiers are: %s, %d, %f, %b (binary data, do not enclose in '') and %%. NOTE: using this syntax will cast None and False values to decimal 0, and True values to decimal 1.
Returns:
A database query result resource, or False if the query was not executed correctly.

create_table_sql(name, table)

source code 
Generate SQL to create a new table from a Drupal schema definition.
Parameters:
  • name - The name of the table to create.
  • table - A Schema API table definition array.
Returns:
An array of SQL statements to create the table.

_process_field(field)

source code 
Set database-engine specific properties for a field.
Parameters:
  • field - A field description array, as specified in the schema documentation.

_create_field_sql(name, spec)

source code 
Create an SQL string for a field to be used in table creation or alteration. Before passing a field out of a schema definition into this function it has to be processed by _db_process_field().
Parameters:
  • name - Name of the field.
  • spec - The field specification, as per the schema data structure format.

rename_table(ret, table, new_name)

source code 
Rename a table.
Parameters:
  • ret - Array to which query results will be added.
  • table - The table to be renamed.
  • new_name - The new name for the table.

drop_table(ret, table)

source code 
Drop a table.
Parameters:
  • ret - Array to which query results will be added.
  • table - The table to be dropped.

add_field(ret, table, field, spec, keys_new=[])

source code 
Add a new field to a table.
Parameters:
  • ret - Array to which query results will be added.
  • table - Name of the table to be altered.
  • field - Name of the field to be added.
  • spec - The field specification array, as taken from a schema definition. The specification may also contain the key 'initial', the newly created field will be set to the value of the key in all rows. This is most useful for creating NOT None columns with no default value in existing tables.
  • keys_new - Optional keys and indexes specification to be created on the table along with adding the field. The format is the same as a table specification but without the 'fields' element. If you are adding a type 'serial' field, you MUST specify at least one key or index including it in this array. @see db_change_field for more explanation why.

drop_field(ret, table, field)

source code 
Drop a field.
Parameters:
  • ret - Array to which query results will be added.
  • table - The table to be altered.
  • field - The field to be dropped.

field_set_default(ret, table, field, default)

source code 
Set the default value for a field.
Parameters:
  • ret - Array to which query results will be added.
  • table - The table to be altered.
  • field - The field to be altered.
  • default - Default value to be set. None for 'default None'.

field_set_no_default(ret, table, field)

source code 
Set a field to have no default value.
Parameters:
  • ret - Array to which query results will be added.
  • table - The table to be altered.
  • field - The field to be altered.

add_primary_key(ret, table, fields)

source code 
Add a primary key.
Parameters:
  • ret - Array to which query results will be added.
  • table - The table to be altered.
  • fields - Fields for the primary key.

drop_primary_key(ret, table)

source code 
Drop the primary key.
Parameters:
  • ret - Array to which query results will be added.
  • table - The table to be altered.

add_unique_key(ret, table, name, fields)

source code 
Add a unique key.
Parameters:
  • ret - Array to which query results will be added.
  • table - The table to be altered.
  • name - The name of the key.
  • fields - An array of field names.

drop_unique_key(ret, table, name)

source code 
Drop a unique key.
Parameters:
  • ret - Array to which query results will be added.
  • table - The table to be altered.
  • name - The name of the key.

add_index(ret, table, name, fields)

source code 
Add an index.
Parameters:
  • ret - Array to which query results will be added.
  • table - The table to be altered.
  • name - The name of the index.
  • fields - An array of field names.

drop_index(ret, table, name)

source code 
Drop an index.
Parameters:
  • ret - Array to which query results will be added.
  • table - The table to be altered.
  • name - The name of the index.

change_field(ret, table, field, field_new, spec, keys_new=[])

source code 
Change a field definition. IMPORTANT NOTE: To maintain database portability, you have to explicitly recreate all indices and primary keys that are using the changed field. That means that you have to drop all affected keys and indexes with db_drop_{primary_key,unique_key,index}() before calling db_change_field(). To recreate the keys and indices, pass the key definitions as the optional keys_new argument directly to db_change_field(). For example, suppose you have:
Parameters:
  • ret - Array to which query results will be added.
  • table - Name of the table.
  • field - Name of the field to change.
  • field_new - New name for the field (set to the same as field if you don't want to change the name).
  • spec - The field specification for the new field.
  • keys_new - Optional keys and indexes specification to be created on the table along with changing the field. The format is the same as a table specification but without the 'fields' element.

last_insert_id(table, field)

source code 
Returns the last insert id.
Parameters:
  • table - The name of the table you inserted into.
  • field - The name of the autoincrement field.

fetch_assoc(result)

source code 
Fetch one result row from the previous query as an array.
Parameters:
  • result - A database query result resource, as returned from db_query().
Returns:
An associative array representing the next row of the result, or False. The keys of this object are the names of the table fields selected by the query, and the values are the field values for this result row.