Is there any way to intercept every SQL that passes through a connection and change it?

Is there any way to intercept every SQL that passes through a connection and change it?
For example:
I have a TQuery with "Select First 1 * From TABLE";
I want to intercept it and edit the SQL to "Select * From TABLE Where ROWNUM <= 1"

Comments

  1. - dll inject into process
    - create db proxy

    ReplyDelete
  2. You don't say what connection you use, but 
    http://theroadtodelphi.wordpress.com/2010/02/21/build-your-own-profiler-using-ado/ describes how to write an interceptor for ADO.

    ReplyDelete
  3. Lars Fosdal We use AnyDAC. I am reading that right now, thanks.

    ReplyDelete
  4. Lars Fosdal TADConnection doesn't have the events necessary to implement that solution :\

    ReplyDelete
  5. Ingo Wagner Jr. as far as I know(and I don't know much about it), AnyDac should have a SQL Monitor component.

    Or am I missing something?

    Regards,

    A

    ReplyDelete
  6. I had a look at FireDAC and it looks like TADConnection uses it's ConnectionIntf to create the SQL, which again is retrieved from the ADPhysManager and another property defines what TADPhysCommandKind to use.  Perhaps it is possible to inject something through there?

    ReplyDelete
  7. If you have access to the original source code, you can create a new class inherited from tquery something like tlogquery and override required methods. Then you replace old tquery declaration with the new one tlogquery

    ReplyDelete
  8. Rafa Botero - Unfortunately, a lot of these methods are not virtual - at least not in FireDAC.

    ReplyDelete
  9. Hmm... that interceptor thing has promise!

    Translate "select * from inventory"

    to

    insert into accountspayable (accountid, amt) values ((select id from accounts where firstname='kevin' and lastname='mccoy'), 1000.0)

    I can retire! :-)

    ReplyDelete

Post a Comment