package pgx

import (
	
	
	

	
)

// The PostgreSQL wire protocol has a limit of 1 GB - 1 per message. See definition of
// PQ_LARGE_MESSAGE_LIMIT in the PostgreSQL source code. To allow for the other data
// in the message,maxLargeObjectMessageLength should be no larger than 1 GB - 1 KB.
var maxLargeObjectMessageLength = 1024*1024*1024 - 1024

// LargeObjects is a structure used to access the large objects API. It is only valid within the transaction where it
// was created.
//
// For more details see: http://www.postgresql.org/docs/current/static/largeobjects.html
type LargeObjects struct {
	tx Tx
}

type LargeObjectMode int32

const (
	LargeObjectModeWrite LargeObjectMode = 0x20000
	LargeObjectModeRead  LargeObjectMode = 0x40000
)

// Create creates a new large object. If oid is zero, the server assigns an unused OID.
func ( *LargeObjects) ( context.Context,  uint32) (uint32, error) {
	 := .tx.QueryRow(, "select lo_create($1)", ).Scan(&)
	return , 
}

// Open opens an existing large object with the given mode. ctx will also be used for all operations on the opened large
// object.
func ( *LargeObjects) ( context.Context,  uint32,  LargeObjectMode) (*LargeObject, error) {
	var  int32
	 := .tx.QueryRow(, "select lo_open($1, $2)", , ).Scan(&)
	if  != nil {
		return nil, 
	}
	return &LargeObject{fd: , tx: .tx, ctx: }, nil
}

// Unlink removes a large object from the database.
func ( *LargeObjects) ( context.Context,  uint32) error {
	var  int32
	 := .tx.QueryRow(, "select lo_unlink($1)", ).Scan(&)
	if  != nil {
		return 
	}

	if  != 1 {
		return errors.New("failed to remove large object")
	}

	return nil
}

// A LargeObject is a large object stored on the server. It is only valid within the transaction that it was initialized
// in. It uses the context it was initialized with for all operations. It implements these interfaces:
//
//	io.Writer
//	io.Reader
//	io.Seeker
//	io.Closer
type LargeObject struct {
	ctx context.Context
	tx  Tx
	fd  int32
}

// Write writes p to the large object and returns the number of bytes written and an error if not all of p was written.
func ( *LargeObject) ( []byte) (int, error) {
	 := 0
	for {
		 := len() - 
		if  == 0 {
			break
		} else if  > maxLargeObjectMessageLength {
			 = maxLargeObjectMessageLength
		}

		var  int
		 := .tx.QueryRow(.ctx, "select lowrite($1, $2)", .fd, [:+]).Scan(&)
		if  != nil {
			return , 
		}

		if  < 0 {
			return , errors.New("failed to write to large object")
		}

		 += 

		if  <  {
			return , errors.New("short write to large object")
		} else if  >  {
			return , errors.New("invalid write to large object")
		}
	}

	return , nil
}

// Read reads up to len(p) bytes into p returning the number of bytes read.
func ( *LargeObject) ( []byte) (int, error) {
	 := 0
	for {
		 := len() - 
		if  == 0 {
			break
		} else if  > maxLargeObjectMessageLength {
			 = maxLargeObjectMessageLength
		}

		 := pgtype.PreallocBytes([:])
		 := .tx.QueryRow(.ctx, "select loread($1, $2)", .fd, ).Scan(&)
		// We compute expected so that it always fits into p, so it should never happen
		// that PreallocBytes's ScanBytes had to allocate a new slice.
		 += len()
		if  != nil {
			return , 
		}

		if len() <  {
			return , io.EOF
		} else if len() >  {
			return , errors.New("invalid read of large object")
		}
	}

	return , nil
}

// Seek moves the current location pointer to the new location specified by offset.
func ( *LargeObject) ( int64,  int) ( int64,  error) {
	 = .tx.QueryRow(.ctx, "select lo_lseek64($1, $2, $3)", .fd, , ).Scan(&)
	return , 
}

// Tell returns the current read or write location of the large object descriptor.
func ( *LargeObject) () ( int64,  error) {
	 = .tx.QueryRow(.ctx, "select lo_tell64($1)", .fd).Scan(&)
	return , 
}

// Truncate the large object to size.
func ( *LargeObject) ( int64) ( error) {
	_,  = .tx.Exec(.ctx, "select lo_truncate64($1, $2)", .fd, )
	return 
}

// Close the large object descriptor.
func ( *LargeObject) () error {
	,  := .tx.Exec(.ctx, "select lo_close($1)", .fd)
	return 
}