10 Commits
Author SHA1 Message Date
gdulai d10d0f8414 Count exec & Select limit/offset
Go Tests / test (push) Successful in 1m16s
2026-06-04 11:08:14 +02:00
gdulai 0cf4e63ac3 Correct build step
Go Tests / test (push) Successful in 1m4s
2026-06-02 22:30:23 +02:00
gdulai 357b765eb7 Add build step
Go Tests / test (push) Failing after 1m2s
2026-06-02 22:28:21 +02:00
gdulai 2c51bba6e4 Add go install to ci/cd
Go Tests / test (push) Successful in 1m4s
2026-06-02 22:23:33 +02:00
gdulai 82dd07ab59 Add go install to ci/cd
Go Tests / test (push) Failing after 2s
2026-06-02 22:21:54 +02:00
gdulai f3c5fb5fe6 Fix failing ci/cd setup
Go Tests / test (push) Failing after 2s
2026-06-02 22:14:44 +02:00
gdulai f19c82a2ad Fix failing ci/cd setup
Debug / test (push) Successful in 2s
2026-06-02 22:10:03 +02:00
gdulai 3e706bada5 Fix failing ci/cd setup
Go Tests / test (push) Failing after 2s
2026-06-02 22:06:53 +02:00
gdulai 91966fcf5a Actionless ci/cd setup
Go Tests / test (push) Failing after 1s
2026-06-02 22:04:53 +02:00
gdulai 7fff1f4d3d Correct insert/returning ddl
Go Tests / test (push) Failing after 1m33s
2026-05-24 21:36:56 +02:00
4 changed files with 270 additions and 23 deletions
+27 -15
View File
@@ -10,24 +10,36 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Checkout code - name: Checkout repository
uses: actions/checkout@v4 run: |
git clone \
"${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git" \
.
- name: Setup Go git checkout "${GITHUB_SHA}"
uses: actions/setup-go@v5
with:
go-version: "1.22"
- name: Cache Go modules - name: Verify checkout
uses: actions/cache@v4 run: |
with: pwd
path: | ls -la
~/.cache/go-build test -f go.mod
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} - name: Install Go
run: |
rm -rf /usr/local/go
curl -LO https://go.dev/dl/go1.26.2.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.26.2.linux-amd64.tar.gz
export PATH=/usr/local/go/bin:$PATH
go version
- name: Download dependencies - name: Download dependencies
run: go mod download run: |
go mod download
- name: Run tests - name: Run tests
run: go test ./... -v run: |
go test ./... -v
- name: Build binary
run: |
go build -o app .
+93 -7
View File
@@ -162,6 +162,8 @@ type Select[T any] struct {
whereStmt string whereStmt string
args []any args []any
results []T results []T
Limit int64
Offset int64
} }
func CreateSelect[T any](orm *simpleorm.ORM, whereStmt string, args ...any) (Select[T], error) { func CreateSelect[T any](orm *simpleorm.ORM, whereStmt string, args ...any) (Select[T], error) {
@@ -170,7 +172,7 @@ func CreateSelect[T any](orm *simpleorm.ORM, whereStmt string, args ...any) (Sel
return Select[T]{}, errors.New("Failed to get table from schema cache") return Select[T]{}, errors.New("Failed to get table from schema cache")
} }
return Select[T]{target: *table, whereStmt: whereStmt, args: args}, nil return Select[T]{target: *table, whereStmt: whereStmt, args: args, Limit: -1, Offset: -1}, nil
} }
func (s *Select[T]) Results() []T { func (s *Select[T]) Results() []T {
@@ -193,7 +195,18 @@ func (s *Select[T]) execute(conn *simpleorm.DBConnection, tx *sql.Tx) error {
dml += " WHERE " + s.whereStmt dml += " WHERE " + s.whereStmt
} }
log.LogDebug("Preparing sql: %s, with args: %s", dml, s.args) var effectiveArgs []any = s.args
if s.Limit != -1 {
dml += " LIMIT ?"
effectiveArgs = append(effectiveArgs, s.Limit)
}
if s.Offset != -1 {
dml += " OFFSET ?"
effectiveArgs = append(effectiveArgs, s.Offset)
}
log.LogDebug("Preparing sql: %s, with args: %s", dml, effectiveArgs)
var stmt *sql.Stmt var stmt *sql.Stmt
if tx != nil { if tx != nil {
@@ -210,12 +223,12 @@ func (s *Select[T]) execute(conn *simpleorm.DBConnection, tx *sql.Tx) error {
log.LogDebug("Executing statement: %s", stmt) log.LogDebug("Executing statement: %s", stmt)
var rows *sql.Rows var rows *sql.Rows
if len(s.args) == 0 { if len(effectiveArgs) == 0 {
rows, err = stmt.Query() rows, err = stmt.Query()
} else { } else {
// Flattent args to make sure it can be parsed correctly // Flattent args to make sure it can be parsed correctly
var flatArgs []any var flatArgs []any
for _, a := range s.args { for _, a := range effectiveArgs {
if s, ok := a.([]any); ok { if s, ok := a.([]any); ok {
flatArgs = append(flatArgs, s...) flatArgs = append(flatArgs, s...)
} else { } else {
@@ -238,6 +251,79 @@ func (s *Select[T]) execute(conn *simpleorm.DBConnection, tx *sql.Tx) error {
return nil return nil
} }
type Count[T any] struct {
target schema.Table
whereStmt string
args []any
result int64
}
func CreateCount[T any](orm *simpleorm.ORM, whereStmt string, args ...any) (Count[T], error) {
table, ok := orm.Cache().Get(reflect.TypeFor[T]().Name())
if !ok {
return Count[T]{}, errors.New("Failed to get table from schema cache")
}
return Count[T]{target: *table, whereStmt: whereStmt, args: args}, nil
}
func (c *Count[T]) Result() int64 {
return c.result
}
func (c *Count[T]) Execute(conn *simpleorm.DBConnection) error {
return c.execute(conn, nil)
}
func (c *Count[T]) execute(conn *simpleorm.DBConnection, tx *sql.Tx) error {
// Reinit the results, new execution
c.result = -1
dml := c.target.GetCountDML()
if c.whereStmt != "" {
dml += " WHERE " + c.whereStmt
}
log.LogDebug("Preparing sql: %s, with args: %s", dml, c.args)
var stmt *sql.Stmt
var err error
if tx != nil {
stmt, err = tx.Prepare(dml)
} else {
stmt, err = conn.Prepare(dml)
}
if err != nil {
return err
}
defer stmt.Close()
log.LogDebug("Executing statement: %s", stmt)
if len(c.args) == 0 {
err = stmt.QueryRow().Scan(&c.result)
} else {
// Flattent args to make sure it can be parsed correctly
var flatArgs []any
for _, a := range c.args {
if s, ok := a.([]any); ok {
flatArgs = append(flatArgs, s...)
} else {
flatArgs = append(flatArgs, a)
}
}
err = stmt.QueryRow(flatArgs...).Scan(&c.result)
}
if err != nil {
return err
}
return nil
}
type Insert[T any] struct { type Insert[T any] struct {
target schema.Table target schema.Table
toInsert []T toInsert []T
@@ -278,7 +364,7 @@ func (ins *Insert[T]) execute(conn *simpleorm.DBConnection, tx *sql.Tx) error {
} }
} }
log.LogInfo("%s [%s]", dml, params) log.LogDebug("%s [%s]", dml, params)
var stmt *sql.Stmt var stmt *sql.Stmt
if tx != nil { if tx != nil {
@@ -347,7 +433,7 @@ func (u Update[T]) execute(conn *simpleorm.DBConnection, tx *sql.Tx) error {
params = append(params, pk) params = append(params, pk)
} }
log.LogInfo("%s [%s]", dml, params) log.LogDebug("%s [%s]", dml, params)
var stmt *sql.Stmt var stmt *sql.Stmt
if tx != nil { if tx != nil {
@@ -405,7 +491,7 @@ func (d *Delete[T]) execute(conn *simpleorm.DBConnection, tx *sql.Tx) error {
} }
} }
log.LogInfo("%s [%s]", dml, params) log.LogDebug("%s [%s]", dml, params)
var stmt *sql.Stmt var stmt *sql.Stmt
if tx != nil { if tx != nil {
+4
View File
@@ -58,6 +58,10 @@ func (t Table) GetSelectDML() (string, error) {
return dml.String(), nil return dml.String(), nil
} }
func (t Table) GetCountDML() string {
return "SELECT COUNT(*) FROM " + t.Name
}
func (t Table) GetInsertDML(count int) (string, error) { func (t Table) GetInsertDML(count int) (string, error) {
var dml strings.Builder var dml strings.Builder
dml.WriteString("INSERT INTO " + util.CamelToSnake(t.Type.Name()) + " (") dml.WriteString("INSERT INTO " + util.CamelToSnake(t.Type.Name()) + " (")
+145
View File
@@ -1,6 +1,7 @@
package simpleorm_test package simpleorm_test
import ( import (
"strconv"
"testing" "testing"
"time" "time"
@@ -681,3 +682,147 @@ func TestTransactionRollback(t *testing.T) {
return return
} }
} }
func TestSelectWithLimitAndOffset(t *testing.T) {
// GIVEN
orm, conn := testSetup()
defer cleanUp("test.db", conn)
testEntites := []Test{}
for i := 0; i < 1000; i++ {
testEntites = append(testEntites, Test{Int64Field: -1, IntField: i, StringField: "Entity " + strconv.Itoa(i)})
}
insertExec, err := exec.NewInsert[Test](orm, testEntites...)
if err != nil {
log.LogError("TestSelectWithLimitAndOffset setup failed: %s", err)
t.Fail()
return
}
err = insertExec.Execute(conn)
if err != nil {
log.LogError("TestSelectWithLimitAndOffset setup failed: %s", err)
t.Fail()
return
}
// WHEN
selectExec, err := exec.CreateSelect[Test](orm, "")
if err != nil {
log.LogError("TestSelectWithLimitAndOffset failed: %s", err)
t.Fail()
return
}
selectExec.Limit = 100
selectExec.Offset = 0
err = selectExec.Execute(conn)
if err != nil {
log.LogError("TestSelectWithLimitAndOffset failed: %s", err)
t.Fail()
return
}
// THEN
if len(selectExec.Results()) != 100 {
log.LogError("TestSelectWithLimitAndOffset expected result size 100, actual: %s", len(selectExec.Results()))
t.Fail()
}
}
func TestCount(t *testing.T) {
// GIVEN
orm, conn := testSetup()
defer cleanUp("test.db", conn)
testEntites := []Test{}
for i := 0; i < 1000; i++ {
testEntites = append(testEntites, Test{Int64Field: -1, IntField: i, StringField: "Entity " + strconv.Itoa(i)})
}
insertExec, err := exec.NewInsert[Test](orm, testEntites...)
if err != nil {
log.LogError("TestSelectWithLimitAndOffset setup failed: %s", err)
t.Fail()
return
}
err = insertExec.Execute(conn)
if err != nil {
log.LogError("TestSelectWithLimitAndOffset setup failed: %s", err)
t.Fail()
return
}
// WHEN
countExec, err := exec.CreateCount[Test](orm, "")
if err != nil {
log.LogError("TestSelectWithLimitAndOffset failed: %s", err)
t.Fail()
return
}
err = countExec.Execute(conn)
if err != nil {
log.LogError("TestSelectWithLimitAndOffset failed: %s", err)
t.Fail()
return
}
// THEN
if countExec.Result() != 1000 {
log.LogError("TestSelectWithLimitAndOffset expected result size 1000, actual: %s", countExec.Result())
t.Fail()
}
}
func TestCountWithCondition(t *testing.T) {
// GIVEN
orm, conn := testSetup()
defer cleanUp("test.db", conn)
testEntites := []Test{}
for i := 0; i < 1000; i++ {
testEntites = append(testEntites, Test{Int64Field: -1, IntField: i, StringField: "Entity " + strconv.Itoa(i)})
}
insertExec, err := exec.NewInsert[Test](orm, testEntites...)
if err != nil {
log.LogError("TestSelectWithLimitAndOffset setup failed: %s", err)
t.Fail()
return
}
err = insertExec.Execute(conn)
if err != nil {
log.LogError("TestSelectWithLimitAndOffset setup failed: %s", err)
t.Fail()
return
}
// WHEN
countExec, err := exec.CreateCount[Test](orm, "id % 2 = 0")
if err != nil {
log.LogError("TestSelectWithLimitAndOffset failed: %s", err)
t.Fail()
return
}
err = countExec.Execute(conn)
if err != nil {
log.LogError("TestSelectWithLimitAndOffset failed: %s", err)
t.Fail()
return
}
// THEN
if countExec.Result() != 500 {
log.LogError("TestSelectWithLimitAndOffset expected result size 500, actual: %s", countExec.Result())
t.Fail()
}
}