예제 #1
0
파일: db.go 프로젝트: keep94/appcommon
// 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
파일: db.go 프로젝트: keep94/appcommon
// 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
파일: db.go 프로젝트: keep94/appcommon
// 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
파일: db.go 프로젝트: keep94/appcommon
// 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
파일: setup.go 프로젝트: keep94/vsafe
// 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
}