]> granicus.if.org Git - postgresql/blobdiff - src/backend/commands/dbcommands.c
This patch implement the TODO [ALTER DATABASE foo OWNER TO bar].
[postgresql] / src / backend / commands / dbcommands.c
index 85f49537efce589c7722f5d7491e455b232ce2dd..02c1bf8e2042a9441486593ba0229765eab049fc 100644 (file)
@@ -9,7 +9,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.131 2004/02/10 01:55:25 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.134 2004/05/26 13:56:45 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -64,7 +64,9 @@ createdb(const CreatedbStmt *stmt)
        char       *alt_loc;
        char       *target_dir;
        char            src_loc[MAXPGPATH];
+#ifndef WIN32
        char            buf[2 * MAXPGPATH + 100];
+#endif
        Oid                     src_dboid;
        AclId           src_owner;
        int                     src_encoding;
@@ -80,7 +82,7 @@ createdb(const CreatedbStmt *stmt)
        char            new_record_nulls[Natts_pg_database];
        Oid                     dboid;
        AclId           datdba;
-       List       *option;
+       ListCell   *option;
        DefElem    *downer = NULL;
        DefElem    *dpath = NULL;
        DefElem    *dtemplate = NULL;
@@ -774,6 +776,52 @@ AlterDatabaseSet(AlterDatabaseSetStmt *stmt)
 }
 
 
+/*
+ * ALTER DATABASE name OWNER TO newowner
+ */
+void
+AlterDatabaseOwner(const char *dbname, const char *newowner)
+{
+       AclId           newdatdba;
+       HeapTuple       tuple,
+                               newtuple;
+       Relation        rel;
+       ScanKeyData scankey;
+       SysScanDesc scan;
+
+       rel = heap_openr(DatabaseRelationName, RowExclusiveLock);
+       ScanKeyInit(&scankey,
+                               Anum_pg_database_datname,
+                               BTEqualStrategyNumber, F_NAMEEQ,
+                               NameGetDatum(dbname));
+       scan = systable_beginscan(rel, DatabaseNameIndex, true,
+                                                         SnapshotNow, 1, &scankey);
+       tuple = systable_getnext(scan);
+       if (!HeapTupleIsValid(tuple))
+               ereport(ERROR,
+                               (errcode(ERRCODE_UNDEFINED_DATABASE),
+                                errmsg("database \"%s\" does not exist", dbname)));
+
+       /* obtain sysid of proposed owner */
+       newdatdba = get_usesysid(newowner); /* will ereport if no such user */
+
+       /* changing owner's database for someone else: must be superuser */
+       /* note that the someone else need not have any permissions */
+       if (!superuser())
+               ereport(ERROR,
+                               (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+                                errmsg("must be superuser to change owner's database for another user")));
+
+       /* change owner */
+       newtuple = heap_copytuple(tuple);
+       ((Form_pg_database) GETSTRUCT(newtuple))->datdba = newdatdba;
+       simple_heap_update(rel, &tuple->t_self, newtuple);
+       CatalogUpdateIndexes(rel, newtuple);
+
+       systable_endscan(scan);
+       heap_close(rel, NoLock);
+}
+
 
 /*
  * Helper functions