-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This change adds a few experimental methods to the QuerySet interface in hopes of simplifying the usage of the library.
- Loading branch information
Showing
3 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// SPDX-FileCopyrightText: Copyright (c) 2023, Ctrl IQ, Inc. All rights reserved | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package pika | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"reflect" | ||
) | ||
|
||
func (b *basePsql[T]) findID(x *T) any { | ||
elem := reflect.ValueOf(x).Elem() | ||
|
||
// Check if ID is a field | ||
idField := elem.FieldByName("ID") | ||
if idField.IsValid() { | ||
return idField.Interface() | ||
} | ||
|
||
// Also check for Id field | ||
idField = elem.FieldByName("Id") | ||
if idField.IsValid() { | ||
return idField.Interface() | ||
} | ||
|
||
// Return nil if ID is not a field | ||
return nil | ||
} | ||
|
||
func (b *basePsql[T]) F(keyval ...any) QuerySet[T] { | ||
args := NewArgs() | ||
var queries []string | ||
for i := 0; i < len(keyval); i += 2 { | ||
args.Set(keyval[i].(string), keyval[i+1]) | ||
filter := fmt.Sprintf("%s=:%s", keyval[i].(string), keyval[i].(string)) | ||
queries = append(queries, filter) | ||
} | ||
|
||
logger.Debugf("F: %s", queries) | ||
|
||
return b.Args(args).Filter(queries...) | ||
} | ||
|
||
func (b *basePsql[T]) D(x *T) error { | ||
id := b.findID(x) | ||
if id == nil { | ||
return fmt.Errorf("id not found") | ||
} | ||
|
||
qs := b.F("id", id) | ||
return qs.Delete() | ||
} | ||
|
||
func (b *basePsql[T]) Transaction(ctx context.Context) (QuerySet[T], error) { | ||
ts := NewPostgreSQLFromDB(b.psql.DB()) | ||
err := ts.Begin(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return Q[T](ts), nil | ||
} | ||
|
||
func (b *basePsql[T]) U(x *T) error { | ||
id := b.findID(x) | ||
if id == nil { | ||
return fmt.Errorf("id not found") | ||
} | ||
|
||
qs := b.F("id", id) | ||
return qs.Update(x) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package pika | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestF_1(t *testing.T) { | ||
psql := newPsql(t) | ||
createTestEntries(t, psql) | ||
|
||
m, err := Q[simpleModel1](psql).F("title", "Test").All() | ||
require.Nil(t, err) | ||
require.NotNil(t, m) | ||
require.Equal(t, 1, len(m)) | ||
require.Equal(t, "Test", m[0].Title) | ||
} | ||
|
||
func TestF_2(t *testing.T) { | ||
psql := newPsql(t) | ||
createTestEntries(t, psql) | ||
|
||
m, err := Q[simpleModel1](psql).F("title", "Test", "description", "Test").All() | ||
require.Nil(t, err) | ||
require.NotNil(t, m) | ||
require.Equal(t, 1, len(m)) | ||
require.Equal(t, "Test", m[0].Title) | ||
} | ||
|
||
func TestF_3Or(t *testing.T) { | ||
psql := newPsql(t) | ||
createTestEntries(t, psql) | ||
|
||
m, err := Q[simpleModel1](psql).F("title", "Test", "title__or", "Test2").All() | ||
require.Nil(t, err) | ||
require.NotNil(t, m) | ||
require.Equal(t, 2, len(m)) | ||
require.Equal(t, "Test", m[0].Title) | ||
require.Equal(t, "Test2", m[1].Title) | ||
} | ||
|
||
func TestU(t *testing.T) { | ||
psql := newPsql(t) | ||
createTestEntries(t, psql) | ||
|
||
m, err := Q[simpleModel1](psql).F("title", "Test").All() | ||
require.Nil(t, err) | ||
require.NotNil(t, m) | ||
require.Equal(t, 1, len(m)) | ||
require.Equal(t, "Test", m[0].Title) | ||
|
||
elem := m[0] | ||
elem.Title = "TestUpdated" | ||
err = Q[simpleModel1](psql).U(elem) | ||
require.Nil(t, err) | ||
|
||
m, err = Q[simpleModel1](psql).F("title", "TestUpdated").All() | ||
require.Nil(t, err) | ||
require.NotNil(t, m) | ||
require.Equal(t, 1, len(m)) | ||
require.Equal(t, "TestUpdated", m[0].Title) | ||
} | ||
|
||
func TestD(t *testing.T) { | ||
psql := newPsql(t) | ||
createTestEntries(t, psql) | ||
|
||
m, err := Q[simpleModel1](psql).F("title", "Test").All() | ||
require.Nil(t, err) | ||
require.NotNil(t, m) | ||
require.Equal(t, 1, len(m)) | ||
require.Equal(t, "Test", m[0].Title) | ||
|
||
elem := m[0] | ||
err = Q[simpleModel1](psql).D(elem) | ||
require.Nil(t, err) | ||
|
||
m, err = Q[simpleModel1](psql).F("title", "Test").All() | ||
require.Nil(t, err) | ||
require.Equal(t, 0, len(m)) | ||
} |