Class | Sequel::Postgres::Database |
In: |
lib/sequel/adapters/postgres.rb
|
Parent: | Sequel::Database |
INFINITE_TIMESTAMP_STRINGS | = | ['infinity'.freeze, '-infinity'.freeze].freeze |
INFINITE_DATETIME_VALUES | = | ([PLUS_INFINITY, MINUS_INFINITY] + INFINITE_TIMESTAMP_STRINGS).freeze |
convert_infinite_timestamps | [RW] | Whether infinite timestamps should be converted on retrieval. By default, no conversion is done, so an error is raised if you attempt to retrieve an infinite timestamp. You can set this to :nil to convert to nil, :string to leave as a string, or :float to convert to an infinite float. |
Add the primary_keys and primary_key_sequences instance variables, so we can get the correct return values for inserted rows.
# File lib/sequel/adapters/postgres.rb, line 167 167: def initialize(*args) 168: super 169: @convert_infinite_timestamps = false 170: initialize_postgres_adapter 171: end
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", and it won‘t ever handle Sequel::SQLTime values correctly. Only public for use by the adapter, shouldn‘t be used by external code.
# File lib/sequel/adapters/postgres.rb, line 177 177: def bound_variable_arg(arg, conn) 178: case arg 179: when Sequel::SQL::Blob 180: conn.escape_bytea(arg) 181: when Sequel::SQLTime 182: literal(arg) 183: when DateTime, Time 184: literal(arg) 185: else 186: arg 187: end 188: end
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, and :sslmode sets whether postgres‘s sslmode. :connect_timeout and :ssl_mode are only supported if the pg driver is used.
# File lib/sequel/adapters/postgres.rb, line 196 196: def connect(server) 197: opts = server_opts(server) 198: conn = if SEQUEL_POSTGRES_USES_PG 199: connection_params = { 200: :host => opts[:host], 201: :port => opts[:port] || 5432, 202: :dbname => opts[:database], 203: :user => opts[:user], 204: :password => opts[:password], 205: :connect_timeout => opts[:connect_timeout] || 20, 206: :sslmode => opts[:sslmode] 207: }.delete_if { |key, value| blank_object?(value) } 208: Adapter.connect(connection_params) 209: else 210: Adapter.connect( 211: (opts[:host] unless blank_object?(opts[:host])), 212: opts[:port] || 5432, 213: nil, '', 214: opts[:database], 215: opts[:user], 216: opts[:password] 217: ) 218: end 219: if encoding = opts[:encoding] || opts[:charset] 220: if conn.respond_to?(:set_client_encoding) 221: conn.set_client_encoding(encoding) 222: else 223: conn.async_exec("set client_encoding to '#{encoding}'") 224: end 225: end 226: conn.instance_variable_set(:@db, self) 227: conn.instance_variable_set(:@prepared_statements, {}) if SEQUEL_POSTGRES_USES_PG 228: connection_configuration_sqls.each{|sql| conn.execute(sql)} 229: conn 230: end
copy_table uses PostgreSQL‘s COPY 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 FROM+ or +COPY TO+ with a filename, you should just use run instead of this method. This method does not currently support +COPY FROM STDIN+, but that may be supported in the future.
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+. |
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.
# File lib/sequel/adapters/postgres.rb, line 264 264: def copy_table(table, opts={}) 265: sql = if table.is_a?(String) 266: sql = table 267: else 268: if opts[:options] || opts[:format] 269: options = " (" 270: options << "FORMAT #{opts[:format]}" if opts[:format] 271: options << "#{', ' if opts[:format]}#{opts[:options]}" if opts[:options] 272: options << ')' 273: end 274: table = if table.is_a?(::Sequel::Dataset) 275: "(#{table.sql})" 276: else 277: literal(table) 278: end 279: sql = "COPY #{table} TO STDOUT#{options}" 280: end 281: synchronize(opts[:server]) do |conn| 282: conn.execute(sql) 283: begin 284: if block_given? 285: while buf = conn.get_copy_data 286: yield buf 287: end 288: nil 289: else 290: b = '' 291: b << buf while buf = conn.get_copy_data 292: b 293: end 294: ensure 295: raise DatabaseDisconnectError, "disconnecting as a partial COPY may leave the connection in an unusable state" if buf 296: end 297: end 298: end
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 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:
# File lib/sequel/adapters/postgres.rb, line 321 321: def listen(channels, opts={}, &block) 322: check_database_errors do 323: synchronize(opts[:server]) do |conn| 324: begin 325: channels = Array(channels) 326: channels.each{|channel| conn.execute("LISTEN #{channel}")} 327: opts[:after_listen].call(conn) if opts[:after_listen] 328: timeout = opts[:timeout] ? [opts[:timeout]] : [] 329: if l = opts[:loop] 330: raise Error, 'calling #listen with :loop requires a block' unless block 331: loop_call = l.respond_to?(:call) 332: catch(:stop) do 333: loop do 334: conn.wait_for_notify(*timeout, &block) 335: l.call(conn) if loop_call 336: end 337: end 338: nil 339: else 340: conn.wait_for_notify(*timeout, &block) 341: end 342: ensure 343: conn.execute("UNLISTEN *") 344: end 345: end 346: end 347: end
If convert_infinite_timestamps is true and the value is infinite, return an appropriate value based on the convert_infinite_timestamps setting.
# File lib/sequel/adapters/postgres.rb, line 352 352: def to_application_timestamp(value) 353: if c = convert_infinite_timestamps 354: case value 355: when *INFINITE_TIMESTAMP_STRINGS 356: infinite_timestamp_value(value) 357: else 358: super 359: end 360: else 361: super 362: end 363: end