Пример #1
0
// LastRowId returns the Id of the last inserted row in database.
func LastRowId(conn *sqlite.Conn) (id int64, err error) {
	stmt, err := conn.Prepare(LastRowIdSQL)
	if err != nil {
		return
	}
	defer stmt.Finalize()
	return LastRowIdFromStmt(stmt)
}
Пример #2
0
// UpdateRow updates a row. ptr points to the business object being updated.
// sql must be of form "update table_name set ... where id = ?"
func UpdateRow(
	conn *sqlite.Conn,
	row RowForWriting,
	ptr interface{},
	sql string) error {
	values, err := UpdateValues(row, ptr)
	if err != nil {
		return err
	}
	return conn.Exec(sql, values...)
}
Пример #3
0
// AddRow adds a new row. row being added must have auto increment id field.
// ptr points to the business object being added.
func AddRow(
	conn *sqlite.Conn,
	row RowForWriting,
	ptr interface{},
	rowId *int64,
	sql string) error {
	values, err := InsertValues(row, ptr)
	if err != nil {
		return err
	}
	if err = conn.Exec(sql, values...); err != nil {
		return err
	}
	*rowId, err = LastRowId(conn)
	return err
}
Пример #4
0
// ReadMultiple executes sql and reads multiple rows.
// consumer may consume either business objects or db.Etagger objects.
// For the latter case, row must also implement RowForWriting
func ReadMultiple(
	conn *sqlite.Conn,
	row RowForReading,
	consumer functional.Consumer,
	sql string,
	params ...interface{}) error {
	stmt, err := conn.Prepare(sql)
	if err != nil {
		return err
	}
	defer stmt.Finalize()
	if err = stmt.Exec(params...); err != nil {
		return err
	}
	return consumer.Consume(ReadRows(row, stmt))
}
Пример #5
0
// SetUpTables creates all needed tables in database for the vsafe app.
func SetUpTables(conn *sqlite.Conn) error {
	err := conn.Exec("create table if not exists user (id INTEGER PRIMARY KEY AUTOINCREMENT, owner INTEGER, name TEXT, key TEXT, checksum TEXT)")
	if err != nil {
		return err
	}
	err = conn.Exec("create unique index if not exists user_name_idx on user (name)")
	if err != nil {
		return err
	}
	err = conn.Exec("create table if not exists entry (id INTEGER PRIMARY KEY AUTOINCREMENT, owner INTEGER, url TEXT, title TEXT, desc TEXT, uname TEXT, password TEXT, special TEXT)")
	if err != nil {
		return err
	}
	return nil
}