Method: Baza::Driver::MysqlJava#insert_multi
- Defined in:
- lib/baza/driver/mysql_java.rb
#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 |