RE: REGEX for query

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jay Blanchard wrote:
> I have a rather interesting issue. I need to locate every query in
> every PHP application we have for an integration project. I have
> started doing some research, but I wanted throw this out there as a
> little exercize because it is interesting.
>
> Several queries are written as
[...]
> How would you begin to get the queries using PHP? Regex? Other
> methods? I am working on isolating where the applications live right
> now, but I would be thrilled to read your opinions and methods.

Assuming unix, I'd do the following from the root of the application to get a list
of files that contain queries:

$ egrep "=[:space:]*\".*\b(SELECT|INSERT|UPDATE)\b" * -ril

That is assuming that you always assign a query to a variable before executing it,
and the select|insert|update is always on the same line as the assignment operator.
For example, the above would not catch:

$variableName =
  "SELECT foo .... ";

But if you're reasonably sure you don't do that anywhere then it might be enough.

I'm assuming that at some point you're going to want to update those queries, or the
code surrounding them.  I'd use a good editor that supports regex searches across
multiple files.  I'd suggest jEdit (www.jedit.org); it's quite powerful and free,
but your favorite editor may have similar functionality already.  My next step would
be to load all of the files that egrep found for me into jEdit, then do a regex
search across all buffers for a similar pattern (
=\s*".*\b(SELECT|INSERT|UPDATE)\b ).  jEdit also has a feature known as HyperSearch
which opens a dockable window that lists every occurrence by file and line number,
and allows you to jump from occurrence to occurence by clicking on each one.  I'd
then hit each one and do the appropriate update (that is, assuming the update in
question isn't so simple that it can be done with a global search and replace).

Anyway, that's how I'd do it.  Hope you got something out of this... :)

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux