Class: PgCtrl::RDBMS

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/pg_ctrl/db.rb,
lib/pg_ctrl/load.rb

Constant Summary collapse

LOAD_USERS_QUERY =
%(
  select  oid as OID,
          rolname as NAME,
          rolsuper as IS_SUPERUSER,
          rolcreaterole as CAN_CREATE_ROLE,
          rolcreatedb as CAN_CREATE_DB,
          rolcanlogin as CAN_LOGIN,
          case when rolname = current_user then true else false end as
              IS_CURRENT_USER
  from    pg_roles
)
LOAD_DATABASES_QUERY =
%(
  select  pg_database.oid as OID,
          datname as NAME,
          rolname as OWNER,
          pg_encoding_to_char(encoding) as ENCODING,
          datcollate as LC_COLLATE,
          datctype as LC_CTYPE,
          datistemplate as IS_TEMPLATE,
          datallowconn as CAN_CONNECT,
          datacl as ACL,
          case when datname = current_database() then true else false end as
              IS_CURRENT_DATABASE
  from    pg_database
  join    pg_roles on datdba = pg_roles.oid
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#set_opts

Constructor Details

#initialize(*args) ⇒ RDBMS

Arguments

initialize(PGconn args)
initialize(PGconn object)


65
66
67
68
69
70
71
72
73
# File 'lib/pg_ctrl/db.rb', line 65

def initialize(*args)
  if args.size == 1 && PGconn === args[0]
    @connection = args[0]
  else
    @connection = PGconn.new(args[0])
  end
  @id = (!host || host == "127.0.0.1" ? "localhost" : host)
  load_data
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



48
49
50
# File 'lib/pg_ctrl/db.rb', line 48

def connection
  @connection
end

#databaseObject (readonly)

Returns the value of attribute database.



59
60
61
# File 'lib/pg_ctrl/db.rb', line 59

def database
  @database
end

#databasesObject (readonly)

Returns the value of attribute databases.



55
56
57
# File 'lib/pg_ctrl/db.rb', line 55

def databases
  @databases
end

#idObject (readonly)

Returns the value of attribute id.



46
47
48
# File 'lib/pg_ctrl/db.rb', line 46

def id
  @id
end

#userObject (readonly)

Current user and database



58
59
60
# File 'lib/pg_ctrl/db.rb', line 58

def user
  @user
end

#usersObject (readonly)

Users and databases indexed by name



54
55
56
# File 'lib/pg_ctrl/db.rb', line 54

def users
  @users
end

Instance Method Details

#hostObject



50
# File 'lib/pg_ctrl/db.rb', line 50

def host() @connection.host end

#ip?Boolean

Returns:

  • (Boolean)


49
# File 'lib/pg_ctrl/db.rb', line 49

def ip?() !host.nil? end

#load_dataObject



72
73
74
75
76
77
# File 'lib/pg_ctrl/load.rb', line 72

def load_data
  @users = RDBMSObjectSet.new
  @databases = RDBMSObjectSet.new
  load_users
  load_databases
end

#load_databasesObject



64
65
66
67
68
69
70
# File 'lib/pg_ctrl/load.rb', line 64

def load_databases
  @connection.each_hash(LOAD_DATABASES_QUERY) { |r|
    @databases.add \
        Database.new(self, r[:oid], r[:name], @users[r[:owner]], r)
    @database = @databases[r[:name]] if r[:is_current_database]
  }
end

#load_usersObject



41
42
43
44
45
46
# File 'lib/pg_ctrl/load.rb', line 41

def load_users
  @connection.each_hash(LOAD_USERS_QUERY) { |r|
    @users.add User.new(self, r[:oid], r[:name], r)
    @user = @users[r[:name]] if r[:is_current_user]
  }
end

#portObject



51
# File 'lib/pg_ctrl/db.rb', line 51

def port() @connection.port.to_i end

#to_sObject



75
# File 'lib/pg_ctrl/db.rb', line 75

def to_s() @id end