Skip to content

Commit a9013ca

Browse files
authored
Merge pull request #8 from darcys22/dev
0.2.0
2 parents 65368ec + 1a08583 commit a9013ca

File tree

15 files changed

+215
-161
lines changed

15 files changed

+215
-161
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,4 @@ SELECT * FROM accounts where account_id in (select account_id from account_tag w
7474
### TODO
7575
- Create Yurnell - programmable journal entries
7676
- Add an edit transaction function
77-
- Add MySQL as a database
78-
- test releases from scratch
7977
- run GoDBLedger on a separate server and access the open port through the network

godbledger/cmd/config.go

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type LedgerConfig struct {
2020
LogVerbosity string // LogVerbosity defines the logging level {debug, info, warn, error, fatal, panic}
2121
ConfigFile string // Location of the TOML config file, including directory path
2222
DatabaseType string // Type of Database being used
23-
DatabaseLocation string // Location of the database file, including directory path
23+
DatabaseLocation string // Location of the database file, including directory path or connection string
2424
}
2525

2626
var (
@@ -33,6 +33,21 @@ var (
3333
Description: `The dumpconfig command shows configuration values.`,
3434
}
3535

36+
InitConfigCommand = &cli.Command{
37+
Action: initConfig,
38+
Name: "init",
39+
Usage: "godbledger init [-m] [databaseLocation]",
40+
ArgsUsage: "",
41+
Category: "MISCELLANEOUS COMMANDS",
42+
Description: `The init command creates configuration file.`,
43+
Flags: []cli.Flag{
44+
&cli.BoolFlag{
45+
Name: "mysql",
46+
Aliases: []string{"m"},
47+
Usage: "set the database to use mysql rather than sqlite"},
48+
},
49+
}
50+
3651
defaultLedgerConfig = &LedgerConfig{
3752
RPCPort: "50051",
3853
DataDirectory: DefaultDataDir(),
@@ -128,3 +143,34 @@ func dumpConfig(ctx *cli.Context) error {
128143

129144
return nil
130145
}
146+
147+
// initConfig is the init command.
148+
func initConfig(ctx *cli.Context) error {
149+
150+
config := defaultLedgerConfig
151+
if ctx.Bool("mysql") {
152+
config.DatabaseType = "mysql"
153+
config.DatabaseLocation = "godbledger:password@tcp(127.0.0.1:3306)/ledger?charset=utf8"
154+
}
155+
156+
if len(ctx.Args().Get(0)) > 0 {
157+
config.DatabaseLocation = ctx.Args().Get(0)
158+
}
159+
_, err := os.Stat(config.ConfigFile)
160+
if os.IsNotExist(err) {
161+
log.Infof("Config File doesn't exist creating at %s", config.ConfigFile)
162+
os.MkdirAll(filepath.Dir(config.ConfigFile), os.ModePerm)
163+
buf := new(bytes.Buffer)
164+
if err := toml.NewEncoder(buf).Encode(config); err != nil {
165+
return err
166+
}
167+
dump, err := os.OpenFile(config.ConfigFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
168+
if err != nil {
169+
return err
170+
}
171+
defer dump.Close()
172+
dump.Write(buf.Bytes())
173+
}
174+
175+
return nil
176+
}

godbledger/core/trialbalance.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,8 @@ package core
22

33
import ()
44

5-
type Tag struct {
6-
Name string `json:"Name"`
7-
Total int `json:"Total"`
8-
Accounts []PDFAccount `json:"Accounts"`
9-
}
10-
11-
type PDFAccount struct {
12-
Account string `json:"Account"`
13-
Amount int `json:"Amount"`
14-
}
15-
16-
var Reporteroutput struct {
17-
Data []Tag `json:"Tags"`
18-
Profit int `json:"Profit"`
19-
NetAssets int `json:"NetAssets"`
5+
type TBAccount struct {
6+
Account string `json:"Account"`
7+
Amount int `json:"Amount"`
8+
Tags []string `json:"Tags"`
209
}

godbledger/db/database.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ type Database interface {
2727
FindUser(pubKey string) (*core.User, error)
2828
AddUser(usr *core.User) error
2929
SafeAddUser(usr *core.User) error
30-
GetTB(date time.Time) error
30+
GetTB(date time.Time) (*[]core.TBAccount, error)
3131
Query(query string, args ...interface{}) (*sql.Rows, error)
3232
}

godbledger/db/mysql/mysqlfuncs.go

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -427,63 +427,64 @@ func (db *Database) TestDB() error {
427427
return err
428428
}
429429

430-
func (db *Database) GetTB(date time.Time) error {
430+
func (db *Database) GetTB(queryDate time.Time) (*[]core.TBAccount, error) {
431431

432432
queryDB := `
433-
SELECT
434-
tags.tag_name,
435-
Table_Aggregate.account_id,
436-
sums
437-
FROM account_tag
438-
join ((SELECT
439-
split_accounts.account_id as account_id,
440-
SUM(splits.amount) as sums
441-
FROM splits
442-
JOIN split_accounts
443-
ON splits.split_id = split_accounts.split_id
444-
GROUP BY split_accounts.account_id
445-
446-
)) AS Table_Aggregate
447-
on account_tag.account_id = Table_Aggregate.account_id
448-
join tags
449-
on tags.tag_id = account_tag.tag_id
450-
order BY tags.tag_name
433+
SELECT
434+
split_accounts.account_id,
435+
SUM(splits.amount)
436+
FROM splits
437+
JOIN split_accounts
438+
ON splits.split_id = split_accounts.split_id
439+
WHERE splits.split_date <= ?
440+
GROUP BY split_accounts.account_id
451441
;`
452442

453-
rows, err := db.DB.Query(queryDB)
443+
log.Debug("Querying Database for Trial Balance")
444+
445+
rows, err := db.DB.Query(queryDB, queryDate)
454446
if err != nil {
455-
log.Debug(err)
456-
return err
447+
log.Fatal(err)
457448
}
458449
defer rows.Close()
459450

460-
accounts := make(map[string][]*core.PDFAccount)
461-
totals := make(map[string]int)
451+
accounts := []core.TBAccount{}
462452

463453
for rows.Next() {
464-
var t *core.PDFAccount
465-
var name string
466-
if err := rows.Scan(&name, &t.Account, &t.Amount); err != nil {
454+
var t core.TBAccount
455+
if err := rows.Scan(&t.Account, &t.Amount); err != nil {
467456
log.Fatal(err)
468457
}
469-
log.Debugf("%v", t)
470-
if val, ok := accounts[name]; ok {
471-
accounts[name] = append(val, t)
472-
totals[name] = totals[name] + t.Amount
473-
} else {
474-
accounts[name] = []*core.PDFAccount{t}
475-
totals[name] = t.Amount
476-
}
458+
accounts = append(accounts, t)
477459
}
478460
if rows.Err() != nil {
479461
log.Fatal(err)
480462
}
481463

482-
//for k, v := range accounts {
483-
//reporteroutput.Data = append(reporteroutput.Data, Tag{k, totals[k], v})
484-
//}
464+
tagsQuery := `
465+
SELECT tag_name
466+
FROM tags
467+
JOIN account_tag
468+
ON account_tag.tag_id = tags.tag_id
469+
JOIN accounts
470+
ON accounts.account_id = account_tag.account_id
471+
WHERE accounts.NAME = ?;
472+
`
473+
474+
for index, element := range accounts {
475+
rows, err = db.DB.Query(tagsQuery, element.Account)
476+
477+
for rows.Next() {
478+
var tag string
479+
if err := rows.Scan(&tag); err != nil {
480+
log.Fatal(err)
481+
}
482+
accounts[index].Tags = append(accounts[index].Tags, tag)
483+
}
485484

486-
return nil
485+
}
486+
487+
return &accounts, nil
487488

488489
}
489490

godbledger/db/sqlite3/sqlite3funcs.go

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -427,66 +427,68 @@ func (db *Database) TestDB() error {
427427
return err
428428
}
429429

430-
func (db *Database) GetTB(date time.Time) error {
430+
func (db *Database) GetTB(queryDate time.Time) (*[]core.TBAccount, error) {
431431

432432
queryDB := `
433-
SELECT
434-
tags.tag_name,
435-
Table_Aggregate.account_id,
436-
sums
437-
FROM account_tag
438-
join ((SELECT
439-
split_accounts.account_id as account_id,
440-
SUM(splits.amount) as sums
441-
FROM splits
442-
JOIN split_accounts
443-
ON splits.split_id = split_accounts.split_id
444-
GROUP BY split_accounts.account_id
445-
446-
)) AS Table_Aggregate
447-
on account_tag.account_id = Table_Aggregate.account_id
448-
join tags
449-
on tags.tag_id = account_tag.tag_id
450-
order BY tags.tag_name
433+
SELECT
434+
split_accounts.account_id,
435+
SUM(splits.amount)
436+
FROM splits
437+
JOIN split_accounts
438+
ON splits.split_id = split_accounts.split_id
439+
WHERE splits.split_date <= ?
440+
GROUP BY split_accounts.account_id
451441
;`
452442

453-
rows, err := db.DB.Query(queryDB)
443+
log.Debug("Querying Database for Trial Balance")
444+
445+
rows, err := db.DB.Query(queryDB, queryDate)
454446
if err != nil {
455-
log.Debug(err)
456-
return err
447+
log.Fatal(err)
457448
}
458449
defer rows.Close()
459450

460-
accounts := make(map[string][]*core.PDFAccount)
461-
totals := make(map[string]int)
451+
accounts := []core.TBAccount{}
462452

463453
for rows.Next() {
464-
var t *core.PDFAccount
465-
var name string
466-
if err := rows.Scan(&name, &t.Account, &t.Amount); err != nil {
454+
var t core.TBAccount
455+
if err := rows.Scan(&t.Account, &t.Amount); err != nil {
467456
log.Fatal(err)
468457
}
469-
log.Debugf("%v", t)
470-
if val, ok := accounts[name]; ok {
471-
accounts[name] = append(val, t)
472-
totals[name] = totals[name] + t.Amount
473-
} else {
474-
accounts[name] = []*core.PDFAccount{t}
475-
totals[name] = t.Amount
476-
}
458+
accounts = append(accounts, t)
477459
}
478460
if rows.Err() != nil {
479461
log.Fatal(err)
480462
}
481463

482-
//for k, v := range accounts {
483-
////reporteroutput.Data = append(reporteroutput.Data, Tag{k, totals[k], v})
484-
//}
464+
tagsQuery := `
465+
SELECT tag_name
466+
FROM tags
467+
JOIN account_tag
468+
ON account_tag.tag_id = tags.tag_id
469+
JOIN accounts
470+
ON accounts.account_id = account_tag.account_id
471+
WHERE accounts.NAME = ?;
472+
`
473+
474+
for index, element := range accounts {
475+
rows, err = db.DB.Query(tagsQuery, element.Account)
476+
477+
for rows.Next() {
478+
var tag string
479+
if err := rows.Scan(&tag); err != nil {
480+
log.Fatal(err)
481+
}
482+
accounts[index].Tags = append(accounts[index].Tags, tag)
483+
}
484+
485+
}
486+
487+
return &accounts, nil
485488

486-
return nil
487489
}
488490

489491
func (db *Database) Query(query string, args ...interface{}) (*sql.Rows, error) {
490-
return db.Query(query, args...)
492+
return db.DB.Query(query, args...)
491493

492494
}

godbledger/ledger/ledger.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,8 @@ func (l *Ledger) GetAccounts(txn *core.Transaction) ([]*core.Account, error) {
147147
return accounts, nil
148148
}
149149

150-
func (l *Ledger) GetTB(date time.Time) (int, error) {
151-
//accounts := []*core.Account{}
152-
153-
return 1, nil
150+
func (l *Ledger) GetTB(date time.Time) (*[]core.TBAccount, error) {
151+
return l.LedgerDb.GetTB(date)
154152
}
155153

156154
func (l *Ledger) Start() {

godbledger/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func main() {
5151
app.Commands = []*cli.Command{
5252
// See config.go
5353
cmd.DumpConfigCommand,
54+
cmd.InitConfigCommand,
5455
}
5556

5657
app.Flags = []cli.Flag{

godbledger/rpc/ledger_server.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,20 @@ func (s *LedgerServer) DeleteTag(ctx context.Context, in *pb.DeleteTagRequest) (
9595

9696
func (s *LedgerServer) GetTB(ctx context.Context, in *pb.TBRequest) (*pb.TBResponse, error) {
9797
log.Info("Received New TB Request")
98-
s.ld.GetTB(time.Now())
98+
accounts, err := s.ld.GetTB(time.Now())
9999

100-
return &pb.TBResponse{}, nil
100+
//log.Debug(accounts)
101+
102+
response := pb.TBResponse{}
103+
104+
for _, account := range *accounts {
105+
response.Lines = append(response.Lines,
106+
&pb.TBLine{
107+
Accountname: account.Account,
108+
Amount: int64(account.Amount),
109+
Tags: account.Tags,
110+
})
111+
}
112+
113+
return &response, err
101114
}

godbledger/version/version.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ import (
2121
)
2222

2323
const (
24-
VersionMajor = 0 // Major version component of the current release
25-
VersionMinor = 1 // Minor version component of the current release
26-
VersionPatch = 0 // Patch version component of the current release
27-
VersionMeta = "unstable" // Version metadata to append to the version string
24+
VersionMajor = 0 // Major version component of the current release
25+
VersionMinor = 2 // Minor version component of the current release
26+
VersionPatch = 0 // Patch version component of the current release
27+
VersionMeta = "alpha" // Version metadata to append to the version string
2828
)
2929

3030
// Version holds the textual version string.

0 commit comments

Comments
 (0)