Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Are prepared statements supposed to bypass PARSE? #146

Open
horrendo opened this issue Jul 7, 2018 · 4 comments
Open

Are prepared statements supposed to bypass PARSE? #146

horrendo opened this issue Jul 7, 2018 · 4 comments

Comments

@horrendo
Copy link

horrendo commented Jul 7, 2018

Maybe I'm doing something wrong here (or misunderstanding things) but this returns true:

puts "Prepared: #{db.prepared_statements?}"

I then ran this:

db.transaction do |trx|
  e1 = Time.measure do
    stmt = trx.connection.build("insert into junk(c1, c2, c3) values ($1, $2, $3)")
    10.times do |i|
      stmt.exec(i, ">" + i.to_s + "<", i & 2 == 0)
    end
  end
  puts "e1 = #{e1}"
  e2 = Time.measure do
    10.times do |i|
      trx.connection.exec("insert into junk(c1, c2, c3) values ($1, $2, $3)", i, ">" + i.to_s + "<", i & 2 == 0)
    end
  end
  puts "e2 = #{e2}"
  trx.rollback
end

I tailed the database log file and expected to see 1 parse followed by 10 sets of bind and execute with the first block, and 10 sets of parse, bind and execute with the second block. However, in both cases I see 10 sets of parse, bind and execute, so I'm wondering if I've misunderstood the purpose of Connection#build.

I noticed #97 so I'm wondering why the pg driver returns true for Connection#prepared_statements? ?

Thanks,

Steve

@jwoertink
Copy link
Contributor

I'm also running in to this. Are prepared statements properly supported?

@bcardiff
Copy link
Collaborator

The entry point for prepared vs unprepared statement is at: https://github.com/will/crystal-pg/blob/master/src/pg/connection.cr#L20-L26 . They are all unprepared under the hood.

@jwoertink
Copy link
Contributor

So this line should probably look more like PreparedStatement.new(self, query) or whatever?

@bcardiff
Copy link
Collaborator

Maybe. It depends on the underlying implementation or protocol. In mysql the binary and text protocol that are used for prepared or unprepared statements is very different. In sqlite all the statements are prepared. I don’t know about the internals of pg protocol.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants