Advertisement
Guest User

Untitled

a guest
Mar 15th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. <?php
  2.  
  3. class PDO_DataAccess
  4. {
  5. /**
  6. * Database connection link
  7. * @var \PDO
  8. */
  9. protected $link;
  10.  
  11. /**
  12. * Connection data
  13. *
  14. * @var DbConfig
  15. */
  16. protected $db_conf;
  17.  
  18.  
  19. public function __construct(PDO $pdo = null, DbConfig $config = null)
  20. {
  21. if ($pdo !== null) {
  22. $this->link = $pdo;
  23. } elseif ($config !== null) {
  24. self::getConnection($config);
  25. } else {
  26. throw new Exception(__METHOD__ . '::Configure your connection, please!');
  27. }
  28. }
  29.  
  30. private function getConnection(DbConfig $config)
  31. {
  32. $pdo_dns = 'mysql:host=' . $config->getDbHost() . ';';
  33. $pdo_dns .= ($config->getDbPort() ? 'port=' . $config->getDbPort() . ';' : '');
  34.  
  35. if ($config->getSocket()) {
  36. $pdo_dns = 'mysql:unix_socket='.$config->getSocket().';';
  37. }
  38.  
  39. $pdo_dns .= 'dbname=' . $config->getDbName();
  40.  
  41.  
  42. $pdo_options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
  43. if ($config->isPersistent()) {
  44. $pdo_options[PDO::ATTR_PERSISTENT] = true;
  45. }
  46.  
  47. $this->db_conf = $config;
  48.  
  49. $this->link = new PDO($pdo_dns, $config->getDbUser(), $config->getDbPass(), $pdo_options);
  50. }
  51. }
  52.  
  53. class DbConfig
  54. {
  55. /**
  56. * @var string
  57. */
  58. private $db_host;
  59.  
  60. /**
  61. * @var string
  62. */
  63. private $db_name;
  64.  
  65. /**
  66. * @var string
  67. */
  68. private $db_user;
  69.  
  70. /**
  71. * @var string
  72. */
  73. private $db_pass;
  74.  
  75. /**
  76. * @var string
  77. */
  78. private $table_prefix;
  79.  
  80. /**
  81. * @var integer
  82. */
  83. private $db_port;
  84.  
  85. /**
  86. * @var string
  87. */
  88. private $socket;
  89.  
  90. /**
  91. * @var boolean
  92. */
  93. private $persistent;
  94.  
  95.  
  96.  
  97. public function __construct($host, $database, $user, $pass)
  98. {
  99. $this->db_port = null; // ini_get("mysqli.default_port")
  100. // $socket = ini_get("mysqli.default_socket");
  101.  
  102. $this->setDbHost($host);
  103. $this->setDbName($database);
  104. $this->setDbUser($user);
  105. $this->setDbPass($pass);
  106. }
  107.  
  108. public function getDbHost()
  109. {
  110. return $this->db_host;
  111. }
  112.  
  113. /**
  114. * @param string $db_host
  115. * @return dbConfig
  116. */
  117. public function setDbHost($db_host)
  118. {
  119. $this->persistent = stripos($db_host, 'p:') === 0;
  120. $host = $this->persistent ? substr($db_host, 2) : $db_host;
  121.  
  122. if (strpos($host, ':') !== false) {
  123. list($host, $port) = explode(':', $host);
  124.  
  125. // PHP may not honor the port number if connecting to 'localhost'
  126. if ($port && is_numeric($port)) {
  127. if (!strcasecmp($host, 'localhost'))
  128. // XXX: Looks like PHP gethostbyname() is IPv4 only
  129. $host = gethostbyname($host);
  130.  
  131. $this->setDbPort($port);
  132. } elseif ($port) {
  133. $this->setSocket($port);
  134. }
  135. }
  136.  
  137. $this->db_host = $host;
  138.  
  139. return $this;
  140. }
  141.  
  142. public function getDbName()
  143. {
  144. return $this->db_name;
  145. }
  146.  
  147. /**
  148. * @param string $db_name
  149. * @return dbConfig
  150. */
  151. public function setDbName($db_name)
  152. {
  153. $this->db_name = $db_name;
  154.  
  155. return $this;
  156. }
  157.  
  158. public function getDbUser()
  159. {
  160. return $this->db_user;
  161. }
  162.  
  163. /**
  164. * @param string $db_user
  165. * @return dbConfig
  166. */
  167. public function setDbUser($db_user)
  168. {
  169. $this->db_user = $db_user;
  170.  
  171. return $this;
  172. }
  173.  
  174. public function getDbPass()
  175. {
  176. return $this->db_pass;
  177. }
  178.  
  179. /**
  180. * @param string $db_pass
  181. * @return dbConfig
  182. */
  183. public function setDbPass($db_pass)
  184. {
  185. $this->db_pass = $db_pass;
  186.  
  187. return $this;
  188. }
  189.  
  190. public function getTablePrefix()
  191. {
  192. return $this->table_prefix;
  193. }
  194.  
  195. /**
  196. * @param string $table_prefix
  197. * @return DbConfig
  198. */
  199. public function setTablePrefix($table_prefix)
  200. {
  201. $this->table_prefix = $table_prefix;
  202.  
  203. return $this;
  204. }
  205.  
  206. public function getDbPort()
  207. {
  208. return $this->db_port;
  209. }
  210.  
  211. /**
  212. * @param int $db_port
  213. * @return DbConfig
  214. */
  215. public function setDbPort($db_port)
  216. {
  217. $this->db_port = (int) $db_port;
  218.  
  219. return $this;
  220. }
  221.  
  222. public function getSocket()
  223. {
  224. return $this->socket;
  225. }
  226.  
  227. /**
  228. * @param string $socket
  229. * @return DbConfig
  230. */
  231. public function setSocket($socket)
  232. {
  233. $this->socket = $socket;
  234.  
  235. return $this;
  236. }
  237.  
  238. public function isPersistent()
  239. {
  240. return $this->persistent;
  241. }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement