PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/migrations/lib/helpers/db_helper.php

https://github.com/mrbmc/erector
PHP | 338 lines | 219 code | 12 blank | 107 comment | 25 complexity | 5317130c26dddd7b60b96216ffef5d89 MD5 | raw file
  1. <?php
  2. /**
  3. * This file houses the MpmDbHelper class.
  4. *
  5. * @package mysql_php_migrations
  6. * @subpackage Helpers
  7. * @license http://www.opensource.org/licenses/bsd-license.php The New BSD License
  8. * @link http://code.google.com/p/mysql-php-migrations/
  9. */
  10. /**
  11. * The MpmDbHelper class is used to fetch database objects (PDO or Mysqli right now) and perform basic database actions.
  12. *
  13. * @package mysql_php_migrations
  14. * @subpackage Helpers
  15. */
  16. class MpmDbHelper
  17. {
  18. /**
  19. * Returns the correct database object based on the database configuration file.
  20. *
  21. * @throws Exception if database configuration file is missing or method is incorrectly defined
  22. *
  23. * @uses MpmDbHelper::getPdoObj()
  24. * @uses MpmDbHelper::getMysqliObj()
  25. * @uses MpmDbHelper::getMethod()
  26. * @uses MPM_METHOD_PDO
  27. * @uses MPM_METHOD_MYSQLI
  28. *
  29. * @return object
  30. */
  31. static public function getDbObj()
  32. {
  33. switch (MpmDbHelper::getMethod())
  34. {
  35. case MPM_METHOD_PDO:
  36. return MpmDbHelper::getPdoObj();
  37. case MPM_METHOD_MYSQLI:
  38. return MpmDbHelper::getMysqliObj();
  39. default:
  40. throw new Exception('Unknown database connection method defined in database configuration.');
  41. }
  42. }
  43. /**
  44. * Returns a PDO object with connection in place.
  45. *
  46. * @throws MpmDatabaseConnectionException if unable to connect to the database
  47. *
  48. * @return PDO
  49. */
  50. static public function getPdoObj()
  51. {
  52. $pdo_settings = array
  53. (
  54. PDO::ATTR_PERSISTENT => true,
  55. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  56. PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true
  57. );
  58. $db_config = $GLOBALS['db_config'];
  59. return new PDO("mysql:host={$db_config->host};port={$db_config->port};dbname={$db_config->name}", $db_config->user, $db_config->pass, $pdo_settings);
  60. }
  61. /**
  62. * Returns an ExceptionalMysqli object with connection in place.
  63. *
  64. * @throws MpmDatabaseConnectionException if unable to connect to the database
  65. *
  66. * @return ExceptionalMysqli
  67. */
  68. static public function getMysqliObj()
  69. {
  70. $db_config = $GLOBALS['db_config'];
  71. return new ExceptionalMysqli($db_config->host, $db_config->user, $db_config->pass, $db_config->name, $db_config->port);
  72. }
  73. /**
  74. * Returns the correct database connection method as set in the database configuration file.
  75. *
  76. * @throws Exception if database configuration file is missing
  77. *
  78. * @return int
  79. */
  80. static public function getMethod()
  81. {
  82. if (!isset($GLOBALS['db_config']))
  83. {
  84. throw new Exception('Missing database configuration.');
  85. }
  86. $db_config = $GLOBALS['db_config'];
  87. return $db_config->method;
  88. }
  89. /**
  90. * Performs a query; $sql should be a SELECT query that returns exactly 1 row of data; returns an object that contains the row
  91. *
  92. * @uses MpmDbHelper::getDbObj()
  93. * @uses MpmDbHelper::getMethod()
  94. * @uses MPM_METHOD_PDO
  95. * @uses MPM_METHOD_MYSQLI
  96. *
  97. * @param string $sql a SELECT query that returns exactly 1 row of data
  98. * @param object $db a PDO or ExceptionalMysqli object that can be used to run the query
  99. *
  100. * @return obj
  101. */
  102. static public function doSingleRowSelect($sql, &$db = null)
  103. {
  104. try
  105. {
  106. if ($db == null)
  107. {
  108. $db = MpmDbHelper::getDbObj();
  109. }
  110. switch (MpmDbHelper::getMethod())
  111. {
  112. case MPM_METHOD_PDO:
  113. $stmt = $db->query($sql);
  114. $obj = $stmt->fetch(PDO::FETCH_OBJ);
  115. return $obj;
  116. case MPM_METHOD_MYSQLI:
  117. $stmt = $db->query($sql);
  118. $obj = $stmt->fetch_object();
  119. return $obj;
  120. default:
  121. throw new Exception('Unknown method defined in database configuration.');
  122. }
  123. }
  124. catch (Exception $e)
  125. {
  126. echo "\n\nError: ", $e->getMessage(), "\n\n";
  127. exit;
  128. }
  129. }
  130. /**
  131. * Performs a SELECT query
  132. *
  133. * @uses MpmDbHelper::getDbObj()
  134. * @uses MpmDbHelper::getMethod()
  135. * @uses MPM_METHOD_PDO
  136. * @uses MPM_METHOD_MYSQLI
  137. *
  138. * @param string $sql a SELECT query
  139. *
  140. * @return array
  141. */
  142. static public function doMultiRowSelect($sql)
  143. {
  144. try
  145. {
  146. $db = MpmDbHelper::getDbObj();
  147. $results = array();
  148. switch (MpmDbHelper::getMethod())
  149. {
  150. case MPM_METHOD_PDO:
  151. $stmt = $db->query($sql);
  152. while ($obj = $stmt->fetch(PDO::FETCH_OBJ))
  153. {
  154. $results[] = $obj;
  155. }
  156. return $results;
  157. case MPM_METHOD_MYSQLI:
  158. $stmt = $db->query($sql);
  159. while($obj = $stmt->fetch_object())
  160. {
  161. $results[] = $obj;
  162. }
  163. return $results;
  164. default:
  165. throw new Exception('Unknown method defined in database configuration.');
  166. }
  167. }
  168. catch (Exception $e)
  169. {
  170. echo "\n\nError: ", $e->getMessage(), "\n\n";
  171. exit;
  172. }
  173. }
  174. /**
  175. * Checks to make sure everything is in place to be able to use the migrations tool.
  176. *
  177. * @uses MpmDbHelper::getMethod()
  178. * @uses MpmDbHelper::getPdoObj()
  179. * @uses MpmDbHelper::getMysqliObj()
  180. * @uses MpmDbHelper::getMethod()
  181. * @uses MpmDbHelper::checkForDbTable()
  182. * @uses MpmCommandLineWriter::getInstance()
  183. * @uses MpmCommandLineWriter::addText()
  184. * @uses MpmCommandLineWriter::write()
  185. * @uses MPM_METHOD_PDO
  186. * @uses MPM_METHOD_MYSQLI
  187. *
  188. * @return void
  189. */
  190. static public function test()
  191. {
  192. $problems = array();
  193. if (!file_exists(MPM_PATH . '/config/db_config.php'))
  194. {
  195. $problems[] = 'You have not yet run the init command. You must run this command before you can use any other commands.';
  196. }
  197. else
  198. {
  199. switch (MpmDbHelper::getMethod())
  200. {
  201. case MPM_METHOD_PDO:
  202. if (!class_exists('PDO'))
  203. {
  204. $problems[] = 'It does not appear that the PDO extension is installed.';
  205. }
  206. $drivers = PDO::getAvailableDrivers();
  207. if (!in_array('mysql', $drivers))
  208. {
  209. $problems[] = 'It appears that the mysql driver for PDO is not installed.';
  210. }
  211. if (count($problems) == 0)
  212. {
  213. try
  214. {
  215. $pdo = MpmDbHelper::getPdoObj();
  216. }
  217. catch (Exception $e)
  218. {
  219. $problems[] = 'Unable to connect to the database: ' . $e->getMessage();
  220. }
  221. }
  222. break;
  223. case MPM_METHOD_MYSQLI:
  224. if (!class_exists('mysqli'))
  225. {
  226. $problems[] = "It does not appear that the mysqli extension is installed.";
  227. }
  228. if (count($problems) == 0)
  229. {
  230. try
  231. {
  232. $mysqli = MpmDbHelper::getMysqliObj();
  233. }
  234. catch (Exception $e)
  235. {
  236. $problems[] = "Unable to connect to the database: " . $e->getMessage();
  237. }
  238. }
  239. break;
  240. }
  241. if (!MpmDbHelper::checkForDbTable())
  242. {
  243. $problems[] = 'Migrations table not found in your database. Re-run the init command.';
  244. }
  245. if (count($problems) > 0)
  246. {
  247. $obj = MpmCommandLineWriter::getInstance();
  248. $obj->addText("It appears there are some problems:");
  249. $obj->addText("\n");
  250. foreach ($problems as $problem)
  251. {
  252. $obj->addText($problem, 4);
  253. $obj->addText("\n");
  254. }
  255. $obj->write();
  256. exit;
  257. }
  258. }
  259. }
  260. /**
  261. * Checks whether or not the mpm_migrations database table exists.
  262. *
  263. * @uses MpmDbHelper::getDbObj()
  264. * @uses MpmDbHelper::getMethod()
  265. * @uses MPM_METHOD_PDO
  266. * @uses MPM_METHOD_MYSQLI
  267. *
  268. * @return bool
  269. */
  270. static public function checkForDbTable()
  271. {
  272. $tables = MpmDbHelper::getTables();
  273. if (count($tables) == 0 || !in_array('mpm_migrations', $tables))
  274. {
  275. return false;
  276. }
  277. return true;
  278. }
  279. /**
  280. * Returns an array of all the tables in the database.
  281. *
  282. * @uses MpmDbHelper::getDbObj()
  283. * @uses MpmDbHelper::getMethod()
  284. *
  285. * @return array
  286. */
  287. static public function getTables(&$dbObj = null)
  288. {
  289. if ($dbObj == null)
  290. {
  291. $dbObj = MpmDbHelper::getDbObj();
  292. }
  293. $sql = "SHOW TABLES";
  294. $tables = array();
  295. switch (MpmDbHelper::getMethod())
  296. {
  297. case MPM_METHOD_PDO:
  298. try
  299. {
  300. foreach ($dbObj->query($sql) as $row)
  301. {
  302. $tables[] = $row[0];
  303. }
  304. }
  305. catch (Exception $e)
  306. {
  307. }
  308. break;
  309. case MPM_METHOD_MYSQLI:
  310. try
  311. {
  312. $result = $dbObj->query($sql);
  313. while ($row = $result->fetch_array())
  314. {
  315. $tables[] = $row[0];
  316. }
  317. }
  318. catch (Exception $e)
  319. {
  320. }
  321. break;
  322. }
  323. return $tables;
  324. }
  325. }
  326. ?>