-
Notifications
You must be signed in to change notification settings - Fork 0
/
DpleQuery.php
181 lines (156 loc) · 4.13 KB
/
DpleQuery.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
if ( !defined( 'MEDIAWIKI' ) ) {
echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" );
die( 1 );
}
/**
* @brief Class DpleQuery.
*
* @file
*
* @ingroup Extensions
* @ingroup Extensions-DynamicPageListEngine
*
* @author [RV1971](http://www.mediawiki.org/wiki/User:RV1971)
*
*/
/**
* @brief Database query specification and result.
*
* This class is simply a wrapper for the parameters and the result of
* DatabaseBase::select().
*
* @ingroup Extensions
* @ingroup Extensions-DynamicPageListEngine
*/
class DpleQuery implements Countable {
/* == private data members == */
private $dbr_; ///< DatabaseBase object.
private $tables_; ///< Tables for DatabaseBase::select().
private $vars_; ///< Fields for DatabaseBase::select().
private $conds_; ///< WHERE conditions for DatabaseBase::select().
private $options_; ///< Options for DatabaseBase::select().
private $joinConds_; ///< Join conditions for DatabaseBase::select().
private $result_; ///< ResultWrapper containing the query result.
/* == magic methods == */
/// Constructor.
public function __construct( $tables = null, $vars = null, $conds = null,
$options = null, $joinConds = null ) {
/** Get a database object. */
$this->dbr_ = wfGetDB( DB_SLAVE );
/** Initialize the class members with the arguments. */
$this->tables_ = (array)$tables;
$this->vars_ = (array)$vars;
$this->conds_ = (array)$conds;
$this->options_ = (array)$options;
$this->joinConds_ = (array)$joinConds;
}
/**
* @brief Implementation of Countable::count.
*
* @return int Number of result rows, or 0 if the query has not
* yet been executed.
*/
public function count() {
if ( isset( $this->result_ ) ) {
return $this->result_->numRows();
} else {
return 0;
}
}
/* == accessors == */
/// Get @ref $dbr_.
public function getDbr() {
return $this->dbr_;
}
/// Get @ref $tables_.
public function getTables() {
return $this->tables_;
}
/// Get @ref $vars_.
public function getVars() {
return $this->vars_;
}
/// Get @ref $conds_.
public function getConds() {
return $this->conds_;
}
/// Get @ref $options_.
public function getOptions() {
return $this->options_;
}
/// Get @ref $joinConds_.
public function getJoinConds() {
return $this->joinConds_;
}
/// Get @ref $result_.
public function getResult() {
return $this->result_;
}
/* == mutators == */
/**
* @brief Add to @ref $tables_.
*
* @param string|array $tables Additional tables to query.
*/
public function addTables( $tables ) {
$this->tables_ = array_merge( $this->tables_, (array)$tables );
}
/**
* @brief Add to @ref $vars_.
*
* @param string|array $vars Additional fields to query.
*/
public function addVars( $vars ) {
$this->vars_ = array_merge( $this->vars_, (array)$vars );
}
/**
* @brief Add to @ref $conds_.
*
* @param string|array $conds Additional WHERE conditions.
*/
public function addConds( $conds ) {
$this->conds_ = array_merge( $this->conds_, (array)$conds );
}
/**
* @brief Set an option in $_option.
*
* @param string $key Option key. To set options which do not have
* keys (such as DISTINCT), pass the option as $key and do not
* pass $value.
*
* @param mixed $value Option value.
*/
public function setOption( $key, $value = NULL ) {
if ( isset( $value ) ) {
$this->options_[$key] = $value;
} else {
$this->options_[] = $key;
}
}
/**
* @brief Add to @ref $joinConds_.
*
* @param string $table Table name or alias.
*
* @param string $joinType. `INNER JOIN`, `LEFT OUTER JOIN` etc.
*
* @param string|array $conds Join conditions.
*
* @param string|array $joinConds Additional JOIN conditions.
*/
public function addJoinCond( $table, $joinType, $conds ) {
$this->joinConds_[$table] = array( $joinType, $conds );
}
/* == operations == */
/**
* @brief Execute the query and store the result in $result_.
*
* @return ResultWrapper $result_.
*/
public function execute( $fname = __METHOD__ ) {
return $this->result_ = $this->dbr_->select(
$this->tables_, $this->vars_, $this->conds_, $fname,
$this->options_, $this->joinConds_ );
}
}