예제 #1
0
// get properties by product and properties' name
func GetProductProperties(propertyId int, propertiesName string) (values []string) {
	conn, _ := db.Connect()
	defer db.CloseConn(conn) // should use db.CloseConn or conn.Close()?

	stmt, err := conn.Prepare("select value from product_property where " +
		"product_id=? and property_name=? order by id asc")
	defer db.CloseStmt(stmt)
	if db.Err(err) {
		panic(err.Error())
		// should here be empty return?
	}

	rows, err := stmt.Query(propertyId, propertiesName)
	defer db.CloseRows(rows)
	if db.Err(err) {
		return nil
	}

	// big performance issue, maybe.
	for rows.Next() {
		var propertyValue string
		rows.Scan(&propertyValue)
		values = append(values, propertyValue)
	}
	return values
}
예제 #2
0
// private
func setProductCSValue(productId int, color string, size string,
	field string, stock int, price float64) {

	conn := db.Connectp()
	defer db.CloseConn(conn)

	_sql := fmt.Sprintf("insert into product_sku (product_id, color, size, %v) values (?,?,?,?) on duplicate key update %v = ?", field, field)

	stmt, err := conn.Prepare(_sql)
	defer db.CloseStmt(stmt) // the safe way to close.
	if db.Err(err) {
		panic(err.Error())
	}

	if field == "stock" {
		_, err := stmt.Exec(productId, color, size, stock, stock)
		if err != nil {
			debug.Error(err)
		}
	} else if field == "price" {
		_, err := stmt.Exec(productId, color, size, price, stock)
		if err != nil {
			debug.Error(err)
		}
	}
}