-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseQuerySelectBabel.php
76 lines (67 loc) · 1.93 KB
/
DatabaseQuerySelectBabel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/**
* A helper module for Babel.
* Adds a ON clause for SQL queries
* and a an OFFSET too.
*
*/
class DatabaseQuerySelectBabel extends DatabaseQuerySelect {
/**
* Setup the components of a SELECT query
*
*/
public function __construct() {
$this->set('select', array());
$this->set('join', array());
$this->set('from', array());
$this->set('leftjoin', array());
$this->set('on', array());
$this->set('where', array());
$this->set('orderby', array());
$this->set('groupby', array());
$this->set('limit', array());
$this->set('offset', array());
$this->set('comment', '');
}
/**
* Return the resulting SQL ready for execution with the database
*
*/
public function getQuery() {
$sql = $this->getQuerySelect() .
$this->getQueryFrom() .
$this->getQueryJoin($this->join, "JOIN") .
$this->getQueryJoin($this->leftjoin, "LEFT JOIN") .
$this->getQueryOn() .
$this->getQueryWhere() .
$this->getQueryGroupby() .
$this->getQueryOrderby() .
$this->getQueryLimit() .
$this->getQueryOffset();
if($this->get('comment') && $this->wire('config')->debug) {
// NOTE: PDO thinks ? and :str param identifiers in /* comments */ are real params
// so we str_replace them out of the comment, and only support comments in debug mode
$comment = str_replace(array('*/', '?', ':'), '', $this->comment);
$sql .= "/* $comment */";
}
return $sql;
}
/**
* Get the ON portion of the query
*
*/
protected function getQueryOn() {
if(!count($this->on)) return '';
$on = $this->on;
$sql = "\nON (" . array_shift($on);
foreach($on as $n) $sql .= " AND $n";
$sql .= ") ";
return $sql;
}
protected function getQueryOffset() {
if(!count($this->offset)) return '';
$offset = $this->offset;
$sql = "\nOFFSET " . reset($offset) . " ";
return $sql;
}
}