sqlfs

package
v0.1.0-rc.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 16, 2019 License: Apache-2.0 Imports: 5 Imported by: 0

README

sqlfs

The package sqlfs provides an io.ReadCloser and an io.WriteCloser that treats a SQL database a filesystem, where each table in the database is like a file. The schema of the table is very simple -- it has only one column of BLOB type. All the rows consist the storage.

sqlfs provides the following features.

Create a file

To create a table named "hello" in a database "mydb" for writing, we can call Create.

f, e := sqlfs.Create(db, "mydb.hello")
f.Write([]byte("hello world!\n"))
f.Close()

where db comes from a call to database/sql.Open.

Append to a file

f, e := sqlfs.Append(db, "mydb.hello")
f.Write([]byte("hello world!\n")
f.Close()

Read from a file

f, e := sqlfs.Open(db, "mydb.hello")
buf := make([]byte, 1024)
f.Read(buf)
f.Close()

Remove a file

DropTable(db, "mydb.hello")

Check if a file exists

HasTable(db, "mydb.hello")

Other I/O operations

Feel free to use standard packages io, ioutil, etc with sqlfs. For example, we can call io.Copy to copy everything from the standard input to a table.

f, e := sqlfs.Create(db, "mydb.hello")
io.Copy(f, os.Stdin)
f.Close()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader implements io.ReadCloser

func Open

func Open(db *sql.DB, table string) (*Reader, error)

Open returns a reader to read from the given table in db.

func (*Reader) Close

func (r *Reader) Close() error

Close the reader connection to sqlfs

func (*Reader) Read

func (r *Reader) Read(p []byte) (n int, e error)

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer implements io.WriteCloser.

func Create

func Create(db *sql.DB, driver, table string) (*Writer, error)

Create creates a new table or truncates an existing table and returns a writer.

func (*Writer) Close

func (w *Writer) Close() error

Close the connection of the sqlfs

func (*Writer) Write

func (w *Writer) Write(p []byte) (n int, e error)

Write write bytes to sqlfs and returns (num_bytes, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL