Guest User

Untitled

a guest
Aug 7th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 KB | None | 0 0
  1. <?php
  2.  
  3. class DB {
  4. /**
  5. * @var DB
  6. */
  7. private static $instancia = NULL;
  8.  
  9. /**
  10. * @var Mysql link identifier
  11. */
  12. private $dbLink = NULL;
  13. /**
  14. * @var string
  15. */
  16. private $user = '';
  17. /**
  18. * @var string
  19. */
  20. private $password = '';
  21. /**
  22. * @var string
  23. */
  24. private $dbName = '';
  25. /**
  26. * @var string
  27. */
  28. private $host = '';
  29.  
  30. /**
  31. * @var array
  32. */
  33. private $lastResult = NULL;
  34. /**
  35. * @var string
  36. */
  37. private $lastError = '';
  38. /**
  39. * @var int
  40. */
  41. private $numRows = 0;
  42. /**
  43. * @var int
  44. */
  45. private $affectedRows = 0;
  46. /**
  47. * @var int
  48. */
  49. private $lastInsertId = 0;
  50.  
  51. /**
  52. * @var int
  53. */
  54. private $transacoes = 0;
  55. /**
  56. * @var bool
  57. */
  58. private $forceRollback = false;
  59.  
  60. /**
  61. * @var int
  62. */
  63. private $timeInicio = 0;
  64. /*
  65. * @var int
  66. */
  67. private $timeFim = 0;
  68.  
  69. /**
  70. * @param string $host
  71. * @param string $user
  72. * @param string $password
  73. * @param string $dbName
  74. */
  75. public function __construct($host, $user, $password, $dbName) {
  76. $this->host = $host;
  77. $this->user = $user;
  78. $this->password = $password;
  79. $this->dbName = $dbName;
  80.  
  81. try {
  82. $this->dbLink = new PDO('mysql:host=' . $this->host . ';dbname=' . $this->dbName, $this->user, $this->password);
  83. }
  84. catch (PDOException $e) {
  85. throw new Exception("Não foi possível conectar ao banco de dados. [" . $e->getCode() . "]");
  86. }
  87.  
  88. $this->query('SET NAMES utf8');
  89.  
  90. self::$instancia = $this;
  91. }
  92.  
  93. /**
  94. * @param string $query
  95. * @param array $array
  96. * @param bool $throwError
  97. * @return int
  98. */
  99. public function query($query, $array = array(), $throwError = true) {
  100. $this->flush();
  101.  
  102. $this->timeInicio = microtime(true);
  103.  
  104. $statement = $this->dbLink->prepare($query);
  105. //$this->result = $statement->execute($array);
  106. if (!$statement || !$statement->execute($array)) {
  107. $this->flush();
  108. $this->error($statement->errorInfo(), $throwError);
  109. }
  110.  
  111. if (preg_match('#^ \s* (insert|delete|update|replace) \s #ix', $query)) {
  112. $this->affectedRows = $statement->rowCount();
  113.  
  114. if (preg_match("/^\\s*(insert|replace) /i", $query)) {
  115. $this->lastInsertId = $this->dbLink->lastInsertId();
  116. }
  117. $this->timeFimo = microtime();
  118. return $this->affectedRows;
  119. } else {
  120. $this->lastResult = $statement->fetchAll(PDO::FETCH_ASSOC);
  121. $this->numRows = count($this->lastResult);
  122. $this->timeFim = microtime(true);
  123. return $this->numRows;
  124. }
  125. }
  126.  
  127. /**
  128. * @return bool
  129. */
  130. public function beginTransaction() {
  131. if ($this->transacoes == 0) {
  132. $this->transacoes = $this->transacoes + 1;
  133. return $this->dbLink->beginTransaction();
  134. }
  135. $this->transacoes = $this->transacoes + 1;
  136. return false;
  137. }
  138.  
  139. /**
  140. * @return bool
  141. */
  142. public function commit() {
  143. if ($this->transacoes == 1) {
  144. if ($this->forceRollback) {
  145. return rollback();
  146. }
  147. $this->transacoes = 0;
  148. $this->forceRollback = false;
  149. return $this->dbLink->commit();
  150. }
  151. $this->transacoes = $this->transacoes - 1;
  152. return false;
  153. }
  154.  
  155. /**
  156. * @return bool
  157. */
  158. public function rollBack() {
  159. $this->forceRollback = true;
  160. if ($this->transacoes == 1) {
  161. $this->transacoes = 0;
  162. $this->forceRollback = false;
  163. return $this->dbLink->rollBack();
  164. }
  165. $this->transacoes = $this->transacoes - 1;
  166. return false;
  167. }
  168.  
  169. /**
  170. *
  171. */
  172. private function flush() {
  173. $this->lastResult = NULL;
  174. $this->lastError = NULL;
  175.  
  176. $this->numRows = 0;
  177. $this->affectedRows = 0;
  178. $this->lastInsertId = 0;
  179.  
  180. $this->timeInicio = 0;
  181. $this->timeFim = 0;
  182.  
  183. $this->lastError = '';
  184. }
  185.  
  186. /**
  187. * @param array $errorInfo
  188. * @param bool $throwError
  189. */
  190. private function error($errorInfo, $throwError = true) {
  191. $this->lastError = $errorInfo[1] . ": " . $errorInfo[2];
  192. if ($throwError)
  193. throw new Exception($this->lastError);
  194. }
  195.  
  196. /**
  197. * @return DB
  198. */
  199. public static function getInstancia() {
  200. return self::$instancia;
  201. }
  202.  
  203. /**
  204. * @return int
  205. */
  206. public function getAffectedRows() {
  207. return $this->affectedRows;
  208. }
  209.  
  210. /**
  211. * @return Mysql
  212. */
  213. public function getDbLink() {
  214. return $this->dbLink;
  215. }
  216.  
  217. /**
  218. * @return string
  219. */
  220. public function getDbName() {
  221. return $this->dbName;
  222. }
  223.  
  224. /**
  225. * @return string
  226. */
  227. public function getHost() {
  228. return $this->host;
  229. }
  230.  
  231. /**
  232. * @return string
  233. */
  234. public function getLastError() {
  235. return $this->lastError;
  236. }
  237.  
  238. /**
  239. * @return int
  240. */
  241. public function getLastInsertId() {
  242. return $this->lastInsertId;
  243. }
  244.  
  245. /**
  246. * @return array
  247. */
  248. public function getLastResult() {
  249. return $this->lastResult;
  250. }
  251.  
  252. /**
  253. * @return int
  254. */
  255. public function getNumRows() {
  256. return $this->numRows;
  257. }
  258.  
  259. /**
  260. * @return string
  261. */
  262. public function getPassword() {
  263. return $this->password;
  264. }
  265.  
  266. /**
  267. * @return string
  268. */
  269. public function getUser() {
  270. return $this->user;
  271. }
  272.  
  273. /**
  274. * @return int
  275. */
  276. public function getQueryExecTime() {
  277. return $this->timeFim - $this->timeInicio;
  278. }
  279. }
  280.  
  281. ?>
Add Comment
Please, Sign In to add comment