Skip to content

Commit

Permalink
updated reporter to use a generic query rather than specific for sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
darcys22 committed Apr 19, 2020
1 parent 82128f6 commit cb40189
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 53 deletions.
2 changes: 2 additions & 0 deletions godbledger/db/database.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package db

import (
"database/sql"
"github.com/darcys22/godbledger/godbledger/core"
"time"
)
Expand All @@ -27,4 +28,5 @@ type Database interface {
AddUser(usr *core.User) error
SafeAddUser(usr *core.User) error
GetTB(date time.Time) error
Query(query string, args ...interface{}) (*sql.Rows, error)
}
12 changes: 7 additions & 5 deletions godbledger/db/mysql/mysqldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ func DSN(DB_USER, DB_PASS, DB_HOST, DB_NAME string) string {
}

// NewDB initializes a new DB.
//TODO(Sean): this should actually use the connection_string rather than hardcoded
func NewDB(connection_string string) (*Database, error) {
//if connection_string == "" {
DB_HOST := "tcp(127.0.0.1:3306)"
DB_NAME := "ledger"
DB_USER := "godbledger"
DB_PASS := "password"
connection_string = DSN(DB_USER, DB_PASS, DB_HOST, DB_NAME)
//DB_HOST := "tcp(127.0.0.1:3306)"
//DB_NAME := "ledger"
//DB_USER := "godbledger"
//DB_PASS := "password"
//connection_string = DSN(DB_USER, DB_PASS, DB_HOST, DB_NAME)
//}
log.Debug(connection_string)
MySQLDB, err := sql.Open("mysql", connection_string)
if err != nil {
log.Fatal(err.Error)
Expand Down
6 changes: 6 additions & 0 deletions godbledger/db/mysql/mysqlfuncs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mysqldb

import (
"database/sql"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -485,3 +486,8 @@ func (db *Database) GetTB(date time.Time) error {
return nil

}

func (db *Database) Query(query string, args ...interface{}) (*sql.Rows, error) {
return db.DB.Query(query, args...)

}
6 changes: 6 additions & 0 deletions godbledger/db/sqlite3/sqlite3funcs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sqlite3db

import (
"database/sql"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -484,3 +485,8 @@ func (db *Database) GetTB(date time.Time) error {

return nil
}

func (db *Database) Query(query string, args ...interface{}) (*sql.Rows, error) {
return db.Query(query, args...)

}
40 changes: 20 additions & 20 deletions godbledger/ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,41 @@ const ledgerDBName = "ledgerdata"
var log = logrus.WithField("prefix", "ledger")

type Ledger struct {
ledgerDb db.Database
config *cmd.LedgerConfig
LedgerDb db.Database
Config *cmd.LedgerConfig
}

func New(ctx *cli.Context, cfg *cmd.LedgerConfig) (*Ledger, error) {

ledger := &Ledger{
config: cfg,
Config: cfg,
}

switch strings.ToLower(cfg.DatabaseType) {
case "sqlite3":

log.Info("Using Sqlite3")
log.Debug("Using Sqlite3")
dbPath := path.Join(cfg.DataDirectory, ledgerDBName)
log.WithField("path", dbPath).Info("Checking db path")
log.WithField("path", dbPath).Debug("Checking db path")
if ctx.Bool(cmd.ClearDB.Name) {
if err := sqlite3db.ClearDB(dbPath); err != nil {
return nil, err
}
}
ledgerdb, err := sqlite3db.NewDB(dbPath)
ledger.ledgerDb = ledgerdb
ledger.LedgerDb = ledgerdb
if err != nil {
return nil, err
}
case "mysql":
log.Info("Using MySQL")
ledgerdb, err := mysqldb.NewDB(ledgerDBName)
log.Debug("Using MySQL")
ledgerdb, err := mysqldb.NewDB(cfg.DatabaseLocation)
//if ctx.Bool(cmd.ClearDB.Name) {
//if err := ledgerdb.ClearDB(ledgerDBName); err != nil {
//return nil, err
//}
//}
ledger.ledgerDb = ledgerdb
ledger.LedgerDb = ledgerdb
if err != nil {
return nil, err
}
Expand All @@ -65,40 +65,40 @@ func New(ctx *cli.Context, cfg *cmd.LedgerConfig) (*Ledger, error) {
log.Println("No implementation available for that database.")
}

log.Info("Initialised database configuration")
log.Debug("Initialised database configuration")

return ledger, nil
}

func (l *Ledger) Insert(txn *core.Transaction) {
log.Info("Created Transaction: %s", txn)
l.ledgerDb.SafeAddUser(txn.Poster)
l.LedgerDb.SafeAddUser(txn.Poster)
currencies, _ := l.GetCurrencies(txn)
for _, currency := range currencies {
l.ledgerDb.SafeAddCurrency(currency)
l.LedgerDb.SafeAddCurrency(currency)
}
accounts, _ := l.GetAccounts(txn)

for _, account := range accounts {
l.ledgerDb.SafeAddAccount(account)
l.ledgerDb.SafeAddTagToAccount(account.Name, "main")
l.LedgerDb.SafeAddAccount(account)
l.LedgerDb.SafeAddTagToAccount(account.Name, "main")
}
l.ledgerDb.AddTransaction(txn)
l.LedgerDb.AddTransaction(txn)
}

func (l *Ledger) Delete(txnID string) {
log.Infof("Deleting Transaction: %s", txnID)
l.ledgerDb.DeleteTransaction(txnID)
l.LedgerDb.DeleteTransaction(txnID)
}

func (l *Ledger) InsertTag(account, tag string) error {
log.Infof("Creating Tag %s on %s", tag, account)
return l.ledgerDb.SafeAddTagToAccount(account, tag)
return l.LedgerDb.SafeAddTagToAccount(account, tag)
}

func (l *Ledger) DeleteTag(account, tag string) error {
log.Infof("Deleting Tag %s from %s", tag, account)
return l.ledgerDb.DeleteTagFromAccount(account, tag)
return l.LedgerDb.DeleteTagFromAccount(account, tag)
}

func (l *Ledger) GetCurrencies(txn *core.Transaction) ([]*core.Currency, error) {
Expand Down Expand Up @@ -154,11 +154,11 @@ func (l *Ledger) GetTB(date time.Time) (int, error) {
}

func (l *Ledger) Start() {
l.ledgerDb.InitDB()
l.LedgerDb.InitDB()
}

func (l *Ledger) Stop() error {
err := l.ledgerDb.Close()
err := l.LedgerDb.Close()
return err
}

Expand Down
25 changes: 14 additions & 11 deletions reporter/transactionlisting.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"fmt"
"os"

"database/sql"
//"database/sql"
"encoding/csv"
_ "github.com/mattn/go-sqlite3"

"github.com/darcys22/godbledger/godbledger/cmd"
"github.com/darcys22/godbledger/godbledger/ledger"

"github.com/olekukonko/tablewriter"
"github.com/urfave/cli"
Expand Down Expand Up @@ -51,14 +51,15 @@ If you want to see all the transactions in the database, or export to CSV
if databasefilepath == "" {
databasefilepath = cfg.DatabaseLocation
}
if _, err := os.Stat(databasefilepath); err != nil {
panic(fmt.Sprintf("Database does not already exist at %s.", databasefilepath))
}

SqliteDB, err := sql.Open("sqlite3", databasefilepath)
if err != nil {
log.Fatal(err)
}
ledger, err := ledger.New(ctx, cfg)
//if _, err := os.Stat(databasefilepath); err != nil {
//panic(fmt.Sprintf("Database does not already exist at %s.", databasefilepath))
//}

//SqliteDB, err := sql.Open("sqlite3", databasefilepath)
//if err != nil {
//log.Fatal(err)
//}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Date", "ID", "Account", "Description", "Currency", "Amount"})
table.SetBorder(false)
Expand All @@ -78,7 +79,9 @@ If you want to see all the transactions in the database, or export to CSV
on splits.transaction_id = transactions.transaction_id
;`

rows, err := SqliteDB.Query(queryDB)
log.Debug("Querying Database")
rows, err := ledger.LedgerDb.Query(queryDB)

if err != nil {
log.Fatal(err)
}
Expand Down
37 changes: 20 additions & 17 deletions reporter/trialbalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"fmt"
"os"

"database/sql"
//"database/sql"
"encoding/csv"
_ "github.com/mattn/go-sqlite3"

"github.com/darcys22/godbledger/godbledger/cmd"
"github.com/darcys22/godbledger/godbledger/ledger"

"github.com/olekukonko/tablewriter"
"github.com/urfave/cli"
Expand Down Expand Up @@ -46,29 +46,32 @@ If you want to see all the transactions in the database, or export to CSV
if databasefilepath == "" {
databasefilepath = cfg.DatabaseLocation
}
if _, err := os.Stat(databasefilepath); err != nil {
panic(fmt.Sprintf("Database does not already exist at %s.", databasefilepath))
}

DB, err := sql.Open(cfg.DatabaseType, databasefilepath)
if err != nil {
log.Fatal(err)
}
ledger, err := ledger.New(ctx, cfg)
//if _, err := os.Stat(databasefilepath); err != nil {
//panic(fmt.Sprintf("Database does not already exist at %s.", databasefilepath))
//}

//DB, err := sql.Open(cfg.DatabaseType, databasefilepath)
//if err != nil {
//log.Fatal(err)
//}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Account", "Balance"})
table.SetBorder(false)

queryDB := `
SELECT
split_accounts.account_id,
SUM(splits.amount)
FROM splits
JOIN split_accounts
ON splits.split_id = split_accounts.split_id
GROUP BY split_accounts.account_id
SELECT
split_accounts.account_id,
SUM(splits.amount)
FROM splits
JOIN split_accounts
ON splits.split_id = split_accounts.split_id
GROUP BY split_accounts.account_id
;`

rows, err := DB.Query(queryDB)
log.Debug("Querying Database")
rows, err := ledger.LedgerDb.Query(queryDB)
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit cb40189

Please sign in to comment.