Class: PG::Doc::Engine
- Inherits:
-
Sinatra::Base
- Object
- Sinatra::Base
- PG::Doc::Engine
- Defined in:
- lib/pg/doc/engine.rb
Instance Method Summary collapse
- #parse_function_argument(arg) ⇒ Object
-
#setup(connection, opts) ⇒ Object
Initializes the internal state for this instance.
Instance Method Details
#parse_function_argument(arg) ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/pg/doc/engine.rb', line 259 def parse_function_argument arg # Determine the argument's mode arg = arg.strip argmode = case arg when /^VARIADIC\b/, /^OUT\b/, /^INOUT\b/, /^IN\b/ _parts = arg.split(" ") _mode = _parts.shift arg = _parts.join(" ") _mode else "IN" end # Determine it's name and type if arg.count(" ") > 0 name, *type = arg.split " " type = type.join " " else name = nil type = arg end {"name" => name, "type" => type, "mode" => argmode} end |
#setup(connection, opts) ⇒ Object
Initializes the internal state for this instance
42 43 44 45 46 47 48 49 50 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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/pg/doc/engine.rb', line 42 def setup connection, opts @conn = PG.connect connection @path_to_markdowns = opts.fetch(:path_to_markdowns, nil) @schema_filter = opts.fetch(:schema_filter, nil) || ->(field) { " \#{field} NOT ILIKE 'pg_%'\n AND \#{field} != 'information_schema'\n SQL\n }\n\n @cache = {schemas: {}}\n\n # Load schemas\n _recordset = @conn.exec <<~SQL\n SELECT\n schema_name\n FROM\n information_schema.schemata\n WHERE\n \#{@schema_filter.call :schema_name}\n ORDER BY\n 1\n SQL\n _recordset.each_with_object(@cache){ |row, h|\n h[:schemas][row[\"schema_name\"]] = {\n tables: {},\n views: {},\n functions: {},\n triggers: {}\n }\n }\n\n # Load tables\n _recordset = @conn.exec <<~SQL\n SELECT\n table_schema,\n table_name,\n obj_description((table_schema || '.' || table_name)::regclass::oid, 'pg_class') as comment\n FROM\n information_schema.tables\n WHERE\n \#{@schema_filter.call :table_schema}\n AND table_type = 'BASE TABLE'\n ORDER BY\n 1, 2\n SQL\n _recordset.each_with_object(@cache){ |row, h|\n h[:schemas][row[\"table_schema\"]][:tables][row[\"table_name\"]] = {\n columns: [],\n foreign_keys: {},\n indexes: {},\n comment: row[\"comment\"]\n }\n }\n\n # Load views\n _recordset = @conn.exec <<~SQL\n SELECT\n table_schema,\n table_name,\n view_definition,\n obj_description((table_schema || '.' || table_name)::regclass::oid, 'pg_class') as comment\n FROM\n information_schema.views\n WHERE\n \#{@schema_filter.call :table_schema}\n ORDER BY\n 1, 2\n SQL\n _recordset.each_with_object(@cache){ |row, h|\n h[:schemas][row[\"table_schema\"]][:views][row[\"table_name\"]] = {\n view_definition: row[\"view_definition\"],\n columns: [],\n comment: row[\"comment\"]\n }\n }\n\n # Load columns\n _recordset = @conn.exec <<~SQL\n SELECT\n c.table_schema,\n c.table_name,\n c.column_name,\n c.ordinal_position,\n c.data_type,\n c.is_nullable,\n c.column_default,\n col_description((c.table_schema || '.' || c.table_name)::regclass::oid, c.ordinal_position) as comment,\n t.table_type\n FROM\n information_schema.columns c\n JOIN information_schema.tables t USING (table_catalog, table_schema, table_name)\n WHERE\n \#{@schema_filter.call :table_schema}\n AND table_type = 'BASE TABLE'\n ORDER BY\n 1, 2, 4\n SQL\n _recordset.each_with_object(@cache){ |row, h|\n type = row.delete(\"table_type\") == \"VIEW\" ? :views : :tables\n schema = row.delete \"table_schema\"\n name = row.delete \"table_name\"\n h[:schemas][schema][type][name][:columns] << row\n }\n\n # Load functions (note: this currently does not support overloaded functions)\n _recordset = @conn.exec <<~SQL\n SELECT\n routine_schema,\n routine_name,\n routine_definition,\n external_language,\n pg_get_function_identity_arguments((routine_schema || '.' || routine_name)::regproc) as arguments,\n pg_get_functiondef((routine_schema || '.' || routine_name)::regproc) as function_definition,\n pg_get_function_result((routine_schema || '.' || routine_name)::regproc) as function_result,\n obj_description((routine_schema || '.' || routine_name)::regproc::oid, 'pg_proc') as comment\n FROM\n information_schema.routines\n WHERE\n \#{@schema_filter.call :routine_schema}\n AND external_name IS NULL\n ORDER BY\n 1, 2\n SQL\n _recordset.each_with_object(@cache){ |row, h|\n dest = row[\"function_result\"] == \"trigger\" ? :triggers : :functions\n h[:schemas][row[\"routine_schema\"]][dest][row[\"routine_name\"]] = {\n external_language: row[\"external_language\"],\n comment: row[\"comment\"],\n arguments: row[\"arguments\"].split(\",\").map{ |arg| parse_function_argument arg },\n function_definition: row[\"function_definition\"],\n function_result: row[\"function_result\"]\n }\n }\n\n # Load foreign keys\n _recordset = @conn.exec <<~SQL\n SELECT\n tc.table_schema,\n tc.table_name,\n tc.constraint_name,\n tc.constraint_type,\n kcu.column_name,\n tc.is_deferrable,\n tc.initially_deferred,\n rc.match_option AS match_type,\n\n rc.update_rule AS on_update,\n rc.delete_rule AS on_delete,\n ccu.table_schema AS references_schema,\n ccu.table_name AS references_table,\n ccu.column_name AS references_field\n\n FROM\n information_schema.table_constraints tc\n\n LEFT JOIN information_schema.key_column_usage kcu\n ON tc.constraint_catalog = kcu.constraint_catalog\n AND tc.constraint_schema = kcu.constraint_schema\n AND tc.constraint_name = kcu.constraint_name\n\n LEFT JOIN information_schema.referential_constraints rc\n ON tc.constraint_catalog = rc.constraint_catalog\n AND tc.constraint_schema = rc.constraint_schema\n AND tc.constraint_name = rc.constraint_name\n\n LEFT JOIN information_schema.constraint_column_usage ccu\n ON rc.unique_constraint_catalog = ccu.constraint_catalog\n AND rc.unique_constraint_schema = ccu.constraint_schema\n AND rc.unique_constraint_name = ccu.constraint_name\n\n WHERE\n tc.constraint_type = 'FOREIGN KEY'\n ORDER BY\n 1, 2\n SQL\n _recordset.each_with_object(@cache){ |row, h|\n h[:schemas][row[\"table_schema\"]][:tables][row[\"table_name\"]][:foreign_keys][row[\"column_name\"]] = row\n }\n\n # Load indexes\n _recordset = @conn.exec <<~SQL\n SELECT\n ns.nspname AS table_schema,\n t.relname AS table_name,\n U.usename AS user_name,\n i.relname AS index_name,\n idx.indisunique AS is_unique,\n idx.indisprimary AS is_primary,\n am.amname AS index_type,\n idx.indkey,\n ARRAY(\n SELECT pg_get_indexdef(idx.indexrelid, k + 1, TRUE)\n FROM generate_subscripts(idx.indkey, 1) AS k\n ORDER BY k\n ) AS index_keys,\n (idx.indexprs IS NOT NULL) OR (idx.indkey::int[] @> array[0]) AS is_functional,\n idx.indpred IS NOT NULL AS is_partial\n FROM\n pg_index AS idx\n JOIN pg_class AS i ON i.oid = idx.indexrelid\n JOIN pg_class AS t ON t.oid = idx.indrelid\n JOIN pg_am AS am ON i.relam = am.oid\n JOIN pg_namespace AS ns ON i.relnamespace = ns.oid\n JOIN pg_user AS U ON i.relowner = U.usesysid\n WHERE\n \#{@schema_filter.call :\"ns.nspname\"}\n ORDER BY\n 1, 2, idx.indisprimary DESC, i.relname\n SQL\n _recordset.each_with_object(@cache){ |row, h|\n h[:schemas][row[\"table_schema\"]][:tables][row[\"table_name\"]][:indexes][row[\"index_name\"]] = row\n }\n\nend\n" |