Code Duplication    Length = 11-39 lines in 17 locations

src/database/Postgres.php 13 locations

@@ 1321-1342 (lines=22) @@
1318
     *
1319
     * @return A recordset
1320
     */
1321
    public function getTableParents($table)
1322
    {
1323
        $c_schema = $this->_schema;
1324
        $this->clean($c_schema);
1325
        $this->clean($table);
1326
1327
        $sql = "
1328
			SELECT
1329
				pn.nspname, relname
1330
			FROM
1331
				pg_catalog.pg_class pc, pg_catalog.pg_inherits pi, pg_catalog.pg_namespace pn
1332
			WHERE
1333
				pc.oid=pi.inhparent
1334
				AND pc.relnamespace=pn.oid
1335
				AND pi.inhrelid = (SELECT oid from pg_catalog.pg_class WHERE relname='{$table}'
1336
					AND relnamespace = (SELECT oid FROM pg_catalog.pg_namespace WHERE nspname = '{$c_schema}'))
1337
			ORDER BY
1338
				pi.inhseqno
1339
		";
1340
1341
        return $this->selectSet($sql);
1342
    }
1343
1344
    /**
1345
     * Finds the names and schemas of child tables.
@@ 1351-1370 (lines=20) @@
1348
     *
1349
     * @return A recordset
1350
     */
1351
    public function getTableChildren($table)
1352
    {
1353
        $c_schema = $this->_schema;
1354
        $this->clean($c_schema);
1355
        $this->clean($table);
1356
1357
        $sql = "
1358
			SELECT
1359
				pn.nspname, relname
1360
			FROM
1361
				pg_catalog.pg_class pc, pg_catalog.pg_inherits pi, pg_catalog.pg_namespace pn
1362
			WHERE
1363
				pc.oid=pi.inhrelid
1364
				AND pc.relnamespace=pn.oid
1365
				AND pi.inhparent = (SELECT oid from pg_catalog.pg_class WHERE relname='{$table}'
1366
					AND relnamespace = (SELECT oid FROM pg_catalog.pg_namespace WHERE nspname = '{$c_schema}'))
1367
		";
1368
1369
        return $this->selectSet($sql);
1370
    }
1371
1372
    /**
1373
     * Returns the SQL definition for the table.
@@ 1710-1730 (lines=21) @@
1707
     *
1708
     * @return A recordset
1709
     */
1710
    public function getTable($table)
1711
    {
1712
        $c_schema = $this->_schema;
1713
        $this->clean($c_schema);
1714
        $this->clean($table);
1715
1716
        $sql = "
1717
			SELECT
1718
			  c.relname, n.nspname, u.usename AS relowner,
1719
			  pg_catalog.obj_description(c.oid, 'pg_class') AS relcomment,
1720
			  (SELECT spcname FROM pg_catalog.pg_tablespace pt WHERE pt.oid=c.reltablespace) AS tablespace
1721
			FROM pg_catalog.pg_class c
1722
			     LEFT JOIN pg_catalog.pg_user u ON u.usesysid = c.relowner
1723
			     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
1724
			WHERE c.relkind = 'r'
1725
			      AND n.nspname = '{$c_schema}'
1726
			      AND n.oid = c.relnamespace
1727
			      AND c.relname = '{$table}'";
1728
1729
        return $this->selectSet($sql);
1730
    }
1731
1732
    /**
1733
     * Retrieve the attribute definition of a table.
@@ 1812-1850 (lines=39) @@
1809
     *
1810
     * @return A recordset
1811
     */
1812
    public function getConstraints($table)
1813
    {
1814
        $c_schema = $this->_schema;
1815
        $this->clean($c_schema);
1816
        $this->clean($table);
1817
1818
        // This SQL is greatly complicated by the need to retrieve
1819
        // index clustering information for primary and unique constraints
1820
        $sql = "SELECT
1821
				pc.conname,
1822
				pg_catalog.pg_get_constraintdef(pc.oid, true) AS consrc,
1823
				pc.contype,
1824
				CASE WHEN pc.contype='u' OR pc.contype='p' THEN (
1825
					SELECT
1826
						indisclustered
1827
					FROM
1828
						pg_catalog.pg_depend pd,
1829
						pg_catalog.pg_class pl,
1830
						pg_catalog.pg_index pi
1831
					WHERE
1832
						pd.refclassid=pc.tableoid
1833
						AND pd.refobjid=pc.oid
1834
						AND pd.objid=pl.oid
1835
						AND pl.oid=pi.indexrelid
1836
				) ELSE
1837
					NULL
1838
				END AS indisclustered
1839
			FROM
1840
				pg_catalog.pg_constraint pc
1841
			WHERE
1842
				pc.conrelid = (SELECT oid FROM pg_catalog.pg_class WHERE relname='{$table}'
1843
					AND relnamespace = (SELECT oid FROM pg_catalog.pg_namespace
1844
					WHERE nspname='{$c_schema}'))
1845
			ORDER BY
1846
				1
1847
		";
1848
1849
        return $this->selectSet($sql);
1850
    }
