-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
mysql.rb
55 lines (47 loc) · 1.39 KB
/
mysql.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
require 'sequel_core/adapters/shared/mysql'
module Sequel
module JDBC
module MySQL
module DatabaseMethods
include Sequel::MySQL::DatabaseMethods
def dataset(opts=nil)
Sequel::JDBC::MySQL::Dataset.new(self, opts)
end
def execute_insert(sql)
begin
log_info(sql)
@pool.hold do |conn|
stmt = conn.createStatement
begin
stmt.executeUpdate(sql)
rs = stmt.executeQuery('SELECT LAST_INSERT_ID()')
rs.next
rs.getInt(1)
rescue NativeException, JavaSQL::SQLException => e
raise Error, e.message
ensure
stmt.close
end
end
rescue NativeException, JavaSQL::SQLException => e
raise Error, "#{sql}\r\n#{e.message}"
end
end
private
def database_name
u = URI.parse(uri.sub(/\Ajdbc:/, ''))
(m = /\/(.*)/.match(u.path)) && m[1]
end
end
class Dataset < JDBC::Dataset
include Sequel::MySQL::DatasetMethods
def insert(*values)
@db.execute_insert(insert_sql(*values))
end
def replace(*args)
@db.execute_insert(replace_sql(*args))
end
end
end
end
end