Class: Sequel::Postgres::Database

Inherits:
Database show all
Includes:
DatabaseMethods
Defined in:
lib/sequel/adapters/postgres.rb

Constant Summary

Constants included from DatabaseMethods

Sequel::Postgres::DatabaseMethods::FOREIGN_KEY_LIST_ON_DELETE_MAP, Sequel::Postgres::DatabaseMethods::ON_COMMIT, Sequel::Postgres::DatabaseMethods::SELECT_CUSTOM_SEQUENCE_SQL, Sequel::Postgres::DatabaseMethods::SELECT_PK_SQL, Sequel::Postgres::DatabaseMethods::SELECT_SERIAL_SEQUENCE_SQL

Constants inherited from Database

Database::ADAPTERS, Database::COLUMN_DEFINITION_ORDER, Database::COLUMN_SCHEMA_DATETIME_TYPES, Database::COLUMN_SCHEMA_STRING_TYPES, Database::COMBINABLE_ALTER_TABLE_OPS, Database::DEFAULT_DATABASE_ERROR_REGEXPS, Database::DEFAULT_STRING_COLUMN_SIZE, Database::EXTENSIONS, Database::OPTS, Database::SCHEMA_TYPE_CLASSES, Database::TRANSACTION_ISOLATION_LEVELS

Instance Attribute Summary

Attributes included from DatabaseMethods

#conversion_procs

Attributes inherited from Database

#cache_schema, #check_string_typecast_bytesize, #dataset_class, #default_string_column_size, #log_connection_info, #log_warn_duration, #loggers, #opts, #pool, #prepared_statements, #sql_log_level, #timezone, #transaction_isolation_level

Instance Method Summary collapse

Methods included from DatabaseMethods

#add_conversion_proc, #add_named_conversion_proc, #check_constraints, #commit_prepared_transaction, #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

Methods inherited from Database

#<<, #[], adapter_class, adapter_scheme, #adapter_scheme, #add_column, #add_index, #add_servers, #after_commit, after_initialize, #after_rollback, #alter_table, #alter_table_generator, #call, #cast_type_literal, connect, #create_join_table, #create_join_table!, #create_join_table?, #create_or_replace_view, #create_table, #create_table!, #create_table?, #create_table_generator, #create_view, #database_type, #dataset, #disconnect, #drop_column, #drop_index, #drop_join_table, #drop_table, #drop_table?, #drop_view, #execute_ddl, #execute_dui, #execute_insert, #extend_datasets, #extension, extension, #fetch, #freeze, #from, #from_application_timestamp, #get, #global_index_namespace?, #in_transaction?, #initialize, #inspect, #literal, #literal_symbol, #literal_symbol_set, load_adapter, #log_connection_yield, #log_exception, #log_info, #logger=, #new_connection, #prepared_statement, #quote_identifier, register_extension, #remove_servers, #rename_column, #rename_table, #rollback_checker, #rollback_on_exit, #run, run_after_initialize, #schema, #schema_type_class, #select, #serial_primary_key_options, #servers, #set_column_default, #set_column_type, #set_prepared_statement, set_shared_adapter_scheme, #sharded?, #single_threaded?, #supports_create_table_if_not_exists?, #supports_deferrable_constraints?, #supports_deferrable_foreign_key_constraints?, #supports_drop_table_if_exists?, #supports_foreign_key_parsing?, #supports_index_parsing?, #supports_partial_indexes?, #supports_prepared_transactions?, #supports_savepoints?, #supports_savepoints_in_prepared_transactions?, #supports_schema_parsing?, #supports_table_listing?, #supports_transaction_isolation_levels?, #supports_transactional_ddl?, #supports_view_listing?, #supports_views_with_check_option?, #supports_views_with_local_check_option?, #synchronize, #table_exists?, #test_connection, #to_application_timestamp, #transaction, #typecast_value, #uri, #url, #valid_connection?

Constructor Details

This class inherits a constructor from Sequel::Database

Instance Method Details

#bound_variable_arg(arg, conn) ⇒ Object

Convert given argument so that it can be used directly by pg. Currently, pg doesn’t handle fractional seconds in Time/DateTime or blobs with “0”. Only public for use by the adapter, shouldn’t be used by external code.



184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/sequel/adapters/postgres.rb', line 184

