public void NpgsqlErrorRepro1() { throw new NotImplementedException(); #if WHAT_TO_DO_WITH_THIS using (var connection = new NpgsqlConnection(ConnectionString)) { connection.Open(); using (var transaction = connection.BeginTransaction()) { var largeObjectMgr = new LargeObjectManager(connection); try { var largeObject = largeObjectMgr.Open(-1, LargeObjectManager.READWRITE); transaction.Commit(); } catch { // ignore the LO failure } } // *1* sometimes it throws "System.NotSupportedException: This stream does not support seek operations" using (var command = connection.CreateCommand()) { command.CommandText = "SELECT * FROM pg_database"; using (var reader = command.ExecuteReader()) { Assert.IsTrue(reader.Read()); // *2* this fails if the initial connection is used } } } // *3* sometimes it throws "System.NotSupportedException: This stream does not support seek operations" #endif }
/// <summary> /// Creates a new media object. /// </summary> /// <param name="media">The memory stream containing the media.</param> /// <param name="type">The media type.</param> /// <param name="rpu">A delegate of type <see cref="StatusMessageReportProgress"/> used to send messages back to the calling object.</param> /// <param name="caller">The calling object.</param> /// <returns>The id for the new media object.</returns> /// <remarks>Documented by Dev03, 2008-08-05</remarks> /// <remarks>Documented by Dev03, 2009-01-13</remarks> public int CreateMedia(Stream media, EMedia type, StatusMessageReportProgress rpu, object caller) { using (NpgsqlConnection conn = PostgreSQLConn.CreateConnection(Parent.CurrentUser)) { NpgsqlTransaction tran = conn.BeginTransaction(); LargeObjectManager lbm = new LargeObjectManager(conn); int noid = lbm.Create(LargeObjectManager.READWRITE); LargeObject largeObject = lbm.Open(noid, LargeObjectManager.READWRITE); byte[] buffer = new byte[media.Length]; media.Read(buffer, 0, (int)media.Length); BufferToLargeObject(buffer, largeObject, rpu, caller); largeObject.Close(); int newId = 0; using (NpgsqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "INSERT INTO \"MediaContent\" (data, media_type) VALUES (:data, :type) RETURNING id;"; cmd.Parameters.Add("data", noid); cmd.Parameters.Add("type", type.ToString()); newId = Convert.ToInt32(PostgreSQLConn.ExecuteScalar(cmd, Parent.CurrentUser)); } tran.Commit(); return(newId); } }
public void NpgsqlErrorRepro1() { using (NpgsqlConnection connection = new NpgsqlConnection(TheConnectionString)) { connection.Open(); using (NpgsqlTransaction transaction = connection.BeginTransaction()) { LargeObjectManager largeObjectMgr = new LargeObjectManager(connection); try { LargeObject largeObject = largeObjectMgr.Open(-1, LargeObjectManager.READWRITE); transaction.Commit(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); // ignore the LO failure } } // *1* sometimes it throws "System.NotSupportedException: This stream does not support seek operations" using (NpgsqlCommand command = connection.CreateCommand()) { command.CommandText = "SELECT * FROM pg_database"; using (NpgsqlDataReader reader = command.ExecuteReader()) { Assert.IsTrue(reader.Read()); // *2* this fails if the initial connection is used } } } // *3* sometimes it throws "System.NotSupportedException: This stream does not support seek operations" }
/// <summary> /// Updates the media. /// </summary> /// <param name="id">The id.</param> /// <param name="media">The media.</param> /// <remarks>Documented by Dev02, 2008-08-06</remarks> /// <remarks>Documented by Dev03, 2009-01-13</remarks> public void UpdateMedia(int id, Stream media) { using (NpgsqlConnection con = PostgreSQLConn.CreateConnection(Parent.CurrentUser)) { NpgsqlTransaction tran = con.BeginTransaction(); int noid; using (NpgsqlCommand cmd = con.CreateCommand()) { cmd.CommandText = "SELECT data FROM \"MediaContent\" WHERE id=:id"; cmd.Parameters.Add("id", id); noid = Convert.ToInt32(PostgreSQLConn.ExecuteScalar(cmd, Parent.CurrentUser)); } LargeObjectManager lbm = new LargeObjectManager(con); lbm.Delete(noid); noid = lbm.Create(LargeObjectManager.READWRITE); LargeObject largeObject = lbm.Open(noid, LargeObjectManager.READWRITE); byte[] buffer = new byte[media.Length]; media.Read(buffer, 0, (int)media.Length); BufferToLargeObject(buffer, largeObject); largeObject.Close(); using (NpgsqlCommand cmd = con.CreateCommand()) { cmd.CommandText = "UPDATE \"MediaContent\" SET data=:data WHERE id=:id"; cmd.Parameters.Add("id", id); cmd.Parameters.Add("data", noid); PostgreSQLConn.ExecuteNonQuery(cmd, Parent.CurrentUser); } tran.Commit(); } }
/// <summary> /// Writes the media object to a stream. /// </summary> /// <param name="mediaId">The media id.</param> /// <param name="output">The output.</param> private void WriteMedia(int mediaId, Stream output) { using (NpgsqlConnection conn = FileHandlerHelpers.GetPgConnection()) { int noid = 0; using (NpgsqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "SELECT data FROM \"MediaContent\" WHERE id=:id;"; cmd.Parameters.Add("id", mediaId); noid = Convert.ToInt32(cmd.ExecuteScalar()); } NpgsqlTransaction tran = conn.BeginTransaction(); LargeObjectManager lbm = new LargeObjectManager(conn); LargeObject largeObject = lbm.Open(noid, LargeObjectManager.READWRITE); largeObject.Seek(0); int size = largeObject.Size(); byte[] buffer = new byte[size]; int read = 0; int offset = 0; while (offset < size) { read = largeObject.Read(buffer, offset, Math.Min(102400, size - offset)); output.Write(buffer, offset, read); offset += 102400; } largeObject.Close(); tran.Commit(); } }
/// <summary> /// Gets the extension stream. /// </summary> /// <param name="guid">The GUID.</param> /// <returns></returns> public Stream GetExtensionStream(Guid guid) { MemoryStream stream = null; using (NpgsqlConnection con = PostgreSQLConn.CreateConnection(Parent.CurrentUser)) { int noid = 0; using (NpgsqlCommand cmd = con.CreateCommand()) { cmd.CommandText = "SELECT data FROM \"Extensions\" WHERE guid=:guid;"; cmd.Parameters.Add("guid", guid.ToString()); object obj = PostgreSQLConn.ExecuteScalar(cmd, Parent.CurrentUser); if (obj == null || obj == DBNull.Value || !(obj as long?).HasValue) { return(stream); } noid = Convert.ToInt32((obj as long?).Value); } NpgsqlTransaction tran = con.BeginTransaction(); try { LargeObjectManager lbm = new LargeObjectManager(con); LargeObject largeObject = lbm.Open(noid, LargeObjectManager.READWRITE); byte[] buffer = LargeObjectToBuffer(largeObject); stream = new MemoryStream(buffer); largeObject.Close(); } catch { } finally { tran.Commit(); } } return(stream); }
public int GetLargeObjectSize(int oid) { using (IDbTransaction transaction = BeginTransaction()) { int result; LargeObject obj = Manager.Open(oid); result = obj.Size(); obj.Close(); return(result); } }
public void NpgsqlErrorRepro2() { #if WHAT_TO_DO_WITH_THIS var connection = new NpgsqlConnection(ConnectionString); connection.Open(); var transaction = connection.BeginTransaction(); var largeObjectMgr = new LargeObjectManager(connection); try { var largeObject = largeObjectMgr.Open(-1, LargeObjectManager.READWRITE); transaction.Commit(); } catch { // ignore the LO failure try { transaction.Dispose(); } catch { // ignore dispose failure } try { connection.Dispose(); } catch { // ignore dispose failure } } using (connection = new NpgsqlConnection(ConnectionString)) { connection.Open(); using (var command = connection.CreateCommand()) { command.CommandText = "SELECT * FROM pg_database"; using (var reader = command.ExecuteReader()) { Assert.IsTrue(reader.Read()); // *1* this fails if the connection for the pool happens to be the bad one from above Assert.IsTrue(!String.IsNullOrEmpty((string)reader["datname"])); } } } #endif }
/// <summary> /// modify a record /// </summary> public void modifyz_voluntarios(z_voluntarios myz_voluntarios, string foto) { CnxBase myBase = new CnxBase(); string reqSQL; reqSQL = "UPDATE z_voluntarios SET id_voluntario=" + myz_voluntarios.id_voluntario + ",id_compania=" + myz_voluntarios.id_compania + ",nombres='" + myz_voluntarios.nombres + "',apellidos='" + myz_voluntarios.apellidos + "',rut='" + myz_voluntarios.rut + "',direccion='" + myz_voluntarios.direccion + "',fecha_nacimiento='" + myz_voluntarios.fecha_nacimiento + "',ingreso='" + myz_voluntarios.ingreso + "',num_llamado=" + myz_voluntarios.num_llamado + ",comuna='" + myz_voluntarios.comuna + "',telefono='" + myz_voluntarios.telefono + "',celular='" + myz_voluntarios.celular + "' WHERE (id_voluntario=" + myz_voluntarios.id_voluntario + ")"; try { NpgsqlConnection myConn = myBase.OpenConnection(myBase.cnxString); if (foto != null) { NpgsqlTransaction t = myConn.BeginTransaction(); LargeObjectManager lbm = new LargeObjectManager(myConn); int noid = lbm.Create(LargeObjectManager.READWRITE); LargeObject lo = lbm.Open(noid, LargeObjectManager.READWRITE); // eliminar antiguo NpgsqlCommand comm = new NpgsqlCommand("select foto from z_voluntarios where id_voluntario=" + myz_voluntarios.id_voluntario, myConn); comm.ExecuteScalar(); //lbm.Unlink(oid); FileStream fs = File.OpenRead(foto); byte[] buf = new byte[fs.Length]; fs.Read(buf, 0, (int)fs.Length); lo.Write(buf); lo.Close(); t.Commit(); reqSQL = "UPDATE z_voluntarios SET id_voluntario=" + myz_voluntarios.id_voluntario + ",id_compania=" + myz_voluntarios.id_compania + ",nombres='" + myz_voluntarios.nombres + "',apellidos='" + myz_voluntarios.apellidos + "',rut='" + myz_voluntarios.rut + "',direccion='" + myz_voluntarios.direccion + "',fecha_nacimiento='" + myz_voluntarios.fecha_nacimiento + "',ingreso='" + myz_voluntarios.ingreso + "',num_llamado=" + myz_voluntarios.num_llamado + ",comuna='" + myz_voluntarios.comuna + "',telefono='" + myz_voluntarios.telefono + "',celular='" + myz_voluntarios.celular + "', foto=" + noid + " WHERE (id_voluntario=" + myz_voluntarios.id_voluntario + ")"; } NpgsqlCommand myCommand = new NpgsqlCommand(reqSQL, myConn); myCommand.ExecuteNonQuery(); myBase.CloseConnection(myConn); } catch (Exception myErr) { throw (new Exception(myErr.ToString() + reqSQL)); } }
/// <summary> /// add a record /// </summary> /// <param name="myID"></param> public void addz_voluntarios(z_voluntarios myz_voluntarios, string foto) { CnxBase myBase = new CnxBase(); string reqSQL; reqSQL = "INSERT INTO z_voluntarios (id_compania,nombres,apellidos,rut,direccion,fecha_nacimiento,ingreso,num_llamado,comuna,telefono,celular) VALUES (" + myz_voluntarios.id_compania + ",'" + myz_voluntarios.nombres + "','" + myz_voluntarios.apellidos + "','" + myz_voluntarios.rut + "','" + myz_voluntarios.direccion + "','" + myz_voluntarios.fecha_nacimiento + "','" + myz_voluntarios.ingreso + "'," + myz_voluntarios.num_llamado + ",'" + myz_voluntarios.comuna + "','" + myz_voluntarios.telefono + "','" + myz_voluntarios.celular + "')"; try { NpgsqlConnection myConn = myBase.OpenConnection(myBase.cnxString); if (foto != null) { NpgsqlTransaction t = myConn.BeginTransaction(); LargeObjectManager lbm = new LargeObjectManager(myConn); int noid = lbm.Create(LargeObjectManager.READWRITE); LargeObject lo = lbm.Open(noid, LargeObjectManager.READWRITE); FileStream fs = File.OpenRead(foto); byte[] buf = new byte[fs.Length]; fs.Read(buf, 0, (int)fs.Length); lo.Write(buf); lo.Close(); t.Commit(); reqSQL = "INSERT INTO z_voluntarios (id_compania,nombres,apellidos,rut,direccion,fecha_nacimiento,ingreso,num_llamado,comuna,telefono,celular, foto) VALUES (" + myz_voluntarios.id_compania + ",'" + myz_voluntarios.nombres + "','" + myz_voluntarios.apellidos + "','" + myz_voluntarios.rut + "','" + myz_voluntarios.direccion + "','" + myz_voluntarios.fecha_nacimiento + "','" + myz_voluntarios.ingreso + "'," + myz_voluntarios.num_llamado + ",'" + myz_voluntarios.comuna + "','" + myz_voluntarios.telefono + "','" + myz_voluntarios.celular + "', " + noid + ")"; } NpgsqlCommand myCommand = new NpgsqlCommand(reqSQL, myConn); myCommand.ExecuteNonQuery(); myBase.CloseConnection(myConn); } catch (Exception myErr) { throw (new Exception(myErr.ToString() + reqSQL)); } }
/// <summary> /// Gets the media. /// </summary> /// <param name="id">The id.</param> /// <param name="cacheConnector">The cache connector.</param> /// <returns>A memory stream for the media object.</returns> /// <remarks>Documented by Dev03, 2008-08-05</remarks> /// <remarks>Documented by Dev03, 2009-01-13</remarks> public Stream GetMediaStream(int id, IDbMediaConnector cacheConnector) { CachingStream stream = null; using (NpgsqlConnection conn = PostgreSQLConn.CreateConnection(Parent.CurrentUser)) { int noid = 0; using (NpgsqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "SELECT data FROM \"MediaContent\" WHERE id=:id;"; cmd.Parameters.Add("id", id); noid = Convert.ToInt32(PostgreSQLConn.ExecuteScalar(cmd, Parent.CurrentUser)); } NpgsqlTransaction tran = conn.BeginTransaction(); LargeObjectManager lbm = new LargeObjectManager(conn); LargeObject largeObject = lbm.Open(noid, LargeObjectManager.READWRITE); byte[] buffer = LargeObjectToBuffer(largeObject); stream = new CachingStream(buffer, id, cacheConnector); largeObject.Close(); tran.Commit(); } return(stream); }
/// <summary> /// Sets the extension stream. /// </summary> /// <param name="guid">The GUID.</param> /// <param name="extensionStream">The extension stream.</param> public void SetExtensionStream(Guid guid, Stream extensionStream) { using (NpgsqlConnection con = PostgreSQLConn.CreateConnection(Parent.CurrentUser)) { NpgsqlTransaction tran = con.BeginTransaction(); LargeObjectManager lbm = new LargeObjectManager(con); int noid = lbm.Create(LargeObjectManager.READWRITE); LargeObject largeObject = lbm.Open(noid, LargeObjectManager.READWRITE); byte[] buffer = new byte[extensionStream.Length]; extensionStream.Read(buffer, 0, (int)extensionStream.Length); BufferToLargeObject(buffer, largeObject); largeObject.Close(); using (NpgsqlCommand cmd = con.CreateCommand()) { cmd.CommandText = "UPDATE \"Extensions\" SET data=:data WHERE guid=:guid"; cmd.Parameters.Add("data", noid); cmd.Parameters.Add("guid", guid.ToString()); PostgreSQLConn.ExecuteNonQuery(cmd, Parent.CurrentUser); } tran.Commit(); } }
public DBFile Upload(string md5, string path_to_contents, string filename, string extension, bool hidden, string compressed_mime) { IDbTransaction transaction = null; LargeObjectManager manager; LargeObject obj; int? oid; DBFile result; long filesize; string gzFilename = null; try { filesize = new FileInfo(path_to_contents).Length; if (filesize > 1024 * 1024 * 500) { throw new Exception("Max file size is 500 MB"); } using (IDbCommand cmd = CreateCommand()) { cmd.CommandText = "SELECT * FROM File WHERE md5 = '" + md5 + "'"; using (IDataReader reader = cmd.ExecuteReader()) { if (reader.Read()) { return(new DBFile(reader)); } } } //Console.WriteLine ("Uploading {0} {1} with compressed mime: {2}", Filename, md5, compressed_mime); // The file is not in the database // Note: there is a race condition here, // the same file might get added to the db before we do it here. // not quite sure how to deal with that except retrying the above if the insert below fails. if (compressed_mime == MimeTypes.GZ) { gzFilename = path_to_contents; } else { gzFilename = FileUtilities.GZCompress(path_to_contents); compressed_mime = MimeTypes.GZ; } transaction = BeginTransaction(); if (Configuration.StoreFilesInDB) { manager = new LargeObjectManager(this.dbcon); oid = manager.Create(LargeObjectManager.READWRITE); obj = manager.Open(oid.Value, LargeObjectManager.READWRITE); using (FileStream st = new FileStream(gzFilename, FileMode.Open, FileAccess.Read, FileShare.Read)) { byte [] buffer = new byte [1024]; int read = -1; while (read != 0) { read = st.Read(buffer, 0, buffer.Length); obj.Write(buffer, 0, read); } } obj.Close(); } else { oid = null; string fn = FileUtilities.CreateFilename(md5, true, true); File.Copy(gzFilename, fn, true); log.DebugFormat("Saved file to: {0}", fn); } result = new DBFile(); result.file_id = oid; result.filename = Path.GetFileName(filename); result.md5 = md5; result.size = (int)filesize; result.hidden = hidden; switch (extension.ToLower()) { case ".log": case ".stdout": case ".stderr": result.mime = MimeTypes.LOG; break; case ".txt": result.mime = MimeTypes.TXT; break; case ".htm": case ".html": result.mime = MimeTypes.HTML; break; case ".png": result.mime = MimeTypes.PNG; break; case ".jpg": result.mime = MimeTypes.JPG; break; case ".bmp": result.mime = MimeTypes.BMP; break; case ".tar": result.mime = MimeTypes.TAR; break; case ".bz": result.mime = MimeTypes.BZ; break; case ".bz2": result.mime = MimeTypes.BZ2; break; case ".zip": result.mime = MimeTypes.ZIP;; break; case ".gz": result.mime = MimeTypes.GZ; break; case ".xpi": result.mime = MimeTypes.XPI; break; case ".crx": result.mime = MimeTypes.CRX; break; default: result.mime = MimeTypes.OCTET_STREAM; break; } result.compressed_mime = compressed_mime; result.Save(this); transaction.Commit(); transaction = null; return(result); } finally { FileUtilities.TryDeleteFile(gzFilename); if (transaction != null) { transaction.Rollback(); } } }