Exemplo n.º 1
0
Arquivo: blob.go Projeto: pkf/gosqlite
// Write writes data into a BLOB incrementally.
// (See http://sqlite.org/c3ref/blob_write.html)
func (w *BlobReadWriter) Write(v []byte) (int, error) {
	if len(v) == 0 {
		return 0, nil
	}
	size, err := w.Size()
	if err != nil {
		return 0, err
	}
	if w.WriteOffset >= size {
		return 0, io.EOF
	}
	/* Write must return a non-nil error if it returns n < len(v) */
	if len(v) > (size - w.WriteOffset) {
		v = v[0 : size-w.WriteOffset]
		err = io.EOF
	}
	p := &v[0]
	n := len(v)
	rv := C.sqlite3_blob_write(w.bl, unsafe.Pointer(p), C.int(n), C.int(w.WriteOffset))
	if rv != C.SQLITE_OK {
		return 0, w.c.error(rv, "BlobReadWiter.Write")
	}
	w.WriteOffset += n
	return n, err
}
Exemplo n.º 2
0
// Write writes data into a BLOB incrementally.
// (See http://sqlite.org/c3ref/blob_write.html)
func (w *BlobReadWriter) Write(v []byte) (int, error) {
	if len(v) == 0 {
		return 0, nil
	}
	size, err := w.Size()
	if err != nil {
		return 0, err
	}
	if w.offset >= size {
		return 0, io.EOF
	}
	/* Write must return a non-nil error if it returns n < len(v) */
	n := size - w.offset
	if len(v) <= int(n) {
		n = int32(len(v))
	} else {
		err = io.EOF
	}
	p := &v[0]
	rv := C.sqlite3_blob_write(w.bl, unsafe.Pointer(p), C.int(n), C.int(w.offset))
	if rv != C.SQLITE_OK {
		return 0, w.c.error(rv, "BlobReadWiter.Write")
	}
	w.offset += n
	return int(n), err
}