Commit 619cec4108b062393133ad1703ff6407fd441721
1 parent
707f53bb
Exists in
master
Alterado CoreExt/DataMapper para executar consultas preparadas
Showing
1 changed file
with
75 additions
and
6 deletions
Show diff stats
ieducar/lib/CoreExt/DataMapper.php
... | ... | @@ -201,6 +201,17 @@ abstract class CoreExt_DataMapper |
201 | 201 | } |
202 | 202 | |
203 | 203 | /** |
204 | + * Retorna o nome do recurso, isto é o nome da tabela sem '_', | |
205 | + * Ex: transporte_aluno => transporte aluno. | |
206 | + * | |
207 | + * @return string | |
208 | + */ | |
209 | + public function resourceName() | |
210 | + { | |
211 | + return strtolower(str_replace('_', ' ', $this->_tableName)); | |
212 | + } | |
213 | + | |
214 | + /** | |
204 | 215 | * Retorna os nomes das colunas da tabela em um array, de acordo com o array |
205 | 216 | * de dados associativo $data. |
206 | 217 | * |
... | ... | @@ -266,6 +277,7 @@ abstract class CoreExt_DataMapper |
266 | 277 | |
267 | 278 | /** |
268 | 279 | * Retorna uma query SQL de recuperação de todos os registros de uma tabela. |
280 | + * | |
269 | 281 | * @param array $data |
270 | 282 | * @param array $where |
271 | 283 | * @param array $orderBy |
... | ... | @@ -281,7 +293,20 @@ abstract class CoreExt_DataMapper |
281 | 293 | if (0 < count($whereArg)) { |
282 | 294 | foreach ($whereArg as $key => $value) { |
283 | 295 | $whereName = $this->_getTableColumn($key); |
284 | - $where[] = sprintf("%s = '%s'", $whereName, $value); | |
296 | + | |
297 | + preg_match('/[<,=,>]/', $value, $matches); | |
298 | + $hasComparisonSign = ! empty($matches); | |
299 | + | |
300 | + // Caso $value contenha <, > ou =, ex: '> $1', não adiciona sinal de igual. | |
301 | + if($hasComparisonSign) | |
302 | + $where[] = sprintf("%s %s", $whereName, $value); | |
303 | + | |
304 | + // Caso $value contenha parametros para consulta preparada ($1, $2...), não adiciona $value entre aspas. | |
305 | + elseif(strpos($value, '$') > -1) | |
306 | + $where[] = sprintf("%s = %s", $whereName, $value); | |
307 | + | |
308 | + else | |
309 | + $where[] = sprintf("%s = '%s'", $whereName, $value); | |
285 | 310 | } |
286 | 311 | } |
287 | 312 | else { |
... | ... | @@ -450,7 +475,8 @@ abstract class CoreExt_DataMapper |
450 | 475 | if ($instance instanceof CoreExt_Entity) { |
451 | 476 | foreach ($this->_primaryKey as $pk) { |
452 | 477 | $whereName = $this->_getTableColumn($pk); |
453 | - $where[] = sprintf("%s = '%d'", $whereName, $instance->get($pk)); | |
478 | + //$where[] = sprintf("%s = '%d'", $whereName, $instance->get($pk)); estoura o decimal. valor 9801762824 retornando 1211828232 | |
479 | + $where[] = sprintf("%s = '%s'", $whereName, $instance->get($pk)); | |
454 | 480 | } |
455 | 481 | } |
456 | 482 | elseif (is_numeric($instance)) { |
... | ... | @@ -473,15 +499,16 @@ abstract class CoreExt_DataMapper |
473 | 499 | * @param array $columns Atributos a serem carregados. O atributo id é sempre carregado. |
474 | 500 | * @param array $where |
475 | 501 | * @param array $orderBy |
502 | + * @param array $addColumnIdIfNotSet Se true, adiciona a coluna 'id' caso não esteja definido no array $columns | |
476 | 503 | * @return array |
477 | 504 | * @todo Problema potencial com busca em registros com compount key. Testar. |
478 | 505 | */ |
479 | - public function findAll(array $columns = array(), array $where = array(), array $orderBy = array()) | |
506 | + public function findAll(array $columns = array(), array $where = array(), array $orderBy = array(), $addColumnIdIfNotSet = true) | |
480 | 507 | { |
481 | 508 | // Inverte chave valor, permitindo array simples como array('nome') |
482 | 509 | if (0 < count($columns)) { |
483 | 510 | $columns = array_flip($columns); |
484 | - if (!isset($columns['id'])) { | |
511 | + if (!isset($columns['id']) && $addColumnIdIfNotSet) { | |
485 | 512 | $columns['id'] = TRUE; |
486 | 513 | } |
487 | 514 | } |
... | ... | @@ -502,6 +529,48 @@ abstract class CoreExt_DataMapper |
502 | 529 | return $list; |
503 | 530 | } |
504 | 531 | |
532 | + | |
533 | + /** | |
534 | + * Retorna todos os registros como objetos CoreExt_Entity retornados pela | |
535 | + * query de _getFindAllStatment() (usando consulta preparada, util para evitar sql injection). | |
536 | + * | |
537 | + * @param array $columns Atributos a serem carregados. O atributo id é sempre carregado. | |
538 | + * @param array $where Condicoes preparadas ex: array('arg1 = $1', 'arg2 = $2'); | |
539 | + * @param array $params Valor das condiçoes ($1, $2 ...) ex: array('1', '3'); | |
540 | + * @param array $orderBy | |
541 | + * @param array $addColumnIdIfNotSet Se true, adiciona a coluna 'id' caso não esteja definido no array $columns | |
542 | + * @return array | |
543 | + * @todo | |
544 | + */ | |
545 | + public function findAllUsingPreparedQuery(array $columns = array(), array $where = array(), array $params = array(), array $orderBy = array(), $addColumnIdIfNotSet = true) { | |
546 | + $list = array(); | |
547 | + | |
548 | + // Inverte chave valor, permitindo array simples como array('nome') | |
549 | + if (0 < count($columns)) { | |
550 | + $columns = array_flip($columns); | |
551 | + if (!isset($columns['id']) && $addColumnIdIfNotSet) { | |
552 | + $columns['id'] = TRUE; | |
553 | + } | |
554 | + } | |
555 | + | |
556 | + // Reseta o locale para o default (en_US) | |
557 | + $this->getLocale()->resetLocale(); | |
558 | + | |
559 | + $sql = $this->_getFindAllStatment($columns, $where, $orderBy); | |
560 | + | |
561 | + if ($this->_getDbAdapter()->execPreparedQuery($sql, $params) != false) { | |
562 | + // Retorna o locale para o usado no restante da aplicação | |
563 | + $this->getLocale()->setLocale(); | |
564 | + | |
565 | + while ($this->_getDbAdapter()->ProximoRegistro()) { | |
566 | + $list[] = $this->_createEntityObject($this->_getDbAdapter()->Tupla()); | |
567 | + } | |
568 | + } | |
569 | + | |
570 | + return $list; | |
571 | + } | |
572 | + | |
573 | + | |
505 | 574 | /** |
506 | 575 | * Retorna um registro que tenha como identificador (chave única ou composta) |
507 | 576 | * o valor dado por $pkey. |
... | ... | @@ -591,7 +660,7 @@ abstract class CoreExt_DataMapper |
591 | 660 | */ |
592 | 661 | public function delete($instance) |
593 | 662 | { |
594 | - return $this->_getDbAdapter()->Consulta($this->_getDeleteStatment($instance)); | |
663 | + return $this->_getDbAdapter()->Consulta($this->_getDeleteStatment($instance)); | |
595 | 664 | } |
596 | 665 | |
597 | 666 | /** |
... | ... | @@ -686,4 +755,4 @@ abstract class CoreExt_DataMapper |
686 | 755 | } |
687 | 756 | return $this->_locale; |
688 | 757 | } |
689 | -} | |
690 | 758 | \ No newline at end of file |
759 | +} | ... | ... |