diff --git a/src/cake/internal/write_query.gleam b/src/cake/internal/write_query.gleam index 5332611..2ed3fd3 100644 --- a/src/cake/internal/write_query.gleam +++ b/src/cake/internal/write_query.gleam @@ -93,6 +93,7 @@ pub type Insert(a) { } pub type InsertIntoTable { + // TODO v1 NoInsertIntoTable InsertIntoTable(name: String) } @@ -353,7 +354,7 @@ pub type UpdateModifier { } pub type UpdateTable { - // TODO v1 NoUpdateTable + NoUpdateTable UpdateTable(String) } @@ -376,10 +377,8 @@ fn update_apply( prepared_statement prp_stm: PreparedStatement, update updt: Update(a), ) { - let UpdateTable(updt_tbl) = updt.table - prp_stm - |> prepared_statement.append_sql("UPDATE " <> updt_tbl) + |> update_table_apply(updt.table) |> update_modifier_apply(updt.modifier) |> prepared_statement.append_sql(" SET") |> update_set_apply(updt.set) @@ -391,6 +390,19 @@ fn update_apply( |> query.epilog_apply(updt.epilog) } +fn update_table_apply( + prepared_statement prp_stm: PreparedStatement, + table tbl: UpdateTable, +) -> PreparedStatement { + case tbl { + NoUpdateTable -> prp_stm + UpdateTable(tbl) -> + prp_stm + |> prepared_statement.append_sql(" ") + |> prepared_statement.append_sql(tbl) + } +} + fn update_modifier_apply( prepared_statement prp_stm: PreparedStatement, update_modifer updt_mdfr: UpdateModifier, @@ -484,6 +496,7 @@ pub type DeleteModifier { } pub type DeleteTable { + // TODO v1 NoDeleteTable DeleteTable(name: String) } diff --git a/src/cake/query/update.gleam b/src/cake/query/update.gleam index 25116f7..4736b70 100644 --- a/src/cake/query/update.gleam +++ b/src/cake/query/update.gleam @@ -7,9 +7,10 @@ import cake/internal/query.{ NoComment, NoEpilog, NoFrom, NoJoins, NoWhere, OrWhere, XorWhere, } import cake/internal/write_query.{ - type Update, type UpdateSet, type UpdateSets, type WriteQuery, NoReturning, - NoUpdateModifier, NoUpdateSets, Returning, Update, UpdateExpressionSet, - UpdateParamSet, UpdateQuery, UpdateSets, UpdateSubQuerySet, UpdateTable, + type Update, type UpdateSet, type UpdateSets, type UpdateTable, + type WriteQuery, NoReturning, NoUpdateModifier, NoUpdateSets, NoUpdateTable, + Returning, Update, UpdateExpressionSet, UpdateParamSet, UpdateQuery, + UpdateSets, UpdateSubQuerySet, UpdateTable, } import cake/param.{type Param} import gleam/list @@ -49,12 +50,12 @@ pub fn string(value vl: String) -> Param { // ▒▒▒ Constructor ▒▒▒ -/// Creates a minimal `Update` query specifying the table and the sets. +/// Creates an empty `Update` query. /// -pub fn new(table tbl: String) -> Update(a) { +pub fn new() -> Update(a) { Update( modifier: NoUpdateModifier, - table: UpdateTable(tbl), + table: NoUpdateTable, set: NoUpdateSets, from: NoFrom, join: NoJoins, @@ -65,6 +66,20 @@ pub fn new(table tbl: String) -> Update(a) { ) } +// ▒▒▒ Table ▒▒▒ + +/// Sets the table of the `Update` query. +/// +pub fn table(query qry: Update(a), table_name tbl_nm: String) -> Update(a) { + Update(..qry, table: UpdateTable(tbl_nm)) +} + +/// Get the table of the `Update` query. +/// +pub fn get_table(query qry: Update(a)) -> UpdateTable { + qry.table +} + // ▒▒▒ Set ▒▒▒ /// Sets a column to a param value. diff --git a/test/cake_test/update_test.gleam b/test/cake_test/update_test.gleam index dd3c2cf..73339c1 100644 --- a/test/cake_test/update_test.gleam +++ b/test/cake_test/update_test.gleam @@ -30,7 +30,8 @@ fn swap_is_wild_sub_query() { } fn update_postes_sqlite_query() { - u.new(table: "cats") + u.new() + |> u.table("cats") |> u.sets([ "age" |> u.set_to_expression("age + 1"), "name" |> u.set_to_param(u.string("Joe")), @@ -41,7 +42,8 @@ fn update_postes_sqlite_query() { } fn update_maria_query() { - u.new(table: "cats") + u.new() + |> u.table("cats") |> u.sets([ "age" |> u.set_to_expression("age + 1"), "name" |> u.set_to_param(u.string("Joe")), @@ -53,7 +55,8 @@ fn update_maria_query() { } fn update_mysql_query() { - u.new(table: "cats") + u.new() + |> u.table("cats") |> u.sets([ "age" |> u.set_to_expression("age + 1"), "name" |> u.set_to_param(u.string("Joe")),