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