Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prepared statements in rails/activerecord

does rails activerecord have any support for prepared statements? best I can tell -no.

like image 469
phil swenson Avatar asked Jan 10 '10 17:01

phil swenson


People also ask

Are Prepared statements precompiled?

When you use prepared statement(i.e pre-compiled statement), As soon as DB gets this statement, it compiles it and caches it so that it can use the last compiled statement for successive call of same statement. So it becomes pre-compiled for successive calls.

Where are prepared statements stored?

Query result obtained by SELECT type prepared statement is stored in query cache on a universal basis independent of the source type of query and can be reused both by another execution of this or another prepared statement and by direct query when its text is identical to the SQL text of the first prepared statement.

What are the benefits of using prepared statements?

Prepared statements offer two major benefits: The query only needs to be parsed (or prepared) once, but can be executed multiple times with the same or different parameters. When the query is prepared, the database will analyze, compile and optimize its plan for executing the query.

What is a prepared SQL statement?

A prepared statement is a parameterized and reusable SQL query which forces the developer to write the SQL command and the user-provided data separately. The SQL command is executed safely, preventing SQL Injection vulnerabilities.


2 Answers

Since Rails 3.1 prepared statements in active_record are in place. See this presentation http://www.youtube.com/watch?v=kWOAHIpmLAI by Aaron Patterson.

like image 113
mehowte Avatar answered Nov 15 '22 04:11

mehowte


There are recent patches available to do this for mysql:

https://rails.lighthouseapp.com/projects/8994/tickets/3151-mysql-adapter-update-to-enable-use-of-stored-procedures

For Oracle, there are 2 related projects that seem like they do the trick:

  • plsql support http://github.com/rsim/ruby-plsql

  • more goodies when using Oracle and AR, with use of ruby-plsql gem for stored procs http://wiki.github.com/rsim/oracle-enhanced/

like image 26
Andrew Kuklewicz Avatar answered Nov 15 '22 06:11

Andrew Kuklewicz