DB Class Reference
[Model]
Factory class for DAO classes. More...
Static Public Member Functions |
|
static | commit ($connection=self::DEFAULT_CONNECTION) |
static | create ($model) |
Creates DAO class for given table. |
|
static | create_connection ($connection_name, $driver, $db_name, $db_user, $db_pwd, $db_host, $params=false) |
Create a connction with given name and
paramteres. |
|
static | end_trans ($status, $connection=self::DEFAULT_CONNECTION) |
static | escape ($value, $connection=self::DEFAULT_CONNECTION) |
Escape given value. |
|
static | escape_database_entity ($obj, $connection=self::DEFAULT_CONNECTION, $type=IDBDriver::FIELD) |
Escape given database object, like table,
field etc. |
|
static | execute ($query, $connection=self::DEFAULT_CONNECTION) |
Execute an query. |
|
static | execute_script ($file, $connection=self::DEFAULT_CONNECTION) |
Execute all statements within an sql file.
|
|
static | explain ($sql, $connection=self::DEFAULT_CONNECTION) |
Explain the given query. |
|
static | extract_next_sql_statement ($handle) |
static | format ($value, $table=null, $column= '') |
Formats value. |
|
static | format_where ($value, $table=null, $column= '') |
Formats value for WHERE clause. |
|
static | get_connection ($name_or_object=self::DEFAULT_CONNECTION) |
Returns connection with given name. |
|
static | get_item ($table, $key, $value) |
Returns (and caches) instance for given
table, with haven given value on given colum. |
|
static | get_item_by_pk ($table, $value) |
Returns (and caches) instance for given
table, with haven given value on primary key. |
|
static | get_item_multi ($table, $arr_values) |
Returns (and caches) instance for given
table, with haven given values. |
|
static | initialize () |
Initialize whole DB
System. |
|
static | last_insert_id ($connection=self::DEFAULT_CONNECTION) |
Get last insert ID. |
|
static | log_query ($query, $seconds, $status, $conn=self::DEFAULT_CONNECTION) |
Log a query. |
|
static | query ($query, $connection=self::DEFAULT_CONNECTION) |
Execute a SELECT query. |
|
static | quote ($val, $connection=self::DEFAULT_CONNECTION) |
Quote and escape a string. |
|
static | rollback ($connection=self::DEFAULT_CONNECTION) |
static | start_trans ($connection=self::DEFAULT_CONNECTION) |
Public Attributes |
|
const | DEFAULT_CONNECTION = 'default' |
const | DEFAULT_CONNNECTION = 'default' |
Static Public Attributes |
|
static | $db_connect_time = 0 |
static | $queries_total_time = 0 |
static | $query_log = array() |
Detailed Description
Factory class for DAO classes.
Definition at line 10 of file db.cls.php.
Member Function Documentation
static DB::commit | ( | $ | connection =
self::DEFAULT_CONNECTION |
) | [static] |
Definition at line 419 of file db.cls.php.
00419 { 00420 $conn = self::get_connection($connection); 00421 $conn->trans_commit(); 00422 }
static DB::create | ( | $ | model | ) | [static] |
Creates DAO class for given table.
- Returns:
- IDataObject
Definition at line 74 of file db.cls.php.
00074 { 00075 Load::models($model); // throws on error 00076 $classname = 'DAO' . ucfirst($model); // Model must be ASCII 00077 if (class_exists($classname)) { 00078 return new $classname(); 00079 } 00080 00081 throw new Exception(tr('Model %s not found', 'core', array('%s' => $model))); 00082 }
static DB::create_connection | ( | $ | connection_name, | |
$ | driver, | |||
$ | db_name, | |||
$ | db_user, | |||
$ | db_pwd, | |||
$ | db_host, | |||
$ | params = false |
|||
) | [static] |
Create a connction with given name and paramteres.
- Parameters:
-
string $connection_name string $driver string $db_name string $db_user string $db_pwd string $db_host mixed $params Driver specific data
- Returns:
- IDBDriver
Definition at line 57 of file db.cls.php.
00057 { 00058 //define database configuration values 00059 Load::directories('model/drivers/' . $driver); 00060 //Load::directories('model/drivers/' . $driver . '/sqlbuilder'); 00061 $drivername = 'DBDriver' . ucfirst($driver); // Driver must be ASCII 00062 $db = new $drivername(); 00063 $db->initialize($db_name, $db_user, $db_pwd, $db_host, $params); 00064 self::$connections[$connection_name] = $db; 00065 00066 return $db; 00067 }
static DB::end_trans | ( | $ | status, | |
$ | connection =
self::DEFAULT_CONNECTION |
|||
) | [static] |
Definition at line 410 of file db.cls.php.
00410 { 00411 if ($status->is_ok()) { 00412 self::commit($connection); 00413 } 00414 else { 00415 self::rollback($connection); 00416 } 00417 }
static DB::escape | ( | $ | value, | |
$ | connection =
self::DEFAULT_CONNECTION |
|||
) | [static] |
Escape given value.
- Parameters:
-
mixed $value
- Returns:
- string
Definition at line 155 of file db.cls.php.
00155 { 00156 $conn = self::get_connection($connection); 00157 return $conn->escape($value); 00158 }
static DB::escape_database_entity | ( | $ | obj, | |
$ | connection =
self::DEFAULT_CONNECTION , |
|||
$ | type = IDBDriver::FIELD |
|||
) | [static] |
Escape given database object, like table, field etc.
- Parameters:
-
string $obj
Definition at line 165 of file db.cls.php.
00165 { 00166 $conn = self::get_connection($connection); 00167 return $conn->escape_database_entity($obj, $type); 00168 }
static DB::execute | ( | $ | query, | |
$ | connection =
self::DEFAULT_CONNECTION |
|||
) | [static] |
Execute an query.
Do not use with SELECT!
- Parameters:
-
string|IDBQuery $query
- Returns:
- Status
Definition at line 260 of file db.cls.php.
00260 { 00261 $timer = new Timer(); 00262 if ($query instanceof IDBQuery) { 00263 $connection = $query->get_table()->get_table_driver(); 00264 $query = $query->get_sql(); 00265 } 00266 $conn = self::get_connection($connection); 00267 $ret = $conn->execute($query); 00268 self::log_query($query, $timer->seconds_elapsed(), $ret, $conn); 00269 return $ret; 00270 }
static DB::execute_script | ( | $ | file, | |
$ | connection =
self::DEFAULT_CONNECTION |
|||
) | [static] |
Execute all statements within an sql file.
- Parameters:
-
string $file
- Returns:
- Status
Definition at line 299 of file db.cls.php.
00299 { 00300 $status = new Status(); 00301 if (file_exists($file)) { 00302 $conn = self::get_connection($connection); 00303 $conn->make_default(); 00304 $handle = fopen($file, 'r'); 00305 $dao = self::create('cache'); 00306 while($query = self::extract_next_sql_statement($handle)) { 00307 if ($query != ';') { 00308 $status->merge($conn->execute($query)); 00309 } 00310 } 00311 fclose($handle); 00312 $def = self::get_connection(); 00313 $def->make_default(); 00314 } 00315 else { 00316 $status->append(tr('File %file not found', 'core', array('%file' => $file))); 00317 } 00318 return $status; 00319 }
static DB::explain | ( | $ | sql, | |
$ | connection =
self::DEFAULT_CONNECTION |
|||
) | [static] |
Explain the given query.
- Since:
- 0.5.1
- Parameters:
-
string $sql
- Returns:
- IDBResultSet False if quey cant be explain or driver does not support it
Definition at line 280 of file db.cls.php.
00280 { 00281 $conn = self::get_connection($connection); 00282 return $conn->explain($sql); 00283 }
static DB::extract_next_sql_statement | ( | $ | handle | ) | [static] |
Definition at line 321 of file db.cls.php.
00321 { 00322 $ret = ''; 00323 $last = ''; 00324 $continue = true; 00325 while ($continue) { 00326 $char = self::read_next($handle, $ret); 00327 if ($char === false) { 00328 break; 00329 } 00330 00331 switch($char) { 00332 case ';': 00333 $continue = false; 00334 break; 00335 case "'": 00336 case '"': 00337 $ret .= self::extract_until($handle, $char); 00338 break; 00339 case '#': 00340 // Command till end of line 00341 $ret = substr($ret, 0, -1); 00342 self::extract_until($handle, "\n"); 00343 break; 00344 case '-': 00345 // handle -- comments (end at end of line) 00346 if ($last == '-') { 00347 $ret = substr($ret, 0, -2); 00348 self::extract_until($handle, "\n"); 00349 } 00350 break; 00351 case '*': 00352 // Handle /* .. */ comments 00353 if ($last == '/') { 00354 $ret = substr($ret, 0, -2); 00355 self::extract_until($handle, "*/"); 00356 } 00357 break; 00358 } 00359 $last = $char; 00360 } 00361 00362 $ret = str_replace("\n", ' ', $ret); 00363 $ret = str_replace("\r", ' ', $ret); 00364 $ret = trim($ret); 00365 return $ret; 00366 }
static DB::format | ( | $ | value, | |
$ | table = null , |
|||
$ | column = '' |
|||
) | [static] |
Formats value.
If no table is given or column does not exists, it will return DB::quote($value)
- Parameters:
-
mixed $value Value to format IDBTable $table The source table string $column The column on table
Definition at line 188 of file db.cls.php.
00188 { 00189 if ($value instanceof DBExpression) { 00190 return $value->format(); 00191 } else { 00192 $field = self::find_field($table, $column); 00193 return $field->format($value); 00194 } 00195 }
static DB::format_where | ( | $ | value, | |
$ | table = null , |
|||
$ | column = '' |
|||
) | [static] |
Formats value for WHERE clause.
If no table is given or column does not exists, it will return DB::quote($value)
- Parameters:
-
mixed $value Value to format IDBTable $table The source table string $column The column on table
Definition at line 204 of file db.cls.php.
static DB::get_connection | ( | $ | name_or_object =
self::DEFAULT_CONNECTION |
) | [static] |
Returns connection with given name.
- Parameters:
-
string|IDBDriver $name
- Returns:
- IDBDriver
Definition at line 35 of file db.cls.php.
00035 { 00036 if ($name_or_object instanceof IDBDriver) { 00037 return $name_or_object; 00038 } 00039 else if (isset(self::$connections[$name_or_object])) { 00040 return self::$connections[$name_or_object]; 00041 } 00042 throw new Exception("Connection $name_or_object not found"); 00043 }
static DB::get_item | ( | $ | table, | |
$ | key, | |||
$ | value | |||
) | [static] |
Returns (and caches) instance for given table, with haven given value on given colum.
- Returns:
- mixed Object or false
Definition at line 107 of file db.cls.php.
00107 { 00108 $ret = false; 00109 if (!empty($value) && !$value instanceof DBNull) { 00110 $ret = self::get_item_multi($table, array($key => $value)); 00111 } 00112 return $ret; 00113 }
static DB::get_item_by_pk | ( | $ | table, | |
$ | value | |||
) | [static] |
Returns (and caches) instance for given table, with haven given value on primary key.
- Attention:
- Works for models with one primary key only
- Returns:
- mixed Object or false
Definition at line 91 of file db.cls.php.
00091 { 00092 $model = self::create($table); // Throws! 00093 /* @var $model IDataObject */ 00094 $pks = $model->get_table_keys(); 00095 if (count($pks) != 1) { 00096 throw new Exception(tr('No or more than 1 keys on model %s: get_item_by_pk() cannot be applied', 'core', array('%s' => $table))); 00097 } 00098 $pk_name = array_shift(array_keys($pks)); 00099 return self::get_item($table, $pk_name, $value); 00100 }
static DB::get_item_multi | ( | $ | table, | |
$ | arr_values | |||
) | [static] |
Returns (and caches) instance for given table, with haven given values.
- Parameters:
-
string $table array $values Associative array with column => value
- Returns:
- IDataObject False if not found
Definition at line 122 of file db.cls.php.
00122 { 00123 ksort($arr_values); 00124 $keys = array('db', $table, strtr(http_build_query($arr_values), '[]', '__')); 00125 $ret = RuntimeCache::get($keys, null); 00126 if (is_null($ret)) { 00127 $ret = false; 00128 $dao = self::create($table); 00129 foreach($arr_values as $col => $value) { 00130 $dao->$col = $value; 00131 } 00132 $dao->limit(1); 00133 if ($dao->find(IDataObject::AUTOFETCH)) { 00134 $ret = clone($dao); // Get rid of Query related properties 00135 } 00136 RuntimeCache::set($keys, $ret); 00137 } 00138 return $ret; 00139 }
static DB::initialize | ( | ) | [static] |
Initialize whole DB System.
Definition at line 144 of file db.cls.php.
00144 { 00145 //define database configuration values 00146 self::$db = self::create_connection(self::DEFAULT_CONNECTION, APP_DB_TYPE, APP_DB_NAME, APP_DB_USER, APP_DB_PWD, APP_DB_HOST); 00147 }
static DB::last_insert_id | ( | $ | connection =
self::DEFAULT_CONNECTION |
) | [static] |
Get last insert ID.
Definition at line 288 of file db.cls.php.
00288 { 00289 $conn = self::get_connection($connection); 00290 return $conn->last_insert_id(); 00291 }
static DB::log_query | ( | $ | query, | |
$ | seconds, | |||
$ | status, | |||
$ | conn =
self::DEFAULT_CONNECTION |
|||
) | [static] |
Log a query.
Definition at line 437 of file db.cls.php.
00437 { 00438 if (Config::has_feature(Config::LOG_QUERIES)) { 00439 $c = self::get_connection($conn); 00440 $log = array( 00441 'query' => $query, 00442 'seconds' => $seconds, 00443 'success' => $status->is_ok(), 00444 'message' => $status->to_string(Status::OUTPUT_PLAIN) 00445 ); 00446 Load::components('logger'); 00447 Logger::log('queries', $log); 00448 00449 $log['connection'] = $c; 00450 self::$query_log[] = $log; 00451 self::$queries_total_time += $seconds; 00452 } 00453 00454 if (Config::has_feature(Config::LOG_SLOW_QUERIES)) { 00455 if ($seconds > Config::get_value(Config::DB_SLOW_QUERY_THRESHOLD, false, 0.0100)) { 00456 $log = array( 00457 'query' => $query, 00458 'seconds' => $seconds, 00459 ); 00460 Load::components('logger'); 00461 Logger::log('slow_queries', $log); 00462 } 00463 } 00464 00465 if ($status->is_error()) { 00466 if (Config::has_feature(Config::LOG_FAILED_QUERIES)) { 00467 $log = array( 00468 'query' => $query, 00469 'message' => $status->to_string(Status::OUTPUT_PLAIN) 00470 ); 00471 Load::components('logger'); 00472 Logger::log('failed_queries', $log); 00473 } 00474 if (Config::has_feature(Config::THROW_ON_DB_ERROR)) { 00475 $text = $status->to_string(Status::OUTPUT_PLAIN) . " [$query]"; 00476 throw new Exception($text); 00477 } 00478 } 00479 }
static DB::query | ( | $ | query, | |
$ | connection =
self::DEFAULT_CONNECTION |
|||
) | [static] |
Execute a SELECT query.
- Parameters:
-
string|IDBQuery $query
- Returns:
- IDBResultSet
Definition at line 242 of file db.cls.php.
00242 { 00243 $timer = new Timer(); 00244 if ($query instanceof IDBQuery) { 00245 $connection = $query->get_table()->get_table_driver(); 00246 $query = $query->get_sql(); 00247 } 00248 $conn = self::get_connection($connection); 00249 $ret = $conn->query($query); 00250 self::log_query($query, $timer->seconds_elapsed(), $ret->get_status(), $conn); 00251 return $ret; 00252 }
static DB::quote | ( | $ | val, | |
$ | connection =
self::DEFAULT_CONNECTION |
|||
) | [static] |
Quote and escape a string.
- Parameters:
-
string $val
- Returns:
- string
Definition at line 176 of file db.cls.php.
00176 { 00177 $conn = self::get_connection($connection); 00178 return $conn->quote($val); 00179 }
static DB::rollback | ( | $ | connection =
self::DEFAULT_CONNECTION |
) | [static] |
Definition at line 424 of file db.cls.php.
00424 { 00425 $conn = self::get_connection($connection); 00426 $conn->trans_rollback(); 00427 }
static DB::start_trans | ( | $ | connection =
self::DEFAULT_CONNECTION |
) | [static] |
Definition at line 405 of file db.cls.php.
00405 { 00406 $conn = self::get_connection($connection); 00407 $conn->trans_start(); 00408 }
Member Data Documentation
DB::$db_connect_time = 0
[static] |
Definition at line 27 of file db.cls.php.
DB::$queries_total_time = 0
[static] |
Definition at line 26 of file db.cls.php.
DB::$query_log = array()
[static] |
Definition at line 25 of file db.cls.php.
const DB::DEFAULT_CONNECTION = 'default' |
Definition at line 11 of file db.cls.php.
const DB::DEFAULT_CONNNECTION = 'default' |
- Deprecated:
- Left for backward compatability only
Definition at line 15 of file db.cls.php.
The documentation for this class was generated from the following file:
- gyro/core/model/base/db.cls.php