Просмотр исходного кода

Adding database connector

Currently uses an older database connector class, to be customized.
Ecconia 7 лет назад
Родитель
Сommit
f3ab59584c
4 измененных файлов с 135 добавлено и 0 удалено
  1. 4 0
      .gitignore
  2. 8 0
      conf/db/sample-credentials.php
  3. 104 0
      src/DB/Database.php
  4. 19 0
      src/DB/DatabasePool.php

+ 4 - 0
.gitignore

@@ -1,3 +1,7 @@
 #Composer and bibs
 /vendor/
 composer.lock
+
+#Database credentials:
+/conf/db/root.php
+/conf/db/user.php

+ 8 - 0
conf/db/sample-credentials.php

@@ -0,0 +1,8 @@
+<?php
+
+return [
+	'username' => 'root',
+	'password' => 'password',
+	'host' => 'localhost',
+	'database' => 'your-db'
+];

+ 104 - 0
src/DB/Database.php

@@ -0,0 +1,104 @@
+<?php
+	namespace RS\DB;
+	
+	use PDO;
+	
+	/**
+	 * Database
+	 * Access databases using PDO and MySQL. Supports prepared statements.
+	 * 
+	 * @package de.it-talent.QuickTalk.lib.base
+	 * @author GIDIX
+	 */
+	class Database {
+		private $db;
+		public $lastQuery;
+
+		public function __construct($host, $username, $password, $database) {
+			$this->db = new PDO('mysql:host=' . $host . ';dbname=' . $database . ';charset=utf8', $username, $password);
+			$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+			$this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
+		}
+
+		/**
+		 * 	Send a query.
+		 *  This method supports prepared statements. Just write ? or use placeholders, like ':id' in your
+		 *  $sql and provide $args with its values.
+		 * 
+		 *  @param string $sql Query to send.
+		 *  @param array $args Params to bind, if preparing.
+		 *  
+		 *  @return PDOStatement PDO Statement
+		 */
+		public function query($sql, array $args = null) {
+			try {
+				if (!is_null($args)) {
+					$this->lastQuery = $this->db->prepare($sql);
+					$this->lastQuery->execute($args);
+				} else {
+					$this->lastQuery = $this->db->query($sql);
+				}
+
+				return $this->lastQuery;
+			} catch (PDOException $e) {
+				$this->error($e->getMessage(), $sql, $e->getCode());
+			}
+		}
+
+		/**
+		 * 	Prepare a statement and return it without executing.
+		 * 
+		 *  @param string $query Query to send.
+		 */
+		public function prepare($sql) {
+			try {
+				return $this->db->prepare($sql);
+			} catch (PDOException $e) {
+				$this->error($e->getMessage(), $sql, $e->getCode());
+			}
+		}
+
+		public function fetchObject(\PDOStatement $stmt = null) {
+			if (!is_null($stmt)) {
+				return $stmt->fetch(PDO::FETCH_OBJ);
+			} else {
+				return $this->lastQuery->fetch(PDO::FETCH_OBJ);
+			}
+		}
+
+		public function numRows(\PDOStatement $stmt = null) {
+			if (!is_null($stmt)) {
+				return $stmt->rowCount();
+			} else {
+				return $this->lastQuery->rowCount();
+			}
+		}
+
+		public function insertID() {
+			return $this->db->lastInsertId();
+		}
+
+		public function beginTransaction() {
+			return $this->db->beginTransaction();
+		}
+
+		public function commit() {
+			return $this->db->commit();
+		}
+
+		public function rollback() {
+			return $this->db->rollBack();
+		}
+
+		protected function error($msg, $sql, $code) {
+			//Functions::log(Functions::LOG_ERROR, $msg . '<br /><br /><code class="important">' . $sql . '</code>');
+			//TODO: Throw exception!
+			die('
+				<h1>Database Error ('.$code.')</h1>
+
+				'.(!is_null($sql) ? '<code>' . $sql . '</code>' : '').'
+				<p>'.$msg.'</p>
+			');
+		}
+	}
+?>

+ 19 - 0
src/DB/DatabasePool.php

@@ -0,0 +1,19 @@
+<?php
+
+namespace RS\DB;
+
+use RS\DB\Database;
+
+class DatabasePool {
+	private static $db;
+
+	public static function getDB() {
+		if(!self::$db) {
+			//TODO: Root PATH class
+			$cred = require __DIR__ . '/../../conf/db/user.php';
+			self::$db = new Database($cred['host'], $cred['username'], $cred['password'], $cred['database']);
+		}
+
+		return self::$db;
+	}
+}