Explorar el Código

Removed config file and checnged method name.

Taddeus Kroes hace 14 años
padre
commit
9468e05bb3
Se han modificado 2 ficheros con 64 adiciones y 10 borrados
  1. 8 3
      pquery.php
  2. 56 7
      pquery.sql.php

+ 8 - 3
pquery.php

@@ -5,12 +5,17 @@
  * @package pQuery
  */
 
-include_once 'pquery.config.php';
-
 /**
  * Common utility class.
  */
 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.
 	 * 
@@ -153,7 +158,7 @@ class pQuery {
 		$files = func_get_args();
 		
 		foreach( $files as $basename ) {
-			$path = PQUERY_ROOT.pQueryConfig::UTILS_FOLDER.$basename.'.php';
+			$path = PQUERY_ROOT.self::UTILS_FOLDER.$basename.'.php';
 			
 			if( !file_exists($path) ) {
 				return self::error('Utility "%s" could not be loaded (looked in "%s").',

+ 56 - 7
pquery.sql.php

@@ -14,6 +14,13 @@ class pQuerySql extends pQuery implements pQueryExtension {
 	
 	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.
 	 * 
@@ -27,6 +34,14 @@ class pQuerySql extends pQuery implements pQueryExtension {
 	 */
 	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.
 	 * 
@@ -91,7 +106,7 @@ class pQuerySql extends pQuery implements pQueryExtension {
 	 * @param array $variables The variables to replace.
 	 * @returns pQuerySql The current query object.
 	 */
-	function set_plain($variables) {
+	function set_unescaped($variables) {
 		return $this->replace_variables($variables, false);
 	}
 	
@@ -114,7 +129,6 @@ class pQuerySql extends pQuery implements pQueryExtension {
 	function execute() {
 		self::assert_connection();
 		
-		//debug('query:', $this->query);
 		$result = mysql_query($this->query, self::$link);
 		
 		if( !$result )
@@ -132,7 +146,7 @@ class pQuerySql extends pQuery implements pQueryExtension {
 	 * @param string $type The format of the result row.
 	 * @returns mixed The fetched row in the requested format.
 	 */
-	function fetch($type) {
+	function fetch($type=self::DEFAULT_FETCH_TYPE) {
 		$this->assert_execution();
 		
 		if( !$this->result )
@@ -152,7 +166,7 @@ class pQuerySql extends pQuery implements pQueryExtension {
 	 * @param string $type The format of the result rows.
 	 * @returns array The result set.
 	 */
-	function fetch_all($type) {
+	function fetch_all($type=self::DEFAULT_FETCH_TYPE) {
 		$results = array();
 		
 		while( ($row = $this->fetch($type)) !== false ) {
@@ -171,6 +185,42 @@ class pQuerySql extends pQuery implements pQueryExtension {
 		$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.
 	 * 
@@ -181,11 +231,10 @@ class pQuerySql extends pQuery implements pQueryExtension {
 		if( self::$link )
 			return;
 		
-		if( !isset(pQueryConfig::$sql) )
-			return self::error('Could not connect to database: no MySQL config found.');
+		self::assert_login_data_exist();
 		
 		// Connect to the database
-		$c = pQueryConfig::$sql;
+		$c = self::$login_data;
 		$link = @mysql_connect($c['host'], $c['username'], $c['password']);
 		
 		if( $link === false )