Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.Dcp_Core_PgInformationSchema.php
Go to the documentation of this file.
1 <?php
2 /**
3  * Helper methods to query Postgresql's information_schema
4  */
5 
6 namespace Dcp\Core;
7 
9 {
10  /**
11  * Check if a table exists.
12  *
13  * @param string $dbaccess dbaccess
14  * @param string $schemaName Schema name (e.g. 'public')
15  * @param string $tableName Table name (e.g. 'doc123')
16  * @return bool bool(true) if table exists in given schema or bool(false) if table does not exists
17  */
18  public static function tableExists($dbaccess, $schemaName, $tableName)
19  {
20  $tpl_sql =
21  /** @lang text */
22  <<<'EOF'
23 SELECT
24  true
25 FROM
26  information_schema.tables
27 WHERE
28  table_schema = %s
29  AND table_name = %s
30 LIMIT 1
31 EOF;
32  $q = sprintf($tpl_sql, pg_escape_literal($schemaName) , pg_escape_literal($tableName));
33  simpleQuery($dbaccess, $q, $res, true, true, true);
34  return ($res === 't');
35  }
36  /**
37  * Get columns for a given table.
38  *
39  * @param string $dbaccess dbaccess
40  * @param string $schemaName Schema name (e.g. 'public')
41  * @param string $tableName Table name (e.g. 'doc123')
42  * @return string[] List of column names for given table
43  */
44  public static function getTableColumns($dbaccess, $schemaName, $tableName)
45  {
46  $tpl_sql =
47  /** @lang text */
48  <<<'EOF'
49 SELECT
50  column_name
51 FROM
52  information_schema.columns
53 WHERE
54  table_schema = %s
55  AND table_name = %s
56 EOF;
57  $q = sprintf($tpl_sql, pg_escape_literal($schemaName) , pg_escape_literal($tableName));
58  simpleQuery($dbaccess, $q, $res, true, false, true);
59  return $res;
60  }
61  /**
62  * Get indexes for a given table.
63  *
64  * @param string $dbaccess dbaccess
65  * @param string $schemaName Schema name (e.g. 'public')
66  * @param string $tableName Table name (e.g. 'doc123')
67  * @return string[] List of index names for given table
68  */
69  public static function getTableIndexes($dbaccess, $schemaName, $tableName)
70  {
71  $tpl_sql =
72  /** @lang text */
73  <<<'EOF'
74 SELECT
75  ci.relname AS index_name
76 FROM
77  pg_class AS ct,
78  pg_class AS ci,
79  pg_namespace AS ns,
80  pg_index AS ix
81 WHERE
82  ns.nspname = %s
83  AND ct.relname = %s
84  AND ct.relkind = 'r'
85  AND ct.relnamespace = ns.oid
86  AND ci.relnamespace = ns.oid
87  AND ct.oid = ix.indrelid
88  AND ci.oid = ix.indexrelid
89 EOF;
90  $q = sprintf($tpl_sql, pg_escape_literal($schemaName) , pg_escape_literal($tableName));
91  simpleQuery($dbaccess, $q, $res, true, false, true);
92  return $res;
93  }
94 }
static getTableIndexes($dbaccess, $schemaName, $tableName)
static tableExists($dbaccess, $schemaName, $tableName)
static getTableColumns($dbaccess, $schemaName, $tableName)
$dbaccess
Definition: checkVault.php:17
simpleQuery($dbaccess, $query, &$result=array(), $singlecolumn=false, $singleresult=false, $useStrict=null)
Definition: Lib.Common.php:484
← centre documentaire © anakeen