Exemplo n.º 1
0
func (b *BlogPost) Save(db sqlx.Ext) error {
	if b.ID == 0 {
		q := `INSERT INTO blog_posts (category, title, content, publish, hero) 
		                      VALUES(:category,:title,:content,:publish,:hero)
		      RETURNING id
		`

		rows, err := sqlx.NamedQuery(db, q, b)
		if err != nil {
			return err
		}

		defer rows.Close()
		rows.Next()
		err = rows.Scan(&b.ID)
		return err
	} else {
		q := `UPDATE blog_posts SET 
			category = :category, title = :title, content = :content, publish = :publish,
			hero = :hero
			WHERE id = :id
		`
		_, err := sqlx.NamedExec(db, q, b)
		return err
	}
}
Exemplo n.º 2
0
// CreateUser ...
func (sdb *SQLiteNewtonDB) CreateUser(user *User) (int64, error) {
	const insertSQL = `INSERT INTO users (username, full_name, password) VALUES (:username, :full_name, :password)`
	result, err := sqlx.NamedExec(sdb.db, insertSQL, user)
	if err != nil {
		return -1, err
	}

	return result.LastInsertId()
}
Exemplo n.º 3
0
// CreateSession writes a session object to disk and returns the id of the new record
func (sdb *SQLiteNewtonDB) CreateSession(session *Session) (int64, error) {
	const insertSQL = `INSERT INTO sessions (access_token, user_id, creation_date) VALUES (:access_token, :user_id, :creation_date)`
	result, err := sqlx.NamedExec(sdb.db, insertSQL, session)
	if err != nil {
		return -1, err
	}

	return result.LastInsertId()
}
Exemplo n.º 4
0
// CreateBookmark ...
func (sdb *SQLiteNewtonDB) CreateBookmark(bookmark *Bookmark) (int64, error) {
	const insertSQL = `INSERT INTO bookmarks (url, title, owner_id) VALUES (:url, :title, :owner_id)`
	result, err := sqlx.NamedExec(sdb.db, insertSQL, bookmark)
	if err != nil {
		return -1, err
	}

	return result.LastInsertId()
}
Exemplo n.º 5
0
// AddLocationRecord ...
func (sdb *SQLiteNewtonDB) AddLocationRecord(locRec *LocationRecord) error {
	const insertSQL = `INSERT INTO location_records (timestamp, latitude, longitude, owner_id) VALUES (:timestamp, :latitude, :longitude, :owner_id)`
	_, err := sqlx.NamedExec(sdb.db, insertSQL, locRec)
	return err
}