def bound_variable_arg(arg, conn)
  case arg
  when Sequel::SQL::Blob
    {:value=>arg, :type=>17, :format=>1}
  # :nocov:
  # Not covered by tests as tests use pg_extended_date_support
  # extension, which has basically the same code.
  when Time, DateTime
    @default_dataset.literal_date_or_time(arg)
  # :nocov:
  else
    arg
  end
end

#call_procedure(name, *args) ⇒ Object

Call a procedure with the given name and arguments. Returns a hash if the procedure returns a value, and nil otherwise. Example:

DB.call_procedure(:foo, 1, 2)
# CALL foo(1, 2)


204
205
206
# File 'lib/sequel/adapters/postgres.rb', line 204

def call_procedure(name, *args)
  dataset.send(:call_procedure, name, args)
end

#connect(server) ⇒ Object

Connects to the database. In addition to the standard database options, using the :encoding or :charset option changes the client encoding for the connection, :connect_timeout is a connection timeout in seconds, :sslmode sets whether postgres’s sslmode, and :notice_receiver handles server notices in a proc. :connect_timeout, :driver_options, :sslmode, and :notice_receiver are only supported if the pg driver is used.



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/sequel/adapters/postgres.rb', line 215

def connect(server)
  opts = server_opts(server)
  if USES_PG
    connection_params = {
      :host => opts[:host],
      :port => opts[:port],
      :dbname => opts[:database],
      :user => opts[:user],
      :password => opts[:password],
      :connect_timeout => opts[:connect_timeout] || 20,
      :sslmode => opts[:sslmode],
      :sslrootcert => opts[:sslrootcert]
    }.delete_if { |key, value| blank_object?(value) }
    # :nocov:
    connection_params.merge!(opts[:driver_options]) if opts[:driver_options]
    # :nocov:
    conn = Adapter.connect(opts[:conn_str] || connection_params)

    conn.instance_variable_set(:@prepared_statements, {})

    if receiver = opts[:notice_receiver]
      conn.set_notice_receiver(&receiver)
    end

    # :nocov:
    if conn.respond_to?(:type_map_for_queries=) && defined?(PG_QUERY_TYPE_MAP)
    # :nocov:
      conn.type_map_for_queries = PG_QUERY_TYPE_MAP
    end
  # :nocov:
  else
    unless typecast_value_boolean(@opts.fetch(:force_standard_strings, true))
      raise Error, "Cannot create connection using postgres-pr unless force_standard_strings is set"
    end

    conn = Adapter.connect(
      (opts[:host] unless blank_object?(opts[:host])),
      opts[:port] || 5432,
      nil, '',
      opts[:database],
      opts[:user],
      opts[:password]
    )
  end
  # :nocov:

  conn.instance_variable_set(:@db, self)

  # :nocov:
  if encoding = opts[:encoding] || opts[:charset]
    if conn.respond_to?(:set_client_encoding)
      conn.set_client_encoding(encoding)
    else
      conn.async_exec("set client_encoding to '#{encoding}'")
    end
  end
  # :nocov:

  connection_configuration_sqls(opts).each{|sql| conn.execute(sql)}
  conn
end

#convert_infinite_timestampsObject

Always false, support was moved to pg_extended_date_support extension. Needs to stay defined here so that sequel_pg works.



279
280
281
# File 'lib/sequel/adapters/postgres.rb', line 279

def convert_infinite_timestamps
  false
end

#convert_infinite_timestamps=(v) ⇒ Object

Enable pg_extended_date_support extension if symbol or string is given.



284
285
286
287
288
289
290
# File 'lib/sequel/adapters/postgres.rb', line 284

def convert_infinite_timestamps=(v)
  case v
  when Symbol, String, true
    extension(:pg_extended_date_support)
    self.convert_infinite_timestamps = v
  end
end

#copy_into(table, opts = OPTS) ⇒ Object

copy_into uses PostgreSQL’s COPY FROM STDIN SQL statement to do very fast inserts into a table using input preformatting in either CSV or PostgreSQL text format. This method is only supported if pg 0.14.0+ is the underlying ruby driver. This method should only be called if you want results returned to the client. If you are using COPY FROM with a filename, you should just use run instead of this method.

The following options are respected:

:columns

The columns to insert into, with the same order as the columns in the input data. If this isn’t given, uses all columns in the table.

:data

The data to copy to PostgreSQL, which should already be in CSV or PostgreSQL text format. This can be either a string, or any object that responds to each and yields string.

:format

The format to use. text is the default, so this should be :csv or :binary.

:options

An options SQL string to use, which should contain comma separated options.

:server

The server on which to run the query.

If a block is provided and :data option is not, this will yield to the block repeatedly. The block should return a string, or nil to signal that it is finished.



431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/sequel/adapters/postgres.rb', line 431

def copy_into(table, opts=OPTS)
  data = opts[:data]
  data = Array(data) if data.is_a?(String)

  if defined?(yield) && data
    raise Error, "Cannot provide both a :data option and a block to copy_into"
  elsif !defined?(yield) && !data
    raise Error, "Must provide either a :data option or a block to copy_into"
  end

  synchronize(opts[:server]) do |conn|
    conn.execute(copy_into_sql(table, opts))
    begin
      if defined?(yield)
        while buf = yield
          conn.put_copy_data(buf)
        end
      else
        data.each{|buff| conn.put_copy_data(buff)}
      end
    rescue Exception => e
      conn.put_copy_end("ruby exception occurred while copying data into PostgreSQL")
    ensure
      conn.put_copy_end unless e
      while res = conn.get_result
        raise e if e
        check_database_errors{res.check}
      end
    end
  end 
end

#copy_table(table, opts = OPTS) ⇒ Object

copy_table uses PostgreSQL’s COPY TO STDOUT SQL statement to return formatted results directly to the caller. This method is only supported if pg is the underlying ruby driver. This method should only be called if you want results returned to the client. If you are using COPY TO with a filename, you should just use run instead of this method.

The table argument supports the following types:

String

Uses the first argument directly as literal SQL. If you are using a version of PostgreSQL before 9.0, you will probably want to use a string if you are using any options at all, as the syntax Sequel uses for options is only compatible with PostgreSQL 9.0+. This should be the full COPY statement passed to PostgreSQL, not just the SELECT query. If a string is given, the :format and :options options are ignored.

Dataset

Uses a query instead of a table name when copying.

other

Uses a table name (usually a symbol) when copying.

The following options are respected:

:format

The format to use. text is the default, so this should be :csv or :binary.

:options

An options SQL string to use, which should contain comma separated options.

:server

The server on which to run the query.

If a block is provided, the method continually yields to the block, one yield per row. If a block is not provided, a single string is returned with all of the data.



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/sequel/adapters/postgres.rb', line 381

def copy_table(table, opts=OPTS)
  synchronize(opts[:server]) do |conn|
    conn.execute(copy_table_sql(table, opts))
    begin
      if defined?(yield)
        while buf = conn.get_copy_data
          yield buf
        end
        b = nil
      else
        b = String.new
        b << buf while buf = conn.get_copy_data
      end

      res = conn.get_last_result
      if !res || res.result_status != 1
        raise PG::NotAllCopyDataRetrieved, "Not all COPY data retrieved"
      end

      b
    rescue => e
      raise_error(e, :disconnect=>true)
    ensure
      if buf && !e
        raise DatabaseDisconnectError, "disconnecting as a partial COPY may leave the connection in an unusable state"
      end
    end
  end 
end

#disconnect_connection(conn) ⇒ Object



292
293
294
295
296
# File 'lib/sequel/adapters/postgres.rb', line 292

def disconnect_connection(conn)
  conn.finish
rescue PGError, IOError
  nil
end

#error_info(e) ⇒ Object

Return a hash of information about the related PGError (or Sequel::DatabaseError that wraps a PGError), with the following entries (any of which may be nil):

:schema

The schema name related to the error

:table

The table name related to the error

:column

the column name related to the error

:constraint

The constraint name related to the error

:type

The datatype name related to the error

:severity

The severity of the error (e.g. “ERROR”)

:sql_state

The SQL state code related to the error

:message_primary

A single line message related to the error

:message_detail

Any detail supplementing the primary message

:message_hint

Possible suggestion about how to fix the problem

:statement_position

Character offset in statement submitted by client where error occurred (starting at 1)

:internal_position

Character offset in internal statement where error occurred (starting at 1)

:internal_query

Text of internally-generated statement where error occurred

:source_file

PostgreSQL source file where the error occurred

:source_line

Line number of PostgreSQL source file where the error occurred

:source_function

Function in PostgreSQL source file where the error occurred

This requires a PostgreSQL 9.3+ server and 9.3+ client library, and ruby-pg 0.16.0+ to be supported.



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/sequel/adapters/postgres.rb', line 323

def error_info(e)
  e = e.wrapped_exception if e.is_a?(DatabaseError)
  r = e.result
  {
    :schema => r.error_field(::PG::PG_DIAG_SCHEMA_NAME),
    :table => r.error_field(::PG::PG_DIAG_TABLE_NAME),
    :column => r.error_field(::PG::PG_DIAG_COLUMN_NAME),
    :constraint => r.error_field(::PG::PG_DIAG_CONSTRAINT_NAME),
    :type => r.error_field(::PG::PG_DIAG_DATATYPE_NAME),
    :severity => r.error_field(::PG::PG_DIAG_SEVERITY),
    :sql_state => r.error_field(::PG::PG_DIAG_SQLSTATE),
    :message_primary => r.error_field(::PG::PG_DIAG_MESSAGE_PRIMARY),
    :message_detail => r.error_field(::PG::PG_DIAG_MESSAGE_DETAIL),
    :message_hint => r.error_field(::PG::PG_DIAG_MESSAGE_HINT),
    :statement_position => r.error_field(::PG::PG_DIAG_STATEMENT_POSITION),
    :internal_position => r.error_field(::PG::PG_DIAG_INTERNAL_POSITION),
    :internal_query => r.error_field(::PG::PG_DIAG_INTERNAL_QUERY),
    :source_file => r.error_field(::PG::PG_DIAG_SOURCE_FILE),
    :source_line => r.error_field(::PG::PG_DIAG_SOURCE_LINE),
    :source_function => r.error_field(::PG::PG_DIAG_SOURCE_FUNCTION)
  }
end

#execute(sql, opts = OPTS, &block) ⇒ Object



347
348
349
# File 'lib/sequel/adapters/postgres.rb', line 347

def execute(sql, opts=OPTS, &block)
  synchronize(opts[:server]){|conn| check_database_errors{_execute(conn, sql, opts, &block)}}
end

#listen(channels, opts = OPTS, &block) ⇒ Object

Listens on the given channel (or multiple channels if channel is an array), waiting for notifications. After a notification is received, or the timeout has passed, stops listening to the channel. Options:

:after_listen

An object that responds to call that is called with the underlying connection after the LISTEN statement is sent, but before the connection starts waiting for notifications.

:loop

Whether to continually wait for notifications, instead of just waiting for a single notification. If this option is given, a block must be provided. If this object responds to call, it is called with the underlying connection after each notification is received (after the block is called). If a :timeout option is used, and a callable object is given, the object will also be called if the timeout expires. If :loop is used and you want to stop listening, you can either break from inside the block given to #listen, or you can throw :stop from inside the :loop object’s call method or the block.

:server

The server on which to listen, if the sharding support is being used.

:timeout

How long to wait for a notification, in seconds (can provide a float value for fractional seconds). If this object responds to call, it will be called and should return the number of seconds to wait. If the loop option is also specified, the object will be called on each iteration to obtain a new timeout value. If not given or nil, waits indefinitely.

This method is only supported if pg is used as the underlying ruby driver. It returns the channel the notification was sent to (as a string), unless :loop was used, in which case it returns nil. If a block is given, it is yielded 3 arguments:

  • the channel the notification was sent to (as a string)

  • the backend pid of the notifier (as an integer),

  • and the payload of the notification (as a string or nil).



486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# File 'lib/sequel/adapters/postgres.rb', line 486

def listen(channels, opts=OPTS, &block)
  check_database_errors do
    synchronize(opts[:server]) do |conn|
      begin
        channels = Array(channels)
        channels.each do |channel|
          sql = "LISTEN ".dup
          dataset.send(:identifier_append, sql, channel)
          conn.execute(sql)
        end
        opts[:after_listen].call(conn) if opts[:after_listen]
        timeout = opts[:timeout]
        if timeout
          timeout_block = timeout.respond_to?(:call) ? timeout : proc{timeout}
        end

        if l = opts[:loop]
          raise Error, 'calling #listen with :loop requires a block' unless block
          loop_call = l.respond_to?(:call)
          catch(:stop) do
            while true
              t = timeout_block ? [timeout_block.call] : []
              conn.wait_for_notify(*t, &block)
              l.call(conn) if loop_call
            end
          end
          nil
        else
          t = timeout_block ? [timeout_block.call] : []
          conn.wait_for_notify(*t, &block)
        end
      ensure
        conn.execute("UNLISTEN *")
      end
    end
  end
end