Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F57016268
Database.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
6 KB
Referenced Files
None
Subscribers
None
Database.php
View Options
<?php
/**
* _, __, _, _ __, _ _, _, _
* / \ |_) (_ | | \ | /_\ |\ |
* \ / |_) , ) | |_/ | | | | \|
* ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
*
* Database base class
*
* @package ObsidianWorkspaces
* @subpackage Keruald
* @author Sébastien Santoro aka Dereckson <dereckson@espace-win.org>
* @license http://www.opensource.org/licenses/bsd-license.php BSD
* @filesource
*
*/
abstract
class
Database
implements
LoadableWithContext
{
///
/// Singleton pattern, LoadableWithContext implementation
///
/**
* @var Database the singleton instance
*/
private
static
$instance
=
null
;
/**
* Loads a new instance of the relevant Database object
*
* @param Context $context The application context
* @return Database The database instance
*/
public
static
function
load
(
Context
$context
)
{
if
(
self
::
$instance
===
null
)
{
if
(!
isset
(
$context
->
config
[
'sql'
][
'engine'
]))
{
throw
new
InvalidArgumentException
(
"To load a database, you need to add in your configuration a parameter block like:
'sql' => [
'engine' => 'TheSQLEngineYouWant',
//... configuration specific to the database
]"
);
}
$className
=
$context
->
config
[
'sql'
][
'engine'
]
.
'Database'
;
if
(!
class_exists
(
$className
))
{
throw
new
InvalidArgumentException
(
"The class matching your engine doesn't exist: $className"
);
}
self
::
$instance
=
$className
::
load
(
$context
);
}
return
self
::
$instance
;
}
///
/// The methods to implement
///
/**
* Executes a query
*
* @param string $query The query to execute
* @return DatabaseResult The query result
*/
public
abstract
function
query
(
$query
);
/**
* Retrieves the id generated by the last statement
*
* @return int The last id generated
*/
public
abstract
function
nextId
();
/**
* Escapes the expression
*
* @param $expression The expression to escape
* @return string The escaped expression
*/
public
abstract
function
escape
(
$expression
);
///
/// Generic methods
///
/**
* Fetches a row of the result
*
* @param DatabaseResult $result The query result
* @return array An associative array with the databae result
*/
public
function
fetchRow
(
DatabaseResult
$result
)
{
return
$result
->
fetchRow
();
}
/**
* Gets number of rows in result
*
* @param DatabaseResult $result The query result
* @return int The number of rows in the specified result
*/
public
function
numRows
(
DatabaseResult
$result
)
{
return
$result
->
numRows
();
}
/**
* Runs a query and fetches a scalar result. Dies if an error occurs.
*
* @param string $query the query
* @param string $errorMessage The error message to print when dying. [optional, by default CantExecuteQuery L10n message]
* @return string The result of the query
*/
public
function
queryScalar
(
$query
=
''
,
$errorMessage
=
''
)
{
if
(
$errorMessage
===
''
)
{
$errorMessage
=
Language
::
get
(
'CantExecuteQuery'
);
}
if
(
$query
===
''
||
$query
===
false
||
$query
===
null
)
{
//No query, no value
return
''
;
}
elseif
(!
$result
=
$this
->
query
(
$query
))
{
//An error have occured
message_die
(
SQL_ERROR
,
$error_message
,
''
,
''
,
''
,
$query
);
}
elseif
(!
$row
=
$this
->
fetchRow
(
$result
))
{
return
''
;
}
else
{
return
$row
[
0
];
}
}
///
/// Events
///
/**
* @var Array
* @event CantConnectToHost Functions to call when it's not possible to connect to the database host
* @eventparam Database $db The current database instance
*/
public
$cantConnectToHostEvents
=
[];
/**
* @var Array
* @event QueryError Functions to call when a query fails.
* @eventparam Database $db The current database instance
* @eventparam string $query The failed query
* @eventparam DatabaseException $ex The exception describing the query error
*/
public
$queryErrorEvents
=
[];
/**
* Called on connect failure
*/
protected
abstract
function
onCantConnectToHost
();
/**
* Called on query error
*
* @param string $query The query executed when the error occured
*/
protected
abstract
function
onQueryError
(
$query
);
///
/// Compatibility feature
///
/**
* Allows the legacy use of sql_query, sql_fetchrow, sql_escape, etc.
*
* @param $name The name of the inaccessible method called
* @param $arguments The method arguments
* @return mixed The result of the method called
* @throws BadMethodCallException when the method name doesn't match a legacy method
* @deprecated
*/
public
function
__call
(
$name
,
$arguments
)
{
if
(
substr
(
$name
,
0
,
4
)
==
'sql_'
)
{
switch
(
$name
)
{
case
'sql_nextid'
:
$newMethodName
=
'nextId'
;
break
;
case
'sql_query_express'
:
$newMethodName
=
'queryScalar'
;
break
;
case
'sql_fetchrow'
:
$newMethodName
=
'fetchRow'
;
break
;
case
'sql_numrows'
:
$newMethodName
=
'numRows'
;
break
;
default
:
$newMethodName
=
substr
(
$name
,
4
);
}
if
(
method_exists
(
$this
,
$newMethodName
))
{
//trigger_error("\$db->$name calls shall be replaced by \$db->$newMethodName calls.", E_USER_DEPRECATED);
return
call_user_func_array
(
array
(
$this
,
$newMethodName
),
$arguments
);
}
}
$className
=
get_class
(
$this
);
throw
new
BadMethodCallException
(
"Method doesn't exist: $className::$name"
);
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, Nov 14, 22:43 (4 d, 19 h ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4229806
Default Alt Text
Database.php (6 KB)
Attached To
Mode
rOBSIDIAN Obsidian Workspaces
Attached
Detach File
Event Timeline
Log In to Comment