Skip to content
This repository
Browse code

support config[:insert_returning] + do not warn missing seq for id co…

…lumn
  • Loading branch information...
commit 54e1f3fe3d4f578e9d2478cb0201cec6dbe15f1a 1 parent 3d551a6
Karol Bucek kares authored

Showing 1 changed file with 26 additions and 21 deletions. Show diff stats Hide diff stats

  1. +26 21 lib/arjdbc/postgresql/adapter.rb
47 lib/arjdbc/postgresql/adapter.rb
@@ -79,6 +79,13 @@ def postgresql_version
79 79 end
80 80 end
81 81
  82 + def use_insert_returning?
  83 + if ( @use_insert_returning ||= nil ).nil?
  84 + @use_insert_returning = supports_insert_with_returning?
  85 + end
  86 + @use_insert_returning
  87 + end
  88 +
82 89 # column behavior based on postgresql_adapter in rails
83 90 module Column
84 91
@@ -595,21 +602,16 @@ def default_sequence_name(table_name, pk = nil)
595 602
596 603 # Resets sequence to the max value of the table's pk if present.
597 604 def reset_pk_sequence!(table, pk = nil, sequence = nil) #:nodoc:
598   - unless pk and sequence
  605 + if ! pk || ! sequence
599 606 default_pk, default_sequence = pk_and_sequence_for(table)
600   - pk ||= default_pk
601   - sequence ||= default_sequence
  607 + pk ||= default_pk; sequence ||= default_sequence
602 608 end
603   - if pk
604   - if sequence
605   - quoted_sequence = quote_column_name(sequence)
  609 + if pk && sequence
  610 + quoted_sequence = quote_column_name(sequence)
606 611
607   - select_value <<-end_sql, 'Reset sequence'
608   - SELECT setval('#{quoted_sequence}', (SELECT COALESCE(MAX(#{quote_column_name pk})+(SELECT increment_by FROM #{quoted_sequence}), (SELECT min_value FROM #{quoted_sequence})) FROM #{quote_table_name(table)}), false)
609   - end_sql
610   - else
611   - @logger.warn "#{table} has primary key #{pk} with no default sequence" if @logger
612   - end
  612 + select_value <<-end_sql, 'Reset Sequence'
  613 + SELECT setval('#{quoted_sequence}', (SELECT COALESCE(MAX(#{quote_column_name pk})+(SELECT increment_by FROM #{quoted_sequence}), (SELECT min_value FROM #{quoted_sequence})) FROM #{quote_table_name(table)}), false)
  614 + end_sql
613 615 end
614 616 end
615 617
@@ -617,7 +619,7 @@ def reset_pk_sequence!(table, pk = nil, sequence = nil) #:nodoc:
617 619 def pk_and_sequence_for(table) #:nodoc:
618 620 # First try looking for a sequence with a dependency on the
619 621 # given table's primary key.
620   - result = select(<<-end_sql, 'PK and serial sequence')[0]
  622 + result = select(<<-end_sql, 'PK and Serial Sequence')[0]
621 623 SELECT attr.attname, seq.relname
622 624 FROM pg_class seq,
623 625 pg_attribute attr,
@@ -634,11 +636,11 @@ def pk_and_sequence_for(table) #:nodoc:
634 636 AND dep.refobjid = '#{quote_table_name(table)}'::regclass
635 637 end_sql
636 638
637   - if result.nil? or result.empty?
  639 + if result.nil? || result.empty?
638 640 # If that fails, try parsing the primary key's default value.
639 641 # Support the 7.x and 8.0 nextval('foo'::text) as well as
640 642 # the 8.1+ nextval('foo'::regclass).
641   - result = select(<<-end_sql, 'PK and custom sequence')[0]
  643 + result = select(<<-end_sql, 'PK and Custom Sequence')[0]
642 644 SELECT attr.attname,
643 645 CASE
644 646 WHEN split_part(def.adsrc, '''', 2) ~ '.' THEN
@@ -656,7 +658,7 @@ def pk_and_sequence_for(table) #:nodoc:
656 658 end_sql
657 659 end
658 660
659   - [result["attname"], result["relname"]]
  661 + [ result['attname'], result['relname'] ]
660 662 rescue
661 663 nil
662 664 end
@@ -672,9 +674,9 @@ def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil, b
672 674 if pk
673 675 sql = to_sql(sql, binds)
674 676 return select_value("#{sql} RETURNING #{quote_column_name(pk)}")
675   - end
  677 + end
676 678 end
677   -
  679 +
678 680 # Otherwise, plain insert
679 681 execute(sql, name, binds)
680 682
@@ -707,17 +709,18 @@ def sql_for_insert(sql, pk, id_value, sequence_name, binds)
707 709 [sql, binds]
708 710 end
709 711
710   - # taken from rails postgresql_adapter.rb
711 712 def primary_key(table)
712   - result = select(<<-end_sql, 'SCHEMA')[0]
  713 + result = select(<<-end_sql, 'SCHEMA').first
713 714 SELECT attr.attname
714 715 FROM pg_attribute attr
715 716 INNER JOIN pg_constraint cons ON attr.attrelid = cons.conrelid AND attr.attnum = cons.conkey[1]
716 717 WHERE cons.contype = 'p'
717 718 AND cons.conrelid = '#{quote_table_name(table)}'::regclass
718 719 end_sql
719   -
  720 +
720 721 result && result["attname"]
  722 + # pk_and_sequence = pk_and_sequence_for(table)
  723 + # pk_and_sequence && pk_and_sequence.first
721 724 end
722 725
723 726 # Returns an array of schema names.
@@ -1368,6 +1371,8 @@ def initialize(*args)
1368 1371 configure_connection
1369 1372
1370 1373 @local_tz = execute('SHOW TIME ZONE', 'SCHEMA').first["TimeZone"]
  1374 + @use_insert_returning = config.key?(:insert_returning) ?
  1375 + self.class.type_cast_config_to_boolean(config[:insert_returning]) : nil
1371 1376 end
1372 1377
1373 1378 class ColumnDefinition < ActiveRecord::ConnectionAdapters::ColumnDefinition

0 comments on commit 54e1f3f

Please sign in to comment.
Something went wrong with that request. Please try again.