MySQL JSON Schema Tree


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. /**
  3.  * I hold mysql methods
  4.  *
  5.  * @name MySQLService
  6.  * @author Jonnie Spratley
  7.  * @version 1.0
  8.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  9.  *
  10.  */
  11. class MySQLConnect
  12. {
  13. public $mysqli;
  14.  
  15. /**
  16. * I hold methods to alter a mysql database
  17. *
  18. * @param [string] $host
  19. * @param [string] $username
  20. * @param [string] $password
  21. */
  22. public function __construct( $host, $username, $password )
  23. {
  24. $link = new mysqli ( $host, $username, $password );
  25.  
  26. /* check connection */
  27. {
  28. trigger_error ( 'Database connection failure: Username/Password was incorrect.', E_USER_ERROR );
  29. exit ();
  30. }
  31. else
  32. {
  33. $this->setMysqli ( $link );
  34. }
  35. }
  36.  
  37. /**
  38. * I execute a query
  39. *
  40. * @param [string] $sql
  41. * @return [array]
  42. */
  43. public function execute( $sql )
  44. {
  45. return $this->queryToARRAY ( $sql );
  46. }
  47.  
  48. /**
  49. * I get the databases
  50. *
  51. * @return [array]
  52. */
  53. public function getDatabases()
  54. {
  55. return $this->queryToARRAY ( "SHOW DATABASES" );
  56. }
  57.  
  58. /**
  59. * I execute a raw query
  60. *
  61. * @param [string] $query
  62. * @return [link]
  63. */
  64. public function realQuery( $query )
  65. {
  66. return $this->mysqli->query ( $query );
  67. }
  68.  
  69. /**
  70. * I start the tree
  71. *
  72. * @return [array]
  73. */
  74. public function tree_getTree()
  75. {
  76. $mysqlVersion = mysqli_get_client_info ( $this->mysqli );
  77. $host = $_SERVER [ 'HTTP_HOST' ] . " (MySQL v. $mysqlVersion )";
  78.  
  79. $hostArray = array (
  80. 'label' => $host, 'type' => 'server', 'children' => $this->tree_getSchemas ()
  81. );
  82. $treeArray [] = array (
  83. 'label' => 'SQL Databases', 'type' => 'servers', 'children' => $hostArray
  84. );
  85.  
  86. return json_encode ( $treeArray );
  87. }
  88.  
  89. /**
  90. * I build the tree
  91. *
  92. * @return [array]
  93. */
  94. private function tree_getSchemas()
  95. {
  96. //Database query
  97. $databaseSQL = $this->realQuery ( "SHOW DATABASES" );
  98.  
  99. //New database array
  100. $databases = array ();
  101.  
  102. //Loop the query
  103. while ( $database = mysqli_fetch_assoc ( $databaseSQL ) )
  104. {
  105. //Create a new array of tables for each database
  106. $tables = array ();
  107. $status = array ();
  108. $size = array ();
  109.  
  110. foreach ( $database as $key => $value )
  111. {
  112. //Set the table array to get the tbles from the database
  113. $tables = $this->tree_db_getTables ( $value );
  114. $status = $this->_db_getStatus ( $value );
  115. $size = $this->_db_getSize ( $value );
  116. }
  117.  
  118. //Add the tables to the database array
  119. $databases [] = array (
  120. "aDatabase" => $value, "aData" => $key, "aType" => "database", "aIcon" => "database", "aStatus" => $status, "aSize" => $size, "aTables" => $tables
  121. );
  122. }
  123.  
  124. $databaseFolder [] = array (
  125. 'label' => 'Schemas', 'children' => $databases
  126. );
  127.  
  128. return $databaseFolder;
  129. }
  130.  
  131. /**
  132. * I get the users auth
  133. *
  134. * @return [array]
  135. */
  136. private function tree_db_getAuthorizations()
  137. {
  138. $authorizationsArray = array (
  139. 'label' => 'Authorization IDs', 'children' => array (
  140. 'label' => 'rfd'
  141. )
  142. );
  143.  
  144. return $authorizationsArray;
  145. }
  146.  
  147. //TODO:
  148. private function tree_db_getDependcenies( $database )
  149. {
  150. $dependceniesArray = array (
  151. 'label' => 'Dependcencies', 'children' => array (
  152. 'label' => 'test'
  153. )
  154. );
  155.  
  156. return $dependceniesArray;
  157. }
  158.  
  159. //TODO:
  160. private function tree_db_getStoredProcs( $database )
  161. {
  162. $storedProcsArray = array (
  163. 'label' => 'Stored Procedures', 'children' => array (
  164. 'label' => 'test'
  165. )
  166. );
  167.  
  168. return $storedProcsArray;
  169. }
  170.  
  171. /**
  172. * I get the tables
  173. *
  174. * @param [string] $database the database
  175. * @return [array]
  176. */
  177. private function tree_db_getTables( $database )
  178. {
  179. //table query
  180. $tableSQL = $this->realQuery ( "SHOW TABLES FROM $database" );
  181.  
  182. //create a new array of tables
  183. $tables = array ();
  184.  
  185. //loop all the results
  186. while ( $table = mysqli_fetch_assoc ( $tableSQL ) )
  187. {
  188. $columns = array ();
  189. $statuss = array ();
  190. $indexes = array ();
  191. $constraints = array ();
  192. $dependicies = array ();
  193. $triggers = array ();
  194.  
  195. //for each table in the result make an array
  196. foreach ( $table as $t_key => $t_value )
  197. {
  198. //get the tables fields for each table
  199. $columns = $this->tree_tbl_getColumns ( $database, $t_value );
  200.  
  201. //now get the primary key for each table
  202. $constraints = $this->tree_tbl_getConstraints ( $database, $t_value );
  203.  
  204. //now get the indexes for each table
  205. $indexes = $this->tree_tbl_getIndexes ( $database, $t_value );
  206.  
  207. //now get the dependencys for each table
  208. $dependicies = $this->tree_tbl_getDependcenies ( $database, $t_value );
  209.  
  210. //now get the triggers for each table
  211. $triggers = $this->tree_tbl_getTriggers ( $database, $t_value );
  212.  
  213. //now get the status for each table
  214. $statuss = $this->_tbl_getStatus ( $database, $t_value );
  215.  
  216. }
  217.  
  218. $columnArr = $columns;
  219. $constraintArr = $constraints;
  220. $indexArr = $indexes;
  221. $dependencyArr = $dependicies;
  222. $triggerArr = $triggers;
  223. $statusArr = $statuss;
  224. $tables [] = array (
  225. "label" => $t_value, "type" => "table", "icon" => "table", "children" => array (
  226. $columnArr, $constraintArr, $indexArr, $dependencyArr, $triggerArr, $statusArr
  227. )
  228. );
  229. }
  230.  
  231. $tableFolder [] = array (
  232. 'label' => 'Tables', 'children' => $tables
  233. );
  234.  
  235. return $tableFolder;
  236. }
  237.  
  238. //TODO:
  239. private function tree_db_getUserFunctions( $database )
  240. {
  241.  
  242. }
  243.  
  244. //TODO:
  245. private function tree_db_getViews( $database )
  246. {
  247.  
  248. }
  249.  
  250. /**
  251. * I get the columns
  252. *
  253. * @param [string] $database
  254. * @param [string] $table
  255. * @return [array]
  256. */
  257. private function tree_tbl_getColumns( $database, $table )
  258. {
  259. $sql = "SHOW FIELDS FROM $database.$table";
  260. $query = $this->realQuery ( $sql );
  261.  
  262. $columnsArray = array ();
  263.  
  264. while ( $row = mysqli_fetch_row ( $query ) )
  265. {
  266. $type = strtoupper ( $row [ 1 ] );
  267. $null = '';
  268.  
  269. //Check if the column can be null
  270. if ( $row [ 2 ] == 'YES' )
  271. {
  272. $null = 'Nullable';
  273. }
  274. $type = '[' . $type . ' ' . $null . ']';
  275.  
  276. $columnsArray [] = array (
  277. 'label' => $row [ 0 ] . ' ' . $type
  278. );
  279. }
  280. //Create the folder
  281. $columnsFolder = array (
  282. 'label' => 'Columns', 'children' => $columnsArray
  283. );
  284.  
  285. return $columnsFolder;
  286. }
  287.  
  288. /**
  289. * I get the primary keys
  290. *
  291. * @param [string] $database
  292. * @param [string] $table
  293. * @return [array]
  294. */
  295. private function tree_tbl_getConstraints( $database, $table )
  296. {
  297. $sql = "SHOW INDEX FROM $database.$table";
  298. $result = $this->realQuery ( $sql );
  299. $constraintArray = array ();
  300.  
  301. while ( $constraint = mysqli_fetch_assoc ( $result ) )
  302. {
  303. //check if the key is the primary key
  304. if ( $constraint [ 'Key_name' ] == 'PRIMARY' )
  305. {
  306. $constraintArray = array (
  307. 'label' => $constraint [ 'Key_name' ]
  308. );
  309. }
  310. }
  311. $constraintFolder = array (
  312. 'label' => 'Constraints', 'children' => array (
  313. $constraintArray
  314. )
  315. );
  316.  
  317. return $constraintFolder;
  318. }
  319.  
  320. //TODO:
  321. /**
  322. * I get the dependcencies
  323. *
  324. * @param [string] $database
  325. * @param [string] $table
  326. * @return [array]
  327. */
  328. private function tree_tbl_getDependcenies( $database, $table )
  329. {
  330. $dependArray = array (
  331. 'label' => 'admin table'
  332. );
  333.  
  334. $dependFolder = array (
  335. 'label' => 'Dependencies', 'children' => array (
  336. $dependArray
  337. )
  338. );
  339.  
  340. return $dependFolder;
  341. }
  342.  
  343. /**
  344. * I get the indexes
  345. *
  346. * @param [string] $database
  347. * @param [string] $table
  348. * @return [array]
  349. */
  350. private function tree_tbl_getIndexes( $database, $table )
  351. {
  352. $sql = "SHOW INDEX FROM $database.$table";
  353. $query = mysqli_query ( $this->mysqli, $sql );
  354.  
  355. $indexArray = array ();
  356.  
  357. while ( $row = mysqli_fetch_row ( $query ) )
  358. {
  359. if ( $row [ 2 ] !== 'PRIMARY' )
  360. {
  361. $indexArray [] = array (
  362. 'label' => $row [ 4 ] . "($row[2])"
  363. );
  364. }
  365. }
  366.  
  367. $indexFolder = array (
  368. 'label' => 'Indexes', 'children' => $indexArray
  369. );
  370.  
  371. return $indexFolder;
  372. }
  373.  
  374. //TODO:
  375. /**
  376. * I get the triggers
  377. *
  378. * @param [string] $database
  379. * @param [string] $table
  380. * @return [array]
  381. */
  382. private function tree_tbl_getTriggers( $database, $table )
  383. {
  384. $triggerArray = $this->queryToARRAY ( "SHOW INDEX FROM $database.$table" );
  385.  
  386. $triggerFolder = array (
  387. 'label' => 'Triggers', 'children' => array (
  388. $triggerArray
  389. )
  390. );
  391.  
  392. return $triggerFolder;
  393. }
  394.  
  395. /**
  396. * I get the table status
  397. *
  398. * @param [string] $database
  399. * @param [string] $table
  400. * @return [array]
  401. */
  402. private function _tbl_getStatus( $database, $table )
  403. {
  404. return $this->queryToARRAY ( "SHOW TABLE STATUS FROM $database LIKE '$table'" );
  405. }
  406.  
  407. /**
  408. * I get the size of all the databases
  409. *
  410. * @param [string] $database the database
  411. * @return [array]
  412. */
  413. private function _db_getSize( $database )
  414. {
  415. $statusSQL = $this->realQuery ( "SHOW TABLE STATUS FROM $database" );
  416. $sizeArray = array ();
  417.  
  418. $totalSize = 0;
  419. $dataSize = 0;
  420. $indexSize = 0;
  421.  
  422. //loop all the results
  423. while ( $size = mysqli_fetch_assoc ( $statusSQL ) )
  424. {
  425. $dataSize += $size [ 'Data_length' ];
  426. $indexSize += $size [ 'Index_length' ];
  427. }
  428. $totalSize = $dataSize + $indexSize;
  429. $sizeArray [] = array (
  430. 'totalSize' => $totalSize, 'dataSize' => $dataSize, 'indexSize' => $indexSize
  431. );
  432.  
  433. return $sizeArray;
  434. }
  435.  
  436. /**
  437. * I get the status of the all the tables for a database.
  438. *
  439. * @param [string] $database the database
  440. * @return [array]
  441. */
  442. private function _db_getStatus( $database )
  443. {
  444. return $this->queryToARRAY ( "SHOW TABLE STATUS FROM $database" );
  445. }
  446.  
  447. /**
  448. * I execute a query and return the results as json.
  449. *
  450. * @param [string] $sql the query to be executed
  451. * @return [json] the result in json
  452. */
  453. private function queryToJSON( $sql )
  454. {
  455. $result = $this->realQuery ( $sql );
  456.  
  457. while ( $row = mysqli_fetch_assoc ( $result ) )
  458. {
  459. $array [] = $row;
  460. }
  461. return json_encode ( $array );
  462. }
  463.  
  464. /**
  465. * I execute a query and return the result as an array.
  466. *
  467. * @param [string] $sql the query to be executed
  468. * @return [array] the result array
  469. */
  470. public function queryToARRAY( $sql )
  471. {
  472. $query = $this->realQuery ( $sql );
  473. $array = array ();
  474.  
  475. while ( $row = mysqli_fetch_assoc ( $query ) )
  476. {
  477. $array [] = $row;
  478. }
  479.  
  480. return $array;
  481. }
  482.  
  483. /**
  484. * I get the query status
  485. *
  486. * @param [string] $sql
  487. * @return [json] mysql status with the ('_') striped out
  488. */
  489. public function queryStatusToJSON( $sql )
  490. {
  491. $result = $this->realQuery ( $sql );
  492.  
  493. while ( $row = mysqli_fetch_assoc ( $result ) )
  494. {
  495. //replace some of the names
  496. $row = str_replace ( 'Com_', '', $row );
  497. //take out the _ of the rows
  498. $row = str_replace ( '_', ' ', $row );
  499.  
  500. $array [] = $row;
  501. }
  502. sort ( $array );
  503.  
  504. return json_encode ( $array );
  505. }
  506.  
  507. /**
  508. * I dump vars
  509. *
  510. * @param [string] $title the title of the dump
  511. * @param [var] $var the var
  512. */
  513. public function dump( $title, $var )
  514. {
  515. print "<h4>$title</h4>";
  516. print "<pre>";
  517. print_r ( $var );
  518. print "</pre>";
  519. }
  520.  
  521. /**
  522. * @return [link]
  523. */
  524. public function getMysqli()
  525. {
  526. return $this->mysqli;
  527. }
  528.  
  529. /**
  530. * @param [link] $mysqli
  531. */
  532. public function setMysqli( $mysqli )
  533. {
  534. $this->mysqli = $mysqli;
  535. }
  536.  
  537. }
  538. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.