* Prepare for a future call to SetClientEncoding. Success should mean * that SetClientEncoding is guaranteed to succeed for this encoding request. * * (But note that success before backend_startup_complete does not guarantee * success after ...) * * Returns 0 if okay, -1 if not (bad encoding or can't support conversion) */
| 109 | * Returns 0 if okay, -1 if not (bad encoding or can't support conversion) |
| 110 | */ |
| 111 | int |
| 112 | PrepareClientEncoding(int encoding) |
| 113 | { |
| 114 | int current_server_encoding; |
| 115 | ListCell *lc; |
| 116 | |
| 117 | if (!PG_VALID_FE_ENCODING(encoding)) |
| 118 | return -1; |
| 119 | |
| 120 | /* Can't do anything during startup, per notes above */ |
| 121 | if (!backend_startup_complete) |
| 122 | return 0; |
| 123 | |
| 124 | current_server_encoding = GetDatabaseEncoding(); |
| 125 | |
| 126 | /* |
| 127 | * Check for cases that require no conversion function. |
| 128 | */ |
| 129 | if (current_server_encoding == encoding || |
| 130 | current_server_encoding == PG_SQL_ASCII || |
| 131 | encoding == PG_SQL_ASCII) |
| 132 | return 0; |
| 133 | |
| 134 | if (IsTransactionState()) |
| 135 | { |
| 136 | /* |
| 137 | * If we're in a live transaction, it's safe to access the catalogs, |
| 138 | * so look up the functions. We repeat the lookup even if the info is |
| 139 | * already cached, so that we can react to changes in the contents of |
| 140 | * pg_conversion. |
| 141 | */ |
| 142 | Oid to_server_proc, |
| 143 | to_client_proc; |
| 144 | ConvProcInfo *convinfo; |
| 145 | MemoryContext oldcontext; |
| 146 | |
| 147 | to_server_proc = FindDefaultConversionProc(encoding, |
| 148 | current_server_encoding); |
| 149 | if (!OidIsValid(to_server_proc)) |
| 150 | return -1; |
| 151 | to_client_proc = FindDefaultConversionProc(current_server_encoding, |
| 152 | encoding); |
| 153 | if (!OidIsValid(to_client_proc)) |
| 154 | return -1; |
| 155 | |
| 156 | /* |
| 157 | * Load the fmgr info into TopMemoryContext (could still fail here) |
| 158 | */ |
| 159 | convinfo = (ConvProcInfo *) MemoryContextAlloc(TopMemoryContext, |
| 160 | sizeof(ConvProcInfo)); |
| 161 | convinfo->s_encoding = current_server_encoding; |
| 162 | convinfo->c_encoding = encoding; |
| 163 | fmgr_info_cxt(to_server_proc, &convinfo->to_server_info, |
| 164 | TopMemoryContext); |
| 165 | fmgr_info_cxt(to_client_proc, &convinfo->to_client_info, |
| 166 | TopMemoryContext); |
| 167 | |
| 168 | /* Attach new info to head of list */ |
no test coverage detected