summaryrefslogtreecommitdiffstats
path: root/modules/database/classes
diff options
context:
space:
mode:
Diffstat (limited to 'modules/database/classes')
-rw-r--r--modules/database/classes/database/query.php17
-rw-r--r--modules/database/classes/db.php7
-rw-r--r--modules/database/classes/driver/mysql/db.php19
-rw-r--r--modules/database/classes/driver/pdo/db.php8
-rw-r--r--modules/database/classes/driver/pdo/query.php6
5 files changed, 48 insertions, 9 deletions
diff --git a/modules/database/classes/database/query.php b/modules/database/classes/database/query.php
index 984949b..a07a7ff 100644
--- a/modules/database/classes/database/query.php
+++ b/modules/database/classes/database/query.php
@@ -148,9 +148,14 @@ abstract class Query_Database {
}
/**
- * Sets fields to be queried from the database
- *
- * @param mixed A single field or an array of them
+ * Sets fields to be queried from the database. You can add aliases to the fields
+ * by passing them as:
+ *
+ * array('field_name','alias')
+ *
+ * Example: $query->fields('id', array('name', 'fairy_name'))
+ *
+ * @param mixed $field,... Fields to be selected from the table
* @return mixed If no parameters are passed returns current array of fields,
* otherwise returns self.
* @access public
@@ -159,10 +164,8 @@ abstract class Query_Database {
$p = func_get_args();
if (empty($p)) {
return $this->_fields;
- }elseif (is_array($p[0])) {
- $this->_fields=$p[0];
- }else {
- $this->_fields=array($p[0]);
+ }else{
+ $this->_fields=$p;
}
return $this;
}
diff --git a/modules/database/classes/db.php b/modules/database/classes/db.php
index dc6934f..0d9ef94 100644
--- a/modules/database/classes/db.php
+++ b/modules/database/classes/db.php
@@ -45,6 +45,13 @@ abstract class DB {
*/
public abstract function get_insert_id();
+ /**
+ * Gets column names for the specified table
+ *
+ * @param string $table Name of the table to get columns from
+ * @return array Array of column names
+ * @access public
+ */
public abstract function list_columns($table);
/**
diff --git a/modules/database/classes/driver/mysql/db.php b/modules/database/classes/driver/mysql/db.php
index e5a63df..ab0a3bb 100644
--- a/modules/database/classes/driver/mysql/db.php
+++ b/modules/database/classes/driver/mysql/db.php
@@ -36,7 +36,24 @@ class DB_Mysql_Driver extends DB{
Config::get("database.{$config}.db")
);
}
-
+
+ /**
+ * Gets column names for the specified table
+ *
+ * @param string $table Name of the table to get columns from
+ * @return array Array of column names
+ * @access public
+ */
+ public function list_columns($table) {
+ $columns=array();
+ $table_desc = $this->execute("DESCRIBE `$table`");
+
+ foreach($table_desc as $column)
+ $columns[] = $column->Field;
+
+ return $columns;
+ }
+
/**
* Builds a new Query implementation
*
diff --git a/modules/database/classes/driver/pdo/db.php b/modules/database/classes/driver/pdo/db.php
index c73995d..b7b869f 100644
--- a/modules/database/classes/driver/pdo/db.php
+++ b/modules/database/classes/driver/pdo/db.php
@@ -61,6 +61,13 @@ class DB_PDO_Driver extends DB{
return $this->conn->lastInsertId();
}
+ /**
+ * Gets column names for the specified table
+ *
+ * @param string $table Name of the table to get columns from
+ * @return array Array of column names
+ * @access public
+ */
public function list_columns($table) {
$columns=array();
if ($this->db_type == 'mysql') {
@@ -80,6 +87,7 @@ class DB_PDO_Driver extends DB{
}
return $columns;
}
+
/**
* Executes a prepared statement query
*
diff --git a/modules/database/classes/driver/pdo/query.php b/modules/database/classes/driver/pdo/query.php
index 647b0c4..f029197 100644
--- a/modules/database/classes/driver/pdo/query.php
+++ b/modules/database/classes/driver/pdo/query.php
@@ -127,7 +127,11 @@ class Query_PDO_Driver extends Query_Database {
}else {
$first = false;
}
- $query.="{$this->escape_field($f)} ";
+ if(is_array($f)){
+ $query.= "{$this->escape_field($f[0])} AS {$f[1]} ";
+ }else {
+ $query.= "{$this->escape_field($f)} ";
+ }
}
}
$query.= "FROM {$this->quote($this->_table)} ";