//update wave_detail_rpt set efile=%d where bool SPostgres::UpdateLobFromFile(SString sTable,SString sLobField,SString sWhere,SString sFile) { LOGBASEDEBUG("Into SPostgres::UpdateLobFromFile(%s,%s,%s,%s)", sTable.data(),sLobField.data(),sWhere.data(),sFile.data()); SString sql; Oid oid; SString param; PGresult *pRes = PQexec(m_pConn, "begin"); if(pRes == NULL) { if(TestConnect() == true)//连接可用 { LOGERROR("Error in SPostgres::UpdateLobFromFile, exec begin error, err=%s", PQerrorMessage(m_pConn)); return false; } //失败自动重连一次数据库 if(!Reconnect()) { LOGERROR("Error in SPostgres::UpdateLobFromFile, exec begin error, err=%s", PQerrorMessage(m_pConn)); return false; } pRes = PQexec(m_pConn, "begin"); if(pRes == NULL) { LOGERROR("Error in SPostgres::UpdateLobFromFile, exec begin error, err=%s", PQerrorMessage(m_pConn)); return false; } } PQclear(pRes); oid = lo_import(m_pConn, sFile.data()); if(oid == 0) { LOGERROR("Error in SPostgres::UpdateLobFromFile(%s,%s,%s,%s), err=%s", sTable.data(),sLobField.data(),sWhere.data(),sFile.data(),PQerrorMessage(m_pConn)); pRes = PQexec(m_pConn, "end"); if(pRes != NULL) PQclear(pRes); return false; } sql.sprintf("update %s set %s=%d where %s",sTable.data(),sLobField.data(),oid,sWhere.data()); pRes = PQexec(m_pConn, sql.data()); LOGBASEDEBUG("%s",sql.data()); if(pRes == NULL || PQresultStatus(pRes) != PGRES_COMMAND_OK ) { LOGERROR("Error in SPostgres::UpdateLobFromFile(%s,%s,%s,%s), sql=%s, err=%s", sTable.data(),sLobField.data(),sWhere.data(),sFile.data(),sql.data(),PQerrorMessage(m_pConn)); lo_unlink(m_pConn,oid); if(pRes != NULL) PQclear(pRes); pRes = PQexec(m_pConn, "end"); if(pRes != NULL) PQclear(pRes); return false; } PQclear(pRes);//add by skt at 2007-9-13不清除将导致内存缓慢增长 pRes = PQexec(m_pConn, "end"); if(pRes != NULL) PQclear(pRes); return pRes != NULL; }
/* * do_lo_import() * * Copy large object from file to database */ bool do_lo_import(const char *filename_arg, const char *comment_arg) { PGresult *res; Oid loid; char oidbuf[32]; bool own_transaction; if (!start_lo_xact("\\lo_import", &own_transaction)) return false; SetCancelConn(); loid = lo_import(pset.db, filename_arg); ResetCancelConn(); if (loid == InvalidOid) { psql_error("%s", PQerrorMessage(pset.db)); return fail_lo_xact("\\lo_import", own_transaction); } /* insert description if given */ if (comment_arg) { char *cmdbuf; char *bufptr; size_t slen = strlen(comment_arg); cmdbuf = malloc(slen * 2 + 256); if (!cmdbuf) return fail_lo_xact("\\lo_import", own_transaction); sprintf(cmdbuf, "COMMENT ON LARGE OBJECT %u IS '", loid); bufptr = cmdbuf + strlen(cmdbuf); bufptr += PQescapeStringConn(pset.db, bufptr, comment_arg, slen, NULL); strcpy(bufptr, "'"); if (!(res = PSQLexec(cmdbuf, false))) { free(cmdbuf); return fail_lo_xact("\\lo_import", own_transaction); } PQclear(res); free(cmdbuf); } if (!finish_lo_xact("\\lo_import", own_transaction)) return false; print_lo_result("lo_import %u", loid); sprintf(oidbuf, "%u", loid); SetVariable(pset.vars, "LASTOID", oidbuf); return true; }
CAMLprim value lo_import_stub(value v_conn, value v_fname) { CAMLparam1(v_conn); PGconn *conn = get_conn(v_conn); value v_res; size_t len = caml_string_length(v_fname) + 1; char *fname = caml_stat_alloc(len); memcpy(fname, String_val(v_fname), len); caml_enter_blocking_section(); v_res = Val_int(lo_import(conn, fname)); free(fname); caml_leave_blocking_section(); CAMLreturn(v_res); }
/* ************************************************************************* */ int pg_lo_import(ClipMachine* mp, SQLCONN* c, const char *filename){ PG_CONN *conn = (PG_CONN*)c; Oid lobjId; if(!conn->at){ _clip_trap_err(mp,0,0,0,subsys,ER_START,er_start); return 1; } lobjId = lo_import(conn->conn, filename); if (lobjId == 0){ _clip_trap_err(mp,0,0,0,subsys,ER_START,er_blob_import); return 1; } _clip_retni(mp,(unsigned int) lobjId); return 0; }
int lobject_open(lobjectObject *self, connectionObject *conn, Oid oid, int mode, Oid new_oid, const char *new_file) { int retvalue = -1; PGresult *pgres = NULL; char *error = NULL; Py_BEGIN_ALLOW_THREADS; pthread_mutex_lock(&(self->conn->lock)); retvalue = pq_begin_locked(self->conn, &pgres, &error); if (retvalue < 0) goto end; /* if the oid is InvalidOid we create a new lob before opening it or we import a file from the FS, depending on the value of new_name */ if (oid == InvalidOid) { if (new_file) self->oid = lo_import(self->conn->pgconn, new_file); else self->oid = lo_create(self->conn->pgconn, new_oid); Dprintf("lobject_open: large object created with oid = %d", self->oid); if (self->oid == InvalidOid) { collect_error(self->conn, &error); retvalue = -1; goto end; } mode = INV_WRITE; } else { self->oid = oid; if (mode == 0) mode = INV_READ; } /* if the oid is a real one we try to open with the given mode, unless the mode is -1, meaning "don't open!" */ if (mode != -1) { self->fd = lo_open(self->conn->pgconn, self->oid, mode); Dprintf("lobject_open: large object opened with fd = %d", self->fd); if (self->fd == -1) { collect_error(self->conn, &error); retvalue = -1; goto end; } } /* set the mode for future reference */ switch (mode) { case -1: self->smode = "n"; break; case INV_READ: self->smode = "r"; break; case INV_WRITE: self->smode = "w"; break; case INV_READ+INV_WRITE: self->smode = "rw"; break; } retvalue = 0; end: pthread_mutex_unlock(&(self->conn->lock)); Py_END_ALLOW_THREADS; if (retvalue < 0) pq_complete_error(self->conn, &pgres, &error); return retvalue; }
int main(int argc, char **argv) { char *in_filename, *out_filename, *out_filename2; char *database; Oid lobjOid; PGconn *conn; PGresult *res; if (argc != 5) { fprintf(stderr, "Usage: %s database_name in_filename out_filename out_filename2\n", argv[0]); exit(1); } database = argv[1]; in_filename = argv[2]; out_filename = argv[3]; out_filename2 = argv[4]; /* * set up the connection */ conn = PQsetdb(NULL, NULL, NULL, NULL, database); /* check to see that the backend connection was successfully made */ if (PQstatus(conn) != CONNECTION_OK) { fprintf(stderr, "Connection to database failed: %s", PQerrorMessage(conn)); exit_nicely(conn); } res = PQexec(conn, "begin"); PQclear(res); printf("importing file \"%s\" ...\n", in_filename); /* lobjOid = importFile(conn, in_filename); */ lobjOid = lo_import(conn, in_filename); if (lobjOid == 0) fprintf(stderr, "%s\n", PQerrorMessage(conn)); else { printf("\tas large object %u.\n", lobjOid); printf("picking out bytes 4294967000-4294968000 of the large object\n"); pickout(conn, lobjOid, 4294967000U, 1000); printf("overwriting bytes 4294967000-4294968000 of the large object with X's\n"); overwrite(conn, lobjOid, 4294967000U, 1000); printf("exporting large object to file \"%s\" ...\n", out_filename); /* exportFile(conn, lobjOid, out_filename); */ if (lo_export(conn, lobjOid, out_filename) < 0) fprintf(stderr, "%s\n", PQerrorMessage(conn)); printf("truncating to 3294968000 bytes\n"); my_truncate(conn, lobjOid, 3294968000U); printf("exporting truncated large object to file \"%s\" ...\n", out_filename2); if (lo_export(conn, lobjOid, out_filename2) < 0) fprintf(stderr, "%s\n", PQerrorMessage(conn)); } res = PQexec(conn, "end"); PQclear(res); PQfinish(conn); return 0; }
int main(int argc, char **argv) { char *in_filename, *out_filename; char *database; Oid lobjOid; PGconn *conn; PGresult *res; if (argc != 4) { fprintf(stderr, "Usage: %s database_name in_filename out_filename\n", argv[0]); exit(1); } database = argv[1]; in_filename = argv[2]; out_filename = argv[3]; /* * set up the connection */ conn = PQsetdb(NULL, NULL, NULL, NULL, database); /* check to see that the backend connection was successfully made */ if (PQstatus(conn) != CONNECTION_OK) { fprintf(stderr, "Connection to database failed: %s", PQerrorMessage(conn)); exit_nicely(conn); } /* Set always-secure search path, so malicous users can't take control. */ res = PQexec(conn, "SELECT pg_catalog.set_config('search_path', '', false)"); if (PQresultStatus(res) != PGRES_TUPLES_OK) { fprintf(stderr, "SET failed: %s", PQerrorMessage(conn)); PQclear(res); exit_nicely(conn); } PQclear(res); res = PQexec(conn, "begin"); PQclear(res); printf("importing file \"%s\" ...\n", in_filename); /* lobjOid = importFile(conn, in_filename); */ lobjOid = lo_import(conn, in_filename); if (lobjOid == 0) fprintf(stderr, "%s\n", PQerrorMessage(conn)); else { printf("\tas large object %u.\n", lobjOid); printf("picking out bytes 1000-2000 of the large object\n"); pickout(conn, lobjOid, 1000, 1000); printf("overwriting bytes 1000-2000 of the large object with X's\n"); overwrite(conn, lobjOid, 1000, 1000); printf("exporting large object to file \"%s\" ...\n", out_filename); /* exportFile(conn, lobjOid, out_filename); */ if (lo_export(conn, lobjOid, out_filename) < 0) fprintf(stderr, "%s\n", PQerrorMessage(conn)); } res = PQexec(conn, "end"); PQclear(res); PQfinish(conn); return 0; }
RAISES_NEG int lobject_open(lobjectObject *self, connectionObject *conn, Oid oid, const char *smode, Oid new_oid, const char *new_file) { int retvalue = -1; PGresult *pgres = NULL; char *error = NULL; int pgmode = 0; int mode; if (0 > (mode = _lobject_parse_mode(smode))) { return -1; } Py_BEGIN_ALLOW_THREADS; pthread_mutex_lock(&(self->conn->lock)); retvalue = pq_begin_locked(self->conn, &pgres, &error, &_save); if (retvalue < 0) goto end; /* if the oid is InvalidOid we create a new lob before opening it or we import a file from the FS, depending on the value of new_file */ if (oid == InvalidOid) { if (new_file) self->oid = lo_import(self->conn->pgconn, new_file); else { /* Use lo_creat when possible to be more middleware-friendly. See ticket #88. */ if (new_oid != InvalidOid) self->oid = lo_create(self->conn->pgconn, new_oid); else self->oid = lo_creat(self->conn->pgconn, INV_READ | INV_WRITE); } Dprintf("lobject_open: large object created with oid = %d", self->oid); if (self->oid == InvalidOid) { collect_error(self->conn, &error); retvalue = -1; goto end; } mode = (mode & ~LOBJECT_READ) | LOBJECT_WRITE; } else { self->oid = oid; } /* if the oid is a real one we try to open with the given mode */ if (mode & LOBJECT_READ) { pgmode |= INV_READ; } if (mode & LOBJECT_WRITE) { pgmode |= INV_WRITE; } if (pgmode) { self->fd = lo_open(self->conn->pgconn, self->oid, pgmode); Dprintf("lobject_open: large object opened with mode = %i fd = %d", pgmode, self->fd); if (self->fd == -1) { collect_error(self->conn, &error); retvalue = -1; goto end; } } /* set the mode for future reference */ self->mode = mode; Py_BLOCK_THREADS; self->smode = _lobject_unparse_mode(mode); Py_UNBLOCK_THREADS; if (NULL == self->smode) { retvalue = 1; /* exception already set */ goto end; } retvalue = 0; end: pthread_mutex_unlock(&(self->conn->lock)); Py_END_ALLOW_THREADS; if (retvalue < 0) pq_complete_error(self->conn, &pgres, &error); /* if retvalue > 0, an exception is already set */ return retvalue; }