2 require_once(
'class.ilDBPdoManager.php');
3 require_once(
'./Services/Database/classes/QueryUtils/class.ilPostgresQueryUtils.php');
16 if (!$this->query_utils) {
55 $query =
'SELECT c.relname AS "Name"' .
' FROM pg_class c, pg_user u' .
' WHERE c.relowner = u.usesysid' .
" AND c.relkind = 'r'" 56 .
' AND NOT EXISTS' .
' (SELECT 1 FROM pg_views' .
' WHERE viewname = c.relname)' .
" AND c.relname !~ '^(pg_|sql_)'" .
' UNION' 57 .
' SELECT c.relname AS "Name"' .
' FROM pg_class c' .
" WHERE c.relkind = 'r'" .
' AND NOT EXISTS' .
' (SELECT 1 FROM pg_views' 58 .
' WHERE viewname = c.relname)' .
' AND NOT EXISTS' .
' (SELECT 1 FROM pg_user' .
' WHERE usesysid = c.relowner)' 59 .
" AND c.relname !~ '^pg_'";
62 if ($db->options[
'portability']) {
63 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
76 $name = $db->quoteIdentifier($name,
true);
78 return $db->manipulate(
"CREATE DATABASE $name");
89 $name = $db->quoteIdentifier($name,
true);
91 return $db->manipulate(
"DROP DATABASE $name");
101 public function alterTable($name, $changes, $check) {
108 foreach ($changes as $change_name => $change) {
109 switch ($change_name) {
125 if (!empty($changes[
'add']) && is_array($changes[
'add'])) {
126 foreach ($changes[
'add'] as $field_name => $field) {
128 $query =
'ADD ' . $db->getFieldDefinition()->getDeclaration($field[
'type'], $field_name, $field);
129 $result = $db->manipulate(
"ALTER TABLE $name $query");
133 if (!empty($changes[
'remove']) && is_array($changes[
'remove'])) {
134 foreach ($changes[
'remove'] as $field_name => $field) {
135 $field_name = $db->quoteIdentifier($field_name,
true);
136 $query =
'DROP ' . $field_name;
137 $result = $db->manipulate(
"ALTER TABLE $name $query");
141 if (!empty($changes[
'change']) && is_array($changes[
'change'])) {
142 foreach ($changes[
'change'] as $field_name => $field) {
143 $field_name = $db->quoteIdentifier($field_name,
true);
144 if (!empty($field[
'type'])) {
145 $server_info = $db->getServerVersion();
147 if (is_array($server_info) && $server_info[
'major'] < 8) {
148 throw new ilDatabaseException(
'changing column type for "' . $change_name .
'\" requires PostgreSQL 8.0 or above');
151 $query =
"ALTER $field_name TYPE " . $db->getFieldDefinition()->getTypeDeclaration($field);
152 $result = $db->manipulate(
"ALTER TABLE $name $query");
154 if (array_key_exists(
'default', $field)) {
155 $query =
"ALTER $field_name SET DEFAULT " . $db->quote($field[
'definition'][
'default'], $field[
'definition'][
'type']);
156 $result = $db->manipulate(
"ALTER TABLE $name $query");
158 if (!empty($field[
'notnull'])) {
159 $query =
"ALTER $field_name " . ($field[
'definition'][
'notnull'] ?
"SET" :
"DROP") .
' NOT NULL';
160 $result = $db->manipulate(
"ALTER TABLE $name $query");
165 if (!empty($changes[
'rename']) && is_array($changes[
'rename'])) {
166 foreach ($changes[
'rename'] as $field_name => $field) {
167 $field_name = $db->quoteIdentifier($field_name,
true);
168 $result = $db->manipulate(
"ALTER TABLE $name RENAME COLUMN $field_name TO " . $db->quoteIdentifier($field[
'name'],
true));
172 if (!empty($changes[
'name'])) {
173 $result = $db->manipulate(
"ALTER TABLE " . $db->quoteIdentifier($name,
true) .
" RENAME TO " . $db->quoteIdentifier($changes[
'name']));
176 foreach ($idx as $index_name) {
177 $index_newname = preg_replace(
"/^$name/", $changes[
'name'], $index_name);
193 $table = $db->quoteIdentifier($table,
true);
194 $res = $this->pdo->query(
"select * from $table");
195 for ($i = 0; $i <
$res->columnCount(); $i++) {
196 $data[] =
$res->getColumnMeta($i)[
"name"];
209 $table = $db->quote($table,
'text');
210 $subquery =
"SELECT indexrelid FROM pg_index, pg_class";
211 $subquery .=
" WHERE pg_class.relname=$table AND pg_class.oid=pg_index.indrelid AND indisunique != 't' AND indisprimary != 't'";
212 $query =
"SELECT relname FROM pg_class WHERE oid IN ($subquery)";
213 $indexes = $db->queryCol(
$query,
'text');
216 foreach ($indexes as $index) {
218 if (!empty($index)) {
223 if ($db->options[
'portability']) {
224 $result = array_change_key_case(
$result, $db->options[
'field_case']);
238 $table = $db->quote($table,
'text');
239 $subquery =
"SELECT indexrelid FROM pg_index, pg_class";
240 $subquery .=
" WHERE pg_class.relname=$table AND pg_class.oid=pg_index.indrelid AND (indisunique = 't' OR indisprimary = 't')";
241 $query =
"SELECT relname FROM pg_class WHERE oid IN ($subquery)";
242 $constraints = $db->queryCol(
$query);
245 foreach ($constraints as $constraint) {
247 if (!empty($constraint)) {
252 if ($db->options[
'portability']
253 && $db->options[
'field_case'] == CASE_LOWER
255 $result = array_change_key_case(
$result, $db->options[
'field_case']);
271 $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name),
true);
273 return $db->manipulate(
"CREATE SEQUENCE $sequence_name INCREMENT 1" . (
$start < 1 ?
" MINVALUE $start" :
'') .
" START $start");
284 $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name),
true);
286 return $db->manipulate(
"DROP SEQUENCE $sequence_name");
299 $name = $db->quoteIdentifier($this->
getDBInstance()->constraintName($table, $name),
true);
301 return $db->manipulate(
"DROP INDEX $name");
311 $idx_name = preg_replace($idx_pattern,
'\\1', $idx);
312 if ($idx_name && !strcasecmp($idx, $this->db_instance->getIndexName($idx_name))) {
327 $query =
"SELECT relname FROM pg_class WHERE relkind = 'S' AND relnamespace IN";
328 $query .=
"(SELECT oid FROM pg_namespace WHERE nspname NOT LIKE 'pg_%' AND nspname != 'information_schema')";
329 $table_names = $db->queryCol(
$query);
332 foreach ($table_names as $table_name) {
335 if ($db->options[
'portability']) {
336 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
351 $table_quoted = $this->
getDBInstance()->quoteIdentifier($table,
true);
354 return $this->pdo->exec(
"ALTER TABLE $table_quoted DROP CONSTRAINT $name");
dropConstraint($table, $name, $primary=false)
alterTable($name, $changes, $check)
listTables($database=null)
listSequences($database=null)
fixSequenceName($sqn, $check=false)
getTableCreationQuery($name, $fields, $options=array())
Class ilDatabaseException.
createTable($name, $fields, $options=array())
if(!is_array($argv)) $options
listTableConstraints($table)
createSequence($seq_name, $start=1, $options=array())
Create styles array
The data for the language used.
Class ilPostgresQueryUtils.