/includes/class/Sql-php4.php

https://gitlab.com/mattyhead/sql-buddy · PHP · 424 lines · 340 code · 64 blank · 20 comment · 154 complexity · 8731881d8870666b67bd10e3c04b76e8 MD5 · raw file

  1. <?php
  2. /*
  3. SQL Buddy - Web based MySQL administration
  4. http://www.sqlbuddy.com/
  5. sql-php4.php
  6. - sql class (php4 compatible)
  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") {
  31. $this->method = "sqlite";
  32. $this->conn = sqlite_open($database, 0666, $sqliteError);
  33. } else {
  34. $this->method = "mysql";
  35. $host = (array_key_exists("host", $opt)) ? $opt['host'] : "";
  36. $this->conn = @mysql_connect($host, $user, $pass);
  37. }
  38. if ($this->conn && $this->adapter == "mysql") {
  39. $this->query("SET NAMES 'utf8'");
  40. }
  41. }
  42. function isConnected() {
  43. return ($this->conn !== false);
  44. }
  45. function disconnect() {
  46. if ($this->conn) {
  47. if ($this->method == "mysql") {
  48. mysql_close($this->conn);
  49. $this->conn = null;
  50. } else if ($this->method == "sqlite") {
  51. sqlite_close($this->conn);
  52. $this->conn = null;
  53. }
  54. }
  55. }
  56. function getAdapter() {
  57. return $this->adapter;
  58. }
  59. function getMethod() {
  60. return $this->method;
  61. }
  62. function getOptionValue($optKey) {
  63. if (array_key_exists($optKey, $this->options)) {
  64. return $this->options[$optKey];
  65. } else {
  66. return false;
  67. }
  68. }
  69. function selectDB($db) {
  70. if ($this->conn) {
  71. $this->db = $db;
  72. if ($this->method == "mysql") {
  73. return (mysql_select_db($db));
  74. } else {
  75. return true;
  76. }
  77. } else {
  78. return false;
  79. }
  80. }
  81. function query($queryText) {
  82. if ($this->conn) {
  83. if ($this->method == "mysql") {
  84. $queryResult = @mysql_query($queryText, $this->conn);
  85. if (!$queryResult) {
  86. $this->errorMessage = mysql_error();
  87. }
  88. return $queryResult;
  89. } else if ($this->method == "sqlite") {
  90. $queryResult = sqlite_query($this->conn, $queryText);
  91. if (!$queryResult) {
  92. $this->errorMessage = sqlite_error_string(sqlite_last_error($this->conn));
  93. }
  94. return $queryResult;
  95. }
  96. } else {
  97. return false;
  98. }
  99. }
  100. function rowCount($resultSet) {
  101. if ($this->conn) {
  102. if ($this->method == "mysql") {
  103. return @mysql_num_rows($resultSet);
  104. } else if ($this->method == "sqlite") {
  105. return @sqlite_num_rows($resultSet);
  106. }
  107. }
  108. }
  109. function isResultSet($resultSet) {
  110. if ($this->conn) {
  111. return ($this->rowCount($resultSet) > 0);
  112. }
  113. }
  114. function fetchArray($resultSet) {
  115. if (!$resultSet)
  116. return false;
  117. if ($this->conn) {
  118. if ($this->method == "mysql") {
  119. return mysql_fetch_row($resultSet);
  120. } else if ($this->method == "sqlite") {
  121. return sqlite_fetch_array($resultSet, SQLITE_NUM);
  122. }
  123. }
  124. }
  125. function fetchAssoc($resultSet) {
  126. if (!$resultSet)
  127. return false;
  128. if ($this->conn) {
  129. if ($this->method == "mysql") {
  130. return mysql_fetch_assoc($resultSet);
  131. } else if ($this->method == "sqlite") {
  132. return sqlite_fetch_array($resultSet, SQLITE_ASSOC);
  133. }
  134. }
  135. }
  136. function affectedRows($resultSet) {
  137. if (!$resultSet)
  138. return false;
  139. if ($this->conn) {
  140. if ($this->method == "mysql") {
  141. return @mysql_affected_rows($resultSet);
  142. } else if ($this->method == "sqlite") {
  143. return sqlite_changes($resultSet);
  144. }
  145. }
  146. }
  147. function result($resultSet, $targetRow, $targetColumn = "") {
  148. if (!$resultSet)
  149. return false;
  150. if ($this->conn) {
  151. if ($this->method == "mysql") {
  152. return mysql_result($resultSet, $targetRow, $targetColumn);
  153. } else if ($this->method == "sqlite") {
  154. return sqlite_column($resultSet, $targetColumn);
  155. }
  156. }
  157. }
  158. function listDatabases() {
  159. if ($this->conn) {
  160. if ($this->adapter == "mysql") {
  161. return $this->query("SHOW DATABASES");
  162. } else if ($this->adapter == "sqlite") {
  163. $database = (array_key_exists("database", $this->options)) ? $this->options['database'] : "";
  164. return $database;
  165. }
  166. }
  167. }
  168. function listTables() {
  169. if ($this->conn) {
  170. if ($this->adapter == "mysql") {
  171. return $this->query("SHOW TABLES");
  172. } else if ($this->adapter == "sqlite") {
  173. return $this->query("SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name");
  174. }
  175. }
  176. }
  177. function hasCharsetSupport()
  178. {
  179. if ($this->conn) {
  180. if ($this->adapter == "mysql" && version_compare($this->getVersion(), "4.1", ">")) {
  181. return true;
  182. } else {
  183. return false;
  184. }
  185. }
  186. }
  187. function listCharset() {
  188. if ($this->conn) {
  189. if ($this->adapter == "mysql") {
  190. return $this->query("SHOW CHARACTER SET");
  191. } else if ($this->adapter == "sqlite") {
  192. return "";
  193. }
  194. }
  195. }
  196. function listCollation() {
  197. if ($this->conn) {
  198. if ($this->adapter == "mysql") {
  199. return $this->query("SHOW COLLATION");
  200. } else if ($this->adapter == "sqlite") {
  201. return "";
  202. }
  203. }
  204. }
  205. function insertId($resultSet = null) {
  206. if ($this->conn) {
  207. if ($this->method == "mysql") {
  208. return mysql_insert_id($resultSet);
  209. } else if ($this->method == "sqlite") {
  210. return sqlite_last_insert_rowid($resultSet);
  211. }
  212. }
  213. }
  214. function escapeString($toEscape) {
  215. if ($this->conn) {
  216. if ($this->adapter == "mysql") {
  217. return mysql_real_escape_string($toEscape);
  218. } else if ($this->adapter == "sqlite") {
  219. return sqlite_escape_string($toEscape);
  220. }
  221. }
  222. }
  223. function getVersion() {
  224. if ($this->conn) {
  225. // cache
  226. if ($this->version) {
  227. return $this->version;
  228. }
  229. if ($this->adapter == "mysql") {
  230. $verSql = mysql_get_server_info();
  231. $version = explode("-", $verSql);
  232. $this->version = $version[0];
  233. return $this->version;
  234. } else if ($this->adapter == "sqlite") {
  235. $this->version = sqlite_libversion();
  236. return $this->version;
  237. }
  238. }
  239. }
  240. // returns the number of rows in a table
  241. function tableRowCount($table) {
  242. if ($this->conn) {
  243. if ($this->adapter == "mysql") {
  244. $countSql = $this->query("SELECT COUNT(*) AS `RowCount` FROM `" . $table . "`");
  245. $count = (int)($this->result($countSql, 0, "RowCount"));
  246. return $count;
  247. } else if ($this->adapter == "sqlite") {
  248. $countSql = $this->query("SELECT COUNT(*) AS 'RowCount' FROM '" . $table . "'");
  249. $count = (int)($this->result($countSql, 0, "RowCount"));
  250. return $count;
  251. }
  252. }
  253. }
  254. // gets column info for a table
  255. function describeTable($table) {
  256. if ($this->conn) {
  257. if ($this->adapter == "mysql") {
  258. return $this->query("DESCRIBE `" . $table . "`");
  259. } else if ($this->adapter == "sqlite") {
  260. $columnSql = $this->query("SELECT sql FROM sqlite_master where tbl_name = '" . $table . "'");
  261. $columnInfo = $this->result($columnSql, 0, "sql");
  262. $columnStart = strpos($columnInfo, '(');
  263. $columns = substr($columnInfo, $columnStart+1, -1);
  264. $columns = split(',[^0-9]', $columns);
  265. $columnList = array();
  266. foreach ($columns as $column) {
  267. $column = trim($column);
  268. $columnSplit = explode(" ", $column, 2);
  269. $columnName = $columnSplit[0];
  270. $columnType = (sizeof($columnSplit) > 1) ? $columnSplit[1] : "";
  271. $columnList[] = array($columnName, $columnType);
  272. }
  273. return $columnList;
  274. }
  275. }
  276. }
  277. /*
  278. Return names, row counts etc for every database, table and view in a JSON string
  279. */
  280. function getMetadata() {
  281. $output = '';
  282. if ($this->conn) {
  283. if ($this->adapter == "mysql" && version_compare($this->getVersion(), "5.0.0", ">=")) {
  284. $this->selectDB("information_schema");
  285. $schemaSql = $this->query("SELECT `SCHEMA_NAME` FROM `SCHEMATA` ORDER BY `SCHEMA_NAME`");
  286. if ($this->rowCount($schemaSql)) {
  287. while ($schema = $this->fetchAssoc($schemaSql)) {
  288. $output .= '{"name": "' . $schema['SCHEMA_NAME'] . '"';
  289. // other interesting columns: TABLE_TYPE, ENGINE, TABLE_COLUMN and many more
  290. $tableSql = $this->query("SELECT `TABLE_NAME`, `TABLE_ROWS` FROM `TABLES` WHERE `TABLE_SCHEMA`='" . $schema['SCHEMA_NAME'] . "' ORDER BY `TABLE_NAME`");
  291. if ($this->rowCount($tableSql)) {
  292. $output .= ',"items": [';
  293. while ($table = $this->fetchAssoc($tableSql)) {
  294. if ($schema['SCHEMA_NAME'] == "information_schema") {
  295. $countSql = $this->query("SELECT COUNT(*) AS `RowCount` FROM `" . $table['TABLE_NAME'] . "`");
  296. $rowCount = (int)($this->result($countSql, 0, "RowCount"));
  297. } else {
  298. $rowCount = (int)($table['TABLE_ROWS']);
  299. }
  300. $output .= '{"name":"' . $table['TABLE_NAME'] . '","rowcount":' . $rowCount . '},';
  301. }
  302. if (substr($output, -1) == ",")
  303. $output = substr($output, 0, -1);
  304. $output .= ']';
  305. }
  306. $output .= '},';
  307. }
  308. $output = substr($output, 0, -1);
  309. }
  310. } else if ($this->adapter == "mysql") {
  311. $schemaSql = $this->listDatabases();
  312. if ($this->rowCount($schemaSql)) {
  313. while ($schema = $this->fetchArray($schemaSql)) {
  314. $output .= '{"name": "' . $schema[0] . '"';
  315. $this->selectDB($schema[0]);
  316. $tableSql = $this->listTables();
  317. if ($this->rowCount($tableSql)) {
  318. $output .= ',"items": [';
  319. while ($table = $this->fetchArray($tableSql)) {
  320. $countSql = $this->query("SELECT COUNT(*) AS `RowCount` FROM `" . $table[0] . "`");
  321. $rowCount = (int)($this->result($countSql, 0, "RowCount"));
  322. $output .= '{"name":"' . $table[0] . '","rowcount":' . $rowCount . '},';
  323. }
  324. if (substr($output, -1) == ",")
  325. $output = substr($output, 0, -1);
  326. $output .= ']';
  327. }
  328. $output .= '},';
  329. }
  330. $output = substr($output, 0, -1);
  331. }
  332. } else if ($this->adapter == "sqlite") {
  333. $database = (array_key_exists("database", $this->options)) ? $this->options['database'] : "";
  334. $output .= '{"name": "' . $database . '"';
  335. $tableSql = $this->listTables();
  336. if ($this->rowCount($tableSql)) {
  337. $output .= ',"items": [';
  338. while ($tableRow = $this->fetchArray($tableSql)) {
  339. $countSql = $this->query("SELECT COUNT(*) AS 'RowCount' FROM '" . $tableRow[0] . "'");
  340. $rowCount = (int)($this->result($countSql, 0, "RowCount"));
  341. $output .= '{"name":"' . $tableRow[0] . '","rowcount":' . $rowCount . '},';
  342. }
  343. if (substr($output, -1) == ",")
  344. $output = substr($output, 0, -1);
  345. $output .= ']';
  346. }
  347. $output .= '}';
  348. }
  349. }
  350. return $output;
  351. }
  352. function error() {
  353. return $this->errorMessage;
  354. }
  355. }