Skip to content
This repository
Browse code

fix trigger list and search for 9.0, push older functions to 8.4 class

  • Loading branch information...
commit 3c9bc7de5cd1c96989e54e42b9e1f2b2e7d54d23 1 parent a96b627
authored April 24, 2010
4  classes/database/Postgres.php
@@ -764,7 +764,7 @@ function findObject($term, $filter) {
764 764
 			UNION ALL
765 765
 			SELECT 'TRIGGER', NULL, pn.nspname, pc.relname, pt.tgname FROM pg_catalog.pg_class pc, pg_catalog.pg_namespace pn,
766 766
 				pg_catalog.pg_trigger pt WHERE pc.relnamespace=pn.oid AND pc.oid=pt.tgrelid
767  
-					AND (NOT pt.tgisconstraint OR NOT EXISTS
  767
+					AND ( pt.tgconstraint = 0 OR NOT EXISTS
768 768
 					(SELECT 1 FROM pg_catalog.pg_depend d JOIN pg_catalog.pg_constraint c
769 769
 					ON (d.refclassid = c.tableoid AND d.refobjid = c.oid)
770 770
 					WHERE d.classid = pt.tableoid AND d.objid = pt.oid AND d.deptype = 'i' AND c.contype = 'f'))
@@ -4835,7 +4835,7 @@ function getTriggers($table = '') {
4835 4835
 			FROM pg_catalog.pg_trigger t, pg_catalog.pg_proc p, pg_catalog.pg_namespace ns
4836 4836
 			WHERE t.tgrelid = (SELECT oid FROM pg_catalog.pg_class WHERE relname='{$table}'
4837 4837
 				AND relnamespace=(SELECT oid FROM pg_catalog.pg_namespace WHERE nspname='{$c_schema}'))
4838  
-				AND (NOT tgisconstraint OR NOT EXISTS
  4838
+				AND ( tgconstraint = 0 OR NOT EXISTS
4839 4839
 						(SELECT 1 FROM pg_catalog.pg_depend d    JOIN pg_catalog.pg_constraint c
4840 4840
 							ON (d.refclassid = c.tableoid AND d.refobjid = c.oid)
4841 4841
 						WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))
169  classes/database/Postgres84.php
@@ -43,6 +43,175 @@ function getHelpPages() {
43 43
 
44 44
 	// Databse functions
45 45
 
  46
+	/**
  47
+	 * Grabs a list of triggers on a table
  48
+	 * @param $table The name of a table whose triggers to retrieve
  49
+	 * @return A recordset
  50
+	 */
  51
+	function getTriggers($table = '') {
  52
+		$c_schema = $this->_schema;
  53
+		$this->clean($c_schema);
  54
+		$this->clean($table);
  55
+
  56
+		$sql = "SELECT
  57
+				t.tgname, pg_catalog.pg_get_triggerdef(t.oid) AS tgdef,
  58
+				CASE WHEN t.tgenabled = 'D' THEN FALSE ELSE TRUE END AS tgenabled, p.oid AS prooid,
  59
+				p.proname || ' (' || pg_catalog.oidvectortypes(p.proargtypes) || ')' AS proproto,
  60
+				ns.nspname AS pronamespace
  61
+			FROM pg_catalog.pg_trigger t, pg_catalog.pg_proc p, pg_catalog.pg_namespace ns
  62
+			WHERE t.tgrelid = (SELECT oid FROM pg_catalog.pg_class WHERE relname='{$table}'
  63
+				AND relnamespace=(SELECT oid FROM pg_catalog.pg_namespace WHERE nspname='{$c_schema}'))
  64
+				AND (NOT tgisconstraint OR NOT EXISTS
  65
+						(SELECT 1 FROM pg_catalog.pg_depend d    JOIN pg_catalog.pg_constraint c
  66
+							ON (d.refclassid = c.tableoid AND d.refobjid = c.oid)
  67
+						WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))
  68
+				AND p.oid=t.tgfoid
  69
+				AND p.pronamespace = ns.oid";
  70
+
  71
+		return $this->selectSet($sql);
  72
+	}
  73
+
  74
+
  75
+	/**
  76
+	 * Searches all system catalogs to find objects that match a certain name.
  77
+	 * @param $term The search term
  78
+	 * @param $filter The object type to restrict to ('' means no restriction)
  79
+	 * @return A recordset
  80
+	 */
  81
+	function findObject($term, $filter) {
  82
+		global $conf;
  83
+
  84
+		// Escape search term for ILIKE match
  85
+		$term = str_replace('_', '\\_', $term);
  86
+		$term = str_replace('%', '\\%', $term);
  87
+		$this->clean($term);
  88
+		$this->clean($filter);
  89
+
  90
+		// Exclude system relations if necessary
  91
+		if (!$conf['show_system']) {
  92
+			// XXX: The mention of information_schema here is in the wrong place, but
  93
+			// it's the quickest fix to exclude the info schema from 7.4
  94
+			$where = " AND pn.nspname NOT LIKE 'pg\\\\_%' AND pn.nspname != 'information_schema'";
  95
+			$lan_where = "AND pl.lanispl";
  96
+		}
  97
+		else {
  98
+			$where = '';
  99
+			$lan_where = '';
  100
+	}
  101
+
  102
+		// Apply outer filter
  103
+		$sql = '';
  104
+		if ($filter != '') {
  105
+			$sql = "SELECT * FROM (";
  106
+		}
  107
+
  108
+		$sql .= "
  109
+			SELECT 'SCHEMA' AS type, oid, NULL AS schemaname, NULL AS relname, nspname AS name
  110
+				FROM pg_catalog.pg_namespace pn WHERE nspname ILIKE '%{$term}%' {$where}
  111
+			UNION ALL
  112
+			SELECT CASE WHEN relkind='r' THEN 'TABLE' WHEN relkind='v' THEN 'VIEW' WHEN relkind='S' THEN 'SEQUENCE' END, pc.oid,
  113
+				pn.nspname, NULL, pc.relname FROM pg_catalog.pg_class pc, pg_catalog.pg_namespace pn
  114
+				WHERE pc.relnamespace=pn.oid AND relkind IN ('r', 'v', 'S') AND relname ILIKE '%{$term}%' {$where}
  115
+			UNION ALL
  116
+			SELECT CASE WHEN pc.relkind='r' THEN 'COLUMNTABLE' ELSE 'COLUMNVIEW' END, NULL, pn.nspname, pc.relname, pa.attname FROM pg_catalog.pg_class pc, pg_catalog.pg_namespace pn,
  117
+				pg_catalog.pg_attribute pa WHERE pc.relnamespace=pn.oid AND pc.oid=pa.attrelid
  118
+				AND pa.attname ILIKE '%{$term}%' AND pa.attnum > 0 AND NOT pa.attisdropped AND pc.relkind IN ('r', 'v') {$where}
  119
+			UNION ALL
  120
+			SELECT 'FUNCTION', pp.oid, pn.nspname, NULL, pp.proname || '(' || pg_catalog.oidvectortypes(pp.proargtypes) || ')' FROM pg_catalog.pg_proc pp, pg_catalog.pg_namespace pn
  121
+				WHERE pp.pronamespace=pn.oid AND NOT pp.proisagg AND pp.proname ILIKE '%{$term}%' {$where}
  122
+			UNION ALL
  123
+			SELECT 'INDEX', NULL, pn.nspname, pc.relname, pc2.relname FROM pg_catalog.pg_class pc, pg_catalog.pg_namespace pn,
  124
+				pg_catalog.pg_index pi, pg_catalog.pg_class pc2 WHERE pc.relnamespace=pn.oid AND pc.oid=pi.indrelid
  125
+				AND pi.indexrelid=pc2.oid
  126
+				AND NOT EXISTS (
  127
+					SELECT 1 FROM pg_catalog.pg_depend d JOIN pg_catalog.pg_constraint c
  128
+					ON (d.refclassid = c.tableoid AND d.refobjid = c.oid)
  129
+					WHERE d.classid = pc2.tableoid AND d.objid = pc2.oid AND d.deptype = 'i' AND c.contype IN ('u', 'p')
  130
+				)
  131
+				AND pc2.relname ILIKE '%{$term}%' {$where}
  132
+			UNION ALL
  133
+			SELECT 'CONSTRAINTTABLE', NULL, pn.nspname, pc.relname, pc2.conname FROM pg_catalog.pg_class pc, pg_catalog.pg_namespace pn,
  134
+				pg_catalog.pg_constraint pc2 WHERE pc.relnamespace=pn.oid AND pc.oid=pc2.conrelid AND pc2.conrelid != 0
  135
+				AND CASE WHEN pc2.contype IN ('f', 'c') THEN TRUE ELSE NOT EXISTS (
  136
+					SELECT 1 FROM pg_catalog.pg_depend d JOIN pg_catalog.pg_constraint c
  137
+					ON (d.refclassid = c.tableoid AND d.refobjid = c.oid)
  138
+					WHERE d.classid = pc2.tableoid AND d.objid = pc2.oid AND d.deptype = 'i' AND c.contype IN ('u', 'p')
  139
+				) END
  140
+				AND pc2.conname ILIKE '%{$term}%' {$where}
  141
+			UNION ALL
  142
+			SELECT 'CONSTRAINTDOMAIN', pt.oid, pn.nspname, pt.typname, pc.conname FROM pg_catalog.pg_type pt, pg_catalog.pg_namespace pn,
  143
+				pg_catalog.pg_constraint pc WHERE pt.typnamespace=pn.oid AND pt.oid=pc.contypid AND pc.contypid != 0
  144
+				AND pc.conname ILIKE '%{$term}%' {$where}
  145
+			UNION ALL
  146
+			SELECT 'TRIGGER', NULL, pn.nspname, pc.relname, pt.tgname FROM pg_catalog.pg_class pc, pg_catalog.pg_namespace pn,
  147
+				pg_catalog.pg_trigger pt WHERE pc.relnamespace=pn.oid AND pc.oid=pt.tgrelid
  148
+					AND (NOT pt.tgisconstraint OR NOT EXISTS
  149
+					(SELECT 1 FROM pg_catalog.pg_depend d JOIN pg_catalog.pg_constraint c
  150
+					ON (d.refclassid = c.tableoid AND d.refobjid = c.oid)
  151
+					WHERE d.classid = pt.tableoid AND d.objid = pt.oid AND d.deptype = 'i' AND c.contype = 'f'))
  152
+				AND pt.tgname ILIKE '%{$term}%' {$where}
  153
+			UNION ALL
  154
+			SELECT 'RULETABLE', NULL, pn.nspname AS schemaname, c.relname AS tablename, r.rulename FROM pg_catalog.pg_rewrite r
  155
+				JOIN pg_catalog.pg_class c ON c.oid = r.ev_class
  156
+				LEFT JOIN pg_catalog.pg_namespace pn ON pn.oid = c.relnamespace
  157
+				WHERE c.relkind='r' AND r.rulename != '_RETURN' AND r.rulename ILIKE '%{$term}%' {$where}
  158
+			UNION ALL
  159
+			SELECT 'RULEVIEW', NULL, pn.nspname AS schemaname, c.relname AS tablename, r.rulename FROM pg_catalog.pg_rewrite r
  160
+				JOIN pg_catalog.pg_class c ON c.oid = r.ev_class
  161
+				LEFT JOIN pg_catalog.pg_namespace pn ON pn.oid = c.relnamespace
  162
+				WHERE c.relkind='v' AND r.rulename != '_RETURN' AND r.rulename ILIKE '%{$term}%' {$where}
  163
+		";
  164
+
  165
+		// Add advanced objects if show_advanced is set
  166
+		if ($conf['show_advanced']) {
  167
+			$sql .= "
  168
+				UNION ALL
  169
+				SELECT CASE WHEN pt.typtype='d' THEN 'DOMAIN' ELSE 'TYPE' END, pt.oid, pn.nspname, NULL,
  170
+					pt.typname FROM pg_catalog.pg_type pt, pg_catalog.pg_namespace pn
  171
+					WHERE pt.typnamespace=pn.oid AND typname ILIKE '%{$term}%'
  172
+					AND (pt.typrelid = 0 OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c WHERE c.oid = pt.typrelid))
  173
+					{$where}
  174
+			 	UNION ALL
  175
+				SELECT 'OPERATOR', po.oid, pn.nspname, NULL, po.oprname FROM pg_catalog.pg_operator po, pg_catalog.pg_namespace pn
  176
+					WHERE po.oprnamespace=pn.oid AND oprname ILIKE '%{$term}%' {$where}
  177
+				UNION ALL
  178
+				SELECT 'CONVERSION', pc.oid, pn.nspname, NULL, pc.conname FROM pg_catalog.pg_conversion pc,
  179
+					pg_catalog.pg_namespace pn WHERE pc.connamespace=pn.oid AND conname ILIKE '%{$term}%' {$where}
  180
+				UNION ALL
  181
+				SELECT 'LANGUAGE', pl.oid, NULL, NULL, pl.lanname FROM pg_catalog.pg_language pl
  182
+					WHERE lanname ILIKE '%{$term}%' {$lan_where}
  183
+				UNION ALL
  184
+				SELECT DISTINCT ON (p.proname) 'AGGREGATE', p.oid, pn.nspname, NULL, p.proname FROM pg_catalog.pg_proc p
  185
+					LEFT JOIN pg_catalog.pg_namespace pn ON p.pronamespace=pn.oid
  186
+					WHERE p.proisagg AND p.proname ILIKE '%{$term}%' {$where}
  187
+				UNION ALL
  188
+				SELECT DISTINCT ON (po.opcname) 'OPCLASS', po.oid, pn.nspname, NULL, po.opcname FROM pg_catalog.pg_opclass po,
  189
+					pg_catalog.pg_namespace pn WHERE po.opcnamespace=pn.oid
  190
+					AND po.opcname ILIKE '%{$term}%' {$where}
  191
+			";
  192
+		}
  193
+		// Otherwise just add domains
  194
+		else {
  195
+			$sql .= "
  196
+				UNION ALL
  197
+				SELECT 'DOMAIN', pt.oid, pn.nspname, NULL,
  198
+					pt.typname FROM pg_catalog.pg_type pt, pg_catalog.pg_namespace pn
  199
+					WHERE pt.typnamespace=pn.oid AND pt.typtype='d' AND typname ILIKE '%{$term}%'
  200
+					AND (pt.typrelid = 0 OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c WHERE c.oid = pt.typrelid))
  201
+					{$where}
  202
+			";
  203
+		}
  204
+
  205
+		if ($filter != '') {
  206
+			// We use like to make RULE, CONSTRAINT and COLUMN searches work
  207
+			$sql .= ") AS sub WHERE type LIKE '{$filter}%' ";
  208
+		}
  209
+
  210
+		$sql .= "ORDER BY type, schemaname, relname, name";
  211
+
  212
+		return $this->selectSet($sql);
  213
+	}
  214
+
46 215
 
47 216
 	// Capabilities
48 217
 

0 notes on commit 3c9bc7d

Please sign in to comment.
Something went wrong with that request. Please try again.