Advertisement
Guest User

Untitled

a guest
Mar 5th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.47 KB | None | 0 0
  1. <?php
  2. class PDOConnection{
  3. private $instance;
  4.  
  5. private $dsn;
  6. private $username;
  7. private $password;
  8. private $options = [];
  9.  
  10. /**
  11. * constructor
  12. *
  13. * @param $dsn
  14. * @param $username
  15. * @param $password
  16. * @param array $options
  17. */
  18. public function __construct($dsn, $username, $password, array $options = []) {
  19. $this->dsn = $dsn;
  20. $this->username = $username;
  21. $this->password = $password;
  22. $this->options = $options;
  23. }
  24.  
  25. /**
  26. * Setting attributes on instance
  27. *
  28. * @param $name
  29. * @param $value
  30. * @return mixed|void
  31. */
  32. public function setAttribute($name, $value) {
  33. if(!$this->instance instanceof PDO) {
  34. throw new LogicException('Cannot set PDO attribute. Please make sure you are connected using the connect() method.');
  35. }
  36. if($this->instance->setAttribute($name, $value) === false) {
  37. throw new LogicException('Could not set PDO attribute: ' . $name);
  38. }
  39. }
  40.  
  41. /**
  42. * Setting options
  43. *
  44. * @param $name
  45. * @param $value
  46. */
  47. public function setOption($name, $value) {
  48. $this->options[$name] = $value;
  49. }
  50.  
  51. /**
  52. * getting connection
  53. * @return PDO
  54. */
  55. public function getConnection() {
  56. if(!$this->instance instanceof PDO) {
  57. throw new LogicException('No database connection established.');
  58. }
  59. return $this->instance;
  60.  
  61. }
  62.  
  63. /**
  64. * connecting to database
  65. *
  66. * @throws ErrorException
  67. */
  68. public function connect() {
  69. try {
  70. $this->instance = new PDO($this->dsn, $this->username, $this->password, $this->options);
  71. }catch(PDOException $exception) {
  72. throw new ErrorException('Could not connect to the database!', null, $exception);
  73. }
  74. }
  75.  
  76. /**
  77. * disconnecting from database
  78. */
  79. public function disconnect() {
  80. $this->instance = null;
  81. }
  82. }
  83.  
  84. class database{
  85. private $dbh;
  86. private $executed = false;
  87. private $stmt;
  88.  
  89. /**
  90. * constructor
  91. **/
  92. public function __construct(){
  93. $conf = Config::getInstance();
  94. switch($conf->db_type){
  95. case "mysql":
  96. $dsn = "mysql:host=$conf->db_host;port=$conf->db_port;dbname=$conf->db_name";
  97. break;
  98. case "sqlite":
  99. $dsn = "sqlite:$conf->db_path;";
  100. break;
  101. case "postgresql":
  102. $dsn = "pgsql:host=$conf->db_host;port=$conf->db_port;dbname=$conf->db_name";
  103. break;
  104. default:
  105. $dsn = "mysql:host=$conf->db_host;port=$conf->db_port;dbname=$conf->db_name";
  106. }
  107. $dsn .= ';charset=utf8';
  108. $connection = new PDOConnection($dsn, $conf->db_user, $conf->db_pass, array(
  109. PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
  110. PDO::ATTR_PERSISTENT => true,
  111. PDO::ATTR_TIMEOUT => 60*60*60*60,
  112. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  113. ));
  114. $connection->connect();
  115. $this->dbh = $connection->getConnection();
  116. }
  117.  
  118. /**
  119. * Provides access to the application PDO instance.
  120. *
  121. * @return PDO
  122. */
  123. public function pdo() {
  124. return $this->dbh;
  125. }
  126.  
  127. /**
  128. * set query statement
  129. *
  130. * @param $query
  131. *
  132. * @return $this
  133. */
  134. public function query($query){
  135. /** @noinspection PhpUndefinedMethodInspection */
  136. $this->stmt = $this->dbh->prepare($query);
  137. return $this;
  138. }
  139.  
  140. /**
  141. * binding database
  142. *
  143. * @param $param
  144. * @param $value
  145. * @param null $type
  146. *
  147. * @return $this
  148. */
  149. public function bind($param, $value, $type = null){
  150. if (is_null($type)) {
  151. switch (true) {
  152. case is_string($value):
  153. $type = PDO::PARAM_STR;
  154. break;
  155. case is_int($value):
  156. $type = PDO::PARAM_INT;
  157. break;
  158. case is_bool($value):
  159. $type = PDO::PARAM_BOOL;
  160. break;
  161. case is_null($value):
  162. $type = PDO::PARAM_NULL;
  163. break;
  164. default:
  165. $type = PDO::PARAM_STR;
  166. }
  167. }
  168. /** @noinspection PhpUndefinedMethodInspection */
  169. $this->stmt->bindValue($param, $value, $type);
  170. return $this;
  171. }
  172.  
  173. /**
  174. * executing query statement
  175. *
  176. * @return $this
  177. */
  178. public function execute(){
  179. /** @noinspection PhpUndefinedMethodInspection */
  180. $this->stmt->execute();
  181. $this->executed = true;
  182. return $this;
  183. }
  184.  
  185. /**
  186. * fetching all result
  187. *
  188. * @param int $fetch
  189. *
  190. * @param null $class
  191. * @param array $args
  192. * @return mixed
  193. */
  194. public function FetchAll($fetch = PDO::FETCH_ASSOC, $class = null, array $args = []){
  195. $this->execute();
  196. if(!is_null($class) && in_array($fetch, [PDO::FETCH_CLASS, PDO::FETCH_OBJ])) {
  197. /** @noinspection PhpUndefinedMethodInspection */
  198. return $this->stmt->fetchAll(PDO::FETCH_CLASS, $class, $args);
  199. }
  200. /** @noinspection PhpUndefinedMethodInspection */
  201. return $this->stmt->fetchAll($fetch);
  202. }
  203.  
  204. /**
  205. * fetching first result only
  206. *
  207. * @param int $fetch
  208. *
  209. * @param null $class
  210. * @param array $args
  211. * @return mixed
  212. */
  213. public function FetchOne($fetch = PDO::FETCH_ASSOC, $class = null, array $args = []){
  214. $this->execute();
  215. if(!is_null($class) && in_array($fetch, [PDO::FETCH_CLASS, PDO::FETCH_OBJ])) {
  216. /** @noinspection PhpUndefinedMethodInspection */
  217. return $this->stmt->fetchObject($class, $args);
  218. }
  219. /** @noinspection PhpUndefinedMethodInspection */
  220. return $this->stmt->fetch($fetch);
  221. }
  222.  
  223. /**
  224. * fetching column
  225. *
  226. * @param int $columnNumber
  227. *
  228. * @return mixed
  229. */
  230. public function FetchColumn($columnNumber=0){
  231. $this->execute();
  232. /** @noinspection PhpUndefinedMethodInspection */
  233. return $this->stmt->fetchColumn($columnNumber);
  234. }
  235.  
  236. /**
  237. * counting rows
  238. *
  239. * @return mixed
  240. */
  241. public function rowCount(){
  242. /** @noinspection PhpUndefinedMethodInspection */
  243. return $this->stmt->rowCount();
  244. }
  245.  
  246. /**
  247. * counting columns
  248. * @return mixed
  249. */
  250. public function columnCount(){
  251. /** @noinspection PhpUndefinedMethodInspection */
  252. return $this->stmt->columnCount();
  253. }
  254.  
  255. /**
  256. * getting last inserted ID
  257. * @return string
  258. */
  259. public function lastInsertId(){
  260. return $this->dbh->lastInsertId();
  261. }
  262.  
  263. /**
  264. * starting transaction
  265. *
  266. * @return bool
  267. */
  268. public function beginTransaction(){
  269. return $this->dbh->beginTransaction();
  270. }
  271.  
  272. /**
  273. * ending transaction
  274. * @return bool
  275. */
  276. public function endTransaction(){
  277. /** @noinspection PhpUndefinedMethodInspection */
  278. return $this->dbh->commit();
  279. }
  280.  
  281. /**
  282. * transaction savepoint
  283. *
  284. * @param $savepoint_name
  285. *
  286. * @return $this
  287. */
  288. public function TransactionSavepoint($savepoint_name){
  289. $this->query("SAVEPOINT :savepointname");
  290. $this->bind(':savepointname',$savepoint_name);
  291. $this->execute();
  292. return $this;
  293. }
  294.  
  295. /**
  296. * canceling transaction
  297. *
  298. * @return bool
  299. */
  300. public function cancelTransaction(){
  301. /** @noinspection PhpUndefinedMethodInspection */
  302. return $this->dbh->rollBack();
  303. }
  304.  
  305. /**
  306. * debuging dump parameters
  307. *
  308. * @return mixed
  309. */
  310. public function debugDumpParams(){
  311. /** @noinspection PhpUndefinedMethodInspection */
  312. return $this->stmt->debugDumpParams();
  313. }
  314.  
  315. /**
  316. * Reset the execution flag.
  317. */
  318. public function closeCursor() {
  319. /** @noinspection PhpUndefinedMethodInspection */
  320. $this->stmt->closeCursor();
  321. $this->executed = false;
  322. }
  323. }
  324.  
  325. $db = new database();
  326. $info = $db->query("SEKECT * FROM `table`")->FetchAll();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement