sequel_pg 1.2.2 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ === 1.3.0 (2012-04-02)
2
+
3
+ * Add major speedup for new Sequel 3.34.0 methods Dataset#to_hash_groups and #select_hash_groups (jeremyevans)
4
+
5
+ * Handle infinite timestamp values using Database#convert_infinite_timestamps in Sequel 3.34.0 (jeremyevans)
6
+
1
7
  === 1.2.2 (2012-03-09)
2
8
 
3
9
  * Get microsecond accuracy when using datetime_class = DateTime with 1.8-1.9.2 stdlib date library via Rational (jeremyevans)
@@ -36,6 +36,10 @@
36
36
  #define SPG_YIELD_KMV_HASH 7
37
37
  #define SPG_YIELD_MKMV_HASH 8
38
38
  #define SPG_YIELD_MODEL 9
39
+ #define SPG_YIELD_KV_HASH_GROUPS 10
40
+ #define SPG_YIELD_MKV_HASH_GROUPS 11
41
+ #define SPG_YIELD_KMV_HASH_GROUPS 12
42
+ #define SPG_YIELD_MKMV_HASH_GROUPS 13
39
43
 
40
44
  static VALUE spg_Sequel;
41
45
  static VALUE spg_Blob;
@@ -49,6 +53,7 @@ static VALUE spg_sym_map;
49
53
  static VALUE spg_sym_first;
50
54
  static VALUE spg_sym_array;
51
55
  static VALUE spg_sym_hash;
56
+ static VALUE spg_sym_hash_groups;
52
57
  static VALUE spg_sym_model;
53
58
  static VALUE spg_sym__sequel_pg_type;
54
59
  static VALUE spg_sym__sequel_pg_value;
@@ -72,6 +77,8 @@ static ID spg_id_utc;
72
77
  static ID spg_id_utc_offset;
73
78
  static ID spg_id_localtime;
74
79
  static ID spg_id_new_offset;
80
+ static ID spg_id_convert_infinite_timestamps;
81
+ static ID spg_id_infinite_timestamp_value;
75
82
 
76
83
  static ID spg_id_call;
77
84
  static ID spg_id_get;
@@ -123,6 +130,17 @@ static VALUE spg_date(const char *s) {
123
130
  return rb_funcall(spg_Date, spg_id_new, 3, INT2NUM(year), INT2NUM(month), INT2NUM(day));
124
131
  }
125
132
 
133
+ static VALUE spg_timestamp_error(const char *s, VALUE self) {
134
+ VALUE db;
135
+ db = rb_funcall(self, spg_id_db, 0);
136
+ if(RTEST(rb_funcall(db, spg_id_convert_infinite_timestamps, 0))) {
137
+ if((strcmp(s, "infinity") == 0) || (strcmp(s, "-infinity") == 0)) {
138
+ return rb_funcall(db, spg_id_infinite_timestamp_value, 1, rb_tainted_str_new2(s));
139
+ }
140
+ }
141
+ rb_raise(rb_eArgError, "unexpected datetime format");
142
+ }
143
+
126
144
  static VALUE spg_timestamp(const char *s, VALUE self) {
127
145
  VALUE dtc, dt, rtz;
128
146
  int tz = SPG_NO_TZ;
@@ -140,7 +158,7 @@ static VALUE spg_timestamp(const char *s, VALUE self) {
140
158
  &usec_start, &usec, &usec_stop,
141
159
  &offset_sign, &offset_hour, &offset_minute);
142
160
  if(tokens < 7) {
143
- rb_raise(rb_eArgError, "unexpected datetime format");
161
+ return spg_timestamp_error(s, self);
144
162
  }
145
163
  usec *= (int) pow(10, (6 - (usec_stop - usec_start)));
146
164
  } else {
@@ -152,7 +170,7 @@ static VALUE spg_timestamp(const char *s, VALUE self) {
152
170
  min = 0;
153
171
  sec = 0;
154
172
  } else if (tokens < 6) {
155
- rb_raise(rb_eArgError, "unexpected datetime format");
173
+ return spg_timestamp_error(s, self);
156
174
  }
157
175
  usec = 0;
158
176
  }
