DB.class.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?PHP
  2. /**
  3. * The Staple_DB class extends the MySQLi class to give added functionality. The Database
  4. * configuration file requires these fields:
  5. *
  6. * host - the mysql hostname of the database server to connect to.
  7. * username - MySQL username to use to connect to the database.
  8. * password - MySQL password to use to connect to the database.
  9. * db - the database to bind to.
  10. *
  11. * Database configuration file is optional. You can specify a configuration array and send it to
  12. * the get function at runtime, or you can use the set functions to configure the database
  13. * connection. If you use the set functions, you will have to manually connect to the database
  14. * by calling the connect function or constructor for this object.
  15. *
  16. * @todo add a database reconnection function for when the username, host, pw, or db changes.
  17. * @author Ironpilot
  18. * @copyright Copywrite (c) 2011, STAPLE CODE
  19. *
  20. * This file is part of the STAPLE Framework.
  21. *
  22. * The STAPLE Framework is free software: you can redistribute it and/or modify
  23. * it under the terms of the GNU Lesser General Public License as published by the
  24. * Free Software Foundation, either version 3 of the License, or (at your option)
  25. * any later version.
  26. *
  27. * The STAPLE Framework is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  29. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  30. * more details.
  31. *
  32. * You should have received a copy of the GNU Lesser General Public License
  33. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  34. */
  35. class Staple_DB extends mysqli
  36. {
  37. /**
  38. *
  39. * Holds the singleton instance for the object.
  40. * @var Staple_DB
  41. * @static
  42. */
  43. protected static $conn;
  44. /**
  45. *
  46. * Hostname for the database server
  47. * @var string
  48. */
  49. protected $host;
  50. /**
  51. *
  52. * Username to access to the database server
  53. * @var string
  54. */
  55. protected $username;
  56. /**
  57. *
  58. * Password to connect to the database
  59. * @var string
  60. */
  61. protected $password;
  62. /**
  63. *
  64. * Database name on the server
  65. * @var string
  66. */
  67. protected $db;
  68. /**
  69. * A boolean value that signifies an active connection to the database server.
  70. * @var boolean
  71. */
  72. protected $connected = false;
  73. /**
  74. * Stores the last executed SQL Statement
  75. * @var string
  76. */
  77. public static $last_query;
  78. /**
  79. * Storage for Named Database Connections
  80. * @var array[Staple_DB]
  81. */
  82. protected static $namedConnections = array();
  83. /**
  84. *
  85. * Overrides the default MySQLi constructor to add funtionality to retrieve database
  86. * settings from configuration file.
  87. *
  88. * @throws Exception
  89. */
  90. public function __construct(array $config = array())
  91. {
  92. if($this->checkConfig($config))
  93. {
  94. $this->host = $config['host'];
  95. $this->username = $config['username'];
  96. $this->password = $config['password'];
  97. $this->db = $config['db'];
  98. }
  99. elseif(!$this->isReady())
  100. {
  101. $globalSettings = Staple_Config::get('db');
  102. if($this->checkConfig($globalSettings))
  103. {
  104. $this->host = $globalSettings['host'];
  105. $this->username = $globalSettings['username'];
  106. $this->password = $globalSettings['password'];
  107. $this->db = $globalSettings['db'];
  108. }
  109. }
  110. if($this->isReady())
  111. {
  112. @parent::__construct($this->host, $this->username, $this->password, $this->db);
  113. if(isset($this->connect_error))
  114. {
  115. throw new Exception("Database Connection Error");
  116. }
  117. else
  118. {
  119. $this->connected = true;
  120. }
  121. }
  122. }
  123. public function __destruct()
  124. {
  125. //Close an open connection
  126. try{
  127. @$this->close();
  128. }
  129. catch(Exception $e){}
  130. }
  131. /**
  132. *
  133. * Creates and returns the primary database connection.
  134. * @return Staple_DB
  135. * @static
  136. */
  137. public static function get(array $conf = array())
  138. {
  139. if (!isset(self::$conn) || $conf != array()) {
  140. $c = __CLASS__;
  141. self::$conn = new $c($conf);
  142. }
  143. return self::$conn;
  144. }
  145. /**
  146. * Creates and/or returns a named database connection.
  147. * @return Staple_DB
  148. * @static
  149. */
  150. public static function getNamedConnection($name)
  151. {
  152. if (!isset(self::$namedConnections[$name])) {
  153. $c = __CLASS__;
  154. self::$namedConnections[$name] = new $c(Staple_Config::get($name));
  155. }
  156. return self::$namedConnections[$name];
  157. }
  158. /**
  159. * Overrides the MySQL connect() function to perform a check for required connection details.
  160. * connect() is an alias for mysqli->__construct().
  161. * @see mysqli::connect()
  162. */
  163. public function connect($host = NULL, $user = NULL, $password = NULL, $database = NULL, $port = NULL, $socket = NULL)
  164. {
  165. if($this->isReady())
  166. {
  167. $this->__construct();
  168. }
  169. else
  170. {
  171. throw new Exception("Database Connection Parameters Not Specified.");
  172. }
  173. }
  174. /**
  175. * (non-PHPdoc)
  176. * @see mysqli::change_user()
  177. * @return bool
  178. */
  179. public function change_user($user, $password, $database = NULL)
  180. {
  181. if(isset($database))
  182. {
  183. $this->setDb($database);
  184. }
  185. $this->setUsername($user);
  186. $this->setPassword($password);
  187. return parent::change_user($this->getUsername(), $this->password, $this->getDb());
  188. }
  189. /**
  190. * (non-PHPdoc)
  191. * @see mysqli::select_db()
  192. */
  193. public function select_db($dbname)
  194. {
  195. $this->setDb($dbname);
  196. return parent::select_db($this->getDb());
  197. }
  198. /**
  199. * (non-PHPdoc)
  200. * @see mysqli::query()
  201. * @return mysqli_result | bool
  202. */
  203. public function query($query,$resultmode = MYSQLI_STORE_RESULT)
  204. {
  205. /**
  206. * @todo add self::multi_query to this function.
  207. */
  208. if($this->connected === true)
  209. {
  210. self::$last_query = $query;
  211. return parent::query($query,$resultmode);
  212. }
  213. else
  214. {
  215. throw new Exception('No Database Connection');
  216. }
  217. }
  218. /**
  219. * @return the $host
  220. */
  221. public function getHost()
  222. {
  223. return $this->host;
  224. }
  225. /**
  226. * @param string $host
  227. */
  228. public function setHost($host)
  229. {
  230. $this->host = $host;
  231. return $this;
  232. }
  233. /**
  234. * @return the $username
  235. */
  236. public function getUsername()
  237. {
  238. return $this->username;
  239. }
  240. /**
  241. * @param string $username
  242. */
  243. public function setUsername($username)
  244. {
  245. $this->username = $username;
  246. return $this;
  247. }
  248. /**
  249. * @return the $db
  250. */
  251. public function getDb()
  252. {
  253. return $this->db;
  254. }
  255. /**
  256. * @param string $db
  257. */
  258. public function setDb($db)
  259. {
  260. $this->db = $db;
  261. return $this;
  262. }
  263. /**
  264. * Sets the database password parameter.
  265. * @param string $password
  266. */
  267. public function setPassword($password)
  268. {
  269. $this->password = $password;
  270. return $this;
  271. }
  272. /**
  273. * @return the $connected
  274. */
  275. public function getConnected()
  276. {
  277. return $this->connected;
  278. }
  279. /**
  280. * @param boolean $connected
  281. */
  282. protected function setConnected($connected)
  283. {
  284. $this->connected = (bool)$connected;
  285. return $this;
  286. }
  287. /**
  288. * @return the $last_query
  289. */
  290. public function getLastQuery()
  291. {
  292. return self::$last_query;
  293. }
  294. /**
  295. * @param string $last_query
  296. */
  297. protected function setLastQuery($last_query)
  298. {
  299. self::$last_query = $last_query;
  300. }
  301. /**
  302. * Returns any database errors that occurred on the last database query.
  303. * @return array[array];
  304. */
  305. public function getErrors()
  306. {
  307. //PHP_VERSION 5.4.0 supports the error list array.
  308. if(PHP_VERSION_ID > 50400)
  309. {
  310. return $this->error_list;
  311. }
  312. else
  313. {
  314. //Make the PHP 5.4 version manually.
  315. if(strlen($this->error) >= 1)
  316. {
  317. return array(array('errno'=>$this->errno, 'sqlstate'=>$this->sqlstate, 'error'=>$this->error));
  318. }
  319. else
  320. {
  321. return array();
  322. }
  323. }
  324. }
  325. /**
  326. *
  327. * Checks the configuration file to make sure that all required keys exist.
  328. * @param array $config
  329. * @throws Exception
  330. */
  331. protected function checkConfig(array $config)
  332. {
  333. $keys = array('host','username','password','db');
  334. foreach($keys as $value)
  335. {
  336. if(!array_key_exists($value, $config))
  337. {
  338. return false;
  339. }
  340. }
  341. return true;
  342. }
  343. /**
  344. * Checks that all required connection parameters have been set.
  345. * @return bool
  346. */
  347. protected function isReady()
  348. {
  349. $keys = array($this->host,$this->username,$this->password,$this->db);
  350. foreach($keys as $config)
  351. {
  352. if(strlen($config) < 1)
  353. {
  354. return false;
  355. }
  356. }
  357. return true;
  358. }
  359. }