examples.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. -- Drop the table first if it exists
  64. DROP TABLE IF EXISTS MyTable;
  65. -- Create the table
  66. CREATE TABLE MyTable ( id int );
  67. -- Insert values
  68. INSERT INTO MyTable (id)
  69. VALUES
  70. (1),(2),(3),(4);
  71. -- Done",
  72. );
  73. // Example statements for removing comments
  74. $comment_statements = array(
  75. "-- This is a comment
  76. SELECT
  77. /* This is another comment
  78. On more than one line */
  79. Id #This is one final comment
  80. as temp, DateCreated as Created FROM MyTable;",
  81. );
  82. ?>
  83. <h1>Formatting And Syntax Highlighting</h1>
  84. <div>
  85. Usage:
  86. <pre>
  87. <?php highlight_string('<?php' . "\n" . '$formatted = SqlFormatter::format($sql);' . "\n" . '?>'); ?>
  88. </pre>
  89. </div>
  90. <table>
  91. <tr>
  92. <th>Original</th>
  93. <th>Formatted And Highlighted</th>
  94. </tr>
  95. <?php foreach ($statements as $sql) { ?>
  96. <tr>
  97. <td>
  98. <pre><?php echo $sql; ?></pre>
  99. </td>
  100. <td><?php echo SqlFormatter::format($sql); ?></td>
  101. </tr>
  102. <?php } ?>
  103. </table>
  104. <h1>Formatting Only</h1>
  105. <div>
  106. Usage:
  107. <pre>
  108. <?php highlight_string('<?php' . "\n" . '$formatted = SqlFormatter::format($sql, false);' . "\n" . '?>'); ?>
  109. </pre>
  110. </div>
  111. <table>
  112. <tr>
  113. <th>Original</th>
  114. <th>Formatted</th>
  115. </tr>
  116. <?php foreach ($statements as $sql) { ?>
  117. <tr>
  118. <td>
  119. <pre><?php echo $sql; ?></pre>
  120. </td>
  121. <td><pre><?php echo htmlentities(SqlFormatter::format($sql,false)); ?></pre></td>
  122. </tr>
  123. <?php } ?>
  124. </table>
  125. <h1>Syntax Highlighting Only</h1>
  126. <div>
  127. Usage:
  128. <pre>
  129. <?php highlight_string('<?php' . "\n" . '$highlighted = SqlFormatter::highlight($sql);' . "\n" . '?>'); ?>
  130. </pre>
  131. </div>
  132. <table>
  133. <tr>
  134. <th>Original</th>
  135. <th>Highlighted</th>
  136. </tr>
  137. <?php foreach ($statements as $sql) { ?>
  138. <tr>
  139. <td>
  140. <pre><?php echo $sql; ?></pre>
  141. </td>
  142. <td><?php echo SqlFormatter::highlight($sql); ?></td>
  143. </tr>
  144. <?php } ?>
  145. </table>
  146. <h1>Splitting SQL Strings Into Individual Queries</h1>
  147. <div>
  148. Usage:
  149. <pre>
  150. <?php highlight_string('<?php' . "\n" . '$queries = SqlFormatter::splitQuery($sql);' . "\n" . '?>'); ?>
  151. </pre>
  152. </div>
  153. <table>
  154. <tr>
  155. <th>Original</th>
  156. <th>Split</th>
  157. </tr>
  158. <?php foreach ($split_statements as $sql) { ?>
  159. <tr>
  160. <td>
  161. <pre><?php echo SqlFormatter::highlight($sql); ?></pre>
  162. </td>
  163. <td><?php
  164. $queries = SqlFormatter::splitQuery($sql);
  165. echo "<ol>";
  166. foreach ($queries as $query) {
  167. echo "<li><pre>" . SqlFormatter::highlight($query) . "</pre></li>";
  168. }
  169. echo "</ol>";
  170. ?></td>
  171. </tr>
  172. <?php } ?>
  173. </table>
  174. <h1>Removing Comments</h1>
  175. <div>
  176. Usage:
  177. <pre>
  178. <?php highlight_string('<?php' . "\n" . '$nocomments = SqlFormatter::removeComments($sql);' . "\n" . '?>'); ?>
  179. </pre>
  180. </div>
  181. <table>
  182. <tr>
  183. <th>Original</th>
  184. <th>Comments Removed</th>
  185. </tr>
  186. <?php foreach ($comment_statements as $sql) { ?>
  187. <tr>
  188. <td>
  189. <pre><?php echo SqlFormatter::highlight($sql); ?></pre>
  190. </td>
  191. <td>
  192. <pre><?php echo SqlFormatter::highlight(SqlFormatter::removeComments($sql)) ?></pre>
  193. </td>
  194. </tr>
  195. <?php } ?>
  196. </table>
  197. </body>
  198. </html>