From 83c6429c4c68ae5e8396829f6d8cce5002e9c25a Mon Sep 17 00:00:00 2001 From: Deon George Date: Wed, 30 Sep 2015 21:46:03 +1000 Subject: [PATCH] Work in Progress --- classes/Database/Query/Builder/Select.php | 125 ++++++++++++++++++++++ classes/Kohana/Database/DB2.php | 59 +++++++++- config/database.php | 1 + 3 files changed, 182 insertions(+), 3 deletions(-) create mode 100644 classes/Database/Query/Builder/Select.php diff --git a/classes/Database/Query/Builder/Select.php b/classes/Database/Query/Builder/Select.php new file mode 100644 index 0000000..74f5ad8 --- /dev/null +++ b/classes/Database/Query/Builder/Select.php @@ -0,0 +1,125 @@ +_distinct === TRUE) + { + // Select only unique results + $query .= 'DISTINCT '; + } + + if (empty($this->_select)) + { + // Select all columns + $query .= '*'; + } + else + { + // Select all columns + $query .= implode(', ', array_unique(array_map($quote_column, $this->_select))); + } + + if ( ! empty($this->_from)) + { + // Set tables to select from + $query .= ' FROM '.implode(', ', array_unique(array_map($quote_table, $this->_from))); + } + + if ( ! empty($this->_join)) + { + // Add tables to join + $query .= ' '.$this->_compile_join($db, $this->_join); + } + + if ( ! empty($this->_where)) + { + // Add selection conditions + $query .= ' WHERE '.$this->_compile_conditions($db, $this->_where); + } + + if ( ! empty($this->_group_by)) + { + // Add grouping + $query .= ' '.$this->_compile_group_by($db, $this->_group_by); + } + + if ( ! empty($this->_having)) + { + // Add filtering conditions + $query .= ' HAVING '.$this->_compile_conditions($db, $this->_having); + } + + if ( ! empty($this->_order_by)) + { + // Add sorting + $query .= ' '.$this->_compile_order_by($db, $this->_order_by); + } + + if ($this->_limit !== NULL) + { + // Add limiting + $query .= ' FETCH FIRST '.$this->_limit.' ROWS ONLY'; + } + + if ($this->_offset !== NULL) + { + // Add offsets + $query .= ' OFFSET '.$this->_offset; + } + + if ( ! empty($this->_union)) + { + foreach ($this->_union as $u) { + $query .= ' UNION '; + if ($u['all'] === TRUE) + { + $query .= 'ALL '; + } + $query .= $u['select']->compile($db); + } + } + + $this->_sql = $query; + + // Cant run through parent, as it overwrites our LIMIT syntax. + // return parent::compile($db); + return $query; + } +} // End Database_Query_Select +?> diff --git a/classes/Kohana/Database/DB2.php b/classes/Kohana/Database/DB2.php index c699e4e..c31935f 100644 --- a/classes/Kohana/Database/DB2.php +++ b/classes/Kohana/Database/DB2.php @@ -16,8 +16,8 @@ class Kohana_Database_DB2 extends Database { // Identifier for this connection within the PHP driver protected $_connection_id; - // DB2 uses a " for identifiers - protected $_identifier = '"'; + // DB2 doesnt use identifies identifiers + protected $_identifier = ''; // Required Abstract Classes public function begin($mode = NULL) {} @@ -229,6 +229,59 @@ class Kohana_Database_DB2 extends Database { public function quote_column($column) { + // Identifiers are escaped by repeating them + $escaped_identifier = $this->_identifier.$this->_identifier; + + if (is_array($column)) { + list($column, $alias) = $column; + $alias = str_replace($this->_identifier, $escaped_identifier, $alias); + } + + if ($column instanceof Database_Query) { + // Create a sub-query + $column = '('.$column->compile($this).')'; + + } elseif ($column instanceof Database_Expression) { + // Compile the expression + $column = $column->compile($this); + + } else { + // Convert to a string + $column = (string) $column; + + $column = str_replace($this->_identifier, $escaped_identifier, $column); + + if ($column === '*') { + return $column; + + } elseif (strpos($column, '.') !== FALSE) { + $parts = explode('.', $column); + + if ($prefix = $this->table_prefix()) { + // Get the offset of the table name, 2nd-to-last part + $offset = count($parts) - 2; + + // Add the table prefix to the table name + $parts[$offset] = $prefix.$parts[$offset]; + } + + foreach ($parts as & $part) { + if ($part !== '*') { + // Quote each of the parts + $part = $this->_identifier.$part.$this->_identifier; + } + } + + $column = implode('.', $parts); + + } else { + $column = $this->_identifier.$column.$this->_identifier; + } + } + + if (isset($alias)) + $column .= ' AS '.$this->_identifier.$alias.$this->_identifier; + return $column; } @@ -299,7 +352,7 @@ class Kohana_Database_DB2 extends Database { public function table_prefix($schema=FALSE) { - return ($schema === TRUE ? $this->_config['connection']['database'].'.' : '').$this->_config['table_prefix']; + return ($schema === TRUE ? $this->_config['connection']['schema'].'.' : '').$this->_config['table_prefix']; } } ?> diff --git a/config/database.php b/config/database.php index aeb4ed4..d98efa7 100644 --- a/config/database.php +++ b/config/database.php @@ -23,6 +23,7 @@ return array 'password' => '', 'persistent' => FALSE, 'database' => '', + 'schema' => '', ), 'table_prefix' => '', 'charset' => 'utf8',