Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added createRecordDiscardResult #1972

Merged
merged 1 commit into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions IHP/ModelSupport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ type family GetModelByTableName (tableName :: Symbol) :: Type
class CanCreate a where
create :: (?modelContext :: ModelContext) => a -> IO a
createMany :: (?modelContext :: ModelContext) => [a] -> IO [a]

-- | Like 'createRecord' but doesn't return the created record
createRecordDiscardResult :: (?modelContext :: ModelContext) => a -> IO ()
createRecordDiscardResult record = do
_ <- createRecord record
pure ()

class CanUpdate a where
updateRecord :: (?modelContext :: ModelContext) => a -> IO a
Expand Down
21 changes: 21 additions & 0 deletions Test/SchemaCompilerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ tests = do
createMany [] = pure []
createMany models = do
sqlQuery (Query $ "INSERT INTO users (id) VALUES " <> (ByteString.intercalate ", " (List.map (\_ -> "(?)") models)) <> " RETURNING id") (List.concat $ List.map (\model -> [toField (model.id)]) models)
createRecordDiscardResult :: (?modelContext :: ModelContext) => User -> IO ()
createRecordDiscardResult model = do
sqlExecDiscardResult "INSERT INTO users (id) VALUES (?)" (Only (model.id))
|]
it "should compile CanUpdate instance with sqlQuery" $ \statement -> do
getInstanceDecl "CanUpdate" compileOutput `shouldBe` [trimming|
Expand Down Expand Up @@ -219,6 +222,9 @@ tests = do
createMany [] = pure []
createMany models = do
sqlQuery (Query $ "INSERT INTO users (id, ids, electricity_unit_price) VALUES " <> (ByteString.intercalate ", " (List.map (\_ -> "(?, ? :: UUID[], ?)") models)) <> " RETURNING id, ids, electricity_unit_price") (List.concat $ List.map (\model -> [toField (model.id), toField (model.ids), toField (fieldWithDefault #electricityUnitPrice model)]) models)
createRecordDiscardResult :: (?modelContext :: ModelContext) => User -> IO ()
createRecordDiscardResult model = do
sqlExecDiscardResult "INSERT INTO users (id, ids, electricity_unit_price) VALUES (?, ? :: UUID[], ?)" ((model.id, model.ids, fieldWithDefault #electricityUnitPrice model))

instance CanUpdate User where
updateRecord model = do
Expand Down Expand Up @@ -291,6 +297,9 @@ tests = do
createMany [] = pure []
createMany models = do
sqlQuery (Query $ "INSERT INTO users (id, ids, electricity_unit_price) VALUES " <> (ByteString.intercalate ", " (List.map (\_ -> "(?, ? :: UUID[], ?)") models)) <> " RETURNING id, ids, electricity_unit_price") (List.concat $ List.map (\model -> [toField (model.id), toField (model.ids), toField (fieldWithDefault #electricityUnitPrice model)]) models)
createRecordDiscardResult :: (?modelContext :: ModelContext) => User -> IO ()
createRecordDiscardResult model = do
sqlExecDiscardResult "INSERT INTO users (id, ids, electricity_unit_price) VALUES (?, ? :: UUID[], ?)" ((model.id, model.ids, fieldWithDefault #electricityUnitPrice model))

instance CanUpdate User where
updateRecord model = do
Expand Down Expand Up @@ -362,6 +371,9 @@ tests = do
createMany [] = pure []
createMany models = do
sqlQuery (Query $ "INSERT INTO users (id) VALUES " <> (ByteString.intercalate ", " (List.map (\_ -> "(?)") models)) <> " RETURNING id") (List.concat $ List.map (\model -> [toField (model.id)]) models)
createRecordDiscardResult :: (?modelContext :: ModelContext) => User -> IO ()
createRecordDiscardResult model = do
sqlExecDiscardResult "INSERT INTO users (id) VALUES (?)" (Only (model.id))

instance CanUpdate User where
updateRecord model = do
Expand Down Expand Up @@ -439,6 +451,9 @@ tests = do
createMany [] = pure []
createMany models = do
sqlQuery (Query $ "INSERT INTO landing_pages (id) VALUES " <> (ByteString.intercalate ", " (List.map (\_ -> "(?)") models)) <> " RETURNING id") (List.concat $ List.map (\model -> [toField (fieldWithDefault #id model)]) models)
createRecordDiscardResult :: (?modelContext :: ModelContext) => LandingPage -> IO ()
createRecordDiscardResult model = do
sqlExecDiscardResult "INSERT INTO landing_pages (id) VALUES (?)" (Only (fieldWithDefault #id model))

instance CanUpdate LandingPage where
updateRecord model = do
Expand Down Expand Up @@ -483,6 +498,9 @@ tests = do
createMany [] = pure []
createMany models = do
sqlQuery (Query $ "INSERT INTO things (thing_arbitrary_ident) VALUES " <> (ByteString.intercalate ", " (List.map (\_ -> "(?)") models)) <> " RETURNING thing_arbitrary_ident") (List.concat $ List.map (\model -> [toField (fieldWithDefault #thingArbitraryIdent model)]) models)
createRecordDiscardResult :: (?modelContext :: ModelContext) => Thing -> IO ()
createRecordDiscardResult model = do
sqlExecDiscardResult "INSERT INTO things (thing_arbitrary_ident) VALUES (?)" (Only (fieldWithDefault #thingArbitraryIdent model))
|]
it "should compile CanUpdate instance with sqlQuery" $ \statement -> do
getInstanceDecl "CanUpdate" compileOutput `shouldBe` [trimming|
Expand Down Expand Up @@ -549,6 +567,9 @@ tests = do
createMany [] = pure []
createMany models = do
sqlQuery (Query $ "INSERT INTO bit_part_refs (bit_ref, part_ref) VALUES " <> (ByteString.intercalate ", " (List.map (\_ -> "(?, ?)") models)) <> " RETURNING bit_ref, part_ref") (List.concat $ List.map (\model -> [toField (model.bitRef), toField (model.partRef)]) models)
createRecordDiscardResult :: (?modelContext :: ModelContext) => BitPartRef -> IO ()
createRecordDiscardResult model = do
sqlExecDiscardResult "INSERT INTO bit_part_refs (bit_ref, part_ref) VALUES (?, ?)" ((model.bitRef, model.partRef))
|]
it "should compile CanUpdate instance with sqlQuery" $ \statement -> do
getInstanceDecl "CanUpdate" compileOutput `shouldBe` [trimming|
Expand Down
5 changes: 5 additions & 0 deletions ihp-ide/IHP/SchemaCompiler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,11 @@ compileCreate table@(CreateTable { name, columns }) =
<> indent ("sqlQuery (Query $ \"INSERT INTO " <> name <> " (" <> columnNames <> ") VALUES \" <> (ByteString.intercalate \", \" (List.map (\\_ -> \"(" <> values <> ")\") models)) <> \" RETURNING " <> columnNames <> "\") " <> createManyFieldValues <> "\n"
)
)
<> indent (
"createRecordDiscardResult :: (?modelContext :: ModelContext) => " <> modelName <> " -> IO ()\n"
<> "createRecordDiscardResult model = do\n"
<> indent ("sqlExecDiscardResult \"INSERT INTO " <> name <> " (" <> columnNames <> ") VALUES (" <> values <> ")\" (" <> compileToRowValues bindings <> ")\n")
)

commaSep :: [Text] -> Text
commaSep = intercalate ", "
Expand Down