Guest User

Untitled

a guest
Oct 4th, 2018
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. <?php
  2.  
  3. class AcessDataPDO{
  4.  
  5. private $driver;
  6. private $user;
  7. private $password;
  8. private $option;
  9.  
  10. protected $PDOconnection;
  11.  
  12. private function beginConnection(){
  13.  
  14. try {
  15. $this->PDOconnection = new PDO($this->driver, $this->user, $this->password, $this->option);
  16. } catch (Exception $e) {
  17. $tag = ' <script type="text/javascript">';
  18. $tag += "alert(\"";
  19. $tag += $e->getFile()." - Na Linha ".$e->getLine()." # ".$e->getMessage()."\")";
  20. $tag += '</script>';
  21. echo $tag;
  22. }
  23.  
  24. }
  25.  
  26. public function __construct(){
  27. require_once 'Configurador.class.php';
  28. $conf = new Configurador();
  29.  
  30. $this->driver = $conf->driver;
  31. $this->user = $conf->user;
  32. $this->password = $conf->password;
  33. $this->option = $conf->option;
  34.  
  35. $this->beginConnection();
  36.  
  37. }
  38.  
  39. public function __construct($driver,$user,$password,$option){
  40. $this->driver = $driver;
  41. $this->user = $user;
  42. $this->password = $password;
  43. $this->option = $option;
  44.  
  45. $this->beginConnection();
  46. }
  47.  
  48. private function query($type, $expression){
  49. if( $type == "INSERT"){
  50. if ($this->PDOconnection->query($expression)){
  51. return true;
  52. }else{
  53. return false;
  54. }
  55.  
  56. }elseif ( $type == "UPDATE"){
  57. if ($this->PDOconnection->query($expression)){
  58. return true;
  59. }else{
  60. return false;
  61. }
  62. }elseif ($type == "DELETE"){
  63. if ($this->PDOconnection->query($expression)){
  64. return true;
  65. }else{
  66. return false;
  67. }
  68.  
  69. }elseif ($type == "SELECT"){
  70. if ($resultSet = $this->PDOconnection->query($expression)){
  71. $answer = $resultSet->fetchObject();
  72. return $answer;
  73. }else{
  74. return false;
  75. }
  76. }
  77. }
  78.  
  79. public function select($columns,$table,$condition){
  80. $expression = "SELECT $coulmns FROM $table ";
  81. if($condition != null){
  82. $expression += "WHERE $condition";
  83. }
  84. return $this->query("SELECT", $expression);
  85. }
  86.  
  87. public function insert($table,$values,$condition){
  88. $expression = "INSERT INTO $table VALUES ($values) ";
  89. if($condition != null){
  90. $expression += "WHERE $condition";
  91. }
  92. return $this->query("INSERT", $expression);
  93. }
  94.  
  95. /**
  96. *
  97. * Enter description here ...
  98. * @param $columnsSet - coluna tem de ser um array
  99. * @param $table
  100. * @param $condition
  101. */
  102. public function update($columnsSet,$table,$condition){
  103. if(is_array($columnsSet)){
  104. $expression = "UPDATE $table SET";
  105. foreach ($columnsSet as $k => $v)
  106. $expression += " $k = $v";
  107. if($condition != null){
  108. $expression += "WHERE $condition";
  109. }
  110. return $this->query("UPDATE", $expression);
  111. }
  112. return false;
  113. }
  114.  
  115.  
  116. public function delete($table,$condition){
  117. $expression = "DELETE FROM $table ";
  118. if($condition != null){
  119. $expression += "WHERE $condition";
  120. }
  121. return $this->query("DELETE", $expression);
  122. }
  123.  
  124. private function __destruct(){
  125. $this->PDOconnection = null;
  126. }
  127. }
  128.  
  129. eu li alguns tutoriais e visando as minhas necessidades pra criar um projeto escolar simples!
  130. A pergunta q eu faço é se o fato se essa classe é mais "inteligente" ou profissional do que
  131. a seguinte classe!
  132.  
  133. <?php
  134.  
  135. class AcessDataPDO{
  136.  
  137. private $driver;
  138. private $host;
  139. private $user;
  140. private $password;
  141. private $dataBase;
  142. private $option;
  143. private $port;
  144.  
  145. protected $PDOconnection;
  146.  
  147. private function beginConnection(){
  148.  
  149. try {
  150. $this->PDOconnection = new PDO($this->driver, $this->user, $this->password, $this->option);
  151. } catch (Exception $e) {
  152. $tag = ' <script type="text/javascript">';
  153. $tag += "alert(\"";
  154. $tag += $e->getFile()." - Na Linha ".$e->getLine()." # ".$e->getMessage()."\")";
  155. $tag += '</script>';
  156. echo $tag;
  157. }
  158.  
  159. }
  160.  
  161. public function __construct(){
  162. require_once 'Configurador.class.php';
  163. $conf = new Configurador();
  164.  
  165. $this->driver = $conf->driver;
  166. $this->user = $conf->user;
  167. $this->password = $conf->password;
  168. $this->option = $conf->option;
  169.  
  170. $this->beginConnection();
  171.  
  172. }
  173.  
  174. public function __construct($driver,$user,$password,$option){
  175. $this->driver = $driver;
  176. $this->user = $user;
  177. $this->password = $password;
  178. $this->option = $option;
  179.  
  180. $this->beginConnection();
  181. }
  182.  
  183. public function query($type, $expression){
  184. if( $type == "INSERT"){
  185. if ($this->PDOconnection->query($expression)){
  186. return true;
  187. }else{
  188. return false;
  189. }
  190.  
  191. }elseif ( $type == "UPDATE"){
  192. if ($this->PDOconnection->query($expression)){
  193. return true;
  194. }else{
  195. return false;
  196. }
  197. }elseif ($type == "DELETE"){
  198. if ($this->PDOconnection->query($expression)){
  199. return true;
  200. }else{
  201. return false;
  202. }
  203.  
  204. }elseif ($type == "SELECT"){
  205. if ($resultSet = $this->PDOconnection->query($expression)){
  206. $answer = $resultSet->fetchObject();
  207. return $answer;
  208. }else{
  209. return false;
  210. }
  211. }
  212. }
  213.  
  214.  
  215. private function __destruct(){
  216. $this->PDOconnection = null;
  217. }
  218. }
Add Comment
Please, Sign In to add comment