You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
3.2 KiB

26 years ago
26 years ago
26 years ago
  1. #!@PERL@
  2. # Copyright (C) 2000, 2004 MySQL AB
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; version 2 of the License.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. $version="1.02";
  17. use Getopt::Long;
  18. $opt_help=$opt_Information=$opt_skip_use_db=0;
  19. $opt_regexp=$opt_dbregexp=".*";
  20. $opt_start_row=1; $opt_rows=9999999999;
  21. GetOptions("Information","help","regexp=s","start_row=i","rows=i",
  22. "dbregexp=s", "skip-use-db")
  23. || usage();
  24. usage() if ($opt_help || $opt_Information);
  25. $query=$search=$database=$set=""; $eoq=0;
  26. while (<>)
  27. {
  28. next if (length($query) == 0 && /^\#/); # Skip comments
  29. $query.=search($_);
  30. if ($eoq)
  31. {
  32. if ($query =~ /^use /i || $query =~ /^SET / ||
  33. ($query =~ /$opt_regexp/o && $database =~ /$opt_dbregexp/o))
  34. {
  35. if ($opt_skip_use_db && $query =~ /^use /i)
  36. {
  37. $query="";
  38. next;
  39. }
  40. if ($opt_start_row <= 1)
  41. {
  42. if ($database)
  43. {
  44. print $database, $set;
  45. $database=$set="";
  46. }
  47. print $query;
  48. last if (--$opt_rows == 0);
  49. }
  50. else
  51. {
  52. $opt_start_row--;
  53. if ($query =~ /^use /)
  54. {
  55. $database=$query;
  56. $set="";
  57. }
  58. elsif ($query =~ /^SET/)
  59. {
  60. $set=$query;
  61. }
  62. else
  63. {
  64. $set="";
  65. }
  66. }
  67. }
  68. $query=""; $search=""; $eoq=0;
  69. }
  70. }
  71. exit 0;
  72. sub search
  73. {
  74. my($row)=shift;
  75. my($i);
  76. for ($i=0 ; $i < length($row) ; $i++)
  77. {
  78. if (length($search))
  79. {
  80. if (length($search) > 1)
  81. { # Comment
  82. next if (substr($row,$i,length($search)) ne $search);
  83. $i+=length($search)-1;
  84. $search="";
  85. }
  86. elsif (substr($row,$i,1) eq '\\') # Escaped char in string
  87. {
  88. $i++;
  89. }
  90. elsif (substr($row,$i,1) eq $search)
  91. {
  92. if (substr($row,$i+1,1) eq $search) # Double " or '
  93. {
  94. $i++;
  95. }
  96. else
  97. {
  98. $search="";
  99. }
  100. }
  101. next;
  102. }
  103. if (substr($row,$i,2) eq '/*') # Comment
  104. {
  105. $search="*/";
  106. $i++;
  107. }
  108. elsif (substr($row,$i,1) eq "'" || substr($row,$i,1) eq '"')
  109. {
  110. $search=substr($row,$i,1);
  111. }
  112. }
  113. $eoq=1 if (!length($search) && $row =~ /;\s*$/);
  114. return $row;
  115. }
  116. sub usage
  117. {
  118. print <<EOF;
  119. $0 Ver $version
  120. Prints all SQL queries that matches a regexp or contains a 'use
  121. database' or 'set ..' command to stdout. A SQL query may contain
  122. newlines. This is useful to find things in a MySQL update log.
  123. $0 takes the following options:
  124. --help or --Information
  125. Shows this help
  126. --regexp=#
  127. Print queries that matches this.
  128. --start_row=#
  129. Start output from this row (first row = 1)
  130. --skip-use-db
  131. Don\'t include \'use database\' commands in the output.
  132. --rows=#
  133. Quit after this many rows.
  134. Example:
  135. $0 --regexp "problem_table" < update.log
  136. $0 --regexp "problem_table" update-log.1 update-log.2
  137. EOF
  138. exit(0);
  139. }