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

mysql2 MULTI_STATEMENTS + ActiveRecord error and patch ... #31569

Closed
JokerCatz opened this Issue Dec 26, 2017 · 2 comments

Comments

Projects
None yet
3 participants
@JokerCatz

JokerCatz commented Dec 26, 2017

if I need multi sql statements in mysql , I need add flag MULTI_STATEMENTS in database.yml
like

default: &default
  adapter: mysql2
  {SKIP}
  flags:
    - MULTI_STATEMENTS

then when I use

ActiveRecord::Base.connection.execute("select 1;" * 10)
  #=> only 1 result
ActiveRecord::Base.connection.execute("select 1;" * 10)
  #=> ActiveRecord::StatementInvalid: Mysql2::Error: Commands out of sync; you can't run this command now
ActiveRecord::Base.connection.execute("select 1;" * 10)
  #=> ActiveRecord::StatementInvalid: Mysql2::Error: MySQL client is not connected

then I found it not pull back all result in multi sql statements , and second execute will be fail ... so I try to patch like this (just demo code)

class ActiveRecord::Base
  def self.multi_sql(safe_raw_sql)
    db_client = ActiveRecord::Base.connection.raw_connection
    datas = []
    begin
      db_client.query("BEGIN")
      db_client.query(safe_raw_sql)
      while data = db_client.next_result
        datas << db_client.store_result
      end
      db_client.query("COMMIT")
    rescue
      db_client.query("ROLLBACK")
    end
    return datas
  end
end

then same demo code will be work but return ugly result obj ...... so ... has any another way to do it ? or just help me fix to ActiveRecord and help another people? becourse it didn't need to reset/clear any connection or something else ...

System configuration

Rails 5.1.4 , Ruby 2.4.1p111

@sodabrew

This comment has been minimized.

Show comment
Hide comment
@sodabrew

sodabrew Mar 22, 2018

Contributor

Hello! I maintain the mysql2 gem. Subscribing to this ticket to keep up with any updates. I don't have any specific advice here for what ActiveRecord should do to handle multiple statements, but I'm interested in participating in the conversation if/when.

Contributor

sodabrew commented Mar 22, 2018

Hello! I maintain the mysql2 gem. Subscribing to this ticket to keep up with any updates. I don't have any specific advice here for what ActiveRecord should do to handle multiple statements, but I'm interested in participating in the conversation if/when.

@rafaelfranca

This comment has been minimized.

Show comment
Hide comment
@rafaelfranca

rafaelfranca Mar 22, 2018

Member

For security reason Active Record don't support multiple statements in the same string. It is possible to use it and we even use internally, but we don't officially support it.

Member

rafaelfranca commented Mar 22, 2018

For security reason Active Record don't support multiple statements in the same string. It is possible to use it and we even use internally, but we don't officially support it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment