db

package module
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2019 License: MIT Imports: 13 Imported by: 6

Documentation

Overview

Package db provides helper functions to access the pocoweb database.

Index

Constants

View Source
const (
	StatusIDFailed = iota
	StatusIDRunning
	StatusIDDone
	StatusIDEmpty
	StatusIDProfiled
	StatusIDPostCorrected
	StatusIDExtendedLexicon
	StatusIDProfiledWithEL
)

Status IDs

View Source
const (
	StatusFailed          = "failed"
	StatusRunning         = "running"
	StatusDone            = "done"
	StatusEmpty           = "empty"
	StatusProfiled        = "profiled"
	StatusPostCorrected   = "post-corrected"
	StatusExtendedLexicon = "extended-lexicon"
	StatusProfiledWithEL  = "profiled-with-el"
)

Status names

View Source
const (
	// IDLength defines the length of auth tokens.
	IDLength = 10
	// Expires defines the time after a session expires
	Expires = 10 * time.Hour
)
View Source
const (
	TypesTableName = "types" // Name of the types table.
	TypesTableID   = "ID"    // ID field
	TypesTableType = "typ"   // type field
	MaxType        = 50      // Max length of type strings
)
View Source
const (
	SuggestionsTableName             = "suggestions"
	SuggestionsTableID               = "id"
	SuggestionsTableBookID           = "bookid"
	SuggestionsTableTokenTypeID      = "tokentypid"
	SuggestionsTableSuggestionTypeID = "suggestiontypid"
	SuggestionsTableModernTypeID     = "moderntypid"
	SuggestionsTableDict             = "dict"
	SuggestionsTableWeight           = "weight"
	SuggestionsTableTopSuggestion    = "topsuggestion"
	SuggestionsTableDistance         = "distance"
	SuggestionsTableHistPatterns     = "histpatterns"
	SuggestionsTableOCRPatterns      = "ocrpatterns"
)
View Source
const (
	// HashLength defines the lenght of the password hash.
	HashLength = 64
	// SaltLength defines the length of the salt.
	SaltLength = 32
)
View Source
const BooksTableName = "books"
View Source
const ContentsTableName = "contents"

ContentsTableName defines the name of the contents table.

View Source
const JobsTableName = "jobs"

JobsTableName defines the name of the jobs table.

View Source
const PagesTableName = "pages"
View Source
const ProjectPagesTableName = "project_pages"
View Source
const ProjectsTableName = "projects"

ProjectsTableName of the the database table.

View Source
const SessionsTableName = "sessions"

Name defines the name of the sessions table.

View Source
const StatusTableName = "status"

StatusTableName defines the name of the jobs status table.

View Source
const TextLinesTableName = "textlines"

TextLinesTableName defines the name of the textlines table.

View Source
const UsersTableName = "users"

Name of the table

Variables

This section is empty.

Functions

func AuthenticateUser

func AuthenticateUser(db DB, user api.User, password string) error

func Begin

func Begin(db DB) (*sql.Tx, error)

Begin calls Begin on the given DB handle and logs the beginning of a transaction.

func CreateAllTables

func CreateAllTables(db DB) error

CreateAllTables creates all tables in the right order. The order is: users -> projects -> books -> pages -> lines.

func CreateTableBooks

func CreateTableBooks(db DB) error

CreateTableBooks the database table books if it does not already exist. This function will fail, if the projects table does not exist.

func CreateTableJobs added in v0.8.0

func CreateTableJobs(db DB) error

CreateTableJobs creates the jobs and jobs status database tables if they do not already exist.

func CreateTableLines

func CreateTableLines(db DB) error

CreateTableLines creates the two tables needed for the storing of text lines in the right order. The creation will fail, if the books and pages tables do not yet exist.

func CreateTablePages

func CreateTablePages(db DB) error

CreateTablePages creates the databases table pages if it does not already exist. This function will fail if the table books does not exist.

func CreateTableProjectPages added in v0.3.0

func CreateTableProjectPages(db DB) error

func CreateTableProjects

func CreateTableProjects(db DB) error

CreateTableProjects creates the project table if it does not already exist. This function will fail, if the users table does not already exist.

func CreateTableSessions

func CreateTableSessions(db DB) error

CreateTableSessions creates the sessions table.

func CreateTableSuggestions added in v0.9.2

func CreateTableSuggestions(db DB) error

CreateTableSuggestions creates the suggestion table.

func CreateTableTypes added in v0.9.1

func CreateTableTypes(db DB) error

CreateTableTypes creates the types table.

func CreateTableUsers

func CreateTableUsers(db DB) error

CreateTableUsers creates the users table if it does not already exist.

func DeleteJobByID added in v0.8.0

func DeleteJobByID(db DB, jobID int) error

DeleteJobByID delete the given job from the database table.

func DeleteSessionByUserID

func DeleteSessionByUserID(db DB, id int64) error

func DeleteUserByID

func DeleteUserByID(db DB, id int64) error

func Exec

func Exec(db DB, stmt string, args ...interface{}) (sql.Result, error)

Exec calls Exec on the given DB handle. The given args are logged.

func FindAllUsers added in v0.2.0

func FindAllUsers(db DB) ([]api.User, error)

FindAllUsers returns all users in the database.

func FindBookPages added in v0.7.0

func FindBookPages(db DB, bookID int) ([]int, error)

FindBookPages returns the page IDs for the given book.

func FindJobByID added in v0.8.0

func FindJobByID(db DB, jobID int) (*api.JobStatus, bool, error)

FindJobByID returns the given job

func FindPageLines added in v0.6.0

func FindPageLines(db DB, bookID, pageID int) ([]int, error)

FindPageLines returns all line IDs for the page identified by the given book and page IDs.

func FindProjectPages added in v0.3.0

func FindProjectPages(db DB, projectID int) ([]int, error)

FindProjectPages returns the page IDs for the given project.

func FindSessionByID

func FindSessionByID(db DB, id string) (*api.Session, bool, error)

func FindUserByEmail

func FindUserByEmail(db DB, email string) (api.User, bool, error)

func FindUserByID

func FindUserByID(db DB, id int64) (api.User, bool, error)

func InsertBook

func InsertBook(db DB, book *Book) error

func InsertLine

func InsertLine(db DB, line *Line) error

InsertLine inserts the given line into the database.

func InsertPage

func InsertPage(db DB, page *Page) error

func InsertProject

func InsertProject(db DB, p *Project) error

func InsertSession

func InsertSession(db DB, u api.User) (*api.Session, error)

InsertSession creates a new unique session for the given user in the database and returns the new session.

func InsertUser

func InsertUser(db DB, user *api.User) error

InsertUser inserts a new user into the database. The user's id is adjusted accordingly.

func NewJob added in v0.9.0

func NewJob(db DB, bookID int, text string) (int, error)

NewJob inserts a new running job into the jobs table and returns the new job ID.

func NewType added in v0.9.1

func NewType(db DB, str string, ids map[string]int) (int, error)

NewType inserts a new (string-) type into the types tables and returns its id. If the type does already exist in the table, the id of the existing string is returned and nothing is inserted into the table. All types are converted to lowercase.

An additional map can be supplied to speed up the creation of types. A nil map can be supplied.

func Query

func Query(db DB, stmt string, args ...interface{}) (*sql.Rows, error)

Query calls Query on the given DB handle. The given args are logged.

func SetJobStatus added in v0.8.0

func SetJobStatus(db DB, jobID, statusID int) error

SetJobStatus sets a new status for a job.

func SetJobStatusWithText added in v0.9.2

func SetJobStatusWithText(db DB, jobID, statusID int, text string) error

SetJobStatusWithText sets a new status and text (name) for a job.

func SetUserPassword

func SetUserPassword(db DB, user api.User, password string) error

func UpdateLine

func UpdateLine(db DB, line *Line) error

UpdateLine updates the contents for the given line.

func UpdateUser

func UpdateUser(db DB, user api.User) error

Types

type Book

type Book struct {
	BookID, Year                             int
	Status                                   map[string]bool
	Author, Title, Description, HistPatterns string
	URI, ProfilerURL, Directory, Lang        string
	Pooled                                   bool
}

func FindBookByID added in v0.4.0

func FindBookByID(db DB, id int) (*Book, bool, error)

FindBookByID loads the book from the database that is identified by the given ID.

func FindBookByProjectID added in v0.5.0

func FindBookByProjectID(db DB, id int) (*Book, bool, error)

FindBookByProjectID loads the book from the database that is identified by the given project ID.

type BookWithContent added in v0.9.2

type BookWithContent struct {
	Book
	Pages map[int]*PageWithContent
}

func LoadBookWithContent added in v0.9.2

func LoadBookWithContent(db DB, bid, pid, lid int) (*BookWithContent, bool, error)

type Char

type Char struct {
	Cor, OCR rune
	Cut, Seq int
	Conf     float64
}

Char defines a character.

type Chars added in v0.6.0

type Chars []Char

Chars defines a slice of characters.

