Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
inoas committed Jun 30, 2024
1 parent 7eb6931 commit 8d81e2e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
21 changes: 17 additions & 4 deletions src/cake/internal/write_query.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pub type Insert(a) {
}

pub type InsertIntoTable {
// TODO v1 NoInsertIntoTable
InsertIntoTable(name: String)
}

Expand Down Expand Up @@ -353,7 +354,7 @@ pub type UpdateModifier {
}

pub type UpdateTable {
// TODO v1 NoUpdateTable
NoUpdateTable
UpdateTable(String)
}

Expand All @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -484,6 +496,7 @@ pub type DeleteModifier {
}

pub type DeleteTable {
// TODO v1 NoDeleteTable
DeleteTable(name: String)
}

Expand Down
27 changes: 21 additions & 6 deletions src/cake/query/update.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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.
Expand Down
9 changes: 6 additions & 3 deletions test/cake_test/update_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand All @@ -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")),
Expand All @@ -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")),
Expand Down

0 comments on commit 8d81e2e

Please sign in to comment.