diff --git a/dialect/psql/delete.go b/dialect/psql/delete.go index 2bfe95d..91b46ad 100644 --- a/dialect/psql/delete.go +++ b/dialect/psql/delete.go @@ -1,6 +1,7 @@ package psql import ( + "github.com/rrgmc/litsql" "github.com/rrgmc/litsql/dialect/psql/tag" "github.com/rrgmc/litsql/internal/idm" ) @@ -8,3 +9,11 @@ import ( 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)) +} diff --git a/dialect/psql/delete_test.go b/dialect/psql/delete_test.go index a00d458..bde3033 100644 --- a/dialect/psql/delete_test.go +++ b/dialect/psql/delete_test.go @@ -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) +} diff --git a/dialect/psql/insert.go b/dialect/psql/insert.go index 9fed828..3654ced 100644 --- a/dialect/psql/insert.go +++ b/dialect/psql/insert.go @@ -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)) +} diff --git a/dialect/psql/insert_test.go b/dialect/psql/insert_test.go index 73c6241..9cbfee5 100644 --- a/dialect/psql/insert_test.go +++ b/dialect/psql/insert_test.go @@ -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) +} diff --git a/dialect/psql/update.go b/dialect/psql/update.go index f327881..8041d1e 100644 --- a/dialect/psql/update.go +++ b/dialect/psql/update.go @@ -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)) +} diff --git a/dialect/psql/update_test.go b/dialect/psql/update_test.go index aeaa563..2333af1 100644 --- a/dialect/psql/update_test.go +++ b/dialect/psql/update_test.go @@ -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) +} diff --git a/internal/idm/rawquery.go b/internal/idm/rawquery.go new file mode 100644 index 0000000..4cc2325 --- /dev/null +++ b/internal/idm/rawquery.go @@ -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}) + }) +} diff --git a/internal/iim/rawquery.go b/internal/iim/rawquery.go new file mode 100644 index 0000000..0152f41 --- /dev/null +++ b/internal/iim/rawquery.go @@ -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}) + }) +} diff --git a/internal/ium/rawquery.go b/internal/ium/rawquery.go new file mode 100644 index 0000000..6c225c6 --- /dev/null +++ b/internal/ium/rawquery.go @@ -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}) + }) +}