func (Chars) AverageConfidence added in v0.6.0

func (cs Chars) AverageConfidence() float64

AverageConfidence calculates the average confidence of the character slice.

func (Chars) Cor added in v0.6.0

func (cs Chars) Cor() string

Cor returns the corrected string.

func (Chars) IsFullyCorrected added in v0.6.0

func (cs Chars) IsFullyCorrected() bool

IsFullyCorrected returns true if all characters in the slice have been corrected.

func (Chars) IsPartiallyCorrected added in v0.6.0

func (cs Chars) IsPartiallyCorrected() bool

IsPartiallyCorrected returns true if a part of the character slice contains corrections.

func (Chars) NextWord added in v0.9.1

func (cs Chars) NextWord() (word, rest Chars)

NextWord returns the next word and the rest in this character sequence. A word/token is any sequence of non whitespace characters. Deletions are ignored.

func (Chars) OCR added in v0.6.0

func (cs Chars) OCR() string

OCR returns the OCR string.

func (Chars) Trim added in v0.14.8

func (cs Chars) Trim(f func(Char) bool) Chars

Trim trims chars from cs from left and from right.

func (Chars) TrimLeft added in v0.14.8

func (cs Chars) TrimLeft(f func(Char) bool) Chars

TrimLeft removes all chars from cs where f returns true.

func (Chars) TrimRight added in v0.14.8

func (cs Chars) TrimRight(f func(Char) bool) Chars

Right removes all chars from cs where f returns true.

type DB

type DB interface {
	Exec(string, ...interface{}) (sql.Result, error)
	Query(string, ...interface{}) (*sql.Rows, error)
	Begin() (*sql.Tx, error)
	Prepare(string) (*sql.Stmt, error)
}

DB defines a simple interface for database handling.

type Line

type Line struct {
	ImagePath                string
	Chars                    Chars
	LineID                   int
	PageID                   int
	BookID                   int
	Left, Right, Top, Bottom int
}

Line defines the line of a page in a book.

func FindLineByID

func FindLineByID(db DB, bookID, pageID, lineID int) (*Line, bool, error)

FindLineByID returns the line identified by the given book, page and line ID.

type Page

type Page struct {
	BookID, PageID           int
	ImagePath, OCRPath       string
	Left, Right, Top, Bottom int
}

type PageWithContent added in v0.9.2

type PageWithContent struct {
	Page
	Lines []Line
}

type Project

type Project struct {
	Book
	ProjectID int
	Pages     int
	Owner     api.User
}

Project wraps a book with project-related information.

func FindProjectByID

func FindProjectByID(db DB, id int) (*Project, bool, error)

func FindProjectByOwner

func FindProjectByOwner(db DB, owner int64) ([]Project, error)

func (Project) String

func (p Project) String() string

type Transaction

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

Transaction wraps a sql.Tx to abbort database transactions.

func NewTransaction

func NewTransaction(tx *sql.Tx, err error) *Transaction

func (*Transaction) Begin

func (t *Transaction) Begin() (*sql.Tx, error)

Begin return this transaction's Tx object with all active errors encountered so far.

func (*Transaction) Do

func (t *Transaction) Do(f func(DB) error)

Do runs a function within the transaction.

func (*Transaction) Done

func (t *Transaction) Done() error

Done commits the transaction if no error was encountered during the execution. If an error was encountered, the whole transaction is rolled back.

func (*Transaction) Exec

func (t *Transaction) Exec(stmt string, args ...interface{}) (sql.Result, error)

func (*Transaction) Prepare added in v0.14.1

func (t *Transaction) Prepare(query string) (*sql.Stmt, error)

func (*Transaction) Query

func (t *Transaction) Query(stmt string, args ...interface{}) (*sql.Rows, error)

type TypeInserter added in v0.14.10

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

TypeInserter helps handle types with their according type IDs.

func NewTypeInserter added in v0.14.10

func NewTypeInserter(db *sql.DB) (*TypeInserter, error)

NewTypeInserter constructs a new TypeInserter instance.

func (*TypeInserter) Close added in v0.14.10

func (t *TypeInserter) Close() (err error)

Close closes the database connections.

func (*TypeInserter) ID added in v0.14.10

func (t *TypeInserter) ID(typ string) (int, error)

ID returns the id of a given type. If the type does not yet exist, it is inserted into the database.

Directories

Path Synopsis
Package sqlite defines a simple helper function to call functions with a temporary sqlite database.
Package sqlite defines a simple helper function to call functions with a temporary sqlite database.

Jump to

Keyboard shortcuts

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