From 7b03cd4332ba92708e1868ca7b608c43c65c9698 Mon Sep 17 00:00:00 2001 From: Yavor Kirov Date: Thu, 8 May 2014 12:25:54 +0300 Subject: [PATCH] [fix] [ugly] use only one connection to db I downloaded your library and I liked it a lot. Sadly it was making 1 db connection for each model call. So my web host kept returning "too many connections to db" error. So I made this (yes, it's ugly) fix to make it work with only one connection. I'd love to update it in my project if you have a prettier solution. --- src/querybuilder.php | 85 ++++++++++++++++++++++---------------------- 1 file changed, 43 insertions(+), 42 deletions(-) diff --git a/src/querybuilder.php b/src/querybuilder.php index d18fac0..20d9270 100644 --- a/src/querybuilder.php +++ b/src/querybuilder.php @@ -1,57 +1,58 @@ -db_conn = $ci->load->database($db_group, true); + $this->db_conn = $__CI->db; - $this->table = $table; - $this->db_conn->from( $this->table ); + $this->table = $table; + $this->db_conn->from($this->table); - if(defined('ELEGANT_DEBUG') and ELEGANT_DEBUG === true) - { - $property_name = 'elegant_db_'.rand(); - $ci->{$property_name} = $this->db_conn; - } - } +// if (defined('ELEGANT_DEBUG') and ELEGANT_DEBUG === true) { +//// $property_name = 'elegant_db_' . rand(); +//// $ci->asd = $this->db_conn; +// } + } - function __call($name, $arguments) - { - if(!method_exists($this->db_conn, $name)) return show_error('Unknown function '.$name, 500); + function __call($name, $arguments) { + if (!method_exists($this->db_conn, $name)) + return show_error('Unknown function ' . $name, 500); - return call_user_func_array( array($this->db_conn, $name), $arguments ); - } + return call_user_func_array(array($this->db_conn, $name), $arguments); + } - function __clone() - { - $this->db_conn = clone $this->db_conn; - } + function select($columns = array()) { + if (empty($columns)) + $columns = '*'; - function select($columns = array()) - { - if (empty($columns)) $columns = '*'; + elseif (is_array($columns)) + $columns = implode(', ', $columns); - elseif (is_array($columns)) $columns = implode(', ', $columns); + $this->db_conn->select($columns); + } - $this->db_conn->select($columns); - } + function insert($data) { + $insert = $this->db_conn->insert($this->table, $data); + return ($insert !== false) ? $this->db_conn->insert_id() : false; + } - function insert($data) - { - $insert = $this->db_conn->insert( $this->table, $data ); - return ($insert !== false) ? $this->db_conn->insert_id() : false; - } + function update($data, $where = array()) { + if (empty($where) and ! is_array($where)) + $where = array(); - function update($data, $where = array()) - { - if(empty($where) and !is_array($where)) $where = array(); + $update = $this->db_conn->update($this->table, $data, $where); + return $update; + } - $update = $this->db_conn->update( $this->table, $data, $where ); - return $update; - } -} \ No newline at end of file +}