examples.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>SqlFormatter Examples</title>
  5. <style>
  6. body {
  7. font-family: arial;
  8. }
  9. table, td, th {
  10. border: 1px solid #aaa;
  11. }
  12. table {
  13. border-width: 1px 1px 0 0;
  14. border-spacing: 0;
  15. }
  16. td, th {
  17. border-width: 0 0 1px 1px;
  18. padding: 5px 10px;
  19. vertical-align: top;
  20. }
  21. pre {
  22. padding: 0;
  23. margin: 0;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <?php
  29. require_once('../lib/SqlFormatter.php');
  30. // Example statements for formatting and highlighting
  31. $statements = array(
  32. "SELECT * FROM MyTable WHERE id = 46",
  33. "SELECT count(*),`Column1`,`Testing`, `Testing Three` FROM `Table1`
  34. WHERE Column1 = 'testing' AND ( (`Column2` = `Column3` OR Column4 >= NOW()) )
  35. GROUP BY Column1 ORDER BY Column3 DESC LIMIT 5,10",
  36. "select * from `Table`, (SELECT group_concat(column1) as col FROM Table2 GROUP BY category)
  37. Table2, Table3 where Table2.col = (Table3.col2 - `Table`.id)",
  38. "insert ignore into Table3 (column1, column2) VALUES ('test1','test2'), ('test3','test4');",
  39. "UPDATE MyTable SET name='sql', category='databases' WHERE id > '65'",
  40. "delete from MyTable WHERE name LIKE \"test%\"",
  41. "SELECT * FROM UnmatchedParens WHERE ( A = B)) AND (((Test=1)",
  42. "-- This is a comment
  43. SELECT
  44. /* This is another comment
  45. On more than one line */
  46. Id #This is one final comment
  47. as temp, DateCreated as Created FROM MyTable;",
  48. );
  49. // Example statements for splitting SQL strings into individual queries
  50. $split_statements = array(
  51. "DROP TABLE IF EXISTS MyTable;
  52. CREATE TABLE MyTable ( id int );
  53. INSERT INTO MyTable (id)
  54. VALUES
  55. (1),(2),(3),(4);
  56. SELECT * FROM MyTable;",
  57. "SELECT \";\"; SELECT \";\\\"; a;\";
  58. SELECT \";
  59. abc\";
  60. SELECT a,b #comment;
  61. FROM test;",
  62. );
  63. // Example statements for removing comments
  64. $comment_statements = array(
  65. "-- This is a comment
  66. SELECT
  67. /* This is another comment
  68. On more than one line */
  69. Id #This is one final comment
  70. as temp, DateCreated as Created FROM MyTable;",
  71. );
  72. ?>
  73. <h1>Formatting And Syntax Highlighting</h1>
  74. <div>
  75. Usage:
  76. <pre>
  77. <?php highlight_string('<?php' . "\n" . '$formatted = SqlFormatter::format($sql);' . "\n" . '?>'); ?>
  78. </pre>
  79. </div>
  80. <table>
  81. <tr>
  82. <th>Original</th>
  83. <th>Formatted And Highlighted</th>
  84. </tr>
  85. <?php foreach ($statements as $sql) { ?>
  86. <tr>
  87. <td>
  88. <pre><?php echo $sql; ?></pre>
  89. </td>
  90. <td><?php echo SqlFormatter::format($sql); ?></td>
  91. </tr>
  92. <?php } ?>
  93. </table>
  94. <h1>Formatting Only</h1>
  95. <div>
  96. Usage:
  97. <pre>
  98. <?php highlight_string('<?php' . "\n" . '$formatted = SqlFormatter::format($sql, false);' . "\n" . '?>'); ?>
  99. </pre>
  100. </div>
  101. <table>
  102. <tr>
  103. <th>Original</th>
  104. <th>Formatted</th>
  105. </tr>
  106. <?php foreach ($statements as $sql) { ?>
  107. <tr>
  108. <td>
  109. <pre><?php echo $sql; ?></pre>
  110. </td>
  111. <td><pre><?php echo htmlentities(SqlFormatter::format($sql,false)); ?></pre></td>
  112. </tr>
  113. <?php } ?>
  114. </table>
  115. <h1>Syntax Highlighting Only</h1>
  116. <div>
  117. Usage:
  118. <pre>
  119. <?php highlight_string('<?php' . "\n" . '$highlighted = SqlFormatter::highlight($sql);' . "\n" . '?>'); ?>
  120. </pre>
  121. </div>
  122. <table>
  123. <tr>
  124. <th>Original</th>
  125. <th>Highlighted</th>
  126. </tr>
  127. <?php foreach ($statements as $sql) { ?>
  128. <tr>
  129. <td>
  130. <pre><?php echo $sql; ?></pre>
  131. </td>
  132. <td><?php echo SqlFormatter::highlight($sql); ?></td>
  133. </tr>
  134. <?php } ?>
  135. </table>
  136. <h1>Splitting SQL Strings Into Individual Queries</h1>
  137. <div>
  138. Usage:
  139. <pre>
  140. <?php highlight_string('<?php' . "\n" . '$queries = SqlFormatter::splitQuery($sql);' . "\n" . '?>'); ?>
  141. </pre>
  142. </div>
  143. <table>
  144. <tr>
  145. <th>Original</th>
  146. <th>Split</th>
  147. </tr>
  148. <?php foreach ($split_statements as $sql) { ?>
  149. <tr>
  150. <td>
  151. <pre><?php echo SqlFormatter::highlight($sql); ?></pre>
  152. </td>
  153. <td><?php
  154. $queries = SqlFormatter::splitQuery($sql);
  155. echo "<ol>";
  156. foreach ($queries as $query) {
  157. echo "<li><pre>" . SqlFormatter::highlight($query) . "</pre></li>";
  158. }
  159. echo "</ol>";
  160. ?></td>
  161. </tr>
  162. <?php } ?>
  163. </table>
  164. <h1>Removing Comments</h1>
  165. <div>
  166. Usage:
  167. <pre>
  168. <?php highlight_string('<?php' . "\n" . '$nocomments = SqlFormatter::removeComments($sql);' . "\n" . '?>'); ?>
  169. </pre>
  170. </div>
  171. <table>
  172. <tr>
  173. <th>Original</th>
  174. <th>Comments Removed</th>
  175. </tr>
  176. <?php foreach ($comment_statements as $sql) { ?>
  177. <tr>
  178. <td>
  179. <pre><?php echo SqlFormatter::highlight($sql); ?></pre>
  180. </td>
  181. <td>
  182. <pre><?php echo SqlFormatter::highlight(SqlFormatter::removeComments($sql)) ?></pre>
  183. </td>
  184. </tr>
  185. <?php } ?>
  186. </table>
  187. </body>
  188. </html>