/program/lib/database/databaselib.php - database factory and database access routines
manufacture a database object
This loads (includes) a specific database access class based on the parameter $db_type. Currently 'mysql' is the only option, but support for PostgreSQL or other databases could be added in the future, see the code that is commented out
Because Website@School is not meant to be an 'enterprisy application', I decided against using an abstract class that would be extended by a specific driver class which would be instantiated via yet another factory type class; I'd like to keep this as simple as possible while retaining the necessary flexibility (and the option to add support for other databases). Toolkits like Adodb seem overkill for this application program.
This routine is called at a fairly early stage in the process. It does not rely on any regular libraries which may be include()'ed lateron. If no valid database type is specified, the function returns FALSE, otherwise a database object is returned.
Note that I did not use a singleton because I think that that pattern is simply a fancy word for a global variable. YMMV.
Note: This file can be safely included from the install.php script, allowing for database-manipulations via this abstraction layer rather than directly going to the database. There are no dependencies on other include()'s other than the actual database class files such as mysql.class.php. Also, this file does not rely on the global variable $CFG, which is also very convenient in the installer (where no CFG is available).
check boolean field in a database-independent way
Various databases have different ways to indicate TRUE or FALSE in boolean type of fields. MySQL uses a tinyint(1) with values NULL, 0 and 1. PostgreSQL uses a lowercase 't' or 'f' etc. We already have two database-specific definitions for TRUE and FALSE: SQL_TRUE and SQL_FALSE. This routine 'converts' the database-specific boolean values back to a form that is useable in PHP. This routine is able to test for either TRUE or FALSE. Any other value is returned as NULL.
Typical use:
$user = db_select_single_record('users','is_active','user_id = 13'); if (db_bool_is(TRUE,$user['is_active'])) { ... }
delete zero or more rows in a table
generate SQL to delete zero or more rows in a table
retrieve the latest database error from $DB
conditionally quote and escape values for use with a database table
If $value is a string, it is escaped and single quotes are added at begin and end. If $value is a boolean, it is converted into the correct value for the database using SQL_FALSE/SQL_TRUE If $value is NULL, it is converted into the string 'NULL' (without quotes) Otherwise the value is not changed.
execute the necessary SQL-code for an INSERT INTO statement
This excutes the SQL-statement created by db_insert_into_sql().
execute the necessary SQL-code for an INSERT INTO statement and return the last_insert_id
This excutes the SQL-statement created by db_insert_into_sql(). If all goes well, the value of the last inserted id is returned.
generate the necessary SQL-code for an INSERT INTO statement
Construct an SQL-statement that inserts data into the speficied table. This routine takes care of properly escaping strings and also handles the addition of the table prefix
wrapper for DB->last_insert_id()
This calls $DB->last_insert_id() in a way that should be compatible with a future PostgreSQL database class. Note that MySQL doesn't care about this. You can get away with leaving table and field parameters empty (as is the default), but for compatibility and documentation purposes you should use the correct values.
Typical use: db_insert_into('users',$fields_array); $user_id = db_last_insert_id('users','user_id');
fetch all selected records from the database in one array
fetch a single record from the database
generate the necessary SQL-code for a simple SELECT statement
Construct an SQL-statement of the form: SELECT field_list FROM table WHERE where_expression ORDER BY orderby_list
The parameter $fields can be either a simple string, indicating a single field or an array when more fields are to be selected
The optional parameter $where is either a simple string with an appropriate expression (without the keyword WHERE) or an array with fieldname/value-pairs. In the latter case the clauses fieldname=value are AND'ed together. If the specified values are string-like, they are properly quoted. Boolean values are treated properly too.
The optional parameter $order is either a simple string with an appropriate list or expression (without the keyword ORDER BY) or an array with fieldnames which will be used to create a comma-delimited string.
Examples:
update one or more fields in a table
generate sql to update one or more fields in a table
construct a where clause from string/array, including the word WHERE
this constructs a where clause including the word 'WHERE' based on the string or array $where.
The optional parameter $where is either a simple string with an appropriate expression (without the keyword WHERE) or an array with fieldname/value-pairs. In the latter case the clauses fieldname=value are AND'ed together. If the specified values are string-like, they are properly quoted. Boolean values are treated properly too. NULL-values yield a standard 'IS NULL' type of expression.
Documentation generated on Tue, 28 Jun 2016 19:09:03 +0200 by phpDocumentor 1.4.0