Skip to content

Commit

Permalink
raw query
Browse files Browse the repository at this point in the history
  • Loading branch information
RangelReale committed Jun 7, 2024
1 parent 55672eb commit f76fe82
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 0 deletions.
9 changes: 9 additions & 0 deletions dialect/psql/delete.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package psql

import (
"github.com/rrgmc/litsql"
"github.com/rrgmc/litsql/dialect/psql/tag"
"github.com/rrgmc/litsql/internal/idm"
)

func Delete(mods ...DeleteMod) DeleteQuery {
return idm.Delete[tag.DeleteTag](Dialect, mods...)
}

func DeleteRaw(rawQuery string, args ...any) DeleteQuery {
return Delete(idm.RawQuery[tag.DeleteTag](rawQuery, args...))
}

func DeleteRawExpr(e litsql.Expression) DeleteQuery {
return Delete(idm.RawQueryExpr[tag.DeleteTag](e))
}
9 changes: 9 additions & 0 deletions dialect/psql/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,12 @@ func TestDelete(t *testing.T) {
assert.Assert(t, queryStr != "")
assert.Assert(t, len(params) == 0)
}

func TestDeleteRaw(t *testing.T) {
query := psql.DeleteRaw("delete * from users where id = ?", 12, 15)
queryStr, params, err := query.Build()

assert.NilError(t, err)
assert.Equal(t, "delete * from users where id = ?", queryStr)
assert.DeepEqual(t, []any{12, 15}, params)
}
10 changes: 10 additions & 0 deletions dialect/psql/insert.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package psql

import (
"github.com/rrgmc/litsql"
"github.com/rrgmc/litsql/dialect/psql/tag"
"github.com/rrgmc/litsql/internal/iim"
"github.com/rrgmc/litsql/internal/ism"
)

func Insert(mods ...InsertMod) InsertQuery {
return iim.Insert[tag.InsertTag](Dialect, mods...)
}

func InsertRaw(rawQuery string, args ...any) InsertQuery {
return Insert(ism.RawQuery[tag.InsertTag](rawQuery, args...))
}

func InsertRawExpr(e litsql.Expression) InsertQuery {
return Insert(ism.RawQueryExpr[tag.InsertTag](e))
}
9 changes: 9 additions & 0 deletions dialect/psql/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,12 @@ func TestInsert(t *testing.T) {
assert.Assert(t, queryStr != "")
assert.Assert(t, len(params) == 0)
}

func TestInsertRaw(t *testing.T) {
query := psql.InsertRaw("insert into users (id) values (?)", 12, 15)
queryStr, params, err := query.Build()

assert.NilError(t, err)
assert.Equal(t, "insert into users (id) values (?)", queryStr)
assert.DeepEqual(t, []any{12, 15}, params)
}
10 changes: 10 additions & 0 deletions dialect/psql/update.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package psql

import (
"github.com/rrgmc/litsql"
"github.com/rrgmc/litsql/dialect/psql/tag"
"github.com/rrgmc/litsql/internal/ism"
"github.com/rrgmc/litsql/internal/ium"
)

func Update(mods ...UpdateMod) UpdateQuery {
return ium.Update[tag.UpdateTag](Dialect, mods...)
}

func UpdateRaw(rawQuery string, args ...any) UpdateQuery {
return Update(ism.RawQuery[tag.UpdateTag](rawQuery, args...))
}

func UpdateRawExpr(e litsql.Expression) UpdateQuery {
return Update(ism.RawQueryExpr[tag.UpdateTag](e))
}
9 changes: 9 additions & 0 deletions dialect/psql/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,12 @@ func TestUpdate(t *testing.T) {
assert.Assert(t, queryStr != "")
assert.Assert(t, len(params) == 1)
}

func TestUpdateRaw(t *testing.T) {
query := psql.UpdateRaw("update users set name = ? where id = ?", 12, 15)
queryStr, params, err := query.Build()

assert.NilError(t, err)
assert.Equal(t, "update users set name = ? where id = ?", queryStr)
assert.DeepEqual(t, []any{12, 15}, params)
}
20 changes: 20 additions & 0 deletions internal/idm/rawquery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package idm

import (
"github.com/rrgmc/litsql"
"github.com/rrgmc/litsql/expr"
"github.com/rrgmc/litsql/internal/iclause"
"github.com/rrgmc/litsql/sq"
)

func RawQuery[T any](rawQuery string, args ...any) sq.QueryMod[T] {
return sq.QueryModFunc[T](func(a litsql.QueryBuilder) {
a.AddQueryClause(&iclause.RawQuery{Query: expr.String(rawQuery), Args: args})
})
}

func RawQueryExpr[T any](rawQuery litsql.Expression) sq.QueryMod[T] {
return sq.QueryModFunc[T](func(a litsql.QueryBuilder) {
a.AddQueryClause(&iclause.RawQuery{Query: rawQuery})
})
}
20 changes: 20 additions & 0 deletions internal/iim/rawquery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package iim

import (
"github.com/rrgmc/litsql"
"github.com/rrgmc/litsql/expr"
"github.com/rrgmc/litsql/internal/iclause"
"github.com/rrgmc/litsql/sq"
)

func RawQuery[T any](rawQuery string, args ...any) sq.QueryMod[T] {
return sq.QueryModFunc[T](func(a litsql.QueryBuilder) {
a.AddQueryClause(&iclause.RawQuery{Query: expr.String(rawQuery), Args: args})
})
}

func RawQueryExpr[T any](rawQuery litsql.Expression) sq.QueryMod[T] {
return sq.QueryModFunc[T](func(a litsql.QueryBuilder) {
a.AddQueryClause(&iclause.RawQuery{Query: rawQuery})
})
}
20 changes: 20 additions & 0 deletions internal/ium/rawquery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ium

import (
"github.com/rrgmc/litsql"
"github.com/rrgmc/litsql/expr"
"github.com/rrgmc/litsql/internal/iclause"
"github.com/rrgmc/litsql/sq"
)

func RawQuery[T any](rawQuery string, args ...any) sq.QueryMod[T] {
return sq.QueryModFunc[T](func(a litsql.QueryBuilder) {
a.AddQueryClause(&iclause.RawQuery{Query: expr.String(rawQuery), Args: args})
})
}

func RawQueryExpr[T any](rawQuery litsql.Expression) sq.QueryMod[T] {
return sq.QueryModFunc[T](func(a litsql.QueryBuilder) {
a.AddQueryClause(&iclause.RawQuery{Query: rawQuery})
})
}

0 comments on commit f76fe82

Please sign in to comment.