having some trboule with the insert

This commit is contained in:
Nickiel12 2024-01-01 22:18:43 -08:00
parent a5f462d782
commit d5fc3d8d23
3 changed files with 95 additions and 11 deletions

8
db.go
View file

@ -23,8 +23,8 @@ func db_get_transactions(transactions *[]Transaction,r *GetTransactionPagination
"SELECT trns_id, trns_amount, trns_description, " +
"trns_account, trns_bucket, trns_date " +
fmt.Sprintf("FROM %stransactions ORDER BY trns_id DESC ", DB_SCHEMA) +
fmt.Sprintf("OFFSET %d ROWS FETCH NEXT %d ROWS ONLY",
r.PageNum, r.ResultCount))
fmt.Sprintf("LIMIT %d OFFSET %d",
r.ResultCount, r.PageNum * r.ResultCount ))
if err != nil {
return err
}
@ -42,7 +42,9 @@ func db_new_transaction(transaction Transaction) (error) {
defer db.Close()
_, err = db.NamedQuery(
log.Debug().Msgf("%#v", transaction)
_, err = db.NamedExec(
fmt.Sprintf("INSERT INTO %stransactions", DB_SCHEMA) +
"(trns_amount, trns_description, trns_account, trns_bucket, trns_date)" +
"VALUES (:trns_amount, :trns_description, :trns_account, :trns_bucket, :trns_date)",

20
main.go
View file

@ -8,7 +8,7 @@ import (
"flag"
"fmt"
"os"
"os"
"time"
//"github.com/shopspring/decimal"
@ -50,11 +50,20 @@ func main() {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
var nFlag = flag.Bool("d", false, "whether to enable debug mode")
var debugFlag = flag.Bool("d", false, "whether to enable debug mode")
var traceFlag = flag.Bool("t", false, "whether to trace logging")
flag.Parse()
if *nFlag {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
if *traceFlag {
zerolog.SetGlobalLevel(zerolog.TraceLevel)
log.Debug().Msg("Enabling trace level debugging")
}
if *debugFlag {
if !*traceFlag {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
log.Debug().Msg("Is debugging")
DB_TYPE = "sqlite3"
DB_SCHEMA = ""
@ -62,6 +71,9 @@ func main() {
debug_mode.Init_testdb(DB_TYPE, DB_CONNECTION_STRING)
}
debug_mode.SetLogLevel(zerolog.GlobalLevel())
log.Info().Msg("starting server")
r := chi.NewRouter()

View file

@ -1,13 +1,52 @@
package debug_mode
import (
_ "github.com/mattn/go-sqlite3"
"github.com/jmoiron/sqlx"
"os"
"encoding/json"
"database/sql"
"time"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
type Transaction struct {
Id int `db:"trns_id" json:"Id"`
Amount string `db:"trns_amount" json:"Amount"`
Description sql.NullString `db:"trns_description" json:"Description"`
Account int `db:"trns_account" json:"Account"`
Bucket sql.NullInt64 `db:"trns_bucket" json:"Bucket"`
Date time.Time `db:"trns_date" json:"TransactionDate"`
}
func SetLogLevel(level zerolog.Level) {
zerolog.SetGlobalLevel(level)
}
func Init_testdb(DB_TYPE string, DB_CONNECTION_STRING string) {
cwd, err := os.Getwd()
if err != nil {
log.Fatal().Err(err).Msg("Could not get current working directory")
} else {
log.Trace().Msgf("Currect working directory is: %s", cwd)
}
_, err = os.Stat(cwd + DB_CONNECTION_STRING)
if err != nil {
log.Debug().Msg("Found existing test.db file. Attempting to delete")
err = os.Remove(DB_CONNECTION_STRING)
if err != nil {
log.Fatal().Err(err).Msg("Failed to delete testing db")
} else {
log.Debug().Msg("Deleted test.db file successfully")
}
} else {
log.Debug().Msg("No existing test.db file found")
}
db, err := sqlx.Connect(DB_TYPE, DB_CONNECTION_STRING)
if err != nil {
log.Fatal().
@ -18,7 +57,6 @@ func Init_testdb(DB_TYPE string, DB_CONNECTION_STRING string) {
defer db.Close()
init_sql := `
CREATE TABLE accounts (
acnt_id Integer PRIMARY KEY,
acnt_dsply_name varchar(50) NOT NULL,
@ -71,6 +109,11 @@ CREATE TABLE transaction_categories (
trns_ctgry_description varchar(250) NULL
);
INSERT INTO accounts (acnt_dsply_name, acnt_description) VALUES ("BECU Saving", "Savings Account");
INSERT INTO buckets (bkt_dsply_code, bkt_dsply_name, bkt_description) VALUES
("SVNGS", "Savings", "Savings Bucket");
INSERT INTO transactions (trns_amount, trns_description, trns_account, trns_bucket, trns_date) VALUES
("50.00", "Money", 1, 1, "2023-11-10");
`
tx := db.MustBegin()
@ -84,6 +127,33 @@ CREATE TABLE transaction_categories (
Msg("Could not commit transaction")
}
jsonExample := `{
"Id": 3,
"Amount": "100",
"Description": {
"String": "Transaction 3",
"Valid": true
},
"Account": 1,
"Bucket": {
"Int64": 1,
"Valid": true
},
"TransactionDate": "2023-11-11T00:00:00Z"
}`
var trns Transaction = Transaction{}
err = json.Unmarshal([]byte(jsonExample), &trns)
if err != nil {
log.Fatal().Err(err).Msg("could not unmarshal")
}
_, err = db.NamedExec("INSERT INTO transactions" +
"(trns_amount, trns_description, trns_account, trns_bucket, trns_date)" +
"VALUES (:trns_amount, :trns_description, :trns_account, :trns_bucket, :trns_date)",
trns)
log.Debug().Msg("Test database initialized")