RAISES_NEG int conn_set_client_encoding(connectionObject *self, const char *pgenc) { PGresult *pgres = NULL; char *error = NULL; int res = -1; char *clean_enc = NULL; /* We must know what python encoding this encoding is. */ if (0 > clear_encoding_name(pgenc, &clean_enc)) { goto exit; } /* If the current encoding is equal to the requested one we don't issue any query to the backend */ if (strcmp(self->encoding, clean_enc) == 0) return 0; Py_BEGIN_ALLOW_THREADS; pthread_mutex_lock(&self->lock); /* abort the current transaction, to set the encoding ouside of transactions */ if ((res = pq_abort_locked(self, &pgres, &error, &_save))) { goto endlock; } if ((res = pq_set_guc_locked(self, "client_encoding", clean_enc, &pgres, &error, &_save))) { goto endlock; } endlock: pthread_mutex_unlock(&self->lock); Py_END_ALLOW_THREADS; if (res < 0) { pq_complete_error(self, &pgres, &error); goto exit; } res = conn_store_encoding(self, pgenc); Dprintf("conn_set_client_encoding: encoding set to %s", self->encoding); exit: PyMem_Free(clean_enc); return res; }
RAISES_NEG int conn_switch_isolation_level(connectionObject *self, int level) { PGresult *pgres = NULL; char *error = NULL; int curr_level; int ret = -1; /* use only supported levels on older PG versions */ if (self->server_version < 80000) { if (level == ISOLATION_LEVEL_READ_UNCOMMITTED) level = ISOLATION_LEVEL_READ_COMMITTED; else if (level == ISOLATION_LEVEL_REPEATABLE_READ) level = ISOLATION_LEVEL_SERIALIZABLE; } if (-1 == (curr_level = conn_get_isolation_level(self))) { return -1; } if (curr_level == level) { /* no need to change level */ return 0; } /* Emulate the previous semantic of set_isolation_level() using the * functions currently available. */ Py_BEGIN_ALLOW_THREADS; pthread_mutex_lock(&self->lock); /* terminate the current transaction if any */ if ((ret = pq_abort_locked(self, &pgres, &error, &_save))) { goto endlock; } if (level == 0) { if ((ret = pq_set_guc_locked(self, "default_transaction_isolation", "default", &pgres, &error, &_save))) { goto endlock; } self->autocommit = 1; } else { /* find the name of the requested level */ const IsolationLevel *isolevel = conn_isolevels; while ((++isolevel)->name) { if (level == isolevel->value) { break; } } if (!isolevel->name) { ret = -1; error = strdup("bad isolation level value"); goto endlock; } if ((ret = pq_set_guc_locked(self, "default_transaction_isolation", isolevel->name, &pgres, &error, &_save))) { goto endlock; } self->autocommit = 0; } Dprintf("conn_switch_isolation_level: switched to level %d", level); endlock: pthread_mutex_unlock(&self->lock); Py_END_ALLOW_THREADS; if (ret < 0) { pq_complete_error(self, &pgres, &error); } return ret; }
RAISES_NEG int conn_set_client_encoding(connectionObject *self, const char *enc) { PGresult *pgres = NULL; char *error = NULL; int res = -1; char *codec = NULL; char *clean_enc = NULL; /* If the current encoding is equal to the requested one we don't issue any query to the backend */ if (strcmp(self->encoding, enc) == 0) return 0; /* We must know what python codec this encoding is. */ if (0 > clear_encoding_name(enc, &clean_enc)) { goto exit; } if (0 > conn_encoding_to_codec(clean_enc, &codec)) { goto exit; } Py_BEGIN_ALLOW_THREADS; pthread_mutex_lock(&self->lock); /* abort the current transaction, to set the encoding ouside of transactions */ if ((res = pq_abort_locked(self, &pgres, &error, &_save))) { goto endlock; } if ((res = pq_set_guc_locked(self, "client_encoding", clean_enc, &pgres, &error, &_save))) { goto endlock; } /* no error, we can proceed and store the new encoding */ { char *tmp = self->encoding; self->encoding = clean_enc; PyMem_Free(tmp); clean_enc = NULL; } /* Store the python codec too. */ { char *tmp = self->codec; self->codec = codec; PyMem_Free(tmp); codec = NULL; } Dprintf("conn_set_client_encoding: set encoding to %s (codec: %s)", self->encoding, self->codec); endlock: pthread_mutex_unlock(&self->lock); Py_END_ALLOW_THREADS; if (res < 0) pq_complete_error(self, &pgres, &error); exit: PyMem_Free(clean_enc); PyMem_Free(codec); return res; }