Exemplo n.º 1
0
func Save(db *sqlite3.Conn, table string, uid string, obj *jason.Object) error {
	handle_err := func(col string, e error) {
		if e != nil {
			logger.Error("Error inserting %v into %v: %v", col, table, e.Error())
		}
	}

	err := db.Exec(fmt.Sprintf("insert or ignore into %v ( uid ) values (?)", table), uid)
	handle_err("", err)
	for k, v := range obj.Map() {
		if k == "uid" {
			continue
		}
		// Are we a string or a number?
		if i, err := v.Int64(); err == nil {
			err = db.Exec(fmt.Sprintf("update %v set %v = ? where uid = ?", table, k), i, uid)
			handle_err(k, err)
		}
		if f, err := v.Float64(); err == nil {
			err = db.Exec(fmt.Sprintf("update %v set %v = ? where uid = ?", table, k), f, uid)
			handle_err(k, err)
		}
		if s, err := v.String(); err == nil {
			err = db.Exec(fmt.Sprintf("update %v set %v = ? where uid = ?", table, k), s, uid)
			handle_err(k, err)
		}
		if arr, err := obj.GetFloat64Array(k); err == nil {
			s := fmt.Sprintf("%v", arr)
			err = db.Exec(fmt.Sprintf("update %v set %v = ? where uid = ?", table, k), s, uid)
			handle_err(k, err)
		}
	}
	return err
}
Exemplo n.º 2
0
func (i *Item) Update(db *sqlite3.Conn) error {
	// update the Item with with user contribution (description)
	args := sqlite3.NamedArgs{"$d": i.Desc,
		"$n": i.Index,
		"$e": i.UserContributed,
		"$i": i.Id}
	return db.Exec(UPDATE_ITEM, args)
}
Exemplo n.º 3
0
func AddVendor(db *sqlite3.Conn, vendorId, vendorDisplayName string) (int64, error) {
	args := sqlite3.NamedArgs{"$v": vendorId,
		"$n": vendorDisplayName}
	result := db.Exec(ADD_VENDOR, args)
	if result == nil {
		pk := getPK(db, "vendor")
		return pk, result
	}
	return BAD_PK, result
}
Exemplo n.º 4
0
func (i *Item) Add(db *sqlite3.Conn, a *Account) (int64, error) {
	// insert the Item object

	// but first check if it's a duplicate or not
	itemPk := getExistingItem(db, i.Barcode, i.Desc)
	if itemPk != BAD_PK {
		return itemPk, nil
	}

	args := sqlite3.NamedArgs{"$b": i.Barcode,
		"$d": i.Desc,
		"$i": i.Index,
		"$e": i.UserContributed,
		"$a": a.Id}
	result := db.Exec(ADD_ITEM, args)
	if result == nil {
		pk := getPK(db, "product")
		return pk, result
	}

	return BAD_PK, result
}
Exemplo n.º 5
0
Arquivo: db.go Projeto: joseche/cagent
func create_table(tbname string, tbcreate string, conn *sqlite3.Conn) (created bool) {
	tbcount := -1
	created = false
	query := "SELECT count(*) as count FROM sqlite_master " +
		"WHERE type='table' AND name='" + tbname + "'"
	ret, err := conn.Query(query)
	if err != nil {
		Err("create_table error: '" + tbname + "': " + err.Error())
	} else {
		ret.Scan(&tbcount)
		ret.Close()
		if tbcount <= 0 {
			err := conn.Exec(tbcreate)
			if err != nil {
				panic("Can't create table: " + tbname + ", " + err.Error())
			} else {
				Debug(tbname + " created")
				created = true
			}
		}
	}
	return created
}
Exemplo n.º 6
0
func (a *Account) Update(db *sqlite3.Conn, newEmail, newApi string) error {
	// update this Account's email and API code
	args := sqlite3.NamedArgs{"$i": a.Id, "$e": newEmail, "$a": newApi}
	return db.Exec(UPDATE_ACCOUNT, args)
}
Exemplo n.º 7
0
func (a *Account) Add(db *sqlite3.Conn) error {
	// insert the Account object
	args := sqlite3.NamedArgs{"$e": a.Email, "$a": a.APICode}
	return db.Exec(ADD_ACCOUNT, args)
}
Exemplo n.º 8
0
func AddVendorProduct(db *sqlite3.Conn, productCode string, vendorId, itemId int64) error {
	args := sqlite3.NamedArgs{"$v": vendorId,
		"$p": productCode,
		"$i": itemId}
	return db.Exec(ADD_VENDOR_PRODUCT, args)
}
Exemplo n.º 9
0
func (i *Item) Unfavorite(db *sqlite3.Conn) error {
	// update the Item, to show it is not a favorite for this Account
	args := sqlite3.NamedArgs{"$i": i.Id}
	return db.Exec(UNFAVORITE_ITEM, args)
}
Exemplo n.º 10
0
func (i *Item) Delete(db *sqlite3.Conn) error {
	// delete the Item
	args := sqlite3.NamedArgs{"$i": i.Id}
	return db.Exec(DELETE_ITEM, args)
}