gadget 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ece84105a691b5880948bbeac5dcb229ba654aa6
4
- data.tar.gz: 4fc8cc56f871fdcb5c471a50a8a9a39295375b7d
3
+ metadata.gz: 974283a8788c4a40b2a75f8d8350f6c316836ff5
4
+ data.tar.gz: 66abb23005b88002c0fae59554f7695b6f452bf0
5
5
  SHA512:
6
- metadata.gz: b18de42940505918186b4622d7e1cac13cd0e55b03b0cd687a634dc6267c2899ee6a3032593c998f3890565be16d94d7989daf6cdefbb19dc2ee6ff6ee3666e6
7
- data.tar.gz: 92381cb642b34f08e841335f4c7bf4fcbcdc9607a811f7dc02385f12e61948bfb4a5b34ed8d6bb07e5273f28ef82d9b1fd50bcf914f61f8ca55e51f45f0dd6a9
6
+ metadata.gz: 95e4383e87f251b97eb861e406d6452c3a7ca684d42e384f30311ff6e85a53b2a1ce72bcbd673191fb6fabfb8dd4d6512cd97b1e3372e2c680649b5e163df165
7
+ data.tar.gz: 167996bc9821904dd2e036de143b02d522aae6d2415536a8508b8059e5bb3c2fdbb209c209bb37d228ba8aaf97faa65697dd3e53178004c1cd7b76cedd6a0c82
data/README.md CHANGED
@@ -32,6 +32,14 @@ If `tablename` is given, returns the columns in only that table.
32
32
  Returns a list of all foreign keys in the schema reachable through `conn`.
33
33
  If `tablename` is given, returns the foreign keys in only that table.
34
34
 
35
+ `#functions(conn)`
36
+
37
+ Returns a list of all functions in the schema reachable through `conn`.
38
+
39
+ `#triggers(conn)`
40
+
41
+ Returns a list of all triggers in the schema reachable through `conn`.
42
+
35
43
  `#dependencies(conn)`
36
44
 
37
45
  Returns a structure representing the dependencies between tables in the schema reachable through `conn`.
data/bin/gadget CHANGED
@@ -4,9 +4,23 @@ require 'gadget'
4
4
 
5
5
  require 'pg'
6
6
 
7
- dbname = ARGV.shift
7
+ (command, dbname) = ARGV
8
8
 
9
- conn = PG::Connection.open(:dbname => dbname)
10
- tables = Gadget.tables_in_dependency_order(conn)
9
+ conn = PG::Connection.open(:dbname => dbname, :host => 'localhost')
10
+ result = nil
11
+ case command
12
+ when 'dependencies'
13
+ result = Gadget.dependencies(conn)
14
+ when 'dependency-graph'
15
+ result = Gadget.dependency_graph(conn)
16
+ when 'dependency-order'
17
+ result = Gadget.tables_in_dependency_order(conn)
18
+ when 'functions'
19
+ result = Gadget.functions(conn)
20
+ when 'tables'
21
+ result = Gadget.tables(conn)
22
+ when 'triggers'
23
+ result = Gadget.triggers(conn)
24
+ end
11
25
  conn.close
12
- p tables
26
+ p result
@@ -25,9 +25,9 @@ module Gadget
25
25
  def self.columns(conn, tablename = nil)
26
26
  sql = <<-END_OF_SQL
27
27
  SELECT t.tablename, a.attname
28
- FROM pg_attribute a
29
- INNER JOIN pg_class c ON a.attrelid=c.oid
30
- INNER JOIN pg_tables t ON c.relname=t.tablename
28
+ FROM pg_catalog.pg_attribute a
29
+ INNER JOIN pg_catalog.pg_class c ON a.attrelid=c.oid
30
+ INNER JOIN pg_catalog.pg_tables t ON c.relname=t.tablename
31
31
  WHERE a.attnum >= 0
32
32
  END_OF_SQL
33
33
  if tablename.nil?
@@ -44,11 +44,11 @@ WHERE a.attnum >= 0
44
44
  def self.foreign_keys(conn, tablename = nil)
45
45
  sql = <<-END_OF_SQL
46
46
  SELECT t1.tablename AS tablename, pg_constraint.conkey as cols, t2.tablename AS refname, pg_constraint.confkey as refcols
47
- FROM pg_constraint
48
- INNER JOIN pg_class c1 ON pg_constraint.conrelid=c1.oid
49
- INNER JOIN pg_tables t1 ON c1.relname=t1.tablename
50
- INNER JOIN pg_class c2 ON pg_constraint.confrelid=c2.oid
51
- INNER JOIN pg_tables t2 ON c2.relname=t2.tablename
47
+ FROM pg_catalog.pg_constraint
48
+ INNER JOIN pg_catalog.pg_class c1 ON pg_constraint.conrelid=c1.oid
49
+ INNER JOIN pg_catalog.pg_tables t1 ON c1.relname=t1.tablename
50
+ INNER JOIN pg_catalog.pg_class c2 ON pg_constraint.confrelid=c2.oid
51
+ INNER JOIN pg_catalog.pg_tables t2 ON c2.relname=t2.tablename
52
52
  WHERE t1.schemaname='public'
53
53
  AND t2.schemaname='public'
54
54
  AND pg_constraint.contype='f'
@@ -105,4 +105,31 @@ AND pg_constraint.contype='f'
105
105
  puts "}"
106
106
  end
107
107
 
108
+ def self.functions(conn)
109
+ rs = conn.exec(<<-END_OF_SQL)
110
+ SELECT p.oid, p.proname
111
+ FROM pg_catalog.pg_proc p
112
+ INNER JOIN pg_catalog.pg_namespace n ON p.pronamespace = n.oid
113
+ WHERE n.nspname = 'public'
114
+ END_OF_SQL
115
+
116
+ tuples = rs.reduce({}) { | h, row | h[row['proname']] = { :oid => row['oid'] }; h }
117
+ rs.clear
118
+ tuples
119
+ end
120
+
121
+ def self.triggers(conn)
122
+ rs = conn.exec(<<-END_OF_SQL)
123
+ SELECT tg.oid, tg.tgname, t.tablename
124
+ FROM pg_catalog.pg_trigger tg
125
+ INNER JOIN pg_catalog.pg_class c ON tg.tgrelid = c.oid
126
+ INNER JOIN pg_catalog.pg_tables t ON c.relname = t.tablename
127
+ WHERE tg.tgconstrrelid = 0
128
+ END_OF_SQL
129
+
130
+ tuples = rs.reduce({}) { | h, row | h[row['tgname']] = { :oid => row['oid'], :tablename => row['tablename'] }; h }
131
+ rs.clear
132
+ tuples
133
+ end
134
+
108
135
  end
@@ -1,3 +1,3 @@
1
1
  module Gadget
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gadget
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Craig S. Cottingham
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-28 00:00:00.000000000 Z
11
+ date: 2014-02-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg
@@ -87,6 +87,14 @@ description: |
87
87
  Returns a list of all foreign keys in the schema reachable through `conn`.
88
88
  If `tablename` is given, returns the foreign keys in only that table.
89
89
 
90
+ `#functions(conn)`
91
+
92
+ Returns a list of all functions in the schema reachable through `conn`.
93
+
94
+ `#triggers(conn)`
95
+
96
+ Returns a list of all triggers in the schema reachable through `conn`.
97
+
90
98
  `#dependencies(conn)`
91
99
 
92
100
  Returns a structure representing the dependencies between tables in the schema reachable through `conn`.