deparse: Support CREATE DOMAIN
authorAlvaro Herrera <alvherre@alvh.no-ip.org>
Fri, 25 Apr 2014 20:54:19 +0000 (17:54 -0300)
committerAlvaro Herrera <alvherre@alvh.no-ip.org>
Mon, 13 Oct 2014 03:19:47 +0000 (00:19 -0300)
src/backend/tcop/deparse_utility.c

index 7f90e6f426cfb99b4aba6624f8cb291efb058f69..5e253277425fb7ad132b8b0c1efcef2bbb055470 100644 (file)
@@ -1559,6 +1559,60 @@ deparse_CreateRangeStmt(Oid objectId, Node *parsetree)
    return range;
 }
 
+static ObjTree *
+deparse_CreateDomain(Oid objectId, Node *parsetree)
+{
+   ObjTree    *createDomain;
+   ObjTree    *tmp;
+   HeapTuple   typTup;
+   Form_pg_type typForm;
+   List       *constraints;
+
+   typTup = SearchSysCache1(TYPEOID,
+                            objectId);
+   if (!HeapTupleIsValid(typTup))
+       elog(ERROR, "cache lookup failed for domain with OID %u", objectId);
+   typForm = (Form_pg_type) GETSTRUCT(typTup);
+
+   createDomain = new_objtree_VA("CREATE DOMAIN %{identity}D AS %{type}D %{not_null}s %{constraints}s %{collation}s",
+                                 0);
+
+   append_object_object(createDomain,
+                        "identity",
+                        new_objtree_for_qualname_id(TypeRelationId,
+                                                    objectId));
+   append_object_object(createDomain,
+                        "type",
+                        new_objtree_for_qualname_id(TypeRelationId,
+                                                    typForm->typbasetype));
+
+   if (typForm->typnotnull)
+       append_string_object(createDomain, "not_null", "NOT NULL");
+   else
+       append_string_object(createDomain, "not_null", "");
+
+   constraints = obtainConstraints(NIL, InvalidOid, objectId);
+   tmp = new_objtree_VA("%{elements: }s", 0);
+   if (constraints == NIL)
+       append_bool_object(tmp, "present", false);
+   else
+       append_array_object(tmp, "elements", constraints);
+   append_object_object(createDomain, "constraints", tmp);
+
+   tmp = new_objtree_VA("COLLATE %{collation}D", 0);
+   if (OidIsValid(typForm->typcollation))
+       append_object_object(tmp, "collation",
+                            new_objtree_for_qualname_id(CollationRelationId,
+                                                        typForm->typcollation));
+   else
+       append_bool_object(tmp, "present", false);
+   append_object_object(createDomain, "collation", tmp);
+
+   ReleaseSysCache(typTup);
+
+   return createDomain;
+}
+
 /*
  * Return the given object type as a string.
  */
@@ -2508,7 +2562,7 @@ deparse_simple_command(StashedCommand *cmd)
            break;
 
        case T_CreateDomainStmt:
-           command = NULL;
+           command = deparse_CreateDomain(objectId, parsetree);
            break;
 
        case T_CreateConversionStmt: