⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pg_dump.c

📁 PostgreSQL7.4.6 for Linux
💻 C
📖 第 1 页 / 共 5 页
字号:
	int			i;	PQExpBuffer query;	NamespaceInfo *nsinfo;	int			i_oid;	int			i_nspname;	int			i_usename;	int			i_nspacl;	/*	 * Before 7.3, there are no real namespaces; create two dummy entries,	 * one for user stuff and one for system stuff.	 */	if (g_fout->remoteVersion < 70300)	{		nsinfo = (NamespaceInfo *) malloc(2 * sizeof(NamespaceInfo));		nsinfo[0].oid = strdup("0");		nsinfo[0].nspname = strdup("");		nsinfo[0].usename = strdup("");		nsinfo[0].nspacl = strdup("");		selectDumpableNamespace(&nsinfo[0]);		nsinfo[1].oid = strdup("1");		nsinfo[1].nspname = strdup("pg_catalog");		nsinfo[1].usename = strdup("");		nsinfo[1].nspacl = strdup("");		selectDumpableNamespace(&nsinfo[1]);		g_namespaces = nsinfo;		g_numNamespaces = *numNamespaces = 2;		return nsinfo;	}	query = createPQExpBuffer();	/* Make sure we are in proper schema */	selectSourceSchema("pg_catalog");	/*	 * we fetch all namespaces including system ones, so that every object	 * we read in can be linked to a containing namespace.	 */	appendPQExpBuffer(query, "SELECT oid, nspname, "	"(select usename from pg_user where nspowner = usesysid) as usename, "					  "nspacl "					  "FROM pg_namespace");	res = PQexec(g_conn, query->data);	if (!res ||		PQresultStatus(res) != PGRES_TUPLES_OK)	{		write_msg(NULL, "query to obtain list of schemas failed: %s", PQerrorMessage(g_conn));		exit_nicely();	}	ntups = PQntuples(res);	nsinfo = (NamespaceInfo *) malloc(ntups * sizeof(NamespaceInfo));	i_oid = PQfnumber(res, "oid");	i_nspname = PQfnumber(res, "nspname");	i_usename = PQfnumber(res, "usename");	i_nspacl = PQfnumber(res, "nspacl");	for (i = 0; i < ntups; i++)	{		nsinfo[i].oid = strdup(PQgetvalue(res, i, i_oid));		nsinfo[i].nspname = strdup(PQgetvalue(res, i, i_nspname));		nsinfo[i].usename = strdup(PQgetvalue(res, i, i_usename));		nsinfo[i].nspacl = strdup(PQgetvalue(res, i, i_nspacl));		/* Decide whether to dump this namespace */		selectDumpableNamespace(&nsinfo[i]);		if (strlen(nsinfo[i].usename) == 0)			write_msg(NULL, "WARNING: owner of schema \"%s\" appears to be invalid\n",					  nsinfo[i].nspname);	}	/*	 * If the user attempted to dump a specific namespace, check to ensure	 * that the specified namespace actually exists.	 */	if (selectSchemaName)	{		for (i = 0; i < ntups; i++)			if (strcmp(nsinfo[i].nspname, selectSchemaName) == 0)				break;		/* Didn't find a match */		if (i == ntups)		{			write_msg(NULL, "specified schema \"%s\" does not exist\n",					  selectSchemaName);			exit_nicely();		}	}	PQclear(res);	destroyPQExpBuffer(query);	g_namespaces = nsinfo;	g_numNamespaces = *numNamespaces = ntups;	return nsinfo;}/* * findNamespace: *		given a namespace OID and an object OID, look up the info read by *		getNamespaces * * NB: for pre-7.3 source database, we use object OID to guess whether it's * a system object or not.	In 7.3 and later there is no guessing. */static NamespaceInfo *findNamespace(const char *nsoid, const char *objoid){	int			i;	if (g_fout->remoteVersion >= 70300)	{		for (i = 0; i < g_numNamespaces; i++)		{			NamespaceInfo *nsinfo = &g_namespaces[i];			if (strcmp(nsoid, nsinfo->oid) == 0)				return nsinfo;		}		write_msg(NULL, "schema with OID %s does not exist\n", nsoid);		exit_nicely();	}	else	{		/* This code depends on the layout set up by getNamespaces. */		if (atooid(objoid) > g_last_builtin_oid)			i = 0;				/* user object */		else			i = 1;				/* system object */		return &g_namespaces[i];	}	return NULL;				/* keep compiler quiet */}/* * getTypes: *	  read all types in the system catalogs and return them in the * TypeInfo* structure * *	numTypes is set to the number of types read in */TypeInfo *getTypes(int *numTypes){	PGresult   *res;	int			ntups;	int			i;	PQExpBuffer query = createPQExpBuffer();	TypeInfo   *tinfo;	int			i_oid;	int			i_typname;	int			i_typnamespace;	int			i_usename;	int			i_typelem;	int			i_typrelid;	int			i_typrelkind;	int			i_typtype;	int			i_typisdefined;	/*	 * we include even the built-in types because those may be used as	 * array elements by user-defined types	 *	 * we filter out the built-in types when we dump out the types	 *	 * same approach for undefined (shell) types	 */	/* Make sure we are in proper schema */	selectSourceSchema("pg_catalog");	if (g_fout->remoteVersion >= 70300)	{		appendPQExpBuffer(query, "SELECT pg_type.oid, typname, "						  "typnamespace, "						  "(select usename from pg_user where typowner = usesysid) as usename, "						  "typelem, typrelid, "						  "CASE WHEN typrelid = 0 THEN ' '::\"char\" "						  "ELSE (SELECT relkind FROM pg_class WHERE oid = typrelid) END as typrelkind, "						  "typtype, typisdefined "						  "FROM pg_type");	}	else	{		appendPQExpBuffer(query, "SELECT pg_type.oid, typname, "						  "0::oid as typnamespace, "						  "(select usename from pg_user where typowner = usesysid) as usename, "						  "typelem, typrelid, "						  "CASE WHEN typrelid = 0 THEN ' '::\"char\" "						  "ELSE (SELECT relkind FROM pg_class WHERE oid = typrelid) END as typrelkind, "						  "typtype, typisdefined "						  "FROM pg_type");	}	res = PQexec(g_conn, query->data);	if (!res ||		PQresultStatus(res) != PGRES_TUPLES_OK)	{		write_msg(NULL, "query to obtain list of data types failed: %s", PQerrorMessage(g_conn));		exit_nicely();	}	ntups = PQntuples(res);	tinfo = (TypeInfo *) malloc(ntups * sizeof(TypeInfo));	i_oid = PQfnumber(res, "oid");	i_typname = PQfnumber(res, "typname");	i_typnamespace = PQfnumber(res, "typnamespace");	i_usename = PQfnumber(res, "usename");	i_typelem = PQfnumber(res, "typelem");	i_typrelid = PQfnumber(res, "typrelid");	i_typrelkind = PQfnumber(res, "typrelkind");	i_typtype = PQfnumber(res, "typtype");	i_typisdefined = PQfnumber(res, "typisdefined");	for (i = 0; i < ntups; i++)	{		tinfo[i].oid = strdup(PQgetvalue(res, i, i_oid));		tinfo[i].typname = strdup(PQgetvalue(res, i, i_typname));		tinfo[i].typnamespace = findNamespace(PQgetvalue(res, i, i_typnamespace),											  tinfo[i].oid);		tinfo[i].usename = strdup(PQgetvalue(res, i, i_usename));		tinfo[i].typelem = strdup(PQgetvalue(res, i, i_typelem));		tinfo[i].typrelid = strdup(PQgetvalue(res, i, i_typrelid));		tinfo[i].typrelkind = *PQgetvalue(res, i, i_typrelkind);		tinfo[i].typtype = *PQgetvalue(res, i, i_typtype);		/*		 * check for user-defined array types, omit system generated ones		 */		if ((strcmp(tinfo[i].typelem, "0") != 0) &&			tinfo[i].typname[0] != '_')			tinfo[i].isArray = true;		else			tinfo[i].isArray = false;		if (strcmp(PQgetvalue(res, i, i_typisdefined), "t") == 0)			tinfo[i].isDefined = true;		else			tinfo[i].isDefined = false;		if (strlen(tinfo[i].usename) == 0 && tinfo[i].isDefined)			write_msg(NULL, "WARNING: owner of data type \"%s\" appears to be invalid\n",					  tinfo[i].typname);	}	*numTypes = ntups;	PQclear(res);	destroyPQExpBuffer(query);	return tinfo;}/* * getOperators: *	  read all operators in the system catalogs and return them in the * OprInfo* structure * *	numOprs is set to the number of operators read in */OprInfo *getOperators(int *numOprs){	PGresult   *res;	int			ntups;	int			i;	PQExpBuffer query = createPQExpBuffer();	OprInfo    *oprinfo;	int			i_oid;	int			i_oprname;	int			i_oprnamespace;	int			i_usename;	int			i_oprcode;	/*	 * find all operators, including builtin operators; we filter out	 * system-defined operators at dump-out time.	 */	/* Make sure we are in proper schema */	selectSourceSchema("pg_catalog");	if (g_fout->remoteVersion >= 70300)	{		appendPQExpBuffer(query, "SELECT pg_operator.oid, oprname, "						  "oprnamespace, "						  "(select usename from pg_user where oprowner = usesysid) as usename, "						  "oprcode::oid as oprcode "						  "from pg_operator");	}	else	{		appendPQExpBuffer(query, "SELECT pg_operator.oid, oprname, "						  "0::oid as oprnamespace, "						  "(select usename from pg_user where oprowner = usesysid) as usename, "						  "oprcode::oid as oprcode "						  "from pg_operator");	}	res = PQexec(g_conn, query->data);	if (!res ||		PQresultStatus(res) != PGRES_TUPLES_OK)	{		write_msg(NULL, "query to obtain list of operators failed: %s", PQerrorMessage(g_conn));		exit_nicely();	}	ntups = PQntuples(res);	*numOprs = ntups;	oprinfo = (OprInfo *) malloc(ntups * sizeof(OprInfo));	i_oid = PQfnumber(res, "oid");	i_oprname = PQfnumber(res, "oprname");	i_oprnamespace = PQfnumber(res, "oprnamespace");	i_usename = PQfnumber(res, "usename");	i_oprcode = PQfnumber(res, "oprcode");	for (i = 0; i < ntups; i++)	{		oprinfo[i].oid = strdup(PQgetvalue(res, i, i_oid));		oprinfo[i].oprname = strdup(PQgetvalue(res, i, i_oprname));		oprinfo[i].oprnamespace = findNamespace(PQgetvalue(res, i, i_oprnamespace),												oprinfo[i].oid);		oprinfo[i].usename = strdup(PQgetvalue(res, i, i_usename));		oprinfo[i].oprcode = strdup(PQgetvalue(res, i, i_oprcode));		if (strlen(oprinfo[i].usename) == 0)			write_msg(NULL, "WARNING: owner of operator \"%s\" appears to be invalid\n",					  oprinfo[i].oprname);	}	PQclear(res);	destroyPQExpBuffer(query);	return oprinfo;}/* * getOpclasses: *	  read all opclasses in the system catalogs and return them in the * OpclassInfo* structure * *	numOpclasses is set to the number of opclasses read in */OpclassInfo *getOpclasses(int *numOpclasses){	PGresult   *res;	int			ntups;	int			i;	PQExpBuffer query = createPQExpBuffer();	OpclassInfo *opcinfo;	int			i_oid;	int			i_opcname;	int			i_opcnamespace;	int			i_usename;	/*	 * find all opclasses, including builtin opclasses; we filter out	 * system-defined opclasses at dump-out time.	 */	/* Make sure we are in proper schema */	selectSourceSchema("pg_catalog");	if (g_fout->remoteVersion >= 70300)	{		appendPQExpBuffer(query, "SELECT pg_opclass.oid, opcname, "						  "opcnamespace, "						  "(select usename from pg_user where opcowner = usesysid) as usename "						  "from pg_opclass");	}	else	{		appendPQExpBuffer(query, "SELECT pg_opclass.oid, opcname, "						  "0::oid as opcnamespace, "						  "''::name as usename "						  "from pg_opclass");	}	res = PQexec(g_conn, query->data);	if (!res ||		PQresultStatus(res) != PGRES_TUPLES_OK)	{		write_msg(NULL, "query to obtain list of operator classes failed: %s", PQerrorMessage(g_conn));		exit_nicely();	}	ntups = PQntuples(res);	*numOpclasses = ntups;	opcinfo = (OpclassInfo *) malloc(ntups * sizeof(OpclassInfo));	i_oid = PQfnumber(res, "oid");	i_opcname = PQfnumber(res, "opcname");	i_opcnamespace = PQfnumber(res, "opcnamespace");	i_usename = PQfnumber(res, "usename");	for (i = 0; i < ntups; i++)	{		opcinfo[i].oid = strdup(PQgetvalue(res, i, i_oid));		opcinfo[i].opcname = strdup(PQgetvalue(res, i, i_opcname));		opcinfo[i].opcnamespace = findNamespace(PQgetvalue(res, i, i_opcnamespace),												opcinfo[i].oid);		opcinfo[i].usename = strdup(PQgetvalue(res, i, i_usename));		if (g_fout->remoteVersion >= 70300)		{			if (strlen(opcinfo[i].usename) == 0)				write_msg(NULL, "WARNING: owner of operator class \"%s\" appears to be invalid\n",						  opcinfo[i].opcname);		}	}	PQclear(res);	destroyPQExpBuffer(query);	return opcinfo;}/* * getAggregates: *	  read all the user-defined aggregates in the system catalogs and * return them in the AggInfo* structure * * numAggs is set to the number of aggregates read in */AggInfo *getAggregates(int *numAggs){	PGresult   *res;	int			ntups;	int			i;	PQExpBuffer query = createPQExpBuffer();	AggInfo    *agginfo;	int			i_oid;	int			i_aggname;	int			i_aggnamespace;	int			i_aggbasetype;	int			i_usename;	int			i_aggacl;	/* Make sure we are in proper schema */	selectSourceSchema("pg_catalog");	/* find all user-defined aggregates */	if (g_fout->remoteVersion >= 70300)	{		appendPQExpBuffer(query, "SELECT pg_proc.oid, proname as aggname, "						  "pronamespace as aggnamespace, "

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -