-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbaccount.go
90 lines (83 loc) · 2.21 KB
/
dbaccount.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
)
type AccountType int
const (
BankAccount AccountType = iota
StockAccount
)
type Account struct {
Accountid int64 `json:"accountid"`
Code string `json:"code"`
Name string `json:"name"`
AccountType AccountType `json:"int"`
Currencyid int64 `json:"currencyid"`
}
func createAccount(db *sql.DB, a *Account) (int64, error) {
s := "INSERT INTO account (code, name, accounttype, currency_id) VALUES (?, ?, ?, ?)"
result, err := sqlexec(db, s, a.Code, a.Name, a.AccountType, a.Currencyid)
if err != nil {
return 0, err
}
id, err := result.LastInsertId()
if err != nil {
return 0, err
}
return id, nil
}
func editAccount(db *sql.DB, a *Account) error {
s := "UPDATE account SET code = ?, name = ?, accounttype = ?, currency_id = ? WHERE account_id = ?"
_, err := sqlexec(db, s, a.Code, a.Name, a.AccountType, a.Currencyid, a.Accountid)
if err != nil {
return err
}
return nil
}
func delAccount(db *sql.DB, accountid int64) error {
s := "DELETE FROM account WHERE account_id = ?"
_, err := sqlexec(db, s, accountid)
if err != nil {
return err
}
return nil
}
func findAccount(db *sql.DB, accountid int64) (*Account, error) {
s := "SELECT account_id, code, name, accounttype, currency_id FROM account WHERE account_id = ?"
row := db.QueryRow(s, accountid)
var a Account
err := row.Scan(&a.Accountid, &a.Code, &a.Name, &a.AccountType, &a.Currencyid)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, err
}
return &a, nil
}
func findAccounts(db *sql.DB, swhere string) ([]*Account, error) {
s := fmt.Sprintf("SELECT account_id, code, name, accounttype, currency_id FROM account WHERE %s", swhere)
rows, err := db.Query(s)
if err != nil {
return nil, err
}
aa := []*Account{}
for rows.Next() {
var a Account
rows.Scan(&a.Accountid, &a.Code, &a.Name, &a.AccountType, &a.Currencyid)
aa = append(aa, &a)
}
return aa, nil
}
func balAccount(db *sql.DB, accountid int64) float64 {
s := "SELECT IFNULL(SUM(amt), 0.0) FROM trans WHERE account_id = ?"
row := db.QueryRow(s, accountid)
var bal float64
err := row.Scan(&bal)
if err != nil {
return 0.0
}
return bal
}