1 | <?php
|
---|
2 | require_once RDFAPI_INCLUDE_DIR . 'sparql/SparqlEngineDb/Offsetter.php';
|
---|
3 |
|
---|
4 | /**
|
---|
5 | * This class takes care of prepared statements:
|
---|
6 | * Preparing them in the database, replacing
|
---|
7 | *
|
---|
8 | * @author Christian Weiske <cweiske@cweiske.de>
|
---|
9 | * @license http://www.gnu.org/licenses/lgpl.html LGPL
|
---|
10 | *
|
---|
11 | * @package sparql
|
---|
12 | */
|
---|
13 | class SparqlEngineDb_Preparator
|
---|
14 | {
|
---|
15 |
|
---|
16 |
|
---|
17 |
|
---|
18 | public function __construct(Query $query, ADOConnection $dbConn)
|
---|
19 | {
|
---|
20 | $this->query = $query;
|
---|
21 | $this->dbConn = $dbConn;
|
---|
22 | $this->arPrefixes = $this->query->getPrefixes();
|
---|
23 | }//public function __construct(Query $query, ADOConnection $dbConn)
|
---|
24 |
|
---|
25 |
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Converts the given queries into sql prepared statments,
|
---|
29 | * calls the prepare command in the database and returns
|
---|
30 | * an array consisting of subarrays. They contain
|
---|
31 | * the db's prepared statement as first value, and an array
|
---|
32 | * of variable positions as second value (key is the position,
|
---|
33 | * the sparql variable is the value).
|
---|
34 | *
|
---|
35 | * @param array $arQueries Array of sql queries part arrays
|
---|
36 | * @param array $arPlaceholders Array of sparql (variable name =>
|
---|
37 | * placeholder name) pairs
|
---|
38 | * @return array Array of (prepared statment, variable positions) pairs
|
---|
39 | */
|
---|
40 | public function prepareInDb($arQueries, $arPlaceholders)
|
---|
41 | {
|
---|
42 | $arDbStatements = array();
|
---|
43 |
|
---|
44 | foreach ($arQueries as $arQuery) {
|
---|
45 | list($strPrepared, $arVariablePositions) = $this->replacePlaceholders(
|
---|
46 | implode('', $arQuery),
|
---|
47 | $arPlaceholders
|
---|
48 | );
|
---|
49 | if (count($arQueries) == 1) {
|
---|
50 | //I currently haven't seens one case in which the count was > 1
|
---|
51 | //if that happens, we will need to add a fix
|
---|
52 | $strPrepared .= SparqlEngineDb_Offsetter::getLimitSql(
|
---|
53 | $this->query,
|
---|
54 | $this->dbConn
|
---|
55 | );
|
---|
56 | }
|
---|
57 | $stmt = $this->dbConn->Prepare($strPrepared);
|
---|
58 | $arDbStatements[] = array(
|
---|
59 | $stmt,
|
---|
60 | $arVariablePositions
|
---|
61 | );
|
---|
62 | }
|
---|
63 |
|
---|
64 | return $arDbStatements;
|
---|
65 | }//public function prepareInDb($arQueries, $arPlaceholders)
|
---|
66 |
|
---|
67 |
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Replaces the placeholders in the given SQL statement with real
|
---|
71 | * SQL prepared statements placeholders.
|
---|
72 | *
|
---|
73 | * @param string $strQuery SQL query with placeholders
|
---|
74 | * @param array $arPlaceholders Array of sparql (variable name =>
|
---|
75 | * placeholder name) pairs
|
---|
76 | * @return array (prepared sql query string, variable positions) pair
|
---|
77 | */
|
---|
78 | protected function replacePlaceholders($strQuery, $arPlaceholders)
|
---|
79 | {
|
---|
80 | $arVariablePositions = array();
|
---|
81 | $this->arTmpVariablePositions = array();
|
---|
82 | $this->arTmpPlaceholders = $arPlaceholders;
|
---|
83 | $strQuery = preg_replace_callback(
|
---|
84 | '/@\\$%_PLACEHOLDER_[0-9]+_%\\$@/',
|
---|
85 | array($this, 'replacePlaceholdersCb'),
|
---|
86 | $strQuery
|
---|
87 | );
|
---|
88 | return array($strQuery, $this->arTmpVariablePositions);
|
---|
89 | }//protected function replacePlaceholders($strQuery, $arPlaceholders)
|
---|
90 |
|
---|
91 |
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Callback method internally used by replacePlaceholders() method.
|
---|
95 | */
|
---|
96 | protected function replacePlaceholdersCb($matches)
|
---|
97 | {
|
---|
98 | $strPlaceholder = $matches[0];
|
---|
99 | $strSparqlVariable = array_search(
|
---|
100 | $strPlaceholder,
|
---|
101 | $this->arTmpPlaceholders
|
---|
102 | );
|
---|
103 | $strDbPlaceholder = $this->dbConn->Param($strSparqlVariable);
|
---|
104 | $this->arTmpVariablePositions[] = $strSparqlVariable;
|
---|
105 |
|
---|
106 | return $strDbPlaceholder;
|
---|
107 | }//protected function replacePlaceholdersCb($matches)
|
---|
108 |
|
---|
109 |
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Executes the given prepared statments, filling the placeholders
|
---|
113 | * with the given values.
|
---|
114 | *
|
---|
115 | * @param array $arDbStatements Return value of prepareInDb()
|
---|
116 | * @param array $arVariableValues Array of (variable name, value) pairs
|
---|
117 | *
|
---|
118 | * @return array Array of database results as returned by Execute()
|
---|
119 | */
|
---|
120 | public function execute($arDbStatements, $arVariableValues)
|
---|
121 | {
|
---|
122 | $arResults = array();
|
---|
123 | $oldmode = $this->dbConn->SetFetchMode(ADODB_FETCH_ASSOC);
|
---|
124 |
|
---|
125 | foreach ($arDbStatements as $arStatement) {
|
---|
126 | list($stmt, $arVariablePositions) = $arStatement;
|
---|
127 | $arVariables = $this->createVariableArray(
|
---|
128 | $arVariablePositions,
|
---|
129 | $arVariableValues
|
---|
130 | );
|
---|
131 | $arResults[] = $this->dbConn->Execute($stmt, $arVariables);
|
---|
132 | }
|
---|
133 |
|
---|
134 | $this->dbConn->SetFetchMode($oldmode);
|
---|
135 |
|
---|
136 | return $arResults;
|
---|
137 | }//public function execute($arDbStatements, $arVariableValues)
|
---|
138 |
|
---|
139 |
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Creates an array full of variables to be passed to the Execute() method
|
---|
143 | * of the database connection object.
|
---|
144 | * Uses the variable positions array to get the positions of the variables
|
---|
145 | * in the result array, and the variable value array to get the actual
|
---|
146 | * values for the prepared statement.
|
---|
147 | *
|
---|
148 | * @param array $arVariablePositions Positions of the variables as returned
|
---|
149 | * by replacePlaceholders().
|
---|
150 | * @param array $arVariableValues Array of (variable name, value) pairs
|
---|
151 | *
|
---|
152 | * @return array Array of variable values
|
---|
153 | */
|
---|
154 | protected function createVariableArray($arVariablePositions, $arVariableValues)
|
---|
155 | {
|
---|
156 | $arVariables = array();
|
---|
157 |
|
---|
158 | foreach ($arVariablePositions as $nPos => $strVariable) {
|
---|
159 | if (!isset($arVariableValues[$strVariable])) {
|
---|
160 | throw new Exception('No value for variable "' . $strVariable . '" in prepared statement');
|
---|
161 | }
|
---|
162 |
|
---|
163 | $strValue = self::replacePrefix(
|
---|
164 | $arVariableValues[$strVariable],
|
---|
165 | $this->arPrefixes
|
---|
166 | );
|
---|
167 |
|
---|
168 | $arVariables[$nPos] = $strValue;
|
---|
169 | }
|
---|
170 |
|
---|
171 | return $arVariables;
|
---|
172 | }//protected function createVariableArray($arVariablePositions, $arVariableValues)
|
---|
173 |
|
---|
174 |
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Replaces all placeholders with their actual values.
|
---|
178 | */
|
---|
179 | public function replacePlaceholdersWithVariables($strQuery, $arPlaceholders, $arVariableValues)
|
---|
180 | {
|
---|
181 | $this->arTmpPlaceholders = $arPlaceholders;
|
---|
182 | $this->arTmpVariableValues = $arVariableValues;
|
---|
183 |
|
---|
184 | $strQuery = preg_replace_callback(
|
---|
185 | '/@\\$%_PLACEHOLDER_[0-9]+_%\\$@/',
|
---|
186 | array($this, 'replacePlaceholdersWithVariablesCb'),
|
---|
187 | $strQuery
|
---|
188 | );
|
---|
189 |
|
---|
190 | return $strQuery;
|
---|
191 | }//public function replacePlaceholdersWithVariables($strQuery, $arPlaceholders, $arVariableValues)
|
---|
192 |
|
---|
193 |
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Callback method internally used by
|
---|
197 | * replacePlaceholdersWithVariables() method.
|
---|
198 | */
|
---|
199 | protected function replacePlaceholdersWithVariablesCb($matches)
|
---|
200 | {
|
---|
201 | $strPlaceholder = $matches[0];
|
---|
202 | $strSparqlVariable = array_search(
|
---|
203 | $strPlaceholder,
|
---|
204 | $this->arTmpPlaceholders
|
---|
205 | );
|
---|
206 | return $this->dbConn->qstr(
|
---|
207 | self::replacePrefix(
|
---|
208 | $this->arTmpVariableValues[$strSparqlVariable],
|
---|
209 | $this->arPrefixes
|
---|
210 | )
|
---|
211 | );
|
---|
212 | }//protected function replacePlaceholdersWithVariablesCb($matches)
|
---|
213 |
|
---|
214 |
|
---|
215 |
|
---|
216 | protected static function replacePrefix($strValue, $arPrefixes)
|
---|
217 | {
|
---|
218 | //replace prefixes?
|
---|
219 | if (count($arParts = explode(':', $strValue, 2)) == 2) {
|
---|
220 | if (isset($arPrefixes[$arParts[0]])) {
|
---|
221 | $strValue = $arPrefixes[$arParts[0]] . $arParts[1];
|
---|
222 | }
|
---|
223 | }
|
---|
224 | return $strValue;
|
---|
225 | }//protected static function replacePrefix($strValue, $arPrefixes)
|
---|
226 |
|
---|
227 | }//class SparqlEngineDb_Preparator
|
---|
228 |
|
---|
229 | ?> |
---|