Guest User

Untitled

a guest
Nov 24th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. <?php
  2.  
  3. namespace CoreBase;
  4.  
  5.  
  6. use CoreCrudRead;
  7.  
  8. use PDO;
  9.  
  10. abstract class Model
  11. {
  12.  
  13. protected $table;
  14.  
  15. public function Read($termo = null, $parseString = null)
  16. {
  17. $Read = new Read();
  18. $Read->ExeRead($this->table, "$termo", $parseString);
  19. return $Read->getResult();
  20. }
  21.  
  22.  
  23.  
  24. }
  25.  
  26. <?php
  27.  
  28. namespace CoreCrud;
  29.  
  30. use PDO;
  31. use PDOException;
  32. use PDOStatement;
  33. use CoreBaseDatabase;
  34.  
  35.  
  36. class Read extends Database {
  37.  
  38. private $Select;
  39. private $Places;
  40. private $Result;
  41. private $Close;
  42.  
  43.  
  44. /** @var PDOStatement */
  45. private $Read;
  46.  
  47. /** @var PDO */
  48. private $Conn;
  49.  
  50. /**
  51. * <b>Exe Read:</b> Executa uma leitura simplificada com Prepared Statments. Basta informar o nome da tabela,
  52. * os termos da seleção e uma analize em cadeia (ParseString) para executar.
  53. * @param STRING $Tabela = Nome da tabela
  54. * @param STRING $Termos = WHERE | ORDER | LIMIT :limit | OFFSET :offset
  55. * @param STRING $ParseString = link={$link}&link2={$link2}
  56. */
  57. public function ExeRead($Tabela, $Termos = null, $ParseString = null) {
  58. if (!empty($ParseString)):
  59. parse_str($ParseString, $this->Places);
  60. endif;
  61.  
  62. $this->Select = "SELECT * FROM {$Tabela} {$Termos}";
  63. $this->Execute();
  64.  
  65. }
  66.  
  67.  
  68. /**
  69. * <b>Obter resultado:</b> Retorna um array com todos os resultados obtidos. Envelope primário númérico. Para obter
  70. * um resultado chame o índice getResult()[0]!
  71. * @return ARRAY $this = Array ResultSet
  72. */
  73. public function getResult() {
  74. return $this->Result;
  75. }
  76.  
  77. /**
  78. * <b>Contar Registros: </b> Retorna o número de registros encontrados pelo select!
  79. * @return INT $Var = Quantidade de registros encontrados
  80. */
  81. public function getRowCount() {
  82. return $this->Read->rowCount();
  83. }
  84.  
  85. /**
  86. * <b>Full Read:</b> Executa leitura de dados via query que deve ser montada manualmente para possibilitar
  87. * seleção de multiplas tabelas em uma única query!
  88. * @param STRING $Query = Query Select Syntax
  89. * @param STRING $ParseString = link={$link}&link2={$link2}
  90. */
  91.  
  92. public function FullRead($Query, $ParseString = null) {
  93. $this->Select = (string) $Query;
  94. if (!empty($ParseString)):
  95. parse_str($ParseString, $this->Places);
  96. endif;
  97. $this->Execute();
  98. }
  99.  
  100.  
  101. public function setPlaces($ParseString) {
  102. parse_str($ParseString, $this->Places);
  103. $this->Execute();
  104. }
  105.  
  106. /**
  107. * ****************************************
  108. * *********** PRIVATE METHODS ************
  109. * ****************************************
  110. */
  111. //Obtém o PDO e Prepara a query
  112. private function Connect() {
  113. $this->Conn = parent::getConn();
  114. $this->Read = $this->Conn->prepare($this->Select);
  115. $this->Read->setFetchMode(PDO::FETCH_OBJ);
  116. }
  117.  
  118. //Cria a sintaxe da query para Prepared Statements
  119. private function getSyntax() {
  120. if ($this->Places):
  121. foreach ($this->Places as $Vinculo => $Valor):
  122. if ($Vinculo == 'limit' || $Vinculo == 'offset'):
  123. $Valor = (int) $Valor;
  124. endif;
  125. $this->Read->bindValue(":{$Vinculo}", $Valor, ( is_int($Valor) ? PDO::PARAM_INT : PDO::PARAM_STR));
  126. endforeach;
  127. endif;
  128. }
  129.  
  130. //Obtém a Conexão e a Syntax, executa a query!
  131. private function Execute() {
  132. $this->Connect();
  133. try {
  134. $this->getSyntax();
  135. $this->Read->execute();
  136. $this->Result = $this->Read->fetchAll();
  137. $this->Close = $this->Read->closeCursor();
  138. } catch (PDOException $e) {
  139. $this->Result = null;
  140. echo "<b>Erro ao Ler:</b> {$e->getMessage()}" . $e->getCode() ;
  141. }
  142. }
  143.  
  144. }
Add Comment
Please, Sign In to add comment