Class: Baza::MysqlBaseDriver

Inherits:
BaseSqlDriver show all
Defined in:
lib/baza/mysql_base_driver.rb

Direct Known Subclasses

Driver::Mysql, Driver::Mysql2

Constant Summary

Constants inherited from BaseSqlDriver

BaseSqlDriver::SELECT_ARGS_ALLOWED_KEYS

Instance Attribute Summary

Attributes inherited from BaseSqlDriver

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseSqlDriver

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

Constructor Details

This class inherits a constructor from Baza::BaseSqlDriver

Class Method Details

.argsObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/baza/mysql_base_driver.rb', line 2

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

Instance Method Details

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

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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
# File 'lib/baza/mysql_base_driver.rb', line 25

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(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})." unless ids_length == arr_hashes_length

    return ids
  else
    return nil
  end
end

#transactionObject



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/baza/mysql_base_driver.rb', line 101

def transaction
  @db.q("START TRANSACTION")

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