Public Method Details |
&getDbObject |
|
public object [unknown] &getDbObject( [ string $dsn ] )
|
| |
function to get a db object for a given dsn.
note: this is a function, not a method of a db object.if $dsn is not given or set to null, a reference to the global shared db objectwill be returned. if it does not exist, this function tries to open the connectionfor it.example:$dsn = array('host'=>'localhost', 'port'=>3306, 'user'=>'foo', 'pass'=>'bar', 'syntax'=>'mysql', 'type'=>'mysql');$db = &getDbObject($dsn);^^ important!!!^
|
| Parameter |
|
| string |
$dsn |
= >>null<< |
|
(a hash, see Bs_Form->dbDsn for a description) |
|
| Returns |
object [unknown] (instance of a subclassed db class) |
| Throws |
bs_exception |
| Copyright |
blueshoes.org |
| Author(s) |
, |
|
Bs_Db |
|
public void Bs_Db( )
|
| |
constructor.
|
| Returns |
void |
|
&getDsn |
|
public mixed &getDsn( [ string $key ] )
|
| |
Return the _dsnInfo-hash.
|
| Parameter |
|
| string |
$key |
= >>NULL<< |
|
if specified then only the value for the given key will be returned. |
|
| Returns |
mixed (&hash return the _dsnInfo-hash or mixed, depending on param $key.) |
|
provides |
|
public bool provides( $feature )
|
| |
Tell whether a DB implementation or its backend extension supports a given feature.
currently, these features are reported (see constructor of subclassing class, like Bs_MySql)'prepare', 'pconnect', 'transactions', 'innerSelects', 'insertId', 'affectedRows','numRows', 'numCols'
|
| Parameter |
|
|
$feature |
|
|
Warning: documentation is missing. |
|
| Returns |
bool whether this DB implementation supports $feature (or NULL if $feature not known). |
|
getFieldValue |
|
public mixed getFieldValue( string $theTable, string $fieldToSearch, string $valueToSearch, mixed $fieldToGet, [ string $caseSensitive ] )
|
| |
Returns 1-n db-field values.
Example I:$birthday = getFieldValue($theTable='employees', $fieldToSearch='username', $valueToSearch='Hugentobler', $fieldToGet='birthday')Example II:list($birthday, $phone) = getFieldValue('employees', 'username', 'Hugentobler', array('birthday', 'phone'))NOTE: Only the first found record is used.
|
| Parameter |
|
| string |
$theTable |
|
|
The db-table to search. Can also be 'database.table'. |
|
|
| string |
$fieldToSearch |
|
|
The field to compare. |
|
|
| string |
$valueToSearch |
|
|
The value to compare. |
|
|
| mixed |
$fieldToGet |
|
|
The field name as string of which we want the value, or an array with field names. |
|
|
| string |
$caseSensitive |
= >>TRUE<< |
|
(opt) if the compare in the db should be made case sensitive or no. default is TRUE. |
|
| Returns |
mixed The desired field value, whatever that is. If param $fieldToGet is an array, an array is returned here. |
| Throws |
bs_exception |
|
read |
|
public void read( string $query )
|
| |
Issue a select query and return a resource (or statement for oci).
|
| Parameter |
|
| string |
$query |
|
|
A read only SQL statement. |
|
| Returns |
void |
| Throws |
Bs_Exception |
| See Also |
countRead() |
|
&rsRead |
|
public object an &rsRead( string $query )
|
| |
Issue a select query and return an instance of Bs_ResultSet.
|
| Parameter |
|
| string |
$query |
|
|
A read only SQL statement. |
|
| Returns |
object an instance of Bs_ResultSet |
| Throws |
Bs_Exception |
| See Also |
read(), countRead() |
|
countRead |
|
public int countRead( string $query )
|
| |
Issue a select query and return the number of 'affected' rows.
This method makes use of this->numRows().
|
| Parameter |
|
| string |
$query |
|
|
A SQL statement. |
|
| Returns |
int number of rows matched (0-x) |
| Throws |
Bs_Exception |
| See Also |
read() |
|
write |
|
public bool write( string $query )
|
| |
Issue a write query (insert, update, delete, replace, drop, alter, create).
|
| Parameter |
|
| string |
$query |
|
|
A modifing SQL statement. |
|
| Returns |
bool true on success, otherwise throws an exception. |
| Throws |
Bs_Exception |
| See Also |
countWrite(), idWrite() |
|
countWrite |
|
public int countWrite( string $query )
|
| |
Issue a write query (update, delete, replace) and return the number of affected rows.
this method makes use of this->affectedRows() read the doc of it!
|
| Parameter |
|
| string |
$query |
|
|
A modifing SQL statement. |
|
| Returns |
int number of rows affected |
| Throws |
Bs_Exception if the query was unsuccessful, FALSE if the count with affectedRows() failed. |
| See Also |
write(), idWrite(), affectedRows() |
|
idWrite |
|
public int idWrite( string $query )
|
| |
Issue an insert query on a table with an auto_increment field and return the newly inserted id.
this method makes use of this->insertId() read the doc of it!
|
| Parameter |
|
| string |
$query |
|
|
A SQL statement that inserts a record. |
|
| Returns |
int >=1 |
| Throws |
Bs_Exception if the query failed, FALSE if the operation was not an insert. |
| See Also |
countWrite(), write(), insertId() |
|
&getOne |
|
public mixed &getOne( mixed $source, [ integer $row, mixed $col ] )
|
| |
Fetch a single value from a data amount given by the param $source.
$source can be either a SQL query string or a result set.IF param $source is a query-string:Execute the query, fetch value and clean up (free the results set).ELSEIF param $source is a result set:Move down $row-rows, fetch value at $col and leave DB curser*after* offset rows. Don't free result set.A row and col offset can be given. Top left cell is row=0 / col=0.The default behavior is to fetch cell [0,0] (top left cell).NOTE: col may be given as col-name (string) instead of offset (int). It will beused as key to identify the right column. So pass the right type!Sample: Given is the table "mytable" containing:\/ID TEXT DATE--------------------------------> 1 'one' 09.05.19602 'two' 25.12.19703 'three' 31.01.1980getOne('SELECT id, teXt, date as birthday FROM mytable'); // would return '1'.getOne($query, 1, 2); // would return '25.12.1970'.getOne($query, 2, 'teXt'); // would return 'two'.
|
| Parameter |
|
| mixed |
$source |
|
|
can be an SQL query as string, or a result set (a resource). |
|
|
| integer |
$row |
= >>0<< |
|
the row number |
|
|
| mixed |
$col |
= >>0<< |
|
which column to return (integer [column number, starting at 0] or string [column name]) |
|
| Returns |
mixed just one value or NULL if row or col are out of bounds or don't exsist. |
| Throws |
Bs_Exception on error |
|
&getRow |
|
public mixed &getRow( mixed $source, [ integer $row, string $fetchMode ] )
|
| |
Fetch a full row of data as vector or hash given by the param $source.
$source can be either a SQL query string or a result set.If fetchMode is BS_DB_FETCHMODE_ASSOC (default) returns a hash array that correspondsto the fetched row else if it's BS_DB_FETCHMODE_ORDERED returns a vector starting at offset 0.IF param $source is a query-string:Execute the query, fetch row and clean up (free the results set).ELSEIF param $source is a result set:Move down $row-rows, fetch row and leave DB curser*after* offset rows. Don't free result set.The default behaver is to fetch current row.Sample: Given is the table "mytable" containing:ID TEXT DATE--------------------------------> 1 'one' 09.05.1960 <-2 'two' 25.12.19703 'three' 31.01.1980getRow('SELECT id, teXt, date as birthday FROM mydate'); // would return array('id' => '1','teXt' => 'one','birthday' => '09.05.1960');
|
| Parameter |
|
| mixed |
$source |
|
|
can be an SQL query string, or a resource. |
|
|
| integer |
$row |
= >>-1<< |
|
IF >=0 will move internal row pointer to abs. position befor fetching the data. Default is -1. |
|
|
| string |
$fetchMode |
= >>BS_DB_FETCHMODE_ASSOC<< |
|
(opt) (default BS_DB_FETCHMODE_ASSOC) see above. |
|
| Returns |
mixed vector , hash. NULL if row offest is out of bounds OR no data found. |
| Throws |
Bs_Exception on error |
|
&getCol |
|
public void &getCol( mixed $source, [ mixed $col, integer $startRow, integer $amount ] )
|
| |
Fetch a full OR fraction col of data as vector given by the param $source.
IF param $source is a query-string:Execute the query, fetch col and clean up (free the results set).ELSEIF param $source is a result set:Move down $row-rows, fetch col and leave DB curser*after* last read row. Don't free result set.NOTE: col may be given as col-name (string) instead of offset (int). It will beused as key to identify the right column. So pass the right type!Sample: Given is the table "mytable" containing:\/ID TEXT DATE-------------------------------1 'one' 09.05.19602 'two' 25.12.19703 'three' 31.01.1980/\getCol('SELECT id, teXt, date as birthday FROM mydate', 'teXt'); // would return array('one', 'two', 'three');If you'd only wand to get array('two', 'three') you'd do:getCol($query, 1, 1) or getCol($query, 1, 1, 2) or getCol($query, 'teXt', 1)
|
| Parameter |
|
| mixed |
$source |
|
|
can be an SQL query string, or a resource. |
|
|
| mixed |
$col |
= >>0<< |
|
which column to return (integer [column number, starting at 0] or string [column name]) |
|
|
| integer |
$startRow |
= >>0<< |
|
the row number at which we start, *including* that row. default is 0. |
|
|
| integer |
$amount |
= >>-1<< |
|
the row number at which we stop, *including* that row. default is -1 which means loop until the end. |
|
| Returns |
void |
| Throws |
Bs_Exception on error |
|
&getAssoc |
|
public void &getAssoc( mixed $source, [ string $forceArray, string $hashInsteadOfVector ] )
|
| |
Fetch the entire data given by the param $source into a hash using the first column as the key.
$source can be either a SQL query string or a result set.IF param $source is a query-string:Execute the query, fetch entire data and clean up (free the results set).ELSEIF param $source is a result set:Fetch the entire data. Don't free result set.If more than 2 values are involved in the result set or if $forceArray is TRUE,a hash containing a vector is returned e.g. array('1' => array('one', '944679408'));otherwise a simple hash is returned (see sample below).If the result set contains fewer than two columns, a BS_DB_ERROR_TRUNCATED exception is raised.Sample: Given is the table "mytable" containing:ID TEXT DATE-------------------------------1 'one' 09.05.19602 'two' 25.12.19703 'three' 31.01.1980example 1:getAssoc('SELECT id,text FROM mytable'); // would return: array( '1' => 'one','2' => 'two','3' => 'three')example 2:getAssoc('SELECT id,text,date FROM mydate') // would return: array( '1' => array('one', '944679408'),'2' => array('two', '944679408'),'3' => array('three', '944679408'))example 3:getAssoc('SELECT id,text,date FROM mydate', TRUE, TRUE) // would return:array( '1' => array('text'=>'one', 'date'=>'944679408'),'2' => array('text'=>'two', 'date'=>'944679408'),'3' => array('text'=>'three', 'date'=>'944679408'))this option is new, 2002/03/25 --andrejspecial note: you have to use a key that is unique. otherwise only one record will beused, i think the later ones overwrite the previews.
|
| Parameter |
|
| mixed |
$source |
|
|
can be an SQL query string, or a resource. |
|
|
| string |
$forceArray |
= >>FALSE,<< |
|
(opt) used only when the query returns exactly two columns. If true, the values of the returned array will be one-element arrays instead of scalars. |
|
|
| string |
$hashInsteadOfVector |
= >>FALSE<< |
|
(default is FALSE. if set to true then the values will be returned in a hash (with field names) instead of a vector. see example 3 above.) |
|
| Returns |
void |
| Throws |
Bs_Exception on error |
|
&getAssoc2 |
|
public void &getAssoc2( mixed $source, [ string $forceArray ] )
|
| |
hash array COLUMN BY COLUMN not record by record.
The rest is like getAssoc(). See above.Sample: Given is the table "mytable" containing:ID TEXT DATE-------------------------------1 'one' 09.05.19602 'two' 25.12.19703 'three' 31.01.1980getAssoc2('SELECT id, teXt, date as birthday FROM mytable');would return:array( 'id' => array('1', '2', '3'),'teXt' => array('one', 'two', 'three'),'birthday' => array('09.05.1960', '25.12.1970', '31.01.1980'))
|
| Parameter |
|
| mixed |
$source |
|
|
can be an SQL query string, or a resource. |
|
|
| string |
$forceArray |
= >>FALSE<< |
|
used only when the query returns exactly 1 column. If true, the values of the returned array will be one-element arrays instead of scalars. |
|
| Returns |
void |
| Throws |
Bs_Exception on error |
|
&getAll |
|
public array &getAll( mixed $source, [ string $fetchMode ] )
|
| |
Fetch the entire data given by the param $source into a hash.
$source can be either a SQL query string or a result set.IF param $source is a query-string:Execute the query, fetch entire data and clean up (free the results set).ELSEIF param $source is a result set:Fetch the entire data. Don't free result set.Sample: Given is the table "mytable" containing:ID TEXT DATE-------------------------------1 'one' 09.05.19602 'two' 25.12.19703 'three' 31.01.1980getAll('SELECT id, teXt, date as birthday FROM mydate', 'teXt', BS_DB_FETCHMODE_ASSOC) returns:would return:array( array('id' => '1', 'teXt' => 'one', 'birthday' => '09.05.1960'),array('id' => '2', 'teXt' => 'two', 'birthday' => '25.12.1970'),array('id' => '3', 'teXt' => 'three', 'birthday' => '31.01.1980'))
|
| Parameter |
|
| mixed |
$source |
|
|
can be an SQL query string, or a resource. |
|
|
| string |
$fetchMode |
= >>BS_DB_FETCHMODE_ASSOC<< |
|
(opt) (default BS_DB_FETCHMODE_ASSOC) see getRow. |
|
| Returns |
array a nested array. See sample above. may be an empty array if nothing was found. |
| Throws |
Bs_Exception on error |
|
assertExtension |
|
public bool assertExtension( $name )
|
| |
Load a PHP database extension if it is not loaded already.
|
| Parameter |
|
|
$name |
|
|
Warning: documentation is missing. |
|
| Returns |
bool true if the extension was already or successfully loaded, false if it could not be loaded |
|
toString |
|
public string toString( )
|
| |
Returns a string with information about the current state of this db object.
for security reason, no user/pass information is returned here because someone(you?) might log the returned value.
Warning: documentation is missing.
|
| Returns |
string |
|
escapeString |
|
public string escapeString( string $query )
|
| |
Escape string for the query.
if there is a better/specialized way for your specific rdbms, overwrite this.
|
| Parameter |
|
|
| Returns |
string the escaped query |
|
formatDateForDb |
|
public string formatDateForDb( string $date )
|
| |
format and return date string in database date format.
overwrite this if needed.
|
| Parameter |
|
| string |
$date |
|
|
(yyyy-mm-dd) |
|
| Returns |
string (date formatted for this rdbms implementation) |
| See Also |
formatDatetimeForDb(), formatTimestampForDb() |
|
formatDatetimeForDb |
|
public string formatDatetimeForDb( string $datetime )
|
| |
format and return datetime string in database datetime format.
overwrite this if needed.
|
| Parameter |
|
| string |
$datetime |
|
|
(yyyy-mm-dd hh:mm:ss) |
|
| Returns |
string (datetime formatted for this rdbms implementation) |
| See Also |
formatDateForDb(), formatTimestampForDb() |
|
formatTimestampForDb |
|
public string formatTimestampForDb( string $timestamp )
|
| |
format and return timestamp string in database timestamp format.
overwrite this if needed.
|
| Parameter |
|
| string |
$timestamp |
|
|
(yyyy-mm-dd hh:mm:ss) |
|
| Returns |
string (timestamp formatted for this rdbms implementation) |
| See Also |
formatDateForDb(), formatDatetimeForDb() |
|
"eArgs |
|
public string "eArgs( array $arr, [ string $glue ] )
|
| |
Takes a hash and returns a string for an sql query.
example 1:$arr = array('name' => 'tom', 'age' => '35', 'sex' => 'm');$sqlQuery = "UPDATE tbl SET " . $db->quoteArgs($arr, ', ') . " WHERE ID = 1";=> "UPDATE tbl SET name = 'tom', age = '35', sex = 'm' WHERE ID = 1";example 2:$arr = array('name' => 'tom', 'age' => '35', 'sex' => 'm');$sqlQuery = "SELECT * FROM tbl WHERE " . $db->quoteArgs($arr, ' AND ');=> "SELECT * FROM tbl WHERE name = 'tom' AND age = '35' AND sex = 'm'";Note I: $this->escapeString() is used on the $value.Note II: boolean values are converted to ints.
|
| Parameter |
|
| array |
$arr |
|
|
an associative array. |
|
|
| string |
$glue |
= >>', '<< |
|
a delimiter, default is ', ', could be ' AND ' or ' OR ' ... |
|
| Returns |
string a string to include in an sql query. |
|
isManipulation |
|
public void isManipulation( string $query )
|
| |
Tells whether a query is a data manipulation query (insert, update, delete replace, alter, drop, create).
|
| Parameter |
|
|
| Returns |
void |
|
getErrorMessage |
|
public string getErrorMessage( long $errCode )
|
| |
Return a textual error message for a bs DB error code
|
| Parameter |
|
| long |
$errCode |
|
|
a bs error code (see constants) |
|
| Returns |
string error message, or false if the error code was not recognized |
|
autoCommit |
|
public void autoCommit( [ string $on ] )
|
| |
Turn autoCommit on or off
|
| Parameter |
|
| string |
$on |
= >>FALSE<< |
|
(opt) If TRUE turn on else off (default TRUE) |
|
| Returns |
void |
| Throws |
Bs_Exception if not supported by this DB |
|
startTransaction |
|
public void startTransaction( )
|
| |
Starts a transaction
|
| Returns |
void |
| Throws |
Bs_Exception if not supported by this DB or on any other error. |
|
commit |
|
public void commit( )
|
| |
Commit last query
|
| Returns |
void |
| Throws |
Bs_Exception if not supported by this DB or on any other error. |
|
rollback |
|
public void rollback( )
|
| |
Rollback last query
|
| Returns |
void |
| Throws |
Bs_Exception if not supported by this DB or on any other error. |
|
setPointer |
|
public mixed setPointer( int $result, $absolutPos )
|
| |
Set the internal row pointer of the result id to point to the specified row number.
The next fetch call would return that row.
|
| Parameter |
|
| int |
$result |
|
|
the row number, which starts at position 0. |
|
|
|
$absolutPos |
|
|
Warning: documentation is missing. |
|
| Returns |
mixed TRUE on success, NULL on a row number (param $num) that is out of bounds. |
| Throws |
Bs_Exception if not supported by this DB or on any other error. |
|
fetchRow |
|
public mixed fetchRow( $result, string $fetchMode )
|
| |
Fetch a row and return it as vector or hash depending on the fetchMode.
Subsequent calls will return the next row in the result set, or NULL if there are no more rows.If fetchMode is BS_DB_FETCHMODE_ASSOC (default) it returns a hash array that correspondsto the fetched row else if it's BS_DB_FETCHMODE_ORDERED returns a vector starting at offset 0.
|
| Parameter |
|
|
$result |
|
|
Warning: documentation is missing. |
|
|
| string |
$fetchMode |
|
|
Warning: documentation is missing. |
|
| Returns |
mixed vector , hash or NULL if there is no more data |
| Throws |
Bs_Exception if not supported by this DB or any other error. |
|
freeResult |
|
public void freeResult( $result )
|
| |
Free the internal resources associated with $result.
|
| Parameter |
|
|
$result |
|
|
Warning: documentation is missing. |
|
| Returns |
void |
|
numCols |
|
public int numCols( $result )
|
| |
Get the number of columns (fields) in a result identifier.
|
| Parameter |
|
|
$result |
|
|
Warning: documentation is missing. |
|
| Returns |
int the number of columns per row in $result |
| Throws |
Bs_Exception if not supported by this DB or any other error. |
|
numRows |
|
public int numRows( $result )
|
| |
Get the number of rows from a result identifier.
|
| Parameter |
|
|
$result |
|
|
Warning: documentation is missing. |
|
| Returns |
int see above |
| Throws |
Bs_Exception if not supported by this DB or any other error. |
|
affectedRows |
|
public int affectedRows( )
|
| |
Gets the number of rows affected by the last data manipulation query.
|
| Returns |
int see above |
| Throws |
Bs_Exception if not supported by this DB or any other error. |
|
insertId |
|
public int insertId( )
|
| |
Get the id generated from the previous INSERT operation.
|
| Returns |
int a positive integer, which means >=1 |
| Throws |
Bs_Exception if not supported by this DB or any other error. |
|
nativeErrorCode |
|
public int nativeErrorCode( )
|
| |
Returns the numerical native error code from the previous DB operation or 0 (zero) if no error occured.
|
| Returns |
int see above |
| Throws |
Bs_Exception if not supported by this DB or any other error. |
|
nativeErrorMsg |
|
public string nativeErrorMsg( )
|
| |
Returns the native error text from the previous DB operation or '' (empty string) if no error occured.
|
| Returns |
string see above |
| Throws |
Bs_Exception if not supported by this DB or any other error. |
|