Skip to content
This repository has been archived by the owner on Aug 31, 2022. It is now read-only.

[fix] [ugly] use only one connection to db #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 43 additions & 42 deletions src/querybuilder.php
Original file line number Diff line number Diff line change
@@ -1,57 +1,58 @@
<?php namespace Elegant;
<?php

namespace Elegant;

class QueryBuilder {

protected $db_conn = null;
protected $table = '';
protected $db_conn = null;
protected $table = '';

function __construct($db_group, $table) {
global $__CI;
if (!isset($__CI)) {
$__CI =& get_instance();
}

function __construct($db_group, $table)
{
$ci =& get_instance();

$this->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;
}
}
}