2 require_once(
'class.ilDBPdoManager.php');
3 require_once(
'./Services/Database/classes/QueryUtils/class.ilPostgresQueryUtils.php');
18 if (!$this->query_utils) {
60 $query =
'SELECT c.relname AS "Name"' .
' FROM pg_class c, pg_user u' .
' WHERE c.relowner = u.usesysid' .
" AND c.relkind = 'r'" 61 .
' AND NOT EXISTS' .
' (SELECT 1 FROM pg_views' .
' WHERE viewname = c.relname)' .
" AND c.relname !~ '^(pg_|sql_)'" .
' UNION' 62 .
' SELECT c.relname AS "Name"' .
' FROM pg_class c' .
" WHERE c.relkind = 'r'" .
' AND NOT EXISTS' .
' (SELECT 1 FROM pg_views' 63 .
' WHERE viewname = c.relname)' .
' AND NOT EXISTS' .
' (SELECT 1 FROM pg_user' .
' WHERE usesysid = c.relowner)' 64 .
" AND c.relname !~ '^pg_'";
67 if ($db->options[
'portability']) {
68 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
84 return $db->manipulate(
"CREATE DATABASE $name");
98 return $db->manipulate(
"DROP DATABASE $name");
116 foreach ($changes as $change_name => $change) {
117 switch ($change_name) {
133 if (!empty($changes[
'add']) && is_array($changes[
'add'])) {
134 foreach ($changes[
'add'] as $field_name => $field) {
135 $query =
'ADD ' . $db->getFieldDefinition()->getDeclaration($field[
'type'], $field_name, $field);
136 $result = $db->manipulate(
"ALTER TABLE $name $query");
140 if (!empty($changes[
'remove']) && is_array($changes[
'remove'])) {
141 foreach ($changes[
'remove'] as $field_name => $field) {
142 $field_name = $db->quoteIdentifier($field_name,
true);
143 $query =
'DROP ' . $field_name;
144 $result = $db->manipulate(
"ALTER TABLE $name $query");
148 if (!empty($changes[
'change']) && is_array($changes[
'change'])) {
149 foreach ($changes[
'change'] as $field_name => $field) {
150 $field_name = $db->quoteIdentifier($field_name,
true);
151 if (!empty($field[
'type'])) {
152 $server_info = $db->getServerVersion();
154 if (is_array($server_info) && $server_info[
'major'] < 8) {
155 throw new ilDatabaseException(
'changing column type for "' . $change_name .
'\" requires PostgreSQL 8.0 or above');
158 $query =
"ALTER $field_name TYPE " . $db->getFieldDefinition()->getTypeDeclaration($field);
159 $result = $db->manipulate(
"ALTER TABLE $name $query");
161 if (array_key_exists(
'default', $field)) {
162 $query =
"ALTER $field_name SET DEFAULT " . $db->quote($field[
'definition'][
'default'], $field[
'definition'][
'type']);
163 $result = $db->manipulate(
"ALTER TABLE $name $query");
165 if (!empty($field[
'notnull'])) {
166 $query =
"ALTER $field_name " . ($field[
'definition'][
'notnull'] ?
"SET" :
"DROP") .
' NOT NULL';
167 $result = $db->manipulate(
"ALTER TABLE $name $query");
172 if (!empty($changes[
'rename']) && is_array($changes[
'rename'])) {
173 foreach ($changes[
'rename'] as $field_name => $field) {
174 $field_name = $db->quoteIdentifier($field_name,
true);
175 $result = $db->manipulate(
"ALTER TABLE $name RENAME COLUMN $field_name TO " . $db->quoteIdentifier($field[
'name'],
true));
179 if (!empty($changes[
'name'])) {
180 $result = $db->manipulate(
"ALTER TABLE " . $db->quoteIdentifier(
$name,
true) .
" RENAME TO " . $db->quoteIdentifier($changes[
'name']));
183 foreach ($idx as $index_name) {
184 $index_newname = preg_replace(
"/^$name/", $changes[
'name'], $index_name);
202 $res = $this->pdo->query(
"select * from $table");
203 for (
$i = 0;
$i <
$res->columnCount();
$i++) {
219 $subquery =
"SELECT indexrelid FROM pg_index, pg_class";
220 $subquery .=
" WHERE pg_class.relname=$table AND pg_class.oid=pg_index.indrelid AND indisunique != 't' AND indisprimary != 't'";
221 $query =
"SELECT relname FROM pg_class WHERE oid IN ($subquery)";
222 $indexes = $db->queryCol(
$query,
'text');
225 foreach ($indexes as
$index) {
227 if (!empty($index)) {
232 if ($db->options[
'portability']) {
233 $result = array_change_key_case(
$result, $db->options[
'field_case']);
249 $subquery =
"SELECT indexrelid FROM pg_index, pg_class";
250 $subquery .=
" WHERE pg_class.relname=$table AND pg_class.oid=pg_index.indrelid AND (indisunique = 't' OR indisprimary = 't')";
251 $query =
"SELECT relname FROM pg_class WHERE oid IN ($subquery)";
252 $constraints = $db->queryCol(
$query);
255 foreach ($constraints as $constraint) {
257 if (!empty($constraint)) {
262 if ($db->options[
'portability']
263 && $db->options[
'field_case'] == CASE_LOWER
265 $result = array_change_key_case(
$result, $db->options[
'field_case']);
282 $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name),
true);
284 return $db->manipulate(
"CREATE SEQUENCE $sequence_name INCREMENT 1" . ($start < 1 ?
" MINVALUE $start" :
'') .
" START $start");
296 $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name),
true);
298 return $db->manipulate(
"DROP SEQUENCE $sequence_name");
314 return $db->manipulate(
"DROP INDEX $name");
325 $idx_name = preg_replace($idx_pattern,
'\\1', $idx);
326 if ($idx_name && !strcasecmp($idx, $this->db_instance->getIndexName($idx_name))) {
342 $query =
"SELECT relname FROM pg_class WHERE relkind = 'S' AND relnamespace IN";
343 $query .=
"(SELECT oid FROM pg_namespace WHERE nspname NOT LIKE 'pg_%' AND nspname != 'information_schema')";
344 $table_names = $db->queryCol(
$query);
347 foreach ($table_names as $table_name) {
350 if ($db->options[
'portability']) {
351 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
370 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())
Create styles array
The data for the language used.
Class ilPostgresQueryUtils.
if(empty($password)) $table
if(!isset($_REQUEST['ReturnTo'])) if(!isset($_REQUEST['AuthId'])) $options