@@ -15,6 +15,7 @@ module Squeel.PostgreSQL.Statement where
1515
1616import Data.Boolean
1717import Data.ByteString (ByteString)
18+import Data.Maybe
1819import Data.Monoid
1920import Data.String
2021import Data.Vinyl
@@ -410,3 +411,34 @@ instance {-# OVERLAPPING #-}
410411instance {-# OVERLAPPABLE #-}
411412 (KnownSymbol table, DropTable table schema0 schema1)
412413 => DropTable table (tabCols ': schema0) (tabCols ': schema1)
414+
415+set :: g x -> (Maybe `Compose` g) x
416+set = Compose . Just
417+
418+same :: (Maybe `Compose` g) x
419+same = Compose Nothing
420+
421+update
422+ :: HasTable table schema columns
423+ => Alias table
424+ -> Rec (Aliased (Maybe `Compose` Expression params '[table ::: columns])) columns
425+ -> Expression params '[table ::: columns] ('NotNull 'PGBool)
426+ -> Statement params schema schema '[]
427+update (Alias table) columns where' = UnsafeStatement $ mconcat
428+ [ "UPDATE "
429+ , fromString $ symbolVal' table
430+ , " SET "
431+ , ByteString.intercalate ", " . catMaybes . recordToList $
432+ rmap (Const . renderSet) columns
433+ , " WHERE ", renderExpression where'
434+ ] where
435+ renderSet
436+ :: Aliased (Maybe `Compose` Expression params tables) column
437+ -> Maybe ByteString
438+ renderSet = \case
439+ Compose (Just expression) `As` Alias column -> Just $ mconcat
440+ [ fromString $ symbolVal' column
441+ , " = "
442+ , renderExpression expression
443+ ]
444+ Compose Nothing `As` _ -> Nothing