dbd-mysql 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/dbd/Mysql.rb CHANGED
@@ -49,7 +49,7 @@ module DBI
49
49
  # Only things that extend DBI's results are documented.
50
50
  #
51
51
  module Mysql
52
- VERSION = "0.4.0"
52
+ VERSION = "0.4.1"
53
53
  DESCRIPTION = "MySQL DBI DBD, Leverages 'mysql' low-level driver"
54
54
 
55
55
  MyError = ::MysqlError
@@ -68,13 +68,15 @@ module DBI
68
68
  when ::DBI::Binary
69
69
  obj = obj.to_s.gsub(/\\/) { "\\\\" }
70
70
  obj = obj.to_s.gsub(/'/) { "''" }
71
- "'#{obj}'"
71
+ "'#{obj}'"
72
+ when ::DateTime
73
+ "'#{obj.strftime("%Y-%m-%d %H:%M:%S")}'"
72
74
  when ::Time
73
- "'#{obj.strftime("%H:%M:%S")}'"
75
+ "'#{obj.strftime("%H:%M:%S")}'"
74
76
  when ::Date
75
- "'#{obj.strftime("%m/%d/%Y")}'"
77
+ "'#{obj.strftime("%Y-%m-%d")}'"
76
78
  when ::NilClass
77
- "NULL"
79
+ "NULL"
78
80
  else
79
81
  obj
80
82
  end
@@ -106,6 +108,52 @@ module DBI::DBD::Mysql::Util
106
108
 
107
109
  end # module Util
108
110
 
111
+ module DBI::DBD::Mysql::Type
112
+ #
113
+ # Custom handling for TIMESTAMP and DATETIME types in MySQL. See DBI::Type
114
+ # for more information.
115
+ #
116
+ class Timestamp < DBI::Type::Null
117
+ def self.parse(obj)
118
+ obj = super
119
+ return obj unless obj
120
+
121
+ case obj.class
122
+ when ::DateTime
123
+ return obj
124
+ when ::String
125
+ return ::DateTime.strptime(obj, "%Y-%m-%d %H:%M:%S")
126
+ else
127
+ return ::DateTime.parse(obj.to_s) if obj.respond_to? :to_s
128
+ return ::DateTime.parse(obj.to_str) if obj.respond_to? :to_str
129
+ return obj
130
+ end
131
+ end
132
+ end
133
+
134
+ #
135
+ # Custom handling for DATE types in MySQL. See DBI::Type for more
136
+ # information.
137
+ #
138
+ class Date < DBI::Type::Null
139
+ def self.parse(obj)
140
+ obj = super
141
+ return obj unless obj
142
+
143
+ case obj.class
144
+ when ::Date
145
+ return obj
146
+ when ::String
147
+ return ::Date.strptime(obj, "%Y-%m-%d")
148
+ else
149
+ return ::Date.parse(obj.to_s) if obj.respond_to? :to_s
150
+ return ::Date.parse(obj.to_str) if obj.respond_to? :to_str
151
+ return obj
152
+ end
153
+ end
154
+ end
155
+ end
156
+
109
157
  require 'dbd/mysql/driver'
110
158
  require 'dbd/mysql/database'
111
159
  require 'dbd/mysql/statement'
@@ -87,13 +87,13 @@ module DBI::DBD::Mysql
87
87
  coercion_method = DBI::Type::Varchar
88
88
  when 'TYPE_DATE'
89
89
  mysql_type_name = 'DATE'
90
- coercion_method = DBI::Type::Timestamp
90
+ coercion_method = DBI::DBD::Mysql::Type::Date
91
91
  when 'TYPE_TIME'
92
92
  mysql_type_name = 'TIME'
93
93
  coercion_method = DBI::Type::Timestamp
94
94
  when 'TYPE_DATETIME', 'TYPE_TIMESTAMP'
95
95
  mysql_type_name = 'DATETIME'
96
- coercion_method = DBI::Type::Timestamp
96
+ coercion_method = DBI::DBD::Mysql::Type::Timestamp
97
97
  when 'TYPE_CHAR'
98
98
  mysql_type_name = 'TINYINT' # questionable?
99
99
  when 'TYPE_TINY_BLOB'
@@ -206,6 +206,12 @@ module DBI::DBD::Mysql
206
206
  col['precision'] = size
207
207
  col['scale'] = decimal
208
208
  col['default'] = row[4]
209
+
210
+ case col['type_name']
211
+ when 'timestamp'
212
+ col['dbi_type'] = DBI::DBD::Mysql::Type::Timestamp
213
+ end
214
+
209
215
  col
210
216
  end # collect
211
217
  end # execute
@@ -127,9 +127,9 @@
127
127
  case dbtype
128
128
  when "postgresql"
129
129
  tables.reject! { |x| x =~ /^pg_/ }
130
- assert_equal %w(array_test bit_test blob_test boolean_test bytea_test field_types_test names precision_test time_test timestamp_test view_names), tables
130
+ assert_equal %w(array_test bit_test blob_test boolean_test bytea_test db_specific_types_test field_types_test names precision_test time_test timestamp_test view_names), tables
131
131
  else
132
- assert_equal %w(bit_test blob_test boolean_test field_types_test names precision_test time_test timestamp_test view_names), tables
132
+ assert_equal %w(bit_test blob_test boolean_test db_specific_types_test field_types_test names precision_test time_test timestamp_test view_names), tables
133
133
  end
134
134
  end
135
135
 
@@ -14,4 +14,6 @@ drop table bit_test;
14
14
  ---
15
15
  drop table field_types_test;
16
16
  ---
17
+ drop table db_specific_types_test;
18
+ ---
17
19
  drop table precision_test;
@@ -14,6 +14,46 @@ class TestMysqlPatches < DBDConfig.testbase(:mysql)
14
14
  end
15
15
  end
16
16
 
17
+ def test_timestamps
18
+ timestamp = "04-06-1978 06:00:00"
19
+ datestamp = "04-06-1978"
20
+ date = Date.strptime(datestamp, "%m-%d-%Y")
21
+ stamp = DateTime.strptime(timestamp, "%m-%d-%Y %H:%M:%S")
22
+ assert_nothing_raised do
23
+ @sth = @dbh.prepare("insert into db_specific_types_test (ts) values (?)")
24
+ @sth.execute(stamp)
25
+ @sth.finish
26
+ end
27
+
28
+ assert_nothing_raised do
29
+ @sth = @dbh.prepare("select ts from db_specific_types_test where ts is not null")
30
+ @sth.execute
31
+
32
+ newstamp = @sth.fetch[0]
33
+
34
+ assert_equal(newstamp, stamp)
35
+ assert_equal(newstamp.strftime("%m-%d-%Y %H:%M:%S"), timestamp)
36
+ @sth.finish
37
+ end
38
+
39
+ assert_nothing_raised do
40
+ @sth = @dbh.prepare("insert into db_specific_types_test (dt) values (?)")
41
+ @sth.execute(date)
42
+ @sth.finish
43
+ end
44
+
45
+ assert_nothing_raised do
46
+ @sth = @dbh.prepare("select dt from db_specific_types_test where dt is not null")
47
+ @sth.execute
48
+
49
+ newdate = @sth.fetch[0]
50
+
51
+ assert_equal(newdate, date)
52
+ assert_equal(newdate.strftime("%m-%d-%Y"), datestamp)
53
+ @sth.finish
54
+ end
55
+ end
56
+
17
57
  # FIXME when the spec is more solid, this should be in the general tests
18
58
  def test_columns
19
59
  assert_nothing_raised do
@@ -24,3 +24,5 @@ create table timestamp_test (mytimestamp timestamp) Engine=InnoDB;
24
24
  create table bit_test (mybit bit) Engine=InnoDB;
25
25
  ---
26
26
  create table field_types_test (foo integer not null primary key default 1);
27
+ ---
28
+ create table db_specific_types_test (ts timestamp, dt date);
metadata CHANGED
@@ -1,90 +1,81 @@
1
1
  --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
2
4
  name: dbd-mysql
3
5
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
6
+ version: 0.4.1
7
+ date: 2008-09-27 00:00:00 -07:00
8
+ summary: MySQL DBI DBD, Leverages 'mysql' low-level driver
9
+ require_paths:
10
+ - lib
11
+ email: ruby-dbi-users@rubyforge.org
12
+ homepage: http://www.rubyforge.org/projects/ruby-dbi
13
+ rubyforge_project: ruby-dbi
14
+ description: MySQL DBI DBD, Leverages 'mysql' low-level driver
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.8.0
24
+ version:
5
25
  platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
6
29
  authors:
7
30
  - Erik Hollensbe
8
31
  - Christopher Maujean
9
- autorequire:
10
- bindir: bin
11
- cert_chain: []
12
-
13
- date: 2008-09-02 00:00:00 -07:00
14
- default_executable:
15
- dependencies:
16
- - !ruby/object:Gem::Dependency
17
- name: dbi
18
- type: :runtime
19
- version_requirement:
20
- version_requirements: !ruby/object:Gem::Requirement
21
- requirements:
22
- - - ">="
23
- - !ruby/object:Gem::Version
24
- version: 0.4.0
25
- version:
26
- - !ruby/object:Gem::Dependency
27
- name: mysql
28
- type: :runtime
29
- version_requirement:
30
- version_requirements: !ruby/object:Gem::Requirement
31
- requirements:
32
- - - ">="
33
- - !ruby/object:Gem::Version
34
- version: "0"
35
- version:
36
- description: MySQL DBI DBD, Leverages 'mysql' low-level driver
37
- email: ruby-dbi-users@rubyforge.org
38
- executables: []
39
-
40
- extensions: []
41
-
42
- extra_rdoc_files:
43
- - README
44
- - LICENSE
45
- - ChangeLog
46
32
  files:
47
- - test/dbd/general/test_types.rb
48
33
  - test/dbd/general/test_database.rb
49
34
  - test/dbd/general/test_statement.rb
35
+ - test/dbd/general/test_types.rb
36
+ - test/dbd/mysql/base.rb
50
37
  - test/dbd/mysql/down.sql
38
+ - test/dbd/mysql/test_blob.rb
51
39
  - test/dbd/mysql/test_patches.rb
52
40
  - test/dbd/mysql/up.sql
53
- - test/dbd/mysql/base.rb
54
- - test/dbd/mysql/test_blob.rb
55
41
  - lib/dbd/Mysql.rb
56
- - lib/dbd/mysql/driver.rb
57
42
  - lib/dbd/mysql/database.rb
43
+ - lib/dbd/mysql/driver.rb
58
44
  - lib/dbd/mysql/statement.rb
59
45
  - test/DBD_TESTS
60
46
  - README
61
47
  - LICENSE
62
48
  - ChangeLog
63
- has_rdoc: true
64
- homepage: http://www.rubyforge.org/projects/ruby-dbi
65
- post_install_message:
49
+ test_files:
50
+ - test/ts_dbd.rb
66
51
  rdoc_options: []
67
52
 
68
- require_paths:
69
- - lib
70
- required_ruby_version: !ruby/object:Gem::Requirement
71
- requirements:
72
- - - ">="
73
- - !ruby/object:Gem::Version
74
- version: 1.8.0
75
- version:
76
- required_rubygems_version: !ruby/object:Gem::Requirement
77
- requirements:
78
- - - ">="
79
- - !ruby/object:Gem::Version
80
- version: "0"
81
- version:
53
+ extra_rdoc_files:
54
+ - README
55
+ - LICENSE
56
+ - ChangeLog
57
+ executables: []
58
+
59
+ extensions: []
60
+
82
61
  requirements: []
83
62
 
84
- rubyforge_project: ruby-dbi
85
- rubygems_version: 1.2.0
86
- signing_key:
87
- specification_version: 2
88
- summary: MySQL DBI DBD, Leverages 'mysql' low-level driver
89
- test_files:
90
- - test/ts_dbd.rb
63
+ dependencies:
64
+ - !ruby/object:Gem::Dependency
65
+ name: dbi
66
+ version_requirement:
67
+ version_requirements: !ruby/object:Gem::Version::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 0.4.0
72
+ version:
73
+ - !ruby/object:Gem::Dependency
74
+ name: mysql
75
+ version_requirement:
76
+ version_requirements: !ruby/object:Gem::Version::Requirement
77
+ requirements:
78
+ - - ">"
79
+ - !ruby/object:Gem::Version
80
+ version: 0.0.0
81
+ version: