Commit 9468e05b authored by Taddeus Kroes's avatar Taddeus Kroes

Removed config file and checnged method name.

parent 59872815
...@@ -5,12 +5,17 @@ ...@@ -5,12 +5,17 @@
* @package pQuery * @package pQuery
*/ */
include_once 'pquery.config.php';
/** /**
* Common utility class. * Common utility class.
*/ */
class pQuery { class pQuery {
/**
* Name of the utilities folder
*
* @var string
*/
const UTILS_FOLDER = 'utils/';
/** /**
* Pattern of the alias created for an extending plugin that has defined an alias. * Pattern of the alias created for an extending plugin that has defined an alias.
* *
...@@ -153,7 +158,7 @@ class pQuery { ...@@ -153,7 +158,7 @@ class pQuery {
$files = func_get_args(); $files = func_get_args();
foreach( $files as $basename ) { foreach( $files as $basename ) {
$path = PQUERY_ROOT.pQueryConfig::UTILS_FOLDER.$basename.'.php'; $path = PQUERY_ROOT.self::UTILS_FOLDER.$basename.'.php';
if( !file_exists($path) ) { if( !file_exists($path) ) {
return self::error('Utility "%s" could not be loaded (looked in "%s").', return self::error('Utility "%s" could not be loaded (looked in "%s").',
......
...@@ -14,6 +14,13 @@ class pQuerySql extends pQuery implements pQueryExtension { ...@@ -14,6 +14,13 @@ class pQuerySql extends pQuery implements pQueryExtension {
static $accepts = array('string' => 'parse_query', 'resource'); static $accepts = array('string' => 'parse_query', 'resource');
/**
* The default row fetching type, one of 'assoc', 'object' or 'array'.
*
* @var resource
*/
const DEFAULT_FETCH_TYPE = 'assoc';
/** /**
* The MySQL link identifier. * The MySQL link identifier.
* *
...@@ -27,6 +34,14 @@ class pQuerySql extends pQuery implements pQueryExtension { ...@@ -27,6 +34,14 @@ class pQuerySql extends pQuery implements pQueryExtension {
*/ */
static $variable_alias = 'query'; static $variable_alias = 'query';
/**
* Database login data, should be an associative array containing
* values for 'host', 'username', 'password' and 'dbname'
*
* @var array
*/
static $login_data = array();
/** /**
* The result of the current query. * The result of the current query.
* *
...@@ -91,7 +106,7 @@ class pQuerySql extends pQuery implements pQueryExtension { ...@@ -91,7 +106,7 @@ class pQuerySql extends pQuery implements pQueryExtension {
* @param array $variables The variables to replace. * @param array $variables The variables to replace.
* @returns pQuerySql The current query object. * @returns pQuerySql The current query object.
*/ */
function set_plain($variables) { function set_unescaped($variables) {
return $this->replace_variables($variables, false); return $this->replace_variables($variables, false);
} }
...@@ -114,7 +129,6 @@ class pQuerySql extends pQuery implements pQueryExtension { ...@@ -114,7 +129,6 @@ class pQuerySql extends pQuery implements pQueryExtension {
function execute() { function execute() {
self::assert_connection(); self::assert_connection();
//debug('query:', $this->query);
$result = mysql_query($this->query, self::$link); $result = mysql_query($this->query, self::$link);
if( !$result ) if( !$result )
...@@ -132,7 +146,7 @@ class pQuerySql extends pQuery implements pQueryExtension { ...@@ -132,7 +146,7 @@ class pQuerySql extends pQuery implements pQueryExtension {
* @param string $type The format of the result row. * @param string $type The format of the result row.
* @returns mixed The fetched row in the requested format. * @returns mixed The fetched row in the requested format.
*/ */
function fetch($type) { function fetch($type=self::DEFAULT_FETCH_TYPE) {
$this->assert_execution(); $this->assert_execution();
if( !$this->result ) if( !$this->result )
...@@ -152,7 +166,7 @@ class pQuerySql extends pQuery implements pQueryExtension { ...@@ -152,7 +166,7 @@ class pQuerySql extends pQuery implements pQueryExtension {
* @param string $type The format of the result rows. * @param string $type The format of the result rows.
* @returns array The result set. * @returns array The result set.
*/ */
function fetch_all($type) { function fetch_all($type=self::DEFAULT_FETCH_TYPE) {
$results = array(); $results = array();
while( ($row = $this->fetch($type)) !== false ) { while( ($row = $this->fetch($type)) !== false ) {
...@@ -171,6 +185,42 @@ class pQuerySql extends pQuery implements pQueryExtension { ...@@ -171,6 +185,42 @@ class pQuerySql extends pQuery implements pQueryExtension {
$this->executed || $this->execute(); $this->executed || $this->execute();
} }
/**
* Set database server login data.
*
* @param string $host The database server to connect with.
* @param string $username The username to login with on the database server.
* @param string $password The password to login with on the database server.
* @param string $dbname The name of the database to select after connecting to the server.
*/
static function set_login_data($host, $username, $password, $dbname) {
// Close any existing connection
if( self::$link ) {
mysql_close(self::$link);
self::$link = null;
}
self::$login_data = array_merge(self::$login_data,
compact('host', 'username', 'password', 'dbname'));
}
/**
* Assert that the database server config has been set.
*/
static function assert_login_data_exist() {
if( !isset(self::$login_data['host']) )
return self::error('No SQL host specified.');
if( !isset(self::$login_data['username']) )
return self::error('No SQL username specified.');
if( !isset(self::$login_data['password']) )
return self::error('No SQL password specified.');
if( !isset(self::$login_data['host']) )
return self::error('No SQL host specified.');
}
/** /**
* Assert that the MySQL connection is opened. * Assert that the MySQL connection is opened.
* *
...@@ -181,11 +231,10 @@ class pQuerySql extends pQuery implements pQueryExtension { ...@@ -181,11 +231,10 @@ class pQuerySql extends pQuery implements pQueryExtension {
if( self::$link ) if( self::$link )
return; return;
if( !isset(pQueryConfig::$sql) ) self::assert_login_data_exist();
return self::error('Could not connect to database: no MySQL config found.');
// Connect to the database // Connect to the database
$c = pQueryConfig::$sql; $c = self::$login_data;
$link = @mysql_connect($c['host'], $c['username'], $c['password']); $link = @mysql_connect($c['host'], $c['username'], $c['password']);
if( $link === false ) if( $link === false )
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment