Differences

This shows you the differences between the selected revision and the current version of the page.

punbb13:database_helpers 2010/04/09 07:41 punbb13:database_helpers 2020/02/06 11:04 current
Line 1: Line 1:
-====== Database helpers ======+====== PunBB 1.3 database helpers ======
PunBB has an implementation of the database layer class. It takes into account the forum configuration and works with different databases. PunBB has an implementation of the database layer class. It takes into account the forum configuration and works with different databases.
On ''include/common.php'' inclusion an instance of this database layer named ''$forum_db'' is being created in global scope to provide database helpers. On ''include/common.php'' inclusion an instance of this database layer named ''$forum_db'' is being created in global scope to provide database helpers.
-==== Performing a query ====+Please remember the following when use PunBB database layer: 
 +  * Potentially harmful data is escaped using ''$forum_db->escape()''. Expected integer values should be forced into integer form using ''intval()'' before being used in a query. 
 +  * Use the SQL 2003 style [[http://en.wikipedia.org/wiki/Join_(SQL)#Inner_join|explicit join notation]]. 
 +  * Database helper functions have an optional parameter ''$no_prefix''. The prefix of table names is being selected during the forum installation and is available in the database layer class (''$forum_db->prefix''). The database layer is able to add the prefix to table names automatically (the default behavior). It's recommended not to change the default value of ''$no_prefix''. 
 + 
 +===== Performing a query =====
  * **Direct query** (not recommended). You can simply write an SQL-statement and execute it.   * **Direct query** (not recommended). You can simply write an SQL-statement and execute it.
-<code php> $result = $forum_db->query('SELECT * FROM topics WHERE id = 10');</code>+<code php> $result = $forum_db->query('SELECT * FROM '.$forum_db->prefix.'topics WHERE id = 10');</code>
Be sure, that your SQL code is cross-compatible with all database engines supported by PunBB. Be sure, that your SQL code is cross-compatible with all database engines supported by PunBB.
-  * **Using [[query builder]]** (recommended). You may transparently build database queries. All the specific of database engines and database structure will automatically be taken in account. Example of usage:+  * **Using query builder** (recommended). You may transparently build database queries. All the specific of database engines and database structure will automatically be taken in account. Example of usage:
<code php>$query = array( <code php>$query = array(
  'SELECT'  => '*',   'SELECT'  => '*',
Line 15: Line 20:
$result = $forum_db->query_build($query); $result = $forum_db->query_build($query);
</code> </code>
-See [[query builder]] page for details. 
-===== Data Manipulation with query builder ======+Let's discuss how to use the query builder.
-First consider the DML (Data Manipulation Language) part of the PunBB database layer class.+===== Query Builder: Data Manipulation ======
==== SELECT ==== ==== SELECT ====
Line 130: Line 134:
$forum_db->query_build($query) or error(__FILE__, __LINE__);</code> $forum_db->query_build($query) or error(__FILE__, __LINE__);</code>
-===== Data Definition with query builder ======+===== Data Definition ======
Now let's consider the DDL (Data Definition Language) part of the PunBB database layer class. Now let's consider the DDL (Data Definition Language) part of the PunBB database layer class.
-==== Creating a table ====+==== Tables ==== 
 + 
 +<code php>function table_exists($table_name, $no_prefix = false)</code> 
 + 
 +This function returns ''true'' if the table with ''$table_name'' name exists and ''false'' otherwise. 
 + 
 +<code php>function create_table($table_name, $schema, $no_prefix = false)</code> 
 + 
 +Takes either two or three parameters and creates a database table. The first parameter is the name of the table to be added. The second is an array of schema for the table itself (see the explanation and example below). The third is the optional parameter ''$no_prefix''. 
 + 
 +<code php>function drop_table($table_name, $no_prefix = false)</code> 
 + 
 +Deletes the table that has the ''$table_name'' name. 
 + 
 +=== Creating a table ===
==Table structure== ==Table structure==
Line 180: Line 198:
<PHP>$forum_db->create_table('categories', $schema);</PHP> <PHP>$forum_db->create_table('categories', $schema);</PHP>
-The ''create_table()'' function takes either two or three parameters. The first parameter is the name of the table to be added. The second is an array of schema for the table itself. The third parameter, which is optional, specifies whether or not to add the database prefix to the table name. If the third parameter is not specified or is set to false, the prefix will be prepended to the table name. If it is specified as true, the prefix will not be prepended. +==== Fields ==== 
 + 
 +<code php>function field_exists($table_name, $field_name, $no_prefix = false)</code> 
 + 
 +Checks if the field ''$field_name'' is present in the table ''$table_name''. 
 + 
 +<code php>function add_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false)</code> 
 + 
 +Adds a field to the ''$table_name'' table. One can specify the name of an existent field, ''$after_field'', to insert after it the field to be added. The meaning of the other parameters is clear from its names. 
 + 
 +<code php>function alter_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false)</code> 
 + 
 +Modifies the ''$field_name'' field. The meaning of parameters is the same as above. 
 + 
 +<code php>function drop_field($table_name, $field_name, $no_prefix = false)</code> 
 + 
 +Removes the ''$field_name'' field from the ''$table_name'' table. 
 + 
 +Here are examples of the fields manipulation. 
 + 
 +<code php>// Taken from admin/db_update.php 
 +$forum_db->alter_field('posts', 'poster_ip', 'VARCHAR(39)', true); 
 +$forum_db->alter_field('users', 'registration_ip', 'VARCHAR(39)', false, '0.0.0.0');</code> 
 + 
 +<code php>// Taken from the pun_pm extension 
 +// Install section 
 +if (!$forum_db->field_exists('users', 'pun_pm_new_messages')) 
 +  $forum_db->add_field('users', 'pun_pm_new_messages', 'INT(10)', true); 
 + 
 +// Uninstall section 
 +$forum_db->drop_field('users', 'pun_pm_new_messages');</code> 
 + 
 +==== Indexes ==== 
 + 
 +<code php>function index_exists($table_name, $index_name, $no_prefix = false)</code> 
 + 
 +Checks if the index ''$index_name'' is present in the table ''$table_name''. 
 + 
 +<code php>function add_index($table_name, $index_name, $index_fields, $unique = false, $no_prefix = false)</code> 
 + 
 +Adds an index to the ''$table_name'' table. ''$index_fields'' is the array of fields to be added to the index. 
 + 
 +<code php>function drop_index($table_name, $index_name, $no_prefix = false)</code> 
 + 
 +Removes the ''$index_name'' index from the ''$table_name'' table. 
 + 
 +Here are examples of the indexes manipulation. 
 + 
 +<code php>// Taken from admin/db_update.php 
 +if (!$forum_db->index_exists('online', 'user_id_ident_idx')) 
 +
 +  ... 
 +  $forum_db->add_index('online', 'user_id_ident_idx', array('user_id', 'ident'), true); 
 +  ... 
 +
 + 
 +$forum_db->drop_index('online', 'user_id_idx');</code>

Personal Tools