[[nsDatabase]]
|
|
———
|
Table of Contents
nsDatabaseThis is MySQL database driver. Currently NickSoft Class Library supports only mysql. MemberstotalTimepublic $totalTime = 0.0; This is total time in seconds of all database queries. It is float value which contains microseconds info too. timeStartpublic $timeStart = 0.0; This is the time since MySQL connection was open. lastQuerypublic $lastQuery; This is the last query executed. dbLinkprotected $dbLink = null; This is MySQL database link resource. It can be set using import method MethodsConstructorpublic function __construct($dbHost,$dbName,$dbUser,$dbPass); Constructor. Sets credentials. Note that connection is not opened on object creation.
connect
public function connect(); import
public function import($link, $dbName = false); Imports a link id resourse. Note that reconnecting will not work if credentials are not correct. selectDb
public function selectDb($dbName); If not successful an exception is thrown. setOptions
public function setOptions($dbFlags); Accepted flags are:
escape
public function escape($str); Escape a string to use as value in MySQL connection. It uses mysql_real_escape_string() and takes connection information in account quoteSynonym for escape foundRows
public function foundRows(); Returns number of rows that would have returned previous SELECT query if there was not LIMIT clause. To work this previous SELECT query must contain SQL_CALC_FOUND_ROWS directive. query
public function query($query); Runs query and returns result resource. In case of error an exception is thrown. queryFirstField
public function queryFirstRow($query); Runs query and returns value of first field of first row of the result. In case of error an exception is thrown. queryFirstRow
public function query($query, $fetchFunc = MYSQL_ASSOC);
Runs query and returns array of values of the first row in the result. If In case of error an exception is thrown. queryRows
public function query($query, $fetchFunc = MYSQL_ASSOC);
Runs query and returns rows of result as array of arrays. If In case of error an exception is thrown. queryAssoc
public function queryAssoc($query, $key, $value);
Returns associative array where key is value of queryAssoc('SELECT id, name FROM groups', 'id', 'name'); queryAssocSimple
public function queryAssocSimple($query); lazy associative array: - first field is the key, second is the value. Good for <select>. example: queryAssocSimple('SELECT id, name FROM groups'); // returns associative array where id field is used for key and name field is used for value listFields
public function listFields($table);
Get info about columns in prepareInsert
public function prepareInsert($fields_arr); The argument is associative array of values with field names as keys.
Formats the values to be used in INSERT query. Complete query will be: INSERT INTO <table> <result> prepareUpdate
public function prepareUpdate($fields_arr); The argument is associative array of values with field names as keys.
Formats the values to be used in UPDATE query. Complete query will be: UPDATE <table> SET <result> WHERE ... insertThis function was renamed from execInsert. execInsert is also available as alias, but it's deprecated, so don't use it and fix it in existing projects.
public function insert($table, $fields_arr); Prepares values with prepareInsert and executes the INSERT query. result is last insert id. updateThis function was renamed from execUpdate. execUpdate is also available as alias, but it's deprecated, so don't use it and fix it in existing projects.
public function execUpdate($table,$fields_arr,$where); Prepares values with prepareUpdate and executes the UPDATE query. result is last insert id. insertUpdateThis function was renamed from execInsertUpdate. execInsertUpdate is also available as alias, but it's deprecated, so don't use it and fix it in existing projects.
public function execInsertUpdate($table, $fields_arr, $key = 'id', $insertId = true); This function executes INSERT … ON DUPLICATE KEY UPDATE query. Sometimes you need different value for insert and update statements. You can do that by making the value array. If you need to to insert 0 and on update to add 1 you can do this: $arr =array( 'id' => 1, 'counter' => array( 'insert' => 0, 'update' => array('counter + 1') // no escaping or quotting ); $db->execInsertUpdate('table', $arr);
So if record with id doesn't exist it'll insert values(1,0). If the record already exists and counter is 2 for example it will update record and increase counter to 3.
When primary key is not autoincrement set simpleUpdate
public function simpleUpdate($table, $fields_arr, $condField = null);
Prepares values with prepareUpdate and executes the UPDATE query. result is last insert id. If escapeField
public static function escapeField($field); if field name is string and consist of letters, numbers and “_” put ` around it. If not - it's a function or constant - return without formatting. startTransaction
public function startTransaction(); Starts a transaction savepoint
public function savepoint($savepoint); Creates a savepoint within a transaction. Note: added in v1.0.1.1-alpha commit
public function commit(); Commits a transaction. releaseSavepoint
public function releaseSavepoint($savepoint); Releases a savepoint within transaction. Note: added in v1.0.1.1-alpha rollback
public function rollback($savepoint = null); Rollback a transaction or savepoint. Changelog:
close
public function close(); Closes the connection to mysql. |