Class: Sequel::Mysql2::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/midori-contrib/sequel/mysql2.rb

Overview

Midori Extension of sequel MySQL through meta programming

Instance Method Summary collapse

Instance Method Details

#_execute(conn, sql, opts, &block) ⇒ Mysql2::Result

Execute the given SQL on the given connection. If the :type option is :select, yield the result of the query, otherwise yield the connection if a block is given.

Parameters:

  • conn (Mysql2::Client)

    connection to database

  • sql (String)

    sql query

  • opts (Hash)

    optional options

Returns:

  • (Mysql2::Result)

    MySQL results



23
24
25
26
27
28
29
30
31
32
# File 'lib/midori-contrib/sequel/mysql2.rb', line 23

def _execute(conn, sql, opts, &block)
  # _execute_nonblock(conn, sql, opts, &block)
  if Fiber.current == EventLoop.root_fiber
    # Block usage
    return _execute_block(conn, sql, opts, &block)
  else
    # Nonblock usage
    return _execute_nonblock(conn, sql, opts, &block)
  end
end

#_execute_blockObject



14
# File 'lib/midori-contrib/sequel/mysql2.rb', line 14

alias_method :_execute_block, :_execute

#_execute_nonblock(conn, sql, opts) ⇒ Mysql2::Result

Execute the given SQL on the given connection. If the :type option is :select, yield the result of the query, otherwise yield the connection if a block is given.

Parameters:

  • conn (Mysql2::Client)

    connection to database

  • sql (String)

    sql query

  • opts (Hash)

    optional options

Returns:

  • (Mysql2::Result)

    MySQL results



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/midori-contrib/sequel/mysql2.rb', line 41

def _execute_nonblock(conn, sql, opts) # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity
  begin
    # :nocov:
    stream = opts[:stream]
    if NativePreparedStatements
      if (args = opts[:arguments])
        args = args.map{|arg| bound_variable_value(arg)}
      end

      case sql
        when ::Mysql2::Statement
          stmt = sql
        when Dataset
          sql = sql.sql
          close_stmt = true
          stmt = conn.prepare(sql)
      end
    end

    r = log_connection_yield((log_sql = opts[:log_sql]) ? sql + log_sql : sql, conn, args) do
      if stmt
        conn.query_options.merge!(cache_rows: true,
                                  database_timezone: timezone,
                                  application_timezone: Sequel.application_timezone,
                                  stream: stream,
                                  cast_booleans: convert_tinyint_to_bool)
        stmt.execute(*args)
        # :nocov:
      else
        if MYSQL_SOCKETS[conn.socket].nil?
          MYSQL_SOCKETS[conn.socket] = IO::open(conn.socket)
        end
        socket = MYSQL_SOCKETS[conn.socket]
        await(Promise.new do |resolve|
          count = 0
          EventLoop.register(socket, :rw) do
            if (count == 0)
              # Writable
              count += 1
              conn.query(sql,
                        database_timezone: timezone,
                        application_timezone: Sequel.application_timezone,
                        stream: stream,
                        async: true)
            else
              # Readable
              begin
                EventLoop.deregister(socket)
                resolve.call(conn.async_result)
              rescue ::Mysql2::Error => e
                resolve.call(PromiseException.new(e))
                next
              end
            end
          end
        end)
      end
    end

    # :nocov:
    if opts[:type] == :select
      if r
        if stream
          begin
            r2 = yield r
          ensure
            # If r2 is nil, it means the block did not exit normally,
            # so the rest of the results must be drained to prevent
            # "commands out of sync" errors.
            r.each{} unless r2
          end
        else
          yield r
        end
      end
    elsif block_given?
      yield conn
    end
  rescue ::Mysql2::Error => e
    raise_error(e)
  ensure
    if stmt
      conn.query_options.replace(conn.instance_variable_get(:@sequel_default_query_options))
      stmt.close if close_stmt
    end
    # :nocov:
  end
end