Skip to content

Commit

Permalink
Added new methods (whereNull, whereNotNull), Updated query method, up…
Browse files Browse the repository at this point in the history
…dated documentation and code optimizaiton.
  • Loading branch information
izniburak committed Sep 8, 2018
1 parent a62decd commit 047677b
Show file tree
Hide file tree
Showing 3 changed files with 362 additions and 249 deletions.
36 changes: 32 additions & 4 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ $db = new \Buki\Pdox($config);
* [groupBy](#groupby)
* [having](#having)
* [orderBy](#orderby)
* [limit](#limit)
* [limit](#limit-offset)
* [pagination](#pagination)
* [insert](#insert)
* [update](#update)
* [delete](#delete)
Expand Down Expand Up @@ -239,6 +240,8 @@ You can use this method in 4 ways. These;
- orWhere
- notWhere
- orNotWhere
- whereNull
- whereNotNull

Example:
```php
Expand All @@ -249,6 +252,9 @@ $db->table('test')->where('active', 1)->notWhere('auth', 1)->getAll();

$db->table('test')->where('age', 20)->orWhere('age', '>', 25)->getAll();
# Output: "SELECT * FROM test WHERE age = '20' OR age > '25'"

$db->table('test')->whereNotNull('email')->getAll();
# Output: "SELECT * FROM test WHERE email IS NOT NULL"
```

### grouped
Expand Down Expand Up @@ -387,7 +393,7 @@ $db->table('test')->where('status', 1)->orderBy('rand()')->limit(10)->getAll();
# Output: "SELECT * FROM test WHERE status='1' ORDER BY rand() LIMIT 10"
```

### limit
### limit - offset
```php
# Usage 1: One parameter
$db->table('test')->limit(10)->getAll();
Expand All @@ -397,6 +403,22 @@ $db->table('test')->limit(10)->getAll();
# Usage 2: Two parameters
$db->table('test')->limit(10, 20)->getAll();
# Output: "SELECT * FROM test LIMIT 10, 20"

# Usage 3: with offset method
$db->table('test')->limit(10)->offset(10)->getAll();
# Output: "SELECT * FROM test LIMIT 10 OFFSET 10"
```

### pagination
```php
# First parameter: Data count of per page
# Second parameter: Active page

$db->table('test')->pagination(15, 1)->getAll();
# Output: "SELECT * FROM test LIMIT 15 OFFSET 0"

$db->table('test')->pagination(15, 2)->getAll();
# Output: "SELECT * FROM test LIMIT 15 OFFSET 15"
```

### insert
Expand Down Expand Up @@ -483,8 +505,14 @@ $db->table(['users', 'pages'])->repair();

### query
```php
$db->query('SELECT * FROM test WHERE id=? AND status=?', [10, 1]);
# Output: "SELECT * FROM test WHERE id='10' AND status='1'"
# Usage 1: Select all records
$db->query('SELECT * FROM test WHERE id=? AND status=?', [10, 1])->fetchAll();

# Usage 2: Select one record
$db->query('SELECT * FROM test WHERE id=? AND status=?', [10, 1])->fetch();

# Usage 3: Other queries like Update, Insert, Delete etc...
$db->query('DELETE FROM test WHERE id=?', [10])->exec();
```

### insertId
Expand Down
31 changes: 17 additions & 14 deletions src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@

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

function __construct($dir = null, $time = 0)
{
if(!file_exists($dir))
if (! file_exists($dir)) {
mkdir($dir, 0755);
}

$this->cacheDir = $dir;
$this->cache = $time;
Expand All @@ -29,40 +30,42 @@ function __construct($dir = null, $time = 0)

public function setCache($sql, $result)
{
if (is_null($this->cache))
if (is_null($this->cache)) {
return false;
}

$cacheFile = $this->cacheDir . $this->fileName($sql) . '.cache';
$cacheFile = fopen($cacheFile, 'w');

if($cacheFile)
fputs($cacheFile, json_encode(["data" => $result, "finish" => $this->finish]));
if ($cacheFile) {
fputs($cacheFile, json_encode(['data' => $result, 'finish' => $this->finish]));
}

return;
}

public function getCache($sql, $array = false)
{
if (is_null($this->cache))
if (is_null($this->cache)) {
return false;
}

$cacheFile = $this->cacheDir . $this->fileName($sql) . ".cache";

$cacheFile = $this->cacheDir . $this->fileName($sql) . '.cache';
if (file_exists($cacheFile)) {
$cache = json_decode(file_get_contents($cacheFile), $array);

if (($array ? $cache["finish"] : $cache->finish) < time()) {
if (($array ? $cache['finish'] : $cache->finish) < time()) {
unlink($cacheFile);
return;
}
else
return ($array ? $cache["data"] : $cache->data);

return ($array ? $cache['data'] : $cache->data);
}

return false;
}

private function fileName($name)
protected function fileName($name)
{
return md5($name);
}
Expand Down
Loading

0 comments on commit 047677b

Please sign in to comment.