PageRenderTime 56ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/app/pages/sqlbuddy/includes/class/Sql.php

https://github.com/heavy-cloud/Codeita
PHP | 495 lines | 403 code | 69 blank | 23 comment | 209 complexity | c6a1c26821640f6dddf2d6c09c266e89 MD5 | raw file
  1. <?php
  2. /*
  3. SQL Buddy - Web based MySQL administration
  4. http://www.sqlbuddy.com/
  5. sql.php
  6. - sql class
  7. MIT license
  8. 2008 Calvin Lough <http://calv.in>
  9. */
  10. class SQL {
  11. var $adapter = "";
  12. var $method = "";
  13. var $version = "";
  14. var $conn = "";
  15. var $options = "";
  16. var $errorMessage = "";
  17. var $db = "";
  18. function SQL($connString, $user = "", $pass = "") {
  19. list($this->adapter, $options) = explode(":", $connString, 2);
  20. if ($this->adapter != "sqlite") {
  21. $this->adapter = "mysql";
  22. }
  23. $optionsList = explode(";", $options);
  24. foreach ($optionsList as $option) {
  25. list($a, $b) = explode("=", $option);
  26. $opt[$a] = $b;
  27. }
  28. $this->options = $opt;
  29. $database = (array_key_exists("database", $opt)) ? $opt['database'] : "";
  30. if ($this->adapter == "sqlite" && substr(sqlite_libversion(), 0, 1) == "3" && class_exists("PDO") && in_array("sqlite", PDO::getAvailableDrivers())) {
  31. $this->method = "pdo";
  32. try
  33. {
  34. $this->conn = new PDO("sqlite:" . $database, null, null, array(PDO::ATTR_PERSISTENT => true));
  35. }
  36. catch (PDOException $error) {
  37. $this->conn = false;
  38. $this->errorMessage = $error->getMessage();
  39. }
  40. } else if ($this->adapter == "sqlite" && substr(sqlite_libversion(), 0, 1) == "2" && class_exists("PDO") && in_array("sqlite2", PDO::getAvailableDrivers())) {
  41. $this->method = "pdo";
  42. try
  43. {
  44. $this->conn = new PDO("sqlite2:" . $database, null, null, array(PDO::ATTR_PERSISTENT => true));
  45. }
  46. catch (PDOException $error) {
  47. $this->conn = false;
  48. $this->errorMessage = $error->getMessage();
  49. }
  50. } else if ($this->adapter == "sqlite") {
  51. $this->method = "sqlite";
  52. $this->conn = sqlite_open($database, 0666, $sqliteError);
  53. } else {
  54. $this->method = "mysql";
  55. $host = (array_key_exists("host", $opt)) ? $opt['host'] : "";
  56. $this->conn = @mysql_connect($host, $user, $pass);
  57. }
  58. if ($this->conn && $this->method == "pdo") {
  59. $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
  60. }
  61. if ($this->conn && $this->adapter == "mysql") {
  62. $this->query("SET NAMES 'utf8'");
  63. }
  64. if ($this->conn && $database) {
  65. $this->db = $database;
  66. }
  67. }
  68. function isConnected() {
  69. return ($this->conn !== false);
  70. }
  71. function disconnect() {
  72. if ($this->conn) {
  73. if ($this->method == "pdo") {
  74. $this->conn = null;
  75. } else if ($this->method == "mysql") {
  76. mysql_close($this->conn);
  77. $this->conn = null;
  78. } else if ($this->method == "sqlite") {
  79. sqlite_close($this->conn);
  80. $this->conn = null;
  81. }
  82. }
  83. }
  84. function getAdapter() {
  85. return $this->adapter;
  86. }
  87. function getMethod() {
  88. return $this->method;
  89. }
  90. function getOptionValue($optKey) {
  91. if (array_key_exists($optKey, $this->options)) {
  92. return $this->options[$optKey];
  93. } else {
  94. return false;
  95. }
  96. }
  97. function selectDB($db) {
  98. if ($this->conn) {
  99. if ($this->method == "mysql") {
  100. $this->db = $db;
  101. return (mysql_select_db($db));
  102. } else {
  103. return true;
  104. }
  105. } else {
  106. return false;
  107. }
  108. }
  109. function query($queryText) {
  110. if ($this->conn) {
  111. if ($this->method == "pdo") {
  112. $queryResult = $this->conn->prepare($queryText);
  113. if ($queryResult)
  114. $queryResult->execute();
  115. if (!$queryResult) {
  116. $errorInfo = $this->conn->errorInfo();
  117. $this->errorMessage = $errorInfo[2];
  118. }
  119. return $queryResult;
  120. } else if ($this->method == "mysql") {
  121. $queryResult = @mysql_query($queryText, $this->conn);
  122. if (!$queryResult) {
  123. $this->errorMessage = mysql_error();
  124. }
  125. return $queryResult;
  126. } else if ($this->method == "sqlite") {
  127. $queryResult = sqlite_query($this->conn, $queryText);
  128. if (!$queryResult) {
  129. $this->errorMessage = sqlite_error_string(sqlite_last_error($this->conn));
  130. }
  131. return $queryResult;
  132. }
  133. } else {
  134. return false;
  135. }
  136. }
  137. // Be careful using this function - when used with pdo, the pointer is moved
  138. // to the end of the result set and the query needs to be rerun. Unless you
  139. // actually need a count of the rows, use the isResultSet() function instead
  140. function rowCount($resultSet) {
  141. if (!$resultSet)
  142. return false;
  143. if ($this->conn) {
  144. if ($this->method == "pdo") {
  145. return count($resultSet->fetchAll());
  146. } else if ($this->method == "mysql") {
  147. return @mysql_num_rows($resultSet);
  148. } else if ($this->method == "sqlite") {
  149. return @sqlite_num_rows($resultSet);
  150. }
  151. }
  152. }
  153. function isResultSet($resultSet) {
  154. if ($this->conn) {
  155. if ($this->method == "pdo") {
  156. return ($resultSet == true);
  157. } else {
  158. return ($this->rowCount($resultSet) > 0);
  159. }
  160. }
  161. }
  162. function fetchArray($resultSet) {
  163. if (!$resultSet)
  164. return false;
  165. if ($this->conn) {
  166. if ($this->method == "pdo") {
  167. return $resultSet->fetch(PDO::FETCH_NUM);
  168. } else if ($this->method == "mysql") {
  169. return mysql_fetch_row($resultSet);
  170. } else if ($this->method == "sqlite") {
  171. return sqlite_fetch_array($resultSet, SQLITE_NUM);
  172. }
  173. }
  174. }
  175. function fetchAssoc($resultSet) {
  176. if (!$resultSet)
  177. return false;
  178. if ($this->conn) {
  179. if ($this->method == "pdo") {
  180. return $resultSet->fetch(PDO::FETCH_ASSOC);
  181. } else if ($this->method == "mysql") {
  182. return mysql_fetch_assoc($resultSet);
  183. } else if ($this->method == "sqlite") {
  184. return sqlite_fetch_array($resultSet, SQLITE_ASSOC);
  185. }
  186. }
  187. }
  188. function affectedRows($resultSet) {
  189. if (!$resultSet)
  190. return false;
  191. if ($this->conn) {
  192. if ($this->method == "pdo") {
  193. return $resultSet->rowCount();
  194. } else if ($this->method == "mysql") {
  195. return @mysql_affected_rows($resultSet);
  196. } else if ($this->method == "sqlite") {
  197. return sqlite_changes($resultSet);
  198. }
  199. }
  200. }
  201. function result($resultSet, $targetRow, $targetColumn = "") {
  202. if (!$resultSet)
  203. return false;
  204. if ($this->conn) {
  205. if ($this->method == "pdo") {
  206. if ($targetColumn) {
  207. $resultRow = $resultSet->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_ABS, $targetRow);
  208. return $resultRow[$targetColumn];
  209. } else {
  210. $resultRow = $resultSet->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_ABS, $targetRow);
  211. return $resultRow[0];
  212. }
  213. } else if ($this->method == "mysql") {
  214. return mysql_result($resultSet, $targetRow, $targetColumn);
  215. } else if ($this->method == "sqlite") {
  216. return sqlite_column($resultSet, $targetColumn);
  217. }
  218. }
  219. }
  220. function listDatabases() {
  221. if ($this->conn) {
  222. if ($this->adapter == "mysql") {
  223. return $this->query("SHOW DATABASES");
  224. } else if ($this->adapter == "sqlite") {
  225. return $this->db;
  226. }
  227. }
  228. }
  229. function listTables() {
  230. if ($this->conn) {
  231. if ($this->adapter == "mysql") {
  232. return $this->query("SHOW TABLES");
  233. } else if ($this->adapter == "sqlite") {
  234. return $this->query("SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name");
  235. }
  236. }
  237. }
  238. function hasCharsetSupport()
  239. {
  240. if ($this->conn) {
  241. if ($this->adapter == "mysql" && version_compare($this->getVersion(), "4.1", ">")) {
  242. return true;
  243. } else {
  244. return false;
  245. }
  246. }
  247. }
  248. function listCharset() {
  249. if ($this->conn) {
  250. if ($this->adapter == "mysql") {
  251. return $this->query("SHOW CHARACTER SET");
  252. } else if ($this->adapter == "sqlite") {
  253. return "";
  254. }
  255. }
  256. }
  257. function listCollation() {
  258. if ($this->conn) {
  259. if ($this->adapter == "mysql") {
  260. return $this->query("SHOW COLLATION");
  261. } else if ($this->adapter == "sqlite") {
  262. return "";
  263. }
  264. }
  265. }
  266. function insertId() {
  267. if ($this->conn) {
  268. if ($this->method == "pdo") {
  269. return $this->conn->lastInsertId();
  270. } else if ($this->method == "mysql") {
  271. return @mysql_insert_id($this->conn);
  272. } else if ($this->method == "sqlite") {
  273. return sqlite_last_insert_rowid($this-conn);
  274. }
  275. }
  276. }
  277. function escapeString($toEscape) {
  278. if ($this->conn) {
  279. if ($this->method == "pdo") {
  280. $toEscape = $this->conn->quote($toEscape);
  281. $toEscape = substr($toEscape, 1, -1);
  282. return $toEscape;
  283. } else if ($this->adapter == "mysql") {
  284. return mysql_real_escape_string($toEscape);
  285. } else if ($this->adapter == "sqlite") {
  286. return sqlite_escape_string($toEscape);
  287. }
  288. }
  289. }
  290. function getVersion() {
  291. if ($this->conn) {
  292. // cache
  293. if ($this->version) {
  294. return $this->version;
  295. }
  296. if ($this->adapter == "mysql") {
  297. $verSql = mysql_get_server_info();
  298. $version = explode("-", $verSql);
  299. $this->version = $version[0];
  300. return $this->version;
  301. } else if ($this->adapter == "sqlite") {
  302. $this->version = sqlite_libversion();
  303. return $this->version;
  304. }
  305. }
  306. }
  307. // returns the number of rows in a table
  308. function tableRowCount($table) {
  309. if ($this->conn) {
  310. if ($this->adapter == "mysql") {
  311. $countSql = $this->query("SELECT COUNT(*) AS `RowCount` FROM `" . $table . "`");
  312. $count = (int)($this->result($countSql, 0, "RowCount"));
  313. return $count;
  314. } else if ($this->adapter == "sqlite") {
  315. $countSql = $this->query("SELECT COUNT(*) AS 'RowCount' FROM '" . $table . "'");
  316. $count = (int)($this->result($countSql, 0, "RowCount"));
  317. return $count;
  318. }
  319. }
  320. }
  321. // gets column info for a table
  322. function describeTable($table) {
  323. if ($this->conn) {
  324. if ($this->adapter == "mysql") {
  325. return $this->query("DESCRIBE `" . $table . "`");
  326. } else if ($this->adapter == "sqlite") {
  327. $columnSql = $this->query("SELECT sql FROM sqlite_master where tbl_name = '" . $table . "'");
  328. $columnInfo = $this->result($columnSql, 0, "sql");
  329. $columnStart = strpos($columnInfo, '(');
  330. $columns = substr($columnInfo, $columnStart+1, -1);
  331. $columns = split(',[^0-9]', $columns);
  332. $columnList = array();
  333. foreach ($columns as $column) {
  334. $column = trim($column);
  335. $columnSplit = explode(" ", $column, 2);
  336. $columnName = $columnSplit[0];
  337. $columnType = (sizeof($columnSplit) > 1) ? $columnSplit[1] : "";
  338. $columnList[] = array($columnName, $columnType);
  339. }
  340. return $columnList;
  341. }
  342. }
  343. }
  344. /*
  345. Return names, row counts etc for every database, table and view in a JSON string
  346. */
  347. function getMetadata() {
  348. $output = '';
  349. if ($this->conn) {
  350. if ($this->adapter == "mysql" && version_compare($this->getVersion(), "5.0.0", ">=")) {
  351. $this->selectDB("information_schema");
  352. $schemaSql = $this->query("SELECT `SCHEMA_NAME` FROM `SCHEMATA` ORDER BY `SCHEMA_NAME`");
  353. if ($this->rowCount($schemaSql)) {
  354. while ($schema = $this->fetchAssoc($schemaSql)) {
  355. $output .= '{"name": "' . $schema['SCHEMA_NAME'] . '"';
  356. // other interesting columns: TABLE_TYPE, ENGINE, TABLE_COLUMN and many more
  357. $tableSql = $this->query("SELECT `TABLE_NAME`, `TABLE_ROWS` FROM `TABLES` WHERE `TABLE_SCHEMA`='" . $schema['SCHEMA_NAME'] . "' ORDER BY `TABLE_NAME`");
  358. if ($this->rowCount($tableSql)) {
  359. $output .= ',"items": [';
  360. while ($table = $this->fetchAssoc($tableSql)) {
  361. if ($schema['SCHEMA_NAME'] == "information_schema") {
  362. $countSql = $this->query("SELECT COUNT(*) AS `RowCount` FROM `" . $table['TABLE_NAME'] . "`");
  363. $rowCount = (int)($this->result($countSql, 0, "RowCount"));
  364. } else {
  365. $rowCount = (int)($table['TABLE_ROWS']);
  366. }
  367. $output .= '{"name":"' . $table['TABLE_NAME'] . '","rowcount":' . $rowCount . '},';
  368. }
  369. if (substr($output, -1) == ",")
  370. $output = substr($output, 0, -1);
  371. $output .= ']';
  372. }
  373. $output .= '},';
  374. }
  375. $output = substr($output, 0, -1);
  376. }
  377. } else if ($this->adapter == "mysql") {
  378. $schemaSql = $this->listDatabases();
  379. if ($this->rowCount($schemaSql)) {
  380. while ($schema = $this->fetchArray($schemaSql)) {
  381. $output .= '{"name": "' . $schema[0] . '"';
  382. $this->selectDB($schema[0]);
  383. $tableSql = $this->listTables();
  384. if ($this->rowCount($tableSql)) {
  385. $output .= ',"items": [';
  386. while ($table = $this->fetchArray($tableSql)) {
  387. $countSql = $this->query("SELECT COUNT(*) AS `RowCount` FROM `" . $table[0] . "`");
  388. $rowCount = (int)($this->result($countSql, 0, "RowCount"));
  389. $output .= '{"name":"' . $table[0] . '","rowcount":' . $rowCount . '},';
  390. }
  391. if (substr($output, -1) == ",")
  392. $output = substr($output, 0, -1);
  393. $output .= ']';
  394. }
  395. $output .= '},';
  396. }
  397. $output = substr($output, 0, -1);
  398. }
  399. } else if ($this->adapter == "sqlite") {
  400. $output .= '{"name": "' . $this->db . '"';
  401. $tableSql = $this->listTables();
  402. if ($tableSql) {
  403. $output .= ',"items": [';
  404. while ($tableRow = $this->fetchArray($tableSql)) {
  405. $countSql = $this->query("SELECT COUNT(*) AS 'RowCount' FROM '" . $tableRow[0] . "'");
  406. $rowCount = (int)($this->result($countSql, 0, "RowCount"));
  407. $output .= '{"name":"' . $tableRow[0] . '","rowcount":' . $rowCount . '},';
  408. }
  409. if (substr($output, -1) == ",")
  410. $output = substr($output, 0, -1);
  411. $output .= ']';
  412. }
  413. $output .= '}';
  414. }
  415. }
  416. return $output;
  417. }
  418. function error() {
  419. return $this->errorMessage;
  420. }
  421. }