Skip to content

Commit

Permalink
Update class.SmartDB.php
Browse files Browse the repository at this point in the history
  • Loading branch information
hanif-mianjee committed May 1, 2014
1 parent ee030c1 commit 5ed6b32
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions class.SmartDB.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @name Smart DB Class
* @author Muhammad Hanif <[email protected]>
Expand Down Expand Up @@ -42,7 +43,7 @@ public function __construct() {
* @param mix $value Value to match in column
* @return none
*/
public function where($column_name, $value, $operator = '='){
public function where($column_name, $value, $operator = '=') {
$this->_where[$column_name] = array();
$this->_where[$column_name][] = $value;
$this->_where[$column_name][] = $operator;
Expand All @@ -69,13 +70,13 @@ public function bind($data) {
where($key, $value);
}
}

/**
* Returns the ID of the last inserted row or sequence value
* @param strinf $name Name of the sequence object from which the ID should be returned.
* @return Int
*/
public function last_id($name = NULL){
public function last_id($name = NULL) {
return $this->_DB->lastInsertId($name);
}

Expand All @@ -93,12 +94,12 @@ public function query($query) {
} catch (PDOException $e) {
$er = $e->errorInfo;
echo '<p style="font-family:arial; background:#F00; color:#FFF; padding:5px;">ERROR: ' . $er[2] . '</p>';
}
}
}

/**
* Bind params and data values
* @return none
* @return none
*/
private function bind_results() {
if (!empty($this->_where)) {
Expand All @@ -109,7 +110,7 @@ private function bind_results() {
if (!empty($this->_data)) {
$i = 0;
foreach ($this->_data as $c => $value) {
$this->_query->bindValue(':' . $c . $i, $value[0], $this->get_type($value[0]));
$this->_query->bindValue(':' . $c . $i, $value, $this->get_type($value));
$i++;
}
}
Expand All @@ -134,7 +135,7 @@ private function get_type($value) {
}
return $param;
}

/**
* Create JOIN statment
* @param String $table_name
Expand All @@ -145,11 +146,11 @@ private function get_type($value) {
*/
private function join($table_name, $override_name, $join_type = 'INNER JOIN', $match = array(), $select = NULL) {
$conditions = array();
foreach($match as $key1 => $key2){
$conditions[] = $key1.'='.$key2;
foreach ($match as $key1 => $key2) {
$conditions[] = $key1 . '=' . $key2;
}
$this->select($select);
$this->_joins[]= $join_type.' '.$table_name.' AS '.$override_name.' ON '. implode(' AND ', $conditions);
$this->_joins[] = $join_type . ' ' . $table_name . ' AS ' . $override_name . ' ON ' . implode(' AND ', $conditions);
}

/**
Expand All @@ -162,7 +163,7 @@ private function join($table_name, $override_name, $join_type = 'INNER JOIN', $m
public function inner_join($table_name, $override_name, $match = array(), $select = NULL) {
$this->join($table_name, $override_name, 'INNER JOIN', $match, $select);
}

/**
* Create LEFT JOIN statement
* @param String $table_name
Expand All @@ -173,7 +174,7 @@ public function inner_join($table_name, $override_name, $match = array(), $selec
public function left_join($table_name, $override_name, $match = array(), $select = NULL) {
$this->join($table_name, $override_name, 'LEFT JOIN', $match, $select);
}

/**
* Create RIGHT JOIN statement
* @param String $table_name
Expand Down Expand Up @@ -208,8 +209,8 @@ private function build_query() {
$query .= ' INTO';
}
$query .= $this->_table;
if(!empty($this->_joins)){

if (!empty($this->_joins)) {
$query .= ' ';
$query .= implode(' ', $this->_joins);
}
Expand All @@ -221,7 +222,7 @@ private function build_query() {
$query .= ' WHERE ';
$columns = array();
foreach ($this->_where as $c => $v) {
$columns[] = $c . ' '. $v[1] .':' . $c;
$columns[] = $c . ' ' . $v[1] . ':' . $c;
}
$query .= implode(' AND ', $columns);
}
Expand Down Expand Up @@ -254,7 +255,7 @@ private function build_update_data() {
$fields = '';
$i = 0;
foreach ($this->_data as $c => $v) {
$fields .= $c . " " .$v[1]. ":" . $c . $i . ", ";
$fields .= $c . " " . $v[1] . ":" . $c . $i . ", ";
$i++;
}
return rtrim($fields, ', ');
Expand Down Expand Up @@ -326,12 +327,14 @@ public function update($table_name, $data) {

/**
* Delete row
* @param String $table_name
* @return none
* @param String $table_name
* @param Int $num_rows How many rows to select
* @return none
*/
public function delete($table_name) {
public function delete($table_name, $num_rows = NULL) {
$this->_action = 'DELETE';
$this->_table = ' ' . $table_name;
$this->_limit = $num_rows;
$this->build_query();
}

Expand Down

0 comments on commit 5ed6b32

Please sign in to comment.