Methods
Public Instance
- add_conversion_proc
- add_named_conversion_proc
- check_constraints
- commit_prepared_transaction
- conversion_procs
- convert_serial_to_identity
- create_function
- create_language
- create_schema
- create_table
- create_table?
- create_trigger
- database_type
- defer_constraints
- do
- drop_function
- drop_language
- drop_schema
- drop_trigger
- foreign_key_list
- freeze
- immediate_constraints
- indexes
- locks
- notify
- primary_key
- primary_key_sequence
- refresh_view
- reset_primary_key_sequence
- rollback_prepared_transaction
- serial_primary_key_options
- server_version
- supports_create_table_if_not_exists?
- supports_deferrable_constraints?
- supports_deferrable_foreign_key_constraints?
- supports_drop_table_if_exists?
- supports_partial_indexes?
- supports_prepared_transactions?
- supports_savepoints?
- supports_transaction_isolation_levels?
- supports_transactional_ddl?
- supports_trigger_conditions?
- tables
- type_supported?
- values
- views
- with_advisory_lock
Included modules
Constants
DATABASE_ERROR_REGEXPS | = | [ # Add this check first, since otherwise it's possible for users to control # which exception class is generated. [/invalid input syntax/, DatabaseError], [/duplicate key value violates unique constraint/, UniqueConstraintViolation], [/violates foreign key constraint/, ForeignKeyConstraintViolation], [/violates check constraint/, CheckConstraintViolation], [/violates not-null constraint/, NotNullConstraintViolation], [/conflicting key value violates exclusion constraint/, ExclusionConstraintViolation], [/could not serialize access/, SerializationFailure], [/could not obtain lock on row in relation/, DatabaseLockTimeout], ].freeze | ||
FOREIGN_KEY_LIST_ON_DELETE_MAP | = | {'a'=>:no_action, 'r'=>:restrict, 'c'=>:cascade, 'n'=>:set_null, 'd'=>:set_default}.freeze | ||
MAX_DATE | = | Date.new(5874897, 12, 31) | ||
MAX_TIMESTAMP | = | (Time.utc(294277) - Rational(1, 1000000)).freeze | ||
MIN_DATE | = | Date.new(-4713, 11, 24) | ||
MIN_TIMESTAMP | = | Time.utc(-4713, 11, 24).freeze | ||
ON_COMMIT | = | {:drop => 'DROP', :delete_rows => 'DELETE ROWS', :preserve_rows => 'PRESERVE ROWS'}.freeze | ||
SELECT_CUSTOM_SEQUENCE_SQL | = | (<<-end_sql SELECT name.nspname AS "schema", CASE WHEN split_part(pg_get_expr(def.adbin, attr.attrelid), '''', 2) ~ '.' THEN substr(split_part(pg_get_expr(def.adbin, attr.attrelid), '''', 2), strpos(split_part(pg_get_expr(def.adbin, attr.attrelid), '''', 2), '.')+1) ELSE split_part(pg_get_expr(def.adbin, attr.attrelid), '''', 2) END AS "sequence" FROM pg_class t JOIN pg_namespace name ON (t.relnamespace = name.oid) JOIN pg_attribute attr ON (t.oid = attrelid) JOIN pg_attrdef def ON (adrelid = attrelid AND adnum = attnum) JOIN pg_constraint cons ON (conrelid = adrelid AND adnum = conkey[1]) WHERE cons.contype = 'p' AND pg_get_expr(def.adbin, attr.attrelid) ~* 'nextval' end_sql ).strip.gsub(/\s+/, ' ').freeze |
SQL fragment for custom sequences (ones not created by serial primary key), Returning the schema and literal form of the sequence name, by parsing the column defaults table. |
|
SELECT_PK_SQL | = | (<<-end_sql SELECT pg_attribute.attname AS pk FROM pg_class, pg_attribute, pg_index, pg_namespace WHERE pg_class.oid = pg_attribute.attrelid AND pg_class.relnamespace = pg_namespace.oid AND pg_class.oid = pg_index.indrelid AND pg_index.indkey[0] = pg_attribute.attnum AND pg_index.indisprimary = 't' end_sql ).strip.gsub(/\s+/, ' ').freeze |
SQL fragment for determining primary key column for the given table. Only returns the first primary key if the table has a composite primary key. |
|
SELECT_SERIAL_SEQUENCE_SQL | = | (<<-end_sql SELECT name.nspname AS "schema", seq.relname AS "sequence" FROM pg_class seq, pg_attribute attr, pg_depend dep, pg_namespace name, pg_constraint cons, pg_class t WHERE seq.oid = dep.objid AND seq.relnamespace = name.oid AND seq.relkind = 'S' AND attr.attrelid = dep.refobjid AND attr.attnum = dep.refobjsubid AND attr.attrelid = cons.conrelid AND attr.attnum = cons.conkey[1] AND attr.attrelid = t.oid AND cons.contype = 'p' end_sql ).strip.gsub(/\s+/, ' ').freeze |
SQL fragment for getting sequence associated with table’s primary key, assuming it was a serial primary key column. |
|
TYPTYPE_METHOD_MAP | = | { 'c' => :schema_composite_type, 'e' => :schema_enum_type, 'r' => :schema_range_type, 'm' => :schema_multirange_type, } | ||
VALID_CLIENT_MIN_MESSAGES | = | %w'DEBUG5 DEBUG4 DEBUG3 DEBUG2 DEBUG1 LOG NOTICE WARNING ERROR FATAL PANIC'.freeze.each(&:freeze) |
Attributes
conversion_procs | [R] |
A hash of conversion procs, keyed by type integer (oid) and having callable values for the conversion proc for that type. |
Public Instance methods
Set a conversion proc for the given oid. The callable can be passed either as a argument or a block.
# File lib/sequel/adapters/shared/postgres.rb 322 def add_conversion_proc(oid, callable=nil, &block) 323 conversion_procs[oid] = callable || block 324 end
Add a conversion proc for a named type, using the given block. This should be used for types without fixed OIDs, which includes all types that are not included in a default PostgreSQL installation.
# File lib/sequel/adapters/shared/postgres.rb 329 def add_named_conversion_proc(name, &block) 330 unless oid = from(:pg_type).where(:typtype=>['b', 'e'], :typname=>name.to_s).get(:oid) 331 raise Error, "No matching type in pg_type for #{name.inspect}" 332 end 333 add_conversion_proc(oid, block) 334 end
A hash of metadata for CHECK constraints on the table. Keys are CHECK constraint name symbols. Values are hashes with the following keys:
:definition |
An SQL fragment for the definition of the constraint |
:columns |
An array of column symbols for the columns referenced in the constraint, can be an empty array if the database cannot deteremine the column symbols. |
# File lib/sequel/adapters/shared/postgres.rb 345 def check_constraints(table) 346 m = output_identifier_meth 347 348 hash = {} 349 _check_constraints_ds.where_each(:conrelid=>regclass_oid(table)) do |row| 350 constraint = m.call(row[:constraint]) 351 entry = hash[constraint] ||= {:definition=>row[:definition], :columns=>[], :validated=>row[:validated], :enforced=>row[:enforced]} 352 entry[:columns] << m.call(row[:column]) if row[:column] 353 end 354 355 hash 356 end
# File lib/sequel/adapters/shared/postgres.rb 336 def commit_prepared_transaction(transaction_id, opts=OPTS) 337 run("COMMIT PREPARED #{literal(transaction_id)}", opts) 338 end
Convert the first primary key column in the table
from being a serial column to being an identity column. If the column is already an identity column, assume it was already converted and make no changes.
Only supported on PostgreSQL 10.2+, since on those versions Sequel
will use identity columns instead of serial columns for auto incrementing primary keys. Only supported when running as a superuser, since regular users cannot modify system tables, and there is no way to keep an existing sequence when changing an existing column to be an identity column.
This method can raise an exception in at least the following cases where it may otherwise succeed (there may be additional cases not listed here):
-
The serial column was added after table creation using PostgreSQL <7.3
-
A regular index also exists on the column (such an index can probably be dropped as the primary key index should suffice)
Options:
:column |
Specify the column to convert instead of using the first primary key column |
:server |
Run the SQL on the given server |
# File lib/sequel/adapters/shared/postgres.rb 376 def convert_serial_to_identity(table, opts=OPTS) 377 raise Error, "convert_serial_to_identity is only supported on PostgreSQL 10.2+" unless server_version >= 100002 378 379 server = opts[:server] 380 server_hash = server ? {:server=>server} : OPTS 381 ds = dataset 382 ds = ds.server(server) if server 383 384 raise Error, "convert_serial_to_identity requires superuser permissions" unless ds.get{current_setting('is_superuser')} == 'on' 385 386 table_oid = regclass_oid(table) 387 im = input_identifier_meth 388 unless column = (opts[:column] || ((sch = schema(table).find{|_, sc| sc[:primary_key] && sc[:auto_increment]}) && sch[0])) 389 raise Error, "could not determine column to convert from serial to identity automatically" 390 end 391 column = im.call(column) 392 393 column_num = ds.from(:pg_attribute). 394 where(:attrelid=>table_oid, :attname=>column). 395 get(:attnum) 396 397 pg_class = Sequel.cast('pg_class', :regclass) 398 res = ds.from(:pg_depend). 399 where(:refclassid=>pg_class, :refobjid=>table_oid, :refobjsubid=>column_num, :classid=>pg_class, :objsubid=>0, :deptype=>%w'a i'). 400 select_map([:objid, Sequel.as({:deptype=>'i'}, :v)]) 401 402 case res.length 403 when 0 404 raise Error, "unable to find related sequence when converting serial to identity" 405 when 1 406 seq_oid, already_identity = res.first 407 else 408 raise Error, "more than one linked sequence found when converting serial to identity" 409 end 410 411 return if already_identity 412 413 transaction(server_hash) do 414 run("ALTER TABLE #{quote_schema_table(table)} ALTER COLUMN #{quote_identifier(column)} DROP DEFAULT", server_hash) 415 416 ds.from(:pg_depend). 417 where(:classid=>pg_class, :objid=>seq_oid, :objsubid=>0, :deptype=>'a'). 418 update(:deptype=>'i') 419 420 ds.from(:pg_attribute). 421 where(:attrelid=>table_oid, :attname=>column). 422 update(:attidentity=>'d') 423 end 424 425 remove_cached_schema(table) 426 nil 427 end
Creates the function in the database. Arguments:
name |
name of the function to create | ||||||||||||||||||||||||||||
definition |
string definition of the function, or object file for a dynamically loaded C function. | ||||||||||||||||||||||||||||
opts |
options hash:
|
# File lib/sequel/adapters/shared/postgres.rb 450 def create_function(name, definition, opts=OPTS) 451 self << create_function_sql(name, definition, opts) 452 end
Create the procedural language in the database. Arguments:
name |
Name of the procedural language (e.g. plpgsql) | ||||||||
opts |
options hash:
|
# File lib/sequel/adapters/shared/postgres.rb 461 def create_language(name, opts=OPTS) 462 self << create_language_sql(name, opts) 463 end
Create a schema in the database. Arguments:
name |
Name of the schema (e.g. admin) | ||||
opts |
options hash:
|
# File lib/sequel/adapters/shared/postgres.rb 470 def create_schema(name, opts=OPTS) 471 self << create_schema_sql(name, opts) 472 end
Support partitions of tables using the :partition_of option.
# File lib/sequel/adapters/shared/postgres.rb 475 def create_table(name, options=OPTS, &block) 476 if options[:partition_of] 477 create_partition_of_table_from_generator(name, CreatePartitionOfTableGenerator.new(&block), options) 478 return 479 end 480 481 super 482 end
Support partitions of tables using the :partition_of option.
# File lib/sequel/adapters/shared/postgres.rb 485 def create_table?(name, options=OPTS, &block) 486 if options[:partition_of] 487 create_table(name, options.merge!(:if_not_exists=>true), &block) 488 return 489 end 490 491 super 492 end
Create a trigger in the database. Arguments:
table |
the table on which this trigger operates | ||||||||||||
name |
the name of this trigger | ||||||||||||
function |
the function to call for this trigger, which should return type trigger. | ||||||||||||
opts |
options hash:
|
# File lib/sequel/adapters/shared/postgres.rb 506 def create_trigger(table, name, function, opts=OPTS) 507 self << create_trigger_sql(table, name, function, opts) 508 end
# File lib/sequel/adapters/shared/postgres.rb 510 def database_type 511 :postgres 512 end
For constraints that are deferrable, defer constraints until transaction commit. Options:
:constraints |
An identifier of the constraint, or an array of identifiers for constraints, to apply this change to specific constraints. |
:server |
The server/shard on which to run the query. |
Examples:
DB.defer_constraints # SET CONSTRAINTS ALL DEFERRED DB.defer_constraints(constraints: [:c1, Sequel[:sc][:c2]]) # SET CONSTRAINTS "c1", "sc"."s2" DEFERRED
# File lib/sequel/adapters/shared/postgres.rb 529 def defer_constraints(opts=OPTS) 530 _set_constraints(' DEFERRED', opts) 531 end
Use PostgreSQL’s DO syntax to execute an anonymous code block. The code should be the literal code string to use in the underlying procedural language. Options:
:language |
The procedural language the code is written in. The PostgreSQL default is plpgsql. Can be specified as a string or a symbol. |
# File lib/sequel/adapters/shared/postgres.rb 538 def do(code, opts=OPTS) 539 language = opts[:language] 540 run "DO #{"LANGUAGE #{literal(language.to_s)} " if language}#{literal(code)}" 541 end
Drops the function from the database. Arguments:
name |
name of the function to drop | ||||||
opts |
options hash:
|
# File lib/sequel/adapters/shared/postgres.rb 549 def drop_function(name, opts=OPTS) 550 self << drop_function_sql(name, opts) 551 end
Drops a procedural language from the database. Arguments:
name |
name of the procedural language to drop | ||||
opts |
options hash:
|
# File lib/sequel/adapters/shared/postgres.rb 558 def drop_language(name, opts=OPTS) 559 self << drop_language_sql(name, opts) 560 end
Drops a schema from the database. Arguments:
name |
name of the schema to drop | ||||
opts |
options hash:
|
# File lib/sequel/adapters/shared/postgres.rb 567 def drop_schema(name, opts=OPTS) 568 self << drop_schema_sql(name, opts) 569 end
Drops a trigger from the database. Arguments:
table |
table from which to drop the trigger | ||||
name |
name of the trigger to drop | ||||
opts |
options hash:
|
# File lib/sequel/adapters/shared/postgres.rb 577 def drop_trigger(table, name, opts=OPTS) 578 self << drop_trigger_sql(table, name, opts) 579 end
Return full foreign key information using the pg system tables, including :name, :on_delete, :on_update, and :deferrable entries in the hashes.
Supports additional options:
:reverse |
Instead of returning foreign keys in the current table, return foreign keys in other tables that reference the current table. |
:schema |
Set to true to have the :table value in the hashes be a qualified identifier. Set to false to use a separate :schema value with the related schema. Defaults to whether the given table argument is a qualified identifier. |
# File lib/sequel/adapters/shared/postgres.rb 591 def foreign_key_list(table, opts=OPTS) 592 m = output_identifier_meth 593 schema, _ = opts.fetch(:schema, schema_and_table(table)) 594 595 h = {} 596 fklod_map = FOREIGN_KEY_LIST_ON_DELETE_MAP 597 reverse = opts[:reverse] 598 599 (reverse ? _reverse_foreign_key_list_ds : _foreign_key_list_ds).where_each(Sequel[:cl][:oid]=>regclass_oid(table)) do |row| 600 if reverse 601 key = [row[:schema], row[:table], row[:name]] 602 else 603 key = row[:name] 604 end 605 606 if r = h[key] 607 r[:columns] << m.call(row[:column]) 608 r[:key] << m.call(row[:refcolumn]) 609 else 610 entry = h[key] = { 611 :name=>m.call(row[:name]), 612 :columns=>[m.call(row[:column])], 613 :key=>[m.call(row[:refcolumn])], 614 :on_update=>fklod_map[row[:on_update]], 615 :on_delete=>fklod_map[row[:on_delete]], 616 :deferrable=>row[:deferrable], 617 :validated=>row[:validated], 618 :enforced=>row[:enforced], 619 :table=>schema ? SQL::QualifiedIdentifier.new(m.call(row[:schema]), m.call(row[:table])) : m.call(row[:table]), 620 } 621 622 unless schema 623 # If not combining schema information into the :table entry 624 # include it as a separate entry. 625 entry[:schema] = m.call(row[:schema]) 626 end 627 end 628 end 629 630 h.values 631 end
# File lib/sequel/adapters/shared/postgres.rb 633 def freeze 634 server_version 635 supports_prepared_transactions? 636 _schema_ds 637 _select_serial_sequence_ds 638 _select_custom_sequence_ds 639 _select_pk_ds 640 _indexes_ds 641 _check_constraints_ds 642 _foreign_key_list_ds 643 _reverse_foreign_key_list_ds 644 @conversion_procs.freeze 645 super 646 end
Immediately apply deferrable constraints.
:constraints |
An identifier of the constraint, or an array of identifiers for constraints, to apply this change to specific constraints. |
:server |
The server/shard on which to run the query. |
Examples:
DB.immediate_constraints # SET CONSTRAINTS ALL IMMEDIATE DB.immediate_constraints(constraints: [:c1, Sequel[:sc][:c2]]) # SET CONSTRAINTS "c1", "sc"."s2" IMMEDIATE
# File lib/sequel/adapters/shared/postgres.rb 662 def immediate_constraints(opts=OPTS) 663 _set_constraints(' IMMEDIATE', opts) 664 end
Use the pg_* system tables to determine indexes on a table
# File lib/sequel/adapters/shared/postgres.rb 667 def indexes(table, opts=OPTS) 668 m = output_identifier_meth 669 cond = {Sequel[:tab][:oid]=>regclass_oid(table, opts)} 670 cond[:indpred] = nil unless opts[:include_partial] 671 672 indexes = {} 673 _indexes_ds.where_each(cond) do |r| 674 i = indexes[m.call(r[:name])] ||= {:columns=>[], :unique=>r[:unique], :deferrable=>r[:deferrable]} 675 i[:columns] << m.call(r[:column]) 676 end 677 indexes 678 end
Dataset
containing all current database locks
# File lib/sequel/adapters/shared/postgres.rb 681 def locks 682 dataset.from(:pg_class).join(:pg_locks, :relation=>:relfilenode).select{[pg_class[:relname], Sequel::SQL::ColumnAll.new(:pg_locks)]} 683 end
Notifies the given channel. See the PostgreSQL NOTIFY documentation. Options:
:payload |
The payload string to use for the NOTIFY statement. Only supported in PostgreSQL 9.0+. |
:server |
The server to which to send the NOTIFY statement, if the sharding support is being used. |
# File lib/sequel/adapters/shared/postgres.rb 691 def notify(channel, opts=OPTS) 692 sql = String.new 693 sql << "NOTIFY " 694 dataset.send(:identifier_append, sql, channel) 695 if payload = opts[:payload] 696 sql << ", " 697 dataset.literal_append(sql, payload.to_s) 698 end 699 execute_ddl(sql, opts) 700 end
Return primary key for the given table.
# File lib/sequel/adapters/shared/postgres.rb 703 def primary_key(table, opts=OPTS) 704 quoted_table = quote_schema_table(table) 705 Sequel.synchronize{return @primary_keys[quoted_table] if @primary_keys.has_key?(quoted_table)} 706 value = _select_pk_ds.where_single_value(Sequel[:pg_class][:oid] => regclass_oid(table, opts)) 707 Sequel.synchronize{@primary_keys[quoted_table] = value} 708 end
Return the sequence providing the default for the primary key for the given table.
# File lib/sequel/adapters/shared/postgres.rb 711 def primary_key_sequence(table, opts=OPTS) 712 quoted_table = quote_schema_table(table) 713 Sequel.synchronize{return @primary_key_sequences[quoted_table] if @primary_key_sequences.has_key?(quoted_table)} 714 cond = {Sequel[:t][:oid] => regclass_oid(table, opts)} 715 value = if pks = _select_serial_sequence_ds.first(cond) 716 literal(SQL::QualifiedIdentifier.new(pks[:schema], pks[:sequence])) 717 elsif pks = _select_custom_sequence_ds.first(cond) 718 literal(SQL::QualifiedIdentifier.new(pks[:schema], LiteralString.new(pks[:sequence]))) 719 end 720 721 Sequel.synchronize{@primary_key_sequences[quoted_table] = value} if value 722 end
Refresh the materialized view with the given name.
DB.refresh_view(:items_view) # REFRESH MATERIALIZED VIEW items_view DB.refresh_view(:items_view, concurrently: true) # REFRESH MATERIALIZED VIEW CONCURRENTLY items_view
# File lib/sequel/adapters/shared/postgres.rb 730 def refresh_view(name, opts=OPTS) 731 run "REFRESH MATERIALIZED VIEW#{' CONCURRENTLY' if opts[:concurrently]} #{quote_schema_table(name)}" 732 end
Reset the primary key sequence for the given table, basing it on the maximum current value of the table’s primary key.
# File lib/sequel/adapters/shared/postgres.rb 736 def reset_primary_key_sequence(table) 737 return unless seq = primary_key_sequence(table) 738 pk = SQL::Identifier.new(primary_key(table)) 739 db = self 740 s, t = schema_and_table(table) 741 table = Sequel.qualify(s, t) if s 742 743 if server_version >= 100000 744 seq_ds = metadata_dataset.from(:pg_sequence).where(:seqrelid=>regclass_oid(LiteralString.new(seq))) 745 increment_by = :seqincrement 746 min_value = :seqmin 747 # :nocov: 748 else 749 seq_ds = metadata_dataset.from(LiteralString.new(seq)) 750 increment_by = :increment_by 751 min_value = :min_value 752 # :nocov: 753 end 754 755 get{setval(seq, db[table].select(coalesce(max(pk)+seq_ds.select(increment_by), seq_ds.select(min_value))), false)} 756 end
# File lib/sequel/adapters/shared/postgres.rb 758 def rollback_prepared_transaction(transaction_id, opts=OPTS) 759 run("ROLLBACK PREPARED #{literal(transaction_id)}", opts) 760 end
PostgreSQL uses SERIAL psuedo-type instead of AUTOINCREMENT for managing incrementing primary keys.
# File lib/sequel/adapters/shared/postgres.rb 764 def serial_primary_key_options 765 # :nocov: 766 auto_increment_key = server_version >= 100002 ? :identity : :serial 767 # :nocov: 768 {:primary_key => true, auto_increment_key => true, :type=>Integer} 769 end
The version of the PostgreSQL server, used for determining capability.
# File lib/sequel/adapters/shared/postgres.rb 772 def server_version(server=nil) 773 return @server_version if @server_version 774 ds = dataset 775 ds = ds.server(server) if server 776 @server_version = swallow_database_error{ds.with_sql("SELECT CAST(current_setting('server_version_num') AS integer) AS v").single_value} || 0 777 end
PostgreSQL supports CREATE TABLE IF NOT EXISTS on 9.1+
# File lib/sequel/adapters/shared/postgres.rb 780 def supports_create_table_if_not_exists? 781 server_version >= 90100 782 end
PostgreSQL 9.0+ supports some types of deferrable constraints beyond foreign key constraints.
# File lib/sequel/adapters/shared/postgres.rb 785 def supports_deferrable_constraints? 786 server_version >= 90000 787 end
PostgreSQL supports deferrable foreign key constraints.
# File lib/sequel/adapters/shared/postgres.rb 790 def supports_deferrable_foreign_key_constraints? 791 true 792 end
PostgreSQL supports DROP TABLE IF EXISTS
# File lib/sequel/adapters/shared/postgres.rb 795 def supports_drop_table_if_exists? 796 true 797 end
PostgreSQL supports partial indexes.
# File lib/sequel/adapters/shared/postgres.rb 800 def supports_partial_indexes? 801 true 802 end
PostgreSQL supports prepared transactions (two-phase commit) if max_prepared_transactions is greater than 0.
# File lib/sequel/adapters/shared/postgres.rb 811 def supports_prepared_transactions? 812 return @supports_prepared_transactions if defined?(@supports_prepared_transactions) 813 @supports_prepared_transactions = self['SHOW max_prepared_transactions'].get.to_i > 0 814 end
PostgreSQL supports savepoints
# File lib/sequel/adapters/shared/postgres.rb 817 def supports_savepoints? 818 true 819 end
PostgreSQL supports transaction isolation levels
# File lib/sequel/adapters/shared/postgres.rb 822 def supports_transaction_isolation_levels? 823 true 824 end
PostgreSQL supports transaction DDL statements.
# File lib/sequel/adapters/shared/postgres.rb 827 def supports_transactional_ddl? 828 true 829 end
PostgreSQL 9.0+ supports trigger conditions.
# File lib/sequel/adapters/shared/postgres.rb 805 def supports_trigger_conditions? 806 server_version >= 90000 807 end
Array of symbols specifying table names in the current database. The dataset used is yielded to the block if one is provided, otherwise, an array of symbols of table names is returned.
Options:
:qualify |
Return the tables as Sequel::SQL::QualifiedIdentifier instances, using the schema the table is located in as the qualifier. |
:schema |
The schema to search |
:server |
The server to use |
# File lib/sequel/adapters/shared/postgres.rb 840 def tables(opts=OPTS, &block) 841 pg_class_relname(['r', 'p'], opts, &block) 842 end
Check whether the given type name string/symbol (e.g. :hstore) is supported by the database.
# File lib/sequel/adapters/shared/postgres.rb 846 def type_supported?(type) 847 Sequel.synchronize{return @supported_types[type] if @supported_types.has_key?(type)} 848 supported = from(:pg_type).where(:typtype=>'b', :typname=>type.to_s).count > 0 849 Sequel.synchronize{return @supported_types[type] = supported} 850 end
Creates a dataset that uses the VALUES clause:
DB.values([[1, 2], [3, 4]]) # VALUES ((1, 2), (3, 4)) DB.values([[1, 2], [3, 4]]).order(:column2).limit(1, 1) # VALUES ((1, 2), (3, 4)) ORDER BY column2 LIMIT 1 OFFSET 1
# File lib/sequel/adapters/shared/postgres.rb 859 def values(v) 860 raise Error, "Cannot provide an empty array for values" if v.empty? 861 @default_dataset.clone(:values=>v) 862 end
Array of symbols specifying view names in the current database.
Options:
:materialized |
Return materialized views |
:qualify |
Return the views as Sequel::SQL::QualifiedIdentifier instances, using the schema the view is located in as the qualifier. |
:schema |
The schema to search |
:server |
The server to use |
# File lib/sequel/adapters/shared/postgres.rb 872 def views(opts=OPTS) 873 relkind = opts[:materialized] ? 'm' : 'v' 874 pg_class_relname(relkind, opts) 875 end
Attempt to acquire an exclusive advisory lock with the given lock_id (which should be a 64-bit integer). If successful, yield to the block, then release the advisory lock when the block exits. If unsuccessful, raise a Sequel::AdvisoryLockError.
DB.with_advisory_lock(1347){DB.get(1)} # SELECT pg_try_advisory_lock(1357) LIMIT 1 # SELECT 1 AS v LIMIT 1 # SELECT pg_advisory_unlock(1357) LIMIT 1
Options:
:wait |
Do not raise an error, instead, wait until the advisory lock can be acquired. |
# File lib/sequel/adapters/shared/postgres.rb 888 def with_advisory_lock(lock_id, opts=OPTS) 889 ds = dataset 890 if server = opts[:server] 891 ds = ds.server(server) 892 end 893 894 synchronize(server) do |c| 895 begin 896 if opts[:wait] 897 ds.get{pg_advisory_lock(lock_id)} 898 locked = true 899 else 900 unless locked = ds.get{pg_try_advisory_lock(lock_id)} 901 raise AdvisoryLockError, "unable to acquire advisory lock #{lock_id.inspect}" 902 end 903 end 904 905 yield 906 ensure 907 ds.get{pg_advisory_unlock(lock_id)} if locked 908 end 909 end 910 end