Skip to content

Commit

Permalink
Code optimization and SQL Join method examples added.
Browse files Browse the repository at this point in the history
  • Loading branch information
izniburak committed Mar 1, 2016
1 parent 01ee96a commit 65351e8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
3 changes: 1 addition & 2 deletions src/Cache.php
Expand Up @@ -14,7 +14,7 @@

class Cache
{
private $cacheDir = null;
private $cacheDir = null;
private $cache = null;
private $finish = null;

Expand Down Expand Up @@ -58,7 +58,6 @@ public function getCache($sql, $array = false)
unlink($cacheFile);
return;
}

else
return ($array ? $cache['data'] : $cache->data);
}
Expand Down
22 changes: 1 addition & 21 deletions src/Pdox.php
Expand Up @@ -78,7 +78,6 @@ public function select($select)
{
if(is_array($select))
$this->select = implode(', ', $select);

else
$this->select = $select;

Expand All @@ -95,7 +94,6 @@ public function table($from)

$this->from = rtrim($f, ', ');
}

else
$this->from = $this->prefix . $from;

Expand All @@ -115,11 +113,9 @@ public function join($table, $field1, $op = null, $field2 = null, $join = '')

if (is_null($this->join))
$this->join = ' ' . $q;

else
$this->join = $this->join . ' ' . $q;
}

else
{
$where = $field1;
Expand All @@ -130,7 +126,6 @@ public function join($table, $field1, $op = null, $field2 = null, $join = '')

if (is_null($this->join))
$this->join = ' ' . $join . 'JOIN' . ' ' . $table . ' ON ' . $where;

else
$this->join = $this->join . ' ' . $join . 'JOIN' . ' ' . $table . ' ON ' . $where;
}
Expand Down Expand Up @@ -183,10 +178,8 @@ public function where($where, $op = null, $val = null, $ao = 'AND')

$where = $w;
}

elseif (!in_array($op, $this->op))
$where = $where . ' = ' . $this->escape($op);

else
$where = $where . ' ' . $op . ' ' . $this->escape($val);
}
Expand All @@ -199,7 +192,6 @@ public function where($where, $op = null, $val = null, $ao = 'AND')

if (is_null($this->where))
$this->where = $where;

else
$this->where = $this->where . ' '.$ao.' ' . $where;

Expand Down Expand Up @@ -235,7 +227,6 @@ public function in($field, Array $keys, $not = '', $ao = 'AND')

if (is_null($this->where))
$this->where = $field . ' ' . $not . 'IN (' . $keys . ')';

else
$this->where = $this->where . ' ' . $ao . ' ' . $field . ' '.$not.'IN (' . $keys . ')';
}
Expand Down Expand Up @@ -268,7 +259,6 @@ public function between($field, $value1, $value2, $not = '', $ao = 'AND')
{
if (is_null($this->where))
$this->where = $field . ' ' . $not . 'BETWEEN ' . $this->escape($value1) . ' AND ' . $this->escape($value2);

else
$this->where = $this->where . ' ' . $ao . ' ' . $field . ' ' . $not . 'BETWEEN ' . $this->escape($value1) . ' AND ' . $this->escape($value2);

Expand Down Expand Up @@ -302,18 +292,15 @@ public function like($field, $data, $type = '%-%', $ao = 'AND')

if($type == '-%')
$like = $data.'%';

elseif($type == '%-')
$like = '%'.$data;

else
$like = '%'.$data.'%';

$like = $this->escape($like);

if (is_null($this->where))
$this->where = $field . ' LIKE ' . $like;

else
$this->where = $this->where . ' '.$ao.' ' . $field . ' LIKE ' . $like;

Expand All @@ -331,7 +318,6 @@ public function limit($limit, $limitEnd = null)
{
if (!is_null($limitEnd))
$this->limit = $limit . ', ' . $limitEnd;

else
$this->limit = $limit;

Expand All @@ -342,12 +328,10 @@ public function orderBy($orderBy, $order_dir = null)
{
if (!is_null($order_dir))
$this->orderBy = $orderBy . ' ' . strtoupper($order_dir);

else
{
if(stristr($orderBy, ' ') || $orderBy == 'rand()')
$this->orderBy = $orderBy;

else
$this->orderBy = $orderBy . ' ASC';
}
Expand All @@ -359,7 +343,6 @@ public function groupBy($groupBy)
{
if(is_array($groupBy))
$this->groupBy = implode(', ', $groupBy);

else
$this->groupBy = $groupBy;

Expand All @@ -382,7 +365,6 @@ public function having($field, $op = null, $val = null)

elseif (!in_array($op, $this->op))
$this->having = $field . ' > ' . $this->escape($op);

else
$this->having = $field . ' ' . $op . ' ' . $this->escape($val);

Expand Down Expand Up @@ -560,7 +542,6 @@ public function query($query, $all = true, $array = false)

$this->result = $q[0];
}

else
{
$q = ($array == false) ? $sql->fetch(\PDO::FETCH_OBJ) : $sql->fetch(\PDO::FETCH_ASSOC);
Expand Down Expand Up @@ -597,7 +578,6 @@ public function query($query, $all = true, $array = false)
return $this->error();
}
}

else
{
$this->cache = null;
Expand Down Expand Up @@ -655,4 +635,4 @@ function __destruct()
{
$this->pdo = null;
}
}
}
32 changes: 32 additions & 0 deletions tests/JoinTest.php
@@ -0,0 +1,32 @@
<?php

require 'vendor/autoload.php';

$config = [
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'test',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
'prefix' => ''
];

$db = new \Buki\Pdox($config);

$records = $db->table('authors')
->select('authors.name, articles.title, articles.slug')
->join('articles', 'users.id', 'articles.user_id')
->where('users.status', 1)
->where('articles.status', 1)
->orderBy('articles.created_at', 'desc')
->limit(10)
->getAll();

foreach($records as $record)
{
echo $record->name . '<br />' .
$record->title . '<br />' .
$record->slug;
}

0 comments on commit 65351e8

Please sign in to comment.