class Janko::Connection
Attributes
backend[R]
Public Class Methods
build(backend)
click to toggle source
# File lib/janko/connection.rb, line 8 def Connection.build(backend) return(default) if backend.nil? return(backend) if backend.is_a?(Connection) new(backend) end
cache_catalog() { || ... }
click to toggle source
# File lib/janko/connection.rb, line 22 def Connection.cache_catalog @catalog ||= yield end
default()
click to toggle source
# File lib/janko/connection.rb, line 14 def Connection.default if Kernel.const_defined?("ActiveRecord::Base") new(Kernel.const_get("ActiveRecord::Base")) else raise("No default connection available.") end end
new(backend)
click to toggle source
# File lib/janko/connection.rb, line 36 def initialize(backend) @backend = extract_raw_connection(backend) end
reset_cached_catalog()
click to toggle source
# File lib/janko/connection.rb, line 26 def Connection.reset_cached_catalog @catalog = nil self end
Public Instance Methods
catalog()
click to toggle source
dba.stackexchange.com/questions/22362/ www.postgresql.org/docs/9.3/static/catalog-pg-attribute.html www.postgresql.org/docs/9.3/static/catalog-pg-attrdef.html
# File lib/janko/connection.rb, line 51 def catalog Connection.cache_catalog do result = backend.exec(<<-END) SELECT relname, attname, typname, pg_get_expr(adbin, 0) FROM pg_class LEFT JOIN pg_namespace ON ( pg_class.relnamespace = pg_namespace.oid) LEFT JOIN pg_attribute ON ( pg_class.oid = pg_attribute.attrelid) LEFT JOIN pg_attrdef ON ( pg_attribute.attrelid = pg_attrdef.adrelid AND pg_attribute.attnum = pg_attrdef.adnum) LEFT JOIN pg_type ON ( pg_attribute.atttypid = pg_type.oid) WHERE pg_class.relkind IN ('r','') AND pg_namespace.nspname NOT IN ('pg_catalog', 'pg_toast') AND pg_table_is_visible(pg_class.oid) AND attnum > 0 AND NOT attisdropped; END output = {} result.each_row do |row| output[row[0]] ||= {} output[row[0]][row[1]] = { type: row[2], default: row[3] } end output end end
column_default(table, column)
click to toggle source
# File lib/janko/connection.rb, line 90 def column_default(table, column) catalog[table][column][:default] end
column_list(table)
click to toggle source
# File lib/janko/connection.rb, line 82 def column_list(table) catalog[table].keys end
column_type(table, column)
click to toggle source
# File lib/janko/connection.rb, line 86 def column_type(table, column) catalog[table][column][:type] end
failed?()
click to toggle source
# File lib/janko/connection.rb, line 44 def failed? backend.transaction_status >= 3 end
in_transaction?()
click to toggle source
# File lib/janko/connection.rb, line 40 def in_transaction? backend.transaction_status > 0 end
Private Instance Methods
extract_raw_connection(backend)
click to toggle source
# File lib/janko/connection.rb, line 100 def extract_raw_connection(backend) return(backend) if backend.is_a?(PG::Connection) maybe(backend).raw_connection._ \ or maybe(backend).connection.raw_connection._ \ or raise("Unable to extract a connection from: #{backend}") end
maybe(*args)
click to toggle source
# File lib/janko/connection.rb, line 96 def maybe(*args) Agrippa::Maybe.new(*args) end