I'm writing a semi-simple database wrapper class and want to have a fetching method which would operate automagically: it should prepare each different statement only the first time around and just bind and execute the query on successive calls.
I guess the main question is: How does re-preparing the same MySql statement work, will PDO magically recognize the statement (so I don't have to) and cease the operation?
If not, I'm planning to achieve do this by generating a unique key for each different query and keep the prepared statements in a private array in the database object - under its unique key. I'm planning to obtain the array key in one of the following ways (none of which I like). In order of preference:
- have the programmer pass an extra, always the same parameter when calling the method - something along the lines of
basename(__FILE__, ".php") . __LINE__
(this method would work only if our method is called within a loop - which is the case most of the time this functionality is needed) - have the programmer pass a totally random string (most likely generated beforehand) as an extra parameter
- use the passed query itself to generate the key - getting the hash of the query or something similar
- achieve the same as the first bullet (above) by calling
debug_backtrace
Has anyone similar experience? Although the system I'm working for does deserve some attention to optimization (it's quite large and growing by the week), perhaps I'm worrying about nothing and there is no performance benefit in doing what I'm doing?