dohmysql 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bin/makedb +1 -1
- data/lib/doh/mysql/cache_connector.rb +3 -2
- data/lib/doh/mysql/db_date.rb +2 -2
- data/lib/doh/mysql/handle.rb +2 -38
- data/lib/doh/mysql/to_sql.rb +6 -0
- data/lib/doh/mysql/typed_row_builder.rb +11 -8
- data/test/connector_instance.dt.rb +1 -1
- data/test/handle.dt.rb +1 -47
- data/test/types.dt.rb +15 -19
- metadata +28 -6
- data/lib/doh/mysql/default_type_guesser.rb +0 -37
data/bin/makedb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
+
require 'mysql2'
|
1
2
|
require 'doh/mysql/handle'
|
2
3
|
require 'doh/mysql/typed_row_builder'
|
4
|
+
Mysql2::Client.default_query_options[:cast_booleans] = true
|
3
5
|
|
4
6
|
module DohDb
|
5
7
|
|
@@ -41,8 +43,7 @@ private
|
|
41
43
|
database ||= @database
|
42
44
|
dbmsg = database.to_s.strip.empty? ? 'no default database' : "database #{database}"
|
43
45
|
dohlog.info("connecting to #@host port #@port as username #@username, #{dbmsg}")
|
44
|
-
mysqlh =
|
45
|
-
mysqlh.query_with_result = false
|
46
|
+
mysqlh = Mysql2::Client.new(:host => @host, :username => @username, :password => @password, :database => database, :port => @port)
|
46
47
|
Handle.new(mysqlh, @row_builder)
|
47
48
|
end
|
48
49
|
|
data/lib/doh/mysql/db_date.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require 'doh/core_ext/datewithtime'
|
2
2
|
require 'doh/mysql/to_sql'
|
3
3
|
|
4
4
|
module DohDb
|
@@ -21,7 +21,7 @@ def self.today
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def self.now
|
24
|
-
dt = DateTime.
|
24
|
+
dt = DateTime.now.utc
|
25
25
|
DateTimeNow.new(dt.year, dt.month, dt.mday, dt.hour, dt.min, dt.sec, dt.zone)
|
26
26
|
end
|
27
27
|
|
data/lib/doh/mysql/handle.rb
CHANGED
@@ -53,7 +53,7 @@ class Handle
|
|
53
53
|
|
54
54
|
def insert(statement)
|
55
55
|
generic_query(statement)
|
56
|
-
retval = @mysqlh.
|
56
|
+
retval = @mysqlh.last_id
|
57
57
|
dohlog.info("insert_id was #{retval}")
|
58
58
|
retval
|
59
59
|
end
|
@@ -71,9 +71,8 @@ class Handle
|
|
71
71
|
# It calls to_s on the statement object to facilitate the use of sql builder objects.
|
72
72
|
def select(statement, row_builder = nil)
|
73
73
|
result_set = generic_query(statement)
|
74
|
-
dohlog.info("selected #{result_set.
|
74
|
+
dohlog.info("selected #{result_set.size} rows")
|
75
75
|
rows = get_row_builder(row_builder).build_rows(result_set)
|
76
|
-
result_set.free
|
77
76
|
rows
|
78
77
|
end
|
79
78
|
|
@@ -140,46 +139,11 @@ class Handle
|
|
140
139
|
select(statement, row_builder).collect { |row| row.at(0) }
|
141
140
|
end
|
142
141
|
|
143
|
-
def multi_select(statement_infos, dflt_row_builder = nil)
|
144
|
-
statements = []
|
145
|
-
row_builders = []
|
146
|
-
statement_infos.each do |info|
|
147
|
-
if info.is_a?(Array)
|
148
|
-
statements.push(info.first)
|
149
|
-
row_builders.push(info.last)
|
150
|
-
else
|
151
|
-
statements.push(info)
|
152
|
-
row_builders.push(nil)
|
153
|
-
end
|
154
|
-
end
|
155
|
-
sqlstr = statements.join(';')
|
156
|
-
|
157
|
-
dohlog.info(sqlstr)
|
158
|
-
@mysqlh.query(sqlstr)
|
159
|
-
retval = []
|
160
|
-
while true
|
161
|
-
custom_builder = row_builders.shift
|
162
|
-
if @mysqlh.field_count > 0
|
163
|
-
result_set = @mysqlh.store_result
|
164
|
-
dohlog.info("selected #{result_set.num_rows} rows")
|
165
|
-
rows = get_row_builder(custom_builder || dflt_row_builder).build_rows(result_set)
|
166
|
-
result_set.free
|
167
|
-
retval.push(rows)
|
168
|
-
end
|
169
|
-
break unless @mysqlh.next_result
|
170
|
-
end
|
171
|
-
retval
|
172
|
-
rescue Exception => excpt
|
173
|
-
dohlog.error("caught exception during query: #{sqlstr}", excpt)
|
174
|
-
raise
|
175
|
-
end
|
176
|
-
|
177
142
|
private
|
178
143
|
def generic_query(statement)
|
179
144
|
sqlstr = statement.to_s
|
180
145
|
dohlog.info(sqlstr)
|
181
146
|
@mysqlh.query(sqlstr)
|
182
|
-
@mysqlh.store_result if @mysqlh.field_count > 0
|
183
147
|
rescue Exception => excpt
|
184
148
|
dohlog.error("caught exception during query: #{sqlstr}", excpt)
|
185
149
|
raise
|
data/lib/doh/mysql/to_sql.rb
CHANGED
@@ -1,25 +1,28 @@
|
|
1
1
|
require 'doh/mysql/readonly_row'
|
2
|
-
require 'doh/mysql/default_type_guesser'
|
3
2
|
|
4
3
|
module DohDb
|
5
4
|
|
6
5
|
class TypedRowBuilder
|
7
6
|
def initialize(row_klass = nil, guesser = nil)
|
8
7
|
@row_klass = row_klass || ReadOnlyRow
|
9
|
-
@guesser = guesser || DefaultTypeGuesser
|
10
8
|
end
|
11
9
|
|
12
10
|
def build_rows(result_set)
|
13
|
-
|
14
|
-
field_names = meta_info.collect {|elem| elem.name}
|
11
|
+
return [] if result_set.size == 0
|
15
12
|
|
16
13
|
retval = []
|
17
14
|
result_set.each do |row|
|
18
|
-
|
19
|
-
|
20
|
-
|
15
|
+
keys = []
|
16
|
+
values = []
|
17
|
+
row.each_pair do |key, value|
|
18
|
+
keys.push(key)
|
19
|
+
if value.is_a?(Time)
|
20
|
+
values.push(value.to_datetime)
|
21
|
+
else
|
22
|
+
values.push(value)
|
23
|
+
end
|
21
24
|
end
|
22
|
-
retval.push(@row_klass.new(
|
25
|
+
retval.push(@row_klass.new(keys, values))
|
23
26
|
end
|
24
27
|
retval
|
25
28
|
end
|
@@ -16,7 +16,7 @@ class Test_ConnectorInstance < DohTest::TestGroup
|
|
16
16
|
assert_equal(nil, DohDb::select_optional_field("SELECT money FROM #{tbl} WHERE id = 7"))
|
17
17
|
assert_raises(UnexpectedQueryResult) {DohDb::select_field("SELECT money FROM #{tbl} WHERE id = 7")}
|
18
18
|
assert_equal(1, DohDb::update_row("UPDATE #{tbl} SET money = 10.95 WHERE id = 1"))
|
19
|
-
assert_raises(
|
19
|
+
assert_raises(Mysql2::Error){DohDb::query("some invalid sql here")}
|
20
20
|
# TODO: re-enable
|
21
21
|
# assert(DohTest::pop_error)
|
22
22
|
assert_raises(UnexpectedQueryResult) {DohDb::update_row("UPDATE #{tbl} SET money = 10.95 WHERE id = 7")}
|
data/test/handle.dt.rb
CHANGED
@@ -52,59 +52,13 @@ class Test_Handle < DohTest::TestGroup
|
|
52
52
|
assert_equal([['some_name']], DohDb::select_values("SELECT field FROM #{tbl}"))
|
53
53
|
end
|
54
54
|
|
55
|
-
def test_multi_select
|
56
|
-
dbh = DohDb::request_handle
|
57
|
-
tbl = "doh_mysql_handle_multi_select_test"
|
58
|
-
DohDb::query("CREATE TEMPORARY TABLE #{tbl} (value CHAR(30))")
|
59
|
-
DohDb::query("INSERT INTO #{tbl} SET value = 'first'")
|
60
|
-
DohDb::query("INSERT INTO #{tbl} SET value = 'second'")
|
61
|
-
|
62
|
-
stmts = []
|
63
|
-
stmts.push("SELECT * FROM #{tbl}")
|
64
|
-
stmts.push("INSERT INTO #{tbl} SET value = 'third'")
|
65
|
-
stmts.push(["SELECT * FROM #{tbl}", :hash])
|
66
|
-
stmts.push("SELECT * FROM #{tbl}")
|
67
|
-
stmts.push("INSERT INTO #{tbl} SET value = 'fourth'")
|
68
|
-
stmts.push(["SELECT * FROM #{tbl}", :smart])
|
69
|
-
select_results = DohDb::multi_select(stmts)
|
70
|
-
assert_equal(4, select_results.size)
|
71
|
-
assert_equal(4, DohDb::select_field("SELECT COUNT(*) FROM #{tbl}"))
|
72
|
-
|
73
|
-
curr_select = select_results[0]
|
74
|
-
assert_equal(2, curr_select.size)
|
75
|
-
assert_instance_of(ReadOnlyRow, curr_select[0])
|
76
|
-
assert_equal({'value' => 'first'}, curr_select[0].to_h)
|
77
|
-
assert_equal({'value' => 'second'}, curr_select[1].to_h)
|
78
|
-
|
79
|
-
curr_select = select_results[1]
|
80
|
-
assert_equal(3, curr_select.size)
|
81
|
-
assert_instance_of(Hash, curr_select[0])
|
82
|
-
assert_equal({'value' => 'first'}, curr_select[0].to_h)
|
83
|
-
assert_equal({'value' => 'second'}, curr_select[1].to_h)
|
84
|
-
assert_equal({'value' => 'third'}, curr_select[2].to_h)
|
85
|
-
|
86
|
-
curr_select = select_results[2]
|
87
|
-
assert_equal(3, curr_select.size)
|
88
|
-
assert_equal({'value' => 'first'}, curr_select[0].to_h)
|
89
|
-
assert_equal({'value' => 'second'}, curr_select[1].to_h)
|
90
|
-
assert_equal({'value' => 'third'}, curr_select[2].to_h)
|
91
|
-
|
92
|
-
curr_select = select_results[3]
|
93
|
-
assert_equal(4, curr_select.size)
|
94
|
-
assert_instance_of(SmartRow, curr_select[0])
|
95
|
-
assert_equal({'value' => 'first'}, curr_select[0].to_h)
|
96
|
-
assert_equal({'value' => 'second'}, curr_select[1].to_h)
|
97
|
-
assert_equal({'value' => 'third'}, curr_select[2].to_h)
|
98
|
-
assert_equal({'value' => 'fourth'}, curr_select[3].to_h)
|
99
|
-
end
|
100
|
-
|
101
55
|
def test_insert_hash
|
102
56
|
dbh = DohDb::request_handle
|
103
57
|
tbl = "doh_mysql_insert_hash_test"
|
104
58
|
dbh.query("CREATE TEMPORARY TABLE #{tbl} (value INT KEY)")
|
105
59
|
hash1 = {'value' => 1}
|
106
60
|
assert_equal(0, dbh.insert_hash(hash1, tbl))
|
107
|
-
assert_raises(
|
61
|
+
assert_raises(Mysql2::Error) { dbh.insert_hash(hash1, tbl) }
|
108
62
|
assert_equal(0, dbh.insert_hash(hash1, tbl, true))
|
109
63
|
end
|
110
64
|
end
|
data/test/types.dt.rb
CHANGED
@@ -3,29 +3,25 @@ require 'doh/mysql/types'
|
|
3
3
|
|
4
4
|
module DohDb
|
5
5
|
|
6
|
-
class ConvertToString
|
7
|
-
def self.build(field, value)
|
8
|
-
value.to_s
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
6
|
class Test_types < DohTest::TestGroup
|
13
7
|
def test_stuff
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
DohDb::query("CREATE TEMPORARY TABLE #{tbl} (amount INT)")
|
18
|
-
DohDb::query("INSERT INTO #{tbl} SET amount = NULL")
|
19
|
-
assert_equal(nil, DohDb::select_field("SELECT amount FROM #{tbl}"))
|
20
|
-
DohDb::query("UPDATE #{tbl} SET amount = 5")
|
21
|
-
assert_equal(5, DohDb::select_field("SELECT amount FROM #{tbl}"))
|
8
|
+
Mysql2::Client.default_query_options[:database_timezone] = :utc
|
9
|
+
Mysql2::Client.default_query_options[:application_timezone] = :utc
|
10
|
+
DohDb::connector_instance.reset
|
22
11
|
|
23
|
-
|
24
|
-
DohDb::query("
|
25
|
-
|
12
|
+
@tbl = "doh_mysql_type_conversion_tests"
|
13
|
+
DohDb::query("DROP TABLE IF EXISTS #@tbl")
|
14
|
+
DohDb::request_handle.query("CREATE TEMPORARY TABLE #{@tbl} (char_field CHAR(1) NOT NULL, int_field INT, bool_field TINYINT(1), date_field DATE, datetime_field DATETIME, decimal_field DECIMAL(7,2))")
|
26
15
|
|
27
|
-
DohDb::
|
28
|
-
|
16
|
+
dbh = DohDb::request_handle
|
17
|
+
dbh.insert("INSERT INTO #{@tbl} SET char_field = 'c', int_field = 5, bool_field = 1, date_field = '2012-02-20', datetime_field = '2012-02-20 21:06:00', decimal_field = 54.12")
|
18
|
+
row = dbh.select_row("SELECT * FROM #{@tbl}")
|
19
|
+
assert_equal('c', row['char_field'])
|
20
|
+
assert_equal(5, row['int_field'])
|
21
|
+
assert_equal(true, row['bool_field'])
|
22
|
+
assert_equal(Date.new(2012,2,20), row['date_field'])
|
23
|
+
assert_equal(Time.utc(2012,2,20, 21, 6, 0), row['datetime_field'])
|
24
|
+
assert_equal(BigDecimal('54.12'), row['decimal_field'])
|
29
25
|
end
|
30
26
|
end
|
31
27
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dohmysql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,19 +10,41 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-02-
|
13
|
+
date: 2012-02-23 00:00:00.000000000Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: dohutil
|
17
|
-
requirement: &
|
17
|
+
requirement: &70236167835480 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ! '>='
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: 0.1.
|
22
|
+
version: 0.1.5
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *70236167835480
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: mysql2
|
28
|
+
requirement: &70236167834600 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.3.11
|
23
34
|
type: :runtime
|
24
35
|
prerelease: false
|
25
|
-
version_requirements: *
|
36
|
+
version_requirements: *70236167834600
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: dohtest
|
39
|
+
requirement: &70236167833680 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: 0.1.4
|
45
|
+
type: :development
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *70236167833680
|
26
48
|
description: wrapper classes around low level mysql gem to provide a better interface
|
27
49
|
email:
|
28
50
|
- devinfo@atpsoft.com
|
@@ -42,7 +64,6 @@ files:
|
|
42
64
|
- lib/doh/mysql/current_date.rb
|
43
65
|
- lib/doh/mysql/database_creator.rb
|
44
66
|
- lib/doh/mysql/db_date.rb
|
45
|
-
- lib/doh/mysql/default_type_guesser.rb
|
46
67
|
- lib/doh/mysql/error.rb
|
47
68
|
- lib/doh/mysql/handle.rb
|
48
69
|
- lib/doh/mysql/hash_row.rb
|
@@ -116,3 +137,4 @@ test_files:
|
|
116
137
|
- test/types.dt.rb
|
117
138
|
- test/unquoted.dt.rb
|
118
139
|
- test/writable_row.dt.rb
|
140
|
+
has_rdoc:
|
@@ -1,37 +0,0 @@
|
|
1
|
-
require 'mysql'
|
2
|
-
require 'doh/mysql/parse'
|
3
|
-
require 'doh/to_display'
|
4
|
-
require 'doh/mysql/types'
|
5
|
-
require 'doh/core_ext/string'
|
6
|
-
|
7
|
-
module DohDb
|
8
|
-
|
9
|
-
class DefaultTypeGuesser
|
10
|
-
# for compatibility with older mysql gems
|
11
|
-
if !MysqlField.const_defined?('TYPE_NEWDECIMAL')
|
12
|
-
MysqlField::TYPE_NEWDECIMAL = 246
|
13
|
-
end
|
14
|
-
DECIMAL_TYPES = [MysqlField::TYPE_DECIMAL, MysqlField::TYPE_NEWDECIMAL]
|
15
|
-
INT_TYPES = [MysqlField::TYPE_TINY,MysqlField::TYPE_SHORT,MysqlField::TYPE_LONG,MysqlField::TYPE_LONGLONG,MysqlField::TYPE_INT24]
|
16
|
-
|
17
|
-
def self.guess_type(value, meta)
|
18
|
-
return nil if value.nil?
|
19
|
-
|
20
|
-
custom_type = DohDb::find_column_type(nil, meta.table, meta.name) || DohDb::find_column_type(nil, nil, meta.name)
|
21
|
-
return custom_type.build(meta.name, value) if custom_type
|
22
|
-
|
23
|
-
return DohDb::parse_bool(value) if (meta.type == MysqlField::TYPE_TINY) && (meta.length == 1) && (meta.max_length == 1)
|
24
|
-
return DohDb::parse_datetime(value) if meta.type == MysqlField::TYPE_DATETIME
|
25
|
-
return DohDb::parse_date(value) if meta.type == MysqlField::TYPE_DATE
|
26
|
-
return DohDb::parse_decimal(value) if DECIMAL_TYPES.include?(meta.type)
|
27
|
-
return DohDb::parse_int(value) if INT_TYPES.include?(meta.type)
|
28
|
-
if meta.type == MysqlField::TYPE_STRING || meta.type == MysqlField::TYPE_VAR_STRING
|
29
|
-
return PhoneDisplayString.new(value) if (value.size == 10) && (meta.name.lastn(5) == 'phone')
|
30
|
-
return SsnDisplayString.new(value) if (value.size == 9) && (meta.max_length == 9) && (meta.name.lastn(3) == 'ssn')
|
31
|
-
return PostalDisplayString.new(value) if [5,9].include?(value.size) && [5,9].include?(meta.max_length) && ((meta.name.lastn(6) == 'postal') || (meta.name.lastn(3) == 'zip'))
|
32
|
-
end
|
33
|
-
value
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
end
|