rdo-mysql 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -134,10 +134,22 @@ static VALUE rdo_mysql_driver_quote(VALUE self, VALUE obj) {
134
134
  return rb_str_new2(quoted);
135
135
  }
136
136
 
137
+ /** Find any Set arguments and ensure they are formatted as Strings */
138
+ static void rdo_mysql_driver_normalize_sets_bang(VALUE * args, int argc) {
139
+ int i = 0;
140
+ for (; i < argc; ++i) {
141
+ if (rb_funcall(args[i], rb_intern("kind_of?"), 1, rb_path2class("Set")))
142
+ args[i] = rb_funcall(args[i], rb_intern("to_a"), 0);
143
+
144
+ if (rb_funcall(args[i], rb_intern("kind_of?"), 1, rb_cArray))
145
+ args[i] = rb_funcall(args[i], rb_intern("join"), 1, rb_str_new2(","));
146
+ }
147
+ }
148
+
137
149
  /** Find any Time/DateTime arguments and ensure they use the system time zone */
138
150
  static void rdo_mysql_driver_normalize_date_times_bang(VALUE * args, int argc) {
139
- int i;
140
- for (i = 0; i < argc; ++i) {
151
+ int i = 0;
152
+ for (; i < argc; ++i) {
141
153
  if (rb_funcall(args[i], rb_intern("kind_of?"), 1, rb_path2class("DateTime"))) {
142
154
  VALUE offset = rb_funcall(
143
155
  rb_funcall(rb_path2class("DateTime"), rb_intern("now"), 0),
@@ -167,6 +179,7 @@ static VALUE rdo_mysql_driver_execute(int argc, VALUE * args, VALUE self) {
167
179
  }
168
180
 
169
181
  rdo_mysql_driver_normalize_date_times_bang(&args[1], argc - 1);
182
+ rdo_mysql_driver_normalize_sets_bang(&args[1], argc - 1);
170
183
 
171
184
  VALUE stmt = RDO_INTERPOLATE(self, args, argc);
172
185
 
@@ -186,6 +199,7 @@ static VALUE rdo_mysql_driver_execute(int argc, VALUE * args, VALUE self) {
186
199
  void Init_rdo_mysql_driver(void) {
187
200
  rb_require("rdo/mysql/driver");
188
201
  rb_require("date");
202
+ rb_require("set");
189
203
 
190
204
  VALUE cMySQL = rb_path2class("RDO::MySQL::Driver");
191
205
 
@@ -27,6 +27,13 @@ static void rdo_mysql_tuple_list_free(RDOMySQLTupleList * list) {
27
27
  free(list);
28
28
  }
29
29
 
30
+ /** Parse the string as a Set */
31
+ static VALUE rdo_mysql_parse_set(char * v, unsigned long len, int enc) {
32
+ return rb_funcall(rb_path2class("Set"),
33
+ rb_intern("new"), 1, rb_funcall(RDO_STRING(v, len, enc),
34
+ rb_intern("split"), 1, rb_str_new2(",")));
35
+ }
36
+
30
37
  /** Constructor to create a new TupleList for the given result */
31
38
  VALUE rdo_mysql_tuple_list_new(MYSQL_RES * res, int encoding) {
32
39
  RDOMySQLTupleList * list = malloc(sizeof(RDOMySQLTupleList));
@@ -59,8 +66,13 @@ static VALUE rdo_mysql_cast_value(char * v, unsigned long len, MYSQL_FIELD f, in
59
66
 
60
67
  case MYSQL_TYPE_STRING:
61
68
  case MYSQL_TYPE_VAR_STRING:
69
+ case MYSQL_TYPE_MEDIUM_BLOB:
70
+ case MYSQL_TYPE_TINY_BLOB:
71
+ case MYSQL_TYPE_LONG_BLOB:
62
72
  case MYSQL_TYPE_BLOB:
63
- if (f.charsetnr == RDO_MYSQL_BINARY_ENC)
73
+ if (f.flags & SET_FLAG)
74
+ return rdo_mysql_parse_set(v, len, enc);
75
+ else if (f.charsetnr == RDO_MYSQL_BINARY_ENC)
64
76
  return RDO_BINARY_STRING(v, len);
65
77
  else
66
78
  return RDO_STRING(v, len, enc);
@@ -74,6 +86,7 @@ static VALUE rdo_mysql_cast_value(char * v, unsigned long len, MYSQL_FIELD f, in
74
86
  return RDO_FLOAT(v);
75
87
 
76
88
  case MYSQL_TYPE_DATE:
89
+ case MYSQL_TYPE_NEWDATE:
77
90
  return RDO_DATE(v);
78
91
 
79
92
  case MYSQL_TYPE_TIMESTAMP:
@@ -120,6 +133,7 @@ static VALUE rdo_mysql_tuple_list_each(VALUE self) {
120
133
  /** Initializer for the TupleList class */
121
134
  void Init_rdo_mysql_tuples(void) {
122
135
  rb_require("rdo/mysql");
136
+ rb_require("set");
123
137
 
124
138
  VALUE mMySQL = rb_path2class("RDO::MySQL");
125
139
 
@@ -7,6 +7,6 @@
7
7
 
8
8
  module RDO
9
9
  module MySQL
10
- VERSION = "0.0.4"
10
+ VERSION = "0.0.5"
11
11
  end
12
12
  end
@@ -1,6 +1,7 @@
1
1
  require "spec_helper"
2
2
  require "date"
3
3
  require "bigdecimal"
4
+ require "set"
4
5
 
5
6
  describe RDO::MySQL::Driver, "bind params" do
6
7
  let(:options) { connection_uri }
@@ -678,4 +679,28 @@ describe RDO::MySQL::Driver, "bind params" do
678
679
  end
679
680
  end
680
681
  end
682
+
683
+ describe "Set param" do
684
+ context "against a set field" do
685
+ let(:table) do
686
+ <<-SQL
687
+ CREATE TABLE test (
688
+ id INT PRIMARY KEY AUTO_INCREMENT,
689
+ pets SET('cat', 'dog', 'mouse')
690
+ )
691
+ SQL
692
+ end
693
+
694
+ let(:insert) do
695
+ [
696
+ "INSERT INTO test (pets) VALUES (?)",
697
+ Set["cat", "mouse"]
698
+ ]
699
+ end
700
+
701
+ it "is inferred correctly" do
702
+ tuple.should == {id: 1, pets: Set["cat", "mouse"]}
703
+ end
704
+ end
705
+ end
681
706
  end
@@ -2,6 +2,7 @@ require "spec_helper"
2
2
  require "bigdecimal"
3
3
  require "date"
4
4
  require "uri"
5
+ require "set"
5
6
 
6
7
  describe RDO::MySQL::Driver, "type casting" do
7
8
  let(:options) { connection_uri }
@@ -267,4 +268,17 @@ describe RDO::MySQL::Driver, "type casting" do
267
268
  value.should == DateTime.new(2012, 9, 30, 19, 4, 36, DateTime.now.zone)
268
269
  end
269
270
  end
271
+
272
+ describe "set cast" do
273
+ before(:each) do
274
+ connection.execute("CREATE TEMPORARY TABLE test (s SET('cat', 'dog', 'mouse'))")
275
+ connection.execute("INSERT INTO test (s) VALUES ('cat,mouse')")
276
+ end
277
+
278
+ let(:sql) { "SELECT s FROM test" }
279
+
280
+ it "returns a Set" do
281
+ value.should == Set["cat", "mouse"]
282
+ end
283
+ end
270
284
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rdo-mysql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-14 00:00:00.000000000 Z
12
+ date: 2012-10-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rdo