16 if (!$this->query_utils) {
58 $query =
'SELECT c.relname AS "Name"' .
' FROM pg_class c, pg_user u' .
' WHERE c.relowner = u.usesysid' .
" AND c.relkind = 'r'" 59 .
' AND NOT EXISTS' .
' (SELECT 1 FROM pg_views' .
' WHERE viewname = c.relname)' .
" AND c.relname !~ '^(pg_|sql_)'" .
' UNION' 60 .
' SELECT c.relname AS "Name"' .
' FROM pg_class c' .
" WHERE c.relkind = 'r'" .
' AND NOT EXISTS' .
' (SELECT 1 FROM pg_views' 61 .
' WHERE viewname = c.relname)' .
' AND NOT EXISTS' .
' (SELECT 1 FROM pg_user' .
' WHERE usesysid = c.relowner)' 62 .
" AND c.relname !~ '^pg_'";
65 if ($db->options[
'portability']) {
66 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
82 return $db->manipulate(
"CREATE DATABASE $name");
96 return $db->manipulate(
"DROP DATABASE $name");
114 foreach ($changes as $change_name => $change) {
115 switch ($change_name) {
131 if (!empty($changes[
'add']) && is_array($changes[
'add'])) {
132 foreach ($changes[
'add'] as $field_name => $field) {
133 $query =
'ADD ' . $db->getFieldDefinition()->getDeclaration($field[
'type'], $field_name, $field);
134 $result = $db->manipulate(
"ALTER TABLE $name $query");
138 if (!empty($changes[
'remove']) && is_array($changes[
'remove'])) {
139 foreach ($changes[
'remove'] as $field_name => $field) {
140 $field_name = $db->quoteIdentifier($field_name,
true);
141 $query =
'DROP ' . $field_name;
142 $result = $db->manipulate(
"ALTER TABLE $name $query");
146 if (!empty($changes[
'change']) && is_array($changes[
'change'])) {
147 foreach ($changes[
'change'] as $field_name => $field) {
148 $field_name = $db->quoteIdentifier($field_name,
true);
149 if (!empty($field[
'type'])) {
150 $server_info = $db->getServerVersion();
152 if (is_array($server_info) && $server_info[
'major'] < 8) {
153 throw new ilDatabaseException(
'changing column type for "' . $change_name .
'\" requires PostgreSQL 8.0 or above');
156 $query =
"ALTER $field_name TYPE " . $db->getFieldDefinition()->getTypeDeclaration($field);
157 $result = $db->manipulate(
"ALTER TABLE $name $query");
159 if (array_key_exists(
'default', $field)) {
160 $query =
"ALTER $field_name SET DEFAULT " . $db->quote($field[
'definition'][
'default'], $field[
'definition'][
'type']);
161 $result = $db->manipulate(
"ALTER TABLE $name $query");
163 if (!empty($field[
'notnull'])) {
164 $query =
"ALTER $field_name " . ($field[
'definition'][
'notnull'] ?
"SET" :
"DROP") .
' NOT NULL';
165 $result = $db->manipulate(
"ALTER TABLE $name $query");
170 if (!empty($changes[
'rename']) && is_array($changes[
'rename'])) {
171 foreach ($changes[
'rename'] as $field_name => $field) {
172 $field_name = $db->quoteIdentifier($field_name,
true);
173 $result = $db->manipulate(
"ALTER TABLE $name RENAME COLUMN $field_name TO " . $db->quoteIdentifier($field[
'name'],
true));
177 if (!empty($changes[
'name'])) {
178 $result = $db->manipulate(
"ALTER TABLE " . $db->quoteIdentifier(
$name,
true) .
" RENAME TO " . $db->quoteIdentifier($changes[
'name']));
181 foreach ($idx as $index_name) {
182 $index_newname = preg_replace(
"/^$name/", $changes[
'name'], $index_name);
199 $table = $db->quoteIdentifier($table,
true);
200 $res = $this->pdo->query(
"select * from $table");
201 for (
$i = 0;
$i <
$res->columnCount();
$i++) {
216 $table = $db->quote($table,
'text');
217 $subquery =
"SELECT indexrelid FROM pg_index, pg_class";
218 $subquery .=
" WHERE pg_class.relname=$table AND pg_class.oid=pg_index.indrelid AND indisunique != 't' AND indisprimary != 't'";
219 $query =
"SELECT relname FROM pg_class WHERE oid IN ($subquery)";
220 $indexes = $db->queryCol(
$query,
'text');
223 foreach ($indexes as
$index) {
225 if (!empty($index)) {
230 if ($db->options[
'portability']) {
231 $result = array_change_key_case(
$result, $db->options[
'field_case']);
246 $table = $db->quote($table,
'text');
247 $subquery =
"SELECT indexrelid FROM pg_index, pg_class";
248 $subquery .=
" WHERE pg_class.relname=$table AND pg_class.oid=pg_index.indrelid AND (indisunique = 't' OR indisprimary = 't')";
249 $query =
"SELECT relname FROM pg_class WHERE oid IN ($subquery)";
250 $constraints = $db->queryCol(
$query);
253 foreach ($constraints as $constraint) {
255 if (!empty($constraint)) {
260 if ($db->options[
'portability']
261 && $db->options[
'field_case'] == CASE_LOWER
263 $result = array_change_key_case(
$result, $db->options[
'field_case']);
280 $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name),
true);
282 return $db->manipulate(
"CREATE SEQUENCE $sequence_name INCREMENT 1" . ($start < 1 ?
" MINVALUE $start" :
'') .
" START $start");
294 $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name),
true);
296 return $db->manipulate(
"DROP SEQUENCE $sequence_name");
312 return $db->manipulate(
"DROP INDEX $name");
323 $idx_name = preg_replace($idx_pattern,
'\\1', $idx);
324 if ($idx_name && !strcasecmp($idx, $this->db_instance->getIndexName($idx_name))) {
340 $query =
"SELECT relname FROM pg_class WHERE relkind = 'S' AND relnamespace IN";
341 $query .=
"(SELECT oid FROM pg_namespace WHERE nspname NOT LIKE 'pg_%' AND nspname != 'information_schema')";
342 $table_names = $db->queryCol(
$query);
345 foreach ($table_names as $table_name) {
348 if ($db->options[
'portability']) {
349 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
365 $table_quoted = $this->
getDBInstance()->quoteIdentifier($table,
true);
368 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())
foreach($_POST as $key=> $value) $res
listTableConstraints($table)
createSequence($seq_name, $start=1, $options=array())
Class ilPostgresQueryUtils.