From c4f1fd3e0d1c29e4f56f7fc3abb40b4ef0ce94e4 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 9 Mar 2012 08:04:47 -0500 Subject: [PATCH] Query Builder select method --- sys/db/db_pdo.php | 10 ++++++++- sys/db/drivers/mysql.php | 8 ++++++++ sys/db/query_builder.php | 44 +++++++++++++++++++++++++++++++++------- 3 files changed, 54 insertions(+), 8 deletions(-) diff --git a/sys/db/db_pdo.php b/sys/db/db_pdo.php index dcd0e11..06ecc29 100644 --- a/sys/db/db_pdo.php +++ b/sys/db/db_pdo.php @@ -155,11 +155,19 @@ abstract class DB_PDO extends PDO { /** * Surrounds the string with the databases identifier escape characters * - * @param string $ident + * @param mixed $ident * @return string */ public function quote_ident($ident) { + if (is_array($ident)) + { + for($i = 0, $count = count($ident); $i < $count; $i++) + { + $ident[$i] = $this->quote_ident($ident[$i]); + } + } + // Split each identifier by the period $hiers = explode('.', $ident); diff --git a/sys/db/drivers/mysql.php b/sys/db/drivers/mysql.php index ad845d0..80f5fe3 100644 --- a/sys/db/drivers/mysql.php +++ b/sys/db/drivers/mysql.php @@ -134,6 +134,14 @@ class MySQL extends DB_PDO { */ public function quote_ident($ident) { + if (is_array($ident)) + { + for($i = 0, $count = count($ident); $i < $count; $i++) + { + $ident[$i] = $this->quote_ident($ident[$i]); + } + } + // Split each identifier by the period $hiers = explode('.', $ident); diff --git a/sys/db/query_builder.php b/sys/db/query_builder.php index f6cd0a4..70028f9 100644 --- a/sys/db/query_builder.php +++ b/sys/db/query_builder.php @@ -18,7 +18,7 @@ */ class Query_Builder { - private $table, $where_array, $sql; + private $table, $where_array, $sql, $select_string; /** * Constructor @@ -79,15 +79,19 @@ class Query_Builder { { $result = $this->db->query($sql); } - else + elseif ( ! empty($this->select_string)) { - $sql = $this->sql->limit($sql, $limit, $offset); - $result = $this->db->query($sql); + // Replace the star with the selected fields + $sql = str_replace('*', $this->select_string, $sql); } - //echo $sql."
"; + // Set the limit, if it exists + if ($limit !== FALSE) + { + $sql = $this->sql->limit($sql, $limit, $offset); + } - return $result; + return $this->db->query($sql); } // -------------------------------------------------------------------------- @@ -100,7 +104,33 @@ class Query_Builder { */ public function select($fields) { - // @todo Implement select method + // Split fields by comma + $fields_array = explode(",", $fields); + $fields_array = array_map('trim', $fields_array); + + // Split on 'As' + foreach ($fields_array as $key => $field) + { + if (stripos($field, 'as') !== FALSE) + { + $fields_array[$key] = preg_split('`as`i', $field); + } + } + + // Quote the identifiers + $safe_array = array_map(array($this->db, 'quote_ident'), $fields_array); + + // Join the strings back together + for($i = 0, $c = count($safe_array); $i < $count; $i++) + { + if (is_array($safe_array[$i]) + { + $safe_array[$i] = implode(' AS ', $safe_array[$i]); + } + } + + $this->select_string = implode(', ', $safe_array); + return $this; }