Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.idea/*
test.db
profile.cov
.vscode/
3 changes: 2 additions & 1 deletion examples/Concurrent-Inserts.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ func ConcurrentInserts() {
textHandler := slog.NewTextHandler(os.Stdout, nil)
l := slog.New(textHandler)

MySQL.New(DSN, l, context.Background())
// MySQL.New(DSN, l, context.Background())
MySQL.NewSQLite3(DSN, l, context.Background())

type InsertPerson struct {
Id int `db:"column=id primarykey=yes table=Test"`
Expand Down
33 changes: 32 additions & 1 deletion examples/Insert-DateDefault.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func InsertDateDefault() {
l := slog.New(textHandler)

// MySQL.New(DSN, l, context.Background())
MySQL.New(DSN, l, context.Background())
MySQL.NewSQLite3(DSN, l, context.Background())

type InsertPersonNOW struct {
Id int `db:"column=id primarykey=yes table=Test"`
Expand All @@ -49,6 +49,14 @@ func InsertDateDefault() {
Ignored int `db:"column=ignored omit=yes"`
}

type InsertPersonNULL struct {
Id int `db:"column=id primarykey=yes table=Test"`
Name string `db:"column=name"`
Dtadded time.Time `db:"column=dtadded readdefault=null"`
Status int `db:"column=status"`
Ignored int `db:"column=ignored omit=yes"`
}

sqlQuery := "INSERT INTO Test(name) VALUES (X'54657374');"

// Then execute the query
Expand Down Expand Up @@ -78,6 +86,29 @@ func InsertDateDefault() {
l.With("Date Should be DEFAULT", test3.Dtadded).Info("Date")
}

nullEntry := InsertPersonNULL{
Name: "Test",
Dtadded: time.Time{},
Status: 1,
}

nullInsertSQL, err6 := MySQL.DB.Insert(nullEntry)
if err6 != nil {
l.Error(err6.Error())
} else {
nullInsertedID, _, err7 := MySQL.DB.Execute(nullInsertSQL)
if err7 != nil {
l.Error(err7.Error())
} else {
test4, err8 := MySQL.QuerySingleStruct[InsertPersonNULL]("SELECT * FROM test WHERE id = ?", nullInsertedID)
if err8 != nil {
l.Error("Query error: " + err8.Error())
} else {
l.With("Date Should be NULL/zero", test4.Dtadded).Info("Date")
}
}
}

l.Info(fmt.Sprintf("Item with ID %d was inserted. %d rows were affected", lastInsertedID, rowsAffected))

ctx.Done()
Expand Down
3 changes: 2 additions & 1 deletion examples/Missing-Columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ func MissingColumns() {
textHandler := slog.NewTextHandler(os.Stdout, nil)
l := slog.New(textHandler)

MySQL.New(DSN, l, context.Background())
// MySQL.New(DSN, l, context.Background())
MySQL.NewSQLite3(DSN, l, context.Background())

type InsertPerson struct {
Id int `db:"column=id primarykey=yes table=Test"`
Expand Down
60 changes: 60 additions & 0 deletions examples/Save-DateDefault.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package examples

import (
"context"
"fmt"
"log/slog"
"os"
"time"

"github.com/joho/godotenv"

MySQL "github.com/SpaceTent/GoStructDB/gsdb"
)

func SaveReadDefaultNull() {

godotenv.Load(".env")
DSN := os.Getenv("DSN")
textHandler := slog.NewTextHandler(os.Stdout, nil)
l := slog.New(textHandler)

// MySQL.New(DSN, l, context.Background())
MySQL.NewSQLite3(DSN, l, context.Background())

type SavePerson struct {
Id int `db:"column=id primarykey=yes table=Test"`
Name string `db:"column=name"`
Dtadded time.Time `db:"column=dtadded readdefault=null"`
Status int `db:"column=status"`
}

entry := SavePerson{
Name: "Save Null",
Dtadded: time.Time{},
Status: 1,
}

lastInsertedID, rowsAffected, err := MySQL.DB.Save(entry, entry.Id)
if err != nil {
l.Error(err.Error())
return
}

loaded, err := MySQL.QuerySingleStruct[SavePerson]("SELECT * FROM test WHERE id = ?", lastInsertedID)
if err != nil {
l.Error("Query error: " + err.Error())
} else {
l.With("Date Should be NULL/zero", loaded.Dtadded).Info("Date")
}

loaded.Status = 2
loaded.Dtadded = time.Now()

_, rowsAffectedUpdate, err := MySQL.DB.Save(loaded, loaded.Id)
if err != nil {
l.Error(err.Error())
}

l.Info(fmt.Sprintf("Save insert rows: %d, save update rows: %d", rowsAffected, rowsAffectedUpdate))
}
1 change: 0 additions & 1 deletion examples/Save.go

This file was deleted.

3 changes: 2 additions & 1 deletion examples/basic-Insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ func BasicInsert() {
textHandler := slog.NewTextHandler(os.Stdout, nil)
l := slog.New(textHandler)

MySQL.New(DSN, l, context.Background())
// MySQL.New(DSN, l, context.Background())
MySQL.NewSQLite3(DSN, l, context.Background())

type InsertPerson struct {
Id int `db:"column=id primarykey=yes table=Test"`
Expand Down
3 changes: 2 additions & 1 deletion examples/basic-insert-withcounter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ func BasicInsertWithCounter() {
textHandler := slog.NewTextHandler(os.Stdout, nil)
l := slog.New(textHandler)

MySQL.New(DSN, l, context.Background())
// MySQL.New(DSN, l, context.Background())
MySQL.NewSQLite3(DSN, l, context.Background())

type InsertPerson struct {
Id int `db:"column=id primarykey=yes table=Test"`
Expand Down
6 changes: 2 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package main

import (
"github.com/SpaceTent/GoStructDB/examples"
)
import "github.com/SpaceTent/GoStructDB/examples"

func main() {

Expand All @@ -12,5 +10,5 @@ func main() {
examples.InsertDateDefault()
examples.MissingColumns()
examples.Lite3()

examples.SaveReadDefaultNull()
}
Loading