Class: Janko::Connection

Inherits:
Object
  • Object
show all
Includes:
Agrippa::Delegation
Defined in:
lib/janko/connection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#backendObject (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_catalogObject



22
23
24
# File 'lib/janko/connection.rb', line 22

def Connection.cache_catalog
    @catalog ||= yield
end

.defaultObject



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_catalogObject



26
27
28
29
# File 'lib/janko/connection.rb', line 26

def Connection.reset_cached_catalog
    @catalog = nil
    self
end

Instance Method Details

#catalogObject



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

Returns:

  • (Boolean)


44
45
46
# File 'lib/janko/connection.rb', line 44

def failed?
    backend.transaction_status >= 3
end

#in_transaction?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/janko/connection.rb', line 40

def in_transaction?
    backend.transaction_status > 0
end