Class: Baza::Driver::MysqlJava

Inherits:
JdbcDriver show all
Defined in:
lib/baza/driver/mysql_java.rb

Defined Under Namespace

Classes: Column, Columns, Commands, Database, Databases, Index, Indexes, Table, Tables, User, Users

Constant Summary

Constants inherited from BaseSqlDriver

BaseSqlDriver::SELECT_ARGS_ALLOWED_KEYS

Instance Attribute Summary collapse

Attributes inherited from BaseSqlDriver

#cols, #db, #indexes, #sep_col, #sep_database, #sep_index, #sep_table, #sep_val, #tables

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from JdbcDriver

#query, #query_ubuf, #result_set_killer

Methods inherited from BaseSqlDriver

#count, #delete, #escape, #escape_column, #escape_database, #escape_index, #escape_table, #insert, #select, #single, #sql_make_where, #sqlval, #supports_multiple_databases?

Constructor Details

#initialize(db) ⇒ MysqlJava

Returns a new instance of MysqlJava.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/baza/driver/mysql_java.rb', line 45

def initialize(db)
  super

  @opts = @db.opts
  @encoding = @opts[:encoding] || "utf8"

  if @db.opts.key?(:port)
    @port = @db.opts[:port].to_i
  else
    @port = 3306
  end

  @java_rs_data = {}
  reconnect
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



6
7
8
# File 'lib/baza/driver/mysql_java.rb', line 6

def conn
  @conn
end

#connsObject (readonly)

Returns the value of attribute conns.



6
7
8
# File 'lib/baza/driver/mysql_java.rb', line 6

def conns
  @conns
end

Class Method Details

.argsObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/baza/driver/mysql_java.rb', line 23

def self.args
  [{
    label: "Host",
    name: "host"
  }, {
    label: "Port",
    name: "port"
  }, {
    label: "Username",
    name: "user"
  }, {
    label: "Password",
    name: "pass"
  }, {
    label: "Database",
    name: "db"
  }, {
    label: "Encoding",
    name: "encoding"
  }]
end

.from_object(args) ⇒ Object

Helper to enable automatic registering of database using Baza::Db.from_object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/baza/driver/mysql_java.rb', line 9

def self.from_object(args)
  if args[:object].class.name == "Java::ComMysqlJdbc::JDBC4Connection"
    return {
      type: :success,
      args: {
        type: :mysql_java,
        conn: args[:object]
      }
    }
  end

  nil
end

Instance Method Details

#closeObject

Closes the connection threadsafe.



78
79
80
# File 'lib/baza/driver/mysql_java.rb', line 78

def close
  @mutex.synchronize { @conn.close }
end

#destroyObject

Destroyes the connection.



83
84
85
86
87
88
89
90
# File 'lib/baza/driver/mysql_java.rb', line 83

def destroy
  @conn = nil
  @db = nil
  @mutex = nil
  @encoding = nil
  @query_args = nil
  @port = nil
end

#insert_multi(tablename, arr_hashes, args = {}) ⇒ Object

Inserts multiple rows in a table. Can return the inserted IDs if asked to in arguments.



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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/baza/driver/mysql_java.rb', line 93

def insert_multi(tablename, arr_hashes, args = {})
  sql = "INSERT INTO `#{tablename}` ("

  first = true
  if args && args[:keys]
    keys = args[:keys]
  elsif arr_hashes.first.is_a?(Hash)
    keys = arr_hashes.first.keys
  else
    raise "Could not figure out keys."
  end

  keys.each do |col_name|
    sql << "," unless first
    first = false if first
    sql << "`#{escape_column(col_name)}`"
  end

  sql << ") VALUES ("

  first = true
  arr_hashes.each do |hash|
    if first
      first = false
    else
      sql << "),("
    end

    first_key = true
    if hash.is_a?(Array)
      hash.each do |val|
        if first_key
          first_key = false
        else
          sql << ","
        end

        sql << @db.sqlval(val)
      end
    else
      hash.each do |_key, val|
        if first_key
          first_key = false
        else
          sql << ","
        end

        sql << @db.sqlval(val)
      end
    end
  end

  sql << ")"

  return sql if args && args[:return_sql]

  query_no_result_set(sql)

  if args && args[:return_id]
    first_id = last_id
    raise "Invalid ID: #{first_id}" if first_id.to_i <= 0
    ids = [first_id]
    1.upto(arr_hashes.length - 1) do |count|
      ids << first_id + count
    end

    ids_length = ids.length
    arr_hashes_length = arr_hashes.length
    raise "Invalid length (#{ids_length}, #{arr_hashes_length})." if ids_length != arr_hashes_length

    return ids
  else
    return nil
  end
end

#reconnectObject

Respawns the connection to the MySQL-database.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/baza/driver/mysql_java.rb', line 62

def reconnect
  @mutex.synchronize do
    if @db.opts[:conn]
      @jdbc_loaded = true
      @conn = @db.opts.fetch(:conn)
    else
      com.mysql.jdbc.Driver
      @conn = java.sql::DriverManager.getConnection(jdbc_connect_command)
    end

    query_no_result_set("SET SQL_MODE = ''")
    query_no_result_set("SET NAMES '#{esc(@encoding)}'") if @encoding
  end
end

#transactionObject



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/baza/driver/mysql_java.rb', line 169

def transaction
  query_no_result_set("START TRANSACTION")

  begin
    yield @db
    query_no_result_set("COMMIT")
  rescue
    query_no_result_set("ROLLBACK")
    raise
  end
end