<commit_before>// Copyright Banrai LLC. All rights reserved. Use of this source code is
// governed by the license that can be found in the LICENSE file.

package database

import (
	"github.com/mxk/go-sqlite/sqlite3"
	"io/ioutil"
	"path"
	"strings"
)

const (
	// Anonymous Account
	ANONYMOUS_EMAIL    = "<EMAIL>"
	ANONYMOUS_API_CODE = "123e4567-e89b-12d3-a456-426655440000"

	// Prepared Statements
	// User accounts
	ADD_ACCOUNT  = "insert into account (email, api_code) values ($e, $a)"
	GET_ACCOUNT  = "select id, api_code from account where email = $e"
	GET_ACCOUNTS = "select id, email, api_code from account"

	// Products
	ADD_ITEM           = "insert into product (barcode, product_desc, product_ind, posted, account) values ($b, $d, $i, strftime('%s','now'), $a)"
	GET_ITEMS          = "select id, barcode, product_desc, product_ind, datetime(posted) from product where account = $a"
	GET_FAVORITE_ITEMS = "select id, barcode, product_desc, product_ind, datetime(posted) from product where is_favorite = 1 and account = $a"
	DELETE_ITEM        = "delete from product where id = $i"
	FAVORITE_ITEM      = "update product set is_favorite = 1 where id = $i"
	UNFAVORITE_ITEM    = "update product set is_favorite = 0 where id = $i"
)

type Item struct {
	Id      int64
	Desc    string
	Barcode string
	Index   int64
	Since   string
}

func (i *Item) Add(db *sqlite3.Conn, a *Account) error {
	// insert the Item object
	args := sqlite3.NamedArgs{"$b": i.Barcode,
		"$d": i.Desc,
		"$i": i.Index,
		"$a": a.Id}
	return db.Exec(ADD_ITEM, args)
}

func GetItems(db *sqlite3.Conn, a *Account) ([]*Item, error) {
	// find all the items for this account
	results := make([]*Item, 0)

	args := sqlite3.NamedArgs{"$a": a.Id}
	row := make(sqlite3.RowMap)
	for s, err := db.Query(GET_ITEMS, args); err == nil; err = s.Next() {
		var rowid int64
		s.Scan(&rowid, row)

		barcode, barcodeFound := row["barcode"]
		desc, descFound := row["product_desc"]
		ind, indFound := row["product_ind"]
		since, sinceFound := row["posted"]
		if barcodeFound {
			result := new(Item)
			result.Id = rowid
			result.Barcode = barcode.(string)
			if descFound {
				result.Desc = desc.(string)
			}
			if indFound {
				result.Index = ind.(int64)
			}
			if sinceFound {
				result.Since = since.(string)
			}
			results = append(results, result)
		}
	}

	return results, nil
}

type Account struct {
	Id      int64
	Email   string
	APICode string
}

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)
}

func GetAccount(db *sqlite3.Conn, email string) (*Account, error) {
	// get the account corresponding to this email
	result := new(Account)
<commit_msg>More Account + Item functions: update, favorite, delete<commit_after>// Copyright Banrai LLC. All rights reserved. Use of this source code is
// governed by the license that can be found in the LICENSE file.

package database

import (
	"github.com/mxk/go-sqlite/sqlite3"
	"io/ioutil"
	"path"
	"strings"
)

const (
	// Anonymous Account
	ANONYMOUS_EMAIL    = "<EMAIL>"
	ANONYMOUS_API_CODE = "<KEY>"

	// Prepared Statements
	// User accounts
	ADD_ACCOUNT    = "insert into account (email, api_code) values ($e, $a)"
	GET_ACCOUNT    = "select id, api_code from account where email = $e"
	GET_ACCOUNTS   = "select id, email, api_code from account"
	UPDATE_ACCOUNT = "update account set email = $e, api_code = $a where id = $i"

	// Products
	ADD_ITEM           = "insert into product (barcode, product_desc, product_ind, posted, account) values ($b, $d, $i, strftime('%s','now'), $a)"
	GET_ITEMS          = "select id, barcode, product_desc, product_ind, datetime(posted) from product where account = $a"
	GET_FAVORITE_ITEMS = "select id, barcode, product_desc, product_ind, datetime(posted) from product where is_favorite = 1 and account = $a"
	DELETE_ITEM        = "delete from product where id = $i"
	FAVORITE_ITEM      = "update product set is_favorite = 1 where id = $i"
	UNFAVORITE_ITEM    = "update product set is_favorite = 0 where id = $i"
)

type Item struct {
	Id      int64
	Desc    string
	Barcode string
	Index   int64
	Since   string
}

func (i *Item) Add(db *sqlite3.Conn, a *Account) error {
	// insert the Item object
	args := sqlite3.NamedArgs{"$b": i.Barcode,
		"$d": i.Desc,
		"$i": i.Index,
		"$a": a.Id}
	return db.Exec(ADD_ITEM, args)
}

func (i *Item) Delete(db *sqlite3.Conn) error {
	// delete the Item
	args := sqlite3.NamedArgs{"$i": i.Id}
	return db.Exec(DELETE_ITEM, args)
}

func (i *Item) Favorite(db *sqlite3.Conn) error {
	// update the Item, to show it is a favorite for this Account
	args := sqlite3.NamedArgs{"$i": i.Id}
	return db.Exec(FAVORITE_ITEM, args)
}

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)
}

func GetItems(db *sqlite3.Conn, a *Account) ([]*Item, error) {
	// find all the items for this account
	results := make([]*Item, 0)

	args := sqlite3.NamedArgs{"$a": a.Id}
	row := make(sqlite3.RowMap)
	for s, err := db.Query(GET_ITEMS, args); err == nil; err = s.Next() {
		var rowid int64
		s.Scan(&rowid, row)

		barcode, barcodeFound := row["barcode"]
		desc, descFound := row["product_desc"]
		ind, indFound := row["product_ind"]
		since, sinceFound := row["posted"]
		if barcodeFound {
			result := new(Item)
			result.Id = rowid
			result.Barcode = barcode.(string)
			if descFound {
				result.Desc = desc.(string)
			}
			if indFound {
				result.Index = ind.(int64)
			}
			if sinceFound {
				result.Since = since.(string)
			}
			results = append(results, result)
		}
	}

	return results, nil
}

type Account struct {
	Id      int64
	Email   string
	APICode string
}

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)
}

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)
}

func GetAccount(db *sqlite3.Conn, email string) (*Account, error) {
	// get the account corresponding to this email
	result := new(Account)

{'fraction_non_alphanumeric': 0.13307210031347963, 'fraction_numerical': 0.012695924764890282, 'mean_word_length': 3.623913043478261, 'pattern_counts': {'":': 20, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7292160', 'n_tokens_mistral': 2434, 'n_tokens_neox': 2288, 'n_words': 687}