Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
DALPostgresConverter.inc
1 <?php
13 require_once dirname(__FILE__).'/DALConverter.inc';
14 
26 {
27 
34  protected $name = 'Postgres';
35 
36 
42  public function __construct()
43  {
44 
45  }//end __construct()
46 
47 
58  public function convertCreateTable(array $table)
59  {
60  $sql = $this->convertCreateHeader($table);
61  $sql .= "\n".$this->convertCreateColumns($table).',';
62  $sql .= "\n".$this->convertConstraints($table['CONSTRAINTS']);
63  $sql .= $this->convertCreateFooter($table);
64 
65  $indexes = $this->convertCreateIndexes($table);
66  if ($indexes !== '') {
67  $sql .= "\n".$indexes;
68  }
69 
70  if (isset($table['SEQUENCES']) === TRUE) {
71  $sql .= "\n".$this->convertCreateSequences($table['SEQUENCES']);
72  }
73 
74  return $sql;
75 
76  }//end convertCreateTable()
77 
78 
87  protected function convertCreateIndexes(array $table)
88  {
89  $sql = '';
90  $indexes = array();
91  foreach ($table['INDEXES'] as $index) {
92  $indexes[] = $this->convertSingleIndex($index, $table['table']);
93  }
94 
95  $sql .= implode(";\n", $indexes).';';
96 
97  return $sql;
98 
99  }//end convertCreateIndexes()
100 
101 
113  protected function convertSingleIndex(array $idx, $tableName)
114  {
115  $sql = 'CREATE INDEX '.$idx['name'].' ON '.$tableName.' (';
116  $sql .= $this->separateFields($idx['COLUMNS']).')';
117  return $sql;
118 
119  }//end convertSingleIndex()
120 
121 
133  protected function handleFunctionSeqNextVal(array $seqName)
134  {
135  $sql = '(SELECT nextVal(\''.$seqName[0].'\'))';
136  return $sql;
137 
138  }//end handleFunctionSeqNextVal()
139 
140 
152  protected function handleFunctionSeqCurrVal(array $seqName)
153  {
154  $sql = '(SELECT currVal(\''.$seqName[0].'\'))';
155  return $sql;
156 
157  }//end handleFunctionSeqCurrVal()
158 
159 
168  public function handleFunctionSequenceExists($sequenceName)
169  {
170  if (is_array($sequenceName) === TRUE) {
171  $sequenceName = $sequenceName[0];
172  }
173 
174  $sql = 'SELECT count(c.relname) FROM pg_catalog.pg_class c, pg_catalog.pg_roles r, pg_catalog.pg_namespace n
175  WHERE r.oid = c.relowner AND n.oid = c.relnamespace AND c.relkind = \'S\'
176  AND n.nspname <> \'pg_catalog\'
177  AND pg_catalog.pg_table_is_visible(c.oid)
178  AND c.relname = '.$sequenceName;
179 
180  return $sql;
181 
182  }//end handleFunctionSequenceExists()
183 
184 
193  protected function handleFunctionConcat(array $args)
194  {
195  $sql = implode(' || ', $args);
196  return $sql;
197 
198  }//end handleFunctionConcat()
199 
200 
212  protected function handleFunctionToDate(array $args)
213  {
214  $iso8601_date = $this->convertSingleField($args[0]);
215  $sql = 'TO_TIMESTAMP('.$iso8601_date.', \'YYYY-MM-DD HH24:MI:SS\')';
216  return $sql;
217 
218  }//end handleFunctionToDate()
219 
220 
229  protected function convertCallQuery(array $callQuery)
230  {
231  $sql = 'SELECT ';
232  $sql .= $this->convertSingleFunction($callQuery);
233  return $sql;
234 
235  }//end convertCallQuery()
236 
237 
247  public function handleFunctionTableExists($tableName, $schema=NULL)
248  {
249  if (is_array($tableName) === TRUE) {
250  $tableName = $tableName[0];
251  }
252 
253  if ($schema === NULL) {
254  $schema = '\''.DAL::getDbName().'\'';
255  }
256 
257  $sql = 'SELECT count(*) FROM INFORMATION_SCHEMA.tables WHERE TABLE_NAME = '.$tableName.' AND TABLE_SCHEMA = \'public\' AND TABLE_CATALOG = '.$schema;
258  return $sql;
259 
260  }//end handleFunctionTableExists()
261 
262 
263 }//end class
264 
265 ?>