1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The Prepare Query builder Class. |
4
|
|
|
* |
5
|
|
|
* @author RN Kushwaha <[email protected]> |
6
|
|
|
* @since v0.0.8 <Date: 29th Dec, 2019> |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Dolphin\Builders; |
10
|
|
|
|
11
|
|
|
use Dolphin\Connections\Connection; |
12
|
|
|
use Dolphin\Parsers\WhereQueryParser; |
13
|
|
|
use Dolphin\Utils\Utils; |
14
|
|
|
use \Exception; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* This class provides the mechanism to build the Queries. |
18
|
|
|
*/ |
19
|
|
|
class PrepareQueryBuilder extends QueryBuilder |
20
|
|
|
{ |
21
|
|
|
private function checkCountable($results = null ): bool{ |
22
|
|
|
return (is_array($results) || is_object($results)) && count($results); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* It prepared and execute queries |
27
|
|
|
* |
28
|
|
|
* @param array $rows |
29
|
|
|
* @return boolean |
30
|
|
|
* @throws Exception |
31
|
|
|
* @author RN Kushwaha <[email protected]> |
32
|
|
|
* @since v0.0.5 |
33
|
|
|
*/ |
34
|
|
|
public function prepare($where, $className, $query, $fetchRows = 'all') |
35
|
|
|
{ |
36
|
|
|
$wqp = new WhereQueryParser(); |
37
|
|
|
$util = new Utils(); |
38
|
|
|
$rows = null; |
39
|
|
|
|
40
|
|
|
try { |
41
|
|
|
$ar = $wqp->parseWhereQuery($where); |
42
|
|
|
$stmt = Connection::get()->prepare($this->queryPrefix($query)); |
43
|
|
|
$stmt->execute($ar); |
44
|
|
|
|
45
|
|
|
if ($fetchRows == 'first') { |
46
|
|
|
$results = $stmt->fetch(\PDO::FETCH_ASSOC); |
47
|
|
|
} else{ |
48
|
|
|
$results = $stmt->fetchAll(\PDO::FETCH_ASSOC); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if($this->checkCountable($results) ){ |
52
|
|
|
// now turn this stdClass object to the object type of calling model |
53
|
|
|
$rows = $util->turnObjects($className, $results); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $rows; |
|
|
|
|
57
|
|
|
} catch (\PDOException $ex) { |
58
|
|
|
throw new \PDOException($ex->getMessage(), 1); |
59
|
|
|
} catch (Exception $e) { |
60
|
|
|
throw new Exception($e->getMessage(), 1); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|