@@ -97,389 +97,389 @@ discard block |
||
97 | 97 | */ |
98 | 98 | abstract class DBManager |
99 | 99 | { |
100 | - /** |
|
101 | - * Name of database |
|
102 | - * @var resource |
|
103 | - */ |
|
104 | - public $database = null; |
|
105 | - |
|
106 | - /** |
|
107 | - * Indicates whether we should die when we get an error from the DB |
|
108 | - */ |
|
109 | - protected $dieOnError = false; |
|
110 | - |
|
111 | - /** |
|
112 | - * Indicates whether we should html encode the results from a query by default |
|
113 | - */ |
|
114 | - protected $encode = true; |
|
115 | - |
|
116 | - /** |
|
117 | - * Records the execution time of the last query |
|
118 | - */ |
|
119 | - protected $query_time = 0; |
|
120 | - |
|
121 | - /** |
|
122 | - * Last error message from the DB backend |
|
123 | - */ |
|
124 | - protected $last_error = false; |
|
125 | - |
|
126 | - /** |
|
127 | - * Registry of available result sets |
|
128 | - */ |
|
129 | - protected $lastResult; |
|
130 | - |
|
131 | - /** |
|
132 | - * Current query count |
|
133 | - */ |
|
134 | - private static $queryCount = 0; |
|
135 | - |
|
136 | - /** |
|
137 | - * Query threshold limit |
|
138 | - */ |
|
139 | - private static $queryLimit = 0; |
|
140 | - |
|
141 | - /** |
|
142 | - * Array of prepared statements and their correspoding parsed tokens |
|
143 | - */ |
|
144 | - protected $preparedTokens = array(); |
|
145 | - |
|
146 | - /** |
|
147 | - * TimeDate instance |
|
148 | - * @var TimeDate |
|
149 | - */ |
|
150 | - protected $timedate; |
|
151 | - |
|
152 | - /** |
|
153 | - * PHP Logger |
|
154 | - * @var Logger |
|
155 | - */ |
|
156 | - protected $log; |
|
157 | - |
|
158 | - /** |
|
159 | - * Table descriptions |
|
160 | - * @var array |
|
161 | - */ |
|
162 | - protected static $table_descriptions = array(); |
|
163 | - |
|
164 | - /** |
|
165 | - * Index descriptions |
|
166 | - * @var array |
|
167 | - */ |
|
168 | - protected static $index_descriptions = array(); |
|
169 | - |
|
170 | - /** |
|
171 | - * Maximum length of identifiers |
|
172 | - * @abstract |
|
173 | - * @var array |
|
174 | - */ |
|
175 | - protected $maxNameLengths = array( |
|
176 | - 'table' => 64, |
|
177 | - 'column' => 64, |
|
178 | - 'index' => 64, |
|
179 | - 'alias' => 64 |
|
180 | - ); |
|
181 | - |
|
182 | - /** |
|
183 | - * DB driver priority |
|
184 | - * Higher priority drivers override lower priority ones |
|
185 | - * @var int |
|
186 | - */ |
|
187 | - public $priority = 0; |
|
188 | - |
|
189 | - /** |
|
190 | - * Driver name label, for install |
|
191 | - * @absrtact |
|
192 | - * @var string |
|
193 | - */ |
|
194 | - public $label = ''; |
|
195 | - |
|
196 | - /** |
|
197 | - * Type names map |
|
198 | - * @abstract |
|
199 | - * @var array |
|
200 | - */ |
|
201 | - protected $type_map = array(); |
|
202 | - |
|
203 | - /** |
|
204 | - * Type classification into: |
|
205 | - * - int |
|
206 | - * - bool |
|
207 | - * - float |
|
208 | - * - date |
|
209 | - * @abstract |
|
210 | - * @var array |
|
211 | - */ |
|
212 | - protected $type_class = array( |
|
213 | - 'int' => 'int', |
|
214 | - 'double' => 'float', |
|
215 | - 'float' => 'float', |
|
216 | - 'uint' => 'int', |
|
217 | - 'ulong' => 'bigint', |
|
218 | - 'long' => 'bigint', |
|
219 | - 'short' => 'int', |
|
220 | - 'date' => 'date', |
|
221 | - 'datetime' => 'date', |
|
222 | - 'datetimecombo' => 'date', |
|
223 | - 'time' => 'time', |
|
224 | - 'bool' => 'bool', |
|
225 | - 'tinyint' => 'int', |
|
226 | - 'currency' => 'float', |
|
227 | - 'decimal' => 'float', |
|
228 | - 'decimal2' => 'float', |
|
229 | - ); |
|
230 | - |
|
231 | - /** |
|
232 | - * Capabilities this DB supports. Supported list: |
|
233 | - * affected_rows Can report query affected rows for UPDATE/DELETE |
|
234 | - * implement getAffectedRowCount() |
|
235 | - * select_rows Can report row count for SELECT |
|
236 | - * implement getRowCount() |
|
237 | - * case_sensitive Supports case-sensitive text columns |
|
238 | - * fulltext Supports fulltext search indexes |
|
239 | - * inline_keys Supports defining keys together with the table |
|
240 | - * auto_increment_sequence Autoincrement support implemented as sequence |
|
241 | - * limit_subquery Supports LIMIT clauses in subqueries |
|
242 | - * create_user Can create users for Sugar |
|
243 | - * create_db Can create databases |
|
244 | - * collation Supports setting collations |
|
245 | - * disable_keys Supports temporarily disabling keys (for upgrades, etc.) |
|
246 | - * |
|
247 | - * @abstract |
|
248 | - * Special cases: |
|
249 | - * fix:expandDatabase - needs expandDatabase fix, see expandDatabase.php |
|
250 | - * TODO: verify if we need these cases |
|
251 | - */ |
|
252 | - protected $capabilities = array(); |
|
253 | - |
|
254 | - /** |
|
255 | - * Database options |
|
256 | - * @var array |
|
257 | - */ |
|
258 | - protected $options = array(); |
|
100 | + /** |
|
101 | + * Name of database |
|
102 | + * @var resource |
|
103 | + */ |
|
104 | + public $database = null; |
|
105 | + |
|
106 | + /** |
|
107 | + * Indicates whether we should die when we get an error from the DB |
|
108 | + */ |
|
109 | + protected $dieOnError = false; |
|
110 | + |
|
111 | + /** |
|
112 | + * Indicates whether we should html encode the results from a query by default |
|
113 | + */ |
|
114 | + protected $encode = true; |
|
115 | + |
|
116 | + /** |
|
117 | + * Records the execution time of the last query |
|
118 | + */ |
|
119 | + protected $query_time = 0; |
|
120 | + |
|
121 | + /** |
|
122 | + * Last error message from the DB backend |
|
123 | + */ |
|
124 | + protected $last_error = false; |
|
125 | + |
|
126 | + /** |
|
127 | + * Registry of available result sets |
|
128 | + */ |
|
129 | + protected $lastResult; |
|
130 | + |
|
131 | + /** |
|
132 | + * Current query count |
|
133 | + */ |
|
134 | + private static $queryCount = 0; |
|
135 | + |
|
136 | + /** |
|
137 | + * Query threshold limit |
|
138 | + */ |
|
139 | + private static $queryLimit = 0; |
|
140 | + |
|
141 | + /** |
|
142 | + * Array of prepared statements and their correspoding parsed tokens |
|
143 | + */ |
|
144 | + protected $preparedTokens = array(); |
|
145 | + |
|
146 | + /** |
|
147 | + * TimeDate instance |
|
148 | + * @var TimeDate |
|
149 | + */ |
|
150 | + protected $timedate; |
|
151 | + |
|
152 | + /** |
|
153 | + * PHP Logger |
|
154 | + * @var Logger |
|
155 | + */ |
|
156 | + protected $log; |
|
157 | + |
|
158 | + /** |
|
159 | + * Table descriptions |
|
160 | + * @var array |
|
161 | + */ |
|
162 | + protected static $table_descriptions = array(); |
|
163 | + |
|
164 | + /** |
|
165 | + * Index descriptions |
|
166 | + * @var array |
|
167 | + */ |
|
168 | + protected static $index_descriptions = array(); |
|
169 | + |
|
170 | + /** |
|
171 | + * Maximum length of identifiers |
|
172 | + * @abstract |
|
173 | + * @var array |
|
174 | + */ |
|
175 | + protected $maxNameLengths = array( |
|
176 | + 'table' => 64, |
|
177 | + 'column' => 64, |
|
178 | + 'index' => 64, |
|
179 | + 'alias' => 64 |
|
180 | + ); |
|
181 | + |
|
182 | + /** |
|
183 | + * DB driver priority |
|
184 | + * Higher priority drivers override lower priority ones |
|
185 | + * @var int |
|
186 | + */ |
|
187 | + public $priority = 0; |
|
188 | + |
|
189 | + /** |
|
190 | + * Driver name label, for install |
|
191 | + * @absrtact |
|
192 | + * @var string |
|
193 | + */ |
|
194 | + public $label = ''; |
|
195 | + |
|
196 | + /** |
|
197 | + * Type names map |
|
198 | + * @abstract |
|
199 | + * @var array |
|
200 | + */ |
|
201 | + protected $type_map = array(); |
|
202 | + |
|
203 | + /** |
|
204 | + * Type classification into: |
|
205 | + * - int |
|
206 | + * - bool |
|
207 | + * - float |
|
208 | + * - date |
|
209 | + * @abstract |
|
210 | + * @var array |
|
211 | + */ |
|
212 | + protected $type_class = array( |
|
213 | + 'int' => 'int', |
|
214 | + 'double' => 'float', |
|
215 | + 'float' => 'float', |
|
216 | + 'uint' => 'int', |
|
217 | + 'ulong' => 'bigint', |
|
218 | + 'long' => 'bigint', |
|
219 | + 'short' => 'int', |
|
220 | + 'date' => 'date', |
|
221 | + 'datetime' => 'date', |
|
222 | + 'datetimecombo' => 'date', |
|
223 | + 'time' => 'time', |
|
224 | + 'bool' => 'bool', |
|
225 | + 'tinyint' => 'int', |
|
226 | + 'currency' => 'float', |
|
227 | + 'decimal' => 'float', |
|
228 | + 'decimal2' => 'float', |
|
229 | + ); |
|
230 | + |
|
231 | + /** |
|
232 | + * Capabilities this DB supports. Supported list: |
|
233 | + * affected_rows Can report query affected rows for UPDATE/DELETE |
|
234 | + * implement getAffectedRowCount() |
|
235 | + * select_rows Can report row count for SELECT |
|
236 | + * implement getRowCount() |
|
237 | + * case_sensitive Supports case-sensitive text columns |
|
238 | + * fulltext Supports fulltext search indexes |
|
239 | + * inline_keys Supports defining keys together with the table |
|
240 | + * auto_increment_sequence Autoincrement support implemented as sequence |
|
241 | + * limit_subquery Supports LIMIT clauses in subqueries |
|
242 | + * create_user Can create users for Sugar |
|
243 | + * create_db Can create databases |
|
244 | + * collation Supports setting collations |
|
245 | + * disable_keys Supports temporarily disabling keys (for upgrades, etc.) |
|
246 | + * |
|
247 | + * @abstract |
|
248 | + * Special cases: |
|
249 | + * fix:expandDatabase - needs expandDatabase fix, see expandDatabase.php |
|
250 | + * TODO: verify if we need these cases |
|
251 | + */ |
|
252 | + protected $capabilities = array(); |
|
253 | + |
|
254 | + /** |
|
255 | + * Database options |
|
256 | + * @var array |
|
257 | + */ |
|
258 | + protected $options = array(); |
|
259 | 259 | |
260 | 260 | /** |
261 | 261 | * Create DB Driver |
262 | 262 | */ |
263 | - public function __construct() |
|
264 | - { |
|
265 | - $this->timedate = TimeDate::getInstance(); |
|
266 | - $this->log = isset($GLOBALS['log']) ? $GLOBALS['log'] : null; |
|
267 | - $this->helper = $this; // compatibility |
|
268 | - } |
|
263 | + public function __construct() |
|
264 | + { |
|
265 | + $this->timedate = TimeDate::getInstance(); |
|
266 | + $this->log = isset($GLOBALS['log']) ? $GLOBALS['log'] : null; |
|
267 | + $this->helper = $this; // compatibility |
|
268 | + } |
|
269 | 269 | |
270 | 270 | /** |
271 | 271 | * Wrapper for those trying to access the private and protected class members directly |
272 | 272 | * @param string $p var name |
273 | 273 | * @return mixed |
274 | 274 | */ |
275 | - public function __get($p) |
|
276 | - { |
|
277 | - $this->log->info('Call to DBManager::$'.$p.' is deprecated'); |
|
278 | - return $this->$p; |
|
279 | - } |
|
280 | - |
|
281 | - /** |
|
282 | - * Returns the current database handle |
|
283 | - * @return resource |
|
284 | - */ |
|
285 | - public function getDatabase() |
|
286 | - { |
|
287 | - $this->checkConnection(); |
|
288 | - return $this->database; |
|
289 | - } |
|
290 | - |
|
291 | - /** |
|
292 | - * Returns this instance's DBHelper |
|
293 | - * Actually now returns $this |
|
294 | - * @deprecated |
|
295 | - * @return DBManager |
|
296 | - */ |
|
297 | - public function getHelper() |
|
298 | - { |
|
299 | - return $this; |
|
300 | - } |
|
301 | - |
|
302 | - /** |
|
303 | - * Checks for error happening in the database |
|
304 | - * |
|
305 | - * @param string $msg message to prepend to the error message |
|
306 | - * @param bool $dieOnError true if we want to die immediately on error |
|
307 | - * @return bool True if there was an error |
|
308 | - */ |
|
309 | - public function checkError($msg = '', $dieOnError = false) |
|
310 | - { |
|
311 | - if (empty($this->database)) { |
|
312 | - $this->registerError($msg, "Database Is Not Connected", $dieOnError); |
|
313 | - return true; |
|
314 | - } |
|
315 | - |
|
316 | - $dberror = $this->lastDbError(); |
|
317 | - if($dberror === false) { |
|
318 | - $this->last_error = false; |
|
319 | - return false; |
|
320 | - } |
|
321 | - $this->registerError($msg, $dberror, $dieOnError); |
|
275 | + public function __get($p) |
|
276 | + { |
|
277 | + $this->log->info('Call to DBManager::$'.$p.' is deprecated'); |
|
278 | + return $this->$p; |
|
279 | + } |
|
280 | + |
|
281 | + /** |
|
282 | + * Returns the current database handle |
|
283 | + * @return resource |
|
284 | + */ |
|
285 | + public function getDatabase() |
|
286 | + { |
|
287 | + $this->checkConnection(); |
|
288 | + return $this->database; |
|
289 | + } |
|
290 | + |
|
291 | + /** |
|
292 | + * Returns this instance's DBHelper |
|
293 | + * Actually now returns $this |
|
294 | + * @deprecated |
|
295 | + * @return DBManager |
|
296 | + */ |
|
297 | + public function getHelper() |
|
298 | + { |
|
299 | + return $this; |
|
300 | + } |
|
301 | + |
|
302 | + /** |
|
303 | + * Checks for error happening in the database |
|
304 | + * |
|
305 | + * @param string $msg message to prepend to the error message |
|
306 | + * @param bool $dieOnError true if we want to die immediately on error |
|
307 | + * @return bool True if there was an error |
|
308 | + */ |
|
309 | + public function checkError($msg = '', $dieOnError = false) |
|
310 | + { |
|
311 | + if (empty($this->database)) { |
|
312 | + $this->registerError($msg, "Database Is Not Connected", $dieOnError); |
|
313 | + return true; |
|
314 | + } |
|
315 | + |
|
316 | + $dberror = $this->lastDbError(); |
|
317 | + if($dberror === false) { |
|
318 | + $this->last_error = false; |
|
319 | + return false; |
|
320 | + } |
|
321 | + $this->registerError($msg, $dberror, $dieOnError); |
|
322 | 322 | return true; |
323 | - } |
|
324 | - |
|
325 | - /** |
|
326 | - * Register database error |
|
327 | - * If die-on-error flag is set, logs the message and dies, |
|
328 | - * otherwise sets last_error to the message |
|
329 | - * @param string $userMessage Message from function user |
|
330 | - * @param string $message Message from SQL driver |
|
331 | - * @param bool $dieOnError |
|
332 | - */ |
|
333 | - protected function registerError($userMessage, $message, $dieOnError = false) |
|
334 | - { |
|
335 | - if(!empty($message)) { |
|
336 | - if(!empty($userMessage)) { |
|
337 | - $message = "$userMessage: $message"; |
|
338 | - } |
|
339 | - if(empty($message)) { |
|
340 | - $message = "Database error"; |
|
341 | - } |
|
342 | - $this->log->fatal($message); |
|
343 | - if ($dieOnError || $this->dieOnError) { |
|
344 | - if(isset($GLOBALS['app_strings']['ERR_DB_FAIL'])) { |
|
345 | - sugar_die($GLOBALS['app_strings']['ERR_DB_FAIL']); |
|
346 | - } else { |
|
347 | - sugar_die("Database error. Please check suitecrm.log for details."); |
|
348 | - } |
|
349 | - } else { |
|
350 | - $this->last_error = $message; |
|
351 | - } |
|
352 | - } |
|
353 | - } |
|
354 | - |
|
355 | - /** |
|
356 | - * Return DB error message for the last query executed |
|
357 | - * @return string Last error message |
|
358 | - */ |
|
359 | - public function lastError() |
|
360 | - { |
|
361 | - return $this->last_error; |
|
362 | - } |
|
363 | - |
|
364 | - /** |
|
365 | - * This method is called by every method that runs a query. |
|
366 | - * If slow query dumping is turned on and the query time is beyond |
|
367 | - * the time limit, we will log the query. This function may do |
|
368 | - * additional reporting or log in a different area in the future. |
|
369 | - * |
|
370 | - * @param string $query query to log |
|
371 | - * @return boolean true if the query was logged, false otherwise |
|
372 | - */ |
|
373 | - protected function dump_slow_queries($query) |
|
374 | - { |
|
375 | - global $sugar_config; |
|
376 | - |
|
377 | - $do_the_dump = isset($sugar_config['dump_slow_queries']) |
|
378 | - ? $sugar_config['dump_slow_queries'] : false; |
|
379 | - $slow_query_time_msec = isset($sugar_config['slow_query_time_msec']) |
|
380 | - ? $sugar_config['slow_query_time_msec'] : 5000; |
|
381 | - |
|
382 | - if($do_the_dump) { |
|
383 | - if($slow_query_time_msec < ($this->query_time * 1000)) { |
|
384 | - // Then log both the query and the query time |
|
385 | - $this->log->fatal('Slow Query (time:'.$this->query_time."\n".$query); |
|
386 | - return true; |
|
387 | - } |
|
388 | - } |
|
389 | - return false; |
|
390 | - } |
|
323 | + } |
|
324 | + |
|
325 | + /** |
|
326 | + * Register database error |
|
327 | + * If die-on-error flag is set, logs the message and dies, |
|
328 | + * otherwise sets last_error to the message |
|
329 | + * @param string $userMessage Message from function user |
|
330 | + * @param string $message Message from SQL driver |
|
331 | + * @param bool $dieOnError |
|
332 | + */ |
|
333 | + protected function registerError($userMessage, $message, $dieOnError = false) |
|
334 | + { |
|
335 | + if(!empty($message)) { |
|
336 | + if(!empty($userMessage)) { |
|
337 | + $message = "$userMessage: $message"; |
|
338 | + } |
|
339 | + if(empty($message)) { |
|
340 | + $message = "Database error"; |
|
341 | + } |
|
342 | + $this->log->fatal($message); |
|
343 | + if ($dieOnError || $this->dieOnError) { |
|
344 | + if(isset($GLOBALS['app_strings']['ERR_DB_FAIL'])) { |
|
345 | + sugar_die($GLOBALS['app_strings']['ERR_DB_FAIL']); |
|
346 | + } else { |
|
347 | + sugar_die("Database error. Please check suitecrm.log for details."); |
|
348 | + } |
|
349 | + } else { |
|
350 | + $this->last_error = $message; |
|
351 | + } |
|
352 | + } |
|
353 | + } |
|
354 | + |
|
355 | + /** |
|
356 | + * Return DB error message for the last query executed |
|
357 | + * @return string Last error message |
|
358 | + */ |
|
359 | + public function lastError() |
|
360 | + { |
|
361 | + return $this->last_error; |
|
362 | + } |
|
363 | + |
|
364 | + /** |
|
365 | + * This method is called by every method that runs a query. |
|
366 | + * If slow query dumping is turned on and the query time is beyond |
|
367 | + * the time limit, we will log the query. This function may do |
|
368 | + * additional reporting or log in a different area in the future. |
|
369 | + * |
|
370 | + * @param string $query query to log |
|
371 | + * @return boolean true if the query was logged, false otherwise |
|
372 | + */ |
|
373 | + protected function dump_slow_queries($query) |
|
374 | + { |
|
375 | + global $sugar_config; |
|
376 | + |
|
377 | + $do_the_dump = isset($sugar_config['dump_slow_queries']) |
|
378 | + ? $sugar_config['dump_slow_queries'] : false; |
|
379 | + $slow_query_time_msec = isset($sugar_config['slow_query_time_msec']) |
|
380 | + ? $sugar_config['slow_query_time_msec'] : 5000; |
|
381 | + |
|
382 | + if($do_the_dump) { |
|
383 | + if($slow_query_time_msec < ($this->query_time * 1000)) { |
|
384 | + // Then log both the query and the query time |
|
385 | + $this->log->fatal('Slow Query (time:'.$this->query_time."\n".$query); |
|
386 | + return true; |
|
387 | + } |
|
388 | + } |
|
389 | + return false; |
|
390 | + } |
|
391 | 391 | |
392 | 392 | /** |
393 | - * Scans order by to ensure that any field being ordered by is. |
|
394 | - * |
|
395 | - * It will throw a warning error to the log file - fatal if slow query logging is enabled |
|
396 | - * |
|
397 | - * @param string $sql query to be run |
|
398 | - * @param bool $object_name optional, object to look up indices in |
|
399 | - * @return bool true if an index is found false otherwise |
|
400 | - */ |
|
393 | + * Scans order by to ensure that any field being ordered by is. |
|
394 | + * |
|
395 | + * It will throw a warning error to the log file - fatal if slow query logging is enabled |
|
396 | + * |
|
397 | + * @param string $sql query to be run |
|
398 | + * @param bool $object_name optional, object to look up indices in |
|
399 | + * @return bool true if an index is found false otherwise |
|
400 | + */ |
|
401 | 401 | protected function checkQuery($sql, $object_name = false) |
402 | 402 | { |
403 | - $match = array(); |
|
404 | - preg_match_all("'.* FROM ([^ ]*).* ORDER BY (.*)'is", $sql, $match); |
|
405 | - $indices = false; |
|
406 | - if (!empty($match[1][0])) |
|
407 | - $table = $match[1][0]; |
|
408 | - else |
|
409 | - return false; |
|
410 | - |
|
411 | - if (!empty($object_name) && !empty($GLOBALS['dictionary'][$object_name])) |
|
412 | - $indices = $GLOBALS['dictionary'][$object_name]['indices']; |
|
413 | - |
|
414 | - if (empty($indices)) { |
|
415 | - foreach ( $GLOBALS['dictionary'] as $current ) { |
|
416 | - if ($current['table'] == $table){ |
|
417 | - $indices = $current['indices']; |
|
418 | - break; |
|
419 | - } |
|
420 | - } |
|
421 | - } |
|
422 | - if (empty($indices)) { |
|
423 | - $this->log->warn('CHECK QUERY: Could not find index definitions for table ' . $table); |
|
424 | - return false; |
|
425 | - } |
|
426 | - if (!empty($match[2][0])) { |
|
427 | - $orderBys = explode(' ', $match[2][0]); |
|
428 | - foreach ($orderBys as $orderBy){ |
|
429 | - $orderBy = trim($orderBy); |
|
430 | - if (empty($orderBy)) |
|
431 | - continue; |
|
432 | - $orderBy = strtolower($orderBy); |
|
433 | - if ($orderBy == 'asc' || $orderBy == 'desc') |
|
434 | - continue; |
|
435 | - |
|
436 | - $orderBy = str_replace(array($table . '.', ','), '', $orderBy); |
|
437 | - |
|
438 | - foreach ($indices as $index) |
|
439 | - if (empty($index['db']) || $index['db'] == $this->dbType) |
|
440 | - foreach ($index['fields'] as $field) |
|
441 | - if ($field == $orderBy) |
|
442 | - return true; |
|
443 | - |
|
444 | - $warning = 'Missing Index For Order By Table: ' . $table . ' Order By:' . $orderBy ; |
|
445 | - if (!empty($GLOBALS['sugar_config']['dump_slow_queries'])) |
|
446 | - $this->log->fatal('CHECK QUERY:' .$warning); |
|
447 | - else |
|
448 | - $this->log->warn('CHECK QUERY:' .$warning); |
|
449 | - } |
|
450 | - } |
|
451 | - return false; |
|
452 | - } |
|
453 | - |
|
454 | - /** |
|
455 | - * Returns the time the last query took to execute |
|
456 | - * |
|
457 | - * @return int |
|
458 | - */ |
|
459 | - public function getQueryTime() |
|
460 | - { |
|
461 | - return $this->query_time; |
|
462 | - } |
|
463 | - |
|
464 | - /** |
|
465 | - * Checks the current connection; if it is not connected then reconnect |
|
466 | - */ |
|
467 | - public function checkConnection() |
|
468 | - { |
|
469 | - $this->last_error = ''; |
|
470 | - if (!isset($this->database)) |
|
471 | - $this->connect(); |
|
472 | - } |
|
473 | - |
|
474 | - /** |
|
475 | - * Sets the dieOnError value |
|
476 | - * |
|
477 | - * @param bool $value |
|
478 | - */ |
|
479 | - public function setDieOnError($value) |
|
480 | - { |
|
481 | - $this->dieOnError = $value; |
|
482 | - } |
|
403 | + $match = array(); |
|
404 | + preg_match_all("'.* FROM ([^ ]*).* ORDER BY (.*)'is", $sql, $match); |
|
405 | + $indices = false; |
|
406 | + if (!empty($match[1][0])) |
|
407 | + $table = $match[1][0]; |
|
408 | + else |
|
409 | + return false; |
|
410 | + |
|
411 | + if (!empty($object_name) && !empty($GLOBALS['dictionary'][$object_name])) |
|
412 | + $indices = $GLOBALS['dictionary'][$object_name]['indices']; |
|
413 | + |
|
414 | + if (empty($indices)) { |
|
415 | + foreach ( $GLOBALS['dictionary'] as $current ) { |
|
416 | + if ($current['table'] == $table){ |
|
417 | + $indices = $current['indices']; |
|
418 | + break; |
|
419 | + } |
|
420 | + } |
|
421 | + } |
|
422 | + if (empty($indices)) { |
|
423 | + $this->log->warn('CHECK QUERY: Could not find index definitions for table ' . $table); |
|
424 | + return false; |
|
425 | + } |
|
426 | + if (!empty($match[2][0])) { |
|
427 | + $orderBys = explode(' ', $match[2][0]); |
|
428 | + foreach ($orderBys as $orderBy){ |
|
429 | + $orderBy = trim($orderBy); |
|
430 | + if (empty($orderBy)) |
|
431 | + continue; |
|
432 | + $orderBy = strtolower($orderBy); |
|
433 | + if ($orderBy == 'asc' || $orderBy == 'desc') |
|
434 | + continue; |
|
435 | + |
|
436 | + $orderBy = str_replace(array($table . '.', ','), '', $orderBy); |
|
437 | + |
|
438 | + foreach ($indices as $index) |
|
439 | + if (empty($index['db']) || $index['db'] == $this->dbType) |
|
440 | + foreach ($index['fields'] as $field) |
|
441 | + if ($field == $orderBy) |
|
442 | + return true; |
|
443 | + |
|
444 | + $warning = 'Missing Index For Order By Table: ' . $table . ' Order By:' . $orderBy ; |
|
445 | + if (!empty($GLOBALS['sugar_config']['dump_slow_queries'])) |
|
446 | + $this->log->fatal('CHECK QUERY:' .$warning); |
|
447 | + else |
|
448 | + $this->log->warn('CHECK QUERY:' .$warning); |
|
449 | + } |
|
450 | + } |
|
451 | + return false; |
|
452 | + } |
|
453 | + |
|
454 | + /** |
|
455 | + * Returns the time the last query took to execute |
|
456 | + * |
|
457 | + * @return int |
|
458 | + */ |
|
459 | + public function getQueryTime() |
|
460 | + { |
|
461 | + return $this->query_time; |
|
462 | + } |
|
463 | + |
|
464 | + /** |
|
465 | + * Checks the current connection; if it is not connected then reconnect |
|
466 | + */ |
|
467 | + public function checkConnection() |
|
468 | + { |
|
469 | + $this->last_error = ''; |
|
470 | + if (!isset($this->database)) |
|
471 | + $this->connect(); |
|
472 | + } |
|
473 | + |
|
474 | + /** |
|
475 | + * Sets the dieOnError value |
|
476 | + * |
|
477 | + * @param bool $value |
|
478 | + */ |
|
479 | + public function setDieOnError($value) |
|
480 | + { |
|
481 | + $this->dieOnError = $value; |
|
482 | + } |
|
483 | 483 | |
484 | 484 | /** |
485 | 485 | * Implements a generic insert for any bean. |
@@ -488,67 +488,67 @@ discard block |
||
488 | 488 | * @return bool query result |
489 | 489 | * |
490 | 490 | */ |
491 | - public function insert(SugarBean $bean) |
|
492 | - { |
|
493 | - $sql = $this->insertSQL($bean); |
|
494 | - $tablename = $bean->getTableName(); |
|
495 | - $msg = "Error inserting into table: $tablename:"; |
|
496 | - return $this->query($sql,true,$msg); |
|
497 | - } |
|
498 | - |
|
499 | - /** |
|
500 | - * Insert data into table by parameter definition |
|
501 | - * @param string $table Table name |
|
502 | - * @param array $field_defs Definitions in vardef-like format |
|
503 | - * @param array $data Key/value to insert |
|
504 | - * @param array $field_map Fields map from SugarBean |
|
505 | - * @param bool $execute Execute or return query? |
|
491 | + public function insert(SugarBean $bean) |
|
492 | + { |
|
493 | + $sql = $this->insertSQL($bean); |
|
494 | + $tablename = $bean->getTableName(); |
|
495 | + $msg = "Error inserting into table: $tablename:"; |
|
496 | + return $this->query($sql,true,$msg); |
|
497 | + } |
|
498 | + |
|
499 | + /** |
|
500 | + * Insert data into table by parameter definition |
|
501 | + * @param string $table Table name |
|
502 | + * @param array $field_defs Definitions in vardef-like format |
|
503 | + * @param array $data Key/value to insert |
|
504 | + * @param array $field_map Fields map from SugarBean |
|
505 | + * @param bool $execute Execute or return query? |
|
506 | 506 | * @return bool query result |
507 | 507 | */ |
508 | - public function insertParams($table, $field_defs, $data, $field_map = null, $execute = true) |
|
509 | - { |
|
510 | - $values = array(); |
|
511 | - foreach ($field_defs as $field => $fieldDef) |
|
512 | - { |
|
513 | - if (isset($fieldDef['source']) && $fieldDef['source'] != 'db') continue; |
|
514 | - //custom fields handle there save seperatley |
|
515 | - if(!empty($field_map) && !empty($field_map[$field]['custom_type'])) continue; |
|
516 | - |
|
517 | - if(isset($data[$field])) { |
|
518 | - // clean the incoming value.. |
|
519 | - $val = from_html($data[$field]); |
|
520 | - } else { |
|
521 | - if(isset($fieldDef['default']) && strlen($fieldDef['default']) > 0) { |
|
522 | - $val = $fieldDef['default']; |
|
523 | - } else { |
|
524 | - $val = null; |
|
525 | - } |
|
526 | - } |
|
527 | - |
|
528 | - //handle auto increment values here - we may have to do something like nextval for oracle |
|
529 | - if (!empty($fieldDef['auto_increment'])) { |
|
530 | - $auto = $this->getAutoIncrementSQL($table, $fieldDef['name']); |
|
531 | - if(!empty($auto)) { |
|
532 | - $values[$field] = $auto; |
|
533 | - } |
|
534 | - } elseif ($fieldDef['name'] == 'deleted') { |
|
535 | - $values['deleted'] = (int)$val; |
|
536 | - } else { |
|
537 | - // need to do some thing about types of values |
|
538 | - if(!is_null($val) || !empty($fieldDef['required'])) { |
|
539 | - $values[$field] = $this->massageValue($val, $fieldDef); |
|
540 | - } |
|
541 | - } |
|
542 | - } |
|
543 | - |
|
544 | - if (empty($values)) |
|
545 | - return $execute?true:''; // no columns set |
|
546 | - |
|
547 | - // get the entire sql |
|
548 | - $query = "INSERT INTO $table (".implode(",", array_keys($values)).") |
|
508 | + public function insertParams($table, $field_defs, $data, $field_map = null, $execute = true) |
|
509 | + { |
|
510 | + $values = array(); |
|
511 | + foreach ($field_defs as $field => $fieldDef) |
|
512 | + { |
|
513 | + if (isset($fieldDef['source']) && $fieldDef['source'] != 'db') continue; |
|
514 | + //custom fields handle there save seperatley |
|
515 | + if(!empty($field_map) && !empty($field_map[$field]['custom_type'])) continue; |
|
516 | + |
|
517 | + if(isset($data[$field])) { |
|
518 | + // clean the incoming value.. |
|
519 | + $val = from_html($data[$field]); |
|
520 | + } else { |
|
521 | + if(isset($fieldDef['default']) && strlen($fieldDef['default']) > 0) { |
|
522 | + $val = $fieldDef['default']; |
|
523 | + } else { |
|
524 | + $val = null; |
|
525 | + } |
|
526 | + } |
|
527 | + |
|
528 | + //handle auto increment values here - we may have to do something like nextval for oracle |
|
529 | + if (!empty($fieldDef['auto_increment'])) { |
|
530 | + $auto = $this->getAutoIncrementSQL($table, $fieldDef['name']); |
|
531 | + if(!empty($auto)) { |
|
532 | + $values[$field] = $auto; |
|
533 | + } |
|
534 | + } elseif ($fieldDef['name'] == 'deleted') { |
|
535 | + $values['deleted'] = (int)$val; |
|
536 | + } else { |
|
537 | + // need to do some thing about types of values |
|
538 | + if(!is_null($val) || !empty($fieldDef['required'])) { |
|
539 | + $values[$field] = $this->massageValue($val, $fieldDef); |
|
540 | + } |
|
541 | + } |
|
542 | + } |
|
543 | + |
|
544 | + if (empty($values)) |
|
545 | + return $execute?true:''; // no columns set |
|
546 | + |
|
547 | + // get the entire sql |
|
548 | + $query = "INSERT INTO $table (".implode(",", array_keys($values)).") |
|
549 | 549 | VALUES (".implode(",", $values).")"; |
550 | - return $execute?$this->query($query):$query; |
|
551 | - } |
|
550 | + return $execute?$this->query($query):$query; |
|
551 | + } |
|
552 | 552 | |
553 | 553 | /** |
554 | 554 | * Implements a generic update for any bean |
@@ -560,13 +560,13 @@ discard block |
||
560 | 560 | * @return bool query result |
561 | 561 | * |
562 | 562 | */ |
563 | - public function update(SugarBean $bean, array $where = array()) |
|
564 | - { |
|
565 | - $sql = $this->updateSQL($bean, $where); |
|
566 | - $tablename = $bean->getTableName(); |
|
567 | - $msg = "Error updating table: $tablename:"; |
|
568 | - return $this->query($sql,true,$msg); |
|
569 | - } |
|
563 | + public function update(SugarBean $bean, array $where = array()) |
|
564 | + { |
|
565 | + $sql = $this->updateSQL($bean, $where); |
|
566 | + $tablename = $bean->getTableName(); |
|
567 | + $msg = "Error updating table: $tablename:"; |
|
568 | + return $this->query($sql,true,$msg); |
|
569 | + } |
|
570 | 570 | |
571 | 571 | /** |
572 | 572 | * Implements a generic delete for any bean identified by id |
@@ -577,215 +577,215 @@ discard block |
||
577 | 577 | * If where is not passed, it defaults to id of table |
578 | 578 | * @return bool query result |
579 | 579 | */ |
580 | - public function delete(SugarBean $bean, array $where = array()) |
|
581 | - { |
|
582 | - $sql = $this->deleteSQL($bean, $where); |
|
583 | - $tableName = $bean->getTableName(); |
|
584 | - $msg = "Error deleting from table: ".$tableName. ":"; |
|
585 | - return $this->query($sql,true,$msg); |
|
586 | - } |
|
587 | - |
|
588 | - /** |
|
589 | - * Implements a generic retrieve for any bean identified by id |
|
590 | - * |
|
591 | - * If we want to pass multiple values for a name, pass it as an array |
|
592 | - * If where is not passed, it defaults to id of table |
|
593 | - * |
|
594 | - * @param SugarBean $bean Sugarbean instance |
|
595 | - * @param array $where values with the keys as names of fields. |
|
596 | - * @return resource result from the query |
|
597 | - */ |
|
598 | - public function retrieve(SugarBean $bean, array $where = array()) |
|
599 | - { |
|
600 | - $sql = $this->retrieveSQL($bean, $where); |
|
601 | - $tableName = $bean->getTableName(); |
|
602 | - $msg = "Error retriving values from table:".$tableName. ":"; |
|
603 | - return $this->query($sql,true,$msg); |
|
604 | - } |
|
605 | - |
|
606 | - /** |
|
607 | - * Implements a generic retrieve for a collection of beans. |
|
608 | - * |
|
609 | - * These beans will be joined in the sql by the key attribute of field defs. |
|
610 | - * Currently, this function does support outer joins. |
|
611 | - * |
|
612 | - * @param array $beans Sugarbean instance(s) |
|
613 | - * @param array $cols columns to be returned with the keys as names of bean as identified by |
|
614 | - * get_class of bean. Values of this array is the array of fieldDefs to be returned for a bean. |
|
615 | - * If an empty array is passed, all columns are selected. |
|
616 | - * @param array $where values with the keys as names of bean as identified by get_class of bean |
|
617 | - * Each value at the first level is an array of values for that bean identified by name of fields. |
|
618 | - * If we want to pass multiple values for a name, pass it as an array |
|
619 | - * If where is not passed, all the rows will be returned. |
|
620 | - * @return resource |
|
621 | - */ |
|
622 | - public function retrieveView(array $beans, array $cols = array(), array $where = array()) |
|
623 | - { |
|
624 | - $sql = $this->retrieveViewSQL($beans, $cols, $where); |
|
625 | - $msg = "Error retriving values from View Collection:"; |
|
626 | - return $this->query($sql,true,$msg); |
|
627 | - } |
|
628 | - |
|
629 | - |
|
630 | - /** |
|
631 | - * Implements creation of a db table for a bean. |
|
632 | - * |
|
633 | - * NOTE: does not handle out-of-table constraints, use createConstraintSQL for that |
|
634 | - * @param SugarBean $bean Sugarbean instance |
|
635 | - */ |
|
636 | - public function createTable(SugarBean $bean) |
|
637 | - { |
|
638 | - $sql = $this->createTableSQL($bean); |
|
639 | - $tablename = $bean->getTableName(); |
|
640 | - $msg = "Error creating table: $tablename:"; |
|
641 | - $this->query($sql,true,$msg); |
|
642 | - if(!$this->supports("inline_keys")) { |
|
643 | - // handle constraints and indices |
|
644 | - $indicesArr = $this->createConstraintSql($bean); |
|
645 | - if (count($indicesArr) > 0) |
|
646 | - foreach ($indicesArr as $indexSql) |
|
647 | - $this->query($indexSql, true, $msg); |
|
648 | - } |
|
649 | - } |
|
650 | - |
|
651 | - /** |
|
652 | - * returns SQL to create constraints or indices |
|
653 | - * |
|
654 | - * @param SugarBean $bean SugarBean instance |
|
655 | - * @return array list of SQL statements |
|
656 | - */ |
|
657 | - protected function createConstraintSql(SugarBean $bean) |
|
658 | - { |
|
659 | - return $this->getConstraintSql($bean->getIndices(), $bean->getTableName()); |
|
660 | - } |
|
661 | - |
|
662 | - /** |
|
663 | - * Implements creation of a db table |
|
664 | - * |
|
665 | - * @param string $tablename |
|
666 | - * @param array $fieldDefs Field definitions, in vardef format |
|
667 | - * @param array $indices Index definitions, in vardef format |
|
668 | - * @param string $engine Engine parameter, used for MySQL engine so far |
|
580 | + public function delete(SugarBean $bean, array $where = array()) |
|
581 | + { |
|
582 | + $sql = $this->deleteSQL($bean, $where); |
|
583 | + $tableName = $bean->getTableName(); |
|
584 | + $msg = "Error deleting from table: ".$tableName. ":"; |
|
585 | + return $this->query($sql,true,$msg); |
|
586 | + } |
|
587 | + |
|
588 | + /** |
|
589 | + * Implements a generic retrieve for any bean identified by id |
|
590 | + * |
|
591 | + * If we want to pass multiple values for a name, pass it as an array |
|
592 | + * If where is not passed, it defaults to id of table |
|
593 | + * |
|
594 | + * @param SugarBean $bean Sugarbean instance |
|
595 | + * @param array $where values with the keys as names of fields. |
|
596 | + * @return resource result from the query |
|
597 | + */ |
|
598 | + public function retrieve(SugarBean $bean, array $where = array()) |
|
599 | + { |
|
600 | + $sql = $this->retrieveSQL($bean, $where); |
|
601 | + $tableName = $bean->getTableName(); |
|
602 | + $msg = "Error retriving values from table:".$tableName. ":"; |
|
603 | + return $this->query($sql,true,$msg); |
|
604 | + } |
|
605 | + |
|
606 | + /** |
|
607 | + * Implements a generic retrieve for a collection of beans. |
|
608 | + * |
|
609 | + * These beans will be joined in the sql by the key attribute of field defs. |
|
610 | + * Currently, this function does support outer joins. |
|
611 | + * |
|
612 | + * @param array $beans Sugarbean instance(s) |
|
613 | + * @param array $cols columns to be returned with the keys as names of bean as identified by |
|
614 | + * get_class of bean. Values of this array is the array of fieldDefs to be returned for a bean. |
|
615 | + * If an empty array is passed, all columns are selected. |
|
616 | + * @param array $where values with the keys as names of bean as identified by get_class of bean |
|
617 | + * Each value at the first level is an array of values for that bean identified by name of fields. |
|
618 | + * If we want to pass multiple values for a name, pass it as an array |
|
619 | + * If where is not passed, all the rows will be returned. |
|
620 | + * @return resource |
|
621 | + */ |
|
622 | + public function retrieveView(array $beans, array $cols = array(), array $where = array()) |
|
623 | + { |
|
624 | + $sql = $this->retrieveViewSQL($beans, $cols, $where); |
|
625 | + $msg = "Error retriving values from View Collection:"; |
|
626 | + return $this->query($sql,true,$msg); |
|
627 | + } |
|
628 | + |
|
629 | + |
|
630 | + /** |
|
631 | + * Implements creation of a db table for a bean. |
|
632 | + * |
|
633 | + * NOTE: does not handle out-of-table constraints, use createConstraintSQL for that |
|
634 | + * @param SugarBean $bean Sugarbean instance |
|
635 | + */ |
|
636 | + public function createTable(SugarBean $bean) |
|
637 | + { |
|
638 | + $sql = $this->createTableSQL($bean); |
|
639 | + $tablename = $bean->getTableName(); |
|
640 | + $msg = "Error creating table: $tablename:"; |
|
641 | + $this->query($sql,true,$msg); |
|
642 | + if(!$this->supports("inline_keys")) { |
|
643 | + // handle constraints and indices |
|
644 | + $indicesArr = $this->createConstraintSql($bean); |
|
645 | + if (count($indicesArr) > 0) |
|
646 | + foreach ($indicesArr as $indexSql) |
|
647 | + $this->query($indexSql, true, $msg); |
|
648 | + } |
|
649 | + } |
|
650 | + |
|
651 | + /** |
|
652 | + * returns SQL to create constraints or indices |
|
653 | + * |
|
654 | + * @param SugarBean $bean SugarBean instance |
|
655 | + * @return array list of SQL statements |
|
656 | + */ |
|
657 | + protected function createConstraintSql(SugarBean $bean) |
|
658 | + { |
|
659 | + return $this->getConstraintSql($bean->getIndices(), $bean->getTableName()); |
|
660 | + } |
|
661 | + |
|
662 | + /** |
|
663 | + * Implements creation of a db table |
|
664 | + * |
|
665 | + * @param string $tablename |
|
666 | + * @param array $fieldDefs Field definitions, in vardef format |
|
667 | + * @param array $indices Index definitions, in vardef format |
|
668 | + * @param string $engine Engine parameter, used for MySQL engine so far |
|
669 | 669 | * @todo: refactor engine param to be more generic |
670 | 670 | * @return bool success value |
671 | 671 | */ |
672 | - public function createTableParams($tablename, $fieldDefs, $indices, $engine = null) |
|
673 | - { |
|
674 | - if (!empty($fieldDefs)) { |
|
675 | - $sql = $this->createTableSQLParams($tablename, $fieldDefs, $indices, $engine); |
|
676 | - $res = true; |
|
677 | - if ($sql) { |
|
678 | - $msg = "Error creating table: $tablename"; |
|
679 | - $res = ($res and $this->query($sql,true,$msg)); |
|
680 | - } |
|
681 | - if(!$this->supports("inline_keys")) { |
|
682 | - // handle constraints and indices |
|
683 | - $indicesArr = $this->getConstraintSql($indices, $tablename); |
|
684 | - if (count($indicesArr) > 0) |
|
685 | - foreach ($indicesArr as $indexSql) |
|
686 | - $res = ($res and $this->query($indexSql, true, "Error creating indexes")); |
|
687 | - } |
|
688 | - return $res; |
|
689 | - } |
|
690 | - return false; |
|
691 | - } |
|
692 | - |
|
693 | - /** |
|
694 | - * Implements repair of a db table for a bean. |
|
695 | - * |
|
696 | - * @param SugarBean $bean SugarBean instance |
|
697 | - * @param bool $execute true if we want the action to take place, false if we just want the sql returned |
|
698 | - * @return string SQL statement or empty string, depending upon $execute |
|
699 | - */ |
|
700 | - public function repairTable(SugarBean $bean, $execute = true) |
|
701 | - { |
|
702 | - $indices = $bean->getIndices(); |
|
703 | - $fielddefs = $bean->getFieldDefinitions(); |
|
704 | - $tablename = $bean->getTableName(); |
|
705 | - |
|
706 | - //Clean the indexes to prevent duplicate definitions |
|
707 | - $new_index = array(); |
|
708 | - foreach($indices as $ind_def){ |
|
709 | - $new_index[$ind_def['name']] = $ind_def; |
|
710 | - } |
|
711 | - //jc: added this for beans that do not actually have a table, namely |
|
712 | - //ForecastOpportunities |
|
713 | - if($tablename == 'does_not_exist' || $tablename == '') |
|
714 | - return ''; |
|
715 | - |
|
716 | - global $dictionary; |
|
717 | - $engine=null; |
|
718 | - if (isset($dictionary[$bean->getObjectName()]['engine']) && !empty($dictionary[$bean->getObjectName()]['engine']) ) |
|
719 | - $engine = $dictionary[$bean->getObjectName()]['engine']; |
|
720 | - |
|
721 | - return $this->repairTableParams($tablename, $fielddefs,$new_index,$execute,$engine); |
|
722 | - } |
|
723 | - |
|
724 | - /** |
|
725 | - * Can this field be null? |
|
726 | - * Auto-increment and ID fields can not be null |
|
727 | - * @param array $vardef |
|
672 | + public function createTableParams($tablename, $fieldDefs, $indices, $engine = null) |
|
673 | + { |
|
674 | + if (!empty($fieldDefs)) { |
|
675 | + $sql = $this->createTableSQLParams($tablename, $fieldDefs, $indices, $engine); |
|
676 | + $res = true; |
|
677 | + if ($sql) { |
|
678 | + $msg = "Error creating table: $tablename"; |
|
679 | + $res = ($res and $this->query($sql,true,$msg)); |
|
680 | + } |
|
681 | + if(!$this->supports("inline_keys")) { |
|
682 | + // handle constraints and indices |
|
683 | + $indicesArr = $this->getConstraintSql($indices, $tablename); |
|
684 | + if (count($indicesArr) > 0) |
|
685 | + foreach ($indicesArr as $indexSql) |
|
686 | + $res = ($res and $this->query($indexSql, true, "Error creating indexes")); |
|
687 | + } |
|
688 | + return $res; |
|
689 | + } |
|
690 | + return false; |
|
691 | + } |
|
692 | + |
|
693 | + /** |
|
694 | + * Implements repair of a db table for a bean. |
|
695 | + * |
|
696 | + * @param SugarBean $bean SugarBean instance |
|
697 | + * @param bool $execute true if we want the action to take place, false if we just want the sql returned |
|
698 | + * @return string SQL statement or empty string, depending upon $execute |
|
699 | + */ |
|
700 | + public function repairTable(SugarBean $bean, $execute = true) |
|
701 | + { |
|
702 | + $indices = $bean->getIndices(); |
|
703 | + $fielddefs = $bean->getFieldDefinitions(); |
|
704 | + $tablename = $bean->getTableName(); |
|
705 | + |
|
706 | + //Clean the indexes to prevent duplicate definitions |
|
707 | + $new_index = array(); |
|
708 | + foreach($indices as $ind_def){ |
|
709 | + $new_index[$ind_def['name']] = $ind_def; |
|
710 | + } |
|
711 | + //jc: added this for beans that do not actually have a table, namely |
|
712 | + //ForecastOpportunities |
|
713 | + if($tablename == 'does_not_exist' || $tablename == '') |
|
714 | + return ''; |
|
715 | + |
|
716 | + global $dictionary; |
|
717 | + $engine=null; |
|
718 | + if (isset($dictionary[$bean->getObjectName()]['engine']) && !empty($dictionary[$bean->getObjectName()]['engine']) ) |
|
719 | + $engine = $dictionary[$bean->getObjectName()]['engine']; |
|
720 | + |
|
721 | + return $this->repairTableParams($tablename, $fielddefs,$new_index,$execute,$engine); |
|
722 | + } |
|
723 | + |
|
724 | + /** |
|
725 | + * Can this field be null? |
|
726 | + * Auto-increment and ID fields can not be null |
|
727 | + * @param array $vardef |
|
728 | 728 | * @return bool |
729 | 729 | */ |
730 | - protected function isNullable($vardef) |
|
731 | - { |
|
732 | - |
|
733 | - if(isset($vardef['isnull']) && (strtolower($vardef['isnull']) == 'false' || $vardef['isnull'] === false) |
|
734 | - && !empty($vardef['required'])) { |
|
735 | - /* required + is_null=false => not null */ |
|
736 | - return false; |
|
737 | - } |
|
738 | - if(empty($vardef['auto_increment']) && (empty($vardef['type']) || $vardef['type'] != 'id') |
|
739 | - && (empty($vardef['dbType']) || $vardef['dbType'] != 'id') |
|
740 | - && (empty($vardef['name']) || ($vardef['name'] != 'id' && $vardef['name'] != 'deleted')) |
|
741 | - ) { |
|
742 | - return true; |
|
743 | - } |
|
744 | - return false; |
|
745 | - } |
|
746 | - |
|
747 | - |
|
748 | - /** |
|
749 | - * Builds the SQL commands that repair a table structure |
|
750 | - * |
|
751 | - * @param string $tablename |
|
752 | - * @param array $fielddefs Field definitions, in vardef format |
|
753 | - * @param array $indices Index definitions, in vardef format |
|
754 | - * @param bool $execute optional, true if we want the queries executed instead of returned |
|
755 | - * @param string $engine optional, MySQL engine |
|
730 | + protected function isNullable($vardef) |
|
731 | + { |
|
732 | + |
|
733 | + if(isset($vardef['isnull']) && (strtolower($vardef['isnull']) == 'false' || $vardef['isnull'] === false) |
|
734 | + && !empty($vardef['required'])) { |
|
735 | + /* required + is_null=false => not null */ |
|
736 | + return false; |
|
737 | + } |
|
738 | + if(empty($vardef['auto_increment']) && (empty($vardef['type']) || $vardef['type'] != 'id') |
|
739 | + && (empty($vardef['dbType']) || $vardef['dbType'] != 'id') |
|
740 | + && (empty($vardef['name']) || ($vardef['name'] != 'id' && $vardef['name'] != 'deleted')) |
|
741 | + ) { |
|
742 | + return true; |
|
743 | + } |
|
744 | + return false; |
|
745 | + } |
|
746 | + |
|
747 | + |
|
748 | + /** |
|
749 | + * Builds the SQL commands that repair a table structure |
|
750 | + * |
|
751 | + * @param string $tablename |
|
752 | + * @param array $fielddefs Field definitions, in vardef format |
|
753 | + * @param array $indices Index definitions, in vardef format |
|
754 | + * @param bool $execute optional, true if we want the queries executed instead of returned |
|
755 | + * @param string $engine optional, MySQL engine |
|
756 | 756 | * @todo: refactor engine param to be more generic |
757 | 757 | * @return string |
758 | 758 | */ |
759 | - public function repairTableParams($tablename, $fielddefs, $indices, $execute = true, $engine = null) |
|
760 | - { |
|
761 | - //jc: had a bug when running the repair if the tablename is blank the repair will |
|
762 | - //fail when it tries to create a repair table |
|
763 | - if ($tablename == '' || empty($fielddefs)) |
|
764 | - return ''; |
|
765 | - |
|
766 | - //if the table does not exist create it and we are done |
|
767 | - $sql = "/* Table : $tablename */\n"; |
|
768 | - if (!$this->tableExists($tablename)) { |
|
769 | - $createtablesql = $this->createTableSQLParams($tablename,$fielddefs,$indices,$engine); |
|
770 | - if($execute && $createtablesql){ |
|
771 | - $this->createTableParams($tablename,$fielddefs,$indices,$engine); |
|
772 | - } |
|
773 | - |
|
774 | - $sql .= "/* MISSING TABLE: {$tablename} */\n"; |
|
775 | - $sql .= $createtablesql . "\n"; |
|
776 | - return $sql; |
|
777 | - } |
|
778 | - |
|
779 | - $compareFieldDefs = $this->get_columns($tablename); |
|
780 | - $compareIndices = $this->get_indices($tablename); |
|
781 | - |
|
782 | - $take_action = false; |
|
783 | - |
|
784 | - // do column comparisons |
|
785 | - $sql .= "/*COLUMNS*/\n"; |
|
786 | - foreach ($fielddefs as $name => $value) { |
|
787 | - if (isset($value['source']) && $value['source'] != 'db') |
|
788 | - continue; |
|
759 | + public function repairTableParams($tablename, $fielddefs, $indices, $execute = true, $engine = null) |
|
760 | + { |
|
761 | + //jc: had a bug when running the repair if the tablename is blank the repair will |
|
762 | + //fail when it tries to create a repair table |
|
763 | + if ($tablename == '' || empty($fielddefs)) |
|
764 | + return ''; |
|
765 | + |
|
766 | + //if the table does not exist create it and we are done |
|
767 | + $sql = "/* Table : $tablename */\n"; |
|
768 | + if (!$this->tableExists($tablename)) { |
|
769 | + $createtablesql = $this->createTableSQLParams($tablename,$fielddefs,$indices,$engine); |
|
770 | + if($execute && $createtablesql){ |
|
771 | + $this->createTableParams($tablename,$fielddefs,$indices,$engine); |
|
772 | + } |
|
773 | + |
|
774 | + $sql .= "/* MISSING TABLE: {$tablename} */\n"; |
|
775 | + $sql .= $createtablesql . "\n"; |
|
776 | + return $sql; |
|
777 | + } |
|
778 | + |
|
779 | + $compareFieldDefs = $this->get_columns($tablename); |
|
780 | + $compareIndices = $this->get_indices($tablename); |
|
781 | + |
|
782 | + $take_action = false; |
|
783 | + |
|
784 | + // do column comparisons |
|
785 | + $sql .= "/*COLUMNS*/\n"; |
|
786 | + foreach ($fielddefs as $name => $value) { |
|
787 | + if (isset($value['source']) && $value['source'] != 'db') |
|
788 | + continue; |
|
789 | 789 | |
790 | 790 | // Bug #42406. Skipping breaked vardef without type or name |
791 | 791 | if (isset($value['name']) == false || $value['name'] == false) |
@@ -799,183 +799,183 @@ discard block |
||
799 | 799 | continue; |
800 | 800 | } |
801 | 801 | |
802 | - $name = strtolower($value['name']); |
|
803 | - // add or fix the field defs per what the DB is expected to give us back |
|
804 | - $this->massageFieldDef($value,$tablename); |
|
805 | - |
|
806 | - $ignorerequired=false; |
|
807 | - |
|
808 | - //Do not track requiredness in the DB, auto_increment, ID, |
|
809 | - // and deleted fields are always required in the DB, so don't force those |
|
810 | - if ($this->isNullable($value)) { |
|
811 | - $value['required'] = false; |
|
812 | - } |
|
813 | - //Should match the conditions in DBManager::oneColumnSQLRep for DB required fields, type='id' fields will sometimes |
|
814 | - |
|
815 | - //come into this function as 'type' = 'char', 'dbType' = 'id' without required set in $value. Assume they are correct and leave them alone. |
|
816 | - else if (($name == 'id' || $value['type'] == 'id' || (isset($value['dbType']) && $value['dbType'] == 'id')) |
|
817 | - && (!isset($value['required']) && isset($compareFieldDefs[$name]['required']))) |
|
818 | - { |
|
819 | - $value['required'] = $compareFieldDefs[$name]['required']; |
|
820 | - } |
|
821 | - |
|
822 | - if ( !isset($compareFieldDefs[$name]) ) { |
|
823 | - // ok we need this field lets create it |
|
824 | - $sql .= "/*MISSING IN DATABASE - $name - ROW*/\n"; |
|
825 | - $sql .= $this->addColumnSQL($tablename, $value) . "\n"; |
|
826 | - if ($execute) |
|
827 | - $this->addColumn($tablename, $value); |
|
828 | - $take_action = true; |
|
829 | - } elseif ( !$this->compareVarDefs($compareFieldDefs[$name],$value)) { |
|
830 | - //fields are different lets alter it |
|
831 | - $sql .= "/*MISMATCH WITH DATABASE - $name - ROW "; |
|
832 | - foreach($compareFieldDefs[$name] as $rKey => $rValue) { |
|
833 | - $sql .= "[$rKey] => '$rValue' "; |
|
834 | - } |
|
835 | - $sql .= "*/\n"; |
|
836 | - $sql .= "/* VARDEF - $name - ROW"; |
|
837 | - foreach($value as $rKey => $rValue) { |
|
838 | - $sql .= "[$rKey] => '$rValue' "; |
|
839 | - } |
|
840 | - $sql .= "*/\n"; |
|
841 | - |
|
842 | - //jc: oracle will complain if you try to execute a statement that sets a column to (not) null |
|
843 | - //when it is already (not) null |
|
844 | - if ( isset($value['isnull']) && isset($compareFieldDefs[$name]['isnull']) && |
|
845 | - $value['isnull'] === $compareFieldDefs[$name]['isnull']) { |
|
846 | - unset($value['required']); |
|
847 | - $ignorerequired=true; |
|
848 | - } |
|
849 | - |
|
850 | - //dwheeler: Once a column has been defined as null, we cannot try to force it back to !null |
|
851 | - if ((isset($value['required']) && ($value['required'] === true || $value['required'] == 'true' || $value['required'] === 1)) |
|
852 | - && (empty($compareFieldDefs[$name]['required']) || $compareFieldDefs[$name]['required'] != 'true')) |
|
853 | - { |
|
854 | - $ignorerequired = true; |
|
855 | - } |
|
856 | - $altersql = $this->alterColumnSQL($tablename, $value,$ignorerequired); |
|
857 | - if(is_array($altersql)) { |
|
858 | - $altersql = join("\n", $altersql); |
|
859 | - } |
|
860 | - $sql .= $altersql . "\n"; |
|
861 | - if($execute){ |
|
862 | - $this->alterColumn($tablename, $value, $ignorerequired); |
|
863 | - } |
|
864 | - $take_action = true; |
|
865 | - } |
|
866 | - } |
|
867 | - |
|
868 | - // do index comparisons |
|
869 | - $sql .= "/* INDEXES */\n"; |
|
870 | - $correctedIndexs = array(); |
|
802 | + $name = strtolower($value['name']); |
|
803 | + // add or fix the field defs per what the DB is expected to give us back |
|
804 | + $this->massageFieldDef($value,$tablename); |
|
871 | 805 | |
872 | - $compareIndices_case_insensitive = array(); |
|
806 | + $ignorerequired=false; |
|
873 | 807 | |
874 | - // do indices comparisons case-insensitive |
|
875 | - foreach($compareIndices as $k => $value){ |
|
876 | - $value['name'] = strtolower($value['name']); |
|
877 | - $compareIndices_case_insensitive[strtolower($k)] = $value; |
|
878 | - } |
|
879 | - $compareIndices = $compareIndices_case_insensitive; |
|
880 | - unset($compareIndices_case_insensitive); |
|
881 | - |
|
882 | - foreach ($indices as $value) { |
|
883 | - if (isset($value['source']) && $value['source'] != 'db') |
|
884 | - continue; |
|
885 | - |
|
886 | - |
|
887 | - $validDBName = $this->getValidDBName($value['name'], true, 'index', true); |
|
888 | - if (isset($compareIndices[$validDBName])) { |
|
889 | - $value['name'] = $validDBName; |
|
890 | - } |
|
891 | - $name = strtolower($value['name']); |
|
892 | - |
|
893 | - //Don't attempt to fix the same index twice in one pass; |
|
894 | - if (isset($correctedIndexs[$name])) |
|
895 | - continue; |
|
896 | - |
|
897 | - //don't bother checking primary nothing we can do about them |
|
898 | - if (isset($value['type']) && $value['type'] == 'primary') |
|
899 | - continue; |
|
900 | - |
|
901 | - //database helpers do not know how to handle full text indices |
|
902 | - if ($value['type']=='fulltext') |
|
903 | - continue; |
|
904 | - |
|
905 | - if ( in_array($value['type'],array('alternate_key','foreign')) ) |
|
906 | - $value['type'] = 'index'; |
|
907 | - |
|
908 | - if ( !isset($compareIndices[$name]) ) { |
|
909 | - //First check if an index exists that doesn't match our name, if so, try to rename it |
|
910 | - $found = false; |
|
911 | - foreach ($compareIndices as $ex_name => $ex_value) { |
|
912 | - if($this->compareVarDefs($ex_value, $value, true)) { |
|
913 | - $found = $ex_name; |
|
914 | - break; |
|
915 | - } |
|
916 | - } |
|
917 | - if ($found) { |
|
918 | - $sql .= "/*MISSNAMED INDEX IN DATABASE - $name - $ex_name */\n"; |
|
919 | - $rename = $this->renameIndexDefs($ex_value, $value, $tablename); |
|
920 | - if($execute) { |
|
921 | - $this->query($rename, true, "Cannot rename index"); |
|
922 | - } |
|
923 | - $sql .= is_array($rename)?join("\n", $rename). "\n":$rename."\n"; |
|
924 | - |
|
925 | - } else { |
|
926 | - // ok we need this field lets create it |
|
927 | - $sql .= "/*MISSING INDEX IN DATABASE - $name -{$value['type']} ROW */\n"; |
|
928 | - $sql .= $this->addIndexes($tablename,array($value), $execute) . "\n"; |
|
929 | - } |
|
930 | - $take_action = true; |
|
931 | - $correctedIndexs[$name] = true; |
|
932 | - } elseif ( !$this->compareVarDefs($compareIndices[$name],$value) ) { |
|
933 | - // fields are different lets alter it |
|
934 | - $sql .= "/*INDEX MISMATCH WITH DATABASE - $name - ROW "; |
|
935 | - foreach ($compareIndices[$name] as $n1 => $t1) { |
|
936 | - $sql .= "<$n1>"; |
|
937 | - if ( $n1 == 'fields' ) |
|
938 | - foreach($t1 as $rKey => $rValue) |
|
939 | - $sql .= "[$rKey] => '$rValue' "; |
|
940 | - else |
|
941 | - $sql .= " $t1 "; |
|
942 | - } |
|
943 | - $sql .= "*/\n"; |
|
944 | - $sql .= "/* VARDEF - $name - ROW"; |
|
945 | - foreach ($value as $n1 => $t1) { |
|
946 | - $sql .= "<$n1>"; |
|
947 | - if ( $n1 == 'fields' ) |
|
948 | - foreach ($t1 as $rKey => $rValue) |
|
949 | - $sql .= "[$rKey] => '$rValue' "; |
|
950 | - else |
|
951 | - $sql .= " $t1 "; |
|
952 | - } |
|
953 | - $sql .= "*/\n"; |
|
954 | - $sql .= $this->modifyIndexes($tablename,array($value), $execute) . "\n"; |
|
955 | - $take_action = true; |
|
956 | - $correctedIndexs[$name] = true; |
|
957 | - } |
|
958 | - } |
|
959 | - |
|
960 | - return ($take_action === true) ? $sql : ''; |
|
961 | - } |
|
808 | + //Do not track requiredness in the DB, auto_increment, ID, |
|
809 | + // and deleted fields are always required in the DB, so don't force those |
|
810 | + if ($this->isNullable($value)) { |
|
811 | + $value['required'] = false; |
|
812 | + } |
|
813 | + //Should match the conditions in DBManager::oneColumnSQLRep for DB required fields, type='id' fields will sometimes |
|
962 | 814 | |
963 | - /** |
|
964 | - * Compares two vardefs |
|
965 | - * |
|
966 | - * @param array $fielddef1 This is from the database |
|
967 | - * @param array $fielddef2 This is from the vardef |
|
968 | - * @param bool $ignoreName Ignore name-only differences? |
|
969 | - * @return bool true if they match, false if they don't |
|
970 | - */ |
|
971 | - public function compareVarDefs($fielddef1, $fielddef2, $ignoreName = false) |
|
972 | - { |
|
973 | - foreach ( $fielddef1 as $key => $value ) { |
|
974 | - if ($key == 'name' && $ignoreName) |
|
975 | - continue; |
|
976 | - if (isset($fielddef2[$key])) |
|
815 | + //come into this function as 'type' = 'char', 'dbType' = 'id' without required set in $value. Assume they are correct and leave them alone. |
|
816 | + else if (($name == 'id' || $value['type'] == 'id' || (isset($value['dbType']) && $value['dbType'] == 'id')) |
|
817 | + && (!isset($value['required']) && isset($compareFieldDefs[$name]['required']))) |
|
977 | 818 | { |
978 | - if (!is_array($fielddef1[$key]) && !is_array($fielddef2[$key])) |
|
819 | + $value['required'] = $compareFieldDefs[$name]['required']; |
|
820 | + } |
|
821 | + |
|
822 | + if ( !isset($compareFieldDefs[$name]) ) { |
|
823 | + // ok we need this field lets create it |
|
824 | + $sql .= "/*MISSING IN DATABASE - $name - ROW*/\n"; |
|
825 | + $sql .= $this->addColumnSQL($tablename, $value) . "\n"; |
|
826 | + if ($execute) |
|
827 | + $this->addColumn($tablename, $value); |
|
828 | + $take_action = true; |
|
829 | + } elseif ( !$this->compareVarDefs($compareFieldDefs[$name],$value)) { |
|
830 | + //fields are different lets alter it |
|
831 | + $sql .= "/*MISMATCH WITH DATABASE - $name - ROW "; |
|
832 | + foreach($compareFieldDefs[$name] as $rKey => $rValue) { |
|
833 | + $sql .= "[$rKey] => '$rValue' "; |
|
834 | + } |
|
835 | + $sql .= "*/\n"; |
|
836 | + $sql .= "/* VARDEF - $name - ROW"; |
|
837 | + foreach($value as $rKey => $rValue) { |
|
838 | + $sql .= "[$rKey] => '$rValue' "; |
|
839 | + } |
|
840 | + $sql .= "*/\n"; |
|
841 | + |
|
842 | + //jc: oracle will complain if you try to execute a statement that sets a column to (not) null |
|
843 | + //when it is already (not) null |
|
844 | + if ( isset($value['isnull']) && isset($compareFieldDefs[$name]['isnull']) && |
|
845 | + $value['isnull'] === $compareFieldDefs[$name]['isnull']) { |
|
846 | + unset($value['required']); |
|
847 | + $ignorerequired=true; |
|
848 | + } |
|
849 | + |
|
850 | + //dwheeler: Once a column has been defined as null, we cannot try to force it back to !null |
|
851 | + if ((isset($value['required']) && ($value['required'] === true || $value['required'] == 'true' || $value['required'] === 1)) |
|
852 | + && (empty($compareFieldDefs[$name]['required']) || $compareFieldDefs[$name]['required'] != 'true')) |
|
853 | + { |
|
854 | + $ignorerequired = true; |
|
855 | + } |
|
856 | + $altersql = $this->alterColumnSQL($tablename, $value,$ignorerequired); |
|
857 | + if(is_array($altersql)) { |
|
858 | + $altersql = join("\n", $altersql); |
|
859 | + } |
|
860 | + $sql .= $altersql . "\n"; |
|
861 | + if($execute){ |
|
862 | + $this->alterColumn($tablename, $value, $ignorerequired); |
|
863 | + } |
|
864 | + $take_action = true; |
|
865 | + } |
|
866 | + } |
|
867 | + |
|
868 | + // do index comparisons |
|
869 | + $sql .= "/* INDEXES */\n"; |
|
870 | + $correctedIndexs = array(); |
|
871 | + |
|
872 | + $compareIndices_case_insensitive = array(); |
|
873 | + |
|
874 | + // do indices comparisons case-insensitive |
|
875 | + foreach($compareIndices as $k => $value){ |
|
876 | + $value['name'] = strtolower($value['name']); |
|
877 | + $compareIndices_case_insensitive[strtolower($k)] = $value; |
|
878 | + } |
|
879 | + $compareIndices = $compareIndices_case_insensitive; |
|
880 | + unset($compareIndices_case_insensitive); |
|
881 | + |
|
882 | + foreach ($indices as $value) { |
|
883 | + if (isset($value['source']) && $value['source'] != 'db') |
|
884 | + continue; |
|
885 | + |
|
886 | + |
|
887 | + $validDBName = $this->getValidDBName($value['name'], true, 'index', true); |
|
888 | + if (isset($compareIndices[$validDBName])) { |
|
889 | + $value['name'] = $validDBName; |
|
890 | + } |
|
891 | + $name = strtolower($value['name']); |
|
892 | + |
|
893 | + //Don't attempt to fix the same index twice in one pass; |
|
894 | + if (isset($correctedIndexs[$name])) |
|
895 | + continue; |
|
896 | + |
|
897 | + //don't bother checking primary nothing we can do about them |
|
898 | + if (isset($value['type']) && $value['type'] == 'primary') |
|
899 | + continue; |
|
900 | + |
|
901 | + //database helpers do not know how to handle full text indices |
|
902 | + if ($value['type']=='fulltext') |
|
903 | + continue; |
|
904 | + |
|
905 | + if ( in_array($value['type'],array('alternate_key','foreign')) ) |
|
906 | + $value['type'] = 'index'; |
|
907 | + |
|
908 | + if ( !isset($compareIndices[$name]) ) { |
|
909 | + //First check if an index exists that doesn't match our name, if so, try to rename it |
|
910 | + $found = false; |
|
911 | + foreach ($compareIndices as $ex_name => $ex_value) { |
|
912 | + if($this->compareVarDefs($ex_value, $value, true)) { |
|
913 | + $found = $ex_name; |
|
914 | + break; |
|
915 | + } |
|
916 | + } |
|
917 | + if ($found) { |
|
918 | + $sql .= "/*MISSNAMED INDEX IN DATABASE - $name - $ex_name */\n"; |
|
919 | + $rename = $this->renameIndexDefs($ex_value, $value, $tablename); |
|
920 | + if($execute) { |
|
921 | + $this->query($rename, true, "Cannot rename index"); |
|
922 | + } |
|
923 | + $sql .= is_array($rename)?join("\n", $rename). "\n":$rename."\n"; |
|
924 | + |
|
925 | + } else { |
|
926 | + // ok we need this field lets create it |
|
927 | + $sql .= "/*MISSING INDEX IN DATABASE - $name -{$value['type']} ROW */\n"; |
|
928 | + $sql .= $this->addIndexes($tablename,array($value), $execute) . "\n"; |
|
929 | + } |
|
930 | + $take_action = true; |
|
931 | + $correctedIndexs[$name] = true; |
|
932 | + } elseif ( !$this->compareVarDefs($compareIndices[$name],$value) ) { |
|
933 | + // fields are different lets alter it |
|
934 | + $sql .= "/*INDEX MISMATCH WITH DATABASE - $name - ROW "; |
|
935 | + foreach ($compareIndices[$name] as $n1 => $t1) { |
|
936 | + $sql .= "<$n1>"; |
|
937 | + if ( $n1 == 'fields' ) |
|
938 | + foreach($t1 as $rKey => $rValue) |
|
939 | + $sql .= "[$rKey] => '$rValue' "; |
|
940 | + else |
|
941 | + $sql .= " $t1 "; |
|
942 | + } |
|
943 | + $sql .= "*/\n"; |
|
944 | + $sql .= "/* VARDEF - $name - ROW"; |
|
945 | + foreach ($value as $n1 => $t1) { |
|
946 | + $sql .= "<$n1>"; |
|
947 | + if ( $n1 == 'fields' ) |
|
948 | + foreach ($t1 as $rKey => $rValue) |
|
949 | + $sql .= "[$rKey] => '$rValue' "; |
|
950 | + else |
|
951 | + $sql .= " $t1 "; |
|
952 | + } |
|
953 | + $sql .= "*/\n"; |
|
954 | + $sql .= $this->modifyIndexes($tablename,array($value), $execute) . "\n"; |
|
955 | + $take_action = true; |
|
956 | + $correctedIndexs[$name] = true; |
|
957 | + } |
|
958 | + } |
|
959 | + |
|
960 | + return ($take_action === true) ? $sql : ''; |
|
961 | + } |
|
962 | + |
|
963 | + /** |
|
964 | + * Compares two vardefs |
|
965 | + * |
|
966 | + * @param array $fielddef1 This is from the database |
|
967 | + * @param array $fielddef2 This is from the vardef |
|
968 | + * @param bool $ignoreName Ignore name-only differences? |
|
969 | + * @return bool true if they match, false if they don't |
|
970 | + */ |
|
971 | + public function compareVarDefs($fielddef1, $fielddef2, $ignoreName = false) |
|
972 | + { |
|
973 | + foreach ( $fielddef1 as $key => $value ) { |
|
974 | + if ($key == 'name' && $ignoreName) |
|
975 | + continue; |
|
976 | + if (isset($fielddef2[$key])) |
|
977 | + { |
|
978 | + if (!is_array($fielddef1[$key]) && !is_array($fielddef2[$key])) |
|
979 | 979 | { |
980 | 980 | if (strtolower($fielddef1[$key]) == strtolower($fielddef2[$key])) |
981 | 981 | { |
@@ -990,64 +990,64 @@ discard block |
||
990 | 990 | } |
991 | 991 | } |
992 | 992 | } |
993 | - //Ignore len if its not set in the vardef |
|
994 | - if ($key == 'len' && empty($fielddef2[$key])) |
|
995 | - continue; |
|
993 | + //Ignore len if its not set in the vardef |
|
994 | + if ($key == 'len' && empty($fielddef2[$key])) |
|
995 | + continue; |
|
996 | 996 | // if the length in db is greather than the vardef, ignore it |
997 | 997 | if ($key == 'len' && ($fielddef1[$key] >= $fielddef2[$key])) { |
998 | 998 | continue; |
999 | 999 | } |
1000 | - return false; |
|
1001 | - } |
|
1002 | - |
|
1003 | - return true; |
|
1004 | - } |
|
1005 | - |
|
1006 | - /** |
|
1007 | - * Compare a field in two tables |
|
1008 | - * @deprecated |
|
1009 | - * @param string $name field name |
|
1010 | - * @param string $table1 |
|
1011 | - * @param string $table2 |
|
1012 | - * @return array array with keys 'msg','table1','table2' |
|
1013 | - */ |
|
1014 | - public function compareFieldInTables($name, $table1, $table2) |
|
1015 | - { |
|
1016 | - $row1 = $this->describeField($name, $table1); |
|
1017 | - $row2 = $this->describeField($name, $table2); |
|
1018 | - $returnArray = array( |
|
1019 | - 'table1' => $row1, |
|
1020 | - 'table2' => $row2, |
|
1021 | - 'msg' => 'error', |
|
1022 | - ); |
|
1023 | - |
|
1024 | - $ignore_filter = array('Key'=>1); |
|
1025 | - if ($row1) { |
|
1026 | - if (!$row2) { |
|
1027 | - // Exists on table1 but not table2 |
|
1028 | - $returnArray['msg'] = 'not_exists_table2'; |
|
1029 | - } |
|
1030 | - else { |
|
1031 | - if (sizeof($row1) != sizeof($row2)) { |
|
1032 | - $returnArray['msg'] = 'no_match'; |
|
1033 | - } |
|
1034 | - else { |
|
1035 | - $returnArray['msg'] = 'match'; |
|
1036 | - foreach($row1 as $key => $value){ |
|
1037 | - //ignore keys when checking we will check them when we do the index check |
|
1038 | - if( !isset($ignore_filter[$key]) && (!isset($row2[$key]) || $row1[$key] !== $row2[$key])){ |
|
1039 | - $returnArray['msg'] = 'no_match'; |
|
1040 | - } |
|
1041 | - } |
|
1042 | - } |
|
1043 | - } |
|
1044 | - } |
|
1045 | - else { |
|
1046 | - $returnArray['msg'] = 'not_exists_table1'; |
|
1047 | - } |
|
1048 | - |
|
1049 | - return $returnArray; |
|
1050 | - } |
|
1000 | + return false; |
|
1001 | + } |
|
1002 | + |
|
1003 | + return true; |
|
1004 | + } |
|
1005 | + |
|
1006 | + /** |
|
1007 | + * Compare a field in two tables |
|
1008 | + * @deprecated |
|
1009 | + * @param string $name field name |
|
1010 | + * @param string $table1 |
|
1011 | + * @param string $table2 |
|
1012 | + * @return array array with keys 'msg','table1','table2' |
|
1013 | + */ |
|
1014 | + public function compareFieldInTables($name, $table1, $table2) |
|
1015 | + { |
|
1016 | + $row1 = $this->describeField($name, $table1); |
|
1017 | + $row2 = $this->describeField($name, $table2); |
|
1018 | + $returnArray = array( |
|
1019 | + 'table1' => $row1, |
|
1020 | + 'table2' => $row2, |
|
1021 | + 'msg' => 'error', |
|
1022 | + ); |
|
1023 | + |
|
1024 | + $ignore_filter = array('Key'=>1); |
|
1025 | + if ($row1) { |
|
1026 | + if (!$row2) { |
|
1027 | + // Exists on table1 but not table2 |
|
1028 | + $returnArray['msg'] = 'not_exists_table2'; |
|
1029 | + } |
|
1030 | + else { |
|
1031 | + if (sizeof($row1) != sizeof($row2)) { |
|
1032 | + $returnArray['msg'] = 'no_match'; |
|
1033 | + } |
|
1034 | + else { |
|
1035 | + $returnArray['msg'] = 'match'; |
|
1036 | + foreach($row1 as $key => $value){ |
|
1037 | + //ignore keys when checking we will check them when we do the index check |
|
1038 | + if( !isset($ignore_filter[$key]) && (!isset($row2[$key]) || $row1[$key] !== $row2[$key])){ |
|
1039 | + $returnArray['msg'] = 'no_match'; |
|
1040 | + } |
|
1041 | + } |
|
1042 | + } |
|
1043 | + } |
|
1044 | + } |
|
1045 | + else { |
|
1046 | + $returnArray['msg'] = 'not_exists_table1'; |
|
1047 | + } |
|
1048 | + |
|
1049 | + return $returnArray; |
|
1050 | + } |
|
1051 | 1051 | // |
1052 | 1052 | // /** |
1053 | 1053 | // * Compare an index in two different tables |
@@ -1097,193 +1097,193 @@ discard block |
||
1097 | 1097 | // } |
1098 | 1098 | |
1099 | 1099 | |
1100 | - /** |
|
1101 | - * Creates an index identified by name on the given fields. |
|
1102 | - * |
|
1103 | - * @param SugarBean $bean SugarBean instance |
|
1104 | - * @param array $fieldDefs Field definitions, in vardef format |
|
1105 | - * @param string $name index name |
|
1106 | - * @param bool $unique optional, true if we want to create an unique index |
|
1100 | + /** |
|
1101 | + * Creates an index identified by name on the given fields. |
|
1102 | + * |
|
1103 | + * @param SugarBean $bean SugarBean instance |
|
1104 | + * @param array $fieldDefs Field definitions, in vardef format |
|
1105 | + * @param string $name index name |
|
1106 | + * @param bool $unique optional, true if we want to create an unique index |
|
1107 | 1107 | * @return bool query result |
1108 | 1108 | */ |
1109 | - public function createIndex(SugarBean $bean, $fieldDefs, $name, $unique = true) |
|
1110 | - { |
|
1111 | - $sql = $this->createIndexSQL($bean, $fieldDefs, $name, $unique); |
|
1112 | - $tablename = $bean->getTableName(); |
|
1113 | - $msg = "Error creating index $name on table: $tablename:"; |
|
1114 | - return $this->query($sql,true,$msg); |
|
1115 | - } |
|
1116 | - |
|
1117 | - /** |
|
1118 | - * returns a SQL query that creates the indices as defined in metadata |
|
1119 | - * @param array $indices Assoc array with index definitions from vardefs |
|
1120 | - * @param string $table Focus table |
|
1121 | - * @return array Array of SQL queries to generate indices |
|
1122 | - */ |
|
1123 | - public function getConstraintSql($indices, $table) |
|
1124 | - { |
|
1125 | - if (!$this->isFieldArray($indices)) |
|
1126 | - $indices = array($indices); |
|
1127 | - |
|
1128 | - $columns = array(); |
|
1129 | - |
|
1130 | - foreach ($indices as $index) { |
|
1131 | - if(!empty($index['db']) && $index['db'] != $this->dbType) |
|
1132 | - continue; |
|
1133 | - if (isset($index['source']) && $index['source'] != 'db') |
|
1134 | - continue; |
|
1135 | - |
|
1136 | - $sql = $this->add_drop_constraint($table, $index); |
|
1137 | - |
|
1138 | - if(!empty($sql)) { |
|
1139 | - $columns[] = $sql; |
|
1140 | - } |
|
1141 | - } |
|
1142 | - |
|
1143 | - return $columns; |
|
1144 | - } |
|
1145 | - |
|
1146 | - /** |
|
1147 | - * Adds a new indexes |
|
1148 | - * |
|
1149 | - * @param string $tablename |
|
1150 | - * @param array $indexes indexes to add |
|
1151 | - * @param bool $execute true if we want to execute the returned sql statement |
|
1152 | - * @return string SQL statement |
|
1153 | - */ |
|
1154 | - public function addIndexes($tablename, $indexes, $execute = true) |
|
1155 | - { |
|
1156 | - $alters = $this->getConstraintSql($indexes, $tablename); |
|
1157 | - if ($execute) { |
|
1158 | - foreach($alters as $sql) { |
|
1159 | - $this->query($sql, true, "Error adding index: "); |
|
1160 | - } |
|
1161 | - } |
|
1162 | - if(!empty($alters)) { |
|
1163 | - $sql = join(";\n", $alters).";\n"; |
|
1164 | - } else { |
|
1165 | - $sql = ''; |
|
1166 | - } |
|
1167 | - return $sql; |
|
1168 | - } |
|
1169 | - |
|
1170 | - /** |
|
1171 | - * Drops indexes |
|
1172 | - * |
|
1173 | - * @param string $tablename |
|
1174 | - * @param array $indexes indexes to drop |
|
1175 | - * @param bool $execute true if we want to execute the returned sql statement |
|
1176 | - * @return string SQL statement |
|
1177 | - */ |
|
1178 | - public function dropIndexes($tablename, $indexes, $execute = true) |
|
1179 | - { |
|
1180 | - $sqls = array(); |
|
1181 | - foreach ($indexes as $index) { |
|
1182 | - $name =$index['name']; |
|
1183 | - $sqls[$name] = $this->add_drop_constraint($tablename,$index,true); |
|
1184 | - } |
|
1185 | - if (!empty($sqls) && $execute) { |
|
1186 | - foreach($sqls as $name => $sql) { |
|
1187 | - unset(self::$index_descriptions[$tablename][$name]); |
|
1188 | - $this->query($sql); |
|
1189 | - } |
|
1190 | - } |
|
1191 | - if(!empty($sqls)) { |
|
1192 | - return join(";\n",$sqls).";"; |
|
1193 | - } else { |
|
1194 | - return ''; |
|
1195 | - } |
|
1196 | - } |
|
1197 | - |
|
1198 | - /** |
|
1199 | - * Modifies indexes |
|
1200 | - * |
|
1201 | - * @param string $tablename |
|
1202 | - * @param array $indexes indexes to modify |
|
1203 | - * @param bool $execute true if we want to execute the returned sql statement |
|
1204 | - * @return string SQL statement |
|
1205 | - */ |
|
1206 | - public function modifyIndexes($tablename, $indexes, $execute = true) |
|
1207 | - { |
|
1208 | - return $this->dropIndexes($tablename, $indexes, $execute)."\n". |
|
1209 | - $this->addIndexes($tablename, $indexes, $execute); |
|
1210 | - } |
|
1211 | - |
|
1212 | - /** |
|
1213 | - * Adds a column to table identified by field def. |
|
1214 | - * |
|
1215 | - * @param string $tablename |
|
1216 | - * @param array $fieldDefs |
|
1109 | + public function createIndex(SugarBean $bean, $fieldDefs, $name, $unique = true) |
|
1110 | + { |
|
1111 | + $sql = $this->createIndexSQL($bean, $fieldDefs, $name, $unique); |
|
1112 | + $tablename = $bean->getTableName(); |
|
1113 | + $msg = "Error creating index $name on table: $tablename:"; |
|
1114 | + return $this->query($sql,true,$msg); |
|
1115 | + } |
|
1116 | + |
|
1117 | + /** |
|
1118 | + * returns a SQL query that creates the indices as defined in metadata |
|
1119 | + * @param array $indices Assoc array with index definitions from vardefs |
|
1120 | + * @param string $table Focus table |
|
1121 | + * @return array Array of SQL queries to generate indices |
|
1122 | + */ |
|
1123 | + public function getConstraintSql($indices, $table) |
|
1124 | + { |
|
1125 | + if (!$this->isFieldArray($indices)) |
|
1126 | + $indices = array($indices); |
|
1127 | + |
|
1128 | + $columns = array(); |
|
1129 | + |
|
1130 | + foreach ($indices as $index) { |
|
1131 | + if(!empty($index['db']) && $index['db'] != $this->dbType) |
|
1132 | + continue; |
|
1133 | + if (isset($index['source']) && $index['source'] != 'db') |
|
1134 | + continue; |
|
1135 | + |
|
1136 | + $sql = $this->add_drop_constraint($table, $index); |
|
1137 | + |
|
1138 | + if(!empty($sql)) { |
|
1139 | + $columns[] = $sql; |
|
1140 | + } |
|
1141 | + } |
|
1142 | + |
|
1143 | + return $columns; |
|
1144 | + } |
|
1145 | + |
|
1146 | + /** |
|
1147 | + * Adds a new indexes |
|
1148 | + * |
|
1149 | + * @param string $tablename |
|
1150 | + * @param array $indexes indexes to add |
|
1151 | + * @param bool $execute true if we want to execute the returned sql statement |
|
1152 | + * @return string SQL statement |
|
1153 | + */ |
|
1154 | + public function addIndexes($tablename, $indexes, $execute = true) |
|
1155 | + { |
|
1156 | + $alters = $this->getConstraintSql($indexes, $tablename); |
|
1157 | + if ($execute) { |
|
1158 | + foreach($alters as $sql) { |
|
1159 | + $this->query($sql, true, "Error adding index: "); |
|
1160 | + } |
|
1161 | + } |
|
1162 | + if(!empty($alters)) { |
|
1163 | + $sql = join(";\n", $alters).";\n"; |
|
1164 | + } else { |
|
1165 | + $sql = ''; |
|
1166 | + } |
|
1167 | + return $sql; |
|
1168 | + } |
|
1169 | + |
|
1170 | + /** |
|
1171 | + * Drops indexes |
|
1172 | + * |
|
1173 | + * @param string $tablename |
|
1174 | + * @param array $indexes indexes to drop |
|
1175 | + * @param bool $execute true if we want to execute the returned sql statement |
|
1176 | + * @return string SQL statement |
|
1177 | + */ |
|
1178 | + public function dropIndexes($tablename, $indexes, $execute = true) |
|
1179 | + { |
|
1180 | + $sqls = array(); |
|
1181 | + foreach ($indexes as $index) { |
|
1182 | + $name =$index['name']; |
|
1183 | + $sqls[$name] = $this->add_drop_constraint($tablename,$index,true); |
|
1184 | + } |
|
1185 | + if (!empty($sqls) && $execute) { |
|
1186 | + foreach($sqls as $name => $sql) { |
|
1187 | + unset(self::$index_descriptions[$tablename][$name]); |
|
1188 | + $this->query($sql); |
|
1189 | + } |
|
1190 | + } |
|
1191 | + if(!empty($sqls)) { |
|
1192 | + return join(";\n",$sqls).";"; |
|
1193 | + } else { |
|
1194 | + return ''; |
|
1195 | + } |
|
1196 | + } |
|
1197 | + |
|
1198 | + /** |
|
1199 | + * Modifies indexes |
|
1200 | + * |
|
1201 | + * @param string $tablename |
|
1202 | + * @param array $indexes indexes to modify |
|
1203 | + * @param bool $execute true if we want to execute the returned sql statement |
|
1204 | + * @return string SQL statement |
|
1205 | + */ |
|
1206 | + public function modifyIndexes($tablename, $indexes, $execute = true) |
|
1207 | + { |
|
1208 | + return $this->dropIndexes($tablename, $indexes, $execute)."\n". |
|
1209 | + $this->addIndexes($tablename, $indexes, $execute); |
|
1210 | + } |
|
1211 | + |
|
1212 | + /** |
|
1213 | + * Adds a column to table identified by field def. |
|
1214 | + * |
|
1215 | + * @param string $tablename |
|
1216 | + * @param array $fieldDefs |
|
1217 | 1217 | * @return bool query result |
1218 | 1218 | */ |
1219 | - public function addColumn($tablename, $fieldDefs) |
|
1220 | - { |
|
1221 | - $sql = $this->addColumnSQL($tablename, $fieldDefs); |
|
1222 | - if ($this->isFieldArray($fieldDefs)){ |
|
1223 | - $columns = array(); |
|
1224 | - foreach ($fieldDefs as $fieldDef) |
|
1225 | - $columns[] = $fieldDef['name']; |
|
1226 | - $columns = implode(",", $columns); |
|
1227 | - } |
|
1228 | - else { |
|
1229 | - $columns = $fieldDefs['name']; |
|
1230 | - } |
|
1231 | - $msg = "Error adding column(s) $columns on table: $tablename:"; |
|
1232 | - return $this->query($sql,true,$msg); |
|
1233 | - } |
|
1234 | - |
|
1235 | - /** |
|
1236 | - * Alters old column identified by oldFieldDef to new fieldDef. |
|
1237 | - * |
|
1238 | - * @param string $tablename |
|
1239 | - * @param array $newFieldDef |
|
1240 | - * @param bool $ignoreRequired optional, true if we are ignoring this being a required field |
|
1219 | + public function addColumn($tablename, $fieldDefs) |
|
1220 | + { |
|
1221 | + $sql = $this->addColumnSQL($tablename, $fieldDefs); |
|
1222 | + if ($this->isFieldArray($fieldDefs)){ |
|
1223 | + $columns = array(); |
|
1224 | + foreach ($fieldDefs as $fieldDef) |
|
1225 | + $columns[] = $fieldDef['name']; |
|
1226 | + $columns = implode(",", $columns); |
|
1227 | + } |
|
1228 | + else { |
|
1229 | + $columns = $fieldDefs['name']; |
|
1230 | + } |
|
1231 | + $msg = "Error adding column(s) $columns on table: $tablename:"; |
|
1232 | + return $this->query($sql,true,$msg); |
|
1233 | + } |
|
1234 | + |
|
1235 | + /** |
|
1236 | + * Alters old column identified by oldFieldDef to new fieldDef. |
|
1237 | + * |
|
1238 | + * @param string $tablename |
|
1239 | + * @param array $newFieldDef |
|
1240 | + * @param bool $ignoreRequired optional, true if we are ignoring this being a required field |
|
1241 | 1241 | * @return bool query result |
1242 | 1242 | */ |
1243 | - public function alterColumn($tablename, $newFieldDef, $ignoreRequired = false) |
|
1244 | - { |
|
1245 | - $sql = $this->alterColumnSQL($tablename, $newFieldDef,$ignoreRequired); |
|
1246 | - if ($this->isFieldArray($newFieldDef)){ |
|
1247 | - $columns = array(); |
|
1248 | - foreach ($newFieldDef as $fieldDef) { |
|
1249 | - $columns[] = $fieldDef['name']; |
|
1250 | - } |
|
1251 | - $columns = implode(",", $columns); |
|
1252 | - } |
|
1253 | - else { |
|
1254 | - $columns = $newFieldDef['name']; |
|
1255 | - } |
|
1256 | - |
|
1257 | - $msg = "Error altering column(s) $columns on table: $tablename:"; |
|
1258 | - $res = $this->query($sql,true,$msg); |
|
1259 | - if($res) { |
|
1260 | - $this->getTableDescription($tablename, true); // reload table description after altering |
|
1261 | - } |
|
1262 | - return $res; |
|
1263 | - } |
|
1264 | - |
|
1265 | - /** |
|
1266 | - * Drops the table associated with a bean |
|
1267 | - * |
|
1268 | - * @param SugarBean $bean SugarBean instance |
|
1243 | + public function alterColumn($tablename, $newFieldDef, $ignoreRequired = false) |
|
1244 | + { |
|
1245 | + $sql = $this->alterColumnSQL($tablename, $newFieldDef,$ignoreRequired); |
|
1246 | + if ($this->isFieldArray($newFieldDef)){ |
|
1247 | + $columns = array(); |
|
1248 | + foreach ($newFieldDef as $fieldDef) { |
|
1249 | + $columns[] = $fieldDef['name']; |
|
1250 | + } |
|
1251 | + $columns = implode(",", $columns); |
|
1252 | + } |
|
1253 | + else { |
|
1254 | + $columns = $newFieldDef['name']; |
|
1255 | + } |
|
1256 | + |
|
1257 | + $msg = "Error altering column(s) $columns on table: $tablename:"; |
|
1258 | + $res = $this->query($sql,true,$msg); |
|
1259 | + if($res) { |
|
1260 | + $this->getTableDescription($tablename, true); // reload table description after altering |
|
1261 | + } |
|
1262 | + return $res; |
|
1263 | + } |
|
1264 | + |
|
1265 | + /** |
|
1266 | + * Drops the table associated with a bean |
|
1267 | + * |
|
1268 | + * @param SugarBean $bean SugarBean instance |
|
1269 | 1269 | * @return bool query result |
1270 | - */ |
|
1271 | - public function dropTable(SugarBean $bean) |
|
1272 | - { |
|
1273 | - return $this->dropTableName($bean->getTableName()); |
|
1274 | - } |
|
1275 | - |
|
1276 | - /** |
|
1277 | - * Drops the table by name |
|
1278 | - * |
|
1279 | - * @param string $name Table name |
|
1270 | + */ |
|
1271 | + public function dropTable(SugarBean $bean) |
|
1272 | + { |
|
1273 | + return $this->dropTableName($bean->getTableName()); |
|
1274 | + } |
|
1275 | + |
|
1276 | + /** |
|
1277 | + * Drops the table by name |
|
1278 | + * |
|
1279 | + * @param string $name Table name |
|
1280 | 1280 | * @return bool query result |
1281 | - */ |
|
1282 | - public function dropTableName($name) |
|
1283 | - { |
|
1284 | - $sql = $this->dropTableNameSQL($name); |
|
1285 | - return $this->query($sql,true,"Error dropping table $name:"); |
|
1286 | - } |
|
1281 | + */ |
|
1282 | + public function dropTableName($name) |
|
1283 | + { |
|
1284 | + $sql = $this->dropTableNameSQL($name); |
|
1285 | + return $this->query($sql,true,"Error dropping table $name:"); |
|
1286 | + } |
|
1287 | 1287 | |
1288 | 1288 | /** |
1289 | 1289 | * Deletes a column identified by fieldDef. |
@@ -1292,13 +1292,13 @@ discard block |
||
1292 | 1292 | * @param array $fieldDefs Vardef definition of the field |
1293 | 1293 | * @return bool query result |
1294 | 1294 | */ |
1295 | - public function deleteColumn(SugarBean $bean, $fieldDefs) |
|
1296 | - { |
|
1297 | - $tablename = $bean->getTableName(); |
|
1298 | - $sql = $this->dropColumnSQL($tablename, $fieldDefs); |
|
1299 | - $msg = "Error deleting column(s) on table: $tablename:"; |
|
1300 | - return $this->query($sql,true,$msg); |
|
1301 | - } |
|
1295 | + public function deleteColumn(SugarBean $bean, $fieldDefs) |
|
1296 | + { |
|
1297 | + $tablename = $bean->getTableName(); |
|
1298 | + $sql = $this->dropColumnSQL($tablename, $fieldDefs); |
|
1299 | + $msg = "Error deleting column(s) on table: $tablename:"; |
|
1300 | + return $this->query($sql,true,$msg); |
|
1301 | + } |
|
1302 | 1302 | |
1303 | 1303 | /** |
1304 | 1304 | * Generate a set of Insert statements based on the bean given |
@@ -1313,228 +1313,228 @@ discard block |
||
1313 | 1313 | * @param bool $is_related_query |
1314 | 1314 | * @return string SQL insert statement |
1315 | 1315 | */ |
1316 | - public function generateInsertSQL(SugarBean $bean, $select_query, $start, $count = -1, $table, $is_related_query = false) |
|
1317 | - { |
|
1318 | - $this->log->info('call to DBManager::generateInsertSQL() is deprecated'); |
|
1319 | - global $sugar_config; |
|
1320 | - |
|
1321 | - $rows_found = 0; |
|
1322 | - $count_query = $bean->create_list_count_query($select_query); |
|
1323 | - if(!empty($count_query)) |
|
1324 | - { |
|
1325 | - // We have a count query. Run it and get the results. |
|
1326 | - $result = $this->query($count_query, true, "Error running count query for $this->object_name List: "); |
|
1327 | - $assoc = $this->fetchByAssoc($result); |
|
1328 | - if(!empty($assoc['c'])) |
|
1329 | - { |
|
1330 | - $rows_found = $assoc['c']; |
|
1331 | - } |
|
1332 | - } |
|
1333 | - if($count == -1){ |
|
1334 | - $count = $sugar_config['list_max_entries_per_page']; |
|
1335 | - } |
|
1336 | - $next_offset = $start + $count; |
|
1337 | - |
|
1338 | - $result = $this->limitQuery($select_query, $start, $count); |
|
1339 | - // get basic insert |
|
1340 | - $sql = "INSERT INTO ".$table; |
|
1341 | - $custom_sql = "INSERT INTO ".$table."_cstm"; |
|
1342 | - |
|
1343 | - // get field definitions |
|
1344 | - $fields = $bean->getFieldDefinitions(); |
|
1345 | - $custom_fields = array(); |
|
1346 | - |
|
1347 | - if($bean->hasCustomFields()){ |
|
1348 | - foreach ($fields as $fieldDef){ |
|
1349 | - if($fieldDef['source'] == 'custom_fields'){ |
|
1350 | - $custom_fields[$fieldDef['name']] = $fieldDef['name']; |
|
1351 | - } |
|
1352 | - } |
|
1353 | - if(!empty($custom_fields)){ |
|
1354 | - $custom_fields['id_c'] = 'id_c'; |
|
1355 | - $id_field = array('name' => 'id_c', 'custom_type' => 'id',); |
|
1356 | - $fields[] = $id_field; |
|
1357 | - } |
|
1358 | - } |
|
1359 | - |
|
1360 | - // get column names and values |
|
1361 | - $row_array = array(); |
|
1362 | - $columns = array(); |
|
1363 | - $cstm_row_array = array(); |
|
1364 | - $cstm_columns = array(); |
|
1365 | - $built_columns = false; |
|
1366 | - while(($row = $this->fetchByAssoc($result)) != null) |
|
1367 | - { |
|
1368 | - $values = array(); |
|
1369 | - $cstm_values = array(); |
|
1370 | - if(!$is_related_query){ |
|
1371 | - foreach ($fields as $fieldDef) |
|
1372 | - { |
|
1373 | - if(isset($fieldDef['source']) && $fieldDef['source'] != 'db' && $fieldDef['source'] != 'custom_fields') continue; |
|
1374 | - $val = $row[$fieldDef['name']]; |
|
1375 | - |
|
1376 | - //handle auto increment values here only need to do this on insert not create |
|
1377 | - if ($fieldDef['name'] == 'deleted'){ |
|
1378 | - $values['deleted'] = $val; |
|
1379 | - if(!$built_columns){ |
|
1380 | - $columns[] = 'deleted'; |
|
1381 | - } |
|
1382 | - } |
|
1383 | - else |
|
1384 | - { |
|
1385 | - $type = $fieldDef['type']; |
|
1386 | - if(!empty($fieldDef['custom_type'])){ |
|
1387 | - $type = $fieldDef['custom_type']; |
|
1388 | - } |
|
1389 | - // need to do some thing about types of values |
|
1390 | - if($this->dbType == 'mysql' && $val == '' && ($type == 'datetime' || $type == 'date' || $type == 'int' || $type == 'currency' || $type == 'decimal')){ |
|
1391 | - if(!empty($custom_fields[$fieldDef['name']])) |
|
1392 | - $cstm_values[$fieldDef['name']] = 'null'; |
|
1393 | - else |
|
1394 | - $values[$fieldDef['name']] = 'null'; |
|
1395 | - }else{ |
|
1396 | - if(isset($type) && $type=='int') { |
|
1397 | - if(!empty($custom_fields[$fieldDef['name']])) |
|
1398 | - $cstm_values[$fieldDef['name']] = $GLOBALS['db']->quote(from_html($val)); |
|
1399 | - else |
|
1400 | - $values[$fieldDef['name']] = $GLOBALS['db']->quote(from_html($val)); |
|
1401 | - } else { |
|
1402 | - if(!empty($custom_fields[$fieldDef['name']])) |
|
1403 | - $cstm_values[$fieldDef['name']] = "'".$GLOBALS['db']->quote(from_html($val))."'"; |
|
1404 | - else |
|
1405 | - $values[$fieldDef['name']] = "'".$GLOBALS['db']->quote(from_html($val))."'"; |
|
1406 | - } |
|
1407 | - } |
|
1408 | - if(!$built_columns){ |
|
1409 | - if(!empty($custom_fields[$fieldDef['name']])) |
|
1410 | - $cstm_columns[] = $fieldDef['name']; |
|
1411 | - else |
|
1412 | - $columns[] = $fieldDef['name']; |
|
1413 | - } |
|
1414 | - } |
|
1415 | - |
|
1416 | - } |
|
1417 | - } else { |
|
1418 | - foreach ($row as $key=>$val) |
|
1419 | - { |
|
1420 | - if($key != 'orc_row'){ |
|
1421 | - $values[$key] = "'$val'"; |
|
1422 | - if(!$built_columns){ |
|
1423 | - $columns[] = $key; |
|
1424 | - } |
|
1425 | - } |
|
1426 | - } |
|
1427 | - } |
|
1428 | - $built_columns = true; |
|
1429 | - if(!empty($values)){ |
|
1430 | - $row_array[] = $values; |
|
1431 | - } |
|
1432 | - if(!empty($cstm_values) && !empty($cstm_values['id_c']) && (strlen($cstm_values['id_c']) > 7)){ |
|
1433 | - $cstm_row_array[] = $cstm_values; |
|
1434 | - } |
|
1435 | - } |
|
1436 | - |
|
1437 | - //if (sizeof ($values) == 0) return ""; // no columns set |
|
1438 | - |
|
1439 | - // get the entire sql |
|
1440 | - $sql .= "(".implode(",", $columns).") "; |
|
1441 | - $sql .= "VALUES"; |
|
1442 | - for($i = 0; $i < count($row_array); $i++){ |
|
1443 | - $sql .= " (".implode(",", $row_array[$i]).")"; |
|
1444 | - if($i < (count($row_array) - 1)){ |
|
1445 | - $sql .= ", "; |
|
1446 | - } |
|
1447 | - } |
|
1448 | - //custom |
|
1449 | - // get the entire sql |
|
1450 | - $custom_sql .= "(".implode(",", $cstm_columns).") "; |
|
1451 | - $custom_sql .= "VALUES"; |
|
1452 | - |
|
1453 | - for($i = 0; $i < count($cstm_row_array); $i++){ |
|
1454 | - $custom_sql .= " (".implode(",", $cstm_row_array[$i]).")"; |
|
1455 | - if($i < (count($cstm_row_array) - 1)){ |
|
1456 | - $custom_sql .= ", "; |
|
1457 | - } |
|
1458 | - } |
|
1459 | - return array('data' => $sql, 'cstm_sql' => $custom_sql, /*'result_count' => $row_count, */ 'total_count' => $rows_found, 'next_offset' => $next_offset); |
|
1460 | - } |
|
1461 | - |
|
1462 | - /** |
|
1463 | - * @deprecated |
|
1464 | - * Disconnects all instances |
|
1465 | - */ |
|
1466 | - public function disconnectAll() |
|
1467 | - { |
|
1468 | - DBManagerFactory::disconnectAll(); |
|
1469 | - } |
|
1470 | - |
|
1471 | - /** |
|
1472 | - * This function sets the query threshold limit |
|
1473 | - * |
|
1474 | - * @param int $limit value of query threshold limit |
|
1475 | - */ |
|
1476 | - public static function setQueryLimit($limit) |
|
1477 | - { |
|
1478 | - //reset the queryCount |
|
1479 | - self::$queryCount = 0; |
|
1480 | - self::$queryLimit = $limit; |
|
1481 | - } |
|
1482 | - |
|
1483 | - /** |
|
1484 | - * Returns the static queryCount value |
|
1485 | - * |
|
1486 | - * @return int value of the queryCount static variable |
|
1487 | - */ |
|
1488 | - public static function getQueryCount() |
|
1489 | - { |
|
1490 | - return self::$queryCount; |
|
1491 | - } |
|
1492 | - |
|
1493 | - |
|
1494 | - /** |
|
1495 | - * Resets the queryCount value to 0 |
|
1496 | - * |
|
1497 | - */ |
|
1498 | - public static function resetQueryCount() |
|
1499 | - { |
|
1500 | - self::$queryCount = 0; |
|
1501 | - } |
|
1502 | - |
|
1503 | - /** |
|
1504 | - * This function increments the global $sql_queries variable |
|
1505 | - */ |
|
1506 | - public function countQuery() |
|
1507 | - { |
|
1508 | - if (self::$queryLimit != 0 && ++self::$queryCount > self::$queryLimit |
|
1509 | - &&(empty($GLOBALS['current_user']) || !is_admin($GLOBALS['current_user']))) { |
|
1316 | + public function generateInsertSQL(SugarBean $bean, $select_query, $start, $count = -1, $table, $is_related_query = false) |
|
1317 | + { |
|
1318 | + $this->log->info('call to DBManager::generateInsertSQL() is deprecated'); |
|
1319 | + global $sugar_config; |
|
1320 | + |
|
1321 | + $rows_found = 0; |
|
1322 | + $count_query = $bean->create_list_count_query($select_query); |
|
1323 | + if(!empty($count_query)) |
|
1324 | + { |
|
1325 | + // We have a count query. Run it and get the results. |
|
1326 | + $result = $this->query($count_query, true, "Error running count query for $this->object_name List: "); |
|
1327 | + $assoc = $this->fetchByAssoc($result); |
|
1328 | + if(!empty($assoc['c'])) |
|
1329 | + { |
|
1330 | + $rows_found = $assoc['c']; |
|
1331 | + } |
|
1332 | + } |
|
1333 | + if($count == -1){ |
|
1334 | + $count = $sugar_config['list_max_entries_per_page']; |
|
1335 | + } |
|
1336 | + $next_offset = $start + $count; |
|
1337 | + |
|
1338 | + $result = $this->limitQuery($select_query, $start, $count); |
|
1339 | + // get basic insert |
|
1340 | + $sql = "INSERT INTO ".$table; |
|
1341 | + $custom_sql = "INSERT INTO ".$table."_cstm"; |
|
1342 | + |
|
1343 | + // get field definitions |
|
1344 | + $fields = $bean->getFieldDefinitions(); |
|
1345 | + $custom_fields = array(); |
|
1346 | + |
|
1347 | + if($bean->hasCustomFields()){ |
|
1348 | + foreach ($fields as $fieldDef){ |
|
1349 | + if($fieldDef['source'] == 'custom_fields'){ |
|
1350 | + $custom_fields[$fieldDef['name']] = $fieldDef['name']; |
|
1351 | + } |
|
1352 | + } |
|
1353 | + if(!empty($custom_fields)){ |
|
1354 | + $custom_fields['id_c'] = 'id_c'; |
|
1355 | + $id_field = array('name' => 'id_c', 'custom_type' => 'id',); |
|
1356 | + $fields[] = $id_field; |
|
1357 | + } |
|
1358 | + } |
|
1359 | + |
|
1360 | + // get column names and values |
|
1361 | + $row_array = array(); |
|
1362 | + $columns = array(); |
|
1363 | + $cstm_row_array = array(); |
|
1364 | + $cstm_columns = array(); |
|
1365 | + $built_columns = false; |
|
1366 | + while(($row = $this->fetchByAssoc($result)) != null) |
|
1367 | + { |
|
1368 | + $values = array(); |
|
1369 | + $cstm_values = array(); |
|
1370 | + if(!$is_related_query){ |
|
1371 | + foreach ($fields as $fieldDef) |
|
1372 | + { |
|
1373 | + if(isset($fieldDef['source']) && $fieldDef['source'] != 'db' && $fieldDef['source'] != 'custom_fields') continue; |
|
1374 | + $val = $row[$fieldDef['name']]; |
|
1375 | + |
|
1376 | + //handle auto increment values here only need to do this on insert not create |
|
1377 | + if ($fieldDef['name'] == 'deleted'){ |
|
1378 | + $values['deleted'] = $val; |
|
1379 | + if(!$built_columns){ |
|
1380 | + $columns[] = 'deleted'; |
|
1381 | + } |
|
1382 | + } |
|
1383 | + else |
|
1384 | + { |
|
1385 | + $type = $fieldDef['type']; |
|
1386 | + if(!empty($fieldDef['custom_type'])){ |
|
1387 | + $type = $fieldDef['custom_type']; |
|
1388 | + } |
|
1389 | + // need to do some thing about types of values |
|
1390 | + if($this->dbType == 'mysql' && $val == '' && ($type == 'datetime' || $type == 'date' || $type == 'int' || $type == 'currency' || $type == 'decimal')){ |
|
1391 | + if(!empty($custom_fields[$fieldDef['name']])) |
|
1392 | + $cstm_values[$fieldDef['name']] = 'null'; |
|
1393 | + else |
|
1394 | + $values[$fieldDef['name']] = 'null'; |
|
1395 | + }else{ |
|
1396 | + if(isset($type) && $type=='int') { |
|
1397 | + if(!empty($custom_fields[$fieldDef['name']])) |
|
1398 | + $cstm_values[$fieldDef['name']] = $GLOBALS['db']->quote(from_html($val)); |
|
1399 | + else |
|
1400 | + $values[$fieldDef['name']] = $GLOBALS['db']->quote(from_html($val)); |
|
1401 | + } else { |
|
1402 | + if(!empty($custom_fields[$fieldDef['name']])) |
|
1403 | + $cstm_values[$fieldDef['name']] = "'".$GLOBALS['db']->quote(from_html($val))."'"; |
|
1404 | + else |
|
1405 | + $values[$fieldDef['name']] = "'".$GLOBALS['db']->quote(from_html($val))."'"; |
|
1406 | + } |
|
1407 | + } |
|
1408 | + if(!$built_columns){ |
|
1409 | + if(!empty($custom_fields[$fieldDef['name']])) |
|
1410 | + $cstm_columns[] = $fieldDef['name']; |
|
1411 | + else |
|
1412 | + $columns[] = $fieldDef['name']; |
|
1413 | + } |
|
1414 | + } |
|
1415 | + |
|
1416 | + } |
|
1417 | + } else { |
|
1418 | + foreach ($row as $key=>$val) |
|
1419 | + { |
|
1420 | + if($key != 'orc_row'){ |
|
1421 | + $values[$key] = "'$val'"; |
|
1422 | + if(!$built_columns){ |
|
1423 | + $columns[] = $key; |
|
1424 | + } |
|
1425 | + } |
|
1426 | + } |
|
1427 | + } |
|
1428 | + $built_columns = true; |
|
1429 | + if(!empty($values)){ |
|
1430 | + $row_array[] = $values; |
|
1431 | + } |
|
1432 | + if(!empty($cstm_values) && !empty($cstm_values['id_c']) && (strlen($cstm_values['id_c']) > 7)){ |
|
1433 | + $cstm_row_array[] = $cstm_values; |
|
1434 | + } |
|
1435 | + } |
|
1436 | + |
|
1437 | + //if (sizeof ($values) == 0) return ""; // no columns set |
|
1438 | + |
|
1439 | + // get the entire sql |
|
1440 | + $sql .= "(".implode(",", $columns).") "; |
|
1441 | + $sql .= "VALUES"; |
|
1442 | + for($i = 0; $i < count($row_array); $i++){ |
|
1443 | + $sql .= " (".implode(",", $row_array[$i]).")"; |
|
1444 | + if($i < (count($row_array) - 1)){ |
|
1445 | + $sql .= ", "; |
|
1446 | + } |
|
1447 | + } |
|
1448 | + //custom |
|
1449 | + // get the entire sql |
|
1450 | + $custom_sql .= "(".implode(",", $cstm_columns).") "; |
|
1451 | + $custom_sql .= "VALUES"; |
|
1452 | + |
|
1453 | + for($i = 0; $i < count($cstm_row_array); $i++){ |
|
1454 | + $custom_sql .= " (".implode(",", $cstm_row_array[$i]).")"; |
|
1455 | + if($i < (count($cstm_row_array) - 1)){ |
|
1456 | + $custom_sql .= ", "; |
|
1457 | + } |
|
1458 | + } |
|
1459 | + return array('data' => $sql, 'cstm_sql' => $custom_sql, /*'result_count' => $row_count, */ 'total_count' => $rows_found, 'next_offset' => $next_offset); |
|
1460 | + } |
|
1461 | + |
|
1462 | + /** |
|
1463 | + * @deprecated |
|
1464 | + * Disconnects all instances |
|
1465 | + */ |
|
1466 | + public function disconnectAll() |
|
1467 | + { |
|
1468 | + DBManagerFactory::disconnectAll(); |
|
1469 | + } |
|
1470 | + |
|
1471 | + /** |
|
1472 | + * This function sets the query threshold limit |
|
1473 | + * |
|
1474 | + * @param int $limit value of query threshold limit |
|
1475 | + */ |
|
1476 | + public static function setQueryLimit($limit) |
|
1477 | + { |
|
1478 | + //reset the queryCount |
|
1479 | + self::$queryCount = 0; |
|
1480 | + self::$queryLimit = $limit; |
|
1481 | + } |
|
1482 | + |
|
1483 | + /** |
|
1484 | + * Returns the static queryCount value |
|
1485 | + * |
|
1486 | + * @return int value of the queryCount static variable |
|
1487 | + */ |
|
1488 | + public static function getQueryCount() |
|
1489 | + { |
|
1490 | + return self::$queryCount; |
|
1491 | + } |
|
1492 | + |
|
1493 | + |
|
1494 | + /** |
|
1495 | + * Resets the queryCount value to 0 |
|
1496 | + * |
|
1497 | + */ |
|
1498 | + public static function resetQueryCount() |
|
1499 | + { |
|
1500 | + self::$queryCount = 0; |
|
1501 | + } |
|
1502 | + |
|
1503 | + /** |
|
1504 | + * This function increments the global $sql_queries variable |
|
1505 | + */ |
|
1506 | + public function countQuery() |
|
1507 | + { |
|
1508 | + if (self::$queryLimit != 0 && ++self::$queryCount > self::$queryLimit |
|
1509 | + &&(empty($GLOBALS['current_user']) || !is_admin($GLOBALS['current_user']))) { |
|
1510 | 1510 | require_once('include/resource/ResourceManager.php'); |
1511 | 1511 | $resourceManager = ResourceManager::getInstance(); |
1512 | 1512 | $resourceManager->notifyObservers('ERR_QUERY_LIMIT'); |
1513 | - } |
|
1514 | - } |
|
1513 | + } |
|
1514 | + } |
|
1515 | + |
|
1516 | + /** |
|
1517 | + * Pre-process string for quoting |
|
1518 | + * @internal |
|
1519 | + * @param string $string |
|
1520 | + * @return string |
|
1521 | + */ |
|
1522 | + protected function quoteInternal($string) |
|
1523 | + { |
|
1524 | + return from_html($string); |
|
1525 | + } |
|
1515 | 1526 | |
1516 | - /** |
|
1517 | - * Pre-process string for quoting |
|
1518 | - * @internal |
|
1519 | - * @param string $string |
|
1527 | + /** |
|
1528 | + * Return string properly quoted with '' |
|
1529 | + * @param string $string |
|
1520 | 1530 | * @return string |
1521 | 1531 | */ |
1522 | - protected function quoteInternal($string) |
|
1523 | - { |
|
1524 | - return from_html($string); |
|
1525 | - } |
|
1526 | - |
|
1527 | - /** |
|
1528 | - * Return string properly quoted with '' |
|
1529 | - * @param string $string |
|
1530 | - * @return string |
|
1531 | - */ |
|
1532 | - public function quoted($string) |
|
1533 | - { |
|
1534 | - return "'".$this->quote($string)."'"; |
|
1535 | - } |
|
1536 | - |
|
1537 | - /** |
|
1532 | + public function quoted($string) |
|
1533 | + { |
|
1534 | + return "'".$this->quote($string)."'"; |
|
1535 | + } |
|
1536 | + |
|
1537 | + /** |
|
1538 | 1538 | * Quote value according to type |
1539 | 1539 | * Numerics aren't quoted |
1540 | 1540 | * Dates are converted and quoted |
@@ -1544,13 +1544,13 @@ discard block |
||
1544 | 1544 | * @return string Quoted value |
1545 | 1545 | */ |
1546 | 1546 | public function quoteType($type, $value) |
1547 | - { |
|
1548 | - if($type == 'date') { |
|
1549 | - return $this->convert($this->quoted($value), "date"); |
|
1550 | - } |
|
1551 | - if($type == 'time') { |
|
1552 | - return $this->convert($this->quoted($value), "time"); |
|
1553 | - } |
|
1547 | + { |
|
1548 | + if($type == 'date') { |
|
1549 | + return $this->convert($this->quoted($value), "date"); |
|
1550 | + } |
|
1551 | + if($type == 'time') { |
|
1552 | + return $this->convert($this->quoted($value), "time"); |
|
1553 | + } |
|
1554 | 1554 | if(isset($this->type_class[$type]) && $this->type_class[$type] == "date") { |
1555 | 1555 | return $this->convert($this->quoted($value), "datetime"); |
1556 | 1556 | } |
@@ -1559,7 +1559,7 @@ discard block |
||
1559 | 1559 | } |
1560 | 1560 | |
1561 | 1561 | return $this->quoted($value); |
1562 | - } |
|
1562 | + } |
|
1563 | 1563 | |
1564 | 1564 | /** |
1565 | 1565 | * Quote the strings of the passed in array |
@@ -1569,177 +1569,177 @@ discard block |
||
1569 | 1569 | * @param array $array |
1570 | 1570 | * @return array Quoted strings |
1571 | 1571 | */ |
1572 | - public function arrayQuote(array &$array) |
|
1573 | - { |
|
1574 | - foreach($array as &$val) { |
|
1575 | - $val = $this->quote($val); |
|
1576 | - } |
|
1577 | - return $array; |
|
1578 | - } |
|
1572 | + public function arrayQuote(array &$array) |
|
1573 | + { |
|
1574 | + foreach($array as &$val) { |
|
1575 | + $val = $this->quote($val); |
|
1576 | + } |
|
1577 | + return $array; |
|
1578 | + } |
|
1579 | 1579 | |
1580 | 1580 | /** |
1581 | 1581 | * Frees out previous results |
1582 | 1582 | * |
1583 | 1583 | * @param resource|bool $result optional, pass if you want to free a single result instead of all results |
1584 | 1584 | */ |
1585 | - protected function freeResult($result = false) |
|
1586 | - { |
|
1587 | - if($result) { |
|
1588 | - $this->freeDbResult($result); |
|
1589 | - } |
|
1590 | - if($this->lastResult) { |
|
1591 | - $this->freeDbResult($this->lastResult); |
|
1592 | - $this->lastResult = null; |
|
1593 | - } |
|
1594 | - } |
|
1595 | - |
|
1596 | - /** |
|
1597 | - * @abstract |
|
1598 | - * Check if query has LIMIT clause |
|
1599 | - * Relevant for now only for Mysql |
|
1600 | - * @param string $sql |
|
1601 | - * @return bool |
|
1602 | - */ |
|
1603 | - protected function hasLimit($sql) |
|
1604 | - { |
|
1605 | - return false; |
|
1606 | - } |
|
1607 | - |
|
1608 | - /** |
|
1609 | - * Runs a query and returns a single row containing single value |
|
1610 | - * |
|
1611 | - * @param string $sql SQL Statement to execute |
|
1612 | - * @param bool $dieOnError True if we want to call die if the query returns errors |
|
1613 | - * @param string $msg Message to log if error occurs |
|
1614 | - * @return array single value from the query |
|
1615 | - */ |
|
1616 | - public function getOne($sql, $dieOnError = false, $msg = '') |
|
1617 | - { |
|
1618 | - $this->log->info("Get One: |$sql|"); |
|
1619 | - if(!$this->hasLimit($sql)) { |
|
1620 | - $queryresult = $this->limitQuery($sql, 0, 1, $dieOnError, $msg); |
|
1621 | - } else { |
|
1622 | - // support old code that passes LIMIT to sql |
|
1623 | - // works only for mysql, so do not rely on this |
|
1624 | - $queryresult = $this->query($sql, $dieOnError, $msg); |
|
1625 | - } |
|
1626 | - $this->checkError($msg.' Get One Failed:' . $sql, $dieOnError); |
|
1627 | - if (!$queryresult) return false; |
|
1628 | - $row = $this->fetchByAssoc($queryresult); |
|
1629 | - if(!empty($row)) { |
|
1630 | - return array_shift($row); |
|
1631 | - } |
|
1632 | - return false; |
|
1633 | - } |
|
1634 | - |
|
1635 | - /** |
|
1636 | - * Runs a query and returns a single row |
|
1637 | - * |
|
1638 | - * @param string $sql SQL Statement to execute |
|
1639 | - * @param bool $dieOnError True if we want to call die if the query returns errors |
|
1640 | - * @param string $msg Message to log if error occurs |
|
1641 | - * @param bool $suppress Message to log if error occurs |
|
1642 | - * @return array single row from the query |
|
1643 | - */ |
|
1644 | - public function fetchOne($sql, $dieOnError = false, $msg = '', $suppress = false) |
|
1645 | - { |
|
1646 | - $this->log->info("Fetch One: |$sql|"); |
|
1647 | - $this->checkConnection(); |
|
1648 | - $queryresult = $this->query($sql, $dieOnError, $msg); |
|
1649 | - $this->checkError($msg.' Fetch One Failed:' . $sql, $dieOnError); |
|
1650 | - |
|
1651 | - if (!$queryresult) return false; |
|
1652 | - |
|
1653 | - $row = $this->fetchByAssoc($queryresult); |
|
1654 | - if ( !$row ) return false; |
|
1655 | - |
|
1656 | - $this->freeResult($queryresult); |
|
1657 | - return $row; |
|
1658 | - } |
|
1585 | + protected function freeResult($result = false) |
|
1586 | + { |
|
1587 | + if($result) { |
|
1588 | + $this->freeDbResult($result); |
|
1589 | + } |
|
1590 | + if($this->lastResult) { |
|
1591 | + $this->freeDbResult($this->lastResult); |
|
1592 | + $this->lastResult = null; |
|
1593 | + } |
|
1594 | + } |
|
1659 | 1595 | |
1660 | 1596 | /** |
1661 | - * Returns the number of rows affected by the last query |
|
1662 | 1597 | * @abstract |
1663 | - * See also affected_rows capability, will return 0 unless the DB supports it |
|
1664 | - * @param resource $result query result resource |
|
1665 | - * @return int |
|
1598 | + * Check if query has LIMIT clause |
|
1599 | + * Relevant for now only for Mysql |
|
1600 | + * @param string $sql |
|
1601 | + * @return bool |
|
1666 | 1602 | */ |
1667 | - public function getAffectedRowCount($result) |
|
1668 | - { |
|
1669 | - return 0; |
|
1670 | - } |
|
1671 | - |
|
1672 | - /** |
|
1673 | - * Returns the number of rows returned by the result |
|
1674 | - * |
|
1675 | - * This function can't be reliably implemented on most DB, do not use it. |
|
1676 | - * @abstract |
|
1677 | - * @deprecated |
|
1678 | - * @param resource $result |
|
1679 | - * @return int |
|
1680 | - */ |
|
1681 | - public function getRowCount($result) |
|
1682 | - { |
|
1683 | - return 0; |
|
1684 | - } |
|
1685 | - |
|
1686 | - /** |
|
1687 | - * Get table description |
|
1688 | - * @param string $tablename |
|
1603 | + protected function hasLimit($sql) |
|
1604 | + { |
|
1605 | + return false; |
|
1606 | + } |
|
1607 | + |
|
1608 | + /** |
|
1609 | + * Runs a query and returns a single row containing single value |
|
1610 | + * |
|
1611 | + * @param string $sql SQL Statement to execute |
|
1612 | + * @param bool $dieOnError True if we want to call die if the query returns errors |
|
1613 | + * @param string $msg Message to log if error occurs |
|
1614 | + * @return array single value from the query |
|
1615 | + */ |
|
1616 | + public function getOne($sql, $dieOnError = false, $msg = '') |
|
1617 | + { |
|
1618 | + $this->log->info("Get One: |$sql|"); |
|
1619 | + if(!$this->hasLimit($sql)) { |
|
1620 | + $queryresult = $this->limitQuery($sql, 0, 1, $dieOnError, $msg); |
|
1621 | + } else { |
|
1622 | + // support old code that passes LIMIT to sql |
|
1623 | + // works only for mysql, so do not rely on this |
|
1624 | + $queryresult = $this->query($sql, $dieOnError, $msg); |
|
1625 | + } |
|
1626 | + $this->checkError($msg.' Get One Failed:' . $sql, $dieOnError); |
|
1627 | + if (!$queryresult) return false; |
|
1628 | + $row = $this->fetchByAssoc($queryresult); |
|
1629 | + if(!empty($row)) { |
|
1630 | + return array_shift($row); |
|
1631 | + } |
|
1632 | + return false; |
|
1633 | + } |
|
1634 | + |
|
1635 | + /** |
|
1636 | + * Runs a query and returns a single row |
|
1637 | + * |
|
1638 | + * @param string $sql SQL Statement to execute |
|
1639 | + * @param bool $dieOnError True if we want to call die if the query returns errors |
|
1640 | + * @param string $msg Message to log if error occurs |
|
1641 | + * @param bool $suppress Message to log if error occurs |
|
1642 | + * @return array single row from the query |
|
1643 | + */ |
|
1644 | + public function fetchOne($sql, $dieOnError = false, $msg = '', $suppress = false) |
|
1645 | + { |
|
1646 | + $this->log->info("Fetch One: |$sql|"); |
|
1647 | + $this->checkConnection(); |
|
1648 | + $queryresult = $this->query($sql, $dieOnError, $msg); |
|
1649 | + $this->checkError($msg.' Fetch One Failed:' . $sql, $dieOnError); |
|
1650 | + |
|
1651 | + if (!$queryresult) return false; |
|
1652 | + |
|
1653 | + $row = $this->fetchByAssoc($queryresult); |
|
1654 | + if ( !$row ) return false; |
|
1655 | + |
|
1656 | + $this->freeResult($queryresult); |
|
1657 | + return $row; |
|
1658 | + } |
|
1659 | + |
|
1660 | + /** |
|
1661 | + * Returns the number of rows affected by the last query |
|
1662 | + * @abstract |
|
1663 | + * See also affected_rows capability, will return 0 unless the DB supports it |
|
1664 | + * @param resource $result query result resource |
|
1665 | + * @return int |
|
1666 | + */ |
|
1667 | + public function getAffectedRowCount($result) |
|
1668 | + { |
|
1669 | + return 0; |
|
1670 | + } |
|
1671 | + |
|
1672 | + /** |
|
1673 | + * Returns the number of rows returned by the result |
|
1674 | + * |
|
1675 | + * This function can't be reliably implemented on most DB, do not use it. |
|
1676 | + * @abstract |
|
1677 | + * @deprecated |
|
1678 | + * @param resource $result |
|
1679 | + * @return int |
|
1680 | + */ |
|
1681 | + public function getRowCount($result) |
|
1682 | + { |
|
1683 | + return 0; |
|
1684 | + } |
|
1685 | + |
|
1686 | + /** |
|
1687 | + * Get table description |
|
1688 | + * @param string $tablename |
|
1689 | 1689 | * @param bool $reload true means load from DB, false allows using cache |
1690 | 1690 | * @return array Vardef-format table description |
1691 | 1691 | * |
1692 | 1692 | */ |
1693 | - public function getTableDescription($tablename, $reload = false) |
|
1694 | - { |
|
1695 | - if($reload || empty(self::$table_descriptions[$tablename])) { |
|
1696 | - self::$table_descriptions[$tablename] = $this->get_columns($tablename); |
|
1697 | - } |
|
1698 | - return self::$table_descriptions[$tablename]; |
|
1699 | - } |
|
1700 | - |
|
1701 | - /** |
|
1702 | - * Returns the field description for a given field in table |
|
1703 | - * |
|
1704 | - * @param string $name |
|
1705 | - * @param string $tablename |
|
1706 | - * @return array |
|
1707 | - */ |
|
1708 | - protected function describeField($name, $tablename) |
|
1709 | - { |
|
1710 | - $table = $this->getTableDescription($tablename); |
|
1711 | - if(!empty($table) && isset($table[$name])) |
|
1712 | - return $table[$name]; |
|
1713 | - |
|
1714 | - $table = $this->getTableDescription($tablename, true); |
|
1715 | - |
|
1716 | - if(isset($table[$name])) |
|
1717 | - return $table[$name]; |
|
1718 | - |
|
1719 | - return array(); |
|
1720 | - } |
|
1721 | - |
|
1722 | - /** |
|
1723 | - * Returns the index description for a given index in table |
|
1724 | - * |
|
1725 | - * @param string $name |
|
1726 | - * @param string $tablename |
|
1727 | - * @return array |
|
1728 | - */ |
|
1729 | - protected function describeIndex($name, $tablename) |
|
1730 | - { |
|
1731 | - if(isset(self::$index_descriptions[$tablename]) && isset(self::$index_descriptions[$tablename]) && isset(self::$index_descriptions[$tablename][$name])){ |
|
1732 | - return self::$index_descriptions[$tablename][$name]; |
|
1733 | - } |
|
1734 | - |
|
1735 | - self::$index_descriptions[$tablename] = $this->get_indices($tablename); |
|
1736 | - |
|
1737 | - if(isset(self::$index_descriptions[$tablename][$name])){ |
|
1738 | - return self::$index_descriptions[$tablename][$name]; |
|
1739 | - } |
|
1740 | - |
|
1741 | - return array(); |
|
1742 | - } |
|
1693 | + public function getTableDescription($tablename, $reload = false) |
|
1694 | + { |
|
1695 | + if($reload || empty(self::$table_descriptions[$tablename])) { |
|
1696 | + self::$table_descriptions[$tablename] = $this->get_columns($tablename); |
|
1697 | + } |
|
1698 | + return self::$table_descriptions[$tablename]; |
|
1699 | + } |
|
1700 | + |
|
1701 | + /** |
|
1702 | + * Returns the field description for a given field in table |
|
1703 | + * |
|
1704 | + * @param string $name |
|
1705 | + * @param string $tablename |
|
1706 | + * @return array |
|
1707 | + */ |
|
1708 | + protected function describeField($name, $tablename) |
|
1709 | + { |
|
1710 | + $table = $this->getTableDescription($tablename); |
|
1711 | + if(!empty($table) && isset($table[$name])) |
|
1712 | + return $table[$name]; |
|
1713 | + |
|
1714 | + $table = $this->getTableDescription($tablename, true); |
|
1715 | + |
|
1716 | + if(isset($table[$name])) |
|
1717 | + return $table[$name]; |
|
1718 | + |
|
1719 | + return array(); |
|
1720 | + } |
|
1721 | + |
|
1722 | + /** |
|
1723 | + * Returns the index description for a given index in table |
|
1724 | + * |
|
1725 | + * @param string $name |
|
1726 | + * @param string $tablename |
|
1727 | + * @return array |
|
1728 | + */ |
|
1729 | + protected function describeIndex($name, $tablename) |
|
1730 | + { |
|
1731 | + if(isset(self::$index_descriptions[$tablename]) && isset(self::$index_descriptions[$tablename]) && isset(self::$index_descriptions[$tablename][$name])){ |
|
1732 | + return self::$index_descriptions[$tablename][$name]; |
|
1733 | + } |
|
1734 | + |
|
1735 | + self::$index_descriptions[$tablename] = $this->get_indices($tablename); |
|
1736 | + |
|
1737 | + if(isset(self::$index_descriptions[$tablename][$name])){ |
|
1738 | + return self::$index_descriptions[$tablename][$name]; |
|
1739 | + } |
|
1740 | + |
|
1741 | + return array(); |
|
1742 | + } |
|
1743 | 1743 | |
1744 | 1744 | /** |
1745 | 1745 | * Truncates a string to a given length |
@@ -1749,14 +1749,14 @@ discard block |
||
1749 | 1749 | * @return string |
1750 | 1750 | * |
1751 | 1751 | */ |
1752 | - public function truncate($string, $len) |
|
1753 | - { |
|
1754 | - if ( is_numeric($len) && $len > 0) |
|
1755 | - { |
|
1756 | - $string = mb_substr($string,0,(int) $len, "UTF-8"); |
|
1757 | - } |
|
1758 | - return $string; |
|
1759 | - } |
|
1752 | + public function truncate($string, $len) |
|
1753 | + { |
|
1754 | + if ( is_numeric($len) && $len > 0) |
|
1755 | + { |
|
1756 | + $string = mb_substr($string,0,(int) $len, "UTF-8"); |
|
1757 | + } |
|
1758 | + return $string; |
|
1759 | + } |
|
1760 | 1760 | |
1761 | 1761 | /** |
1762 | 1762 | * Returns the database string needed for concatinating multiple database strings together |
@@ -1766,121 +1766,121 @@ discard block |
||
1766 | 1766 | * @param string $space Separator between strings, default is single space |
1767 | 1767 | * @return string |
1768 | 1768 | */ |
1769 | - public function concat($table, array $fields, $space = ' ') |
|
1770 | - { |
|
1771 | - if(empty($fields)) return ''; |
|
1772 | - $elems = array(); |
|
1773 | - $space = $this->quoted($space); |
|
1774 | - foreach ( $fields as $field ) { |
|
1775 | - if(!empty($elems)) $elems[] = $space; |
|
1776 | - $elems[] = $this->convert("$table.$field", 'IFNULL', array("''")); |
|
1777 | - } |
|
1778 | - $first = array_shift($elems); |
|
1779 | - return "LTRIM(RTRIM(".$this->convert($first, 'CONCAT', $elems)."))"; |
|
1780 | - } |
|
1781 | - |
|
1782 | - /** |
|
1783 | - * Given a sql stmt attempt to parse it into the sql and the tokens. Then return the index of this prepared statement |
|
1784 | - * Tokens can come in the following forms: |
|
1785 | - * ? - a scalar which will be quoted |
|
1786 | - * ! - a literal which will not be quoted |
|
1787 | - * & - binary data to read from a file |
|
1788 | - * |
|
1789 | - * @param string $sql The sql to parse |
|
1790 | - * @return int index of the prepared statement to be used with execute |
|
1791 | - */ |
|
1792 | - public function prepareQuery($sql) |
|
1793 | - { |
|
1794 | - //parse out the tokens |
|
1795 | - $tokens = preg_split('/((?<!\\\)[&?!])/', $sql, -1, PREG_SPLIT_DELIM_CAPTURE); |
|
1796 | - |
|
1797 | - //maintain a count of the actual tokens for quick reference in execute |
|
1798 | - $count = 0; |
|
1799 | - |
|
1800 | - $sqlStr = ''; |
|
1801 | - foreach ($tokens as $key => $val) { |
|
1802 | - switch ($val) { |
|
1803 | - case '?' : |
|
1804 | - case '!' : |
|
1805 | - case '&' : |
|
1806 | - $count++; |
|
1807 | - $sqlStr .= '?'; |
|
1808 | - break; |
|
1809 | - |
|
1810 | - default : |
|
1811 | - //escape any special characters |
|
1812 | - $tokens[$key] = preg_replace('/\\\([&?!])/', "\\1", $val); |
|
1813 | - $sqlStr .= $tokens[$key]; |
|
1814 | - break; |
|
1815 | - } // switch |
|
1816 | - } // foreach |
|
1817 | - |
|
1818 | - $this->preparedTokens[] = array('tokens' => $tokens, 'tokenCount' => $count, 'sqlString' => $sqlStr); |
|
1819 | - end($this->preparedTokens); |
|
1820 | - return key($this->preparedTokens); |
|
1821 | - } |
|
1822 | - |
|
1823 | - /** |
|
1824 | - * Takes a prepared stmt index and the data to replace and creates the query and runs it. |
|
1825 | - * |
|
1826 | - * @param int $stmt The index of the prepared statement from preparedTokens |
|
1827 | - * @param array $data The array of data to replace the tokens with. |
|
1828 | - * @return resource result set or false on error |
|
1829 | - */ |
|
1830 | - public function executePreparedQuery($stmt, $data = array()) |
|
1831 | - { |
|
1832 | - if(!empty($this->preparedTokens[$stmt])){ |
|
1833 | - if(!is_array($data)){ |
|
1834 | - $data = array($data); |
|
1835 | - } |
|
1836 | - |
|
1837 | - $pTokens = $this->preparedTokens[$stmt]; |
|
1838 | - |
|
1839 | - //ensure that the number of data elements matches the number of replacement tokens |
|
1840 | - //we found in prepare(). |
|
1841 | - if(count($data) != $pTokens['tokenCount']){ |
|
1842 | - //error the data count did not match the token count |
|
1843 | - return false; |
|
1844 | - } |
|
1845 | - |
|
1846 | - $query = ''; |
|
1847 | - $dataIndex = 0; |
|
1848 | - $tokens = $pTokens['tokens']; |
|
1849 | - foreach ($tokens as $val) { |
|
1850 | - switch ($val) { |
|
1851 | - case '?': |
|
1852 | - $query .= $this->quote($data[$dataIndex++]); |
|
1853 | - break; |
|
1854 | - case '&': |
|
1855 | - $filename = $data[$dataIndex++]; |
|
1856 | - $query .= file_get_contents($filename); |
|
1857 | - break; |
|
1858 | - case '!': |
|
1859 | - $query .= $data[$dataIndex++]; |
|
1860 | - break; |
|
1861 | - default: |
|
1862 | - $query .= $val; |
|
1863 | - break; |
|
1864 | - }//switch |
|
1865 | - }//foreach |
|
1866 | - return $this->query($query); |
|
1867 | - }else{ |
|
1868 | - return false; |
|
1869 | - } |
|
1870 | - } |
|
1871 | - |
|
1872 | - /** |
|
1873 | - * Run both prepare and execute without the client having to run both individually. |
|
1874 | - * |
|
1875 | - * @param string $sql The sql to parse |
|
1876 | - * @param array $data The array of data to replace the tokens with. |
|
1877 | - * @return resource result set or false on error |
|
1878 | - */ |
|
1879 | - public function pQuery($sql, $data = array()) |
|
1880 | - { |
|
1881 | - $stmt = $this->prepareQuery($sql); |
|
1882 | - return $this->executePreparedQuery($stmt, $data); |
|
1883 | - } |
|
1769 | + public function concat($table, array $fields, $space = ' ') |
|
1770 | + { |
|
1771 | + if(empty($fields)) return ''; |
|
1772 | + $elems = array(); |
|
1773 | + $space = $this->quoted($space); |
|
1774 | + foreach ( $fields as $field ) { |
|
1775 | + if(!empty($elems)) $elems[] = $space; |
|
1776 | + $elems[] = $this->convert("$table.$field", 'IFNULL', array("''")); |
|
1777 | + } |
|
1778 | + $first = array_shift($elems); |
|
1779 | + return "LTRIM(RTRIM(".$this->convert($first, 'CONCAT', $elems)."))"; |
|
1780 | + } |
|
1781 | + |
|
1782 | + /** |
|
1783 | + * Given a sql stmt attempt to parse it into the sql and the tokens. Then return the index of this prepared statement |
|
1784 | + * Tokens can come in the following forms: |
|
1785 | + * ? - a scalar which will be quoted |
|
1786 | + * ! - a literal which will not be quoted |
|
1787 | + * & - binary data to read from a file |
|
1788 | + * |
|
1789 | + * @param string $sql The sql to parse |
|
1790 | + * @return int index of the prepared statement to be used with execute |
|
1791 | + */ |
|
1792 | + public function prepareQuery($sql) |
|
1793 | + { |
|
1794 | + //parse out the tokens |
|
1795 | + $tokens = preg_split('/((?<!\\\)[&?!])/', $sql, -1, PREG_SPLIT_DELIM_CAPTURE); |
|
1796 | + |
|
1797 | + //maintain a count of the actual tokens for quick reference in execute |
|
1798 | + $count = 0; |
|
1799 | + |
|
1800 | + $sqlStr = ''; |
|
1801 | + foreach ($tokens as $key => $val) { |
|
1802 | + switch ($val) { |
|
1803 | + case '?' : |
|
1804 | + case '!' : |
|
1805 | + case '&' : |
|
1806 | + $count++; |
|
1807 | + $sqlStr .= '?'; |
|
1808 | + break; |
|
1809 | + |
|
1810 | + default : |
|
1811 | + //escape any special characters |
|
1812 | + $tokens[$key] = preg_replace('/\\\([&?!])/', "\\1", $val); |
|
1813 | + $sqlStr .= $tokens[$key]; |
|
1814 | + break; |
|
1815 | + } // switch |
|
1816 | + } // foreach |
|
1817 | + |
|
1818 | + $this->preparedTokens[] = array('tokens' => $tokens, 'tokenCount' => $count, 'sqlString' => $sqlStr); |
|
1819 | + end($this->preparedTokens); |
|
1820 | + return key($this->preparedTokens); |
|
1821 | + } |
|
1822 | + |
|
1823 | + /** |
|
1824 | + * Takes a prepared stmt index and the data to replace and creates the query and runs it. |
|
1825 | + * |
|
1826 | + * @param int $stmt The index of the prepared statement from preparedTokens |
|
1827 | + * @param array $data The array of data to replace the tokens with. |
|
1828 | + * @return resource result set or false on error |
|
1829 | + */ |
|
1830 | + public function executePreparedQuery($stmt, $data = array()) |
|
1831 | + { |
|
1832 | + if(!empty($this->preparedTokens[$stmt])){ |
|
1833 | + if(!is_array($data)){ |
|
1834 | + $data = array($data); |
|
1835 | + } |
|
1836 | + |
|
1837 | + $pTokens = $this->preparedTokens[$stmt]; |
|
1838 | + |
|
1839 | + //ensure that the number of data elements matches the number of replacement tokens |
|
1840 | + //we found in prepare(). |
|
1841 | + if(count($data) != $pTokens['tokenCount']){ |
|
1842 | + //error the data count did not match the token count |
|
1843 | + return false; |
|
1844 | + } |
|
1845 | + |
|
1846 | + $query = ''; |
|
1847 | + $dataIndex = 0; |
|
1848 | + $tokens = $pTokens['tokens']; |
|
1849 | + foreach ($tokens as $val) { |
|
1850 | + switch ($val) { |
|
1851 | + case '?': |
|
1852 | + $query .= $this->quote($data[$dataIndex++]); |
|
1853 | + break; |
|
1854 | + case '&': |
|
1855 | + $filename = $data[$dataIndex++]; |
|
1856 | + $query .= file_get_contents($filename); |
|
1857 | + break; |
|
1858 | + case '!': |
|
1859 | + $query .= $data[$dataIndex++]; |
|
1860 | + break; |
|
1861 | + default: |
|
1862 | + $query .= $val; |
|
1863 | + break; |
|
1864 | + }//switch |
|
1865 | + }//foreach |
|
1866 | + return $this->query($query); |
|
1867 | + }else{ |
|
1868 | + return false; |
|
1869 | + } |
|
1870 | + } |
|
1871 | + |
|
1872 | + /** |
|
1873 | + * Run both prepare and execute without the client having to run both individually. |
|
1874 | + * |
|
1875 | + * @param string $sql The sql to parse |
|
1876 | + * @param array $data The array of data to replace the tokens with. |
|
1877 | + * @return resource result set or false on error |
|
1878 | + */ |
|
1879 | + public function pQuery($sql, $data = array()) |
|
1880 | + { |
|
1881 | + $stmt = $this->prepareQuery($sql); |
|
1882 | + return $this->executePreparedQuery($stmt, $data); |
|
1883 | + } |
|
1884 | 1884 | |
1885 | 1885 | /********************** SQL FUNCTIONS ****************************/ |
1886 | 1886 | /** |
@@ -1890,933 +1890,933 @@ discard block |
||
1890 | 1890 | * @param SugarBean $bean SugarBean instance |
1891 | 1891 | * @return string SQL Create Table statement |
1892 | 1892 | */ |
1893 | - public function createTableSQL(SugarBean $bean) |
|
1894 | - { |
|
1895 | - $tablename = $bean->getTableName(); |
|
1896 | - $fieldDefs = $bean->getFieldDefinitions(); |
|
1897 | - $indices = $bean->getIndices(); |
|
1898 | - return $this->createTableSQLParams($tablename, $fieldDefs, $indices); |
|
1899 | - } |
|
1900 | - |
|
1901 | - /** |
|
1902 | - * Generates SQL for insert statement. |
|
1903 | - * |
|
1904 | - * @param SugarBean $bean SugarBean instance |
|
1905 | - * @return string SQL Create Table statement |
|
1906 | - */ |
|
1907 | - public function insertSQL(SugarBean $bean) |
|
1908 | - { |
|
1909 | - // get column names and values |
|
1910 | - $sql = $this->insertParams($bean->getTableName(), $bean->getFieldDefinitions(), get_object_vars($bean), |
|
1911 | - isset($bean->field_name_map)?$bean->field_name_map:null, false); |
|
1912 | - return $sql; |
|
1913 | - } |
|
1914 | - |
|
1915 | - /** |
|
1916 | - * Generates SQL for update statement. |
|
1917 | - * |
|
1918 | - * @param SugarBean $bean SugarBean instance |
|
1919 | - * @param array $where Optional, where conditions in an array |
|
1920 | - * @return string SQL Create Table statement |
|
1921 | - */ |
|
1922 | - public function updateSQL(SugarBean $bean, array $where = array()) |
|
1923 | - { |
|
1924 | - $primaryField = $bean->getPrimaryFieldDefinition(); |
|
1925 | - $columns = array(); |
|
1893 | + public function createTableSQL(SugarBean $bean) |
|
1894 | + { |
|
1895 | + $tablename = $bean->getTableName(); |
|
1896 | + $fieldDefs = $bean->getFieldDefinitions(); |
|
1897 | + $indices = $bean->getIndices(); |
|
1898 | + return $this->createTableSQLParams($tablename, $fieldDefs, $indices); |
|
1899 | + } |
|
1900 | + |
|
1901 | + /** |
|
1902 | + * Generates SQL for insert statement. |
|
1903 | + * |
|
1904 | + * @param SugarBean $bean SugarBean instance |
|
1905 | + * @return string SQL Create Table statement |
|
1906 | + */ |
|
1907 | + public function insertSQL(SugarBean $bean) |
|
1908 | + { |
|
1909 | + // get column names and values |
|
1910 | + $sql = $this->insertParams($bean->getTableName(), $bean->getFieldDefinitions(), get_object_vars($bean), |
|
1911 | + isset($bean->field_name_map)?$bean->field_name_map:null, false); |
|
1912 | + return $sql; |
|
1913 | + } |
|
1914 | + |
|
1915 | + /** |
|
1916 | + * Generates SQL for update statement. |
|
1917 | + * |
|
1918 | + * @param SugarBean $bean SugarBean instance |
|
1919 | + * @param array $where Optional, where conditions in an array |
|
1920 | + * @return string SQL Create Table statement |
|
1921 | + */ |
|
1922 | + public function updateSQL(SugarBean $bean, array $where = array()) |
|
1923 | + { |
|
1924 | + $primaryField = $bean->getPrimaryFieldDefinition(); |
|
1925 | + $columns = array(); |
|
1926 | 1926 | $fields = $bean->getFieldDefinitions(); |
1927 | - // get column names and values |
|
1928 | - foreach ($fields as $field => $fieldDef) { |
|
1929 | - if (isset($fieldDef['source']) && $fieldDef['source'] != 'db') continue; |
|
1930 | - // Do not write out the id field on the update statement. |
|
1931 | - // We are not allowed to change ids. |
|
1932 | - if (empty($fieldDef['name']) || $fieldDef['name'] == $primaryField['name']) continue; |
|
1933 | - |
|
1934 | - // If the field is an auto_increment field, then we shouldn't be setting it. This was added |
|
1935 | - // specially for Bugs and Cases which have a number associated with them. |
|
1936 | - if (!empty($bean->field_name_map[$field]['auto_increment'])) continue; |
|
1937 | - |
|
1938 | - //custom fields handle their save separately |
|
1939 | - if(isset($bean->field_name_map) && !empty($bean->field_name_map[$field]['custom_type'])) continue; |
|
1940 | - |
|
1941 | - // no need to clear deleted since we only update not deleted records anyway |
|
1942 | - if($fieldDef['name'] == 'deleted' && empty($bean->deleted)) continue; |
|
1943 | - |
|
1944 | - if(isset($bean->$field)) { |
|
1945 | - $val = from_html($bean->$field); |
|
1946 | - } else { |
|
1947 | - continue; |
|
1948 | - } |
|
1949 | - |
|
1950 | - if(!empty($fieldDef['type']) && $fieldDef['type'] == 'bool'){ |
|
1951 | - $val = $bean->getFieldValue($field); |
|
1952 | - } |
|
1953 | - |
|
1954 | - if(strlen($val) == 0) { |
|
1955 | - if(isset($fieldDef['default']) && strlen($fieldDef['default']) > 0) { |
|
1956 | - $val = $fieldDef['default']; |
|
1957 | - } else { |
|
1958 | - $val = null; |
|
1959 | - } |
|
1960 | - } |
|
1961 | - |
|
1962 | - if(!empty($val) && !empty($fieldDef['len']) && strlen($val) > $fieldDef['len']) { |
|
1963 | - $val = $this->truncate($val, $fieldDef['len']); |
|
1964 | - } |
|
1965 | - $columnName = $this->quoteIdentifier($fieldDef['name']); |
|
1966 | - if(!is_null($val) || !empty($fieldDef['required'])) { |
|
1967 | - $columns[] = "{$columnName}=".$this->massageValue($val, $fieldDef); |
|
1968 | - } elseif($this->isNullable($fieldDef)) { |
|
1969 | - $columns[] = "{$columnName}=NULL"; |
|
1970 | - } else { |
|
1971 | - $columns[] = "{$columnName}=".$this->emptyValue($fieldDef['type']); |
|
1972 | - } |
|
1973 | - } |
|
1974 | - |
|
1975 | - if ( sizeof($columns) == 0 ) |
|
1976 | - return ""; // no columns set |
|
1977 | - |
|
1978 | - // build where clause |
|
1979 | - $where = $this->getWhereClause($bean, $this->updateWhereArray($bean, $where)); |
|
1980 | - if(isset($fields['deleted'])) { |
|
1981 | - $where .= " AND deleted=0"; |
|
1982 | - } |
|
1983 | - |
|
1984 | - return "UPDATE ".$bean->getTableName()." |
|
1927 | + // get column names and values |
|
1928 | + foreach ($fields as $field => $fieldDef) { |
|
1929 | + if (isset($fieldDef['source']) && $fieldDef['source'] != 'db') continue; |
|
1930 | + // Do not write out the id field on the update statement. |
|
1931 | + // We are not allowed to change ids. |
|
1932 | + if (empty($fieldDef['name']) || $fieldDef['name'] == $primaryField['name']) continue; |
|
1933 | + |
|
1934 | + // If the field is an auto_increment field, then we shouldn't be setting it. This was added |
|
1935 | + // specially for Bugs and Cases which have a number associated with them. |
|
1936 | + if (!empty($bean->field_name_map[$field]['auto_increment'])) continue; |
|
1937 | + |
|
1938 | + //custom fields handle their save separately |
|
1939 | + if(isset($bean->field_name_map) && !empty($bean->field_name_map[$field]['custom_type'])) continue; |
|
1940 | + |
|
1941 | + // no need to clear deleted since we only update not deleted records anyway |
|
1942 | + if($fieldDef['name'] == 'deleted' && empty($bean->deleted)) continue; |
|
1943 | + |
|
1944 | + if(isset($bean->$field)) { |
|
1945 | + $val = from_html($bean->$field); |
|
1946 | + } else { |
|
1947 | + continue; |
|
1948 | + } |
|
1949 | + |
|
1950 | + if(!empty($fieldDef['type']) && $fieldDef['type'] == 'bool'){ |
|
1951 | + $val = $bean->getFieldValue($field); |
|
1952 | + } |
|
1953 | + |
|
1954 | + if(strlen($val) == 0) { |
|
1955 | + if(isset($fieldDef['default']) && strlen($fieldDef['default']) > 0) { |
|
1956 | + $val = $fieldDef['default']; |
|
1957 | + } else { |
|
1958 | + $val = null; |
|
1959 | + } |
|
1960 | + } |
|
1961 | + |
|
1962 | + if(!empty($val) && !empty($fieldDef['len']) && strlen($val) > $fieldDef['len']) { |
|
1963 | + $val = $this->truncate($val, $fieldDef['len']); |
|
1964 | + } |
|
1965 | + $columnName = $this->quoteIdentifier($fieldDef['name']); |
|
1966 | + if(!is_null($val) || !empty($fieldDef['required'])) { |
|
1967 | + $columns[] = "{$columnName}=".$this->massageValue($val, $fieldDef); |
|
1968 | + } elseif($this->isNullable($fieldDef)) { |
|
1969 | + $columns[] = "{$columnName}=NULL"; |
|
1970 | + } else { |
|
1971 | + $columns[] = "{$columnName}=".$this->emptyValue($fieldDef['type']); |
|
1972 | + } |
|
1973 | + } |
|
1974 | + |
|
1975 | + if ( sizeof($columns) == 0 ) |
|
1976 | + return ""; // no columns set |
|
1977 | + |
|
1978 | + // build where clause |
|
1979 | + $where = $this->getWhereClause($bean, $this->updateWhereArray($bean, $where)); |
|
1980 | + if(isset($fields['deleted'])) { |
|
1981 | + $where .= " AND deleted=0"; |
|
1982 | + } |
|
1983 | + |
|
1984 | + return "UPDATE ".$bean->getTableName()." |
|
1985 | 1985 | SET ".implode(",", $columns)." |
1986 | 1986 | $where"; |
1987 | - } |
|
1988 | - |
|
1989 | - /** |
|
1990 | - * This method returns a where array so that it has id entry if |
|
1991 | - * where is not an array or is empty |
|
1992 | - * |
|
1993 | - * @param SugarBean $bean SugarBean instance |
|
1994 | - * @param array $where Optional, where conditions in an array |
|
1995 | - * @return array |
|
1996 | - */ |
|
1997 | - protected function updateWhereArray(SugarBean $bean, array $where = array()) |
|
1998 | - { |
|
1999 | - if (count($where) == 0) { |
|
2000 | - $fieldDef = $bean->getPrimaryFieldDefinition(); |
|
2001 | - $primaryColumn = $fieldDef['name']; |
|
2002 | - |
|
2003 | - $val = $bean->getFieldValue($fieldDef['name']); |
|
2004 | - if ($val != FALSE){ |
|
2005 | - $where[$primaryColumn] = $val; |
|
2006 | - } |
|
2007 | - } |
|
2008 | - |
|
2009 | - return $where; |
|
2010 | - } |
|
2011 | - |
|
2012 | - /** |
|
2013 | - * Returns a where clause without the 'where' key word |
|
2014 | - * |
|
2015 | - * The clause returned does not have an 'and' at the beginning and the columns |
|
2016 | - * are joined by 'and'. |
|
2017 | - * |
|
2018 | - * @param string $table table name |
|
2019 | - * @param array $whereArray Optional, where conditions in an array |
|
2020 | - * @return string |
|
2021 | - */ |
|
2022 | - protected function getColumnWhereClause($table, array $whereArray = array()) |
|
2023 | - { |
|
2024 | - $where = array(); |
|
2025 | - foreach ($whereArray as $name => $val) { |
|
2026 | - $op = "="; |
|
2027 | - if (is_array($val)) { |
|
2028 | - $op = "IN"; |
|
2029 | - $temp = array(); |
|
2030 | - foreach ($val as $tval){ |
|
2031 | - $temp[] = $this->quoted($tval); |
|
2032 | - } |
|
2033 | - $val = implode(",", $temp); |
|
2034 | - $val = "($val)"; |
|
2035 | - } else { |
|
2036 | - $val = $this->quoted($val); |
|
2037 | - } |
|
2038 | - |
|
2039 | - $where[] = " $table.$name $op $val"; |
|
2040 | - } |
|
2041 | - |
|
2042 | - if (!empty($where)) |
|
2043 | - return implode(" AND ", $where); |
|
2044 | - |
|
2045 | - return ''; |
|
2046 | - } |
|
2047 | - |
|
2048 | - /** |
|
2049 | - * This method returns a complete where clause built from the |
|
2050 | - * where values specified. |
|
2051 | - * |
|
2052 | - * @param SugarBean $bean SugarBean that describes the table |
|
2053 | - * @param array $whereArray Optional, where conditions in an array |
|
2054 | - * @return string |
|
2055 | - */ |
|
2056 | - protected function getWhereClause(SugarBean $bean, array $whereArray=array()) |
|
2057 | - { |
|
2058 | - return " WHERE " . $this->getColumnWhereClause($bean->getTableName(), $whereArray); |
|
2059 | - } |
|
2060 | - |
|
2061 | - /** |
|
2062 | - * Outputs a correct string for the sql statement according to value |
|
2063 | - * |
|
2064 | - * @param mixed $val |
|
2065 | - * @param array $fieldDef field definition |
|
2066 | - * @return mixed |
|
2067 | - */ |
|
2068 | - public function massageValue($val, $fieldDef) |
|
2069 | - { |
|
2070 | - $type = $this->getFieldType($fieldDef); |
|
2071 | - |
|
2072 | - if(isset($this->type_class[$type])) { |
|
2073 | - // handle some known types |
|
2074 | - switch($this->type_class[$type]) { |
|
2075 | - case 'bool': |
|
2076 | - case 'int': |
|
2077 | - if (!empty($fieldDef['required']) && $val == ''){ |
|
2078 | - if (isset($fieldDef['default'])){ |
|
2079 | - return $fieldDef['default']; |
|
2080 | - } |
|
2081 | - return 0; |
|
2082 | - } |
|
2083 | - return intval($val); |
|
1987 | + } |
|
1988 | + |
|
1989 | + /** |
|
1990 | + * This method returns a where array so that it has id entry if |
|
1991 | + * where is not an array or is empty |
|
1992 | + * |
|
1993 | + * @param SugarBean $bean SugarBean instance |
|
1994 | + * @param array $where Optional, where conditions in an array |
|
1995 | + * @return array |
|
1996 | + */ |
|
1997 | + protected function updateWhereArray(SugarBean $bean, array $where = array()) |
|
1998 | + { |
|
1999 | + if (count($where) == 0) { |
|
2000 | + $fieldDef = $bean->getPrimaryFieldDefinition(); |
|
2001 | + $primaryColumn = $fieldDef['name']; |
|
2002 | + |
|
2003 | + $val = $bean->getFieldValue($fieldDef['name']); |
|
2004 | + if ($val != FALSE){ |
|
2005 | + $where[$primaryColumn] = $val; |
|
2006 | + } |
|
2007 | + } |
|
2008 | + |
|
2009 | + return $where; |
|
2010 | + } |
|
2011 | + |
|
2012 | + /** |
|
2013 | + * Returns a where clause without the 'where' key word |
|
2014 | + * |
|
2015 | + * The clause returned does not have an 'and' at the beginning and the columns |
|
2016 | + * are joined by 'and'. |
|
2017 | + * |
|
2018 | + * @param string $table table name |
|
2019 | + * @param array $whereArray Optional, where conditions in an array |
|
2020 | + * @return string |
|
2021 | + */ |
|
2022 | + protected function getColumnWhereClause($table, array $whereArray = array()) |
|
2023 | + { |
|
2024 | + $where = array(); |
|
2025 | + foreach ($whereArray as $name => $val) { |
|
2026 | + $op = "="; |
|
2027 | + if (is_array($val)) { |
|
2028 | + $op = "IN"; |
|
2029 | + $temp = array(); |
|
2030 | + foreach ($val as $tval){ |
|
2031 | + $temp[] = $this->quoted($tval); |
|
2032 | + } |
|
2033 | + $val = implode(",", $temp); |
|
2034 | + $val = "($val)"; |
|
2035 | + } else { |
|
2036 | + $val = $this->quoted($val); |
|
2037 | + } |
|
2038 | + |
|
2039 | + $where[] = " $table.$name $op $val"; |
|
2040 | + } |
|
2041 | + |
|
2042 | + if (!empty($where)) |
|
2043 | + return implode(" AND ", $where); |
|
2044 | + |
|
2045 | + return ''; |
|
2046 | + } |
|
2047 | + |
|
2048 | + /** |
|
2049 | + * This method returns a complete where clause built from the |
|
2050 | + * where values specified. |
|
2051 | + * |
|
2052 | + * @param SugarBean $bean SugarBean that describes the table |
|
2053 | + * @param array $whereArray Optional, where conditions in an array |
|
2054 | + * @return string |
|
2055 | + */ |
|
2056 | + protected function getWhereClause(SugarBean $bean, array $whereArray=array()) |
|
2057 | + { |
|
2058 | + return " WHERE " . $this->getColumnWhereClause($bean->getTableName(), $whereArray); |
|
2059 | + } |
|
2060 | + |
|
2061 | + /** |
|
2062 | + * Outputs a correct string for the sql statement according to value |
|
2063 | + * |
|
2064 | + * @param mixed $val |
|
2065 | + * @param array $fieldDef field definition |
|
2066 | + * @return mixed |
|
2067 | + */ |
|
2068 | + public function massageValue($val, $fieldDef) |
|
2069 | + { |
|
2070 | + $type = $this->getFieldType($fieldDef); |
|
2071 | + |
|
2072 | + if(isset($this->type_class[$type])) { |
|
2073 | + // handle some known types |
|
2074 | + switch($this->type_class[$type]) { |
|
2075 | + case 'bool': |
|
2076 | + case 'int': |
|
2077 | + if (!empty($fieldDef['required']) && $val == ''){ |
|
2078 | + if (isset($fieldDef['default'])){ |
|
2079 | + return $fieldDef['default']; |
|
2080 | + } |
|
2081 | + return 0; |
|
2082 | + } |
|
2083 | + return intval($val); |
|
2084 | 2084 | case 'bigint' : |
2085 | 2085 | $val = (float)$val; |
2086 | - if (!empty($fieldDef['required']) && $val == false){ |
|
2087 | - if (isset($fieldDef['default'])){ |
|
2088 | - return $fieldDef['default']; |
|
2089 | - } |
|
2090 | - return 0; |
|
2091 | - } |
|
2086 | + if (!empty($fieldDef['required']) && $val == false){ |
|
2087 | + if (isset($fieldDef['default'])){ |
|
2088 | + return $fieldDef['default']; |
|
2089 | + } |
|
2090 | + return 0; |
|
2091 | + } |
|
2092 | 2092 | return $val; |
2093 | - case 'float': |
|
2094 | - if (!empty($fieldDef['required']) && $val == ''){ |
|
2095 | - if (isset($fieldDef['default'])){ |
|
2096 | - return $fieldDef['default']; |
|
2097 | - } |
|
2098 | - return 0; |
|
2099 | - } |
|
2100 | - return floatval($val); |
|
2101 | - case 'time': |
|
2102 | - case 'date': |
|
2103 | - // empty date can't be '', so convert it to either NULL or empty date value |
|
2104 | - if($val == '') { |
|
2105 | - if (!empty($fieldDef['required'])) { |
|
2106 | - if (isset($fieldDef['default'])) { |
|
2107 | - return $fieldDef['default']; |
|
2108 | - } |
|
2109 | - return $this->emptyValue($type); |
|
2110 | - } |
|
2111 | - return "NULL"; |
|
2112 | - } |
|
2113 | - break; |
|
2114 | - } |
|
2115 | - } else { |
|
2116 | - if(!empty($val) && !empty($fieldDef['len']) && strlen($val) > $fieldDef['len']) { |
|
2117 | - $val = $this->truncate($val, $fieldDef['len']); |
|
2118 | - } |
|
2119 | - } |
|
2120 | - |
|
2121 | - if ( is_null($val) ) { |
|
2122 | - if(!empty($fieldDef['required'])) { |
|
2123 | - if (isset($fieldDef['default']) && $fieldDef['default'] != ''){ |
|
2124 | - return $fieldDef['default']; |
|
2125 | - } |
|
2126 | - return $this->emptyValue($type); |
|
2127 | - } else { |
|
2128 | - return "NULL"; |
|
2129 | - } |
|
2130 | - } |
|
2093 | + case 'float': |
|
2094 | + if (!empty($fieldDef['required']) && $val == ''){ |
|
2095 | + if (isset($fieldDef['default'])){ |
|
2096 | + return $fieldDef['default']; |
|
2097 | + } |
|
2098 | + return 0; |
|
2099 | + } |
|
2100 | + return floatval($val); |
|
2101 | + case 'time': |
|
2102 | + case 'date': |
|
2103 | + // empty date can't be '', so convert it to either NULL or empty date value |
|
2104 | + if($val == '') { |
|
2105 | + if (!empty($fieldDef['required'])) { |
|
2106 | + if (isset($fieldDef['default'])) { |
|
2107 | + return $fieldDef['default']; |
|
2108 | + } |
|
2109 | + return $this->emptyValue($type); |
|
2110 | + } |
|
2111 | + return "NULL"; |
|
2112 | + } |
|
2113 | + break; |
|
2114 | + } |
|
2115 | + } else { |
|
2116 | + if(!empty($val) && !empty($fieldDef['len']) && strlen($val) > $fieldDef['len']) { |
|
2117 | + $val = $this->truncate($val, $fieldDef['len']); |
|
2118 | + } |
|
2119 | + } |
|
2120 | + |
|
2121 | + if ( is_null($val) ) { |
|
2122 | + if(!empty($fieldDef['required'])) { |
|
2123 | + if (isset($fieldDef['default']) && $fieldDef['default'] != ''){ |
|
2124 | + return $fieldDef['default']; |
|
2125 | + } |
|
2126 | + return $this->emptyValue($type); |
|
2127 | + } else { |
|
2128 | + return "NULL"; |
|
2129 | + } |
|
2130 | + } |
|
2131 | 2131 | if($type == "datetimecombo") { |
2132 | 2132 | $type = "datetime"; |
2133 | 2133 | } |
2134 | - return $this->convert($this->quoted($val), $type); |
|
2135 | - } |
|
2136 | - |
|
2137 | - /** |
|
2138 | - * Massages the field defintions to fill in anything else the DB backend may add |
|
2139 | - * |
|
2140 | - * @param array $fieldDef |
|
2141 | - * @param string $tablename |
|
2142 | - * @return array |
|
2143 | - */ |
|
2144 | - public function massageFieldDef(&$fieldDef, $tablename) |
|
2145 | - { |
|
2146 | - if ( !isset($fieldDef['dbType']) ) { |
|
2147 | - if ( isset($fieldDef['dbtype']) ) |
|
2148 | - $fieldDef['dbType'] = $fieldDef['dbtype']; |
|
2149 | - else |
|
2150 | - $fieldDef['dbType'] = $fieldDef['type']; |
|
2151 | - } |
|
2152 | - $type = $this->getColumnType($fieldDef['dbType'],$fieldDef['name'],$tablename); |
|
2153 | - $matches = array(); |
|
2134 | + return $this->convert($this->quoted($val), $type); |
|
2135 | + } |
|
2136 | + |
|
2137 | + /** |
|
2138 | + * Massages the field defintions to fill in anything else the DB backend may add |
|
2139 | + * |
|
2140 | + * @param array $fieldDef |
|
2141 | + * @param string $tablename |
|
2142 | + * @return array |
|
2143 | + */ |
|
2144 | + public function massageFieldDef(&$fieldDef, $tablename) |
|
2145 | + { |
|
2146 | + if ( !isset($fieldDef['dbType']) ) { |
|
2147 | + if ( isset($fieldDef['dbtype']) ) |
|
2148 | + $fieldDef['dbType'] = $fieldDef['dbtype']; |
|
2149 | + else |
|
2150 | + $fieldDef['dbType'] = $fieldDef['type']; |
|
2151 | + } |
|
2152 | + $type = $this->getColumnType($fieldDef['dbType'],$fieldDef['name'],$tablename); |
|
2153 | + $matches = array(); |
|
2154 | 2154 | // len can be a number or a string like 'max', for example, nvarchar(max) |
2155 | 2155 | preg_match_all('/(\w+)(?:\(([0-9]+,?[0-9]*|\w+)\)|)/i', $type, $matches); |
2156 | - if ( isset($matches[1][0]) ) |
|
2157 | - $fieldDef['type'] = $matches[1][0]; |
|
2158 | - if ( isset($matches[2][0]) && empty($fieldDef['len']) ) |
|
2159 | - $fieldDef['len'] = $matches[2][0]; |
|
2160 | - if ( !empty($fieldDef['precision']) && is_numeric($fieldDef['precision']) && !strstr($fieldDef['len'],',') ) |
|
2161 | - $fieldDef['len'] .= ",{$fieldDef['precision']}"; |
|
2162 | - if (!empty($fieldDef['required']) || ($fieldDef['name'] == 'id' && !isset($fieldDef['required'])) ) { |
|
2163 | - $fieldDef['required'] = 'true'; |
|
2164 | - } |
|
2165 | - } |
|
2166 | - |
|
2167 | - /** |
|
2168 | - * Take an SQL statement and produce a list of fields used in that select |
|
2169 | - * @param string $selectStatement |
|
2170 | - * @return array |
|
2171 | - */ |
|
2172 | - public function getSelectFieldsFromQuery($selectStatement) |
|
2173 | - { |
|
2174 | - $selectStatement = trim($selectStatement); |
|
2175 | - if (strtoupper(substr($selectStatement, 0, 6)) == "SELECT") |
|
2176 | - $selectStatement = trim(substr($selectStatement, 6)); |
|
2177 | - |
|
2178 | - //Due to sql functions existing in many selects, we can't use php explode |
|
2179 | - $fields = array(); |
|
2180 | - $level = 0; |
|
2181 | - $selectField = ""; |
|
2182 | - $strLen = strlen($selectStatement); |
|
2183 | - for($i = 0; $i < $strLen; $i++) |
|
2184 | - { |
|
2185 | - $char = $selectStatement[$i]; |
|
2186 | - |
|
2187 | - if ($char == "," && $level == 0) |
|
2188 | - { |
|
2189 | - $field = $this->getFieldNameFromSelect(trim($selectField)); |
|
2190 | - $fields[$field] = $selectField; |
|
2191 | - $selectField = ""; |
|
2192 | - } |
|
2193 | - else if ($char == "("){ |
|
2194 | - $level++; |
|
2195 | - $selectField .= $char; |
|
2196 | - } |
|
2197 | - else if($char == ")"){ |
|
2198 | - $level--; |
|
2199 | - $selectField .= $char; |
|
2200 | - |
|
2201 | - |
|
2202 | - }else{ |
|
2203 | - $selectField .= $char; |
|
2204 | - } |
|
2205 | - |
|
2206 | - } |
|
2207 | - $fields[$this->getFieldNameFromSelect($selectField)] = $selectField; |
|
2208 | - return $fields; |
|
2209 | - } |
|
2210 | - |
|
2211 | - /** |
|
2212 | - * returns the field name used in a select |
|
2213 | - * @param string $string SELECT query |
|
2156 | + if ( isset($matches[1][0]) ) |
|
2157 | + $fieldDef['type'] = $matches[1][0]; |
|
2158 | + if ( isset($matches[2][0]) && empty($fieldDef['len']) ) |
|
2159 | + $fieldDef['len'] = $matches[2][0]; |
|
2160 | + if ( !empty($fieldDef['precision']) && is_numeric($fieldDef['precision']) && !strstr($fieldDef['len'],',') ) |
|
2161 | + $fieldDef['len'] .= ",{$fieldDef['precision']}"; |
|
2162 | + if (!empty($fieldDef['required']) || ($fieldDef['name'] == 'id' && !isset($fieldDef['required'])) ) { |
|
2163 | + $fieldDef['required'] = 'true'; |
|
2164 | + } |
|
2165 | + } |
|
2166 | + |
|
2167 | + /** |
|
2168 | + * Take an SQL statement and produce a list of fields used in that select |
|
2169 | + * @param string $selectStatement |
|
2170 | + * @return array |
|
2171 | + */ |
|
2172 | + public function getSelectFieldsFromQuery($selectStatement) |
|
2173 | + { |
|
2174 | + $selectStatement = trim($selectStatement); |
|
2175 | + if (strtoupper(substr($selectStatement, 0, 6)) == "SELECT") |
|
2176 | + $selectStatement = trim(substr($selectStatement, 6)); |
|
2177 | + |
|
2178 | + //Due to sql functions existing in many selects, we can't use php explode |
|
2179 | + $fields = array(); |
|
2180 | + $level = 0; |
|
2181 | + $selectField = ""; |
|
2182 | + $strLen = strlen($selectStatement); |
|
2183 | + for($i = 0; $i < $strLen; $i++) |
|
2184 | + { |
|
2185 | + $char = $selectStatement[$i]; |
|
2186 | + |
|
2187 | + if ($char == "," && $level == 0) |
|
2188 | + { |
|
2189 | + $field = $this->getFieldNameFromSelect(trim($selectField)); |
|
2190 | + $fields[$field] = $selectField; |
|
2191 | + $selectField = ""; |
|
2192 | + } |
|
2193 | + else if ($char == "("){ |
|
2194 | + $level++; |
|
2195 | + $selectField .= $char; |
|
2196 | + } |
|
2197 | + else if($char == ")"){ |
|
2198 | + $level--; |
|
2199 | + $selectField .= $char; |
|
2200 | + |
|
2201 | + |
|
2202 | + }else{ |
|
2203 | + $selectField .= $char; |
|
2204 | + } |
|
2205 | + |
|
2206 | + } |
|
2207 | + $fields[$this->getFieldNameFromSelect($selectField)] = $selectField; |
|
2208 | + return $fields; |
|
2209 | + } |
|
2210 | + |
|
2211 | + /** |
|
2212 | + * returns the field name used in a select |
|
2213 | + * @param string $string SELECT query |
|
2214 | 2214 | * @return string |
2215 | 2215 | */ |
2216 | - protected function getFieldNameFromSelect($string) |
|
2217 | - { |
|
2218 | - if(strncasecmp($string, "DISTINCT ", 9) == 0) { |
|
2219 | - $string = substr($string, 9); |
|
2220 | - } |
|
2221 | - if (stripos($string, " as ") !== false) |
|
2222 | - //"as" used for an alias |
|
2223 | - return trim(substr($string, strripos($string, " as ") + 4)); |
|
2224 | - else if (strrpos($string, " ") != 0) |
|
2225 | - //Space used as a delimiter for an alias |
|
2226 | - return trim(substr($string, strrpos($string, " "))); |
|
2227 | - else if (strpos($string, ".") !== false) |
|
2228 | - //No alias, but a table.field format was used |
|
2229 | - return substr($string, strpos($string, ".") + 1); |
|
2230 | - else |
|
2231 | - //Give up and assume the whole thing is the field name |
|
2232 | - return $string; |
|
2233 | - } |
|
2234 | - |
|
2235 | - /** |
|
2236 | - * Generates SQL for delete statement identified by id. |
|
2237 | - * |
|
2238 | - * @param SugarBean $bean SugarBean instance |
|
2239 | - * @param array $where where conditions in an array |
|
2240 | - * @return string SQL Update Statement |
|
2241 | - */ |
|
2242 | - public function deleteSQL(SugarBean $bean, array $where) |
|
2243 | - { |
|
2244 | - $where = $this->getWhereClause($bean, $this->updateWhereArray($bean, $where)); |
|
2245 | - return "UPDATE ".$bean->getTableName()." SET deleted=1 $where"; |
|
2246 | - } |
|
2216 | + protected function getFieldNameFromSelect($string) |
|
2217 | + { |
|
2218 | + if(strncasecmp($string, "DISTINCT ", 9) == 0) { |
|
2219 | + $string = substr($string, 9); |
|
2220 | + } |
|
2221 | + if (stripos($string, " as ") !== false) |
|
2222 | + //"as" used for an alias |
|
2223 | + return trim(substr($string, strripos($string, " as ") + 4)); |
|
2224 | + else if (strrpos($string, " ") != 0) |
|
2225 | + //Space used as a delimiter for an alias |
|
2226 | + return trim(substr($string, strrpos($string, " "))); |
|
2227 | + else if (strpos($string, ".") !== false) |
|
2228 | + //No alias, but a table.field format was used |
|
2229 | + return substr($string, strpos($string, ".") + 1); |
|
2230 | + else |
|
2231 | + //Give up and assume the whole thing is the field name |
|
2232 | + return $string; |
|
2233 | + } |
|
2234 | + |
|
2235 | + /** |
|
2236 | + * Generates SQL for delete statement identified by id. |
|
2237 | + * |
|
2238 | + * @param SugarBean $bean SugarBean instance |
|
2239 | + * @param array $where where conditions in an array |
|
2240 | + * @return string SQL Update Statement |
|
2241 | + */ |
|
2242 | + public function deleteSQL(SugarBean $bean, array $where) |
|
2243 | + { |
|
2244 | + $where = $this->getWhereClause($bean, $this->updateWhereArray($bean, $where)); |
|
2245 | + return "UPDATE ".$bean->getTableName()." SET deleted=1 $where"; |
|
2246 | + } |
|
2247 | + |
|
2248 | + /** |
|
2249 | + * Generates SQL for select statement for any bean identified by id. |
|
2250 | + * |
|
2251 | + * @param SugarBean $bean SugarBean instance |
|
2252 | + * @param array $where where conditions in an array |
|
2253 | + * @return string SQL Select Statement |
|
2254 | + */ |
|
2255 | + public function retrieveSQL(SugarBean $bean, array $where) |
|
2256 | + { |
|
2257 | + $where = $this->getWhereClause($bean, $this->updateWhereArray($bean, $where)); |
|
2258 | + return "SELECT * FROM ".$bean->getTableName()." $where AND deleted=0"; |
|
2259 | + } |
|
2260 | + |
|
2261 | + /** |
|
2262 | + * This method implements a generic sql for a collection of beans. |
|
2263 | + * |
|
2264 | + * Currently, this function does not support outer joins. |
|
2265 | + * |
|
2266 | + * @param array $beans Array of values returned by get_class method as the keys and a bean as |
|
2267 | + * the value for that key. These beans will be joined in the sql by the key |
|
2268 | + * attribute of field defs. |
|
2269 | + * @param array $cols Optional, columns to be returned with the keys as names of bean |
|
2270 | + * as identified by get_class of bean. Values of this array is the array of fieldDefs |
|
2271 | + * to be returned for a bean. If an empty array is passed, all columns are selected. |
|
2272 | + * @param array $whereClause Optional, values with the keys as names of bean as identified |
|
2273 | + * by get_class of bean. Each value at the first level is an array of values for that |
|
2274 | + * bean identified by name of fields. If we want to pass multiple values for a name, |
|
2275 | + * pass it as an array. If where is not passed, all the rows will be returned. |
|
2276 | + * |
|
2277 | + * @return string SQL Select Statement |
|
2278 | + */ |
|
2279 | + public function retrieveViewSQL(array $beans, array $cols = array(), array $whereClause = array()) |
|
2280 | + { |
|
2281 | + $relations = array(); // stores relations between tables as they are discovered |
|
2282 | + $where = $select = array(); |
|
2283 | + foreach ($beans as $beanID => $bean) { |
|
2284 | + $tableName = $bean->getTableName(); |
|
2285 | + $beanTables[$beanID] = $tableName; |
|
2286 | + |
|
2287 | + $table = $beanID; |
|
2288 | + $tables[$table] = $tableName; |
|
2289 | + $aliases[$tableName][] = $table; |
|
2290 | + |
|
2291 | + // build part of select for this table |
|
2292 | + if (is_array($cols[$beanID])) |
|
2293 | + foreach ($cols[$beanID] as $def) $select[] = $table.".".$def['name']; |
|
2294 | + |
|
2295 | + // build part of where clause |
|
2296 | + if (is_array($whereClause[$beanID])){ |
|
2297 | + $where[] = $this->getColumnWhereClause($table, $whereClause[$beanID]); |
|
2298 | + } |
|
2299 | + // initialize so that it can be used properly in form clause generation |
|
2300 | + $table_used_in_from[$table] = false; |
|
2301 | + |
|
2302 | + $indices = $bean->getIndices(); |
|
2303 | + foreach ($indices as $index){ |
|
2304 | + if ($index['type'] == 'foreign') { |
|
2305 | + $relationship[$table][] = array('foreignTable'=> $index['foreignTable'] |
|
2306 | + ,'foreignColumn'=>$index['foreignField'] |
|
2307 | + ,'localColumn'=> $index['fields'] |
|
2308 | + ); |
|
2309 | + } |
|
2310 | + } |
|
2311 | + $where[] = " $table.deleted = 0"; |
|
2312 | + } |
|
2313 | + |
|
2314 | + // join these clauses |
|
2315 | + $select = !empty($select) ? implode(",", $select) : "*"; |
|
2316 | + $where = implode(" AND ", $where); |
|
2317 | + |
|
2318 | + // generate the from clause. Use relations array to generate outer joins |
|
2319 | + // all the rest of the tables will be used as a simple from |
|
2320 | + // relations table define relations between table1 and table2 through column on table 1 |
|
2321 | + // table2 is assumed to joining through primary key called id |
|
2322 | + $separator = ""; |
|
2323 | + $from = ''; $table_used_in_from = array(); |
|
2324 | + foreach ($relations as $table1 => $rightsidearray){ |
|
2325 | + if ($table_used_in_from[$table1]) continue; // table has been joined |
|
2326 | + |
|
2327 | + $from .= $separator." ".$table1; |
|
2328 | + $table_used_in_from[$table1] = true; |
|
2329 | + foreach ($rightsidearray as $tablearray){ |
|
2330 | + $table2 = $tablearray['foreignTable']; // get foreign table |
|
2331 | + $tableAlias = $aliases[$table2]; // get a list of aliases for this table |
|
2332 | + foreach ($tableAlias as $table2) { |
|
2333 | + //choose first alias that does not match |
|
2334 | + // we are doing this because of self joins. |
|
2335 | + // in case of self joins, the same table will have many aliases. |
|
2336 | + if ($table2 != $table1) break; |
|
2337 | + } |
|
2338 | + |
|
2339 | + $col = $tablearray['foreingColumn']; |
|
2340 | + $name = $tablearray['localColumn']; |
|
2341 | + $from .= " LEFT JOIN $table on ($table1.$name = $table2.$col)"; |
|
2342 | + $table_used_in_from[$table2] = true; |
|
2343 | + } |
|
2344 | + $separator = ","; |
|
2345 | + } |
|
2346 | + |
|
2347 | + return "SELECT $select FROM $from WHERE $where"; |
|
2348 | + } |
|
2349 | + |
|
2350 | + /** |
|
2351 | + * Generates SQL for create index statement for a bean. |
|
2352 | + * |
|
2353 | + * @param SugarBean $bean SugarBean instance |
|
2354 | + * @param array $fields fields used in the index |
|
2355 | + * @param string $name index name |
|
2356 | + * @param bool $unique Optional, set to true if this is an unique index |
|
2357 | + * @return string SQL Select Statement |
|
2358 | + */ |
|
2359 | + public function createIndexSQL(SugarBean $bean, array $fields, $name, $unique = true) |
|
2360 | + { |
|
2361 | + $unique = ($unique) ? "unique" : ""; |
|
2362 | + $tablename = $bean->getTableName(); |
|
2363 | + $columns = array(); |
|
2364 | + // get column names |
|
2365 | + foreach ($fields as $fieldDef) |
|
2366 | + $columns[] = $fieldDef['name']; |
|
2367 | + |
|
2368 | + if (empty($columns)) |
|
2369 | + return ""; |
|
2370 | + |
|
2371 | + $columns = implode(",", $columns); |
|
2372 | + |
|
2373 | + return "CREATE $unique INDEX $name ON $tablename ($columns)"; |
|
2374 | + } |
|
2375 | + |
|
2376 | + /** |
|
2377 | + * Returns the type of the variable in the field |
|
2378 | + * |
|
2379 | + * @param array $fieldDef Vardef-format field def |
|
2380 | + * @return string |
|
2381 | + */ |
|
2382 | + public function getFieldType($fieldDef) |
|
2383 | + { |
|
2384 | + // get the type for db type. if that is not set, |
|
2385 | + // get it from type. This is done so that |
|
2386 | + // we do not have change a lot of existing code |
|
2387 | + // and add dbtype where type is being used for some special |
|
2388 | + // purposes like referring to foreign table etc. |
|
2389 | + if(!empty($fieldDef['dbType'])) |
|
2390 | + return $fieldDef['dbType']; |
|
2391 | + if(!empty($fieldDef['dbtype'])) |
|
2392 | + return $fieldDef['dbtype']; |
|
2393 | + if (!empty($fieldDef['type'])) |
|
2394 | + return $fieldDef['type']; |
|
2395 | + if (!empty($fieldDef['Type'])) |
|
2396 | + return $fieldDef['Type']; |
|
2397 | + if (!empty($fieldDef['data_type'])) |
|
2398 | + return $fieldDef['data_type']; |
|
2399 | + |
|
2400 | + return null; |
|
2401 | + } |
|
2402 | + |
|
2403 | + /** |
|
2404 | + * retrieves the different components from the passed column type as it is used in the type mapping and vardefs |
|
2405 | + * type format: <baseType>[(<len>[,<scale>])] |
|
2406 | + * @param string $type Column type |
|
2407 | + * @return array|bool array containing the different components of the passed in type or false in case the type contains illegal characters |
|
2408 | + */ |
|
2409 | + public function getTypeParts($type) |
|
2410 | + { |
|
2411 | + if(preg_match("#(?P<type>\w+)\s*(?P<arg>\((?P<len>\w+)\s*(,\s*(?P<scale>\d+))*\))*#", $type, $matches)) |
|
2412 | + { |
|
2413 | + $return = array(); // Not returning matches array as such as we don't want to expose the regex make up on the interface |
|
2414 | + $return['baseType'] = $matches['type']; |
|
2415 | + if( isset($matches['arg'])) { |
|
2416 | + $return['arg'] = $matches['arg']; |
|
2417 | + } |
|
2418 | + if( isset($matches['len'])) { |
|
2419 | + $return['len'] = $matches['len']; |
|
2420 | + } |
|
2421 | + if( isset($matches['scale'])) { |
|
2422 | + $return['scale'] = $matches['scale']; |
|
2423 | + } |
|
2424 | + return $return; |
|
2425 | + } else { |
|
2426 | + return false; |
|
2427 | + } |
|
2428 | + } |
|
2429 | + |
|
2430 | + /** |
|
2431 | + * Returns the defintion for a single column |
|
2432 | + * |
|
2433 | + * @param array $fieldDef Vardef-format field def |
|
2434 | + * @param bool $ignoreRequired Optional, true if we should ignore this being a required field |
|
2435 | + * @param string $table Optional, table name |
|
2436 | + * @param bool $return_as_array Optional, true if we should return the result as an array instead of sql |
|
2437 | + * @return string or array if $return_as_array is true |
|
2438 | + */ |
|
2439 | + protected function oneColumnSQLRep($fieldDef, $ignoreRequired = false, $table = '', $return_as_array = false) |
|
2440 | + { |
|
2441 | + $name = $fieldDef['name']; |
|
2442 | + $type = $this->getFieldType($fieldDef); |
|
2443 | + $colType = $this->getColumnType($type); |
|
2444 | + |
|
2445 | + if($parts = $this->getTypeParts($colType)) |
|
2446 | + { |
|
2447 | + $colBaseType = $parts['baseType']; |
|
2448 | + $defLen = isset($parts['len']) ? $parts['len'] : '255'; // Use the mappings length (precision) as default if it exists |
|
2449 | + } |
|
2450 | + |
|
2451 | + if(!empty($fieldDef['len'])) { |
|
2452 | + if (in_array($colBaseType, array( 'nvarchar', 'nchar', 'varchar', 'varchar2', 'char', |
|
2453 | + 'clob', 'blob', 'text'))) { |
|
2454 | + $colType = "$colBaseType(${fieldDef['len']})"; |
|
2455 | + } elseif(($colBaseType == 'decimal' || $colBaseType == 'float')){ |
|
2456 | + if(!empty($fieldDef['precision']) && is_numeric($fieldDef['precision'])) |
|
2457 | + if(strpos($fieldDef['len'],',') === false){ |
|
2458 | + $colType = $colBaseType . "(".$fieldDef['len'].",".$fieldDef['precision'].")"; |
|
2459 | + }else{ |
|
2460 | + $colType = $colBaseType . "(".$fieldDef['len'].")"; |
|
2461 | + } |
|
2462 | + else |
|
2463 | + $colType = $colBaseType . "(".$fieldDef['len'].")"; |
|
2464 | + } |
|
2465 | + } else { |
|
2466 | + if (in_array($colBaseType, array( 'nvarchar', 'nchar', 'varchar', 'varchar2', 'char'))) { |
|
2467 | + $colType = "$colBaseType($defLen)"; |
|
2468 | + } |
|
2469 | + } |
|
2470 | + |
|
2471 | + $default = ''; |
|
2472 | + |
|
2473 | + // Bug #52610 We should have ability don't add DEFAULT part to query for boolean fields |
|
2474 | + if (!empty($fieldDef['no_default'])) |
|
2475 | + { |
|
2476 | + // nothing to do |
|
2477 | + } |
|
2478 | + elseif (isset($fieldDef['default']) && strlen($fieldDef['default']) > 0) |
|
2479 | + { |
|
2480 | + $default = " DEFAULT ".$this->quoted($fieldDef['default']); |
|
2481 | + } |
|
2482 | + elseif (!isset($default) && $type == 'bool') |
|
2483 | + { |
|
2484 | + $default = " DEFAULT 0 "; |
|
2485 | + } |
|
2486 | + |
|
2487 | + $auto_increment = ''; |
|
2488 | + if(!empty($fieldDef['auto_increment']) && $fieldDef['auto_increment']) |
|
2489 | + $auto_increment = $this->setAutoIncrement($table , $fieldDef['name']); |
|
2490 | + |
|
2491 | + $required = 'NULL'; // MySQL defaults to NULL, SQL Server defaults to NOT NULL -- must specify |
|
2492 | + //Starting in 6.0, only ID and auto_increment fields will be NOT NULL in the DB. |
|
2493 | + if ((empty($fieldDef['isnull']) || strtolower($fieldDef['isnull']) == 'false') && |
|
2494 | + (!empty($auto_increment) || $name == 'id' || ($fieldDef['type'] == 'id' && !empty($fieldDef['required'])))) { |
|
2495 | + $required = "NOT NULL"; |
|
2496 | + } |
|
2497 | + // If the field is marked both required & isnull=>false - alwqys make it not null |
|
2498 | + // Use this to ensure primary key fields never defined as null |
|
2499 | + if(isset($fieldDef['isnull']) && (strtolower($fieldDef['isnull']) == 'false' || $fieldDef['isnull'] === false) |
|
2500 | + && !empty($fieldDef['required'])) { |
|
2501 | + $required = "NOT NULL"; |
|
2502 | + } |
|
2503 | + if ($ignoreRequired) |
|
2504 | + $required = ""; |
|
2505 | + |
|
2506 | + if ( $return_as_array ) { |
|
2507 | + return array( |
|
2508 | + 'name' => $name, |
|
2509 | + 'colType' => $colType, |
|
2510 | + 'colBaseType' => $colBaseType, // Adding base type for easier processing in derived classes |
|
2511 | + 'default' => $default, |
|
2512 | + 'required' => $required, |
|
2513 | + 'auto_increment' => $auto_increment, |
|
2514 | + 'full' => "$name $colType $default $required $auto_increment", |
|
2515 | + ); |
|
2516 | + } else { |
|
2517 | + return "$name $colType $default $required $auto_increment"; |
|
2518 | + } |
|
2519 | + } |
|
2520 | + |
|
2521 | + /** |
|
2522 | + * Returns SQL defintions for all columns in a table |
|
2523 | + * |
|
2524 | + * @param array $fieldDefs Vardef-format field def |
|
2525 | + * @param bool $ignoreRequired Optional, true if we should ignor this being a required field |
|
2526 | + * @param string $tablename Optional, table name |
|
2527 | + * @return string SQL column definitions |
|
2528 | + */ |
|
2529 | + protected function columnSQLRep($fieldDefs, $ignoreRequired = false, $tablename) |
|
2530 | + { |
|
2531 | + $columns = array(); |
|
2532 | + |
|
2533 | + if ($this->isFieldArray($fieldDefs)) { |
|
2534 | + foreach ($fieldDefs as $fieldDef) { |
|
2535 | + if(!isset($fieldDef['source']) || $fieldDef['source'] == 'db') { |
|
2536 | + $columns[] = $this->oneColumnSQLRep($fieldDef,false, $tablename); |
|
2537 | + } |
|
2538 | + } |
|
2539 | + $columns = implode(",", $columns); |
|
2540 | + } |
|
2541 | + else { |
|
2542 | + $columns = $this->oneColumnSQLRep($fieldDefs,$ignoreRequired, $tablename); |
|
2543 | + } |
|
2544 | + |
|
2545 | + return $columns; |
|
2546 | + } |
|
2547 | + |
|
2548 | + /** |
|
2549 | + * Returns the next value for an auto increment |
|
2550 | + * @abstract |
|
2551 | + * @param string $table Table name |
|
2552 | + * @param string $field_name Field name |
|
2553 | + * @return string |
|
2554 | + */ |
|
2555 | + public function getAutoIncrement($table, $field_name) |
|
2556 | + { |
|
2557 | + return ""; |
|
2558 | + } |
|
2559 | + |
|
2560 | + /** |
|
2561 | + * Returns the sql for the next value in a sequence |
|
2562 | + * @abstract |
|
2563 | + * @param string $table Table name |
|
2564 | + * @param string $field_name Field name |
|
2565 | + * @return string |
|
2566 | + */ |
|
2567 | + public function getAutoIncrementSQL($table, $field_name) |
|
2568 | + { |
|
2569 | + return ""; |
|
2570 | + } |
|
2571 | + |
|
2572 | + /** |
|
2573 | + * Either creates an auto increment through queries or returns sql for auto increment |
|
2574 | + * that can be appended to the end of column defination (mysql) |
|
2575 | + * @abstract |
|
2576 | + * @param string $table Table name |
|
2577 | + * @param string $field_name Field name |
|
2578 | + * @return string |
|
2579 | + */ |
|
2580 | + protected function setAutoIncrement($table, $field_name) |
|
2581 | + { |
|
2582 | + $this->deleteAutoIncrement($table, $field_name); |
|
2583 | + return ""; |
|
2584 | + } |
|
2585 | + |
|
2586 | + /** |
|
2587 | + * Sets the next auto-increment value of a column to a specific value. |
|
2588 | + * @abstract |
|
2589 | + * @param string $table Table name |
|
2590 | + * @param string $field_name Field name |
|
2591 | + * @param int $start_value Starting autoincrement value |
|
2592 | + * @return string |
|
2593 | + * |
|
2594 | + */ |
|
2595 | + public function setAutoIncrementStart($table, $field_name, $start_value) |
|
2596 | + { |
|
2597 | + return ""; |
|
2598 | + } |
|
2599 | + |
|
2600 | + /** |
|
2601 | + * Deletes an auto increment |
|
2602 | + * @abstract |
|
2603 | + * @param string $table tablename |
|
2604 | + * @param string $field_name |
|
2605 | + */ |
|
2606 | + public function deleteAutoIncrement($table, $field_name) |
|
2607 | + { |
|
2608 | + return; |
|
2609 | + } |
|
2610 | + |
|
2611 | + /** |
|
2612 | + * This method generates sql for adding a column to table identified by field def. |
|
2613 | + * |
|
2614 | + * @param string $tablename |
|
2615 | + * @param array $fieldDefs |
|
2616 | + * @return string SQL statement |
|
2617 | + */ |
|
2618 | + public function addColumnSQL($tablename, $fieldDefs) |
|
2619 | + { |
|
2620 | + return $this->changeColumnSQL($tablename, $fieldDefs, 'add'); |
|
2621 | + } |
|
2622 | + |
|
2623 | + /** |
|
2624 | + * This method genrates sql for altering old column identified by oldFieldDef to new fieldDef. |
|
2625 | + * |
|
2626 | + * @param string $tablename |
|
2627 | + * @param array $newFieldDefs |
|
2628 | + * @param bool $ignorerequired Optional, true if we should ignor this being a required field |
|
2629 | + * @return string|array SQL statement(s) |
|
2630 | + */ |
|
2631 | + public function alterColumnSQL($tablename, $newFieldDefs, $ignorerequired = false) |
|
2632 | + { |
|
2633 | + return $this->changeColumnSQL($tablename, $newFieldDefs, 'modify', $ignorerequired); |
|
2634 | + } |
|
2247 | 2635 | |
2248 | 2636 | /** |
2249 | - * Generates SQL for select statement for any bean identified by id. |
|
2637 | + * Generates SQL for dropping a table. |
|
2250 | 2638 | * |
2251 | - * @param SugarBean $bean SugarBean instance |
|
2252 | - * @param array $where where conditions in an array |
|
2253 | - * @return string SQL Select Statement |
|
2639 | + * @param SugarBean $bean Sugarbean instance |
|
2640 | + * @return string SQL statement |
|
2254 | 2641 | */ |
2255 | - public function retrieveSQL(SugarBean $bean, array $where) |
|
2256 | - { |
|
2257 | - $where = $this->getWhereClause($bean, $this->updateWhereArray($bean, $where)); |
|
2258 | - return "SELECT * FROM ".$bean->getTableName()." $where AND deleted=0"; |
|
2259 | - } |
|
2642 | + public function dropTableSQL(SugarBean $bean) |
|
2643 | + { |
|
2644 | + return $this->dropTableNameSQL($bean->getTableName()); |
|
2645 | + } |
|
2260 | 2646 | |
2261 | 2647 | /** |
2262 | - * This method implements a generic sql for a collection of beans. |
|
2648 | + * Generates SQL for dropping a table. |
|
2263 | 2649 | * |
2264 | - * Currently, this function does not support outer joins. |
|
2650 | + * @param string $name table name |
|
2651 | + * @return string SQL statement |
|
2652 | + */ |
|
2653 | + public function dropTableNameSQL($name) |
|
2654 | + { |
|
2655 | + return "DROP TABLE ".$name; |
|
2656 | + } |
|
2657 | + |
|
2658 | + /** |
|
2659 | + * Generates SQL for truncating a table. |
|
2660 | + * @param string $name table name |
|
2661 | + * @return string |
|
2662 | + */ |
|
2663 | + public function truncateTableSQL($name) |
|
2664 | + { |
|
2665 | + return "TRUNCATE $name"; |
|
2666 | + } |
|
2667 | + |
|
2668 | + /** |
|
2669 | + * This method generates sql that deletes a column identified by fieldDef. |
|
2265 | 2670 | * |
2266 | - * @param array $beans Array of values returned by get_class method as the keys and a bean as |
|
2267 | - * the value for that key. These beans will be joined in the sql by the key |
|
2268 | - * attribute of field defs. |
|
2269 | - * @param array $cols Optional, columns to be returned with the keys as names of bean |
|
2270 | - * as identified by get_class of bean. Values of this array is the array of fieldDefs |
|
2271 | - * to be returned for a bean. If an empty array is passed, all columns are selected. |
|
2272 | - * @param array $whereClause Optional, values with the keys as names of bean as identified |
|
2273 | - * by get_class of bean. Each value at the first level is an array of values for that |
|
2274 | - * bean identified by name of fields. If we want to pass multiple values for a name, |
|
2275 | - * pass it as an array. If where is not passed, all the rows will be returned. |
|
2671 | + * @param SugarBean $bean Sugarbean instance |
|
2672 | + * @param array $fieldDefs |
|
2673 | + * @return string SQL statement |
|
2674 | + */ |
|
2675 | + public function deleteColumnSQL(SugarBean $bean, $fieldDefs) |
|
2676 | + { |
|
2677 | + return $this->dropColumnSQL($bean->getTableName(), $fieldDefs); |
|
2678 | + } |
|
2679 | + |
|
2680 | + /** |
|
2681 | + * This method generates sql that drops a column identified by fieldDef. |
|
2682 | + * Designed to work like the other addColumnSQL() and alterColumnSQL() functions |
|
2276 | 2683 | * |
2277 | - * @return string SQL Select Statement |
|
2684 | + * @param string $tablename |
|
2685 | + * @param array $fieldDefs |
|
2686 | + * @return string SQL statement |
|
2278 | 2687 | */ |
2279 | - public function retrieveViewSQL(array $beans, array $cols = array(), array $whereClause = array()) |
|
2280 | - { |
|
2281 | - $relations = array(); // stores relations between tables as they are discovered |
|
2282 | - $where = $select = array(); |
|
2283 | - foreach ($beans as $beanID => $bean) { |
|
2284 | - $tableName = $bean->getTableName(); |
|
2285 | - $beanTables[$beanID] = $tableName; |
|
2286 | - |
|
2287 | - $table = $beanID; |
|
2288 | - $tables[$table] = $tableName; |
|
2289 | - $aliases[$tableName][] = $table; |
|
2290 | - |
|
2291 | - // build part of select for this table |
|
2292 | - if (is_array($cols[$beanID])) |
|
2293 | - foreach ($cols[$beanID] as $def) $select[] = $table.".".$def['name']; |
|
2294 | - |
|
2295 | - // build part of where clause |
|
2296 | - if (is_array($whereClause[$beanID])){ |
|
2297 | - $where[] = $this->getColumnWhereClause($table, $whereClause[$beanID]); |
|
2298 | - } |
|
2299 | - // initialize so that it can be used properly in form clause generation |
|
2300 | - $table_used_in_from[$table] = false; |
|
2301 | - |
|
2302 | - $indices = $bean->getIndices(); |
|
2303 | - foreach ($indices as $index){ |
|
2304 | - if ($index['type'] == 'foreign') { |
|
2305 | - $relationship[$table][] = array('foreignTable'=> $index['foreignTable'] |
|
2306 | - ,'foreignColumn'=>$index['foreignField'] |
|
2307 | - ,'localColumn'=> $index['fields'] |
|
2308 | - ); |
|
2309 | - } |
|
2310 | - } |
|
2311 | - $where[] = " $table.deleted = 0"; |
|
2312 | - } |
|
2313 | - |
|
2314 | - // join these clauses |
|
2315 | - $select = !empty($select) ? implode(",", $select) : "*"; |
|
2316 | - $where = implode(" AND ", $where); |
|
2317 | - |
|
2318 | - // generate the from clause. Use relations array to generate outer joins |
|
2319 | - // all the rest of the tables will be used as a simple from |
|
2320 | - // relations table define relations between table1 and table2 through column on table 1 |
|
2321 | - // table2 is assumed to joining through primary key called id |
|
2322 | - $separator = ""; |
|
2323 | - $from = ''; $table_used_in_from = array(); |
|
2324 | - foreach ($relations as $table1 => $rightsidearray){ |
|
2325 | - if ($table_used_in_from[$table1]) continue; // table has been joined |
|
2326 | - |
|
2327 | - $from .= $separator." ".$table1; |
|
2328 | - $table_used_in_from[$table1] = true; |
|
2329 | - foreach ($rightsidearray as $tablearray){ |
|
2330 | - $table2 = $tablearray['foreignTable']; // get foreign table |
|
2331 | - $tableAlias = $aliases[$table2]; // get a list of aliases for this table |
|
2332 | - foreach ($tableAlias as $table2) { |
|
2333 | - //choose first alias that does not match |
|
2334 | - // we are doing this because of self joins. |
|
2335 | - // in case of self joins, the same table will have many aliases. |
|
2336 | - if ($table2 != $table1) break; |
|
2337 | - } |
|
2338 | - |
|
2339 | - $col = $tablearray['foreingColumn']; |
|
2340 | - $name = $tablearray['localColumn']; |
|
2341 | - $from .= " LEFT JOIN $table on ($table1.$name = $table2.$col)"; |
|
2342 | - $table_used_in_from[$table2] = true; |
|
2343 | - } |
|
2344 | - $separator = ","; |
|
2345 | - } |
|
2346 | - |
|
2347 | - return "SELECT $select FROM $from WHERE $where"; |
|
2348 | - } |
|
2349 | - |
|
2350 | - /** |
|
2351 | - * Generates SQL for create index statement for a bean. |
|
2352 | - * |
|
2353 | - * @param SugarBean $bean SugarBean instance |
|
2354 | - * @param array $fields fields used in the index |
|
2355 | - * @param string $name index name |
|
2356 | - * @param bool $unique Optional, set to true if this is an unique index |
|
2357 | - * @return string SQL Select Statement |
|
2358 | - */ |
|
2359 | - public function createIndexSQL(SugarBean $bean, array $fields, $name, $unique = true) |
|
2360 | - { |
|
2361 | - $unique = ($unique) ? "unique" : ""; |
|
2362 | - $tablename = $bean->getTableName(); |
|
2363 | - $columns = array(); |
|
2364 | - // get column names |
|
2365 | - foreach ($fields as $fieldDef) |
|
2366 | - $columns[] = $fieldDef['name']; |
|
2367 | - |
|
2368 | - if (empty($columns)) |
|
2369 | - return ""; |
|
2370 | - |
|
2371 | - $columns = implode(",", $columns); |
|
2372 | - |
|
2373 | - return "CREATE $unique INDEX $name ON $tablename ($columns)"; |
|
2374 | - } |
|
2375 | - |
|
2376 | - /** |
|
2377 | - * Returns the type of the variable in the field |
|
2378 | - * |
|
2379 | - * @param array $fieldDef Vardef-format field def |
|
2380 | - * @return string |
|
2381 | - */ |
|
2382 | - public function getFieldType($fieldDef) |
|
2383 | - { |
|
2384 | - // get the type for db type. if that is not set, |
|
2385 | - // get it from type. This is done so that |
|
2386 | - // we do not have change a lot of existing code |
|
2387 | - // and add dbtype where type is being used for some special |
|
2388 | - // purposes like referring to foreign table etc. |
|
2389 | - if(!empty($fieldDef['dbType'])) |
|
2390 | - return $fieldDef['dbType']; |
|
2391 | - if(!empty($fieldDef['dbtype'])) |
|
2392 | - return $fieldDef['dbtype']; |
|
2393 | - if (!empty($fieldDef['type'])) |
|
2394 | - return $fieldDef['type']; |
|
2395 | - if (!empty($fieldDef['Type'])) |
|
2396 | - return $fieldDef['Type']; |
|
2397 | - if (!empty($fieldDef['data_type'])) |
|
2398 | - return $fieldDef['data_type']; |
|
2399 | - |
|
2400 | - return null; |
|
2401 | - } |
|
2688 | + public function dropColumnSQL($tablename, $fieldDefs) |
|
2689 | + { |
|
2690 | + return $this->changeColumnSQL($tablename, $fieldDefs, 'drop'); |
|
2691 | + } |
|
2402 | 2692 | |
2403 | 2693 | /** |
2404 | - * retrieves the different components from the passed column type as it is used in the type mapping and vardefs |
|
2405 | - * type format: <baseType>[(<len>[,<scale>])] |
|
2406 | - * @param string $type Column type |
|
2407 | - * @return array|bool array containing the different components of the passed in type or false in case the type contains illegal characters |
|
2694 | + * Return a version of $proposed that can be used as a column name in any of our supported databases |
|
2695 | + * Practically this means no longer than 25 characters as the smallest identifier length for our supported DBs is 30 chars for Oracle plus we add on at least four characters in some places (for indicies for example) |
|
2696 | + * @param string|array $name Proposed name for the column |
|
2697 | + * @param bool|string $ensureUnique Ensure the name is unique |
|
2698 | + * @param string $type Name type (table, column) |
|
2699 | + * @param bool $force Force new name |
|
2700 | + * @return string|array Valid column name trimmed to right length and with invalid characters removed |
|
2408 | 2701 | */ |
2409 | - public function getTypeParts($type) |
|
2702 | + public function getValidDBName($name, $ensureUnique = false, $type = 'column', $force = false) |
|
2410 | 2703 | { |
2411 | - if(preg_match("#(?P<type>\w+)\s*(?P<arg>\((?P<len>\w+)\s*(,\s*(?P<scale>\d+))*\))*#", $type, $matches)) |
|
2412 | - { |
|
2413 | - $return = array(); // Not returning matches array as such as we don't want to expose the regex make up on the interface |
|
2414 | - $return['baseType'] = $matches['type']; |
|
2415 | - if( isset($matches['arg'])) { |
|
2416 | - $return['arg'] = $matches['arg']; |
|
2704 | + if(is_array($name)) { |
|
2705 | + $result = array(); |
|
2706 | + foreach($name as $field) { |
|
2707 | + $result[] = $this->getValidDBName($field, $ensureUnique, $type); |
|
2417 | 2708 | } |
2418 | - if( isset($matches['len'])) { |
|
2419 | - $return['len'] = $matches['len']; |
|
2709 | + return $result; |
|
2710 | + } else { |
|
2711 | + if(strchr($name, ".")) { |
|
2712 | + // this is a compound name with dots, handle separately |
|
2713 | + $parts = explode(".", $name); |
|
2714 | + if(count($parts) > 2) { |
|
2715 | + // some weird name, cut to table.name |
|
2716 | + array_splice($parts, 0, count($parts)-2); |
|
2717 | + } |
|
2718 | + $parts = $this->getValidDBName($parts, $ensureUnique, $type, $force); |
|
2719 | + return join(".", $parts); |
|
2420 | 2720 | } |
2421 | - if( isset($matches['scale'])) { |
|
2422 | - $return['scale'] = $matches['scale']; |
|
2721 | + // first strip any invalid characters - all but word chars (which is alphanumeric and _) |
|
2722 | + $name = preg_replace( '/[^\w]+/i', '', $name ) ; |
|
2723 | + $len = strlen( $name ) ; |
|
2724 | + $maxLen = empty($this->maxNameLengths[$type]) ? $this->maxNameLengths[$type]['column'] : $this->maxNameLengths[$type]; |
|
2725 | + if ($len <= $maxLen && !$force) { |
|
2726 | + return strtolower($name); |
|
2727 | + } |
|
2728 | + if ($ensureUnique) { |
|
2729 | + $md5str = md5($name); |
|
2730 | + $tail = substr ( $name, -11) ; |
|
2731 | + $temp = substr($md5str , strlen($md5str)-4 ); |
|
2732 | + $result = substr( $name, 0, 10) . $temp . $tail ; |
|
2733 | + } else { |
|
2734 | + $result = substr( $name, 0, 11) . substr( $name, 11 - $maxLen); |
|
2423 | 2735 | } |
2424 | - return $return; |
|
2425 | - } else { |
|
2426 | - return false; |
|
2427 | - } |
|
2428 | - } |
|
2429 | - |
|
2430 | - /** |
|
2431 | - * Returns the defintion for a single column |
|
2432 | - * |
|
2433 | - * @param array $fieldDef Vardef-format field def |
|
2434 | - * @param bool $ignoreRequired Optional, true if we should ignore this being a required field |
|
2435 | - * @param string $table Optional, table name |
|
2436 | - * @param bool $return_as_array Optional, true if we should return the result as an array instead of sql |
|
2437 | - * @return string or array if $return_as_array is true |
|
2438 | - */ |
|
2439 | - protected function oneColumnSQLRep($fieldDef, $ignoreRequired = false, $table = '', $return_as_array = false) |
|
2440 | - { |
|
2441 | - $name = $fieldDef['name']; |
|
2442 | - $type = $this->getFieldType($fieldDef); |
|
2443 | - $colType = $this->getColumnType($type); |
|
2444 | 2736 | |
2445 | - if($parts = $this->getTypeParts($colType)) |
|
2446 | - { |
|
2447 | - $colBaseType = $parts['baseType']; |
|
2448 | - $defLen = isset($parts['len']) ? $parts['len'] : '255'; // Use the mappings length (precision) as default if it exists |
|
2737 | + return strtolower( $result ) ; |
|
2449 | 2738 | } |
2739 | + } |
|
2450 | 2740 | |
2451 | - if(!empty($fieldDef['len'])) { |
|
2452 | - if (in_array($colBaseType, array( 'nvarchar', 'nchar', 'varchar', 'varchar2', 'char', |
|
2453 | - 'clob', 'blob', 'text'))) { |
|
2454 | - $colType = "$colBaseType(${fieldDef['len']})"; |
|
2455 | - } elseif(($colBaseType == 'decimal' || $colBaseType == 'float')){ |
|
2456 | - if(!empty($fieldDef['precision']) && is_numeric($fieldDef['precision'])) |
|
2457 | - if(strpos($fieldDef['len'],',') === false){ |
|
2458 | - $colType = $colBaseType . "(".$fieldDef['len'].",".$fieldDef['precision'].")"; |
|
2459 | - }else{ |
|
2460 | - $colType = $colBaseType . "(".$fieldDef['len'].")"; |
|
2461 | - } |
|
2462 | - else |
|
2463 | - $colType = $colBaseType . "(".$fieldDef['len'].")"; |
|
2464 | - } |
|
2465 | - } else { |
|
2466 | - if (in_array($colBaseType, array( 'nvarchar', 'nchar', 'varchar', 'varchar2', 'char'))) { |
|
2467 | - $colType = "$colBaseType($defLen)"; |
|
2468 | - } |
|
2469 | - } |
|
2741 | + /** |
|
2742 | + * Returns the valid type for a column given the type in fieldDef |
|
2743 | + * |
|
2744 | + * @param string $type field type |
|
2745 | + * @return string valid type for the given field |
|
2746 | + */ |
|
2747 | + public function getColumnType($type) |
|
2748 | + { |
|
2749 | + return isset($this->type_map[$type])?$this->type_map[$type]:$type; |
|
2750 | + } |
|
2470 | 2751 | |
2471 | - $default = ''; |
|
2752 | + /** |
|
2753 | + * Checks to see if passed array is truely an array of defitions |
|
2754 | + * |
|
2755 | + * Such an array may have type as a key but it will point to an array |
|
2756 | + * for a true array of definitions an to a col type for a definition only |
|
2757 | + * |
|
2758 | + * @param mixed $defArray |
|
2759 | + * @return bool |
|
2760 | + */ |
|
2761 | + public function isFieldArray($defArray) |
|
2762 | + { |
|
2763 | + if ( !is_array($defArray) ) |
|
2764 | + return false; |
|
2472 | 2765 | |
2473 | - // Bug #52610 We should have ability don't add DEFAULT part to query for boolean fields |
|
2474 | - if (!empty($fieldDef['no_default'])) |
|
2475 | - { |
|
2476 | - // nothing to do |
|
2477 | - } |
|
2478 | - elseif (isset($fieldDef['default']) && strlen($fieldDef['default']) > 0) |
|
2479 | - { |
|
2480 | - $default = " DEFAULT ".$this->quoted($fieldDef['default']); |
|
2481 | - } |
|
2482 | - elseif (!isset($default) && $type == 'bool') |
|
2483 | - { |
|
2484 | - $default = " DEFAULT 0 "; |
|
2766 | + if ( isset($defArray['type']) ){ |
|
2767 | + // type key exists. May be an array of defs or a simple definition |
|
2768 | + return is_array($defArray['type']); // type is not an array => definition else array |
|
2485 | 2769 | } |
2486 | 2770 | |
2487 | - $auto_increment = ''; |
|
2488 | - if(!empty($fieldDef['auto_increment']) && $fieldDef['auto_increment']) |
|
2489 | - $auto_increment = $this->setAutoIncrement($table , $fieldDef['name']); |
|
2490 | - |
|
2491 | - $required = 'NULL'; // MySQL defaults to NULL, SQL Server defaults to NOT NULL -- must specify |
|
2492 | - //Starting in 6.0, only ID and auto_increment fields will be NOT NULL in the DB. |
|
2493 | - if ((empty($fieldDef['isnull']) || strtolower($fieldDef['isnull']) == 'false') && |
|
2494 | - (!empty($auto_increment) || $name == 'id' || ($fieldDef['type'] == 'id' && !empty($fieldDef['required'])))) { |
|
2495 | - $required = "NOT NULL"; |
|
2496 | - } |
|
2497 | - // If the field is marked both required & isnull=>false - alwqys make it not null |
|
2498 | - // Use this to ensure primary key fields never defined as null |
|
2499 | - if(isset($fieldDef['isnull']) && (strtolower($fieldDef['isnull']) == 'false' || $fieldDef['isnull'] === false) |
|
2500 | - && !empty($fieldDef['required'])) { |
|
2501 | - $required = "NOT NULL"; |
|
2502 | - } |
|
2503 | - if ($ignoreRequired) |
|
2504 | - $required = ""; |
|
2505 | - |
|
2506 | - if ( $return_as_array ) { |
|
2507 | - return array( |
|
2508 | - 'name' => $name, |
|
2509 | - 'colType' => $colType, |
|
2510 | - 'colBaseType' => $colBaseType, // Adding base type for easier processing in derived classes |
|
2511 | - 'default' => $default, |
|
2512 | - 'required' => $required, |
|
2513 | - 'auto_increment' => $auto_increment, |
|
2514 | - 'full' => "$name $colType $default $required $auto_increment", |
|
2515 | - ); |
|
2516 | - } else { |
|
2517 | - return "$name $colType $default $required $auto_increment"; |
|
2518 | - } |
|
2519 | - } |
|
2520 | - |
|
2521 | - /** |
|
2522 | - * Returns SQL defintions for all columns in a table |
|
2523 | - * |
|
2524 | - * @param array $fieldDefs Vardef-format field def |
|
2525 | - * @param bool $ignoreRequired Optional, true if we should ignor this being a required field |
|
2526 | - * @param string $tablename Optional, table name |
|
2527 | - * @return string SQL column definitions |
|
2528 | - */ |
|
2529 | - protected function columnSQLRep($fieldDefs, $ignoreRequired = false, $tablename) |
|
2530 | - { |
|
2531 | - $columns = array(); |
|
2532 | - |
|
2533 | - if ($this->isFieldArray($fieldDefs)) { |
|
2534 | - foreach ($fieldDefs as $fieldDef) { |
|
2535 | - if(!isset($fieldDef['source']) || $fieldDef['source'] == 'db') { |
|
2536 | - $columns[] = $this->oneColumnSQLRep($fieldDef,false, $tablename); |
|
2537 | - } |
|
2538 | - } |
|
2539 | - $columns = implode(",", $columns); |
|
2540 | - } |
|
2541 | - else { |
|
2542 | - $columns = $this->oneColumnSQLRep($fieldDefs,$ignoreRequired, $tablename); |
|
2543 | - } |
|
2544 | - |
|
2545 | - return $columns; |
|
2546 | - } |
|
2547 | - |
|
2548 | - /** |
|
2549 | - * Returns the next value for an auto increment |
|
2550 | - * @abstract |
|
2551 | - * @param string $table Table name |
|
2552 | - * @param string $field_name Field name |
|
2553 | - * @return string |
|
2554 | - */ |
|
2555 | - public function getAutoIncrement($table, $field_name) |
|
2556 | - { |
|
2557 | - return ""; |
|
2558 | - } |
|
2559 | - |
|
2560 | - /** |
|
2561 | - * Returns the sql for the next value in a sequence |
|
2562 | - * @abstract |
|
2563 | - * @param string $table Table name |
|
2564 | - * @param string $field_name Field name |
|
2565 | - * @return string |
|
2566 | - */ |
|
2567 | - public function getAutoIncrementSQL($table, $field_name) |
|
2568 | - { |
|
2569 | - return ""; |
|
2570 | - } |
|
2571 | - |
|
2572 | - /** |
|
2573 | - * Either creates an auto increment through queries or returns sql for auto increment |
|
2574 | - * that can be appended to the end of column defination (mysql) |
|
2575 | - * @abstract |
|
2576 | - * @param string $table Table name |
|
2577 | - * @param string $field_name Field name |
|
2578 | - * @return string |
|
2579 | - */ |
|
2580 | - protected function setAutoIncrement($table, $field_name) |
|
2581 | - { |
|
2582 | - $this->deleteAutoIncrement($table, $field_name); |
|
2583 | - return ""; |
|
2584 | - } |
|
2771 | + // type does not exist. Must be array of definitions |
|
2772 | + return true; |
|
2773 | + } |
|
2585 | 2774 | |
2586 | 2775 | /** |
2587 | - * Sets the next auto-increment value of a column to a specific value. |
|
2588 | - * @abstract |
|
2589 | - * @param string $table Table name |
|
2590 | - * @param string $field_name Field name |
|
2591 | - * @param int $start_value Starting autoincrement value |
|
2592 | - * @return string |
|
2776 | + * returns true if the type can be mapped to a valid column type |
|
2593 | 2777 | * |
2778 | + * @param string $type |
|
2779 | + * @return bool |
|
2594 | 2780 | */ |
2595 | - public function setAutoIncrementStart($table, $field_name, $start_value) |
|
2596 | - { |
|
2597 | - return ""; |
|
2598 | - } |
|
2599 | - |
|
2600 | - /** |
|
2601 | - * Deletes an auto increment |
|
2602 | - * @abstract |
|
2603 | - * @param string $table tablename |
|
2604 | - * @param string $field_name |
|
2605 | - */ |
|
2606 | - public function deleteAutoIncrement($table, $field_name) |
|
2607 | - { |
|
2608 | - return; |
|
2609 | - } |
|
2610 | - |
|
2611 | - /** |
|
2612 | - * This method generates sql for adding a column to table identified by field def. |
|
2613 | - * |
|
2614 | - * @param string $tablename |
|
2615 | - * @param array $fieldDefs |
|
2616 | - * @return string SQL statement |
|
2617 | - */ |
|
2618 | - public function addColumnSQL($tablename, $fieldDefs) |
|
2619 | - { |
|
2620 | - return $this->changeColumnSQL($tablename, $fieldDefs, 'add'); |
|
2621 | - } |
|
2622 | - |
|
2623 | - /** |
|
2624 | - * This method genrates sql for altering old column identified by oldFieldDef to new fieldDef. |
|
2625 | - * |
|
2626 | - * @param string $tablename |
|
2627 | - * @param array $newFieldDefs |
|
2628 | - * @param bool $ignorerequired Optional, true if we should ignor this being a required field |
|
2629 | - * @return string|array SQL statement(s) |
|
2630 | - */ |
|
2631 | - public function alterColumnSQL($tablename, $newFieldDefs, $ignorerequired = false) |
|
2632 | - { |
|
2633 | - return $this->changeColumnSQL($tablename, $newFieldDefs, 'modify', $ignorerequired); |
|
2634 | - } |
|
2635 | - |
|
2636 | - /** |
|
2637 | - * Generates SQL for dropping a table. |
|
2638 | - * |
|
2639 | - * @param SugarBean $bean Sugarbean instance |
|
2640 | - * @return string SQL statement |
|
2641 | - */ |
|
2642 | - public function dropTableSQL(SugarBean $bean) |
|
2643 | - { |
|
2644 | - return $this->dropTableNameSQL($bean->getTableName()); |
|
2645 | - } |
|
2646 | - |
|
2647 | - /** |
|
2648 | - * Generates SQL for dropping a table. |
|
2649 | - * |
|
2650 | - * @param string $name table name |
|
2651 | - * @return string SQL statement |
|
2652 | - */ |
|
2653 | - public function dropTableNameSQL($name) |
|
2654 | - { |
|
2655 | - return "DROP TABLE ".$name; |
|
2656 | - } |
|
2657 | - |
|
2658 | - /** |
|
2659 | - * Generates SQL for truncating a table. |
|
2660 | - * @param string $name table name |
|
2661 | - * @return string |
|
2662 | - */ |
|
2663 | - public function truncateTableSQL($name) |
|
2664 | - { |
|
2665 | - return "TRUNCATE $name"; |
|
2666 | - } |
|
2667 | - |
|
2668 | - /** |
|
2669 | - * This method generates sql that deletes a column identified by fieldDef. |
|
2670 | - * |
|
2671 | - * @param SugarBean $bean Sugarbean instance |
|
2672 | - * @param array $fieldDefs |
|
2673 | - * @return string SQL statement |
|
2674 | - */ |
|
2675 | - public function deleteColumnSQL(SugarBean $bean, $fieldDefs) |
|
2676 | - { |
|
2677 | - return $this->dropColumnSQL($bean->getTableName(), $fieldDefs); |
|
2678 | - } |
|
2679 | - |
|
2680 | - /** |
|
2681 | - * This method generates sql that drops a column identified by fieldDef. |
|
2682 | - * Designed to work like the other addColumnSQL() and alterColumnSQL() functions |
|
2683 | - * |
|
2684 | - * @param string $tablename |
|
2685 | - * @param array $fieldDefs |
|
2686 | - * @return string SQL statement |
|
2687 | - */ |
|
2688 | - public function dropColumnSQL($tablename, $fieldDefs) |
|
2689 | - { |
|
2690 | - return $this->changeColumnSQL($tablename, $fieldDefs, 'drop'); |
|
2691 | - } |
|
2781 | + protected function validColumnType($type) |
|
2782 | + { |
|
2783 | + $type = $this->getColumnType($type); |
|
2784 | + return !empty($type); |
|
2785 | + } |
|
2692 | 2786 | |
2693 | 2787 | /** |
2694 | - * Return a version of $proposed that can be used as a column name in any of our supported databases |
|
2695 | - * Practically this means no longer than 25 characters as the smallest identifier length for our supported DBs is 30 chars for Oracle plus we add on at least four characters in some places (for indicies for example) |
|
2696 | - * @param string|array $name Proposed name for the column |
|
2697 | - * @param bool|string $ensureUnique Ensure the name is unique |
|
2698 | - * @param string $type Name type (table, column) |
|
2699 | - * @param bool $force Force new name |
|
2700 | - * @return string|array Valid column name trimmed to right length and with invalid characters removed |
|
2701 | - */ |
|
2702 | - public function getValidDBName($name, $ensureUnique = false, $type = 'column', $force = false) |
|
2703 | - { |
|
2704 | - if(is_array($name)) { |
|
2705 | - $result = array(); |
|
2706 | - foreach($name as $field) { |
|
2707 | - $result[] = $this->getValidDBName($field, $ensureUnique, $type); |
|
2708 | - } |
|
2709 | - return $result; |
|
2710 | - } else { |
|
2711 | - if(strchr($name, ".")) { |
|
2712 | - // this is a compound name with dots, handle separately |
|
2713 | - $parts = explode(".", $name); |
|
2714 | - if(count($parts) > 2) { |
|
2715 | - // some weird name, cut to table.name |
|
2716 | - array_splice($parts, 0, count($parts)-2); |
|
2717 | - } |
|
2718 | - $parts = $this->getValidDBName($parts, $ensureUnique, $type, $force); |
|
2719 | - return join(".", $parts); |
|
2720 | - } |
|
2721 | - // first strip any invalid characters - all but word chars (which is alphanumeric and _) |
|
2722 | - $name = preg_replace( '/[^\w]+/i', '', $name ) ; |
|
2723 | - $len = strlen( $name ) ; |
|
2724 | - $maxLen = empty($this->maxNameLengths[$type]) ? $this->maxNameLengths[$type]['column'] : $this->maxNameLengths[$type]; |
|
2725 | - if ($len <= $maxLen && !$force) { |
|
2726 | - return strtolower($name); |
|
2727 | - } |
|
2728 | - if ($ensureUnique) { |
|
2729 | - $md5str = md5($name); |
|
2730 | - $tail = substr ( $name, -11) ; |
|
2731 | - $temp = substr($md5str , strlen($md5str)-4 ); |
|
2732 | - $result = substr( $name, 0, 10) . $temp . $tail ; |
|
2733 | - } else { |
|
2734 | - $result = substr( $name, 0, 11) . substr( $name, 11 - $maxLen); |
|
2735 | - } |
|
2736 | - |
|
2737 | - return strtolower( $result ) ; |
|
2738 | - } |
|
2739 | - } |
|
2740 | - |
|
2741 | - /** |
|
2742 | - * Returns the valid type for a column given the type in fieldDef |
|
2743 | - * |
|
2744 | - * @param string $type field type |
|
2745 | - * @return string valid type for the given field |
|
2746 | - */ |
|
2747 | - public function getColumnType($type) |
|
2748 | - { |
|
2749 | - return isset($this->type_map[$type])?$this->type_map[$type]:$type; |
|
2750 | - } |
|
2751 | - |
|
2752 | - /** |
|
2753 | - * Checks to see if passed array is truely an array of defitions |
|
2754 | - * |
|
2755 | - * Such an array may have type as a key but it will point to an array |
|
2756 | - * for a true array of definitions an to a col type for a definition only |
|
2757 | - * |
|
2758 | - * @param mixed $defArray |
|
2759 | - * @return bool |
|
2760 | - */ |
|
2761 | - public function isFieldArray($defArray) |
|
2762 | - { |
|
2763 | - if ( !is_array($defArray) ) |
|
2764 | - return false; |
|
2765 | - |
|
2766 | - if ( isset($defArray['type']) ){ |
|
2767 | - // type key exists. May be an array of defs or a simple definition |
|
2768 | - return is_array($defArray['type']); // type is not an array => definition else array |
|
2769 | - } |
|
2770 | - |
|
2771 | - // type does not exist. Must be array of definitions |
|
2772 | - return true; |
|
2773 | - } |
|
2774 | - |
|
2775 | - /** |
|
2776 | - * returns true if the type can be mapped to a valid column type |
|
2777 | - * |
|
2778 | - * @param string $type |
|
2779 | - * @return bool |
|
2780 | - */ |
|
2781 | - protected function validColumnType($type) |
|
2782 | - { |
|
2783 | - $type = $this->getColumnType($type); |
|
2784 | - return !empty($type); |
|
2785 | - } |
|
2786 | - |
|
2787 | - /** |
|
2788 | - * Generate query for audit table |
|
2789 | - * @param SugarBean $bean SugarBean that was changed |
|
2790 | - * @param array $changes List of changes, contains 'before' and 'after' |
|
2788 | + * Generate query for audit table |
|
2789 | + * @param SugarBean $bean SugarBean that was changed |
|
2790 | + * @param array $changes List of changes, contains 'before' and 'after' |
|
2791 | 2791 | * @return string Audit table INSERT query |
2792 | 2792 | */ |
2793 | - protected function auditSQL(SugarBean $bean, $changes) |
|
2794 | - { |
|
2795 | - global $current_user; |
|
2796 | - $sql = "INSERT INTO ".$bean->get_audit_table_name(); |
|
2797 | - //get field defs for the audit table. |
|
2798 | - require('metadata/audit_templateMetaData.php'); |
|
2799 | - $fieldDefs = $dictionary['audit']['fields']; |
|
2800 | - |
|
2801 | - $values=array(); |
|
2802 | - $values['id'] = $this->massageValue(create_guid(), $fieldDefs['id']); |
|
2803 | - $values['parent_id']= $this->massageValue($bean->id, $fieldDefs['parent_id']); |
|
2804 | - $values['field_name']= $this->massageValue($changes['field_name'], $fieldDefs['field_name']); |
|
2805 | - $values['data_type'] = $this->massageValue($changes['data_type'], $fieldDefs['data_type']); |
|
2806 | - if ($changes['data_type']=='text') { |
|
2807 | - $values['before_value_text'] = $this->massageValue($changes['before'], $fieldDefs['before_value_text']); |
|
2808 | - $values['after_value_text'] = $this->massageValue($changes['after'], $fieldDefs['after_value_text']); |
|
2809 | - } else { |
|
2810 | - $values['before_value_string'] = $this->massageValue($changes['before'], $fieldDefs['before_value_string']); |
|
2811 | - $values['after_value_string'] = $this->massageValue($changes['after'], $fieldDefs['after_value_string']); |
|
2812 | - } |
|
2813 | - $values['date_created'] = $this->massageValue(TimeDate::getInstance()->nowDb(), $fieldDefs['date_created'] ); |
|
2814 | - $values['created_by'] = $this->massageValue($current_user->id, $fieldDefs['created_by']); |
|
2815 | - |
|
2816 | - $sql .= "(".implode(",", array_keys($values)).") "; |
|
2817 | - $sql .= "VALUES(".implode(",", $values).")"; |
|
2818 | - return $sql; |
|
2819 | - } |
|
2793 | + protected function auditSQL(SugarBean $bean, $changes) |
|
2794 | + { |
|
2795 | + global $current_user; |
|
2796 | + $sql = "INSERT INTO ".$bean->get_audit_table_name(); |
|
2797 | + //get field defs for the audit table. |
|
2798 | + require('metadata/audit_templateMetaData.php'); |
|
2799 | + $fieldDefs = $dictionary['audit']['fields']; |
|
2800 | + |
|
2801 | + $values=array(); |
|
2802 | + $values['id'] = $this->massageValue(create_guid(), $fieldDefs['id']); |
|
2803 | + $values['parent_id']= $this->massageValue($bean->id, $fieldDefs['parent_id']); |
|
2804 | + $values['field_name']= $this->massageValue($changes['field_name'], $fieldDefs['field_name']); |
|
2805 | + $values['data_type'] = $this->massageValue($changes['data_type'], $fieldDefs['data_type']); |
|
2806 | + if ($changes['data_type']=='text') { |
|
2807 | + $values['before_value_text'] = $this->massageValue($changes['before'], $fieldDefs['before_value_text']); |
|
2808 | + $values['after_value_text'] = $this->massageValue($changes['after'], $fieldDefs['after_value_text']); |
|
2809 | + } else { |
|
2810 | + $values['before_value_string'] = $this->massageValue($changes['before'], $fieldDefs['before_value_string']); |
|
2811 | + $values['after_value_string'] = $this->massageValue($changes['after'], $fieldDefs['after_value_string']); |
|
2812 | + } |
|
2813 | + $values['date_created'] = $this->massageValue(TimeDate::getInstance()->nowDb(), $fieldDefs['date_created'] ); |
|
2814 | + $values['created_by'] = $this->massageValue($current_user->id, $fieldDefs['created_by']); |
|
2815 | + |
|
2816 | + $sql .= "(".implode(",", array_keys($values)).") "; |
|
2817 | + $sql .= "VALUES(".implode(",", $values).")"; |
|
2818 | + return $sql; |
|
2819 | + } |
|
2820 | 2820 | |
2821 | 2821 | /** |
2822 | 2822 | * Saves changes to module's audit table |
@@ -2826,10 +2826,10 @@ discard block |
||
2826 | 2826 | * @return bool query result |
2827 | 2827 | * |
2828 | 2828 | */ |
2829 | - public function save_audit_records(SugarBean $bean, $changes) |
|
2830 | - { |
|
2831 | - return $this->query($this->auditSQL($bean, $changes)); |
|
2832 | - } |
|
2829 | + public function save_audit_records(SugarBean $bean, $changes) |
|
2830 | + { |
|
2831 | + return $this->query($this->auditSQL($bean, $changes)); |
|
2832 | + } |
|
2833 | 2833 | |
2834 | 2834 | /** |
2835 | 2835 | * Finds fields whose value has changed. |
@@ -2841,7 +2841,7 @@ discard block |
||
2841 | 2841 | * @return array |
2842 | 2842 | */ |
2843 | 2843 | public function getDataChanges(SugarBean &$bean, array $field_filter = null) |
2844 | - { |
|
2844 | + { |
|
2845 | 2845 | $changed_values=array(); |
2846 | 2846 | |
2847 | 2847 | $fetched_row = array(); |
@@ -2918,10 +2918,10 @@ discard block |
||
2918 | 2918 | } |
2919 | 2919 | } |
2920 | 2920 | } |
2921 | - } |
|
2922 | - } |
|
2923 | - return $changed_values; |
|
2924 | - } |
|
2921 | + } |
|
2922 | + } |
|
2923 | + return $changed_values; |
|
2924 | + } |
|
2925 | 2925 | |
2926 | 2926 | /** |
2927 | 2927 | * Uses the audit enabled fields array to find fields whose value has changed. |
@@ -2937,40 +2937,40 @@ discard block |
||
2937 | 2937 | return $this->getDataChanges($bean, array_keys($audit_fields)); |
2938 | 2938 | } |
2939 | 2939 | |
2940 | - /** |
|
2941 | - * Setup FT indexing |
|
2942 | - * @abstract |
|
2943 | - */ |
|
2944 | - public function full_text_indexing_setup() |
|
2945 | - { |
|
2946 | - // Most DBs have nothing to setup, so provide default empty function |
|
2947 | - } |
|
2948 | - |
|
2949 | - /** |
|
2950 | - * Quotes a string for storing in the database |
|
2951 | - * @deprecated |
|
2952 | - * Return value will be not surrounded by quotes |
|
2953 | - * |
|
2954 | - * @param string $string |
|
2955 | - * @return string |
|
2956 | - */ |
|
2957 | - public function escape_quote($string) |
|
2958 | - { |
|
2959 | - return $this->quote($string); |
|
2960 | - } |
|
2961 | - |
|
2962 | - /** |
|
2963 | - * Quotes a string for storing in the database |
|
2964 | - * @deprecated |
|
2965 | - * Return value will be not surrounded by quotes |
|
2966 | - * |
|
2967 | - * @param string $string |
|
2968 | - * @return string |
|
2969 | - */ |
|
2970 | - public function quoteFormEmail($string) |
|
2971 | - { |
|
2972 | - return $this->quote($string); |
|
2973 | - } |
|
2940 | + /** |
|
2941 | + * Setup FT indexing |
|
2942 | + * @abstract |
|
2943 | + */ |
|
2944 | + public function full_text_indexing_setup() |
|
2945 | + { |
|
2946 | + // Most DBs have nothing to setup, so provide default empty function |
|
2947 | + } |
|
2948 | + |
|
2949 | + /** |
|
2950 | + * Quotes a string for storing in the database |
|
2951 | + * @deprecated |
|
2952 | + * Return value will be not surrounded by quotes |
|
2953 | + * |
|
2954 | + * @param string $string |
|
2955 | + * @return string |
|
2956 | + */ |
|
2957 | + public function escape_quote($string) |
|
2958 | + { |
|
2959 | + return $this->quote($string); |
|
2960 | + } |
|
2961 | + |
|
2962 | + /** |
|
2963 | + * Quotes a string for storing in the database |
|
2964 | + * @deprecated |
|
2965 | + * Return value will be not surrounded by quotes |
|
2966 | + * |
|
2967 | + * @param string $string |
|
2968 | + * @return string |
|
2969 | + */ |
|
2970 | + public function quoteFormEmail($string) |
|
2971 | + { |
|
2972 | + return $this->quote($string); |
|
2973 | + } |
|
2974 | 2974 | |
2975 | 2975 | /** |
2976 | 2976 | * Renames an index using fields definition |
@@ -2980,49 +2980,49 @@ discard block |
||
2980 | 2980 | * @param string $table_name |
2981 | 2981 | * @return string SQL statement |
2982 | 2982 | */ |
2983 | - public function renameIndexDefs($old_definition, $new_definition, $table_name) |
|
2984 | - { |
|
2985 | - return array($this->add_drop_constraint($table_name,$old_definition,true), |
|
2986 | - $this->add_drop_constraint($table_name,$new_definition), false); |
|
2987 | - } |
|
2983 | + public function renameIndexDefs($old_definition, $new_definition, $table_name) |
|
2984 | + { |
|
2985 | + return array($this->add_drop_constraint($table_name,$old_definition,true), |
|
2986 | + $this->add_drop_constraint($table_name,$new_definition), false); |
|
2987 | + } |
|
2988 | 2988 | |
2989 | - /** |
|
2990 | - * Check if type is boolean |
|
2991 | - * @param string $type |
|
2989 | + /** |
|
2990 | + * Check if type is boolean |
|
2991 | + * @param string $type |
|
2992 | 2992 | * @return bool |
2993 | 2993 | */ |
2994 | - public function isBooleanType($type) |
|
2995 | - { |
|
2996 | - return 'bool' == $type; |
|
2997 | - } |
|
2994 | + public function isBooleanType($type) |
|
2995 | + { |
|
2996 | + return 'bool' == $type; |
|
2997 | + } |
|
2998 | 2998 | |
2999 | - /** |
|
3000 | - * Get truth value for boolean type |
|
3001 | - * Allows 'off' to mean false, along with all 'empty' values |
|
3002 | - * @param mixed $val |
|
2999 | + /** |
|
3000 | + * Get truth value for boolean type |
|
3001 | + * Allows 'off' to mean false, along with all 'empty' values |
|
3002 | + * @param mixed $val |
|
3003 | 3003 | * @return bool |
3004 | - */ |
|
3005 | - protected function _getBooleanValue($val) |
|
3006 | - { |
|
3007 | - //need to put the === sign here otherwise true == 'non empty string' |
|
3008 | - if (empty($val) or $val==='off') |
|
3009 | - return false; |
|
3010 | - |
|
3011 | - return true; |
|
3012 | - } |
|
3013 | - |
|
3014 | - /** |
|
3015 | - * Check if type is a number |
|
3016 | - * @param string $type |
|
3004 | + */ |
|
3005 | + protected function _getBooleanValue($val) |
|
3006 | + { |
|
3007 | + //need to put the === sign here otherwise true == 'non empty string' |
|
3008 | + if (empty($val) or $val==='off') |
|
3009 | + return false; |
|
3010 | + |
|
3011 | + return true; |
|
3012 | + } |
|
3013 | + |
|
3014 | + /** |
|
3015 | + * Check if type is a number |
|
3016 | + * @param string $type |
|
3017 | 3017 | * @return bool |
3018 | - */ |
|
3019 | - public function isNumericType($type) |
|
3020 | - { |
|
3021 | - if(isset($this->type_class[$type]) && ($this->type_class[$type] == 'int' || $this->type_class[$type] == 'float')) { |
|
3022 | - return true; |
|
3023 | - } |
|
3024 | - return false; |
|
3025 | - } |
|
3018 | + */ |
|
3019 | + public function isNumericType($type) |
|
3020 | + { |
|
3021 | + if(isset($this->type_class[$type]) && ($this->type_class[$type] == 'int' || $this->type_class[$type] == 'float')) { |
|
3022 | + return true; |
|
3023 | + } |
|
3024 | + return false; |
|
3025 | + } |
|
3026 | 3026 | |
3027 | 3027 | /** |
3028 | 3028 | * Check if the value is empty value for this type |
@@ -3030,272 +3030,272 @@ discard block |
||
3030 | 3030 | * @param string $type Type (one of vardef types) |
3031 | 3031 | * @return bool true if the value if empty |
3032 | 3032 | */ |
3033 | - protected function _emptyValue($val, $type) |
|
3034 | - { |
|
3035 | - if (empty($val)) |
|
3036 | - return true; |
|
3037 | - |
|
3038 | - if($this->emptyValue($type) == $val) { |
|
3039 | - return true; |
|
3040 | - } |
|
3041 | - switch ($type) { |
|
3042 | - case 'decimal': |
|
3043 | - case 'decimal2': |
|
3044 | - case 'int': |
|
3045 | - case 'double': |
|
3046 | - case 'float': |
|
3047 | - case 'uint': |
|
3048 | - case 'ulong': |
|
3049 | - case 'long': |
|
3050 | - case 'short': |
|
3051 | - return ($val == 0); |
|
3052 | - case 'date': |
|
3053 | - if ($val == '0000-00-00') |
|
3054 | - return true; |
|
3055 | - if ($val == 'NULL') |
|
3056 | - return true; |
|
3057 | - return false; |
|
3058 | - } |
|
3059 | - |
|
3060 | - return false; |
|
3061 | - } |
|
3062 | - |
|
3063 | - /** |
|
3033 | + protected function _emptyValue($val, $type) |
|
3034 | + { |
|
3035 | + if (empty($val)) |
|
3036 | + return true; |
|
3037 | + |
|
3038 | + if($this->emptyValue($type) == $val) { |
|
3039 | + return true; |
|
3040 | + } |
|
3041 | + switch ($type) { |
|
3042 | + case 'decimal': |
|
3043 | + case 'decimal2': |
|
3044 | + case 'int': |
|
3045 | + case 'double': |
|
3046 | + case 'float': |
|
3047 | + case 'uint': |
|
3048 | + case 'ulong': |
|
3049 | + case 'long': |
|
3050 | + case 'short': |
|
3051 | + return ($val == 0); |
|
3052 | + case 'date': |
|
3053 | + if ($val == '0000-00-00') |
|
3054 | + return true; |
|
3055 | + if ($val == 'NULL') |
|
3056 | + return true; |
|
3057 | + return false; |
|
3058 | + } |
|
3059 | + |
|
3060 | + return false; |
|
3061 | + } |
|
3062 | + |
|
3063 | + /** |
|
3064 | 3064 | * @abstract |
3065 | - * Does this type represent text (i.e., non-varchar) value? |
|
3066 | - * @param string $type |
|
3065 | + * Does this type represent text (i.e., non-varchar) value? |
|
3066 | + * @param string $type |
|
3067 | 3067 | * @return bool |
3068 | - */ |
|
3069 | - public function isTextType($type) |
|
3070 | - { |
|
3071 | - return false; |
|
3072 | - } |
|
3073 | - |
|
3074 | - /** |
|
3075 | - * Check if this DB supports certain capability |
|
3076 | - * See $this->capabilities for the list |
|
3077 | - * @param string $cap |
|
3068 | + */ |
|
3069 | + public function isTextType($type) |
|
3070 | + { |
|
3071 | + return false; |
|
3072 | + } |
|
3073 | + |
|
3074 | + /** |
|
3075 | + * Check if this DB supports certain capability |
|
3076 | + * See $this->capabilities for the list |
|
3077 | + * @param string $cap |
|
3078 | 3078 | * @return bool |
3079 | - */ |
|
3080 | - public function supports($cap) |
|
3081 | - { |
|
3082 | - return !empty($this->capabilities[$cap]); |
|
3083 | - } |
|
3084 | - |
|
3085 | - /** |
|
3086 | - * Create ORDER BY clause for ENUM type field |
|
3087 | - * @param string $order_by Field name |
|
3088 | - * @param array $values Possible enum value |
|
3089 | - * @param string $order_dir Order direction, ASC or DESC |
|
3079 | + */ |
|
3080 | + public function supports($cap) |
|
3081 | + { |
|
3082 | + return !empty($this->capabilities[$cap]); |
|
3083 | + } |
|
3084 | + |
|
3085 | + /** |
|
3086 | + * Create ORDER BY clause for ENUM type field |
|
3087 | + * @param string $order_by Field name |
|
3088 | + * @param array $values Possible enum value |
|
3089 | + * @param string $order_dir Order direction, ASC or DESC |
|
3090 | 3090 | * @return string |
3091 | 3091 | */ |
3092 | - public function orderByEnum($order_by, $values, $order_dir) |
|
3093 | - { |
|
3094 | - $i = 0; |
|
3095 | - $order_by_arr = array(); |
|
3096 | - foreach ($values as $key => $value) { |
|
3097 | - if($key == '') { |
|
3098 | - $order_by_arr[] = "WHEN ($order_by='' OR $order_by IS NULL) THEN $i"; |
|
3099 | - } else { |
|
3100 | - $order_by_arr[] = "WHEN $order_by=".$this->quoted($key)." THEN $i"; |
|
3101 | - } |
|
3102 | - $i++; |
|
3103 | - } |
|
3104 | - return "CASE ".implode("\n", $order_by_arr)." ELSE $i END $order_dir\n"; |
|
3105 | - } |
|
3106 | - |
|
3107 | - /** |
|
3108 | - * Return representation of an empty value depending on type |
|
3109 | - * The value is fully quoted, converted, etc. |
|
3110 | - * @param string $type |
|
3092 | + public function orderByEnum($order_by, $values, $order_dir) |
|
3093 | + { |
|
3094 | + $i = 0; |
|
3095 | + $order_by_arr = array(); |
|
3096 | + foreach ($values as $key => $value) { |
|
3097 | + if($key == '') { |
|
3098 | + $order_by_arr[] = "WHEN ($order_by='' OR $order_by IS NULL) THEN $i"; |
|
3099 | + } else { |
|
3100 | + $order_by_arr[] = "WHEN $order_by=".$this->quoted($key)." THEN $i"; |
|
3101 | + } |
|
3102 | + $i++; |
|
3103 | + } |
|
3104 | + return "CASE ".implode("\n", $order_by_arr)." ELSE $i END $order_dir\n"; |
|
3105 | + } |
|
3106 | + |
|
3107 | + /** |
|
3108 | + * Return representation of an empty value depending on type |
|
3109 | + * The value is fully quoted, converted, etc. |
|
3110 | + * @param string $type |
|
3111 | 3111 | * @return mixed Empty value |
3112 | 3112 | */ |
3113 | - public function emptyValue($type) |
|
3114 | - { |
|
3115 | - if(isset($this->type_class[$type]) && ($this->type_class[$type] == 'bool' || $this->type_class[$type] == 'int' || $this->type_class[$type] == 'float')) { |
|
3116 | - return 0; |
|
3117 | - } |
|
3113 | + public function emptyValue($type) |
|
3114 | + { |
|
3115 | + if(isset($this->type_class[$type]) && ($this->type_class[$type] == 'bool' || $this->type_class[$type] == 'int' || $this->type_class[$type] == 'float')) { |
|
3116 | + return 0; |
|
3117 | + } |
|
3118 | 3118 | |
3119 | - return "''"; |
|
3120 | - } |
|
3119 | + return "''"; |
|
3120 | + } |
|
3121 | 3121 | |
3122 | - /** |
|
3123 | - * List of available collation settings |
|
3122 | + /** |
|
3123 | + * List of available collation settings |
|
3124 | 3124 | * @abstract |
3125 | - * @return string |
|
3126 | - */ |
|
3127 | - public function getDefaultCollation() |
|
3128 | - { |
|
3129 | - return null; |
|
3130 | - } |
|
3131 | - |
|
3132 | - /** |
|
3133 | - * List of available collation settings |
|
3125 | + * @return string |
|
3126 | + */ |
|
3127 | + public function getDefaultCollation() |
|
3128 | + { |
|
3129 | + return null; |
|
3130 | + } |
|
3131 | + |
|
3132 | + /** |
|
3133 | + * List of available collation settings |
|
3134 | 3134 | * @abstract |
3135 | - * @return array |
|
3136 | - */ |
|
3137 | - public function getCollationList() |
|
3138 | - { |
|
3139 | - return null; |
|
3140 | - } |
|
3141 | - |
|
3142 | - /** |
|
3143 | - * Returns the number of columns in a table |
|
3144 | - * |
|
3145 | - * @param string $table_name |
|
3146 | - * @return int |
|
3147 | - */ |
|
3148 | - public function number_of_columns($table_name) |
|
3149 | - { |
|
3150 | - $table = $this->getTableDescription($table_name); |
|
3151 | - return count($table); |
|
3152 | - } |
|
3153 | - |
|
3154 | - /** |
|
3155 | - * Return limit query based on given query |
|
3156 | - * @param string $sql |
|
3157 | - * @param int $start |
|
3158 | - * @param int $count |
|
3159 | - * @param bool $dieOnError |
|
3160 | - * @param string $msg |
|
3135 | + * @return array |
|
3136 | + */ |
|
3137 | + public function getCollationList() |
|
3138 | + { |
|
3139 | + return null; |
|
3140 | + } |
|
3141 | + |
|
3142 | + /** |
|
3143 | + * Returns the number of columns in a table |
|
3144 | + * |
|
3145 | + * @param string $table_name |
|
3146 | + * @return int |
|
3147 | + */ |
|
3148 | + public function number_of_columns($table_name) |
|
3149 | + { |
|
3150 | + $table = $this->getTableDescription($table_name); |
|
3151 | + return count($table); |
|
3152 | + } |
|
3153 | + |
|
3154 | + /** |
|
3155 | + * Return limit query based on given query |
|
3156 | + * @param string $sql |
|
3157 | + * @param int $start |
|
3158 | + * @param int $count |
|
3159 | + * @param bool $dieOnError |
|
3160 | + * @param string $msg |
|
3161 | 3161 | * @return resource|bool query result |
3162 | 3162 | * @see DBManager::limitQuery() |
3163 | - */ |
|
3164 | - public function limitQuerySql($sql, $start, $count, $dieOnError=false, $msg='') |
|
3165 | - { |
|
3166 | - return $this->limitQuery($sql,$start,$count,$dieOnError,$msg,false); |
|
3167 | - } |
|
3168 | - |
|
3169 | - /** |
|
3170 | - * Return current time in format fit for insertion into DB (with quotes) |
|
3171 | - * @return string |
|
3172 | - */ |
|
3173 | - public function now() |
|
3174 | - { |
|
3175 | - return $this->convert($this->quoted(TimeDate::getInstance()->nowDb()), "datetime"); |
|
3176 | - } |
|
3177 | - |
|
3178 | - /** |
|
3179 | - * Check if connecting user has certain privilege |
|
3180 | - * @param string $privilege |
|
3163 | + */ |
|
3164 | + public function limitQuerySql($sql, $start, $count, $dieOnError=false, $msg='') |
|
3165 | + { |
|
3166 | + return $this->limitQuery($sql,$start,$count,$dieOnError,$msg,false); |
|
3167 | + } |
|
3168 | + |
|
3169 | + /** |
|
3170 | + * Return current time in format fit for insertion into DB (with quotes) |
|
3171 | + * @return string |
|
3172 | + */ |
|
3173 | + public function now() |
|
3174 | + { |
|
3175 | + return $this->convert($this->quoted(TimeDate::getInstance()->nowDb()), "datetime"); |
|
3176 | + } |
|
3177 | + |
|
3178 | + /** |
|
3179 | + * Check if connecting user has certain privilege |
|
3180 | + * @param string $privilege |
|
3181 | 3181 | * @return bool Privilege allowed? |
3182 | 3182 | */ |
3183 | - public function checkPrivilege($privilege) |
|
3184 | - { |
|
3185 | - switch($privilege) { |
|
3186 | - case "CREATE TABLE": |
|
3187 | - $this->query("CREATE TABLE temp (id varchar(36))"); |
|
3188 | - break; |
|
3189 | - case "DROP TABLE": |
|
3190 | - $sql = $this->dropTableNameSQL("temp"); |
|
3191 | - $this->query($sql); |
|
3192 | - break; |
|
3193 | - case "INSERT": |
|
3194 | - $this->query("INSERT INTO temp (id) VALUES ('abcdef0123456789abcdef0123456789abcd')"); |
|
3195 | - break; |
|
3196 | - case "UPDATE": |
|
3197 | - $this->query("UPDATE temp SET id = '100000000000000000000000000000000000' WHERE id = 'abcdef0123456789abcdef0123456789abcd'"); |
|
3198 | - break; |
|
3199 | - case 'SELECT': |
|
3200 | - return $this->getOne('SELECT id FROM temp WHERE id=\'100000000000000000000000000000000000\'', false); |
|
3201 | - case 'DELETE': |
|
3202 | - $this->query("DELETE FROM temp WHERE id = '100000000000000000000000000000000000'"); |
|
3203 | - break; |
|
3204 | - case "ADD COLUMN": |
|
3205 | - $test = array("test" => array("name" => "test", "type" => "varchar", "len" => 50)); |
|
3206 | - $sql = $this->changeColumnSQL("temp", $test, "add"); |
|
3207 | - $this->query($sql); |
|
3208 | - break; |
|
3209 | - case "CHANGE COLUMN": |
|
3210 | - $test = array("test" => array("name" => "test", "type" => "varchar", "len" => 100)); |
|
3211 | - $sql = $this->changeColumnSQL("temp", $test, "modify"); |
|
3212 | - $this->query($sql); |
|
3213 | - break; |
|
3214 | - case "DROP COLUMN": |
|
3215 | - $test = array("test" => array("name" => "test", "type" => "varchar", "len" => 100)); |
|
3216 | - $sql = $this->changeColumnSQL("temp", $test, "drop"); |
|
3217 | - $this->query($sql); |
|
3218 | - break; |
|
3219 | - default: |
|
3220 | - return false; |
|
3221 | - } |
|
3222 | - if($this->checkError("Checking privileges")) { |
|
3223 | - return false; |
|
3224 | - } |
|
3225 | - return true; |
|
3226 | - } |
|
3227 | - |
|
3228 | - /** |
|
3229 | - * Check if the query is a select query |
|
3230 | - * @param string $query |
|
3183 | + public function checkPrivilege($privilege) |
|
3184 | + { |
|
3185 | + switch($privilege) { |
|
3186 | + case "CREATE TABLE": |
|
3187 | + $this->query("CREATE TABLE temp (id varchar(36))"); |
|
3188 | + break; |
|
3189 | + case "DROP TABLE": |
|
3190 | + $sql = $this->dropTableNameSQL("temp"); |
|
3191 | + $this->query($sql); |
|
3192 | + break; |
|
3193 | + case "INSERT": |
|
3194 | + $this->query("INSERT INTO temp (id) VALUES ('abcdef0123456789abcdef0123456789abcd')"); |
|
3195 | + break; |
|
3196 | + case "UPDATE": |
|
3197 | + $this->query("UPDATE temp SET id = '100000000000000000000000000000000000' WHERE id = 'abcdef0123456789abcdef0123456789abcd'"); |
|
3198 | + break; |
|
3199 | + case 'SELECT': |
|
3200 | + return $this->getOne('SELECT id FROM temp WHERE id=\'100000000000000000000000000000000000\'', false); |
|
3201 | + case 'DELETE': |
|
3202 | + $this->query("DELETE FROM temp WHERE id = '100000000000000000000000000000000000'"); |
|
3203 | + break; |
|
3204 | + case "ADD COLUMN": |
|
3205 | + $test = array("test" => array("name" => "test", "type" => "varchar", "len" => 50)); |
|
3206 | + $sql = $this->changeColumnSQL("temp", $test, "add"); |
|
3207 | + $this->query($sql); |
|
3208 | + break; |
|
3209 | + case "CHANGE COLUMN": |
|
3210 | + $test = array("test" => array("name" => "test", "type" => "varchar", "len" => 100)); |
|
3211 | + $sql = $this->changeColumnSQL("temp", $test, "modify"); |
|
3212 | + $this->query($sql); |
|
3213 | + break; |
|
3214 | + case "DROP COLUMN": |
|
3215 | + $test = array("test" => array("name" => "test", "type" => "varchar", "len" => 100)); |
|
3216 | + $sql = $this->changeColumnSQL("temp", $test, "drop"); |
|
3217 | + $this->query($sql); |
|
3218 | + break; |
|
3219 | + default: |
|
3220 | + return false; |
|
3221 | + } |
|
3222 | + if($this->checkError("Checking privileges")) { |
|
3223 | + return false; |
|
3224 | + } |
|
3225 | + return true; |
|
3226 | + } |
|
3227 | + |
|
3228 | + /** |
|
3229 | + * Check if the query is a select query |
|
3230 | + * @param string $query |
|
3231 | 3231 | * @return bool Is query SELECT? |
3232 | 3232 | */ |
3233 | - protected function isSelect($query) |
|
3234 | - { |
|
3235 | - $query = trim($query); |
|
3236 | - $select_check = strpos(strtolower($query), strtolower("SELECT")); |
|
3237 | - //Checks to see if there is union select which is valid |
|
3238 | - $select_check2 = strpos(strtolower($query), strtolower("(SELECT")); |
|
3239 | - if($select_check==0 || $select_check2==0){ |
|
3240 | - //Returning false means query is ok! |
|
3241 | - return true; |
|
3242 | - } |
|
3243 | - return false; |
|
3244 | - } |
|
3245 | - |
|
3246 | - /** |
|
3247 | - * Parse fulltext search query with mysql syntax: |
|
3248 | - * terms quoted by "" |
|
3249 | - * + means the term must be included |
|
3250 | - * - means the term must be excluded |
|
3251 | - * * or % at the end means wildcard |
|
3252 | - * @param string $query |
|
3253 | - * @return array of 3 elements - query terms, mandatory terms and excluded terms |
|
3254 | - */ |
|
3255 | - public function parseFulltextQuery($query) |
|
3256 | - { |
|
3257 | - /* split on space or comma, double quotes with \ for escape */ |
|
3258 | - if(strpbrk($query, " ,")) { |
|
3259 | - // ("([^"]*?)"|[^" ,]+)((, )+)? |
|
3260 | - // '/([^" ,]+|".*?[^\\\\]")(,|\s)\s*/' |
|
3261 | - if(!preg_match_all('/("([^"]*?)"|[^"\s,]+)((,\s)+)?/', $query, $m)) { |
|
3262 | - return false; |
|
3263 | - } |
|
3264 | - $qterms = $m[1]; |
|
3265 | - } else { |
|
3266 | - $qterms = array($query); |
|
3267 | - } |
|
3268 | - $terms = $must_terms = $not_terms = array(); |
|
3269 | - foreach($qterms as $item) { |
|
3270 | - if($item[0] == '"') { |
|
3271 | - $item = trim($item, '"'); |
|
3272 | - } |
|
3273 | - if($item[0] == '+') { |
|
3233 | + protected function isSelect($query) |
|
3234 | + { |
|
3235 | + $query = trim($query); |
|
3236 | + $select_check = strpos(strtolower($query), strtolower("SELECT")); |
|
3237 | + //Checks to see if there is union select which is valid |
|
3238 | + $select_check2 = strpos(strtolower($query), strtolower("(SELECT")); |
|
3239 | + if($select_check==0 || $select_check2==0){ |
|
3240 | + //Returning false means query is ok! |
|
3241 | + return true; |
|
3242 | + } |
|
3243 | + return false; |
|
3244 | + } |
|
3245 | + |
|
3246 | + /** |
|
3247 | + * Parse fulltext search query with mysql syntax: |
|
3248 | + * terms quoted by "" |
|
3249 | + * + means the term must be included |
|
3250 | + * - means the term must be excluded |
|
3251 | + * * or % at the end means wildcard |
|
3252 | + * @param string $query |
|
3253 | + * @return array of 3 elements - query terms, mandatory terms and excluded terms |
|
3254 | + */ |
|
3255 | + public function parseFulltextQuery($query) |
|
3256 | + { |
|
3257 | + /* split on space or comma, double quotes with \ for escape */ |
|
3258 | + if(strpbrk($query, " ,")) { |
|
3259 | + // ("([^"]*?)"|[^" ,]+)((, )+)? |
|
3260 | + // '/([^" ,]+|".*?[^\\\\]")(,|\s)\s*/' |
|
3261 | + if(!preg_match_all('/("([^"]*?)"|[^"\s,]+)((,\s)+)?/', $query, $m)) { |
|
3262 | + return false; |
|
3263 | + } |
|
3264 | + $qterms = $m[1]; |
|
3265 | + } else { |
|
3266 | + $qterms = array($query); |
|
3267 | + } |
|
3268 | + $terms = $must_terms = $not_terms = array(); |
|
3269 | + foreach($qterms as $item) { |
|
3270 | + if($item[0] == '"') { |
|
3271 | + $item = trim($item, '"'); |
|
3272 | + } |
|
3273 | + if($item[0] == '+') { |
|
3274 | 3274 | if (strlen($item) > 1) { |
3275 | 3275 | $must_terms[] = substr($item, 1); |
3276 | 3276 | } |
3277 | 3277 | continue; |
3278 | - } |
|
3279 | - if($item[0] == '-') { |
|
3278 | + } |
|
3279 | + if($item[0] == '-') { |
|
3280 | 3280 | if (strlen($item) > 1) { |
3281 | - $not_terms[] = substr($item, 1); |
|
3281 | + $not_terms[] = substr($item, 1); |
|
3282 | 3282 | } |
3283 | 3283 | continue; |
3284 | - } |
|
3285 | - $terms[] = $item; |
|
3286 | - } |
|
3287 | - return array($terms, $must_terms, $not_terms); |
|
3288 | - } |
|
3284 | + } |
|
3285 | + $terms[] = $item; |
|
3286 | + } |
|
3287 | + return array($terms, $must_terms, $not_terms); |
|
3288 | + } |
|
3289 | 3289 | |
3290 | 3290 | // Methods to check respective queries |
3291 | - protected $standardQueries = array( |
|
3292 | - 'ALTER TABLE' => 'verifyAlterTable', |
|
3293 | - 'DROP TABLE' => 'verifyDropTable', |
|
3294 | - 'CREATE TABLE' => 'verifyCreateTable', |
|
3295 | - 'INSERT INTO' => 'verifyInsertInto', |
|
3296 | - 'UPDATE' => 'verifyUpdate', |
|
3297 | - 'DELETE FROM' => 'verifyDeleteFrom', |
|
3298 | - ); |
|
3291 | + protected $standardQueries = array( |
|
3292 | + 'ALTER TABLE' => 'verifyAlterTable', |
|
3293 | + 'DROP TABLE' => 'verifyDropTable', |
|
3294 | + 'CREATE TABLE' => 'verifyCreateTable', |
|
3295 | + 'INSERT INTO' => 'verifyInsertInto', |
|
3296 | + 'UPDATE' => 'verifyUpdate', |
|
3297 | + 'DELETE FROM' => 'verifyDeleteFrom', |
|
3298 | + ); |
|
3299 | 3299 | |
3300 | 3300 | |
3301 | 3301 | /** |
@@ -3303,8 +3303,8 @@ discard block |
||
3303 | 3303 | * @param string $query SQL query |
3304 | 3304 | * @return string |
3305 | 3305 | */ |
3306 | - protected function extractTableName($query) |
|
3307 | - { |
|
3306 | + protected function extractTableName($query) |
|
3307 | + { |
|
3308 | 3308 | $query = preg_replace('/[^A-Za-z0-9_\s]/', "", $query); |
3309 | 3309 | $query = trim(str_replace(array_keys($this->standardQueries), '', $query)); |
3310 | 3310 | |
@@ -3313,7 +3313,7 @@ discard block |
||
3313 | 3313 | $table = substr($query, 0, $end); |
3314 | 3314 | |
3315 | 3315 | return $table; |
3316 | - } |
|
3316 | + } |
|
3317 | 3317 | |
3318 | 3318 | /** |
3319 | 3319 | * Verify SQl statement using per-DB verification function |
@@ -3322,284 +3322,284 @@ discard block |
||
3322 | 3322 | * @param array $skipTables List of blacklisted tables that aren't checked |
3323 | 3323 | * @return string |
3324 | 3324 | */ |
3325 | - public function verifySQLStatement($query, $skipTables) |
|
3326 | - { |
|
3327 | - $query = trim($query); |
|
3328 | - foreach($this->standardQueries as $qstart => $check) { |
|
3329 | - if(strncasecmp($qstart, $query, strlen($qstart)) == 0) { |
|
3330 | - if(is_callable(array($this, $check))) { |
|
3331 | - $table = $this->extractTableName($query); |
|
3332 | - if(!in_array($table, $skipTables)) { |
|
3333 | - return call_user_func(array($this, $check), $table, $query); |
|
3334 | - } else { |
|
3335 | - $this->log->debug("Skipping table $table as blacklisted"); |
|
3336 | - } |
|
3337 | - } else { |
|
3338 | - $this->log->debug("No verification for $qstart on {$this->dbType}"); |
|
3339 | - } |
|
3340 | - break; |
|
3341 | - } |
|
3342 | - } |
|
3343 | - return ""; |
|
3344 | - } |
|
3345 | - |
|
3346 | - /** |
|
3347 | - * Tests an CREATE TABLE query |
|
3348 | - * @param string $table The table name to get DDL |
|
3349 | - * @param string $query The query to test. |
|
3350 | - * @return string Non-empty if error found |
|
3351 | - */ |
|
3352 | - protected function verifyCreateTable($table, $query) |
|
3353 | - { |
|
3354 | - $this->log->debug('verifying CREATE statement...'); |
|
3355 | - |
|
3356 | - // rewrite DDL with _temp name |
|
3357 | - $this->log->debug('testing query: ['.$query.']'); |
|
3358 | - $tempname = $table."__uw_temp"; |
|
3359 | - $tempTableQuery = str_replace("CREATE TABLE {$table}", "CREATE TABLE $tempname", $query); |
|
3360 | - |
|
3361 | - if(strpos($tempTableQuery, '__uw_temp') === false) { |
|
3362 | - return 'Could not use a temp table to test query!'; |
|
3363 | - } |
|
3364 | - |
|
3365 | - $this->query($tempTableQuery, false, "Preflight Failed for: {$query}"); |
|
3366 | - |
|
3367 | - $error = $this->lastError(); // empty on no-errors |
|
3368 | - if(!empty($error)) { |
|
3369 | - return $error; |
|
3370 | - } |
|
3371 | - |
|
3372 | - // check if table exists |
|
3373 | - $this->log->debug('testing for table: '.$table); |
|
3374 | - if(!$this->tableExists($tempname)) { |
|
3375 | - return "Failed to create temp table!"; |
|
3376 | - } |
|
3377 | - |
|
3378 | - $this->dropTableName($tempname); |
|
3379 | - return ''; |
|
3380 | - } |
|
3381 | - |
|
3382 | - /** |
|
3383 | - * Execute multiple queries one after another |
|
3384 | - * @param array $sqls Queries |
|
3385 | - * @param bool $dieOnError Die on error, passed to query() |
|
3386 | - * @param string $msg Error message, passed to query() |
|
3387 | - * @param bool $suppress Supress errors, passed to query() |
|
3388 | - * @return resource|bool result set or success/failure bool |
|
3389 | - */ |
|
3390 | - public function queryArray(array $sqls, $dieOnError = false, $msg = '', $suppress = false) |
|
3391 | - { |
|
3392 | - $last = true; |
|
3393 | - foreach($sqls as $sql) { |
|
3394 | - if(!($last = $this->query($sql, $dieOnError, $msg, $suppress))) { |
|
3395 | - break; |
|
3396 | - } |
|
3397 | - } |
|
3398 | - return $last; |
|
3399 | - } |
|
3400 | - |
|
3401 | - /** |
|
3402 | - * Fetches the next row in the query result into an associative array |
|
3403 | - * |
|
3404 | - * @param resource $result |
|
3405 | - * @param bool $encode Need to HTML-encode the result? |
|
3406 | - * @return array returns false if there are no more rows available to fetch |
|
3407 | - */ |
|
3408 | - public function fetchByAssoc($result, $encode = true) |
|
3409 | - { |
|
3410 | - if (empty($result)) return false; |
|
3411 | - |
|
3412 | - if(is_int($encode) && func_num_args() == 3) { |
|
3413 | - // old API: $result, $rowNum, $encode |
|
3414 | - $GLOBALS['log']->deprecated("Using row number in fetchByAssoc is not portable and no longer supported. Please fix your code."); |
|
3415 | - $encode = func_get_arg(2); |
|
3416 | - } |
|
3417 | - $row = $this->fetchRow($result); |
|
3418 | - if (!empty($row) && $encode && $this->encode) { |
|
3419 | - return array_map('to_html', $row); |
|
3420 | - } else { |
|
3421 | - return $row; |
|
3422 | - } |
|
3423 | - } |
|
3424 | - |
|
3425 | - /** |
|
3426 | - * Get DB driver name used for install/upgrade scripts |
|
3427 | - * @return string |
|
3428 | - */ |
|
3429 | - public function getScriptName() |
|
3430 | - { |
|
3431 | - // Usually the same name as dbType |
|
3432 | - return $this->dbType; |
|
3433 | - } |
|
3434 | - |
|
3435 | - /** |
|
3436 | - * Set database options |
|
3437 | - * Options are usually db-dependant and derive from $config['dbconfigoption'] |
|
3438 | - * @param array $options |
|
3439 | - * @return DBManager |
|
3440 | - */ |
|
3441 | - public function setOptions($options) |
|
3442 | - { |
|
3443 | - $this->options = $options; |
|
3444 | - return $this; |
|
3445 | - } |
|
3446 | - |
|
3447 | - /** |
|
3448 | - * Get DB options |
|
3449 | - * @return array |
|
3450 | - */ |
|
3451 | - public function getOptions() |
|
3452 | - { |
|
3453 | - return $this->options; |
|
3454 | - } |
|
3455 | - |
|
3456 | - /** |
|
3457 | - * Get DB option by name |
|
3458 | - * @param string $option Option name |
|
3459 | - * @return mixed Option value or null if doesn't exist |
|
3460 | - */ |
|
3461 | - public function getOption($option) |
|
3462 | - { |
|
3463 | - if(isset($this->options[$option])) { |
|
3464 | - return $this->options[$option]; |
|
3465 | - } |
|
3466 | - return null; |
|
3467 | - } |
|
3468 | - |
|
3469 | - /** |
|
3470 | - * Commits pending changes to the database when the driver is setup to support transactions. |
|
3471 | - * Note that the default implementation is applicable for transaction-less or auto commit scenarios. |
|
3472 | - * @abstract |
|
3473 | - * @return bool true if commit succeeded, false if it failed |
|
3474 | - */ |
|
3475 | - public function commit() |
|
3476 | - { |
|
3477 | - $this->log->info("DBManager.commit() stub"); |
|
3478 | - return true; |
|
3479 | - } |
|
3480 | - |
|
3481 | - /** |
|
3482 | - * Rollsback pending changes to the database when the driver is setup to support transactions. |
|
3483 | - * Note that the default implementation is applicable for transaction-less or auto commit scenarios. |
|
3484 | - * Since rollbacks cannot be done, this implementation always returns false. |
|
3485 | - * @abstract |
|
3486 | - * @return bool true if rollback succeeded, false if it failed |
|
3487 | - */ |
|
3488 | - public function rollback() |
|
3489 | - { |
|
3490 | - $this->log->info("DBManager.rollback() stub"); |
|
3491 | - return false; |
|
3492 | - } |
|
3493 | - |
|
3494 | - /** |
|
3495 | - * Check if this DB name is valid |
|
3496 | - * |
|
3497 | - * @param string $name |
|
3498 | - * @return bool |
|
3499 | - */ |
|
3500 | - public function isDatabaseNameValid($name) |
|
3501 | - { |
|
3502 | - // Generic case - no slashes, no dots |
|
3503 | - return preg_match('#[/.\\\\]#', $name)==0; |
|
3504 | - } |
|
3505 | - |
|
3506 | - /** |
|
3507 | - * Check special requirements for DB installation. |
|
3508 | - * @abstract |
|
3509 | - * If everything is OK, return true. |
|
3510 | - * If something's wrong, return array of error code and parameters |
|
3511 | - * @return mixed |
|
3512 | - */ |
|
3513 | - public function canInstall() |
|
3514 | - { |
|
3515 | - return true; |
|
3516 | - } |
|
3517 | - |
|
3518 | - /** |
|
3519 | - * @abstract |
|
3325 | + public function verifySQLStatement($query, $skipTables) |
|
3326 | + { |
|
3327 | + $query = trim($query); |
|
3328 | + foreach($this->standardQueries as $qstart => $check) { |
|
3329 | + if(strncasecmp($qstart, $query, strlen($qstart)) == 0) { |
|
3330 | + if(is_callable(array($this, $check))) { |
|
3331 | + $table = $this->extractTableName($query); |
|
3332 | + if(!in_array($table, $skipTables)) { |
|
3333 | + return call_user_func(array($this, $check), $table, $query); |
|
3334 | + } else { |
|
3335 | + $this->log->debug("Skipping table $table as blacklisted"); |
|
3336 | + } |
|
3337 | + } else { |
|
3338 | + $this->log->debug("No verification for $qstart on {$this->dbType}"); |
|
3339 | + } |
|
3340 | + break; |
|
3341 | + } |
|
3342 | + } |
|
3343 | + return ""; |
|
3344 | + } |
|
3345 | + |
|
3346 | + /** |
|
3347 | + * Tests an CREATE TABLE query |
|
3348 | + * @param string $table The table name to get DDL |
|
3349 | + * @param string $query The query to test. |
|
3350 | + * @return string Non-empty if error found |
|
3351 | + */ |
|
3352 | + protected function verifyCreateTable($table, $query) |
|
3353 | + { |
|
3354 | + $this->log->debug('verifying CREATE statement...'); |
|
3355 | + |
|
3356 | + // rewrite DDL with _temp name |
|
3357 | + $this->log->debug('testing query: ['.$query.']'); |
|
3358 | + $tempname = $table."__uw_temp"; |
|
3359 | + $tempTableQuery = str_replace("CREATE TABLE {$table}", "CREATE TABLE $tempname", $query); |
|
3360 | + |
|
3361 | + if(strpos($tempTableQuery, '__uw_temp') === false) { |
|
3362 | + return 'Could not use a temp table to test query!'; |
|
3363 | + } |
|
3364 | + |
|
3365 | + $this->query($tempTableQuery, false, "Preflight Failed for: {$query}"); |
|
3366 | + |
|
3367 | + $error = $this->lastError(); // empty on no-errors |
|
3368 | + if(!empty($error)) { |
|
3369 | + return $error; |
|
3370 | + } |
|
3371 | + |
|
3372 | + // check if table exists |
|
3373 | + $this->log->debug('testing for table: '.$table); |
|
3374 | + if(!$this->tableExists($tempname)) { |
|
3375 | + return "Failed to create temp table!"; |
|
3376 | + } |
|
3377 | + |
|
3378 | + $this->dropTableName($tempname); |
|
3379 | + return ''; |
|
3380 | + } |
|
3381 | + |
|
3382 | + /** |
|
3383 | + * Execute multiple queries one after another |
|
3384 | + * @param array $sqls Queries |
|
3385 | + * @param bool $dieOnError Die on error, passed to query() |
|
3386 | + * @param string $msg Error message, passed to query() |
|
3387 | + * @param bool $suppress Supress errors, passed to query() |
|
3388 | + * @return resource|bool result set or success/failure bool |
|
3389 | + */ |
|
3390 | + public function queryArray(array $sqls, $dieOnError = false, $msg = '', $suppress = false) |
|
3391 | + { |
|
3392 | + $last = true; |
|
3393 | + foreach($sqls as $sql) { |
|
3394 | + if(!($last = $this->query($sql, $dieOnError, $msg, $suppress))) { |
|
3395 | + break; |
|
3396 | + } |
|
3397 | + } |
|
3398 | + return $last; |
|
3399 | + } |
|
3400 | + |
|
3401 | + /** |
|
3402 | + * Fetches the next row in the query result into an associative array |
|
3403 | + * |
|
3404 | + * @param resource $result |
|
3405 | + * @param bool $encode Need to HTML-encode the result? |
|
3406 | + * @return array returns false if there are no more rows available to fetch |
|
3407 | + */ |
|
3408 | + public function fetchByAssoc($result, $encode = true) |
|
3409 | + { |
|
3410 | + if (empty($result)) return false; |
|
3411 | + |
|
3412 | + if(is_int($encode) && func_num_args() == 3) { |
|
3413 | + // old API: $result, $rowNum, $encode |
|
3414 | + $GLOBALS['log']->deprecated("Using row number in fetchByAssoc is not portable and no longer supported. Please fix your code."); |
|
3415 | + $encode = func_get_arg(2); |
|
3416 | + } |
|
3417 | + $row = $this->fetchRow($result); |
|
3418 | + if (!empty($row) && $encode && $this->encode) { |
|
3419 | + return array_map('to_html', $row); |
|
3420 | + } else { |
|
3421 | + return $row; |
|
3422 | + } |
|
3423 | + } |
|
3424 | + |
|
3425 | + /** |
|
3426 | + * Get DB driver name used for install/upgrade scripts |
|
3427 | + * @return string |
|
3428 | + */ |
|
3429 | + public function getScriptName() |
|
3430 | + { |
|
3431 | + // Usually the same name as dbType |
|
3432 | + return $this->dbType; |
|
3433 | + } |
|
3434 | + |
|
3435 | + /** |
|
3436 | + * Set database options |
|
3437 | + * Options are usually db-dependant and derive from $config['dbconfigoption'] |
|
3438 | + * @param array $options |
|
3439 | + * @return DBManager |
|
3440 | + */ |
|
3441 | + public function setOptions($options) |
|
3442 | + { |
|
3443 | + $this->options = $options; |
|
3444 | + return $this; |
|
3445 | + } |
|
3446 | + |
|
3447 | + /** |
|
3448 | + * Get DB options |
|
3449 | + * @return array |
|
3450 | + */ |
|
3451 | + public function getOptions() |
|
3452 | + { |
|
3453 | + return $this->options; |
|
3454 | + } |
|
3455 | + |
|
3456 | + /** |
|
3457 | + * Get DB option by name |
|
3458 | + * @param string $option Option name |
|
3459 | + * @return mixed Option value or null if doesn't exist |
|
3460 | + */ |
|
3461 | + public function getOption($option) |
|
3462 | + { |
|
3463 | + if(isset($this->options[$option])) { |
|
3464 | + return $this->options[$option]; |
|
3465 | + } |
|
3466 | + return null; |
|
3467 | + } |
|
3468 | + |
|
3469 | + /** |
|
3470 | + * Commits pending changes to the database when the driver is setup to support transactions. |
|
3471 | + * Note that the default implementation is applicable for transaction-less or auto commit scenarios. |
|
3472 | + * @abstract |
|
3473 | + * @return bool true if commit succeeded, false if it failed |
|
3474 | + */ |
|
3475 | + public function commit() |
|
3476 | + { |
|
3477 | + $this->log->info("DBManager.commit() stub"); |
|
3478 | + return true; |
|
3479 | + } |
|
3480 | + |
|
3481 | + /** |
|
3482 | + * Rollsback pending changes to the database when the driver is setup to support transactions. |
|
3483 | + * Note that the default implementation is applicable for transaction-less or auto commit scenarios. |
|
3484 | + * Since rollbacks cannot be done, this implementation always returns false. |
|
3485 | + * @abstract |
|
3486 | + * @return bool true if rollback succeeded, false if it failed |
|
3487 | + */ |
|
3488 | + public function rollback() |
|
3489 | + { |
|
3490 | + $this->log->info("DBManager.rollback() stub"); |
|
3491 | + return false; |
|
3492 | + } |
|
3493 | + |
|
3494 | + /** |
|
3495 | + * Check if this DB name is valid |
|
3496 | + * |
|
3497 | + * @param string $name |
|
3498 | + * @return bool |
|
3499 | + */ |
|
3500 | + public function isDatabaseNameValid($name) |
|
3501 | + { |
|
3502 | + // Generic case - no slashes, no dots |
|
3503 | + return preg_match('#[/.\\\\]#', $name)==0; |
|
3504 | + } |
|
3505 | + |
|
3506 | + /** |
|
3507 | + * Check special requirements for DB installation. |
|
3508 | + * @abstract |
|
3509 | + * If everything is OK, return true. |
|
3510 | + * If something's wrong, return array of error code and parameters |
|
3511 | + * @return mixed |
|
3512 | + */ |
|
3513 | + public function canInstall() |
|
3514 | + { |
|
3515 | + return true; |
|
3516 | + } |
|
3517 | + |
|
3518 | + /** |
|
3519 | + * @abstract |
|
3520 | 3520 | * Code run on new database before installing |
3521 | - */ |
|
3522 | - public function preInstall() |
|
3523 | - { |
|
3524 | - } |
|
3521 | + */ |
|
3522 | + public function preInstall() |
|
3523 | + { |
|
3524 | + } |
|
3525 | + |
|
3526 | + /** |
|
3527 | + * @abstract |
|
3528 | + * Code run on new database after installing |
|
3529 | + */ |
|
3530 | + public function postInstall() |
|
3531 | + { |
|
3532 | + } |
|
3533 | + |
|
3534 | + /** |
|
3535 | + * Disable keys on the table |
|
3536 | + * @abstract |
|
3537 | + * @param string $tableName |
|
3538 | + */ |
|
3539 | + public function disableKeys($tableName) |
|
3540 | + { |
|
3541 | + } |
|
3525 | 3542 | |
3526 | - /** |
|
3543 | + /** |
|
3544 | + * Re-enable keys on the table |
|
3527 | 3545 | * @abstract |
3528 | - * Code run on new database after installing |
|
3529 | - */ |
|
3530 | - public function postInstall() |
|
3531 | - { |
|
3532 | - } |
|
3533 | - |
|
3534 | - /** |
|
3535 | - * Disable keys on the table |
|
3536 | - * @abstract |
|
3537 | - * @param string $tableName |
|
3538 | - */ |
|
3539 | - public function disableKeys($tableName) |
|
3540 | - { |
|
3541 | - } |
|
3542 | - |
|
3543 | - /** |
|
3544 | - * Re-enable keys on the table |
|
3545 | - * @abstract |
|
3546 | - * @param string $tableName |
|
3547 | - */ |
|
3548 | - public function enableKeys($tableName) |
|
3549 | - { |
|
3550 | - } |
|
3551 | - |
|
3552 | - /** |
|
3553 | - * Quote string in DB-specific manner |
|
3554 | - * @param string $string |
|
3555 | - * @return string |
|
3556 | - */ |
|
3557 | - abstract public function quote($string); |
|
3546 | + * @param string $tableName |
|
3547 | + */ |
|
3548 | + public function enableKeys($tableName) |
|
3549 | + { |
|
3550 | + } |
|
3551 | + |
|
3552 | + /** |
|
3553 | + * Quote string in DB-specific manner |
|
3554 | + * @param string $string |
|
3555 | + * @return string |
|
3556 | + */ |
|
3557 | + abstract public function quote($string); |
|
3558 | 3558 | |
3559 | 3559 | abstract public function quoteIdentifier($string); |
3560 | 3560 | |
3561 | - /** |
|
3562 | - * Use when you need to convert a database string to a different value; this function does it in a |
|
3563 | - * database-backend aware way |
|
3564 | - * Supported conversions: |
|
3565 | - * today return current date |
|
3566 | - * left Take substring from the left |
|
3567 | - * date_format Format date as string, supports %Y-%m-%d, %Y-%m, %Y |
|
3561 | + /** |
|
3562 | + * Use when you need to convert a database string to a different value; this function does it in a |
|
3563 | + * database-backend aware way |
|
3564 | + * Supported conversions: |
|
3565 | + * today return current date |
|
3566 | + * left Take substring from the left |
|
3567 | + * date_format Format date as string, supports %Y-%m-%d, %Y-%m, %Y |
|
3568 | 3568 | * time_format Format time as string |
3569 | 3569 | * date Convert date string to datetime value |
3570 | 3570 | * time Convert time string to datetime value |
3571 | - * datetime Convert datetime string to datetime value |
|
3572 | - * ifnull If var is null, use default value |
|
3573 | - * concat Concatenate strings |
|
3574 | - * quarter Quarter number of the date |
|
3575 | - * length Length of string |
|
3576 | - * month Month number of the date |
|
3577 | - * add_date Add specified interval to a date |
|
3571 | + * datetime Convert datetime string to datetime value |
|
3572 | + * ifnull If var is null, use default value |
|
3573 | + * concat Concatenate strings |
|
3574 | + * quarter Quarter number of the date |
|
3575 | + * length Length of string |
|
3576 | + * month Month number of the date |
|
3577 | + * add_date Add specified interval to a date |
|
3578 | 3578 | * add_time Add time interval to a date |
3579 | 3579 | * text2char Convert text field to varchar |
3580 | - * |
|
3581 | - * @param string $string database string to convert |
|
3582 | - * @param string $type type of conversion to do |
|
3583 | - * @param array $additional_parameters optional, additional parameters to pass to the db function |
|
3584 | - * @return string |
|
3585 | - */ |
|
3586 | - abstract public function convert($string, $type, array $additional_parameters = array()); |
|
3587 | - |
|
3588 | - /** |
|
3589 | - * Converts from Database data to app data |
|
3590 | - * |
|
3591 | - * Supported types |
|
3592 | - * - date |
|
3593 | - * - time |
|
3594 | - * - datetime |
|
3580 | + * |
|
3581 | + * @param string $string database string to convert |
|
3582 | + * @param string $type type of conversion to do |
|
3583 | + * @param array $additional_parameters optional, additional parameters to pass to the db function |
|
3584 | + * @return string |
|
3585 | + */ |
|
3586 | + abstract public function convert($string, $type, array $additional_parameters = array()); |
|
3587 | + |
|
3588 | + /** |
|
3589 | + * Converts from Database data to app data |
|
3590 | + * |
|
3591 | + * Supported types |
|
3592 | + * - date |
|
3593 | + * - time |
|
3594 | + * - datetime |
|
3595 | 3595 | * - datetimecombo |
3596 | 3596 | * - timestamp |
3597 | - * |
|
3598 | - * @param string $string database string to convert |
|
3599 | - * @param string $type type of conversion to do |
|
3600 | - * @return string |
|
3601 | - */ |
|
3602 | - abstract public function fromConvert($string, $type); |
|
3597 | + * |
|
3598 | + * @param string $string database string to convert |
|
3599 | + * @param string $type type of conversion to do |
|
3600 | + * @return string |
|
3601 | + */ |
|
3602 | + abstract public function fromConvert($string, $type); |
|
3603 | 3603 | |
3604 | 3604 | /** |
3605 | 3605 | * Parses and runs queries |
@@ -3611,7 +3611,7 @@ discard block |
||
3611 | 3611 | * @param bool $keepResult Keep query result in the object? |
3612 | 3612 | * @return resource|bool result set or success/failure bool |
3613 | 3613 | */ |
3614 | - abstract public function query($sql, $dieOnError = false, $msg = '', $suppress = false, $keepResult = false); |
|
3614 | + abstract public function query($sql, $dieOnError = false, $msg = '', $suppress = false, $keepResult = false); |
|
3615 | 3615 | |
3616 | 3616 | /** |
3617 | 3617 | * Runs a limit query: one where we specify where to start getting records and how many to get |
@@ -3624,173 +3624,173 @@ discard block |
||
3624 | 3624 | * @param bool $execute Execute or return SQL? |
3625 | 3625 | * @return resource query result |
3626 | 3626 | */ |
3627 | - abstract function limitQuery($sql, $start, $count, $dieOnError = false, $msg = '', $execute = true); |
|
3628 | - |
|
3629 | - |
|
3630 | - /** |
|
3631 | - * Free Database result |
|
3632 | - * @param resource $dbResult |
|
3633 | - */ |
|
3634 | - abstract protected function freeDbResult($dbResult); |
|
3635 | - |
|
3636 | - /** |
|
3637 | - * Rename column in the DB |
|
3638 | - * @param string $tablename |
|
3639 | - * @param string $column |
|
3640 | - * @param string $newname |
|
3641 | - */ |
|
3642 | - abstract function renameColumnSQL($tablename, $column, $newname); |
|
3643 | - |
|
3644 | - /** |
|
3645 | - * Returns definitions of all indies for passed table. |
|
3646 | - * |
|
3647 | - * return will is a multi-dimensional array that |
|
3648 | - * categorizes the index definition by types, unique, primary and index. |
|
3649 | - * <code> |
|
3650 | - * <?php |
|
3651 | - * array( O |
|
3652 | - * 'index1'=> array ( |
|
3653 | - * 'name' => 'index1', |
|
3654 | - * 'type' => 'primary', |
|
3655 | - * 'fields' => array('field1','field2') |
|
3656 | - * ) |
|
3657 | - * ) |
|
3658 | - * ?> |
|
3659 | - * </code> |
|
3660 | - * This format is similar to how indicies are defined in vardef file. |
|
3661 | - * |
|
3662 | - * @param string $tablename |
|
3663 | - * @return array |
|
3664 | - */ |
|
3665 | - abstract public function get_indices($tablename); |
|
3666 | - |
|
3667 | - /** |
|
3668 | - * Returns definitions of all indies for passed table. |
|
3669 | - * |
|
3670 | - * return will is a multi-dimensional array that |
|
3671 | - * categorizes the index definition by types, unique, primary and index. |
|
3672 | - * <code> |
|
3673 | - * <?php |
|
3674 | - * array( |
|
3675 | - * 'field1'=> array ( |
|
3676 | - * 'name' => 'field1', |
|
3677 | - * 'type' => 'varchar', |
|
3678 | - * 'len' => '200' |
|
3679 | - * ) |
|
3680 | - * ) |
|
3681 | - * ?> |
|
3682 | - * </code> |
|
3683 | - * This format is similar to how indicies are defined in vardef file. |
|
3684 | - * |
|
3685 | - * @param string $tablename |
|
3686 | - * @return array |
|
3687 | - */ |
|
3688 | - abstract public function get_columns($tablename); |
|
3689 | - |
|
3690 | - /** |
|
3691 | - * Generates alter constraint statement given a table name and vardef definition. |
|
3692 | - * |
|
3693 | - * Supports both adding and droping a constraint. |
|
3694 | - * |
|
3695 | - * @param string $table tablename |
|
3696 | - * @param array $definition field definition |
|
3697 | - * @param bool $drop true if we are dropping the constraint, false if we are adding it |
|
3698 | - * @return string SQL statement |
|
3699 | - */ |
|
3700 | - abstract public function add_drop_constraint($table, $definition, $drop = false); |
|
3701 | - |
|
3702 | - /** |
|
3703 | - * Returns the description of fields based on the result |
|
3704 | - * |
|
3705 | - * @param resource $result |
|
3706 | - * @param boolean $make_lower_case |
|
3707 | - * @return array field array |
|
3708 | - */ |
|
3709 | - abstract public function getFieldsArray($result, $make_lower_case = false); |
|
3710 | - |
|
3711 | - /** |
|
3712 | - * Returns an array of tables for this database |
|
3713 | - * |
|
3714 | - * @return array|false an array of with table names, false if no tables found |
|
3715 | - */ |
|
3716 | - abstract public function getTablesArray(); |
|
3717 | - |
|
3718 | - /** |
|
3719 | - * Return's the version of the database |
|
3720 | - * |
|
3721 | - * @return string |
|
3722 | - */ |
|
3723 | - abstract public function version(); |
|
3724 | - |
|
3725 | - /** |
|
3726 | - * Checks if a table with the name $tableName exists |
|
3727 | - * and returns true if it does or false otherwise |
|
3728 | - * |
|
3729 | - * @param string $tableName |
|
3730 | - * @return bool |
|
3731 | - */ |
|
3732 | - abstract public function tableExists($tableName); |
|
3733 | - |
|
3734 | - /** |
|
3735 | - * Fetches the next row in the query result into an associative array |
|
3736 | - * |
|
3737 | - * @param resource $result |
|
3738 | - * @return array returns false if there are no more rows available to fetch |
|
3739 | - */ |
|
3740 | - abstract public function fetchRow($result); |
|
3741 | - |
|
3742 | - /** |
|
3743 | - * Connects to the database backend |
|
3744 | - * |
|
3745 | - * Takes in the database settings and opens a database connection based on those |
|
3746 | - * will open either a persistent or non-persistent connection. |
|
3747 | - * If a persistent connection is desired but not available it will defualt to non-persistent |
|
3748 | - * |
|
3749 | - * configOptions must include |
|
3750 | - * db_host_name - server ip |
|
3751 | - * db_user_name - database user name |
|
3752 | - * db_password - database password |
|
3753 | - * |
|
3754 | - * @param array $configOptions |
|
3755 | - * @param boolean $dieOnError |
|
3756 | - */ |
|
3757 | - abstract public function connect(array $configOptions = null, $dieOnError = false); |
|
3758 | - |
|
3759 | - /** |
|
3760 | - * Generates sql for create table statement for a bean. |
|
3761 | - * |
|
3762 | - * @param string $tablename |
|
3763 | - * @param array $fieldDefs |
|
3764 | - * @param array $indices |
|
3765 | - * @return string SQL Create Table statement |
|
3766 | - */ |
|
3767 | - abstract public function createTableSQLParams($tablename, $fieldDefs, $indices); |
|
3768 | - |
|
3769 | - /** |
|
3770 | - * Generates the SQL for changing columns |
|
3771 | - * |
|
3772 | - * @param string $tablename |
|
3773 | - * @param array $fieldDefs |
|
3774 | - * @param string $action |
|
3775 | - * @param bool $ignoreRequired Optional, true if we should ignor this being a required field |
|
3776 | - * @return string|array |
|
3777 | - */ |
|
3778 | - abstract protected function changeColumnSQL($tablename, $fieldDefs, $action, $ignoreRequired = false); |
|
3779 | - |
|
3780 | - /** |
|
3781 | - * Disconnects from the database |
|
3782 | - * |
|
3783 | - * Also handles any cleanup needed |
|
3784 | - */ |
|
3785 | - abstract public function disconnect(); |
|
3786 | - |
|
3787 | - /** |
|
3788 | - * Get last database error |
|
3789 | - * This function should return last error as reported by DB driver |
|
3790 | - * and should return false if no error condition happened |
|
3791 | - * @return string|false Error message or false if no error happened |
|
3792 | - */ |
|
3793 | - abstract public function lastDbError(); |
|
3627 | + abstract function limitQuery($sql, $start, $count, $dieOnError = false, $msg = '', $execute = true); |
|
3628 | + |
|
3629 | + |
|
3630 | + /** |
|
3631 | + * Free Database result |
|
3632 | + * @param resource $dbResult |
|
3633 | + */ |
|
3634 | + abstract protected function freeDbResult($dbResult); |
|
3635 | + |
|
3636 | + /** |
|
3637 | + * Rename column in the DB |
|
3638 | + * @param string $tablename |
|
3639 | + * @param string $column |
|
3640 | + * @param string $newname |
|
3641 | + */ |
|
3642 | + abstract function renameColumnSQL($tablename, $column, $newname); |
|
3643 | + |
|
3644 | + /** |
|
3645 | + * Returns definitions of all indies for passed table. |
|
3646 | + * |
|
3647 | + * return will is a multi-dimensional array that |
|
3648 | + * categorizes the index definition by types, unique, primary and index. |
|
3649 | + * <code> |
|
3650 | + * <?php |
|
3651 | + * array( O |
|
3652 | + * 'index1'=> array ( |
|
3653 | + * 'name' => 'index1', |
|
3654 | + * 'type' => 'primary', |
|
3655 | + * 'fields' => array('field1','field2') |
|
3656 | + * ) |
|
3657 | + * ) |
|
3658 | + * ?> |
|
3659 | + * </code> |
|
3660 | + * This format is similar to how indicies are defined in vardef file. |
|
3661 | + * |
|
3662 | + * @param string $tablename |
|
3663 | + * @return array |
|
3664 | + */ |
|
3665 | + abstract public function get_indices($tablename); |
|
3666 | + |
|
3667 | + /** |
|
3668 | + * Returns definitions of all indies for passed table. |
|
3669 | + * |
|
3670 | + * return will is a multi-dimensional array that |
|
3671 | + * categorizes the index definition by types, unique, primary and index. |
|
3672 | + * <code> |
|
3673 | + * <?php |
|
3674 | + * array( |
|
3675 | + * 'field1'=> array ( |
|
3676 | + * 'name' => 'field1', |
|
3677 | + * 'type' => 'varchar', |
|
3678 | + * 'len' => '200' |
|
3679 | + * ) |
|
3680 | + * ) |
|
3681 | + * ?> |
|
3682 | + * </code> |
|
3683 | + * This format is similar to how indicies are defined in vardef file. |
|
3684 | + * |
|
3685 | + * @param string $tablename |
|
3686 | + * @return array |
|
3687 | + */ |
|
3688 | + abstract public function get_columns($tablename); |
|
3689 | + |
|
3690 | + /** |
|
3691 | + * Generates alter constraint statement given a table name and vardef definition. |
|
3692 | + * |
|
3693 | + * Supports both adding and droping a constraint. |
|
3694 | + * |
|
3695 | + * @param string $table tablename |
|
3696 | + * @param array $definition field definition |
|
3697 | + * @param bool $drop true if we are dropping the constraint, false if we are adding it |
|
3698 | + * @return string SQL statement |
|
3699 | + */ |
|
3700 | + abstract public function add_drop_constraint($table, $definition, $drop = false); |
|
3701 | + |
|
3702 | + /** |
|
3703 | + * Returns the description of fields based on the result |
|
3704 | + * |
|
3705 | + * @param resource $result |
|
3706 | + * @param boolean $make_lower_case |
|
3707 | + * @return array field array |
|
3708 | + */ |
|
3709 | + abstract public function getFieldsArray($result, $make_lower_case = false); |
|
3710 | + |
|
3711 | + /** |
|
3712 | + * Returns an array of tables for this database |
|
3713 | + * |
|
3714 | + * @return array|false an array of with table names, false if no tables found |
|
3715 | + */ |
|
3716 | + abstract public function getTablesArray(); |
|
3717 | + |
|
3718 | + /** |
|
3719 | + * Return's the version of the database |
|
3720 | + * |
|
3721 | + * @return string |
|
3722 | + */ |
|
3723 | + abstract public function version(); |
|
3724 | + |
|
3725 | + /** |
|
3726 | + * Checks if a table with the name $tableName exists |
|
3727 | + * and returns true if it does or false otherwise |
|
3728 | + * |
|
3729 | + * @param string $tableName |
|
3730 | + * @return bool |
|
3731 | + */ |
|
3732 | + abstract public function tableExists($tableName); |
|
3733 | + |
|
3734 | + /** |
|
3735 | + * Fetches the next row in the query result into an associative array |
|
3736 | + * |
|
3737 | + * @param resource $result |
|
3738 | + * @return array returns false if there are no more rows available to fetch |
|
3739 | + */ |
|
3740 | + abstract public function fetchRow($result); |
|
3741 | + |
|
3742 | + /** |
|
3743 | + * Connects to the database backend |
|
3744 | + * |
|
3745 | + * Takes in the database settings and opens a database connection based on those |
|
3746 | + * will open either a persistent or non-persistent connection. |
|
3747 | + * If a persistent connection is desired but not available it will defualt to non-persistent |
|
3748 | + * |
|
3749 | + * configOptions must include |
|
3750 | + * db_host_name - server ip |
|
3751 | + * db_user_name - database user name |
|
3752 | + * db_password - database password |
|
3753 | + * |
|
3754 | + * @param array $configOptions |
|
3755 | + * @param boolean $dieOnError |
|
3756 | + */ |
|
3757 | + abstract public function connect(array $configOptions = null, $dieOnError = false); |
|
3758 | + |
|
3759 | + /** |
|
3760 | + * Generates sql for create table statement for a bean. |
|
3761 | + * |
|
3762 | + * @param string $tablename |
|
3763 | + * @param array $fieldDefs |
|
3764 | + * @param array $indices |
|
3765 | + * @return string SQL Create Table statement |
|
3766 | + */ |
|
3767 | + abstract public function createTableSQLParams($tablename, $fieldDefs, $indices); |
|
3768 | + |
|
3769 | + /** |
|
3770 | + * Generates the SQL for changing columns |
|
3771 | + * |
|
3772 | + * @param string $tablename |
|
3773 | + * @param array $fieldDefs |
|
3774 | + * @param string $action |
|
3775 | + * @param bool $ignoreRequired Optional, true if we should ignor this being a required field |
|
3776 | + * @return string|array |
|
3777 | + */ |
|
3778 | + abstract protected function changeColumnSQL($tablename, $fieldDefs, $action, $ignoreRequired = false); |
|
3779 | + |
|
3780 | + /** |
|
3781 | + * Disconnects from the database |
|
3782 | + * |
|
3783 | + * Also handles any cleanup needed |
|
3784 | + */ |
|
3785 | + abstract public function disconnect(); |
|
3786 | + |
|
3787 | + /** |
|
3788 | + * Get last database error |
|
3789 | + * This function should return last error as reported by DB driver |
|
3790 | + * and should return false if no error condition happened |
|
3791 | + * @return string|false Error message or false if no error happened |
|
3792 | + */ |
|
3793 | + abstract public function lastDbError(); |
|
3794 | 3794 | |
3795 | 3795 | /** |
3796 | 3796 | * Check if this query is valid |
@@ -3798,82 +3798,82 @@ discard block |
||
3798 | 3798 | * @param string $query |
3799 | 3799 | * @return bool |
3800 | 3800 | */ |
3801 | - abstract public function validateQuery($query); |
|
3802 | - |
|
3803 | - /** |
|
3804 | - * Check if this driver can be used |
|
3805 | - * @return bool |
|
3806 | - */ |
|
3807 | - abstract public function valid(); |
|
3808 | - |
|
3809 | - /** |
|
3810 | - * Check if certain database exists |
|
3811 | - * @param string $dbname |
|
3812 | - */ |
|
3813 | - abstract public function dbExists($dbname); |
|
3814 | - |
|
3815 | - /** |
|
3816 | - * Get tables like expression |
|
3817 | - * @param string $like Expression describing tables |
|
3818 | - * @return array |
|
3819 | - */ |
|
3820 | - abstract public function tablesLike($like); |
|
3821 | - |
|
3822 | - /** |
|
3823 | - * Create a database |
|
3824 | - * @param string $dbname |
|
3825 | - */ |
|
3826 | - abstract public function createDatabase($dbname); |
|
3827 | - |
|
3828 | - /** |
|
3829 | - * Drop a database |
|
3830 | - * @param string $dbname |
|
3831 | - */ |
|
3832 | - abstract public function dropDatabase($dbname); |
|
3833 | - |
|
3834 | - /** |
|
3835 | - * Get database configuration information (DB-dependent) |
|
3836 | - * @return array|null |
|
3837 | - */ |
|
3838 | - abstract public function getDbInfo(); |
|
3839 | - |
|
3840 | - /** |
|
3841 | - * Check if certain DB user exists |
|
3842 | - * @param string $username |
|
3843 | - */ |
|
3844 | - abstract public function userExists($username); |
|
3845 | - |
|
3846 | - /** |
|
3847 | - * Create DB user |
|
3848 | - * @param string $database_name |
|
3849 | - * @param string $host_name |
|
3850 | - * @param string $user |
|
3851 | - * @param string $password |
|
3852 | - */ |
|
3853 | - abstract public function createDbUser($database_name, $host_name, $user, $password); |
|
3854 | - |
|
3855 | - /** |
|
3856 | - * Check if the database supports fulltext indexing |
|
3857 | - * Note that database driver can be capable of supporting FT (see supports('fulltext)) |
|
3858 | - * but particular instance can still have it disabled |
|
3859 | - * @return bool |
|
3860 | - */ |
|
3861 | - abstract public function full_text_indexing_installed(); |
|
3862 | - |
|
3863 | - /** |
|
3864 | - * Generate fulltext query from set of terms |
|
3865 | - * @param string $field Field to search against |
|
3866 | - * @param array $terms Search terms that may be or not be in the result |
|
3867 | - * @param array $must_terms Search terms that have to be in the result |
|
3868 | - * @param array $exclude_terms Search terms that have to be not in the result |
|
3869 | - */ |
|
3870 | - abstract public function getFulltextQuery($field, $terms, $must_terms = array(), $exclude_terms = array()); |
|
3871 | - |
|
3872 | - /** |
|
3873 | - * Get install configuration for this DB |
|
3874 | - * @return array |
|
3875 | - */ |
|
3876 | - abstract public function installConfig(); |
|
3801 | + abstract public function validateQuery($query); |
|
3802 | + |
|
3803 | + /** |
|
3804 | + * Check if this driver can be used |
|
3805 | + * @return bool |
|
3806 | + */ |
|
3807 | + abstract public function valid(); |
|
3808 | + |
|
3809 | + /** |
|
3810 | + * Check if certain database exists |
|
3811 | + * @param string $dbname |
|
3812 | + */ |
|
3813 | + abstract public function dbExists($dbname); |
|
3814 | + |
|
3815 | + /** |
|
3816 | + * Get tables like expression |
|
3817 | + * @param string $like Expression describing tables |
|
3818 | + * @return array |
|
3819 | + */ |
|
3820 | + abstract public function tablesLike($like); |
|
3821 | + |
|
3822 | + /** |
|
3823 | + * Create a database |
|
3824 | + * @param string $dbname |
|
3825 | + */ |
|
3826 | + abstract public function createDatabase($dbname); |
|
3827 | + |
|
3828 | + /** |
|
3829 | + * Drop a database |
|
3830 | + * @param string $dbname |
|
3831 | + */ |
|
3832 | + abstract public function dropDatabase($dbname); |
|
3833 | + |
|
3834 | + /** |
|
3835 | + * Get database configuration information (DB-dependent) |
|
3836 | + * @return array|null |
|
3837 | + */ |
|
3838 | + abstract public function getDbInfo(); |
|
3839 | + |
|
3840 | + /** |
|
3841 | + * Check if certain DB user exists |
|
3842 | + * @param string $username |
|
3843 | + */ |
|
3844 | + abstract public function userExists($username); |
|
3845 | + |
|
3846 | + /** |
|
3847 | + * Create DB user |
|
3848 | + * @param string $database_name |
|
3849 | + * @param string $host_name |
|
3850 | + * @param string $user |
|
3851 | + * @param string $password |
|
3852 | + */ |
|
3853 | + abstract public function createDbUser($database_name, $host_name, $user, $password); |
|
3854 | + |
|
3855 | + /** |
|
3856 | + * Check if the database supports fulltext indexing |
|
3857 | + * Note that database driver can be capable of supporting FT (see supports('fulltext)) |
|
3858 | + * but particular instance can still have it disabled |
|
3859 | + * @return bool |
|
3860 | + */ |
|
3861 | + abstract public function full_text_indexing_installed(); |
|
3862 | + |
|
3863 | + /** |
|
3864 | + * Generate fulltext query from set of terms |
|
3865 | + * @param string $field Field to search against |
|
3866 | + * @param array $terms Search terms that may be or not be in the result |
|
3867 | + * @param array $must_terms Search terms that have to be in the result |
|
3868 | + * @param array $exclude_terms Search terms that have to be not in the result |
|
3869 | + */ |
|
3870 | + abstract public function getFulltextQuery($field, $terms, $must_terms = array(), $exclude_terms = array()); |
|
3871 | + |
|
3872 | + /** |
|
3873 | + * Get install configuration for this DB |
|
3874 | + * @return array |
|
3875 | + */ |
|
3876 | + abstract public function installConfig(); |
|
3877 | 3877 | |
3878 | 3878 | /** |
3879 | 3879 | * Returns a DB specific FROM clause which can be used to select against functions. |
@@ -3890,5 +3890,5 @@ discard block |
||
3890 | 3890 | * @abstract |
3891 | 3891 | * @return string |
3892 | 3892 | */ |
3893 | - abstract public function getGuidSQL(); |
|
3893 | + abstract public function getGuidSQL(); |
|
3894 | 3894 | } |
@@ -2,68 +2,68 @@ discard block |
||
2 | 2 | |
3 | 3 | class ContactTest extends PHPUnit_Framework_TestCase { |
4 | 4 | |
5 | - public function testContact() { |
|
5 | + public function testContact() { |
|
6 | 6 | |
7 | - //execute the contructor and check for the Object type and attributes |
|
8 | - $contact = new Contact(); |
|
9 | - $this->assertInstanceOf('Contact',$contact); |
|
10 | - $this->assertInstanceOf('Person',$contact); |
|
11 | - $this->assertInstanceOf('SugarBean',$contact); |
|
7 | + //execute the contructor and check for the Object type and attributes |
|
8 | + $contact = new Contact(); |
|
9 | + $this->assertInstanceOf('Contact',$contact); |
|
10 | + $this->assertInstanceOf('Person',$contact); |
|
11 | + $this->assertInstanceOf('SugarBean',$contact); |
|
12 | 12 | |
13 | - $this->assertAttributeEquals('Contacts', 'module_dir', $contact); |
|
14 | - $this->assertAttributeEquals('Contact', 'object_name', $contact); |
|
15 | - $this->assertAttributeEquals('contacts', 'table_name', $contact); |
|
16 | - $this->assertAttributeEquals('accounts_contacts', 'rel_account_table', $contact); |
|
17 | - $this->assertAttributeEquals('opportunities_contacts', 'rel_opportunity_table', $contact); |
|
18 | - $this->assertAttributeEquals(true, 'importable', $contact); |
|
19 | - $this->assertAttributeEquals(true, 'new_schema', $contact); |
|
13 | + $this->assertAttributeEquals('Contacts', 'module_dir', $contact); |
|
14 | + $this->assertAttributeEquals('Contact', 'object_name', $contact); |
|
15 | + $this->assertAttributeEquals('contacts', 'table_name', $contact); |
|
16 | + $this->assertAttributeEquals('accounts_contacts', 'rel_account_table', $contact); |
|
17 | + $this->assertAttributeEquals('opportunities_contacts', 'rel_opportunity_table', $contact); |
|
18 | + $this->assertAttributeEquals(true, 'importable', $contact); |
|
19 | + $this->assertAttributeEquals(true, 'new_schema', $contact); |
|
20 | 20 | |
21 | - } |
|
21 | + } |
|
22 | 22 | |
23 | - public function testadd_list_count_joins() |
|
24 | - { |
|
25 | - error_reporting(E_ERROR | E_PARSE); |
|
23 | + public function testadd_list_count_joins() |
|
24 | + { |
|
25 | + error_reporting(E_ERROR | E_PARSE); |
|
26 | 26 | |
27 | - $contact = new Contact(); |
|
27 | + $contact = new Contact(); |
|
28 | 28 | |
29 | - //test with empty strings |
|
30 | - $query = ""; |
|
31 | - $contact->add_list_count_joins($query, ''); |
|
32 | - $this->assertEquals(" LEFT JOIN contacts_cstm ON contacts.id = contacts_cstm.id_c ",$query); |
|
29 | + //test with empty strings |
|
30 | + $query = ""; |
|
31 | + $contact->add_list_count_joins($query, ''); |
|
32 | + $this->assertEquals(" LEFT JOIN contacts_cstm ON contacts.id = contacts_cstm.id_c ",$query); |
|
33 | 33 | |
34 | 34 | |
35 | - //test with valid string |
|
36 | - $query = ""; |
|
37 | - $expected = "\n LEFT JOIN accounts_contacts\n ON contacts.id=accounts_contacts.contact_id\n LEFT JOIN accounts\n ON accounts_contacts.account_id=accounts.id\n LEFT JOIN contacts_cstm ON contacts.id = contacts_cstm.id_c "; |
|
38 | - $contact->add_list_count_joins($query, 'accounts.name'); |
|
39 | - $query = preg_replace('/\s+/', '', $query); |
|
40 | - $expected =preg_replace('/\s+/', '', $expected); |
|
41 | - $this->assertSame($expected,$query); |
|
35 | + //test with valid string |
|
36 | + $query = ""; |
|
37 | + $expected = "\n LEFT JOIN accounts_contacts\n ON contacts.id=accounts_contacts.contact_id\n LEFT JOIN accounts\n ON accounts_contacts.account_id=accounts.id\n LEFT JOIN contacts_cstm ON contacts.id = contacts_cstm.id_c "; |
|
38 | + $contact->add_list_count_joins($query, 'accounts.name'); |
|
39 | + $query = preg_replace('/\s+/', '', $query); |
|
40 | + $expected =preg_replace('/\s+/', '', $expected); |
|
41 | + $this->assertSame($expected,$query); |
|
42 | 42 | |
43 | - //test with valid string |
|
44 | - $query = ""; |
|
45 | - $expected = "\n LEFT JOIN accounts_contacts\n ON contacts.id=accounts_contacts.contact_id\n LEFT JOIN accounts\n ON accounts_contacts.account_id=accounts.id\n LEFT JOIN contacts_cstm ON contacts.id = contacts_cstm.id_c "; |
|
46 | - $contact->add_list_count_joins($query, 'contacts.name'); |
|
47 | - $this->assertSame(" LEFT JOIN contacts_cstm ON contacts.id = contacts_cstm.id_c ",$query); |
|
43 | + //test with valid string |
|
44 | + $query = ""; |
|
45 | + $expected = "\n LEFT JOIN accounts_contacts\n ON contacts.id=accounts_contacts.contact_id\n LEFT JOIN accounts\n ON accounts_contacts.account_id=accounts.id\n LEFT JOIN contacts_cstm ON contacts.id = contacts_cstm.id_c "; |
|
46 | + $contact->add_list_count_joins($query, 'contacts.name'); |
|
47 | + $this->assertSame(" LEFT JOIN contacts_cstm ON contacts.id = contacts_cstm.id_c ",$query); |
|
48 | 48 | |
49 | 49 | |
50 | - } |
|
50 | + } |
|
51 | 51 | |
52 | - public function testlistviewACLHelper() |
|
53 | - { |
|
54 | - $contact = new Contact(); |
|
52 | + public function testlistviewACLHelper() |
|
53 | + { |
|
54 | + $contact = new Contact(); |
|
55 | 55 | |
56 | - $expected = array( "MAIN"=>"a", "ACCOUNT"=>"a"); |
|
57 | - $actual = $contact->listviewACLHelper(); |
|
58 | - $this->assertSame($expected,$actual); |
|
56 | + $expected = array( "MAIN"=>"a", "ACCOUNT"=>"a"); |
|
57 | + $actual = $contact->listviewACLHelper(); |
|
58 | + $this->assertSame($expected,$actual); |
|
59 | 59 | |
60 | - } |
|
60 | + } |
|
61 | 61 | |
62 | 62 | /** |
63 | 63 | * @todo: NEEDS FIXING! |
64 | 64 | */ |
65 | - public function testcreate_new_list_query() |
|
66 | - { |
|
65 | + public function testcreate_new_list_query() |
|
66 | + { |
|
67 | 67 | /* |
68 | 68 | $contact = new Contact(); |
69 | 69 | |
@@ -79,239 +79,239 @@ discard block |
||
79 | 79 | $this->assertSame($expected,$actual); |
80 | 80 | */ |
81 | 81 | $this->assertTrue(true, "NEEDS FIXING!"); |
82 | - } |
|
82 | + } |
|
83 | 83 | |
84 | 84 | |
85 | - public function testaddress_popup_create_new_list_query() |
|
86 | - { |
|
85 | + public function testaddress_popup_create_new_list_query() |
|
86 | + { |
|
87 | 87 | |
88 | - $contact = new Contact(); |
|
88 | + $contact = new Contact(); |
|
89 | 89 | |
90 | - //test with empty string params |
|
91 | - $expected = "SELECT LTRIM(RTRIM(CONCAT(IFNULL(contacts.first_name,''),'',IFNULL(contacts.last_name,'')))) name, \n contacts.*,\n accounts.name as account_name,\n accounts.id as account_id,\n accounts.assigned_user_id account_id_owner,\n users.user_name as assigned_user_name ,contacts_cstm.*\n FROM contacts LEFT JOIN users\n ON contacts.assigned_user_id=users.id\n LEFT JOIN accounts_contacts\n ON contacts.id=accounts_contacts.contact_id and accounts_contacts.deleted = 0\n LEFT JOIN accounts\n ON accounts_contacts.account_id=accounts.id AND accounts.deleted=0 LEFT JOIN email_addr_bean_rel eabl ON eabl.bean_id = contacts.id AND eabl.bean_module = 'Contacts' and eabl.primary_address = 1 and eabl.deleted=0 LEFT JOIN email_addresses ea ON (ea.id = eabl.email_address_id) LEFT JOIN contacts_cstm ON contacts.id = contacts_cstm.id_c where contacts.deleted=0 "; |
|
92 | - $actual = $contact->address_popup_create_new_list_query('',''); |
|
93 | - $this->assertSame($expected,$actual); |
|
90 | + //test with empty string params |
|
91 | + $expected = "SELECT LTRIM(RTRIM(CONCAT(IFNULL(contacts.first_name,''),'',IFNULL(contacts.last_name,'')))) name, \n contacts.*,\n accounts.name as account_name,\n accounts.id as account_id,\n accounts.assigned_user_id account_id_owner,\n users.user_name as assigned_user_name ,contacts_cstm.*\n FROM contacts LEFT JOIN users\n ON contacts.assigned_user_id=users.id\n LEFT JOIN accounts_contacts\n ON contacts.id=accounts_contacts.contact_id and accounts_contacts.deleted = 0\n LEFT JOIN accounts\n ON accounts_contacts.account_id=accounts.id AND accounts.deleted=0 LEFT JOIN email_addr_bean_rel eabl ON eabl.bean_id = contacts.id AND eabl.bean_module = 'Contacts' and eabl.primary_address = 1 and eabl.deleted=0 LEFT JOIN email_addresses ea ON (ea.id = eabl.email_address_id) LEFT JOIN contacts_cstm ON contacts.id = contacts_cstm.id_c where contacts.deleted=0 "; |
|
92 | + $actual = $contact->address_popup_create_new_list_query('',''); |
|
93 | + $this->assertSame($expected,$actual); |
|
94 | 94 | |
95 | 95 | |
96 | - //test with valid string params |
|
97 | - $expected = "SELECT LTRIM(RTRIM(CONCAT(IFNULL(contacts.first_name,''),'',IFNULL(contacts.last_name,'')))) name, \n contacts.*,\n accounts.name as account_name,\n accounts.id as account_id,\n accounts.assigned_user_id account_id_owner,\n users.user_name as assigned_user_name ,contacts_cstm.*\n FROM contacts LEFT JOIN users\n ON contacts.assigned_user_id=users.id\n LEFT JOIN accounts_contacts\n ON contacts.id=accounts_contacts.contact_id and accounts_contacts.deleted = 0\n LEFT JOIN accounts\n ON accounts_contacts.account_id=accounts.id AND accounts.deleted=0 LEFT JOIN email_addr_bean_rel eabl ON eabl.bean_id = contacts.id AND eabl.bean_module = 'Contacts' and eabl.primary_address = 1 and eabl.deleted=0 LEFT JOIN email_addresses ea ON (ea.id = eabl.email_address_id) LEFT JOIN contacts_cstm ON contacts.id = contacts_cstm.id_c where (contacts.name=\"\") AND contacts.deleted=0 "; |
|
98 | - $actual = $contact->address_popup_create_new_list_query('contacts.id','contacts.name=""'); |
|
99 | - $this->assertSame($expected,$actual); |
|
96 | + //test with valid string params |
|
97 | + $expected = "SELECT LTRIM(RTRIM(CONCAT(IFNULL(contacts.first_name,''),'',IFNULL(contacts.last_name,'')))) name, \n contacts.*,\n accounts.name as account_name,\n accounts.id as account_id,\n accounts.assigned_user_id account_id_owner,\n users.user_name as assigned_user_name ,contacts_cstm.*\n FROM contacts LEFT JOIN users\n ON contacts.assigned_user_id=users.id\n LEFT JOIN accounts_contacts\n ON contacts.id=accounts_contacts.contact_id and accounts_contacts.deleted = 0\n LEFT JOIN accounts\n ON accounts_contacts.account_id=accounts.id AND accounts.deleted=0 LEFT JOIN email_addr_bean_rel eabl ON eabl.bean_id = contacts.id AND eabl.bean_module = 'Contacts' and eabl.primary_address = 1 and eabl.deleted=0 LEFT JOIN email_addresses ea ON (ea.id = eabl.email_address_id) LEFT JOIN contacts_cstm ON contacts.id = contacts_cstm.id_c where (contacts.name=\"\") AND contacts.deleted=0 "; |
|
98 | + $actual = $contact->address_popup_create_new_list_query('contacts.id','contacts.name=""'); |
|
99 | + $this->assertSame($expected,$actual); |
|
100 | 100 | |
101 | - } |
|
101 | + } |
|
102 | 102 | |
103 | - public function testcreate_export_query() |
|
104 | - { |
|
105 | - $contact = new Contact(); |
|
103 | + public function testcreate_export_query() |
|
104 | + { |
|
105 | + $contact = new Contact(); |
|
106 | 106 | |
107 | - //test with empty string params |
|
108 | - $expected = "SELECT\n contacts.*,\n email_addresses.email_address email_address,\n '' email_addresses_non_primary, accounts.name as account_name,\n users.user_name as assigned_user_name ,contacts_cstm.jjwg_maps_lng_c,contacts_cstm.jjwg_maps_lat_c,contacts_cstm.jjwg_maps_geocode_status_c,contacts_cstm.jjwg_maps_address_c FROM contacts LEFT JOIN users\n ON contacts.assigned_user_id=users.id LEFT JOIN accounts_contacts\n ON ( contacts.id=accounts_contacts.contact_id and (accounts_contacts.deleted is null or accounts_contacts.deleted = 0))\n LEFT JOIN accounts\n ON accounts_contacts.account_id=accounts.id LEFT JOIN email_addr_bean_rel on contacts.id = email_addr_bean_rel.bean_id and email_addr_bean_rel.bean_module='Contacts' and email_addr_bean_rel.deleted=0 and email_addr_bean_rel.primary_address=1 LEFT JOIN email_addresses on email_addresses.id = email_addr_bean_rel.email_address_id LEFT JOIN contacts_cstm ON contacts.id = contacts_cstm.id_c where ( accounts.deleted IS NULL OR accounts.deleted=0 )\n AND contacts.deleted=0 "; |
|
109 | - $actual = $contact->create_export_query('',''); |
|
110 | - $this->assertSame($expected,$actual); |
|
107 | + //test with empty string params |
|
108 | + $expected = "SELECT\n contacts.*,\n email_addresses.email_address email_address,\n '' email_addresses_non_primary, accounts.name as account_name,\n users.user_name as assigned_user_name ,contacts_cstm.jjwg_maps_lng_c,contacts_cstm.jjwg_maps_lat_c,contacts_cstm.jjwg_maps_geocode_status_c,contacts_cstm.jjwg_maps_address_c FROM contacts LEFT JOIN users\n ON contacts.assigned_user_id=users.id LEFT JOIN accounts_contacts\n ON ( contacts.id=accounts_contacts.contact_id and (accounts_contacts.deleted is null or accounts_contacts.deleted = 0))\n LEFT JOIN accounts\n ON accounts_contacts.account_id=accounts.id LEFT JOIN email_addr_bean_rel on contacts.id = email_addr_bean_rel.bean_id and email_addr_bean_rel.bean_module='Contacts' and email_addr_bean_rel.deleted=0 and email_addr_bean_rel.primary_address=1 LEFT JOIN email_addresses on email_addresses.id = email_addr_bean_rel.email_address_id LEFT JOIN contacts_cstm ON contacts.id = contacts_cstm.id_c where ( accounts.deleted IS NULL OR accounts.deleted=0 )\n AND contacts.deleted=0 "; |
|
109 | + $actual = $contact->create_export_query('',''); |
|
110 | + $this->assertSame($expected,$actual); |
|
111 | 111 | |
112 | 112 | |
113 | - //test with valid string params |
|
114 | - $expected = "SELECT\n contacts.*,\n email_addresses.email_address email_address,\n '' email_addresses_non_primary, accounts.name as account_name,\n users.user_name as assigned_user_name ,contacts_cstm.jjwg_maps_lng_c,contacts_cstm.jjwg_maps_lat_c,contacts_cstm.jjwg_maps_geocode_status_c,contacts_cstm.jjwg_maps_address_c FROM contacts LEFT JOIN users\n ON contacts.assigned_user_id=users.id LEFT JOIN accounts_contacts\n ON ( contacts.id=accounts_contacts.contact_id and (accounts_contacts.deleted is null or accounts_contacts.deleted = 0))\n LEFT JOIN accounts\n ON accounts_contacts.account_id=accounts.id LEFT JOIN email_addr_bean_rel on contacts.id = email_addr_bean_rel.bean_id and email_addr_bean_rel.bean_module='Contacts' and email_addr_bean_rel.deleted=0 and email_addr_bean_rel.primary_address=1 LEFT JOIN email_addresses on email_addresses.id = email_addr_bean_rel.email_address_id LEFT JOIN contacts_cstm ON contacts.id = contacts_cstm.id_c where (contacts.name=\"\") AND ( accounts.deleted IS NULL OR accounts.deleted=0 )\n AND contacts.deleted=0 "; |
|
115 | - $actual = $contact->create_export_query('contacts.id','contacts.name=""'); |
|
116 | - $this->assertSame($expected,$actual); |
|
113 | + //test with valid string params |
|
114 | + $expected = "SELECT\n contacts.*,\n email_addresses.email_address email_address,\n '' email_addresses_non_primary, accounts.name as account_name,\n users.user_name as assigned_user_name ,contacts_cstm.jjwg_maps_lng_c,contacts_cstm.jjwg_maps_lat_c,contacts_cstm.jjwg_maps_geocode_status_c,contacts_cstm.jjwg_maps_address_c FROM contacts LEFT JOIN users\n ON contacts.assigned_user_id=users.id LEFT JOIN accounts_contacts\n ON ( contacts.id=accounts_contacts.contact_id and (accounts_contacts.deleted is null or accounts_contacts.deleted = 0))\n LEFT JOIN accounts\n ON accounts_contacts.account_id=accounts.id LEFT JOIN email_addr_bean_rel on contacts.id = email_addr_bean_rel.bean_id and email_addr_bean_rel.bean_module='Contacts' and email_addr_bean_rel.deleted=0 and email_addr_bean_rel.primary_address=1 LEFT JOIN email_addresses on email_addresses.id = email_addr_bean_rel.email_address_id LEFT JOIN contacts_cstm ON contacts.id = contacts_cstm.id_c where (contacts.name=\"\") AND ( accounts.deleted IS NULL OR accounts.deleted=0 )\n AND contacts.deleted=0 "; |
|
115 | + $actual = $contact->create_export_query('contacts.id','contacts.name=""'); |
|
116 | + $this->assertSame($expected,$actual); |
|
117 | 117 | |
118 | - } |
|
118 | + } |
|
119 | 119 | |
120 | - public function testfill_in_additional_list_fields() { |
|
120 | + public function testfill_in_additional_list_fields() { |
|
121 | 121 | |
122 | - $contact = new Contact(); |
|
122 | + $contact = new Contact(); |
|
123 | 123 | |
124 | - //test with attributes preset and verify attributes are set accordingly |
|
125 | - $contact->first_name = "firstn"; |
|
126 | - $contact->last_name = "lastn"; |
|
127 | - $contact->email1 = "[email protected]"; |
|
128 | - $contact->email2 = "[email protected]"; |
|
124 | + //test with attributes preset and verify attributes are set accordingly |
|
125 | + $contact->first_name = "firstn"; |
|
126 | + $contact->last_name = "lastn"; |
|
127 | + $contact->email1 = "[email protected]"; |
|
128 | + $contact->email2 = "[email protected]"; |
|
129 | 129 | |
130 | 130 | |
131 | - $contact->fill_in_additional_list_fields(); |
|
131 | + $contact->fill_in_additional_list_fields(); |
|
132 | 132 | |
133 | - $this->assertEquals("firstn lastn",$contact->full_name); |
|
134 | - $this->assertEquals("firstn lastn <[email protected]>",$contact->email_and_name1); |
|
135 | - $this->assertEquals("firstn lastn <[email protected]>",$contact->email_and_name2); |
|
133 | + $this->assertEquals("firstn lastn",$contact->full_name); |
|
134 | + $this->assertEquals("firstn lastn <[email protected]>",$contact->email_and_name1); |
|
135 | + $this->assertEquals("firstn lastn <[email protected]>",$contact->email_and_name2); |
|
136 | 136 | |
137 | - } |
|
137 | + } |
|
138 | 138 | |
139 | - public function testfill_in_additional_detail_fields() { |
|
139 | + public function testfill_in_additional_detail_fields() { |
|
140 | 140 | |
141 | - $contact = new Contact(); |
|
141 | + $contact = new Contact(); |
|
142 | 142 | |
143 | - //test with attributes preset and verify attributes are set accordingly |
|
144 | - $contact->id = "1"; |
|
143 | + //test with attributes preset and verify attributes are set accordingly |
|
144 | + $contact->id = "1"; |
|
145 | 145 | |
146 | - $contact->fill_in_additional_detail_fields(); |
|
146 | + $contact->fill_in_additional_detail_fields(); |
|
147 | 147 | |
148 | - $this->assertEquals("",$contact->account_name); |
|
149 | - $this->assertEquals("",$contact->account_id); |
|
150 | - $this->assertEquals("",$contact->report_to_name); |
|
148 | + $this->assertEquals("",$contact->account_name); |
|
149 | + $this->assertEquals("",$contact->account_id); |
|
150 | + $this->assertEquals("",$contact->report_to_name); |
|
151 | 151 | |
152 | - } |
|
152 | + } |
|
153 | 153 | |
154 | 154 | |
155 | - public function testload_contacts_users_relationship(){ |
|
155 | + public function testload_contacts_users_relationship(){ |
|
156 | 156 | |
157 | - $contact = new Contact(); |
|
157 | + $contact = new Contact(); |
|
158 | 158 | |
159 | - //execute the method and test if it works and does not throws an exception. |
|
160 | - try { |
|
161 | - $contact->load_contacts_users_relationship(); |
|
162 | - $this->assertTrue(true); |
|
163 | - } |
|
164 | - catch (Exception $e) { |
|
165 | - $this->fail(); |
|
166 | - } |
|
159 | + //execute the method and test if it works and does not throws an exception. |
|
160 | + try { |
|
161 | + $contact->load_contacts_users_relationship(); |
|
162 | + $this->assertTrue(true); |
|
163 | + } |
|
164 | + catch (Exception $e) { |
|
165 | + $this->fail(); |
|
166 | + } |
|
167 | 167 | |
168 | - } |
|
168 | + } |
|
169 | 169 | |
170 | - public function testget_list_view_data() { |
|
170 | + public function testget_list_view_data() { |
|
171 | 171 | |
172 | - $contact = new Contact(); |
|
172 | + $contact = new Contact(); |
|
173 | 173 | |
174 | - //test with attributes preset and verify attributes are set accordingly |
|
175 | - $contact->first_name = "first"; |
|
176 | - $contact->last_name = "last"; |
|
177 | - |
|
178 | - $expected = array ( |
|
179 | - 'NAME' => 'first last', |
|
180 | - 'DELETED' => 0, |
|
181 | - 'FIRST_NAME' => 'first', |
|
182 | - 'LAST_NAME' => 'last', |
|
183 | - 'FULL_NAME' => 'first last', |
|
184 | - 'DO_NOT_CALL' => '0', |
|
185 | - 'PORTAL_USER_TYPE' => 'Single user', |
|
186 | - 'ENCODED_NAME' => 'first last', |
|
187 | - 'EMAIL1' => '', |
|
188 | - 'EMAIL1_LINK' => '<a href=\'javascript:void(0);\' onclick=\'SUGAR.quickCompose.init({"fullComposeUrl":"contact_id=\\u0026parent_type=Contacts\\u0026parent_id=\\u0026parent_name=first+last\\u0026to_addrs_ids=\\u0026to_addrs_names=first+last\\u0026to_addrs_emails=\\u0026to_email_addrs=first+last%26nbsp%3B%26lt%3B%26gt%3B\\u0026return_module=Contacts\\u0026return_action=ListView\\u0026return_id=","composePackage":{"contact_id":"","parent_type":"Contacts","parent_id":"","parent_name":"first last","to_addrs_ids":"","to_addrs_names":"first last","to_addrs_emails":"","to_email_addrs":"first last \\u003C\\u003E","return_module":"Contacts","return_action":"ListView","return_id":""}});\' class=\'\'>', |
|
189 | - 'EMAIL_AND_NAME1' => 'first last <>', |
|
190 | - ); |
|
191 | - |
|
192 | - $actual = $contact->get_list_view_data(); |
|
193 | - //$this->assertSame($expected, $actual); |
|
194 | - $this->assertEquals($expected['NAME'], $actual['NAME']); |
|
195 | - $this->assertEquals($expected['FIRST_NAME'], $actual['FIRST_NAME']); |
|
196 | - $this->assertEquals($expected['LAST_NAME'], $actual['LAST_NAME']); |
|
197 | - $this->assertEquals($expected['FULL_NAME'], $actual['FULL_NAME']); |
|
198 | - $this->assertEquals($expected['ENCODED_NAME'], $actual['ENCODED_NAME']); |
|
199 | - $this->assertEquals($expected['EMAIL_AND_NAME1'], $actual['EMAIL_AND_NAME1']); |
|
200 | - |
|
201 | - } |
|
202 | - |
|
203 | - |
|
204 | - public function testbuild_generic_where_clause () |
|
205 | - { |
|
174 | + //test with attributes preset and verify attributes are set accordingly |
|
175 | + $contact->first_name = "first"; |
|
176 | + $contact->last_name = "last"; |
|
206 | 177 | |
207 | - $contact = new Contact(); |
|
178 | + $expected = array ( |
|
179 | + 'NAME' => 'first last', |
|
180 | + 'DELETED' => 0, |
|
181 | + 'FIRST_NAME' => 'first', |
|
182 | + 'LAST_NAME' => 'last', |
|
183 | + 'FULL_NAME' => 'first last', |
|
184 | + 'DO_NOT_CALL' => '0', |
|
185 | + 'PORTAL_USER_TYPE' => 'Single user', |
|
186 | + 'ENCODED_NAME' => 'first last', |
|
187 | + 'EMAIL1' => '', |
|
188 | + 'EMAIL1_LINK' => '<a href=\'javascript:void(0);\' onclick=\'SUGAR.quickCompose.init({"fullComposeUrl":"contact_id=\\u0026parent_type=Contacts\\u0026parent_id=\\u0026parent_name=first+last\\u0026to_addrs_ids=\\u0026to_addrs_names=first+last\\u0026to_addrs_emails=\\u0026to_email_addrs=first+last%26nbsp%3B%26lt%3B%26gt%3B\\u0026return_module=Contacts\\u0026return_action=ListView\\u0026return_id=","composePackage":{"contact_id":"","parent_type":"Contacts","parent_id":"","parent_name":"first last","to_addrs_ids":"","to_addrs_names":"first last","to_addrs_emails":"","to_email_addrs":"first last \\u003C\\u003E","return_module":"Contacts","return_action":"ListView","return_id":""}});\' class=\'\'>', |
|
189 | + 'EMAIL_AND_NAME1' => 'first last <>', |
|
190 | + ); |
|
208 | 191 | |
209 | - //test with string |
|
210 | - $expected = "contacts.last_name like 'test%' or contacts.first_name like 'test%' or accounts.name like 'test%' or contacts.assistant like 'test%' or ea.email_address like 'test%'"; |
|
211 | - $actual = $contact->build_generic_where_clause('test'); |
|
212 | - $this->assertSame($expected,$actual); |
|
192 | + $actual = $contact->get_list_view_data(); |
|
193 | + //$this->assertSame($expected, $actual); |
|
194 | + $this->assertEquals($expected['NAME'], $actual['NAME']); |
|
195 | + $this->assertEquals($expected['FIRST_NAME'], $actual['FIRST_NAME']); |
|
196 | + $this->assertEquals($expected['LAST_NAME'], $actual['LAST_NAME']); |
|
197 | + $this->assertEquals($expected['FULL_NAME'], $actual['FULL_NAME']); |
|
198 | + $this->assertEquals($expected['ENCODED_NAME'], $actual['ENCODED_NAME']); |
|
199 | + $this->assertEquals($expected['EMAIL_AND_NAME1'], $actual['EMAIL_AND_NAME1']); |
|
213 | 200 | |
201 | + } |
|
214 | 202 | |
215 | - //test with number |
|
216 | - $expected = "contacts.last_name like '1%' or contacts.first_name like '1%' or accounts.name like '1%' or contacts.assistant like '1%' or ea.email_address like '1%' or contacts.phone_home like '%1%' or contacts.phone_mobile like '%1%' or contacts.phone_work like '%1%' or contacts.phone_other like '%1%' or contacts.phone_fax like '%1%' or contacts.assistant_phone like '%1%'"; |
|
217 | - $actual = $contact->build_generic_where_clause(1); |
|
218 | - $this->assertSame($expected,$actual); |
|
219 | 203 | |
220 | - } |
|
204 | + public function testbuild_generic_where_clause () |
|
205 | + { |
|
221 | 206 | |
222 | - public function testset_notification_body() |
|
223 | - { |
|
224 | - $contact = new Contact(); |
|
207 | + $contact = new Contact(); |
|
225 | 208 | |
226 | - //test with attributes preset and verify attributes are set accordingly |
|
227 | - $contact->first_name = "first"; |
|
228 | - $contact->last_name = "last"; |
|
229 | - $contact->description = "some text"; |
|
230 | - $contact->fill_in_additional_list_fields(); |
|
209 | + //test with string |
|
210 | + $expected = "contacts.last_name like 'test%' or contacts.first_name like 'test%' or accounts.name like 'test%' or contacts.assistant like 'test%' or ea.email_address like 'test%'"; |
|
211 | + $actual = $contact->build_generic_where_clause('test'); |
|
212 | + $this->assertSame($expected,$actual); |
|
231 | 213 | |
232 | - $result = $contact->set_notification_body(new Sugar_Smarty(), $contact); |
|
233 | 214 | |
234 | - $this->assertEquals($contact->full_name ,$result->_tpl_vars['CONTACT_NAME']); |
|
235 | - $this->assertEquals($contact->description ,$result->_tpl_vars['CONTACT_DESCRIPTION']); |
|
215 | + //test with number |
|
216 | + $expected = "contacts.last_name like '1%' or contacts.first_name like '1%' or accounts.name like '1%' or contacts.assistant like '1%' or ea.email_address like '1%' or contacts.phone_home like '%1%' or contacts.phone_mobile like '%1%' or contacts.phone_work like '%1%' or contacts.phone_other like '%1%' or contacts.phone_fax like '%1%' or contacts.assistant_phone like '%1%'"; |
|
217 | + $actual = $contact->build_generic_where_clause(1); |
|
218 | + $this->assertSame($expected,$actual); |
|
236 | 219 | |
237 | - } |
|
220 | + } |
|
238 | 221 | |
239 | - public function testget_contact_id_by_email() |
|
240 | - { |
|
241 | - $contact = new Contact(); |
|
222 | + public function testset_notification_body() |
|
223 | + { |
|
224 | + $contact = new Contact(); |
|
242 | 225 | |
243 | - $result = $contact->get_contact_id_by_email(""); |
|
244 | - $this->assertEquals(null,$result); |
|
226 | + //test with attributes preset and verify attributes are set accordingly |
|
227 | + $contact->first_name = "first"; |
|
228 | + $contact->last_name = "last"; |
|
229 | + $contact->description = "some text"; |
|
230 | + $contact->fill_in_additional_list_fields(); |
|
245 | 231 | |
232 | + $result = $contact->set_notification_body(new Sugar_Smarty(), $contact); |
|
246 | 233 | |
247 | - //$result = $contact->get_contact_id_by_email("[email protected]"); |
|
248 | - //$this->assertEquals(null,$result); |
|
234 | + $this->assertEquals($contact->full_name ,$result->_tpl_vars['CONTACT_NAME']); |
|
235 | + $this->assertEquals($contact->description ,$result->_tpl_vars['CONTACT_DESCRIPTION']); |
|
249 | 236 | |
250 | - $this->markTestSkipped('Invalid Columns(email1,email2) in Query '); |
|
237 | + } |
|
251 | 238 | |
252 | - } |
|
239 | + public function testget_contact_id_by_email() |
|
240 | + { |
|
241 | + $contact = new Contact(); |
|
253 | 242 | |
254 | - public function testsave_relationship_changes() { |
|
243 | + $result = $contact->get_contact_id_by_email(""); |
|
244 | + $this->assertEquals(null,$result); |
|
255 | 245 | |
256 | - $contact = new Contact(); |
|
257 | 246 | |
258 | - //execute the method and test if it works and does not throws an exception. |
|
259 | - try { |
|
260 | - $contact->save_relationship_changes(true); |
|
261 | - $contact->save_relationship_changes(false); |
|
262 | - $this->assertTrue(true); |
|
263 | - } |
|
264 | - catch (Exception $e) { |
|
265 | - $this->fail(); |
|
266 | - } |
|
247 | + //$result = $contact->get_contact_id_by_email("[email protected]"); |
|
248 | + //$this->assertEquals(null,$result); |
|
267 | 249 | |
268 | - } |
|
250 | + $this->markTestSkipped('Invalid Columns(email1,email2) in Query '); |
|
269 | 251 | |
270 | - public function testbean_implements() |
|
271 | - { |
|
272 | - $contact = new Contact(); |
|
273 | - $this->assertEquals(false, $contact->bean_implements('')); //test with blank value |
|
274 | - $this->assertEquals(false, $contact->bean_implements('test')); //test with invalid value |
|
275 | - $this->assertEquals(true, $contact->bean_implements('ACL')); //test with valid value |
|
276 | - } |
|
252 | + } |
|
277 | 253 | |
278 | - public function testget_unlinked_email_query() |
|
279 | - { |
|
280 | - $contact = new Contact(); |
|
254 | + public function testsave_relationship_changes() { |
|
281 | 255 | |
282 | - //execute the method and verify that it retunrs expected results |
|
283 | - $expected = "SELECT emails.id FROM emails JOIN (select DISTINCT email_id from emails_email_addr_rel eear\n\n join email_addr_bean_rel eabr on eabr.bean_id ='' and eabr.bean_module = 'Contacts' and\n eabr.email_address_id = eear.email_address_id and eabr.deleted=0\n where eear.deleted=0 and eear.email_id not in\n (select eb.email_id from emails_beans eb where eb.bean_module ='Contacts' and eb.bean_id = '')\n ) derivedemails on derivedemails.email_id = emails.id"; |
|
284 | - $actual = $contact->get_unlinked_email_query(); |
|
285 | - $this->assertSame($expected,$actual); |
|
256 | + $contact = new Contact(); |
|
257 | + |
|
258 | + //execute the method and test if it works and does not throws an exception. |
|
259 | + try { |
|
260 | + $contact->save_relationship_changes(true); |
|
261 | + $contact->save_relationship_changes(false); |
|
262 | + $this->assertTrue(true); |
|
263 | + } |
|
264 | + catch (Exception $e) { |
|
265 | + $this->fail(); |
|
266 | + } |
|
267 | + |
|
268 | + } |
|
269 | + |
|
270 | + public function testbean_implements() |
|
271 | + { |
|
272 | + $contact = new Contact(); |
|
273 | + $this->assertEquals(false, $contact->bean_implements('')); //test with blank value |
|
274 | + $this->assertEquals(false, $contact->bean_implements('test')); //test with invalid value |
|
275 | + $this->assertEquals(true, $contact->bean_implements('ACL')); //test with valid value |
|
276 | + } |
|
277 | + |
|
278 | + public function testget_unlinked_email_query() |
|
279 | + { |
|
280 | + $contact = new Contact(); |
|
281 | + |
|
282 | + //execute the method and verify that it retunrs expected results |
|
283 | + $expected = "SELECT emails.id FROM emails JOIN (select DISTINCT email_id from emails_email_addr_rel eear\n\n join email_addr_bean_rel eabr on eabr.bean_id ='' and eabr.bean_module = 'Contacts' and\n eabr.email_address_id = eear.email_address_id and eabr.deleted=0\n where eear.deleted=0 and eear.email_id not in\n (select eb.email_id from emails_beans eb where eb.bean_module ='Contacts' and eb.bean_id = '')\n ) derivedemails on derivedemails.email_id = emails.id"; |
|
284 | + $actual = $contact->get_unlinked_email_query(); |
|
285 | + $this->assertSame($expected,$actual); |
|
286 | 286 | |
287 | - } |
|
287 | + } |
|
288 | 288 | |
289 | 289 | |
290 | 290 | public function testprocess_sync_to_outlook() |
291 | 291 | { |
292 | - $contact = new Contact(); |
|
292 | + $contact = new Contact(); |
|
293 | 293 | |
294 | - //execute the method and test if it works and does not throws an exception. |
|
295 | - try { |
|
296 | - $contact->process_sync_to_outlook("all"); |
|
297 | - $this->assertTrue(true); |
|
298 | - } |
|
299 | - catch (Exception $e) { |
|
300 | - $this->fail(); |
|
301 | - } |
|
294 | + //execute the method and test if it works and does not throws an exception. |
|
295 | + try { |
|
296 | + $contact->process_sync_to_outlook("all"); |
|
297 | + $this->assertTrue(true); |
|
298 | + } |
|
299 | + catch (Exception $e) { |
|
300 | + $this->fail(); |
|
301 | + } |
|
302 | 302 | |
303 | 303 | |
304 | - //execute the method and test if it works and does not throws an exception. |
|
305 | - try { |
|
306 | - $contact->process_sync_to_outlook("1"); |
|
307 | - $this->assertTrue(true); |
|
308 | - } |
|
309 | - catch (Exception $e) { |
|
310 | - $this->fail(); |
|
311 | - } |
|
304 | + //execute the method and test if it works and does not throws an exception. |
|
305 | + try { |
|
306 | + $contact->process_sync_to_outlook("1"); |
|
307 | + $this->assertTrue(true); |
|
308 | + } |
|
309 | + catch (Exception $e) { |
|
310 | + $this->fail(); |
|
311 | + } |
|
312 | 312 | |
313 | 313 | |
314 | 314 | |
315 | - } |
|
315 | + } |
|
316 | 316 | |
317 | 317 | } |
@@ -3,102 +3,102 @@ discard block |
||
3 | 3 | class LeadTest extends PHPUnit_Framework_TestCase { |
4 | 4 | |
5 | 5 | |
6 | - public function testLead() |
|
7 | - { |
|
6 | + public function testLead() |
|
7 | + { |
|
8 | 8 | |
9 | - //execute the contructor and check for the Object type and attributes |
|
10 | - $lead = new Lead(); |
|
9 | + //execute the contructor and check for the Object type and attributes |
|
10 | + $lead = new Lead(); |
|
11 | 11 | |
12 | - $this->assertInstanceOf('Lead',$lead); |
|
13 | - $this->assertInstanceOf('Person',$lead); |
|
14 | - $this->assertInstanceOf('SugarBean',$lead); |
|
12 | + $this->assertInstanceOf('Lead',$lead); |
|
13 | + $this->assertInstanceOf('Person',$lead); |
|
14 | + $this->assertInstanceOf('SugarBean',$lead); |
|
15 | 15 | |
16 | - $this->assertAttributeEquals('Leads', 'module_dir', $lead); |
|
17 | - $this->assertAttributeEquals('Lead', 'object_name', $lead); |
|
18 | - $this->assertAttributeEquals('Leads', 'object_names', $lead); |
|
19 | - $this->assertAttributeEquals('leads', 'table_name', $lead); |
|
16 | + $this->assertAttributeEquals('Leads', 'module_dir', $lead); |
|
17 | + $this->assertAttributeEquals('Lead', 'object_name', $lead); |
|
18 | + $this->assertAttributeEquals('Leads', 'object_names', $lead); |
|
19 | + $this->assertAttributeEquals('leads', 'table_name', $lead); |
|
20 | 20 | |
21 | - $this->assertAttributeEquals(true, 'new_schema', $lead); |
|
22 | - $this->assertAttributeEquals(true, 'importable', $lead); |
|
21 | + $this->assertAttributeEquals(true, 'new_schema', $lead); |
|
22 | + $this->assertAttributeEquals(true, 'importable', $lead); |
|
23 | 23 | |
24 | - } |
|
24 | + } |
|
25 | 25 | |
26 | - public function testget_account() |
|
27 | - { |
|
28 | - error_reporting(E_ERROR | E_PARSE); |
|
26 | + public function testget_account() |
|
27 | + { |
|
28 | + error_reporting(E_ERROR | E_PARSE); |
|
29 | 29 | |
30 | - $lead = new Lead(); |
|
30 | + $lead = new Lead(); |
|
31 | 31 | |
32 | - //test without pre settting attributes |
|
33 | - $result = $lead->get_account(); |
|
34 | - $this->assertEquals(null,$result); |
|
32 | + //test without pre settting attributes |
|
33 | + $result = $lead->get_account(); |
|
34 | + $this->assertEquals(null,$result); |
|
35 | 35 | |
36 | 36 | |
37 | - //test with required attributes preset |
|
38 | - $lead->account_id =1; |
|
39 | - $result = $lead->get_account(); |
|
40 | - $this->assertEquals(null,$result); |
|
37 | + //test with required attributes preset |
|
38 | + $lead->account_id =1; |
|
39 | + $result = $lead->get_account(); |
|
40 | + $this->assertEquals(null,$result); |
|
41 | 41 | |
42 | 42 | |
43 | - } |
|
43 | + } |
|
44 | 44 | |
45 | - public function testget_opportunity() |
|
46 | - { |
|
45 | + public function testget_opportunity() |
|
46 | + { |
|
47 | 47 | |
48 | - $lead = new Lead(); |
|
48 | + $lead = new Lead(); |
|
49 | 49 | |
50 | - //test without pre settting attributes |
|
51 | - $result = $lead->get_opportunity(); |
|
52 | - $this->assertEquals(null,$result); |
|
50 | + //test without pre settting attributes |
|
51 | + $result = $lead->get_opportunity(); |
|
52 | + $this->assertEquals(null,$result); |
|
53 | 53 | |
54 | 54 | |
55 | - //test with required attributes preset |
|
56 | - $lead->opportunity_id =1; |
|
57 | - $result = $lead->get_opportunity(); |
|
58 | - $this->assertEquals(null,$result); |
|
55 | + //test with required attributes preset |
|
56 | + $lead->opportunity_id =1; |
|
57 | + $result = $lead->get_opportunity(); |
|
58 | + $this->assertEquals(null,$result); |
|
59 | 59 | |
60 | - } |
|
60 | + } |
|
61 | 61 | |
62 | - public function testget_contact() |
|
63 | - { |
|
62 | + public function testget_contact() |
|
63 | + { |
|
64 | 64 | |
65 | - $lead = new Lead(); |
|
65 | + $lead = new Lead(); |
|
66 | 66 | |
67 | - //test without pre settting attributes |
|
68 | - $result = $lead->get_contact(); |
|
69 | - $this->assertEquals(null,$result); |
|
67 | + //test without pre settting attributes |
|
68 | + $result = $lead->get_contact(); |
|
69 | + $this->assertEquals(null,$result); |
|
70 | 70 | |
71 | 71 | |
72 | - //test with required attributes preset |
|
73 | - $lead->contact_id =1; |
|
74 | - $result = $lead->get_contact(); |
|
75 | - $this->assertEquals(null,$result); |
|
72 | + //test with required attributes preset |
|
73 | + $lead->contact_id =1; |
|
74 | + $result = $lead->get_contact(); |
|
75 | + $this->assertEquals(null,$result); |
|
76 | 76 | |
77 | 77 | |
78 | - } |
|
78 | + } |
|
79 | 79 | |
80 | - public function testcreate_list_query() |
|
81 | - { |
|
82 | - $lead = new Lead(); |
|
80 | + public function testcreate_list_query() |
|
81 | + { |
|
82 | + $lead = new Lead(); |
|
83 | 83 | |
84 | - //test with empty string params |
|
85 | - $expected = "SELECT leads.*, users.user_name assigned_user_name,leads_cstm.* FROM leads LEFT JOIN users\n ON leads.assigned_user_id=users.id LEFT JOIN email_addr_bean_rel eabl ON eabl.bean_id = leads.id AND eabl.bean_module = 'Leads' and eabl.primary_address = 1 and eabl.deleted=0 LEFT JOIN email_addresses ea ON (ea.id = eabl.email_address_id) LEFT JOIN leads_cstm ON leads.id = leads_cstm.id_c where leads.deleted=0 "; |
|
86 | - $actual = $lead->create_list_query('',''); |
|
87 | - $this->assertSame($expected,$actual); |
|
84 | + //test with empty string params |
|
85 | + $expected = "SELECT leads.*, users.user_name assigned_user_name,leads_cstm.* FROM leads LEFT JOIN users\n ON leads.assigned_user_id=users.id LEFT JOIN email_addr_bean_rel eabl ON eabl.bean_id = leads.id AND eabl.bean_module = 'Leads' and eabl.primary_address = 1 and eabl.deleted=0 LEFT JOIN email_addresses ea ON (ea.id = eabl.email_address_id) LEFT JOIN leads_cstm ON leads.id = leads_cstm.id_c where leads.deleted=0 "; |
|
86 | + $actual = $lead->create_list_query('',''); |
|
87 | + $this->assertSame($expected,$actual); |
|
88 | 88 | |
89 | 89 | |
90 | - //test with valid string params |
|
91 | - $expected = "SELECT leads.*, users.user_name assigned_user_name,leads_cstm.* FROM leads LEFT JOIN users\n ON leads.assigned_user_id=users.id LEFT JOIN email_addr_bean_rel eabl ON eabl.bean_id = leads.id AND eabl.bean_module = 'Leads' and eabl.primary_address = 1 and eabl.deleted=0 LEFT JOIN email_addresses ea ON (ea.id = eabl.email_address_id) LEFT JOIN leads_cstm ON leads.id = leads_cstm.id_c where (users.user_name=\"\") AND leads.deleted=0 ORDER BY leads.id"; |
|
92 | - $actual = $lead->create_list_query('leads.id','users.user_name=""'); |
|
93 | - $this->assertSame($expected,$actual); |
|
90 | + //test with valid string params |
|
91 | + $expected = "SELECT leads.*, users.user_name assigned_user_name,leads_cstm.* FROM leads LEFT JOIN users\n ON leads.assigned_user_id=users.id LEFT JOIN email_addr_bean_rel eabl ON eabl.bean_id = leads.id AND eabl.bean_module = 'Leads' and eabl.primary_address = 1 and eabl.deleted=0 LEFT JOIN email_addresses ea ON (ea.id = eabl.email_address_id) LEFT JOIN leads_cstm ON leads.id = leads_cstm.id_c where (users.user_name=\"\") AND leads.deleted=0 ORDER BY leads.id"; |
|
92 | + $actual = $lead->create_list_query('leads.id','users.user_name=""'); |
|
93 | + $this->assertSame($expected,$actual); |
|
94 | 94 | |
95 | - } |
|
95 | + } |
|
96 | 96 | |
97 | 97 | /** |
98 | 98 | * @todo: NEEDS FIXING! |
99 | 99 | */ |
100 | - public function testcreate_new_list_query() |
|
101 | - { |
|
100 | + public function testcreate_new_list_query() |
|
101 | + { |
|
102 | 102 | /* |
103 | 103 | $lead = new Lead(); |
104 | 104 | |
@@ -114,26 +114,26 @@ discard block |
||
114 | 114 | $this->assertSame($expected,$actual); |
115 | 115 | */ |
116 | 116 | $this->assertTrue(true, "NEEDS FIXING!"); |
117 | - } |
|
117 | + } |
|
118 | 118 | |
119 | 119 | public function testSaveAndConverted_lead() |
120 | 120 | { |
121 | - $lead = new Lead(); |
|
121 | + $lead = new Lead(); |
|
122 | 122 | |
123 | - $lead->first_name = "firstn"; |
|
124 | - $lead->last_name ="lastnn"; |
|
125 | - $lead->lead_source = "test"; |
|
123 | + $lead->first_name = "firstn"; |
|
124 | + $lead->last_name ="lastnn"; |
|
125 | + $lead->lead_source = "test"; |
|
126 | 126 | |
127 | - $result = $lead->save(); |
|
127 | + $result = $lead->save(); |
|
128 | 128 | |
129 | - //test for record ID to verify that record is saved |
|
130 | - $this->assertTrue(isset($lead->id)); |
|
131 | - $this->assertEquals(36, strlen($lead->id)); |
|
132 | - $this->assertEquals("New", $lead->status); |
|
129 | + //test for record ID to verify that record is saved |
|
130 | + $this->assertTrue(isset($lead->id)); |
|
131 | + $this->assertEquals(36, strlen($lead->id)); |
|
132 | + $this->assertEquals("New", $lead->status); |
|
133 | 133 | |
134 | 134 | |
135 | - //test converted_lead method after saving |
|
136 | - /*$lead->converted_lead("'" . $lead->id . "'" , "'1'", "'1'", "'1'"); |
|
135 | + //test converted_lead method after saving |
|
136 | + /*$lead->converted_lead("'" . $lead->id . "'" , "'1'", "'1'", "'1'"); |
|
137 | 137 | |
138 | 138 | //retrieve back to test if attributes are updated in db |
139 | 139 | $lead = $lead->retrieve($lead->id); |
@@ -144,206 +144,206 @@ discard block |
||
144 | 144 | $this->assertEquals("1", $lead->opportunity_id); |
145 | 145 | */ |
146 | 146 | |
147 | - $this->markTestSkipped("converted_lead: Error in query, id's not properly escaped "); |
|
147 | + $this->markTestSkipped("converted_lead: Error in query, id's not properly escaped "); |
|
148 | 148 | |
149 | 149 | |
150 | - //mark the record as deleted and verify that this record cannot be retrieved anymore. |
|
151 | - $lead->mark_deleted($lead->id); |
|
152 | - $result = $lead->retrieve($lead->id); |
|
153 | - $this->assertEquals(null,$result); |
|
150 | + //mark the record as deleted and verify that this record cannot be retrieved anymore. |
|
151 | + $lead->mark_deleted($lead->id); |
|
152 | + $result = $lead->retrieve($lead->id); |
|
153 | + $this->assertEquals(null,$result); |
|
154 | 154 | |
155 | 155 | } |
156 | 156 | |
157 | 157 | |
158 | 158 | |
159 | - public function testfill_in_additional_list_fields() |
|
160 | - { |
|
161 | - $lead = new Lead(); |
|
159 | + public function testfill_in_additional_list_fields() |
|
160 | + { |
|
161 | + $lead = new Lead(); |
|
162 | 162 | |
163 | - $lead->first_name = "firstn"; |
|
164 | - $lead->last_name ="lastn"; |
|
163 | + $lead->first_name = "firstn"; |
|
164 | + $lead->last_name ="lastn"; |
|
165 | 165 | |
166 | - $lead->fill_in_additional_list_fields(); |
|
166 | + $lead->fill_in_additional_list_fields(); |
|
167 | 167 | |
168 | - $this->assertEquals("firstn lastn", $lead->name ); |
|
168 | + $this->assertEquals("firstn lastn", $lead->name ); |
|
169 | 169 | |
170 | - } |
|
170 | + } |
|
171 | 171 | |
172 | 172 | |
173 | - public function testfill_in_additional_detail_fields() |
|
174 | - { |
|
173 | + public function testfill_in_additional_detail_fields() |
|
174 | + { |
|
175 | 175 | |
176 | - $lead = new Lead(); |
|
176 | + $lead = new Lead(); |
|
177 | 177 | |
178 | - $lead->first_name = "firstn"; |
|
179 | - $lead->last_name ="lastn"; |
|
178 | + $lead->first_name = "firstn"; |
|
179 | + $lead->last_name ="lastn"; |
|
180 | 180 | |
181 | - $lead->fill_in_additional_detail_fields(); |
|
181 | + $lead->fill_in_additional_detail_fields(); |
|
182 | 182 | |
183 | - $this->assertEquals("firstn lastn", $lead->name ); |
|
183 | + $this->assertEquals("firstn lastn", $lead->name ); |
|
184 | 184 | |
185 | 185 | |
186 | 186 | |
187 | - } |
|
187 | + } |
|
188 | 188 | |
189 | - public function testget_list_view_data(){ |
|
189 | + public function testget_list_view_data(){ |
|
190 | 190 | |
191 | - $lead = new Lead(); |
|
191 | + $lead = new Lead(); |
|
192 | 192 | |
193 | - $expected = array ( |
|
194 | - 'NAME' => ' ', |
|
195 | - 'DELETED' => 0, |
|
196 | - 'FULL_NAME' => ' ', |
|
197 | - 'DO_NOT_CALL' => '0', |
|
198 | - 'CONVERTED' => '0', |
|
199 | - 'ENCODED_NAME' => ' ', |
|
200 | - 'EMAIL1' => '', |
|
201 | - 'EMAIL1_LINK' => '<a href=\'javascript:void(0);\' onclick=\'SUGAR.quickCompose.init({"fullComposeUrl":"contact_id=\\u0026parent_type=Leads\\u0026parent_id=\\u0026parent_name=+\\u0026to_addrs_ids=\\u0026to_addrs_names=\\u0026to_addrs_emails=\\u0026to_email_addrs=+%26nbsp%3B%26lt%3B%26gt%3B\\u0026return_module=Leads\\u0026return_action=ListView\\u0026return_id=","composePackage":{"contact_id":"","parent_type":"Leads","parent_id":"","parent_name":" ","to_addrs_ids":"","to_addrs_names":"","to_addrs_emails":"","to_email_addrs":" \\u003C\\u003E","return_module":"Leads","return_action":"ListView","return_id":""}});\' class=\'\'>', |
|
202 | - 'ACC_NAME_FROM_ACCOUNTS' => NULL, |
|
203 | - ); |
|
193 | + $expected = array ( |
|
194 | + 'NAME' => ' ', |
|
195 | + 'DELETED' => 0, |
|
196 | + 'FULL_NAME' => ' ', |
|
197 | + 'DO_NOT_CALL' => '0', |
|
198 | + 'CONVERTED' => '0', |
|
199 | + 'ENCODED_NAME' => ' ', |
|
200 | + 'EMAIL1' => '', |
|
201 | + 'EMAIL1_LINK' => '<a href=\'javascript:void(0);\' onclick=\'SUGAR.quickCompose.init({"fullComposeUrl":"contact_id=\\u0026parent_type=Leads\\u0026parent_id=\\u0026parent_name=+\\u0026to_addrs_ids=\\u0026to_addrs_names=\\u0026to_addrs_emails=\\u0026to_email_addrs=+%26nbsp%3B%26lt%3B%26gt%3B\\u0026return_module=Leads\\u0026return_action=ListView\\u0026return_id=","composePackage":{"contact_id":"","parent_type":"Leads","parent_id":"","parent_name":" ","to_addrs_ids":"","to_addrs_names":"","to_addrs_emails":"","to_email_addrs":" \\u003C\\u003E","return_module":"Leads","return_action":"ListView","return_id":""}});\' class=\'\'>', |
|
202 | + 'ACC_NAME_FROM_ACCOUNTS' => NULL, |
|
203 | + ); |
|
204 | 204 | |
205 | - $actual = $lead->get_list_view_data(); |
|
205 | + $actual = $lead->get_list_view_data(); |
|
206 | 206 | |
207 | - //$this->assertSame($expected, $actual); |
|
208 | - $this->assertEquals($expected['NAME'], $actual['NAME']); |
|
209 | - $this->assertEquals($expected['DELETED'], $actual['DELETED']); |
|
210 | - $this->assertEquals($expected['FULL_NAME'], $actual['FULL_NAME']); |
|
211 | - $this->assertEquals($expected['DO_NOT_CALL'], $actual['DO_NOT_CALL']); |
|
212 | - $this->assertEquals($expected['EMAIL1_LINK'], $actual['EMAIL1_LINK']); |
|
207 | + //$this->assertSame($expected, $actual); |
|
208 | + $this->assertEquals($expected['NAME'], $actual['NAME']); |
|
209 | + $this->assertEquals($expected['DELETED'], $actual['DELETED']); |
|
210 | + $this->assertEquals($expected['FULL_NAME'], $actual['FULL_NAME']); |
|
211 | + $this->assertEquals($expected['DO_NOT_CALL'], $actual['DO_NOT_CALL']); |
|
212 | + $this->assertEquals($expected['EMAIL1_LINK'], $actual['EMAIL1_LINK']); |
|
213 | 213 | |
214 | - } |
|
214 | + } |
|
215 | 215 | |
216 | 216 | |
217 | 217 | public function testget_linked_fields() |
218 | 218 | { |
219 | - $lead = new Lead(); |
|
220 | - |
|
221 | - $expected = array ( |
|
222 | - 'created_by_link', |
|
223 | - 'modified_user_link', |
|
224 | - 'assigned_user_link', |
|
225 | - 'email_addresses_primary', |
|
226 | - 'email_addresses', |
|
227 | - 'reports_to_link', |
|
228 | - 'reportees', |
|
229 | - 'contacts', |
|
230 | - 'accounts', |
|
231 | - 'contact', |
|
232 | - 'opportunity', |
|
233 | - 'campaign_leads', |
|
234 | - 'tasks', |
|
235 | - 'notes', |
|
236 | - 'meetings', |
|
237 | - 'calls', |
|
238 | - 'emails', |
|
239 | - 'campaigns', |
|
240 | - 'prospect_lists', |
|
241 | - 'fp_events_leads_1', |
|
242 | - 'SecurityGroups', |
|
243 | - ); |
|
244 | - $actual = $lead->get_linked_fields(); |
|
245 | - $this->assertTrue(is_array($actual)); |
|
219 | + $lead = new Lead(); |
|
220 | + |
|
221 | + $expected = array ( |
|
222 | + 'created_by_link', |
|
223 | + 'modified_user_link', |
|
224 | + 'assigned_user_link', |
|
225 | + 'email_addresses_primary', |
|
226 | + 'email_addresses', |
|
227 | + 'reports_to_link', |
|
228 | + 'reportees', |
|
229 | + 'contacts', |
|
230 | + 'accounts', |
|
231 | + 'contact', |
|
232 | + 'opportunity', |
|
233 | + 'campaign_leads', |
|
234 | + 'tasks', |
|
235 | + 'notes', |
|
236 | + 'meetings', |
|
237 | + 'calls', |
|
238 | + 'emails', |
|
239 | + 'campaigns', |
|
240 | + 'prospect_lists', |
|
241 | + 'fp_events_leads_1', |
|
242 | + 'SecurityGroups', |
|
243 | + ); |
|
244 | + $actual = $lead->get_linked_fields(); |
|
245 | + $this->assertTrue(is_array($actual)); |
|
246 | 246 | sort($expected); |
247 | 247 | $actualKeys = array_keys($actual); |
248 | 248 | sort($actualKeys); |
249 | - $this->assertSame($expected ,$actualKeys); |
|
249 | + $this->assertSame($expected ,$actualKeys); |
|
250 | 250 | |
251 | 251 | } |
252 | 252 | |
253 | - public function testbuild_generic_where_clause () |
|
254 | - { |
|
253 | + public function testbuild_generic_where_clause () |
|
254 | + { |
|
255 | 255 | |
256 | - $lead = new Lead(); |
|
256 | + $lead = new Lead(); |
|
257 | 257 | |
258 | - //test with empty string params |
|
259 | - $expected = "leads.last_name like '%' or leads.account_name like '%' or leads.first_name like '%' or ea.email_address like '%'"; |
|
260 | - $actual = $lead->build_generic_where_clause(""); |
|
261 | - $this->assertSame($expected,$actual); |
|
258 | + //test with empty string params |
|
259 | + $expected = "leads.last_name like '%' or leads.account_name like '%' or leads.first_name like '%' or ea.email_address like '%'"; |
|
260 | + $actual = $lead->build_generic_where_clause(""); |
|
261 | + $this->assertSame($expected,$actual); |
|
262 | 262 | |
263 | 263 | |
264 | - //test with valid string params |
|
265 | - $expected = "leads.last_name like '%' or leads.account_name like '%' or leads.first_name like '%' or ea.email_address like '%'"; |
|
266 | - $actual = $lead->build_generic_where_clause("123"); |
|
267 | - $this->assertSame($expected,$actual); |
|
264 | + //test with valid string params |
|
265 | + $expected = "leads.last_name like '%' or leads.account_name like '%' or leads.first_name like '%' or ea.email_address like '%'"; |
|
266 | + $actual = $lead->build_generic_where_clause("123"); |
|
267 | + $this->assertSame($expected,$actual); |
|
268 | 268 | |
269 | - } |
|
269 | + } |
|
270 | 270 | |
271 | - public function testset_notification_body() |
|
272 | - { |
|
271 | + public function testset_notification_body() |
|
272 | + { |
|
273 | 273 | |
274 | - $lead = new Lead(); |
|
274 | + $lead = new Lead(); |
|
275 | 275 | |
276 | - //test with attributes preset and verify template variables are set accordingly |
|
276 | + //test with attributes preset and verify template variables are set accordingly |
|
277 | 277 | |
278 | - $lead->first_name = "firstn"; |
|
279 | - $lead->last_name ="lastn"; |
|
280 | - $lead->salutation = "Mr"; |
|
281 | - $lead->lead_source = "Email"; |
|
282 | - $lead->status = "New"; |
|
283 | - $lead->description = "tes description"; |
|
278 | + $lead->first_name = "firstn"; |
|
279 | + $lead->last_name ="lastn"; |
|
280 | + $lead->salutation = "Mr"; |
|
281 | + $lead->lead_source = "Email"; |
|
282 | + $lead->status = "New"; |
|
283 | + $lead->description = "tes description"; |
|
284 | 284 | |
285 | - $result = $lead->set_notification_body(new Sugar_Smarty(), $lead); |
|
285 | + $result = $lead->set_notification_body(new Sugar_Smarty(), $lead); |
|
286 | 286 | |
287 | - $this->assertEquals("Mr firstn lastn" ,$result->_tpl_vars['LEAD_NAME']); |
|
288 | - $this->assertEquals($lead->lead_source ,$result->_tpl_vars['LEAD_SOURCE']); |
|
289 | - $this->assertEquals($lead->status ,$result->_tpl_vars['LEAD_STATUS']); |
|
290 | - $this->assertEquals($lead->description ,$result->_tpl_vars['LEAD_DESCRIPTION']); |
|
287 | + $this->assertEquals("Mr firstn lastn" ,$result->_tpl_vars['LEAD_NAME']); |
|
288 | + $this->assertEquals($lead->lead_source ,$result->_tpl_vars['LEAD_SOURCE']); |
|
289 | + $this->assertEquals($lead->status ,$result->_tpl_vars['LEAD_STATUS']); |
|
290 | + $this->assertEquals($lead->description ,$result->_tpl_vars['LEAD_DESCRIPTION']); |
|
291 | 291 | |
292 | - } |
|
292 | + } |
|
293 | 293 | |
294 | - public function testbean_implements() |
|
295 | - { |
|
296 | - $lead = new Lead(); |
|
294 | + public function testbean_implements() |
|
295 | + { |
|
296 | + $lead = new Lead(); |
|
297 | 297 | |
298 | - $this->assertEquals(false, $lead->bean_implements('')); //test with blank value |
|
299 | - $this->assertEquals(false, $lead->bean_implements('test')); //test with invalid value |
|
300 | - $this->assertEquals(true, $lead->bean_implements('ACL')); //test with valid value |
|
298 | + $this->assertEquals(false, $lead->bean_implements('')); //test with blank value |
|
299 | + $this->assertEquals(false, $lead->bean_implements('test')); //test with invalid value |
|
300 | + $this->assertEquals(true, $lead->bean_implements('ACL')); //test with valid value |
|
301 | 301 | |
302 | - } |
|
302 | + } |
|
303 | 303 | |
304 | - public function testlistviewACLHelper() |
|
305 | - { |
|
306 | - $lead = new Lead(); |
|
304 | + public function testlistviewACLHelper() |
|
305 | + { |
|
306 | + $lead = new Lead(); |
|
307 | 307 | |
308 | - $expected = array("MAIN"=>"a", "ACCOUNT"=>"a", "OPPORTUNITY"=>"a", "CONTACT"=>"a" ); |
|
309 | - $actual = $lead->listviewACLHelper(); |
|
310 | - $this->assertSame($expected,$actual); |
|
308 | + $expected = array("MAIN"=>"a", "ACCOUNT"=>"a", "OPPORTUNITY"=>"a", "CONTACT"=>"a" ); |
|
309 | + $actual = $lead->listviewACLHelper(); |
|
310 | + $this->assertSame($expected,$actual); |
|
311 | 311 | |
312 | - } |
|
312 | + } |
|
313 | 313 | |
314 | 314 | |
315 | - public function testconvertCustomFieldsForm() |
|
316 | - { |
|
317 | - $lead = new Lead(); |
|
315 | + public function testconvertCustomFieldsForm() |
|
316 | + { |
|
317 | + $lead = new Lead(); |
|
318 | 318 | |
319 | - $form = ""; |
|
320 | - $prefix = ""; |
|
321 | - $tempBean = new Contact(); |
|
319 | + $form = ""; |
|
320 | + $prefix = ""; |
|
321 | + $tempBean = new Contact(); |
|
322 | 322 | |
323 | - $result = $lead->convertCustomFieldsForm($form, $tempBean, $prefix); |
|
323 | + $result = $lead->convertCustomFieldsForm($form, $tempBean, $prefix); |
|
324 | 324 | |
325 | - $this->assertEquals(true,$result); |
|
326 | - $this->assertgreaterThanOrEqual("", $form); //no filed with source = custom_fields |
|
325 | + $this->assertEquals(true,$result); |
|
326 | + $this->assertgreaterThanOrEqual("", $form); //no filed with source = custom_fields |
|
327 | 327 | |
328 | - } |
|
328 | + } |
|
329 | 329 | |
330 | 330 | |
331 | - public function testget_unlinked_email_query() |
|
332 | - { |
|
333 | - $lead = new Lead(); |
|
331 | + public function testget_unlinked_email_query() |
|
332 | + { |
|
333 | + $lead = new Lead(); |
|
334 | 334 | |
335 | - $expected = "SELECT emails.id FROM emails JOIN (select DISTINCT email_id from emails_email_addr_rel eear\n\n join email_addr_bean_rel eabr on eabr.bean_id ='' and eabr.bean_module = 'Leads' and\n eabr.email_address_id = eear.email_address_id and eabr.deleted=0\n where eear.deleted=0 and eear.email_id not in\n (select eb.email_id from emails_beans eb where eb.bean_module ='Leads' and eb.bean_id = '')\n ) derivedemails on derivedemails.email_id = emails.id"; |
|
336 | - $actual = $lead->get_unlinked_email_query(); |
|
337 | - $this->assertSame($expected,$actual); |
|
335 | + $expected = "SELECT emails.id FROM emails JOIN (select DISTINCT email_id from emails_email_addr_rel eear\n\n join email_addr_bean_rel eabr on eabr.bean_id ='' and eabr.bean_module = 'Leads' and\n eabr.email_address_id = eear.email_address_id and eabr.deleted=0\n where eear.deleted=0 and eear.email_id not in\n (select eb.email_id from emails_beans eb where eb.bean_module ='Leads' and eb.bean_id = '')\n ) derivedemails on derivedemails.email_id = emails.id"; |
|
336 | + $actual = $lead->get_unlinked_email_query(); |
|
337 | + $this->assertSame($expected,$actual); |
|
338 | 338 | |
339 | - } |
|
339 | + } |
|
340 | 340 | |
341 | 341 | |
342 | 342 | public function testget_old_related_calls() |
343 | 343 | { |
344 | - $lead = new Lead(); |
|
344 | + $lead = new Lead(); |
|
345 | 345 | |
346 | - $expected = array(); |
|
346 | + $expected = array(); |
|
347 | 347 | $expected['select']='SELECT calls.id '; |
348 | 348 | $expected['from']='FROM calls '; |
349 | 349 | $expected['where']=" WHERE calls.parent_id = '$this->id' |
@@ -351,28 +351,28 @@ discard block |
||
351 | 351 | $expected['join'] = ""; |
352 | 352 | $expected['join_tables'][0] = ''; |
353 | 353 | |
354 | - $actual = $lead->get_old_related_calls(); |
|
355 | - $this->assertSame($expected,$actual); |
|
354 | + $actual = $lead->get_old_related_calls(); |
|
355 | + $this->assertSame($expected,$actual); |
|
356 | 356 | |
357 | 357 | } |
358 | 358 | |
359 | 359 | |
360 | 360 | public function testgetActivitiesOptions() |
361 | - { |
|
362 | - $lead = new Lead(); |
|
361 | + { |
|
362 | + $lead = new Lead(); |
|
363 | 363 | |
364 | - $expected = array( "copy"=>"Copy", "move"=>"Move", "donothing"=>"Do Nothing"); |
|
365 | - $actual = $lead->getActivitiesOptions(); |
|
366 | - $this->assertSame($expected,$actual); |
|
364 | + $expected = array( "copy"=>"Copy", "move"=>"Move", "donothing"=>"Do Nothing"); |
|
365 | + $actual = $lead->getActivitiesOptions(); |
|
366 | + $this->assertSame($expected,$actual); |
|
367 | 367 | |
368 | 368 | } |
369 | 369 | |
370 | 370 | |
371 | 371 | public function testget_old_related_meetings() |
372 | 372 | { |
373 | - $lead = new Lead(); |
|
373 | + $lead = new Lead(); |
|
374 | 374 | |
375 | - $expected = array(); |
|
375 | + $expected = array(); |
|
376 | 376 | $expected['select']='SELECT meetings.id '; |
377 | 377 | $expected['from']='FROM meetings '; |
378 | 378 | $expected['where']=" WHERE meetings.parent_id = '' |
@@ -380,8 +380,8 @@ discard block |
||
380 | 380 | $expected['join'] = ""; |
381 | 381 | $expected['join_tables'][0] = ''; |
382 | 382 | |
383 | - $actual = $lead->get_old_related_meetings(); |
|
384 | - $this->assertSame($expected,$actual); |
|
383 | + $actual = $lead->get_old_related_meetings(); |
|
384 | + $this->assertSame($expected,$actual); |
|
385 | 385 | |
386 | 386 | } |
387 | 387 |
@@ -3,132 +3,132 @@ discard block |
||
3 | 3 | class ProjectTest extends PHPUnit_Framework_TestCase { |
4 | 4 | |
5 | 5 | |
6 | - public function testProject() |
|
7 | - { |
|
8 | - //execute the contructor and check for the Object type and attributes |
|
9 | - $project = new Project(); |
|
6 | + public function testProject() |
|
7 | + { |
|
8 | + //execute the contructor and check for the Object type and attributes |
|
9 | + $project = new Project(); |
|
10 | 10 | |
11 | - $this->assertInstanceOf('Project',$project); |
|
12 | - $this->assertInstanceOf('SugarBean',$project); |
|
11 | + $this->assertInstanceOf('Project',$project); |
|
12 | + $this->assertInstanceOf('SugarBean',$project); |
|
13 | 13 | |
14 | 14 | |
15 | - $this->assertAttributeEquals('project', 'table_name', $project); |
|
16 | - $this->assertAttributeEquals('Project', 'module_dir', $project); |
|
17 | - $this->assertAttributeEquals('Project', 'object_name', $project); |
|
15 | + $this->assertAttributeEquals('project', 'table_name', $project); |
|
16 | + $this->assertAttributeEquals('Project', 'module_dir', $project); |
|
17 | + $this->assertAttributeEquals('Project', 'object_name', $project); |
|
18 | 18 | |
19 | - $this->assertAttributeEquals(true, 'new_schema', $project); |
|
19 | + $this->assertAttributeEquals(true, 'new_schema', $project); |
|
20 | 20 | |
21 | - } |
|
21 | + } |
|
22 | 22 | |
23 | - public function testfill_in_additional_detail_fields() |
|
24 | - { |
|
25 | - error_reporting(E_ERROR | E_PARSE); |
|
23 | + public function testfill_in_additional_detail_fields() |
|
24 | + { |
|
25 | + error_reporting(E_ERROR | E_PARSE); |
|
26 | 26 | |
27 | - $project = new Project(); |
|
27 | + $project = new Project(); |
|
28 | 28 | |
29 | - //test without setting assigned_user_id |
|
30 | - $project->fill_in_additional_detail_fields(); |
|
31 | - $this->assertEquals("", $project->assigned_user_name); |
|
29 | + //test without setting assigned_user_id |
|
30 | + $project->fill_in_additional_detail_fields(); |
|
31 | + $this->assertEquals("", $project->assigned_user_name); |
|
32 | 32 | |
33 | 33 | |
34 | - //test with assigned_user_id set |
|
35 | - $project->assigned_user_id = 1; |
|
36 | - $project->fill_in_additional_detail_fields(); |
|
37 | - $this->assertEquals("Administrator", $project->assigned_user_name); |
|
34 | + //test with assigned_user_id set |
|
35 | + $project->assigned_user_id = 1; |
|
36 | + $project->fill_in_additional_detail_fields(); |
|
37 | + $this->assertEquals("Administrator", $project->assigned_user_name); |
|
38 | 38 | |
39 | - } |
|
39 | + } |
|
40 | 40 | |
41 | 41 | |
42 | - public function testfill_in_additional_list_fields() |
|
43 | - { |
|
44 | - $project = new Project(); |
|
42 | + public function testfill_in_additional_list_fields() |
|
43 | + { |
|
44 | + $project = new Project(); |
|
45 | 45 | |
46 | - //test without setting assigned_user_id |
|
47 | - $project->fill_in_additional_list_fields(); |
|
48 | - $this->assertEquals("", $project->assigned_user_name); |
|
46 | + //test without setting assigned_user_id |
|
47 | + $project->fill_in_additional_list_fields(); |
|
48 | + $this->assertEquals("", $project->assigned_user_name); |
|
49 | 49 | |
50 | 50 | |
51 | - //test with assigned_user_id set |
|
52 | - $project->assigned_user_id = 1; |
|
53 | - $project->fill_in_additional_list_fields(); |
|
54 | - $this->assertEquals("Administrator", $project->assigned_user_name); |
|
51 | + //test with assigned_user_id set |
|
52 | + $project->assigned_user_id = 1; |
|
53 | + $project->fill_in_additional_list_fields(); |
|
54 | + $this->assertEquals("Administrator", $project->assigned_user_name); |
|
55 | 55 | |
56 | - } |
|
56 | + } |
|
57 | 57 | |
58 | 58 | |
59 | 59 | public function testsave_relationship_changes() |
60 | 60 | { |
61 | 61 | |
62 | - $project = new Project(); |
|
62 | + $project = new Project(); |
|
63 | 63 | |
64 | - $project->id =1; |
|
65 | - $_REQUEST['relate_id'] = 2; |
|
66 | - $_REQUEST['relate_to'] = "contacts"; |
|
64 | + $project->id =1; |
|
65 | + $_REQUEST['relate_id'] = 2; |
|
66 | + $_REQUEST['relate_to'] = "contacts"; |
|
67 | 67 | |
68 | - //execute the method and test if it works and does not throws an exception. |
|
69 | - try { |
|
70 | - $project->save_relationship_changes(true); |
|
71 | - $this->assertTrue(true); |
|
72 | - } |
|
73 | - catch (Exception $e) { |
|
74 | - $this->fail(); |
|
75 | - } |
|
68 | + //execute the method and test if it works and does not throws an exception. |
|
69 | + try { |
|
70 | + $project->save_relationship_changes(true); |
|
71 | + $this->assertTrue(true); |
|
72 | + } |
|
73 | + catch (Exception $e) { |
|
74 | + $this->fail(); |
|
75 | + } |
|
76 | 76 | |
77 | 77 | } |
78 | 78 | |
79 | 79 | |
80 | - public function test_get_total_estimated_effort() |
|
81 | - { |
|
82 | - //$project = new Project(); |
|
83 | - //$result = $project->_get_total_estimated_effort("1"); |
|
84 | - $this->markTestIncomplete('Can Not be implemented: Unknown column parent_id in where clause \n Argument 3 passed to MysqlManager::convert() must be of the type array, integer given'); |
|
80 | + public function test_get_total_estimated_effort() |
|
81 | + { |
|
82 | + //$project = new Project(); |
|
83 | + //$result = $project->_get_total_estimated_effort("1"); |
|
84 | + $this->markTestIncomplete('Can Not be implemented: Unknown column parent_id in where clause \n Argument 3 passed to MysqlManager::convert() must be of the type array, integer given'); |
|
85 | 85 | |
86 | - } |
|
86 | + } |
|
87 | 87 | |
88 | 88 | |
89 | - public function test_get_total_actual_effort() |
|
90 | - { |
|
91 | - $this->markTestIncomplete('Can Not be implemented: Unknown column parent_id in where clause \n Argument 3 passed to MysqlManager::convert() must be of the type array, integer given'); |
|
92 | - } |
|
89 | + public function test_get_total_actual_effort() |
|
90 | + { |
|
91 | + $this->markTestIncomplete('Can Not be implemented: Unknown column parent_id in where clause \n Argument 3 passed to MysqlManager::convert() must be of the type array, integer given'); |
|
92 | + } |
|
93 | 93 | |
94 | 94 | |
95 | - public function testget_summary_text() |
|
96 | - { |
|
97 | - $project = new Project(); |
|
95 | + public function testget_summary_text() |
|
96 | + { |
|
97 | + $project = new Project(); |
|
98 | 98 | |
99 | - //test without setting name |
|
100 | - $this->assertEquals(Null,$project->get_summary_text()); |
|
99 | + //test without setting name |
|
100 | + $this->assertEquals(Null,$project->get_summary_text()); |
|
101 | 101 | |
102 | - //test with name set |
|
103 | - $project->name = "test"; |
|
104 | - $this->assertEquals('test',$project->get_summary_text()); |
|
102 | + //test with name set |
|
103 | + $project->name = "test"; |
|
104 | + $this->assertEquals('test',$project->get_summary_text()); |
|
105 | 105 | |
106 | - } |
|
106 | + } |
|
107 | 107 | |
108 | 108 | |
109 | - public function testbuild_generic_where_clause () |
|
110 | - { |
|
109 | + public function testbuild_generic_where_clause () |
|
110 | + { |
|
111 | 111 | |
112 | - $project = new Project(); |
|
112 | + $project = new Project(); |
|
113 | 113 | |
114 | - //test with empty string params |
|
115 | - $expected = "project.name LIKE '%%'"; |
|
116 | - $actual = $project->build_generic_where_clause(''); |
|
117 | - $this->assertSame($expected,$actual); |
|
114 | + //test with empty string params |
|
115 | + $expected = "project.name LIKE '%%'"; |
|
116 | + $actual = $project->build_generic_where_clause(''); |
|
117 | + $this->assertSame($expected,$actual); |
|
118 | 118 | |
119 | 119 | |
120 | - //test with valid string params |
|
121 | - $expected = "project.name LIKE '%%'"; |
|
122 | - $actual = $project->build_generic_where_clause('test'); |
|
123 | - $this->assertSame($expected,$actual); |
|
120 | + //test with valid string params |
|
121 | + $expected = "project.name LIKE '%%'"; |
|
122 | + $actual = $project->build_generic_where_clause('test'); |
|
123 | + $this->assertSame($expected,$actual); |
|
124 | 124 | |
125 | - } |
|
125 | + } |
|
126 | 126 | |
127 | 127 | /** |
128 | 128 | * @todo: NEEDS FIXING! |
129 | 129 | */ |
130 | - public function testget_list_view_data() |
|
131 | - { |
|
130 | + public function testget_list_view_data() |
|
131 | + { |
|
132 | 132 | /* |
133 | 133 | $project = new Project(); |
134 | 134 | |
@@ -146,44 +146,44 @@ discard block |
||
146 | 146 | $this->assertSame($expected, $actual); |
147 | 147 | */ |
148 | 148 | $this->assertTrue(true, "NEEDS FIXING!"); |
149 | - } |
|
149 | + } |
|
150 | 150 | |
151 | - public function testbean_implements(){ |
|
151 | + public function testbean_implements(){ |
|
152 | 152 | |
153 | - $project = new Project(); |
|
153 | + $project = new Project(); |
|
154 | 154 | |
155 | - $this->assertEquals(false, $project->bean_implements('')); //test with blank value |
|
156 | - $this->assertEquals(false, $project->bean_implements('test')); //test with invalid value |
|
157 | - $this->assertEquals(true, $project->bean_implements('ACL')); //test with valid value |
|
155 | + $this->assertEquals(false, $project->bean_implements('')); //test with blank value |
|
156 | + $this->assertEquals(false, $project->bean_implements('test')); //test with invalid value |
|
157 | + $this->assertEquals(true, $project->bean_implements('ACL')); //test with valid value |
|
158 | 158 | |
159 | - } |
|
159 | + } |
|
160 | 160 | |
161 | 161 | public function testcreate_export_query() |
162 | 162 | { |
163 | - $project = new Project(); |
|
163 | + $project = new Project(); |
|
164 | 164 | |
165 | - //test with empty string params |
|
166 | - $expected = "SELECT\n project.*,\n users.user_name as assigned_user_name ,project_cstm.jjwg_maps_lng_c,project_cstm.jjwg_maps_lat_c,project_cstm.jjwg_maps_geocode_status_c,project_cstm.jjwg_maps_address_c FROM project LEFT JOIN project_cstm ON project.id = project_cstm.id_c LEFT JOIN users\n ON project.assigned_user_id=users.id where project.deleted=0 "; |
|
167 | - $actual = $project->create_export_query('',''); |
|
168 | - $this->assertSame($expected,$actual); |
|
165 | + //test with empty string params |
|
166 | + $expected = "SELECT\n project.*,\n users.user_name as assigned_user_name ,project_cstm.jjwg_maps_lng_c,project_cstm.jjwg_maps_lat_c,project_cstm.jjwg_maps_geocode_status_c,project_cstm.jjwg_maps_address_c FROM project LEFT JOIN project_cstm ON project.id = project_cstm.id_c LEFT JOIN users\n ON project.assigned_user_id=users.id where project.deleted=0 "; |
|
167 | + $actual = $project->create_export_query('',''); |
|
168 | + $this->assertSame($expected,$actual); |
|
169 | 169 | |
170 | 170 | |
171 | - //test with valid string params |
|
172 | - $expected = "SELECT\n project.*,\n users.user_name as assigned_user_name ,project_cstm.jjwg_maps_lng_c,project_cstm.jjwg_maps_lat_c,project_cstm.jjwg_maps_geocode_status_c,project_cstm.jjwg_maps_address_c FROM project LEFT JOIN project_cstm ON project.id = project_cstm.id_c LEFT JOIN users\n ON project.assigned_user_id=users.id where (users.user_name) AND project.deleted=0 ORDER BY project.id"; |
|
173 | - $actual = $project->create_export_query('project.id','users.user_name'); |
|
174 | - $this->assertSame($expected,$actual); |
|
171 | + //test with valid string params |
|
172 | + $expected = "SELECT\n project.*,\n users.user_name as assigned_user_name ,project_cstm.jjwg_maps_lng_c,project_cstm.jjwg_maps_lat_c,project_cstm.jjwg_maps_geocode_status_c,project_cstm.jjwg_maps_address_c FROM project LEFT JOIN project_cstm ON project.id = project_cstm.id_c LEFT JOIN users\n ON project.assigned_user_id=users.id where (users.user_name) AND project.deleted=0 ORDER BY project.id"; |
|
173 | + $actual = $project->create_export_query('project.id','users.user_name'); |
|
174 | + $this->assertSame($expected,$actual); |
|
175 | 175 | |
176 | 176 | } |
177 | 177 | |
178 | - public function testgetAllProjectTasks(){ |
|
178 | + public function testgetAllProjectTasks(){ |
|
179 | 179 | |
180 | - $project = new Project(); |
|
180 | + $project = new Project(); |
|
181 | 181 | |
182 | - $project->id = 1; |
|
183 | - $result = $project->getAllProjectTasks(); |
|
184 | - $this->assertTrue(is_array($result)); |
|
182 | + $project->id = 1; |
|
183 | + $result = $project->getAllProjectTasks(); |
|
184 | + $this->assertTrue(is_array($result)); |
|
185 | 185 | |
186 | - } |
|
186 | + } |
|
187 | 187 | |
188 | 188 | } |
189 | 189 | ?> |
@@ -40,42 +40,42 @@ |
||
40 | 40 | global $current_user; |
41 | 41 | $module_name = "Tasks"; |
42 | 42 | $searchFields['Tasks'] = |
43 | - array ( |
|
44 | - 'name' => array( 'query_type'=>'default'), |
|
43 | + array ( |
|
44 | + 'name' => array( 'query_type'=>'default'), |
|
45 | 45 | 'contact_name' => array('query_type' => 'default', 'db_field' => array('contacts.first_name', 'contacts.last_name'), 'force_unifiedsearch' => true), |
46 | 46 | 'current_user_only'=> array('query_type'=>'default','db_field'=>array('assigned_user_id'),'my_items'=>true, 'vname' => 'LBL_CURRENT_USER_FILTER', 'type' => 'bool'), |
47 | 47 | 'assigned_user_id'=> array('query_type'=>'default'), |
48 | 48 | 'status'=> array('query_type'=>'default', 'options' => 'task_status_dom', 'template_var' => 'STATUS_FILTER'), |
49 | 49 | |
50 | - 'open_only' => array( |
|
51 | - 'query_type'=>'default', |
|
52 | - 'db_field'=>array('status'), |
|
53 | - 'operator'=>'not in', |
|
54 | - 'closed_values' => array('Completed', 'Deferred'), |
|
55 | - 'type'=>'bool', |
|
56 | - ), |
|
50 | + 'open_only' => array( |
|
51 | + 'query_type'=>'default', |
|
52 | + 'db_field'=>array('status'), |
|
53 | + 'operator'=>'not in', |
|
54 | + 'closed_values' => array('Completed', 'Deferred'), |
|
55 | + 'type'=>'bool', |
|
56 | + ), |
|
57 | 57 | 'favorites_only' => array( |
58 | 58 | 'query_type'=>'format', |
59 | 59 | 'operator' => 'subquery', |
60 | - 'subquery' => "SELECT favorites.parent_id FROM favorites |
|
60 | + 'subquery' => "SELECT favorites.parent_id FROM favorites |
|
61 | 61 | WHERE favorites.deleted = 0 |
62 | 62 | and favorites.parent_type = '".$module_name."' |
63 | 63 | and favorites.assigned_user_id = '" .$current_user->id . "') OR NOT ({0}", |
64 | 64 | 'db_field'=>array('id')), |
65 | - //Range Search Support |
|
66 | - 'range_date_entered' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
67 | - 'start_range_date_entered' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
68 | - 'end_range_date_entered' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
69 | - 'range_date_modified' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
70 | - 'start_range_date_modified' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
71 | - 'end_range_date_modified' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
65 | + //Range Search Support |
|
66 | + 'range_date_entered' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
67 | + 'start_range_date_entered' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
68 | + 'end_range_date_entered' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
69 | + 'range_date_modified' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
70 | + 'start_range_date_modified' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
71 | + 'end_range_date_modified' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
72 | 72 | |
73 | - 'range_date_start' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
74 | - 'start_range_date_start' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
75 | - 'end_range_date_start' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
76 | - 'range_date_due' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
77 | - 'start_range_date_due' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
78 | - 'end_range_date_due' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
79 | - //Range Search Support |
|
80 | - ); |
|
73 | + 'range_date_start' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
74 | + 'start_range_date_start' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
75 | + 'end_range_date_start' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
76 | + 'range_date_due' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
77 | + 'start_range_date_due' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
78 | + 'end_range_date_due' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
79 | + //Range Search Support |
|
80 | + ); |
|
81 | 81 | ?> |
@@ -54,24 +54,24 @@ discard block |
||
54 | 54 | |
55 | 55 | } |
56 | 56 | |
57 | - function getFormBody($prefix, $mod='',$formname='', $size='30') { |
|
57 | + function getFormBody($prefix, $mod='',$formname='', $size='30') { |
|
58 | 58 | |
59 | 59 | |
60 | - global $mod_strings; |
|
60 | + global $mod_strings; |
|
61 | 61 | |
62 | - $temp_strings = $mod_strings; |
|
62 | + $temp_strings = $mod_strings; |
|
63 | 63 | |
64 | - if(!empty($mod)) { |
|
65 | - global $current_language; |
|
66 | - $mod_strings = return_module_language($current_language, $mod); |
|
67 | - } |
|
68 | - global $app_strings; |
|
69 | - global $app_list_strings; |
|
64 | + if(!empty($mod)) { |
|
65 | + global $current_language; |
|
66 | + $mod_strings = return_module_language($current_language, $mod); |
|
67 | + } |
|
68 | + global $app_strings; |
|
69 | + global $app_list_strings; |
|
70 | 70 | |
71 | - $lbl_required_symbol = $app_strings['LBL_REQUIRED_SYMBOL']; |
|
72 | - $lbl_subject = $mod_strings['LBL_NOTE_SUBJECT']; |
|
73 | - $lbl_description = $mod_strings['LBL_NOTE']; |
|
74 | - $default_parent_type= $app_list_strings['record_type_default_key']; |
|
71 | + $lbl_required_symbol = $app_strings['LBL_REQUIRED_SYMBOL']; |
|
72 | + $lbl_subject = $mod_strings['LBL_NOTE_SUBJECT']; |
|
73 | + $lbl_description = $mod_strings['LBL_NOTE']; |
|
74 | + $default_parent_type= $app_list_strings['record_type_default_key']; |
|
75 | 75 | |
76 | 76 | $form = <<<EOF |
77 | 77 | <input type="hidden" name="${prefix}record" value=""> |
@@ -93,325 +93,325 @@ discard block |
||
93 | 93 | </table></p> |
94 | 94 | EOF; |
95 | 95 | |
96 | - $javascript = new javascript(); |
|
97 | - $javascript->setFormName($formname); |
|
98 | - $javascript->setSugarBean(new EmailTemplate()); |
|
99 | - $javascript->addRequiredFields($prefix); |
|
100 | - $form .=$javascript->getScript(); |
|
101 | - $mod_strings = $temp_strings; |
|
102 | - return $form; |
|
103 | - } |
|
104 | - |
|
105 | - function getForm($prefix, $mod='') { |
|
106 | - if(!empty($mod)) { |
|
107 | - global $current_language; |
|
108 | - $mod_strings = return_module_language($current_language, $mod); |
|
109 | - }else global $mod_strings; |
|
110 | - global $app_strings; |
|
111 | - global $app_list_strings; |
|
112 | - |
|
113 | - $lbl_save_button_title = $app_strings['LBL_SAVE_BUTTON_TITLE']; |
|
114 | - $lbl_save_button_key = $app_strings['LBL_SAVE_BUTTON_KEY']; |
|
115 | - $lbl_save_button_label = $app_strings['LBL_SAVE_BUTTON_LABEL']; |
|
116 | - |
|
117 | - |
|
118 | - $the_form = get_left_form_header($mod_strings['LBL_NEW_FORM_TITLE']); |
|
96 | + $javascript = new javascript(); |
|
97 | + $javascript->setFormName($formname); |
|
98 | + $javascript->setSugarBean(new EmailTemplate()); |
|
99 | + $javascript->addRequiredFields($prefix); |
|
100 | + $form .=$javascript->getScript(); |
|
101 | + $mod_strings = $temp_strings; |
|
102 | + return $form; |
|
103 | + } |
|
104 | + |
|
105 | + function getForm($prefix, $mod='') { |
|
106 | + if(!empty($mod)) { |
|
107 | + global $current_language; |
|
108 | + $mod_strings = return_module_language($current_language, $mod); |
|
109 | + }else global $mod_strings; |
|
110 | + global $app_strings; |
|
111 | + global $app_list_strings; |
|
112 | + |
|
113 | + $lbl_save_button_title = $app_strings['LBL_SAVE_BUTTON_TITLE']; |
|
114 | + $lbl_save_button_key = $app_strings['LBL_SAVE_BUTTON_KEY']; |
|
115 | + $lbl_save_button_label = $app_strings['LBL_SAVE_BUTTON_LABEL']; |
|
116 | + |
|
117 | + |
|
118 | + $the_form = get_left_form_header($mod_strings['LBL_NEW_FORM_TITLE']); |
|
119 | 119 | $the_form .= <<<EOQ |
120 | 120 | |
121 | 121 | <form name="${prefix}EmailTemplateSave" onSubmit="return check_form('${prefix}EmailTemplateSave')" method="POST" action="index.php"> |
122 | 122 | <input type="hidden" name="${prefix}module" value="EmailTemplates"> |
123 | 123 | <input type="hidden" name="${prefix}action" value="Save"> |
124 | 124 | EOQ; |
125 | - $the_form .= $this->getFormBody($prefix, $mod, "${prefix}EmailTemplateSave", "20"); |
|
125 | + $the_form .= $this->getFormBody($prefix, $mod, "${prefix}EmailTemplateSave", "20"); |
|
126 | 126 | $the_form .= <<<EOQ |
127 | 127 | <p><input title="$lbl_save_button_title" accessKey="$lbl_save_button_key" class="button" type="submit" name="button" value=" $lbl_save_button_label " ></p> |
128 | 128 | </form> |
129 | 129 | |
130 | 130 | EOQ; |
131 | 131 | |
132 | - $the_form .= get_left_form_footer(); |
|
133 | - $the_form .= get_validate_record_js(); |
|
134 | - |
|
135 | - |
|
136 | - return $the_form; |
|
137 | - } |
|
138 | - |
|
139 | - |
|
140 | - function handleSave($prefix,$redirect=true, $useRequired=false, $useSiteURL = false, $entryPoint = 'download', $useUploadFolder = false) |
|
141 | - { |
|
142 | - require_once('include/formbase.php'); |
|
143 | - require_once('include/upload_file.php'); |
|
144 | - global $upload_maxsize; |
|
145 | - global $mod_strings; |
|
146 | - global $sugar_config; |
|
147 | - |
|
148 | - $focus = new EmailTemplate(); |
|
149 | - if ($useRequired && !checkRequired($prefix, array_keys($focus->required_fields))) { |
|
150 | - return null; |
|
151 | - } |
|
152 | - $focus = populateFromPost($prefix, $focus); |
|
153 | - //process the text only flag |
|
154 | - if (isset($_POST['text_only']) && ($_POST['text_only'] == '1')) { |
|
155 | - $focus->text_only = 1; |
|
156 | - } else { |
|
157 | - $focus->text_only = 0; |
|
158 | - } |
|
159 | - if (!$focus->ACLAccess('Save')) { |
|
160 | - ACLController::displayNoAccess(true); |
|
161 | - sugar_cleanup(true); |
|
162 | - } |
|
163 | - if (!isset($_REQUEST['published'])) $focus->published = 'off'; |
|
164 | - |
|
165 | - $this->handleAttachmentsProcessImages($focus, $redirect, $useSiteURL, $entryPoint, $useUploadFolder); |
|
166 | - return $focus; |
|
167 | - } |
|
168 | - |
|
169 | - public function handleAttachmentsProcessImages($focus, $redirect, $useSiteURL = false, $entryPoint = 'download', $useUploadFolder = false) { |
|
170 | - $return_id = $this->processImages($focus, $useSiteURL, $entryPoint, $useUploadFolder); |
|
171 | - return $this->handleAttachments($focus, $redirect, $return_id); |
|
172 | - } |
|
173 | - |
|
174 | - public function processImages(&$focus, $useSiteURL, $entryPoint, $useUploadFolder) { |
|
175 | - global $sugar_config; |
|
176 | - $preProcessedImages = array(); |
|
177 | - $emailTemplateBodyHtml = from_html($focus->body_html); |
|
178 | - if (strpos($emailTemplateBodyHtml, '"cache/images/')) { |
|
179 | - $matches = array(); |
|
180 | - preg_match_all('#<img[^>]*[\s]+src[^=]*=[\s]*["\']cache/images/(.+?)["\']#si', $emailTemplateBodyHtml, $matches); |
|
181 | - foreach ($matches[1] as $match) { |
|
182 | - $filename = urldecode($match); |
|
183 | - if ($filename != pathinfo($filename, PATHINFO_BASENAME)) { |
|
184 | - // don't allow paths there |
|
185 | - $emailTemplateBodyHtml = str_replace("cache/images/$match", "", $emailTemplateBodyHtml); |
|
186 | - continue; |
|
187 | - } |
|
188 | - $file_location = sugar_cached("images/{$filename}"); |
|
189 | - $mime_type = pathinfo($filename, PATHINFO_EXTENSION); |
|
190 | - |
|
191 | - if (file_exists($file_location)) { |
|
192 | - //$id = create_guid(); |
|
193 | - |
|
194 | - $note = new Note(); |
|
195 | - $note->save(); |
|
196 | - $id = $note->id; |
|
197 | - |
|
198 | - $newFileLocation = "upload://$id"; |
|
199 | - if (!copy($file_location, $newFileLocation)) { |
|
200 | - $GLOBALS['log']->debug("EMAIL Template could not copy attachment to $newFileLocation"); |
|
201 | - } else { |
|
202 | - if($useUploadFolder) { |
|
203 | - $secureLink = ($useSiteURL ? $sugar_config['site_url'] . '/' : '') . "public/{$id}"; |
|
204 | - // create a copy with correct extension by mime type |
|
205 | - if(!file_exists('public')) { |
|
206 | - sugar_mkdir('public', 777); |
|
207 | - } |
|
208 | - if(copy($file_location, "public/{$id}.{$mime_type}")) { |
|
209 | - $secureLink .= ".{$mime_type}"; |
|
210 | - } |
|
211 | - } |
|
212 | - else { |
|
213 | - $secureLink = ($useSiteURL ? $sugar_config['site_url'] . '/' : '') . "index.php?entryPoint=" . $entryPoint . "&type=Notes&id={$id}&filename=" . $match; |
|
214 | - } |
|
215 | - |
|
216 | - $emailTemplateBodyHtml = str_replace("cache/images/$match", $secureLink, $emailTemplateBodyHtml); |
|
217 | - //unlink($file_location); |
|
218 | - $preProcessedImages[$filename] = $id; |
|
219 | - } |
|
220 | - } // if |
|
221 | - } // foreach |
|
222 | - } // if |
|
223 | - if (isset($GLOBALS['check_notify'])) { |
|
224 | - $check_notify = $GLOBALS['check_notify']; |
|
225 | - } else { |
|
226 | - $check_notify = FALSE; |
|
227 | - } |
|
228 | - if($preProcessedImages) { |
|
229 | - $focus->body_html = $emailTemplateBodyHtml; |
|
230 | - } |
|
231 | - $return_id = $focus->save($check_notify); |
|
232 | - return $return_id; |
|
233 | - } |
|
234 | - |
|
235 | - public function handleAttachments($focus, $redirect, $return_id) { |
|
236 | - /////////////////////////////////////////////////////////////////////////////// |
|
237 | - //// ATTACHMENT HANDLING |
|
238 | - |
|
239 | - /////////////////////////////////////////////////////////////////////////// |
|
240 | - //// ADDING NEW ATTACHMENTS |
|
241 | - |
|
242 | - global $mod_strings; |
|
243 | - |
|
244 | - $max_files_upload = count($_FILES); |
|
245 | - |
|
246 | - if(!empty($focus->id)) { |
|
247 | - $note = new Note(); |
|
248 | - $where = "notes.parent_id='{$focus->id}'"; |
|
249 | - if(!empty($_REQUEST['old_id'])) { // to support duplication of email templates |
|
250 | - $where .= " OR notes.parent_id='".htmlspecialchars($_REQUEST['old_id'], ENT_QUOTES)."'"; |
|
251 | - } |
|
252 | - $notes_list = $note->get_full_list("", $where, true); |
|
253 | - } |
|
254 | - |
|
255 | - if(!isset($notes_list)) { |
|
256 | - $notes_list = array(); |
|
257 | - } |
|
258 | - |
|
259 | - if(!is_array($focus->attachments)) { // PHP5 does not auto-create arrays(). Need to initialize it here. |
|
260 | - $focus->attachments = array(); |
|
261 | - } |
|
262 | - $focus->attachments = array_merge($focus->attachments, $notes_list); |
|
263 | - |
|
264 | - |
|
265 | - |
|
266 | - //for($i = 0; $i < $max_files_upload; $i++) { |
|
267 | - |
|
268 | - foreach ($_FILES as $key => $file) |
|
269 | - { |
|
270 | - $note = new Note(); |
|
271 | - |
|
272 | - //Images are presaved above so we need to prevent duplicate files from being created. |
|
273 | - if( isset($preProcessedImages[$file['name']]) ) |
|
274 | - { |
|
275 | - $oldId = $preProcessedImages[$file['name']]; |
|
276 | - $note->id = $oldId; |
|
277 | - $note->new_with_id = TRUE; |
|
278 | - $GLOBALS['log']->debug("Image {$file['name']} has already been processed."); |
|
132 | + $the_form .= get_left_form_footer(); |
|
133 | + $the_form .= get_validate_record_js(); |
|
134 | + |
|
135 | + |
|
136 | + return $the_form; |
|
137 | + } |
|
138 | + |
|
139 | + |
|
140 | + function handleSave($prefix,$redirect=true, $useRequired=false, $useSiteURL = false, $entryPoint = 'download', $useUploadFolder = false) |
|
141 | + { |
|
142 | + require_once('include/formbase.php'); |
|
143 | + require_once('include/upload_file.php'); |
|
144 | + global $upload_maxsize; |
|
145 | + global $mod_strings; |
|
146 | + global $sugar_config; |
|
147 | + |
|
148 | + $focus = new EmailTemplate(); |
|
149 | + if ($useRequired && !checkRequired($prefix, array_keys($focus->required_fields))) { |
|
150 | + return null; |
|
151 | + } |
|
152 | + $focus = populateFromPost($prefix, $focus); |
|
153 | + //process the text only flag |
|
154 | + if (isset($_POST['text_only']) && ($_POST['text_only'] == '1')) { |
|
155 | + $focus->text_only = 1; |
|
156 | + } else { |
|
157 | + $focus->text_only = 0; |
|
158 | + } |
|
159 | + if (!$focus->ACLAccess('Save')) { |
|
160 | + ACLController::displayNoAccess(true); |
|
161 | + sugar_cleanup(true); |
|
162 | + } |
|
163 | + if (!isset($_REQUEST['published'])) $focus->published = 'off'; |
|
164 | + |
|
165 | + $this->handleAttachmentsProcessImages($focus, $redirect, $useSiteURL, $entryPoint, $useUploadFolder); |
|
166 | + return $focus; |
|
167 | + } |
|
168 | + |
|
169 | + public function handleAttachmentsProcessImages($focus, $redirect, $useSiteURL = false, $entryPoint = 'download', $useUploadFolder = false) { |
|
170 | + $return_id = $this->processImages($focus, $useSiteURL, $entryPoint, $useUploadFolder); |
|
171 | + return $this->handleAttachments($focus, $redirect, $return_id); |
|
172 | + } |
|
173 | + |
|
174 | + public function processImages(&$focus, $useSiteURL, $entryPoint, $useUploadFolder) { |
|
175 | + global $sugar_config; |
|
176 | + $preProcessedImages = array(); |
|
177 | + $emailTemplateBodyHtml = from_html($focus->body_html); |
|
178 | + if (strpos($emailTemplateBodyHtml, '"cache/images/')) { |
|
179 | + $matches = array(); |
|
180 | + preg_match_all('#<img[^>]*[\s]+src[^=]*=[\s]*["\']cache/images/(.+?)["\']#si', $emailTemplateBodyHtml, $matches); |
|
181 | + foreach ($matches[1] as $match) { |
|
182 | + $filename = urldecode($match); |
|
183 | + if ($filename != pathinfo($filename, PATHINFO_BASENAME)) { |
|
184 | + // don't allow paths there |
|
185 | + $emailTemplateBodyHtml = str_replace("cache/images/$match", "", $emailTemplateBodyHtml); |
|
186 | + continue; |
|
187 | + } |
|
188 | + $file_location = sugar_cached("images/{$filename}"); |
|
189 | + $mime_type = pathinfo($filename, PATHINFO_EXTENSION); |
|
190 | + |
|
191 | + if (file_exists($file_location)) { |
|
192 | + //$id = create_guid(); |
|
193 | + |
|
194 | + $note = new Note(); |
|
195 | + $note->save(); |
|
196 | + $id = $note->id; |
|
197 | + |
|
198 | + $newFileLocation = "upload://$id"; |
|
199 | + if (!copy($file_location, $newFileLocation)) { |
|
200 | + $GLOBALS['log']->debug("EMAIL Template could not copy attachment to $newFileLocation"); |
|
201 | + } else { |
|
202 | + if($useUploadFolder) { |
|
203 | + $secureLink = ($useSiteURL ? $sugar_config['site_url'] . '/' : '') . "public/{$id}"; |
|
204 | + // create a copy with correct extension by mime type |
|
205 | + if(!file_exists('public')) { |
|
206 | + sugar_mkdir('public', 777); |
|
207 | + } |
|
208 | + if(copy($file_location, "public/{$id}.{$mime_type}")) { |
|
209 | + $secureLink .= ".{$mime_type}"; |
|
210 | + } |
|
211 | + } |
|
212 | + else { |
|
213 | + $secureLink = ($useSiteURL ? $sugar_config['site_url'] . '/' : '') . "index.php?entryPoint=" . $entryPoint . "&type=Notes&id={$id}&filename=" . $match; |
|
214 | + } |
|
215 | + |
|
216 | + $emailTemplateBodyHtml = str_replace("cache/images/$match", $secureLink, $emailTemplateBodyHtml); |
|
217 | + //unlink($file_location); |
|
218 | + $preProcessedImages[$filename] = $id; |
|
219 | + } |
|
220 | + } // if |
|
221 | + } // foreach |
|
222 | + } // if |
|
223 | + if (isset($GLOBALS['check_notify'])) { |
|
224 | + $check_notify = $GLOBALS['check_notify']; |
|
225 | + } else { |
|
226 | + $check_notify = FALSE; |
|
227 | + } |
|
228 | + if($preProcessedImages) { |
|
229 | + $focus->body_html = $emailTemplateBodyHtml; |
|
230 | + } |
|
231 | + $return_id = $focus->save($check_notify); |
|
232 | + return $return_id; |
|
233 | + } |
|
234 | + |
|
235 | + public function handleAttachments($focus, $redirect, $return_id) { |
|
236 | + /////////////////////////////////////////////////////////////////////////////// |
|
237 | + //// ATTACHMENT HANDLING |
|
238 | + |
|
239 | + /////////////////////////////////////////////////////////////////////////// |
|
240 | + //// ADDING NEW ATTACHMENTS |
|
241 | + |
|
242 | + global $mod_strings; |
|
243 | + |
|
244 | + $max_files_upload = count($_FILES); |
|
245 | + |
|
246 | + if(!empty($focus->id)) { |
|
247 | + $note = new Note(); |
|
248 | + $where = "notes.parent_id='{$focus->id}'"; |
|
249 | + if(!empty($_REQUEST['old_id'])) { // to support duplication of email templates |
|
250 | + $where .= " OR notes.parent_id='".htmlspecialchars($_REQUEST['old_id'], ENT_QUOTES)."'"; |
|
279 | 251 | } |
252 | + $notes_list = $note->get_full_list("", $where, true); |
|
253 | + } |
|
254 | + |
|
255 | + if(!isset($notes_list)) { |
|
256 | + $notes_list = array(); |
|
257 | + } |
|
258 | + |
|
259 | + if(!is_array($focus->attachments)) { // PHP5 does not auto-create arrays(). Need to initialize it here. |
|
260 | + $focus->attachments = array(); |
|
261 | + } |
|
262 | + $focus->attachments = array_merge($focus->attachments, $notes_list); |
|
263 | + |
|
264 | + |
|
265 | + |
|
266 | + //for($i = 0; $i < $max_files_upload; $i++) { |
|
267 | + |
|
268 | + foreach ($_FILES as $key => $file) |
|
269 | + { |
|
270 | + $note = new Note(); |
|
280 | 271 | |
281 | - $i=preg_replace("/email_attachment(.+)/",'$1',$key); |
|
282 | - $upload_file = new UploadFile($key); |
|
283 | - |
|
284 | - if(isset($_FILES[$key]) && $upload_file->confirm_upload() && preg_match("/^email_attachment/",$key)) { |
|
285 | - $note->filename = $upload_file->get_stored_file_name(); |
|
286 | - $note->file = $upload_file; |
|
287 | - $note->name = $mod_strings['LBL_EMAIL_ATTACHMENT'].': '.$note->file->original_file_name; |
|
288 | - if(isset($_REQUEST['embedded'.$i]) && !empty($_REQUEST['embedded'.$i])){ |
|
289 | - if($_REQUEST['embedded'.$i]=='true'){ |
|
290 | - $note->embed_flag =true; |
|
291 | - } |
|
292 | - else{ |
|
293 | - $note->embed_flag =false; |
|
294 | - } |
|
295 | - } |
|
296 | - array_push($focus->attachments, $note); |
|
297 | - } |
|
298 | - |
|
299 | - } |
|
300 | - |
|
301 | - $focus->saved_attachments = array(); |
|
302 | - foreach($focus->attachments as $note) |
|
303 | - { |
|
304 | - if( !empty($note->id) && $note->new_with_id === FALSE) |
|
305 | - { |
|
306 | - if(empty($_REQUEST['old_id'])) |
|
307 | - array_push($focus->saved_attachments, $note); // to support duplication of email templates |
|
308 | - else |
|
309 | - { |
|
310 | - // we're duplicating a template with attachments |
|
311 | - // dupe the file, create a new note, assign the note to the new template |
|
312 | - $newNote = new Note(); |
|
313 | - $newNote->retrieve($note->id); |
|
314 | - $newNote->id = create_guid(); |
|
315 | - $newNote->parent_id = $focus->id; |
|
316 | - $newNote->new_with_id = true; |
|
317 | - $newNote->date_modified = ''; |
|
318 | - $newNote->date_entered = ''; |
|
319 | - /* BEGIN - SECURITY GROUPS */ |
|
320 | - //Need to do this so that attachments show under an EmailTemplate correctly for a normal user |
|
321 | - global $current_user; |
|
322 | - $newNote->assigned_user_id = $current_user->id; |
|
323 | - /* END - SECURITY GROUPS */ |
|
324 | - $newNoteId = $newNote->save(); |
|
325 | - |
|
326 | - UploadFile::duplicate_file($note->id, $newNoteId, $note->filename); |
|
327 | - } |
|
328 | - continue; |
|
329 | - } |
|
330 | - $note->parent_id = $focus->id; |
|
331 | - $note->parent_type = 'Emails'; |
|
332 | - $note->file_mime_type = $note->file->mime_type; |
|
333 | - /* BEGIN - SECURITY GROUPS */ |
|
334 | - //Need to do this so that attachments show under an EmailTemplate correctly for a normal user |
|
335 | - global $current_user; |
|
336 | - $note->assigned_user_id = $current_user->id; |
|
337 | - /* END - SECURITY GROUPS */ |
|
338 | - $note_id = $note->save(); |
|
339 | - array_push($focus->saved_attachments, $note); |
|
340 | - $note->id = $note_id; |
|
341 | - |
|
342 | - if($note->new_with_id === FALSE) |
|
343 | - $note->file->final_move($note->id); |
|
344 | - else |
|
345 | - $GLOBALS['log']->debug("Not performing final move for note id {$note->id} as it has already been processed"); |
|
346 | - } |
|
347 | - |
|
348 | - //// END NEW ATTACHMENTS |
|
349 | - /////////////////////////////////////////////////////////////////////////// |
|
350 | - |
|
351 | - /////////////////////////////////////////////////////////////////////////// |
|
352 | - //// ATTACHMENTS FROM DOCUMENTS |
|
353 | - $count=''; |
|
354 | - //_pp($_REQUEST); |
|
355 | - //_ppd(count($_REQUEST['document'])); |
|
356 | - if(!empty($_REQUEST['document'])){ |
|
357 | - $count = count($_REQUEST['document']); |
|
272 | + //Images are presaved above so we need to prevent duplicate files from being created. |
|
273 | + if( isset($preProcessedImages[$file['name']]) ) |
|
274 | + { |
|
275 | + $oldId = $preProcessedImages[$file['name']]; |
|
276 | + $note->id = $oldId; |
|
277 | + $note->new_with_id = TRUE; |
|
278 | + $GLOBALS['log']->debug("Image {$file['name']} has already been processed."); |
|
279 | + } |
|
280 | + |
|
281 | + $i=preg_replace("/email_attachment(.+)/",'$1',$key); |
|
282 | + $upload_file = new UploadFile($key); |
|
283 | + |
|
284 | + if(isset($_FILES[$key]) && $upload_file->confirm_upload() && preg_match("/^email_attachment/",$key)) { |
|
285 | + $note->filename = $upload_file->get_stored_file_name(); |
|
286 | + $note->file = $upload_file; |
|
287 | + $note->name = $mod_strings['LBL_EMAIL_ATTACHMENT'].': '.$note->file->original_file_name; |
|
288 | + if(isset($_REQUEST['embedded'.$i]) && !empty($_REQUEST['embedded'.$i])){ |
|
289 | + if($_REQUEST['embedded'.$i]=='true'){ |
|
290 | + $note->embed_flag =true; |
|
291 | + } |
|
292 | + else{ |
|
293 | + $note->embed_flag =false; |
|
294 | + } |
|
295 | + } |
|
296 | + array_push($focus->attachments, $note); |
|
297 | + } |
|
298 | + |
|
299 | + } |
|
300 | + |
|
301 | + $focus->saved_attachments = array(); |
|
302 | + foreach($focus->attachments as $note) |
|
303 | + { |
|
304 | + if( !empty($note->id) && $note->new_with_id === FALSE) |
|
305 | + { |
|
306 | + if(empty($_REQUEST['old_id'])) |
|
307 | + array_push($focus->saved_attachments, $note); // to support duplication of email templates |
|
308 | + else |
|
309 | + { |
|
310 | + // we're duplicating a template with attachments |
|
311 | + // dupe the file, create a new note, assign the note to the new template |
|
312 | + $newNote = new Note(); |
|
313 | + $newNote->retrieve($note->id); |
|
314 | + $newNote->id = create_guid(); |
|
315 | + $newNote->parent_id = $focus->id; |
|
316 | + $newNote->new_with_id = true; |
|
317 | + $newNote->date_modified = ''; |
|
318 | + $newNote->date_entered = ''; |
|
319 | + /* BEGIN - SECURITY GROUPS */ |
|
320 | + //Need to do this so that attachments show under an EmailTemplate correctly for a normal user |
|
321 | + global $current_user; |
|
322 | + $newNote->assigned_user_id = $current_user->id; |
|
323 | + /* END - SECURITY GROUPS */ |
|
324 | + $newNoteId = $newNote->save(); |
|
325 | + |
|
326 | + UploadFile::duplicate_file($note->id, $newNoteId, $note->filename); |
|
327 | + } |
|
328 | + continue; |
|
329 | + } |
|
330 | + $note->parent_id = $focus->id; |
|
331 | + $note->parent_type = 'Emails'; |
|
332 | + $note->file_mime_type = $note->file->mime_type; |
|
333 | + /* BEGIN - SECURITY GROUPS */ |
|
334 | + //Need to do this so that attachments show under an EmailTemplate correctly for a normal user |
|
335 | + global $current_user; |
|
336 | + $note->assigned_user_id = $current_user->id; |
|
337 | + /* END - SECURITY GROUPS */ |
|
338 | + $note_id = $note->save(); |
|
339 | + array_push($focus->saved_attachments, $note); |
|
340 | + $note->id = $note_id; |
|
341 | + |
|
342 | + if($note->new_with_id === FALSE) |
|
343 | + $note->file->final_move($note->id); |
|
344 | + else |
|
345 | + $GLOBALS['log']->debug("Not performing final move for note id {$note->id} as it has already been processed"); |
|
346 | + } |
|
347 | + |
|
348 | + //// END NEW ATTACHMENTS |
|
349 | + /////////////////////////////////////////////////////////////////////////// |
|
350 | + |
|
351 | + /////////////////////////////////////////////////////////////////////////// |
|
352 | + //// ATTACHMENTS FROM DOCUMENTS |
|
353 | + $count=''; |
|
354 | + //_pp($_REQUEST); |
|
355 | + //_ppd(count($_REQUEST['document'])); |
|
356 | + if(!empty($_REQUEST['document'])){ |
|
357 | + $count = count($_REQUEST['document']); |
|
358 | 358 | } |
359 | 359 | else{ |
360 | - $count=10; |
|
360 | + $count=10; |
|
361 | 361 | } |
362 | 362 | |
363 | - for($i=0; $i<$count; $i++) { |
|
364 | - if(isset($_REQUEST['documentId'.$i]) && !empty($_REQUEST['documentId'.$i])) { |
|
365 | - $doc = new Document(); |
|
366 | - $docRev = new DocumentRevision(); |
|
367 | - $docNote = new Note(); |
|
363 | + for($i=0; $i<$count; $i++) { |
|
364 | + if(isset($_REQUEST['documentId'.$i]) && !empty($_REQUEST['documentId'.$i])) { |
|
365 | + $doc = new Document(); |
|
366 | + $docRev = new DocumentRevision(); |
|
367 | + $docNote = new Note(); |
|
368 | 368 | |
369 | - $doc->retrieve($_REQUEST['documentId'.$i]); |
|
370 | - $docRev->retrieve($doc->document_revision_id); |
|
369 | + $doc->retrieve($_REQUEST['documentId'.$i]); |
|
370 | + $docRev->retrieve($doc->document_revision_id); |
|
371 | 371 | |
372 | - array_push($focus->saved_attachments, $docRev); |
|
372 | + array_push($focus->saved_attachments, $docRev); |
|
373 | 373 | |
374 | - $docNote->name = $doc->document_name; |
|
375 | - $docNote->filename = $docRev->filename; |
|
376 | - $docNote->description = $doc->description; |
|
377 | - $docNote->parent_id = $focus->id; |
|
378 | - $docNote->parent_type = 'Emails'; |
|
379 | - $docNote->file_mime_type = $docRev->file_mime_type; |
|
380 | - $docId = $docNote = $docNote->save(); |
|
374 | + $docNote->name = $doc->document_name; |
|
375 | + $docNote->filename = $docRev->filename; |
|
376 | + $docNote->description = $doc->description; |
|
377 | + $docNote->parent_id = $focus->id; |
|
378 | + $docNote->parent_type = 'Emails'; |
|
379 | + $docNote->file_mime_type = $docRev->file_mime_type; |
|
380 | + $docId = $docNote = $docNote->save(); |
|
381 | 381 | |
382 | - UploadFile::duplicate_file($docRev->id, $docId, $docRev->filename); |
|
383 | - } |
|
382 | + UploadFile::duplicate_file($docRev->id, $docId, $docRev->filename); |
|
383 | + } |
|
384 | 384 | |
385 | - } |
|
385 | + } |
|
386 | 386 | |
387 | - //// END ATTACHMENTS FROM DOCUMENTS |
|
388 | - /////////////////////////////////////////////////////////////////////////// |
|
387 | + //// END ATTACHMENTS FROM DOCUMENTS |
|
388 | + /////////////////////////////////////////////////////////////////////////// |
|
389 | 389 | |
390 | - /////////////////////////////////////////////////////////////////////////// |
|
391 | - //// REMOVE ATTACHMENTS |
|
390 | + /////////////////////////////////////////////////////////////////////////// |
|
391 | + //// REMOVE ATTACHMENTS |
|
392 | 392 | |
393 | - if(isset($_REQUEST['remove_attachment']) && !empty($_REQUEST['remove_attachment'])) { |
|
394 | - foreach($_REQUEST['remove_attachment'] as $noteId) { |
|
395 | - $q = 'UPDATE notes SET deleted = 1 WHERE id = \''.$noteId.'\''; |
|
396 | - $focus->db->query($q); |
|
397 | - } |
|
393 | + if(isset($_REQUEST['remove_attachment']) && !empty($_REQUEST['remove_attachment'])) { |
|
394 | + foreach($_REQUEST['remove_attachment'] as $noteId) { |
|
395 | + $q = 'UPDATE notes SET deleted = 1 WHERE id = \''.$noteId.'\''; |
|
396 | + $focus->db->query($q); |
|
397 | + } |
|
398 | 398 | |
399 | - } |
|
399 | + } |
|
400 | 400 | |
401 | - //// END REMOVE ATTACHMENTS |
|
402 | - /////////////////////////////////////////////////////////////////////////// |
|
403 | - //// END ATTACHMENT HANDLING |
|
404 | - /////////////////////////////////////////////////////////////////////////////// |
|
401 | + //// END REMOVE ATTACHMENTS |
|
402 | + /////////////////////////////////////////////////////////////////////////// |
|
403 | + //// END ATTACHMENT HANDLING |
|
404 | + /////////////////////////////////////////////////////////////////////////////// |
|
405 | 405 | |
406 | 406 | clear_register_value('select_array', $focus->object_name); |
407 | 407 | |
408 | - if($redirect) { |
|
409 | - $GLOBALS['log']->debug("Saved record with id of ".$return_id); |
|
410 | - handleRedirect($return_id, "EmailTemplates"); |
|
411 | - }else{ |
|
412 | - return $focus; |
|
413 | - } |
|
414 | - } |
|
408 | + if($redirect) { |
|
409 | + $GLOBALS['log']->debug("Saved record with id of ".$return_id); |
|
410 | + handleRedirect($return_id, "EmailTemplates"); |
|
411 | + }else{ |
|
412 | + return $focus; |
|
413 | + } |
|
414 | + } |
|
415 | 415 | |
416 | 416 | } |
417 | 417 | ?> |
418 | 418 | \ No newline at end of file |
@@ -40,47 +40,47 @@ |
||
40 | 40 | global $current_user; |
41 | 41 | $module_name = "Contacts"; |
42 | 42 | $searchFields['Contacts'] = |
43 | - array ( |
|
44 | - 'first_name' => array( 'query_type'=>'default'), |
|
45 | - 'last_name'=> array('query_type'=>'default'), |
|
46 | - 'search_name'=> array('query_type'=>'default','db_field'=>array('first_name','last_name'),'force_unifiedsearch'=>true), |
|
47 | - 'account_name'=> array('query_type'=>'default','db_field'=>array('accounts.name')), |
|
48 | - 'lead_source'=> array('query_type'=>'default','operator'=>'=', 'options' => 'lead_source_dom', 'template_var' => 'LEAD_SOURCE_OPTIONS'), |
|
49 | - 'do_not_call'=> array('query_type'=>'default', 'input_type' => 'checkbox', 'operator'=>'='), |
|
50 | - 'phone'=> array('query_type'=>'default','db_field'=>array('phone_mobile','phone_work','phone_other','phone_fax','assistant_phone')), |
|
51 | - 'email'=> array( |
|
52 | - 'query_type' => 'default', |
|
53 | - 'operator' => 'subquery', |
|
54 | - 'subquery' => 'SELECT eabr.bean_id FROM email_addr_bean_rel eabr JOIN email_addresses ea ON (ea.id = eabr.email_address_id) WHERE eabr.deleted=0 AND ea.email_address LIKE', |
|
55 | - 'db_field' => array( |
|
56 | - 'id', |
|
57 | - ), |
|
58 | - ), |
|
43 | + array ( |
|
44 | + 'first_name' => array( 'query_type'=>'default'), |
|
45 | + 'last_name'=> array('query_type'=>'default'), |
|
46 | + 'search_name'=> array('query_type'=>'default','db_field'=>array('first_name','last_name'),'force_unifiedsearch'=>true), |
|
47 | + 'account_name'=> array('query_type'=>'default','db_field'=>array('accounts.name')), |
|
48 | + 'lead_source'=> array('query_type'=>'default','operator'=>'=', 'options' => 'lead_source_dom', 'template_var' => 'LEAD_SOURCE_OPTIONS'), |
|
49 | + 'do_not_call'=> array('query_type'=>'default', 'input_type' => 'checkbox', 'operator'=>'='), |
|
50 | + 'phone'=> array('query_type'=>'default','db_field'=>array('phone_mobile','phone_work','phone_other','phone_fax','assistant_phone')), |
|
51 | + 'email'=> array( |
|
52 | + 'query_type' => 'default', |
|
53 | + 'operator' => 'subquery', |
|
54 | + 'subquery' => 'SELECT eabr.bean_id FROM email_addr_bean_rel eabr JOIN email_addresses ea ON (ea.id = eabr.email_address_id) WHERE eabr.deleted=0 AND ea.email_address LIKE', |
|
55 | + 'db_field' => array( |
|
56 | + 'id', |
|
57 | + ), |
|
58 | + ), |
|
59 | 59 | 'favorites_only' => array( |
60 | 60 | 'query_type'=>'format', |
61 | 61 | 'operator' => 'subquery', |
62 | - 'subquery' => "SELECT favorites.parent_id FROM favorites |
|
62 | + 'subquery' => "SELECT favorites.parent_id FROM favorites |
|
63 | 63 | WHERE favorites.deleted = 0 |
64 | 64 | and favorites.parent_type = '".$module_name."' |
65 | 65 | and favorites.assigned_user_id = '" .$current_user->id . "') OR NOT ({0}", |
66 | 66 | 'db_field'=>array('id')), |
67 | - 'assistant'=> array('query_type'=>'default'), |
|
68 | - 'address_street'=> array('query_type'=>'default','db_field'=>array('primary_address_street','alt_address_street')), |
|
69 | - 'address_city'=> array('query_type'=>'default','db_field'=>array('primary_address_city','alt_address_city')), |
|
70 | - 'address_state'=> array('query_type'=>'default','db_field'=>array('primary_address_state','alt_address_state')), |
|
71 | - 'address_postalcode'=> array('query_type'=>'default','db_field'=>array('primary_address_postalcode','alt_address_postalcode')), |
|
72 | - 'address_country'=> array('query_type'=>'default','db_field'=>array('primary_address_country','alt_address_country')), |
|
73 | - 'current_user_only'=> array('query_type'=>'default','db_field'=>array('assigned_user_id'),'my_items'=>true, 'vname' => 'LBL_CURRENT_USER_FILTER', 'type' => 'bool'), |
|
74 | - 'assigned_user_id'=> array('query_type'=>'default'), |
|
67 | + 'assistant'=> array('query_type'=>'default'), |
|
68 | + 'address_street'=> array('query_type'=>'default','db_field'=>array('primary_address_street','alt_address_street')), |
|
69 | + 'address_city'=> array('query_type'=>'default','db_field'=>array('primary_address_city','alt_address_city')), |
|
70 | + 'address_state'=> array('query_type'=>'default','db_field'=>array('primary_address_state','alt_address_state')), |
|
71 | + 'address_postalcode'=> array('query_type'=>'default','db_field'=>array('primary_address_postalcode','alt_address_postalcode')), |
|
72 | + 'address_country'=> array('query_type'=>'default','db_field'=>array('primary_address_country','alt_address_country')), |
|
73 | + 'current_user_only'=> array('query_type'=>'default','db_field'=>array('assigned_user_id'),'my_items'=>true, 'vname' => 'LBL_CURRENT_USER_FILTER', 'type' => 'bool'), |
|
74 | + 'assigned_user_id'=> array('query_type'=>'default'), |
|
75 | 75 | 'account_id'=> array('query_type'=>'default','db_field'=>array('accounts.id')), |
76 | 76 | 'campaign_name'=> array('query_type'=>'default'), |
77 | - //Range Search Support |
|
78 | - 'range_date_entered' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
79 | - 'start_range_date_entered' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
80 | - 'end_range_date_entered' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
81 | - 'range_date_modified' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
82 | - 'start_range_date_modified' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
77 | + //Range Search Support |
|
78 | + 'range_date_entered' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
79 | + 'start_range_date_entered' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
80 | + 'end_range_date_entered' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
81 | + 'range_date_modified' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
82 | + 'start_range_date_modified' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
83 | 83 | 'end_range_date_modified' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
84 | - //Range Search Support |
|
85 | - ); |
|
84 | + //Range Search Support |
|
85 | + ); |
|
86 | 86 | ?> |
@@ -40,48 +40,48 @@ |
||
40 | 40 | global $current_user; |
41 | 41 | $module_name = "Opportunities"; |
42 | 42 | $searchFields['Opportunities'] = |
43 | - array ( |
|
44 | - 'name' => array( 'query_type'=>'default'), |
|
45 | - 'account_name'=> array('query_type'=>'default','db_field'=>array('accounts.name')), |
|
46 | - 'amount'=> array('query_type'=>'default'), |
|
47 | - 'next_step'=> array('query_type'=>'default'), |
|
48 | - 'probability'=> array('query_type'=>'default'), |
|
49 | - 'lead_source'=> array('query_type'=>'default', 'operator'=>'=', 'options' => 'lead_source_dom', 'template_var' => 'LEAD_SOURCE_OPTIONS'), |
|
50 | - 'opportunity_type'=> array('query_type'=>'default', 'operator'=>'=', 'options' => 'opportunity_type_dom', 'template_var' => 'TYPE_OPTIONS'), |
|
51 | - 'sales_stage'=> array('query_type'=>'default', 'operator'=>'=', 'options' => 'sales_stage_dom', 'template_var' => 'SALES_STAGE_OPTIONS', 'options_add_blank' => true), |
|
52 | - 'current_user_only'=> array('query_type'=>'default','db_field'=>array('assigned_user_id'),'my_items'=>true, 'vname' => 'LBL_CURRENT_USER_FILTER', 'type' => 'bool'), |
|
53 | - 'assigned_user_id'=> array('query_type'=>'default'), |
|
54 | - 'open_only' => array( |
|
55 | - 'query_type'=>'default', |
|
56 | - 'db_field'=>array('sales_stage'), |
|
57 | - 'operator'=>'not in', |
|
58 | - 'closed_values' => array('Closed Won', 'Closed Lost'), |
|
59 | - 'type'=>'bool', |
|
60 | - ), |
|
43 | + array ( |
|
44 | + 'name' => array( 'query_type'=>'default'), |
|
45 | + 'account_name'=> array('query_type'=>'default','db_field'=>array('accounts.name')), |
|
46 | + 'amount'=> array('query_type'=>'default'), |
|
47 | + 'next_step'=> array('query_type'=>'default'), |
|
48 | + 'probability'=> array('query_type'=>'default'), |
|
49 | + 'lead_source'=> array('query_type'=>'default', 'operator'=>'=', 'options' => 'lead_source_dom', 'template_var' => 'LEAD_SOURCE_OPTIONS'), |
|
50 | + 'opportunity_type'=> array('query_type'=>'default', 'operator'=>'=', 'options' => 'opportunity_type_dom', 'template_var' => 'TYPE_OPTIONS'), |
|
51 | + 'sales_stage'=> array('query_type'=>'default', 'operator'=>'=', 'options' => 'sales_stage_dom', 'template_var' => 'SALES_STAGE_OPTIONS', 'options_add_blank' => true), |
|
52 | + 'current_user_only'=> array('query_type'=>'default','db_field'=>array('assigned_user_id'),'my_items'=>true, 'vname' => 'LBL_CURRENT_USER_FILTER', 'type' => 'bool'), |
|
53 | + 'assigned_user_id'=> array('query_type'=>'default'), |
|
54 | + 'open_only' => array( |
|
55 | + 'query_type'=>'default', |
|
56 | + 'db_field'=>array('sales_stage'), |
|
57 | + 'operator'=>'not in', |
|
58 | + 'closed_values' => array('Closed Won', 'Closed Lost'), |
|
59 | + 'type'=>'bool', |
|
60 | + ), |
|
61 | 61 | 'favorites_only' => array( |
62 | 62 | 'query_type'=>'format', |
63 | 63 | 'operator' => 'subquery', |
64 | - 'subquery' => "SELECT favorites.parent_id FROM favorites |
|
64 | + 'subquery' => "SELECT favorites.parent_id FROM favorites |
|
65 | 65 | WHERE favorites.deleted = 0 |
66 | 66 | and favorites.parent_type = '".$module_name."' |
67 | 67 | and favorites.assigned_user_id = '" .$current_user->id . "') OR NOT ({0}", |
68 | 68 | 'db_field'=>array('id')), |
69 | 69 | |
70 | - //Range Search Support |
|
71 | - 'range_date_entered' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
72 | - 'start_range_date_entered' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
73 | - 'end_range_date_entered' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
74 | - 'range_date_modified' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
75 | - 'start_range_date_modified' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
76 | - 'end_range_date_modified' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
70 | + //Range Search Support |
|
71 | + 'range_date_entered' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
72 | + 'start_range_date_entered' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
73 | + 'end_range_date_entered' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
74 | + 'range_date_modified' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
75 | + 'start_range_date_modified' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
76 | + 'end_range_date_modified' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
77 | 77 | |
78 | - 'range_date_closed' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
79 | - 'start_range_date_closed' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
80 | - 'end_range_date_closed' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
78 | + 'range_date_closed' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
79 | + 'start_range_date_closed' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
80 | + 'end_range_date_closed' => array ('query_type' => 'default', 'enable_range_search' => true, 'is_date_field' => true), |
|
81 | 81 | |
82 | - 'range_amount' => array ('query_type' => 'default', 'enable_range_search' => true), |
|
83 | - 'start_range_amount' => array ('query_type' => 'default', 'enable_range_search' => true), |
|
84 | - 'end_range_amount' => array ('query_type' => 'default', 'enable_range_search' => true), |
|
85 | - //Range Search Support |
|
86 | - ); |
|
82 | + 'range_amount' => array ('query_type' => 'default', 'enable_range_search' => true), |
|
83 | + 'start_range_amount' => array ('query_type' => 'default', 'enable_range_search' => true), |
|
84 | + 'end_range_amount' => array ('query_type' => 'default', 'enable_range_search' => true), |
|
85 | + //Range Search Support |
|
86 | + ); |
|
87 | 87 | ?> |
@@ -107,11 +107,11 @@ |
||
107 | 107 | } |
108 | 108 | |
109 | 109 | $typeOfPerson = !empty($_REQUEST['typeOfPerson']) ? $_REQUEST['typeOfPerson'] : 'Lead'; |
110 | - $person = new $typeOfPerson(); |
|
111 | - $fieldsMetaData = new FieldsMetaData(); |
|
112 | - $xtpl=new XTemplate ('modules/Campaigns/WebToLeadForm.html'); |
|
113 | - $xtpl->assign("MOD", $mod_strings); |
|
114 | - $xtpl->assign("APP", $app_strings); |
|
110 | + $person = new $typeOfPerson(); |
|
111 | + $fieldsMetaData = new FieldsMetaData(); |
|
112 | + $xtpl=new XTemplate ('modules/Campaigns/WebToLeadForm.html'); |
|
113 | + $xtpl->assign("MOD", $mod_strings); |
|
114 | + $xtpl->assign("APP", $app_strings); |
|
115 | 115 | |
116 | 116 | include_once 'WebToLeadFormBuilder.php'; |
117 | 117 | $Web_To_Lead_Form_html = WebToLeadFormBuilder::generate( |