@@ -458,21 +476,21 @@ static VALUE spg_yield_hash_rows(VALUE self, VALUE rres, VALUE ignore) {
458
476
  type = SPG_YIELD_FIRST;
459
477
  } else if (pg_type == spg_sym_array) {
460
478
  type = SPG_YIELD_ARRAY;
461
- } else if (pg_type == spg_sym_hash && rb_type(pg_value) == T_ARRAY) {
479
+ } else if ((pg_type == spg_sym_hash || pg_type == spg_sym_hash_groups) && rb_type(pg_value) == T_ARRAY) {
462
480
  VALUE pg_value_key, pg_value_value;
463
481
  pg_value_key = rb_ary_entry(pg_value, 0);
464
482
  pg_value_value = rb_ary_entry(pg_value, 1);
465
483
  if (SYMBOL_P(pg_value_key)) {
466
484
  if (SYMBOL_P(pg_value_value)) {
467
- type = SPG_YIELD_KV_HASH;
485
+ type = pg_type == spg_sym_hash_groups ? SPG_YIELD_KV_HASH_GROUPS : SPG_YIELD_KV_HASH;
468
486
  } else if (rb_type(pg_value_value) == T_ARRAY) {
469
- type = SPG_YIELD_KMV_HASH;
487
+ type = pg_type == spg_sym_hash_groups ? SPG_YIELD_KMV_HASH_GROUPS : SPG_YIELD_KMV_HASH;
470
488
  }
471
489
  } else if (rb_type(pg_value_key) == T_ARRAY) {
472
490
  if (SYMBOL_P(pg_value_value)) {
473
- type = SPG_YIELD_MKV_HASH;
491
+ type = pg_type == spg_sym_hash_groups ? SPG_YIELD_MKV_HASH_GROUPS : SPG_YIELD_MKV_HASH;
474
492
  } else if (rb_type(pg_value_value) == T_ARRAY) {
475
- type = SPG_YIELD_MKMV_HASH;
493
+ type = pg_type == spg_sym_hash_groups ? SPG_YIELD_MKMV_HASH_GROUPS : SPG_YIELD_MKMV_HASH;
476
494
  }
477
495
  }
478
496
  } else if (pg_type == spg_sym_model && rb_type(pg_value) == T_CLASS) {
@@ -529,54 +547,114 @@ static VALUE spg_yield_hash_rows(VALUE self, VALUE rres, VALUE ignore) {
529
547
  }
530
548
  break;
531
549
  case SPG_YIELD_KV_HASH:
550
+ case SPG_YIELD_KV_HASH_GROUPS:
532
551
  /* Hash with single key and single value */
533
552
  {
534
553
  VALUE k, v;
535
554
  h = rb_hash_new();
536
555
  k = spg__field_id(rb_ary_entry(pg_value, 0), colsyms, nfields);
537
556
  v = spg__field_id(rb_ary_entry(pg_value, 1), colsyms, nfields);
538
- for(i=0; i<ntuples; i++) {
539
- rb_hash_aset(h, spg__col_value(self, res, i, k, colconvert ENC_INDEX), spg__col_value(self, res, i, v, colconvert ENC_INDEX));
540
- }
557
+ if(type == SPG_YIELD_KV_HASH) {
558
+ for(i=0; i<ntuples; i++) {
559
+ rb_hash_aset(h, spg__col_value(self, res, i, k, colconvert ENC_INDEX), spg__col_value(self, res, i, v, colconvert ENC_INDEX));
560
+ }
561
+ } else {
562
+ VALUE kv, vv, a;
563
+ for(i=0; i<ntuples; i++) {
564
+ kv = spg__col_value(self, res, i, k, colconvert ENC_INDEX);
565
+ vv = spg__col_value(self, res, i, v, colconvert ENC_INDEX);
566
+ a = rb_hash_lookup(h, kv);
567
+ if(!RTEST(a)) {
568
+ rb_hash_aset(h, kv, rb_ary_new3(1, vv));
569
+ } else {
570
+ rb_ary_push(a, vv);
571
+ }
572
+ }
573
+ }
541
574
  rb_yield(h);
542
575
  }
543
576
  break;
544
577
  case SPG_YIELD_MKV_HASH:
578
+ case SPG_YIELD_MKV_HASH_GROUPS:
545
579
  /* Hash with array of keys and single value */
546
580
  {
547
581
  VALUE k, v;
548
582
  h = rb_hash_new();
549
583
  k = spg__field_ids(rb_ary_entry(pg_value, 0), colsyms, nfields);
550
584
  v = spg__field_id(rb_ary_entry(pg_value, 1), colsyms, nfields);
551
- for(i=0; i<ntuples; i++) {
552
- rb_hash_aset(h, spg__col_values(self, k, colsyms, nfields, res, i, colconvert ENC_INDEX), spg__col_value(self, res, i, v, colconvert ENC_INDEX));
553
- }
585
+ if(type == SPG_YIELD_MKV_HASH) {
586
+ for(i=0; i<ntuples; i++) {
587
+ rb_hash_aset(h, spg__col_values(self, k, colsyms, nfields, res, i, colconvert ENC_INDEX), spg__col_value(self, res, i, v, colconvert ENC_INDEX));
588
+ }
589
+ } else {
590
+ VALUE kv, vv, a;
591
+ for(i=0; i<ntuples; i++) {
592
+ kv = spg__col_values(self, k, colsyms, nfields, res, i, colconvert ENC_INDEX);
593
+ vv = spg__col_value(self, res, i, v, colconvert ENC_INDEX);
594
+ a = rb_hash_lookup(h, kv);
595
+ if(!RTEST(a)) {
596
+ rb_hash_aset(h, kv, rb_ary_new3(1, vv));
597
+ } else {
598
+ rb_ary_push(a, vv);
599
+ }
600
+ }
601
+ }
554
602
  rb_yield(h);
555
603
  }
556
604
  break;
557
605
  case SPG_YIELD_KMV_HASH:
606
+ case SPG_YIELD_KMV_HASH_GROUPS:
558
607
  /* Hash with single keys and array of values */
559
608
  {
560
609
  VALUE k, v;
561
610
  h = rb_hash_new();
562
611
  k = spg__field_id(rb_ary_entry(pg_value, 0), colsyms, nfields);
563
612
  v = spg__field_ids(rb_ary_entry(pg_value, 1), colsyms, nfields);
564
- for(i=0; i<ntuples; i++) {
565
- rb_hash_aset(h, spg__col_value(self, res, i, k, colconvert ENC_INDEX), spg__col_values(self, v, colsyms, nfields, res, i, colconvert ENC_INDEX));
566
- }
613
+ if(type == SPG_YIELD_KMV_HASH) {
614
+ for(i=0; i<ntuples; i++) {
615
+ rb_hash_aset(h, spg__col_value(self, res, i, k, colconvert ENC_INDEX), spg__col_values(self, v, colsyms, nfields, res, i, colconvert ENC_INDEX));
616
+ }
617
+ } else {
618
+ VALUE kv, vv, a;
619
+ for(i=0; i<ntuples; i++) {
620
+ kv = spg__col_value(self, res, i, k, colconvert ENC_INDEX);
621
+ vv = spg__col_values(self, v, colsyms, nfields, res, i, colconvert ENC_INDEX);
622
+ a = rb_hash_lookup(h, kv);
623
+ if(!RTEST(a)) {
624
+ rb_hash_aset(h, kv, rb_ary_new3(1, vv));
625
+ } else {
626
+ rb_ary_push(a, vv);
627
+ }
628
+ }
629
+ }
567
630
  rb_yield(h);
568
631
  }
569
632
  break;
570
633
  case SPG_YIELD_MKMV_HASH:
634
+ case SPG_YIELD_MKMV_HASH_GROUPS:
571
635
  /* Hash with array of keys and array of values */
572
636
  {
573
637
  VALUE k, v;
574
638
  h = rb_hash_new();
575
639
  k = spg__field_ids(rb_ary_entry(pg_value, 0), colsyms, nfields);
576
640
  v = spg__field_ids(rb_ary_entry(pg_value, 1), colsyms, nfields);
577
- for(i=0; i<ntuples; i++) {
578
- rb_hash_aset(h, spg__col_values(self, k, colsyms, nfields, res, i, colconvert ENC_INDEX), spg__col_values(self, v, colsyms, nfields, res, i, colconvert ENC_INDEX));
579
- }
641
+ if(type == SPG_YIELD_MKMV_HASH) {
642
+ for(i=0; i<ntuples; i++) {
643
+ rb_hash_aset(h, spg__col_values(self, k, colsyms, nfields, res, i, colconvert ENC_INDEX), spg__col_values(self, v, colsyms, nfields, res, i, colconvert ENC_INDEX));
644
+ }
645
+ } else {
646
+ VALUE kv, vv, a;
647
+ for(i=0; i<ntuples; i++) {
648
+ kv = spg__col_values(self, k, colsyms, nfields, res, i, colconvert ENC_INDEX);
649
+ vv = spg__col_values(self, v, colsyms, nfields, res, i, colconvert ENC_INDEX);
650
+ a = rb_hash_lookup(h, kv);
651
+ if(!RTEST(a)) {
652
+ rb_hash_aset(h, kv, rb_ary_new3(1, vv));
653
+ } else {
654
+ rb_ary_push(a, vv);
655
+ }
656
+ }
657
+ }
580
658
  rb_yield(h);
581
659
  }
582
660
  break;
@@ -615,6 +693,8 @@ void Init_sequel_pg(void) {
615
693
  spg_id_utc_offset = rb_intern("utc_offset");
616
694
  spg_id_localtime = rb_intern("localtime");
617
695
  spg_id_new_offset = rb_intern("new_offset");
696
+ spg_id_convert_infinite_timestamps = rb_intern("convert_infinite_timestamps");
697
+ spg_id_infinite_timestamp_value = rb_intern("infinite_timestamp_value");
618
698
 
619
699
  spg_id_call = rb_intern("call");
620
700
  spg_id_get = rb_intern("[]");
@@ -634,6 +714,7 @@ void Init_sequel_pg(void) {
634
714
  spg_sym_first = ID2SYM(rb_intern("first"));
635
715
  spg_sym_array = ID2SYM(rb_intern("array"));
636
716
  spg_sym_hash = ID2SYM(rb_intern("hash"));
717
+ spg_sym_hash_groups = ID2SYM(rb_intern("hash_groups"));
637
718
  spg_sym_model = ID2SYM(rb_intern("model"));
638
719
  spg_sym__sequel_pg_type = ID2SYM(rb_intern("_sequel_pg_type"));
639
720
  spg_sym__sequel_pg_value = ID2SYM(rb_intern("_sequel_pg_value"));
@@ -40,6 +40,15 @@ class Sequel::Postgres::Dataset
40
40
  end
41
41
  end
42
42
 
43
+ # In the case where both arguments given, use an optimized version.
44
+ def to_hash_groups(key_column, value_column = nil)
45
+ if value_column
46
+ clone(:_sequel_pg_type=>:hash_groups, :_sequel_pg_value=>[key_column, value_column]).fetch_rows(sql){|s| return s}
47
+ else
48
+ super
49
+ end
50
+ end
51
+
43
52
  # If model loads are being optimized and this is a model load, use the optimized
44
53
  # version.
45
54
  def each
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel_pg
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-09 00:00:00.000000000 Z
12
+ date: 2012-03-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: pg
16
- requirement: &4298956700 !ruby/object:Gem::Requirement
16
+ requirement: &4390944760 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.8.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *4298956700
24
+ version_requirements: *4390944760
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: sequel
27
- requirement: &4298956220 !ruby/object:Gem::Requirement
27
+ requirement: &4414168760 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 3.29.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *4298956220
35
+ version_requirements: *4414168760
36
36
  description: ! 'sequel_pg overwrites the inner loop of the Sequel postgres
37
37
 
38
38
  adapter row fetching code with a C version. The C version