Class: Janko::Connection
- Inherits:
-
Object
- Object
- Janko::Connection
- Includes:
- Agrippa::Delegation
- Defined in:
- lib/janko/connection.rb
Instance Attribute Summary collapse
-
#backend ⇒ Object
readonly
Returns the value of attribute backend.
Class Method Summary collapse
Instance Method Summary collapse
- #catalog ⇒ Object
- #column_default(table, column) ⇒ Object
- #column_list(table) ⇒ Object
- #column_type(table, column) ⇒ Object
- #failed? ⇒ Boolean
- #in_transaction? ⇒ Boolean
-
#initialize(backend) ⇒ Connection
constructor
A new instance of Connection.
Constructor Details
#initialize(backend) ⇒ Connection
Returns a new instance of Connection.
36 37 38 |
# File 'lib/janko/connection.rb', line 36 def initialize(backend) @backend = extract_raw_connection(backend) end |
Instance Attribute Details
#backend ⇒ Object (readonly)
Returns the value of attribute backend.
31 32 33 |
# File 'lib/janko/connection.rb', line 31 def backend @backend end |
Class Method Details
.build(backend) ⇒ Object
8 9 10 11 12 |
# 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 ⇒ Object
22 23 24 |
# File 'lib/janko/connection.rb', line 22 def Connection.cache_catalog @catalog ||= yield end |
.default ⇒ Object
14 15 16 17 18 19 20 |
# 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 |
.reset_cached_catalog ⇒ Object
26 27 28 29 |
# File 'lib/janko/connection.rb', line 26 def Connection.reset_cached_catalog @catalog = nil self end |
Instance Method Details
#catalog ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# 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) ⇒ Object
90 91 92 |
# File 'lib/janko/connection.rb', line 90 def column_default(table, column) catalog[table][column][:default] end |
#column_list(table) ⇒ Object
82 83 84 |
# File 'lib/janko/connection.rb', line 82 def column_list(table) catalog[table].keys end |
#column_type(table, column) ⇒ Object
86 87 88 |
# File 'lib/janko/connection.rb', line 86 def column_type(table, column) catalog[table][column][:type] end |
#failed? ⇒ Boolean
44 45 46 |
# File 'lib/janko/connection.rb', line 44 def failed? backend.transaction_status >= 3 end |
#in_transaction? ⇒ Boolean
40 41 42 |
# File 'lib/janko/connection.rb', line 40 def in_transaction? backend.transaction_status > 0 end |