|
30 | 30 | #include "catalog/pg_ts_config.h" |
31 | 31 | #include "catalog/pg_ts_dict.h" |
32 | 32 | #include "catalog/pg_type.h" |
| 33 | +#include "commands/dbcommands.h" |
33 | 34 | #include "lib/stringinfo.h" |
34 | 35 | #include "mb/pg_wchar.h" |
35 | 36 | #include "miscadmin.h" |
@@ -1763,6 +1764,123 @@ regnamespacesend(PG_FUNCTION_ARGS) |
1763 | 1764 | return oidsend(fcinfo); |
1764 | 1765 | } |
1765 | 1766 |
|
| 1767 | +/* |
| 1768 | + * regdatabasein - converts database name to database OID |
| 1769 | + * |
| 1770 | + * We also accept a numeric OID, for symmetry with the output routine. |
| 1771 | + * |
| 1772 | + * '-' signifies unknown (OID 0). In all other cases, the input must |
| 1773 | + * match an existing pg_database entry. |
| 1774 | + */ |
| 1775 | +Datum |
| 1776 | +regdatabasein(PG_FUNCTION_ARGS) |
| 1777 | +{ |
| 1778 | + char *db_name_or_oid = PG_GETARG_CSTRING(0); |
| 1779 | + Node *escontext = fcinfo->context; |
| 1780 | + Oid result; |
| 1781 | + List *names; |
| 1782 | + |
| 1783 | + /* Handle "-" or numeric OID */ |
| 1784 | + if (parseDashOrOid(db_name_or_oid, &result, escontext)) |
| 1785 | + PG_RETURN_OID(result); |
| 1786 | + |
| 1787 | + /* The rest of this wouldn't work in bootstrap mode */ |
| 1788 | + if (IsBootstrapProcessingMode()) |
| 1789 | + elog(ERROR, "regdatabase values must be OIDs in bootstrap mode"); |
| 1790 | + |
| 1791 | + /* Normal case: see if the name matches any pg_database entry. */ |
| 1792 | + names = stringToQualifiedNameList(db_name_or_oid, escontext); |
| 1793 | + if (names == NIL) |
| 1794 | + PG_RETURN_NULL(); |
| 1795 | + |
| 1796 | + if (list_length(names) != 1) |
| 1797 | + ereturn(escontext, (Datum) 0, |
| 1798 | + (errcode(ERRCODE_INVALID_NAME), |
| 1799 | + errmsg("invalid name syntax"))); |
| 1800 | + |
| 1801 | + result = get_database_oid(strVal(linitial(names)), true); |
| 1802 | + |
| 1803 | + if (!OidIsValid(result)) |
| 1804 | + ereturn(escontext, (Datum) 0, |
| 1805 | + (errcode(ERRCODE_UNDEFINED_OBJECT), |
| 1806 | + errmsg("database \"%s\" does not exist", |
| 1807 | + strVal(linitial(names))))); |
| 1808 | + |
| 1809 | + PG_RETURN_OID(result); |
| 1810 | +} |
| 1811 | + |
| 1812 | +/* |
| 1813 | + * to_regdatabase - converts database name to database OID |
| 1814 | + * |
| 1815 | + * If the name is not found, we return NULL. |
| 1816 | + */ |
| 1817 | +Datum |
| 1818 | +to_regdatabase(PG_FUNCTION_ARGS) |
| 1819 | +{ |
| 1820 | + char *db_name = text_to_cstring(PG_GETARG_TEXT_PP(0)); |
| 1821 | + Datum result; |
| 1822 | + ErrorSaveContext escontext = {T_ErrorSaveContext}; |
| 1823 | + |
| 1824 | + if (!DirectInputFunctionCallSafe(regdatabasein, db_name, |
| 1825 | + InvalidOid, -1, |
| 1826 | + (Node *) &escontext, |
| 1827 | + &result)) |
| 1828 | + PG_RETURN_NULL(); |
| 1829 | + PG_RETURN_DATUM(result); |
| 1830 | +} |
| 1831 | + |
| 1832 | +/* |
| 1833 | + * regdatabaseout - converts database OID to database name |
| 1834 | + */ |
| 1835 | +Datum |
| 1836 | +regdatabaseout(PG_FUNCTION_ARGS) |
| 1837 | +{ |
| 1838 | + Oid dboid = PG_GETARG_OID(0); |
| 1839 | + char *result; |
| 1840 | + |
| 1841 | + if (dboid == InvalidOid) |
| 1842 | + { |
| 1843 | + result = pstrdup("-"); |
| 1844 | + PG_RETURN_CSTRING(result); |
| 1845 | + } |
| 1846 | + |
| 1847 | + result = get_database_name(dboid); |
| 1848 | + |
| 1849 | + if (result) |
| 1850 | + { |
| 1851 | + /* pstrdup is not really necessary, but it avoids a compiler warning */ |
| 1852 | + result = pstrdup(quote_identifier(result)); |
| 1853 | + } |
| 1854 | + else |
| 1855 | + { |
| 1856 | + /* If OID doesn't match any database, return it numerically */ |
| 1857 | + result = (char *) palloc(NAMEDATALEN); |
| 1858 | + snprintf(result, NAMEDATALEN, "%u", dboid); |
| 1859 | + } |
| 1860 | + |
| 1861 | + PG_RETURN_CSTRING(result); |
| 1862 | +} |
| 1863 | + |
| 1864 | +/* |
| 1865 | + * regdatabaserecv - converts external binary format to regdatabase |
| 1866 | + */ |
| 1867 | +Datum |
| 1868 | +regdatabaserecv(PG_FUNCTION_ARGS) |
| 1869 | +{ |
| 1870 | + /* Exactly the same as oidrecv, so share code */ |
| 1871 | + return oidrecv(fcinfo); |
| 1872 | +} |
| 1873 | + |
| 1874 | +/* |
| 1875 | + * regdatabasesend - converts regdatabase to binary format |
| 1876 | + */ |
| 1877 | +Datum |
| 1878 | +regdatabasesend(PG_FUNCTION_ARGS) |
| 1879 | +{ |
| 1880 | + /* Exactly the same as oidsend, so share code */ |
| 1881 | + return oidsend(fcinfo); |
| 1882 | +} |
| 1883 | + |
1766 | 1884 | /* |
1767 | 1885 | * text_regclass: convert text to regclass |
1768 | 1886 | * |
|
0 commit comments