1851
1852
    /**
1853
     * Returns the SQL for changing the current user.
@@ 2343-2365 (lines=23) @@
2340
     *
2341
     * @return \PHPPgAdmin\Database\A recordset
2342
     */
2343
    public function getTriggers($table = '')
2344
    {
2345
        $c_schema = $this->_schema;
2346
        $this->clean($c_schema);
2347
        $this->clean($table);
2348
2349
        $sql = "SELECT
2350
				t.tgname, pg_catalog.pg_get_triggerdef(t.oid) AS tgdef,
2351
				CASE WHEN t.tgenabled = 'D' THEN FALSE ELSE TRUE END AS tgenabled, p.oid AS prooid,
2352
				p.proname || ' (' || pg_catalog.oidvectortypes(p.proargtypes) || ')' AS proproto,
2353
				ns.nspname AS pronamespace
2354
			FROM pg_catalog.pg_trigger t, pg_catalog.pg_proc p, pg_catalog.pg_namespace ns
2355
			WHERE t.tgrelid = (SELECT oid FROM pg_catalog.pg_class WHERE relname='{$table}'
2356
				AND relnamespace=(SELECT oid FROM pg_catalog.pg_namespace WHERE nspname='{$c_schema}'))
2357
				AND ( tgconstraint = 0 OR NOT EXISTS
2358
						(SELECT 1 FROM pg_catalog.pg_depend d    JOIN pg_catalog.pg_constraint c
2359
							ON (d.refclassid = c.tableoid AND d.refobjid = c.oid)
2360
						WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))
2361
				AND p.oid=t.tgfoid
2362
				AND p.pronamespace = ns.oid";
2363
2364
        return $this->selectSet($sql);
2365
    }
2366
2367
    /**
2368
     * Returns a list of all rules on a table OR view.
@@ 2374-2389 (lines=16) @@
2371
     *
2372
     * @return A recordset
2373
     */
2374
    public function getRules($table)
2375
    {
2376
        $c_schema = $this->_schema;
2377
        $this->clean($c_schema);
2378
        $this->clean($table);
2379
2380
        $sql = "
2381
			SELECT *
2382
			FROM pg_catalog.pg_rules
2383
			WHERE
2384
				schemaname='{$c_schema}' AND tablename='{$table}'
2385
			ORDER BY rulename
2386
		";
2387
2388
        return $this->selectSet($sql);
2389
    }
2390
2391
    /**
2392
     * Creates a new table in the database.
@@ 4477-4495 (lines=19) @@
4474
     *
4475
     * @return true if the table has been already clustered
4476
     */
4477
    public function alreadyClustered($table)
4478
    {
4479
        $c_schema = $this->_schema;
4480
        $this->clean($c_schema);
4481
        $this->clean($table);
4482
4483
        $sql = "SELECT i.indisclustered
4484
			FROM pg_catalog.pg_class c, pg_catalog.pg_index i
4485
			WHERE c.relname = '{$table}'
4486
				AND c.oid = i.indrelid AND i.indisclustered
4487
				AND c.relnamespace = (SELECT oid FROM pg_catalog.pg_namespace
4488
					WHERE nspname='{$c_schema}')
4489
				";
4490
4491
        $v = $this->selectSet($sql);
4492
4493
        return !($v->recordCount() == 0);
4494
    }
4495
4496
    /**
4497
     * Creates an index.
4498
     *
@@ 5072-5105 (lines=34) @@
5069
     *
5070
     * @return A recordset
5071
     */
5072
    public function getReferrers($table)
5073
    {
5074
        $this->clean($table);
5075
5076
        $status = $this->beginTransaction();
5077
        if ($status != 0) {
5078
            return -1;
5079
        }
5080
5081
        $c_schema = $this->_schema;
5082
        $this->clean($c_schema);
5083
5084
        $sql = "
5085
			SELECT
5086
				pn.nspname,
5087
				pl.relname,
5088
				pc.conname,
5089
				pg_catalog.pg_get_constraintdef(pc.oid) AS consrc
5090
			FROM
5091
				pg_catalog.pg_constraint pc,
5092
				pg_catalog.pg_namespace pn,
5093
				pg_catalog.pg_class pl
5094
			WHERE
5095
				pc.connamespace = pn.oid
5096
				AND pc.conrelid = pl.oid
5097
				AND pc.contype = 'f'
5098
				AND confrelid = (SELECT oid FROM pg_catalog.pg_class WHERE relname='{$table}'
5099
					AND relnamespace = (SELECT oid FROM pg_catalog.pg_namespace
5100
					WHERE nspname='{$c_schema}'))
5101
			ORDER BY 1,2,3
5102
		";
5103
5104
        return $this->selectSet($sql);
5105
    }
5106
5107
    // Type functions
5108
@@ 6266-6281 (lines=16) @@
6263
     *
6264
     * @return A recordset
6265
     */
6266
    public function getTrigger($table, $trigger)
6267
    {
6268
        $c_schema = $this->_schema;
6269
        $this->clean($c_schema);
6270
        $this->clean($table);
6271
        $this->clean($trigger);
6272
6273
        $sql = "
6274
			SELECT * FROM pg_catalog.pg_trigger t, pg_catalog.pg_class c
6275
			WHERE t.tgrelid=c.oid AND c.relname='{$table}' AND t.tgname='{$trigger}'
6276
				AND c.relnamespace=(
6277
					SELECT oid FROM pg_catalog.pg_namespace
6278
					WHERE nspname='{$c_schema}')";
6279
6280
        return $this->selectSet($sql);
6281
    }
6282
6283
    /**
6284
     * A helper function for getTriggers that translates
@@ 9569-9579 (lines=11) @@
9566
     *
9567
     * @return A recordset
9568
     */
9569
    public function getStatsTableTuples($table)
9570
    {
9571
        $c_schema = $this->_schema;
9572
        $this->clean($c_schema);
9573
        $this->clean($table);
9574
9575
        $sql = "SELECT * FROM pg_stat_all_tables
9576
			WHERE schemaname='{$c_schema}' AND relname='{$table}'";
9577
9578
        return $this->selectSet($sql);
9579
    }
9580
9581
    /**
9582
     * Fetches I/0 statistics for a table.
@@ 9588-9598 (lines=11) @@
9585
     *
9586
     * @return A recordset
9587
     */
9588
    public function getStatsTableIO($table)
9589
    {
9590
        $c_schema = $this->_schema;
9591
        $this->clean($c_schema);
9592
        $this->clean($table);
9593
9594
        $sql = "SELECT * FROM pg_statio_all_tables
9595
			WHERE schemaname='{$c_schema}' AND relname='{$table}'";
9596
9597
        return $this->selectSet($sql);
9598
    }
9599
9600
    /**
9601
     * Fetches tuple statistics for all indexes on a table.
@@ 9607-9617 (lines=11) @@
9604
     *
9605
     * @return A recordset
9606
     */
9607
    public function getStatsIndexTuples($table)
9608
    {
9609
        $c_schema = $this->_schema;
9610
        $this->clean($c_schema);
9611
        $this->clean($table);
9612
9613
        $sql = "SELECT * FROM pg_stat_all_indexes
9614
			WHERE schemaname='{$c_schema}' AND relname='{$table}' ORDER BY indexrelname";
9615
9616
        return $this->selectSet($sql);
9617
    }
9618
9619
    /**
9620
     * Fetches I/0 statistics for all indexes on a table.
@@ 9626-9637 (lines=12) @@
9623
     *
9624
     * @return A recordset
9625
     */
9626
    public function getStatsIndexIO($table)
9627
    {
9628
        $c_schema = $this->_schema;
9629
        $this->clean($c_schema);
9630
        $this->clean($table);
9631
9632
        $sql = "SELECT * FROM pg_statio_all_indexes
9633
			WHERE schemaname='{$c_schema}' AND relname='{$table}'
9634
			ORDER BY indexrelname";
9635
9636
        return $this->selectSet($sql);
9637
    }
9638
9639
    public function hasAggregateSortOp()
9640
    {

src/database/Postgres74.php 1 location

@@ 377-395 (lines=19) @@
374
         *
375
         * @return A recordset
376
         */
377
        public function getTable($table)
378
        {
379
            $c_schema = $this->_schema;
380
            $this->clean($c_schema);
381
            $this->clean($table);
382
383
            $sql = "
384
			SELECT
385
			  c.relname, n.nspname, u.usename AS relowner,
386
			  pg_catalog.obj_description(c.oid, 'pg_class') AS relcomment
387
			FROM pg_catalog.pg_class c
388
			     LEFT JOIN pg_catalog.pg_user u ON u.usesysid = c.relowner
389
			     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
390
			WHERE c.relkind = 'r'
391
				AND n.nspname = '{$c_schema}'
392
			    AND c.relname = '{$table}'";
393
394
            return $this->selectSet($sql);
395
        }
396
397
        /**
398
         * Return all tables in current database (and schema).

src/database/Postgres80.php 1 location

@@ 174-198 (lines=25) @@
171
         *
172
         * @return A recordset
173
         */
174
        public function getAggregate($name, $basetype)
175
        {
176
            $c_schema = $this->_schema;
177
            $this->clean($c_schema);
178
            $this->clean($name);
179
            $this->clean($basetype);
180
181
            $sql = "
182
			SELECT p.proname,
183
				CASE p.proargtypes[0]
184
					WHEN 'pg_catalog.\"any\"'::pg_catalog.regtype THEN NULL
185
					ELSE pg_catalog.format_type(p.proargtypes[0], NULL)
186
				END AS proargtypes, a.aggtransfn, format_type(a.aggtranstype, NULL) AS aggstype,
187
				a.aggfinalfn, a.agginitval, u.usename, pg_catalog.obj_description(p.oid, 'pg_proc') AS aggrcomment
188
			FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n, pg_catalog.pg_user u, pg_catalog.pg_aggregate a
189
			WHERE n.oid = p.pronamespace AND p.proowner=u.usesysid AND p.oid=a.aggfnoid
190
				AND p.proisagg AND n.nspname='{$c_schema}'
191
				AND p.proname='{$name}'
192
				AND CASE p.proargtypes[0]
193
					WHEN 'pg_catalog.\"any\"'::pg_catalog.regtype THEN ''
194
					ELSE pg_catalog.format_type(p.proargtypes[0], NULL)
195
				END ='{$basetype}'";
196
197
            return $this->selectSet($sql);
198
        }
199
200
        // Sequence functions
201

src/database/Postgres82.php 1 location

@@ 133-154 (lines=22) @@
130
         *
131
         * @return \PHPPgAdmin\Database\A recordset
132
         */
133
        public function getTriggers($table = '')
134
        {
135
            $c_schema = $this->_schema;
136
            $this->clean($c_schema);
137
            $this->clean($table);
138
139
            $sql = "SELECT
140
				t.tgname, pg_catalog.pg_get_triggerdef(t.oid) AS tgdef, t.tgenabled, p.oid AS prooid,
141
				p.proname || ' (' || pg_catalog.oidvectortypes(p.proargtypes) || ')' AS proproto,
142
				ns.nspname AS pronamespace
143
			FROM pg_catalog.pg_trigger t, pg_catalog.pg_proc p, pg_catalog.pg_namespace ns
144
			WHERE t.tgrelid = (SELECT oid FROM pg_catalog.pg_class WHERE relname='{$table}'
145
				AND relnamespace=(SELECT oid FROM pg_catalog.pg_namespace WHERE nspname='{$c_schema}'))
146
				AND (NOT tgisconstraint OR NOT EXISTS
147
						(SELECT 1 FROM pg_catalog.pg_depend d    JOIN pg_catalog.pg_constraint c
148
							ON (d.refclassid = c.tableoid AND d.refobjid = c.oid)
149
						WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))
150
				AND p.oid=t.tgfoid
151
				AND p.pronamespace = ns.oid";
152
153
            return $this->selectSet($sql);
154
        }
155
156
        // Function functions
157

src/database/Postgres84.php 1 location

@@ 37-59 (lines=23) @@
34
         *
35
         * @return \PHPPgAdmin\Database\A recordset
36
         */
37
        public function getTriggers($table = '')
38
        {
39
            $c_schema = $this->_schema;
40
            $this->clean($c_schema);
41
            $this->clean($table);
42
43
            $sql = "SELECT
44
				t.tgname, pg_catalog.pg_get_triggerdef(t.oid) AS tgdef,
45
				CASE WHEN t.tgenabled = 'D' THEN FALSE ELSE TRUE END AS tgenabled, p.oid AS prooid,
46
				p.proname || ' (' || pg_catalog.oidvectortypes(p.proargtypes) || ')' AS proproto,
47
				ns.nspname AS pronamespace
48
			FROM pg_catalog.pg_trigger t, pg_catalog.pg_proc p, pg_catalog.pg_namespace ns
49
			WHERE t.tgrelid = (SELECT oid FROM pg_catalog.pg_class WHERE relname='{$table}'
50
				AND relnamespace=(SELECT oid FROM pg_catalog.pg_namespace WHERE nspname='{$c_schema}'))
51
				AND (NOT tgisconstraint OR NOT EXISTS
52
						(SELECT 1 FROM pg_catalog.pg_depend d    JOIN pg_catalog.pg_constraint c
53
							ON (d.refclassid = c.tableoid AND d.refobjid = c.oid)
54
						WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))
55
				AND p.oid=t.tgfoid
56
				AND p.pronamespace = ns.oid";
57
58
            return $this->selectSet($sql);
59
        }
60
61
        /**
62
         * Searches all system catalogs to find objects that match a certain name.