on dev help sign in
printSQL and docs
commit5f2840c3b0e79e66512ccc73de40a18124dc8460 authorEitan Chatav <eitan.chatav@gmail.com> dateThu 21 Jun 2018, 17:26 (8 years ago) parent1e229ac
3 files changed+173−151
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+33−28 squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+128−122 squeal-postgresql/src/Squeal/PostgreSQL/Render.hs+12−1
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+33−28
@@ -113,6 +113,9 @@ newtype Definition
113113 = UnsafeDefinition { renderDefinition :: ByteString }
114114 deriving (GHC.Generic,Show,Eq,Ord,NFData)
115115
116+instance RenderSQL (Definition schema0 schema1) where
117+ renderSQL = renderDefinition
118+
116119instance Category Definition where
117120 id = UnsafeDefinition ";"
118121 ddl1 . ddl0 = UnsafeDefinition $
@@ -126,7 +129,7 @@ CREATE statements
126129
127130>>> :set -XOverloadedLabels
128131>>> :{
129−Data.ByteString.Char8.putStrLn . renderDefinition $
132+printSQL $
130133 createTable #tab ((int & nullable) `As` #a :* (real & nullable) `As` #b :* Nil) Nil
131134:}
132135CREATE TABLE "tab" ("a" int NULL, "b" real NULL);
@@ -154,8 +157,10 @@ Instead, the schema already has the table so if the table did not yet exist, the
154157>>> type Table = '[] :=> '["a" ::: 'NoDef :=> 'Null 'PGint4, "b" ::: 'NoDef :=> 'Null 'PGfloat4]
155158>>> type Schema = '["tab" ::: 'Table Table]
156159>>> :{
157−Data.ByteString.Char8.putStrLn $ renderDefinition
158− (createTableIfNotExists #tab ((int & nullable) `As` #a :* (real & nullable) `As` #b :* Nil) Nil :: Definition Schema Schema)
160+let
161+ setup :: Definition Schema Schema
162+ setup = createTableIfNotExists #tab ((int & nullable) `As` #a :* (real & nullable) `As` #b :* Nil) Nil
163+in printSQL setup
159164:}
160165CREATE TABLE IF NOT EXISTS "tab" ("a" int NULL, "b" real NULL);
161166-}
@@ -243,7 +248,7 @@ let
243248 ( check (#a :* #b :* Nil) (#a .> #b) `As` #inequality :* Nil )
244249:}
245250
246−>>> Data.ByteString.Char8.putStrLn $ renderDefinition definition
251+>>> printSQL definition
247252CREATE TABLE "tab" ("a" int NOT NULL, "b" int NOT NULL, CONSTRAINT "inequality" CHECK (("a" > "b")));
248253-}
249254check
@@ -277,7 +282,7 @@ let
277282 ( unique (#a :* #b :* Nil) `As` #uq_a_b :* Nil )
278283:}
279284
280−>>> Data.ByteString.Char8.putStrLn $ renderDefinition definition
285+>>> printSQL definition
281286CREATE TABLE "tab" ("a" int NULL, "b" int NULL, CONSTRAINT "uq_a_b" UNIQUE ("a", "b"));
282287-}
283288unique
@@ -310,7 +315,7 @@ let
310315 ( primaryKey #id `As` #pk_id :* Nil )
311316:}
312317
313−>>> Data.ByteString.Char8.putStrLn $ renderDefinition definition
318+>>> printSQL definition
314319CREATE TABLE "tab" ("id" serial, "name" text NOT NULL, CONSTRAINT "pk_id" PRIMARY KEY ("id"));
315320-}
316321primaryKey
@@ -361,7 +366,7 @@ let
361366 ( primaryKey #id `As` #pk_emails :*
362367 foreignKey #user_id #users #id
363368 OnDeleteCascade OnUpdateCascade `As` #fk_user_id :* Nil )
364−in Data.ByteString.Char8.putStrLn $ renderDefinition setup
369+in printSQL setup
365370:}
366371CREATE TABLE "users" ("id" serial, "name" text NOT NULL, CONSTRAINT "pk_users" PRIMARY KEY ("id"));
367372CREATE TABLE "emails" ("id" serial, "user_id" int NOT NULL, "email" text NULL, CONSTRAINT "pk_emails" PRIMARY KEY ("id"), CONSTRAINT "fk_user_id" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE);
@@ -392,7 +397,7 @@ let
392397 ( primaryKey #id `As` #employees_pk :*
393398 foreignKey #employer_id #employees #id
394399 OnDeleteCascade OnUpdateCascade `As` #employees_employer_fk :* Nil )
395−in Data.ByteString.Char8.putStrLn $ renderDefinition setup
400+in printSQL setup
396401:}
397402CREATE TABLE "employees" ("id" serial, "name" text NOT NULL, "employer_id" integer NULL, CONSTRAINT "employees_pk" PRIMARY KEY ("id"), CONSTRAINT "employees_employer_fk" FOREIGN KEY ("employer_id") REFERENCES "employees" ("id") ON DELETE CASCADE ON UPDATE CASCADE);
398403-}
@@ -487,7 +492,7 @@ DROP statements
487492-- definition = dropTable #muh_table
488493-- :}
489494--
490−-- >>> Data.ByteString.Char8.putStrLn $ renderDefinition definition
495+-- >>> printSQL definition
491496-- DROP TABLE "muh_table";
492497dropTable
493498 :: Has table schema ('Table t)
@@ -513,7 +518,7 @@ alterTable tab alteration = UnsafeDefinition $
513518
514519-- | `alterTableRename` changes the name of a table from the schema.
515520--
516−-- >>> Data.ByteString.Char8.putStrLn $ renderDefinition $ alterTableRename #foo #bar
521+-- >>> printSQL $ alterTableRename #foo #bar
517522-- ALTER TABLE "foo" RENAME TO "bar";
518523alterTableRename
519524 :: (KnownSymbol table0, KnownSymbol table1)
@@ -541,7 +546,7 @@ newtype AlterTable
541546-- '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]
542547-- '["tab" ::: 'Table ('["positive" ::: Check '["col"]] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]
543548-- definition = alterTable #tab (addConstraint #positive (check (#col :* Nil) (#col .> 0)))
544−-- in Data.ByteString.Char8.putStrLn $ renderDefinition definition
549+-- in printSQL definition
545550-- :}
546551-- ALTER TABLE "tab" ADD CONSTRAINT "positive" CHECK (("col" > 0));
547552addConstraint
@@ -565,7 +570,7 @@ addConstraint alias constraint = UnsafeAlterTable $
565570-- '["tab" ::: 'Table ('["positive" ::: Check '["col"]] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]
566571-- '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]
567572-- definition = alterTable #tab (dropConstraint #positive)
568−-- in Data.ByteString.Char8.putStrLn $ renderDefinition definition
573+-- in printSQL definition
569574-- :}
570575-- ALTER TABLE "tab" DROP CONSTRAINT "positive";
571576dropConstraint
@@ -592,7 +597,7 @@ class AddColumn ty where
592597 -- '[ "col1" ::: 'NoDef :=> 'Null 'PGint4
593598 -- , "col2" ::: 'Def :=> 'Null 'PGtext ])]
594599 -- definition = alterTable #tab (addColumn #col2 (text & nullable & default_ "foo"))
595− -- in Data.ByteString.Char8.putStrLn $ renderDefinition definition
600+ -- in printSQL definition
596601 -- :}
597602 -- ALTER TABLE "tab" ADD COLUMN "col2" text NULL DEFAULT E'foo';
598603 --
@@ -604,7 +609,7 @@ class AddColumn ty where
604609 -- '[ "col1" ::: 'NoDef :=> 'Null 'PGint4
605610 -- , "col2" ::: 'NoDef :=> 'Null 'PGtext ])]
606611 -- definition = alterTable #tab (addColumn #col2 (text & nullable))
607− -- in Data.ByteString.Char8.putStrLn $ renderDefinition definition
612+ -- in printSQL definition
608613 -- :}
609614 -- ALTER TABLE "tab" ADD COLUMN "col2" text NULL;
610615 addColumn
@@ -633,7 +638,7 @@ instance {-# OVERLAPPABLE #-} AddColumn ('NoDef :=> 'Null ty)
633638-- , "col2" ::: 'NoDef :=> 'Null 'PGtext ])]
634639-- '["tab" ::: 'Table ('[] :=> '["col1" ::: 'NoDef :=> 'Null 'PGint4])]
635640-- definition = alterTable #tab (dropColumn #col2)
636−-- in Data.ByteString.Char8.putStrLn $ renderDefinition definition
641+-- in printSQL definition
637642-- :}
638643-- ALTER TABLE "tab" DROP COLUMN "col2";
639644dropColumn
@@ -654,7 +659,7 @@ dropColumn column = UnsafeAlterTable $
654659-- '["tab" ::: 'Table ('[] :=> '["foo" ::: 'NoDef :=> 'Null 'PGint4])]
655660-- '["tab" ::: 'Table ('[] :=> '["bar" ::: 'NoDef :=> 'Null 'PGint4])]
656661-- definition = alterTable #tab (renameColumn #foo #bar)
657−-- in Data.ByteString.Char8.putStrLn $ renderDefinition definition
662+-- in printSQL definition
658663-- :}
659664-- ALTER TABLE "tab" RENAME COLUMN "foo" TO "bar";
660665renameColumn
@@ -697,7 +702,7 @@ newtype AlterColumn (schema :: SchemaType) (ty0 :: ColumnType) (ty1 :: ColumnTyp
697702-- '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4])]
698703-- '["tab" ::: 'Table ('[] :=> '["col" ::: 'Def :=> 'Null 'PGint4])]
699704-- definition = alterTable #tab (alterColumn #col (setDefault 5))
700−-- in Data.ByteString.Char8.putStrLn $ renderDefinition definition
705+-- in printSQL definition
701706-- :}
702707-- ALTER TABLE "tab" ALTER COLUMN "col" SET DEFAULT 5;
703708setDefault
@@ -714,7 +719,7 @@ setDefault expression = UnsafeAlterColumn $
714719-- '["tab" ::: 'Table ('[] :=> '["col" ::: 'Def :=> 'Null 'PGint4])]
715720-- '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4])]
716721-- definition = alterTable #tab (alterColumn #col dropDefault)
717−-- in Data.ByteString.Char8.putStrLn $ renderDefinition definition
722+-- in printSQL definition
718723-- :}
719724-- ALTER TABLE "tab" ALTER COLUMN "col" DROP DEFAULT;
720725dropDefault :: AlterColumn schema ('Def :=> ty) ('NoDef :=> ty)
@@ -730,7 +735,7 @@ dropDefault = UnsafeAlterColumn $ "DROP DEFAULT"
730735-- '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4])]
731736-- '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]
732737-- definition = alterTable #tab (alterColumn #col setNotNull)
733−-- in Data.ByteString.Char8.putStrLn $ renderDefinition definition
738+-- in printSQL definition
734739-- :}
735740-- ALTER TABLE "tab" ALTER COLUMN "col" SET NOT NULL;
736741setNotNull
@@ -745,7 +750,7 @@ setNotNull = UnsafeAlterColumn $ "SET NOT NULL"
745750-- '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]
746751-- '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4])]
747752-- definition = alterTable #tab (alterColumn #col dropNotNull)
748−-- in Data.ByteString.Char8.putStrLn $ renderDefinition definition
753+-- in printSQL definition
749754-- :}
750755-- ALTER TABLE "tab" ALTER COLUMN "col" DROP NOT NULL;
751756dropNotNull
@@ -763,7 +768,7 @@ dropNotNull = UnsafeAlterColumn $ "DROP NOT NULL"
763768-- '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGnumeric])]
764769-- definition =
765770-- alterTable #tab (alterColumn #col (alterType (numeric & notNullable)))
766−-- in Data.ByteString.Char8.putStrLn $ renderDefinition definition
771+-- in printSQL definition
767772-- :}
768773-- ALTER TABLE "tab" ALTER COLUMN "col" TYPE numeric NOT NULL;
769774alterType :: ColumnTypeExpression schema ty -> AlterColumn schema ty0 ty
@@ -779,7 +784,7 @@ alterType ty = UnsafeAlterColumn $ "TYPE" <+> renderColumnTypeExpression ty
779784-- , "bc" ::: 'View ('["b" ::: 'Null 'PGint4, "c" ::: 'Null 'PGint4])]
780785-- definition =
781786-- createView #bc (select (#b :* #c :* Nil) (from (table #abc)))
782−-- in Data.ByteString.Char8.putStrLn $ renderDefinition definition
787+-- in printSQL definition
783788-- :}
784789-- CREATE VIEW "bc" AS SELECT "b" AS "b", "c" AS "c" FROM "abc" AS "abc";
785790createView
@@ -801,7 +806,7 @@ createView alias query = UnsafeDefinition $
801806-- , "bc" ::: 'View ('["b" ::: 'Null 'PGint4, "c" ::: 'Null 'PGint4])]
802807-- '[ "abc" ::: 'Table ('[] :=> '["a" ::: 'NoDef :=> 'Null 'PGint4, "b" ::: 'NoDef :=> 'Null 'PGint4, "c" ::: 'NoDef :=> 'Null 'PGint4])]
803808-- definition = dropView #bc
804−-- in Data.ByteString.Char8.putStrLn $ renderDefinition definition
809+-- in printSQL definition
805810-- :}
806811-- DROP VIEW "bc";
807812dropView
@@ -812,7 +817,7 @@ dropView v = UnsafeDefinition $ "DROP VIEW" <+> renderAlias v <> ";"
812817
813818-- | Enumerated types are created using the `createTypeEnum` command, for example
814819--
815−-- >>> Data.ByteString.Char8.putStrLn $ renderDefinition $ createTypeEnum #mood (label @"sad" :* label @"ok" :* label @"happy" :* Nil)
820+-- >>> printSQL $ createTypeEnum #mood (label @"sad" :* label @"ok" :* label @"happy" :* Nil)
816821-- CREATE TYPE "mood" AS ENUM ('sad', 'ok', 'happy');
817822createTypeEnum
818823 :: (KnownSymbol enum, SOP.All KnownSymbol labels)
@@ -830,7 +835,7 @@ createTypeEnum enum labels = UnsafeDefinition $
830835-- >>> data Schwarma = Beef | Lamb | Chicken deriving GHC.Generic
831836-- >>> instance SOP.Generic Schwarma
832837-- >>> instance SOP.HasDatatypeInfo Schwarma
833−-- >>> Data.ByteString.Char8.putStrLn $ renderDefinition $ createTypeEnumWith @Schwarma #schwarma
838+-- >>> printSQL $ createTypeEnumWith @Schwarma #schwarma
834839-- CREATE TYPE "schwarma" AS ENUM ('Beef', 'Lamb', 'Chicken');
835840createTypeEnumWith
836841 :: forall hask enum schema.
@@ -847,7 +852,7 @@ createTypeEnumWith enum = createTypeEnum enum
847852-- | `createTypeComposite` creates a composite type. The composite type is
848853-- specified by a list of attribute names and data types.
849854--
850−-- >>> Data.ByteString.Char8.putStrLn $ renderDefinition $ createTypeComposite #complex (float8 `As` #real :* float8 `As` #imaginary :* Nil)
855+-- >>> printSQL $ createTypeComposite #complex (float8 `As` #real :* float8 `As` #imaginary :* Nil)
851856-- CREATE TYPE "complex" AS ("real" float8, "imaginary" float8);
852857createTypeComposite
853858 :: (KnownSymbol ty, SOP.SListI fields)
@@ -869,7 +874,7 @@ createTypeComposite ty fields = UnsafeDefinition $
869874-- >>> data Complex = Complex {real :: Maybe Double, imaginary :: Maybe Double} deriving GHC.Generic
870875-- >>> instance SOP.Generic Complex
871876-- >>> instance SOP.HasDatatypeInfo Complex
872−-- >>> Data.ByteString.Char8.putStrLn $ renderDefinition $ createTypeCompositeWith @Complex #complex
877+-- >>> printSQL $ createTypeCompositeWith @Complex #complex
873878-- CREATE TYPE "complex" AS ("real" float8, "imaginary" float8);
874879createTypeCompositeWith
875880 :: forall hask ty schema.
@@ -890,7 +895,7 @@ createTypeCompositeWith ty = createTypeComposite ty $ zipAs
890895-- >>> data Schwarma = Beef | Lamb | Chicken deriving GHC.Generic
891896-- >>> instance SOP.Generic Schwarma
892897-- >>> instance SOP.HasDatatypeInfo Schwarma
893−-- >>> Data.ByteString.Char8.putStrLn $ renderDefinition (dropType #schwarma :: Definition '["schwarma" ::: 'Typedef (EnumWith Schwarma)] '[])
898+-- >>> printSQL (dropType #schwarma :: Definition '["schwarma" ::: 'Typedef (EnumWith Schwarma)] '[])
894899-- DROP TYPE "schwarma";
895900dropType
896901 :: Has tydef schema ('Typedef ty)
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+128−122
@@ -172,6 +172,9 @@ newtype Expression
172172 = UnsafeExpression { renderExpression :: ByteString }
173173 deriving (GHC.Generic,Show,Eq,Ord,NFData)
174174
175+instance RenderSQL (Expression schema relations grouping params ty) where
176+ renderSQL = renderExpression
177+
175178{- | A `HasParameter` constraint is used to indicate a value that is
176179supplied externally to a SQL statement.
177180`Squeal.PostgreSQL.PQ.manipulateParams`,
@@ -278,15 +281,15 @@ instance
278281
279282-- | analagous to `Nothing`
280283--
281−-- >>> renderExpression $ null_
282−-- "NULL"
284+-- >>> printSQL null_
285+-- NULL
283286null_ :: Expression schema rels grouping params ('Null ty)
284287null_ = UnsafeExpression "NULL"
285288
286289-- | analagous to `Just`
287290--
288−-- >>> renderExpression $ notNull true
289−-- "TRUE"
291+-- >>> printSQL $ notNull true
292+-- TRUE
290293notNull
291294 :: Expression schema rels grouping params ('NotNull ty)
292295 -> Expression schema rels grouping params ('Null ty)
@@ -294,8 +297,8 @@ notNull = UnsafeExpression . renderExpression
294297
295298-- | return the leftmost value which is not NULL
296299--
297−-- >>> renderExpression $ coalesce [null_, notNull true] false
298−-- "COALESCE(NULL, TRUE, FALSE)"
300+-- >>> printSQL $ coalesce [null_, notNull true] false
301+-- COALESCE(NULL, TRUE, FALSE)
299302coalesce
300303 :: [Expression schema relations grouping params ('Null ty)]
301304 -- ^ @NULL@s may be present
@@ -308,8 +311,8 @@ coalesce nullxs notNullx = UnsafeExpression $
308311
309312-- | analagous to `Data.Maybe.fromMaybe` using @COALESCE@
310313--
311−-- >>> renderExpression $ fromNull true null_
312−-- "COALESCE(NULL, TRUE)"
314+-- >>> printSQL $ fromNull true null_
315+-- COALESCE(NULL, TRUE)
313316fromNull
314317 :: Expression schema relations grouping params ('NotNull ty)
315318 -- ^ what to convert @NULL@ to
@@ -317,16 +320,16 @@ fromNull
317320 -> Expression schema relations grouping params ('NotNull ty)
318321fromNull notNullx nullx = coalesce [nullx] notNullx
319322
320−-- | >>> renderExpression $ null_ & isNull
321−-- "NULL IS NULL"
323+-- | >>> printSQL $ null_ & isNull
324+-- NULL IS NULL
322325isNull
323326 :: Expression schema relations grouping params ('Null ty)
324327 -- ^ possibly @NULL@
325328 -> Condition schema relations grouping params
326329isNull x = UnsafeExpression $ renderExpression x <+> "IS NULL"
327330
328−-- | >>> renderExpression $ null_ & isNotNull
329−-- "NULL IS NOT NULL"
331+-- | >>> printSQL $ null_ & isNotNull
332+-- NULL IS NOT NULL
330333isNotNull
331334 :: Expression schema relations grouping params ('Null ty)
332335 -- ^ possibly @NULL@
@@ -335,8 +338,8 @@ isNotNull x = UnsafeExpression $ renderExpression x <+> "IS NOT NULL"
335338
336339-- | analagous to `maybe` using @IS NULL@
337340--
338−-- >>> renderExpression $ matchNull true not_ null_
339−-- "CASE WHEN NULL IS NULL THEN TRUE ELSE (NOT NULL) END"
341+-- >>> printSQL $ matchNull true not_ null_
342+-- CASE WHEN NULL IS NULL THEN TRUE ELSE (NOT NULL) END
340343matchNull
341344 :: Expression schema relations grouping params (nullty)
342345 -- ^ what to convert @NULL@ to
@@ -348,12 +351,14 @@ matchNull
348351matchNull y f x = ifThenElse (isNull x) y
349352 (f (UnsafeExpression (renderExpression x)))
350353
351−-- | right inverse to `fromNull`, if its arguments are equal then
352−-- `nullIf` gives @NULL@.
353−--
354−-- >>> :set -XTypeApplications -XDataKinds
355−-- >>> renderExpression @'[] @_ @_ @'[_] $ fromNull false (nullIf false (param @1))
356−-- "COALESCE(NULL IF (FALSE, ($1 :: bool)), FALSE)"
354+{-| right inverse to `fromNull`, if its arguments are equal then
355+`nullIf` gives @NULL@.
356+
357+>>> :set -XTypeApplications -XDataKinds
358+>>> let expr = nullIf false (param @1) :: Expression schema rels grp '[ 'NotNull 'PGbool] ('Null 'PGbool)
359+>>> printSQL expr
360+NULL IF (FALSE, ($1 :: bool))
361+-}
357362nullIf
358363 :: Expression schema relations grouping params ('NotNull ty)
359364 -- ^ @NULL@ is absent
@@ -363,8 +368,8 @@ nullIf
363368nullIf x y = UnsafeExpression $ "NULL IF" <+> parenthesized
364369 (renderExpression x <> ", " <> renderExpression y)
365370
366−-- | >>> renderExpression $ array [null_, notNull false, notNull true]
367−-- "ARRAY[NULL, FALSE, TRUE]"
371+-- | >>> printSQL $ array [null_, notNull false, notNull true]
372+-- ARRAY[NULL, FALSE, TRUE]
368373array
369374 :: [Expression schema relations grouping params ('Null ty)]
370375 -- ^ array elements
@@ -381,8 +386,8 @@ instance (KnownSymbol label, label `In` labels) => IsPGlabel label
381386--
382387-- >>> type Complex = PGcomposite '["real" ::: 'PGfloat8, "imaginary" ::: 'PGfloat8]
383388-- >>> let i = row (0 `As` #real :* 1 `As` #imaginary :* Nil) :: Expression '[] '[] 'Ungrouped '[] ('NotNull Complex)
384−-- >>> renderExpression i
385−-- "ROW(0, 1)"
389+-- >>> printSQL i
390+-- ROW(0, 1)
386391row
387392 :: SListI (Nulls fields)
388393 => NP (Aliased (Expression schema relations grouping params)) (Nulls fields)
@@ -407,8 +412,9 @@ instance Monoid
407412 mempty = array []
408413 mappend = (<>)
409414
410−-- | >>> renderExpression @'[] @_ @_ @'[_] $ greatest currentTimestamp [param @1]
411−-- "GREATEST(CURRENT_TIMESTAMP, ($1 :: timestamp with time zone))"
415+-- | >>> let expr = greatest currentTimestamp [param @1] :: Expression sch rels grp '[ 'NotNull 'PGtimestamptz] ('NotNull 'PGtimestamptz)
416+-- >>> printSQL expr
417+-- GREATEST(CURRENT_TIMESTAMP, ($1 :: timestamp with time zone))
412418greatest
413419 :: Expression schema relations grouping params (nullty)
414420 -- ^ needs at least 1 argument
@@ -418,8 +424,8 @@ greatest
418424greatest x xs = UnsafeExpression $ "GREATEST("
419425 <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
420426
421−-- | >>> renderExpression $ least currentTimestamp [null_]
422−-- "LEAST(CURRENT_TIMESTAMP, NULL)"
427+-- | >>> printSQL $ least currentTimestamp [null_]
428+-- LEAST(CURRENT_TIMESTAMP, NULL)
423429least
424430 :: Expression schema relations grouping params (nullty)
425431 -- ^ needs at least 1 argument
@@ -429,8 +435,8 @@ least
429435least x xs = UnsafeExpression $ "LEAST("
430436 <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
431437
432−-- | >>> renderExpression $ unsafeBinaryOp "OR" true false
433−-- "(TRUE OR FALSE)"
438+-- | >>> printSQL $ unsafeBinaryOp "OR" true false
439+-- (TRUE OR FALSE)
434440unsafeBinaryOp
435441 :: ByteString
436442 -- ^ operator
@@ -440,8 +446,8 @@ unsafeBinaryOp
440446unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
441447 renderExpression x <+> op <+> renderExpression y
442448
443−-- | >>> renderExpression $ unsafeUnaryOp "NOT" true
444−-- "(NOT TRUE)"
449+-- | >>> printSQL $ unsafeUnaryOp "NOT" true
450+-- (NOT TRUE)
445451unsafeUnaryOp
446452 :: ByteString
447453 -- ^ operator
@@ -450,8 +456,8 @@ unsafeUnaryOp
450456unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
451457 op <+> renderExpression x
452458
453−-- | >>> renderExpression $ unsafeFunction "f" true
454−-- "f(TRUE)"
459+-- | >>> printSQL $ unsafeFunction "f" true
460+-- f(TRUE)
455461unsafeFunction
456462 :: ByteString
457463 -- ^ function
@@ -503,9 +509,9 @@ instance (PGNum ty, PGFloating ty) => Floating
503509-- let
504510-- expression :: Expression schema relations grouping params (nullity 'PGfloat4)
505511-- expression = atan2_ pi 2
506−-- in renderExpression expression
512+-- in printSQL expression
507513-- :}
508−-- "atan2(pi(), 2)"
514+-- atan2(pi(), 2)
509515atan2_
510516 :: PGFloating float
511517 => Expression schema relations grouping params (nullity float)
@@ -520,8 +526,8 @@ atan2_ y x = UnsafeExpression $
520526-- represents a run-time type conversion. The cast will succeed only if a
521527-- suitable type conversion operation has been defined.
522528--
523−-- | >>> renderExpression $ true & cast int4
524−-- "(TRUE :: int4)"
529+-- | >>> printSQL $ true & cast int4
530+-- (TRUE :: int4)
525531cast
526532 :: TypeExpression schema ty1
527533 -- ^ type to cast as
@@ -537,9 +543,9 @@ cast ty x = UnsafeExpression $ parenthesized $
537543-- let
538544-- expression :: Expression schema relations grouping params (nullity 'PGint2)
539545-- expression = 5 `quot_` 2
540−-- in renderExpression expression
546+-- in printSQL expression
541547-- :}
542−-- "(5 / 2)"
548+-- (5 / 2)
543549quot_
544550 :: PGIntegral int
545551 => Expression schema relations grouping params (nullity int)
@@ -555,9 +561,9 @@ quot_ = unsafeBinaryOp "/"
555561-- let
556562-- expression :: Expression schema relations grouping params (nullity 'PGint2)
557563-- expression = 5 `rem_` 2
558−-- in renderExpression expression
564+-- in printSQL expression
559565-- :}
560−-- "(5 % 2)"
566+-- (5 % 2)
561567rem_
562568 :: PGIntegral int
563569 => Expression schema relations grouping params (nullity int)
@@ -571,9 +577,9 @@ rem_ = unsafeBinaryOp "%"
571577-- let
572578-- expression :: Expression schema relations grouping params (nullity 'PGfloat4)
573579-- expression = trunc pi
574−-- in renderExpression expression
580+-- in printSQL expression
575581-- :}
576−-- "trunc(pi())"
582+-- trunc(pi())
577583trunc
578584 :: PGFloating frac
579585 => Expression schema relations grouping params (nullity frac)
@@ -585,9 +591,9 @@ trunc = unsafeFunction "trunc"
585591-- let
586592-- expression :: Expression schema relations grouping params (nullity 'PGfloat4)
587593-- expression = round_ pi
588−-- in renderExpression expression
594+-- in printSQL expression
589595-- :}
590−-- "round(pi())"
596+-- round(pi())
591597round_
592598 :: PGFloating frac
593599 => Expression schema relations grouping params (nullity frac)
@@ -599,9 +605,9 @@ round_ = unsafeFunction "round"
599605-- let
600606-- expression :: Expression schema relations grouping params (nullity 'PGfloat4)
601607-- expression = ceiling_ pi
602−-- in renderExpression expression
608+-- in printSQL expression
603609-- :}
604−-- "ceiling(pi())"
610+-- ceiling(pi())
605611ceiling_
606612 :: PGFloating frac
607613 => Expression schema relations grouping params (nullity frac)
@@ -616,33 +622,33 @@ ceiling_ = unsafeFunction "ceiling"
616622type Condition schema relations grouping params =
617623 Expression schema relations grouping params ('NotNull 'PGbool)
618624
619−-- | >>> renderExpression true
620−-- "TRUE"
625+-- | >>> printSQL true
626+-- TRUE
621627true :: Condition schema relations grouping params
622628true = UnsafeExpression "TRUE"
623629
624−-- | >>> renderExpression false
625−-- "FALSE"
630+-- | >>> printSQL false
631+-- FALSE
626632false :: Condition schema relations grouping params
627633false = UnsafeExpression "FALSE"
628634
629−-- | >>> renderExpression $ not_ true
630−-- "(NOT TRUE)"
635+-- | >>> printSQL $ not_ true
636+-- (NOT TRUE)
631637not_
632638 :: Condition schema relations grouping params
633639 -> Condition schema relations grouping params
634640not_ = unsafeUnaryOp "NOT"
635641
636−-- | >>> renderExpression $ true .&& false
637−-- "(TRUE AND FALSE)"
642+-- | >>> printSQL $ true .&& false
643+-- (TRUE AND FALSE)
638644(.&&)
639645 :: Condition schema relations grouping params
640646 -> Condition schema relations grouping params
641647 -> Condition schema relations grouping params
642648(.&&) = unsafeBinaryOp "AND"
643649
644−-- | >>> renderExpression $ true .|| false
645−-- "(TRUE OR FALSE)"
650+-- | >>> printSQL $ true .|| false
651+-- (TRUE OR FALSE)
646652(.||)
647653 :: Condition schema relations grouping params
648654 -> Condition schema relations grouping params
@@ -653,9 +659,9 @@ not_ = unsafeUnaryOp "NOT"
653659-- let
654660-- expression :: Expression schema relations grouping params (nullity 'PGint2)
655661-- expression = caseWhenThenElse [(true, 1), (false, 2)] 3
656−-- in renderExpression expression
662+-- in printSQL expression
657663-- :}
658−-- "CASE WHEN TRUE THEN 1 WHEN FALSE THEN 2 ELSE 3 END"
664+-- CASE WHEN TRUE THEN 1 WHEN FALSE THEN 2 ELSE 3 END
659665caseWhenThenElse
660666 :: [ ( Condition schema relations grouping params
661667 , Expression schema relations grouping params (ty)
@@ -681,9 +687,9 @@ caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
681687-- let
682688-- expression :: Expression schema relations grouping params (nullity 'PGint2)
683689-- expression = ifThenElse true 1 0
684−-- in renderExpression expression
690+-- in printSQL expression
685691-- :}
686−-- "CASE WHEN TRUE THEN 1 ELSE 0 END"
692+-- CASE WHEN TRUE THEN 1 ELSE 0 END
687693ifThenElse
688694 :: Condition schema relations grouping params
689695 -> Expression schema relations grouping params (ty) -- ^ then
@@ -694,8 +700,8 @@ ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
694700-- | Comparison operations like `.==`, `./=`, `.>`, `.>=`, `.<` and `.<=`
695701-- will produce @NULL@s if one of their arguments is @NULL@.
696702--
697−-- >>> renderExpression $ notNull true .== null_
698−-- "(TRUE = NULL)"
703+-- >>> printSQL $ notNull true .== null_
704+-- (TRUE = NULL)
699705(.==)
700706 :: Expression schema relations grouping params (nullity ty) -- ^ lhs
701707 -> Expression schema relations grouping params (nullity ty) -- ^ rhs
@@ -703,8 +709,8 @@ ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
703709(.==) = unsafeBinaryOp "="
704710infix 4 .==
705711
706−-- | >>> renderExpression $ notNull true ./= null_
707−-- "(TRUE <> NULL)"
712+-- | >>> printSQL $ notNull true ./= null_
713+-- (TRUE <> NULL)
708714(./=)
709715 :: Expression schema relations grouping params (nullity ty) -- ^ lhs
710716 -> Expression schema relations grouping params (nullity ty) -- ^ rhs
@@ -712,8 +718,8 @@ infix 4 .==
712718(./=) = unsafeBinaryOp "<>"
713719infix 4 ./=
714720
715−-- | >>> renderExpression $ notNull true .>= null_
716−-- "(TRUE >= NULL)"
721+-- | >>> printSQL $ notNull true .>= null_
722+-- (TRUE >= NULL)
717723(.>=)
718724 :: Expression schema relations grouping params (nullity ty) -- ^ lhs
719725 -> Expression schema relations grouping params (nullity ty) -- ^ rhs
@@ -721,8 +727,8 @@ infix 4 ./=
721727(.>=) = unsafeBinaryOp ">="
722728infix 4 .>=
723729
724−-- | >>> renderExpression $ notNull true .< null_
725−-- "(TRUE < NULL)"
730+-- | >>> printSQL $ notNull true .< null_
731+-- (TRUE < NULL)
726732(.<)
727733 :: Expression schema relations grouping params (nullity ty) -- ^ lhs
728734 -> Expression schema relations grouping params (nullity ty) -- ^ rhs
@@ -730,8 +736,8 @@ infix 4 .>=
730736(.<) = unsafeBinaryOp "<"
731737infix 4 .<
732738
733−-- | >>> renderExpression $ notNull true .<= null_
734−-- "(TRUE <= NULL)"
739+-- | >>> printSQL $ notNull true .<= null_
740+-- (TRUE <= NULL)
735741(.<=)
736742 :: Expression schema relations grouping params (nullity ty) -- ^ lhs
737743 -> Expression schema relations grouping params (nullity ty) -- ^ rhs
@@ -739,8 +745,8 @@ infix 4 .<
739745(.<=) = unsafeBinaryOp "<="
740746infix 4 .<=
741747
742−-- | >>> renderExpression $ notNull true .> null_
743−-- "(TRUE > NULL)"
748+-- | >>> printSQL $ notNull true .> null_
749+-- (TRUE > NULL)
744750(.>)
745751 :: Expression schema relations grouping params (nullity ty) -- ^ lhs
746752 -> Expression schema relations grouping params (nullity ty) -- ^ rhs
@@ -748,32 +754,32 @@ infix 4 .<=
748754(.>) = unsafeBinaryOp ">"
749755infix 4 .>
750756
751−-- | >>> renderExpression currentDate
752−-- "CURRENT_DATE"
757+-- | >>> printSQL currentDate
758+-- CURRENT_DATE
753759currentDate
754760 :: Expression schema relations grouping params (nullity 'PGdate)
755761currentDate = UnsafeExpression "CURRENT_DATE"
756762
757−-- | >>> renderExpression currentTime
758−-- "CURRENT_TIME"
763+-- | >>> printSQL currentTime
764+-- CURRENT_TIME
759765currentTime
760766 :: Expression schema relations grouping params (nullity 'PGtimetz)
761767currentTime = UnsafeExpression "CURRENT_TIME"
762768
763−-- | >>> renderExpression currentTimestamp
764−-- "CURRENT_TIMESTAMP"
769+-- | >>> printSQL currentTimestamp
770+-- CURRENT_TIMESTAMP
765771currentTimestamp
766772 :: Expression schema relations grouping params (nullity 'PGtimestamptz)
767773currentTimestamp = UnsafeExpression "CURRENT_TIMESTAMP"
768774
769−-- | >>> renderExpression localTime
770−-- "LOCALTIME"
775+-- | >>> printSQL localTime
776+-- LOCALTIME
771777localTime
772778 :: Expression schema relations grouping params (nullity 'PGtime)
773779localTime = UnsafeExpression "LOCALTIME"
774780
775−-- | >>> renderExpression localTimestamp
776−-- "LOCALTIMESTAMP"
781+-- | >>> printSQL localTimestamp
782+-- LOCALTIMESTAMP
777783localTimestamp
778784 :: Expression schema relations grouping params (nullity 'PGtimestamp)
779785localTimestamp = UnsafeExpression "LOCALTIMESTAMP"
@@ -807,24 +813,24 @@ instance Monoid
807813 mempty = fromString ""
808814 mappend = (<>)
809815
810−-- | >>> renderExpression $ lower "ARRRGGG"
811−-- "lower(E'ARRRGGG')"
816+-- | >>> printSQL $ lower "ARRRGGG"
817+-- lower(E'ARRRGGG')
812818lower
813819 :: Expression schema relations grouping params (nullity 'PGtext)
814820 -- ^ string to lower case
815821 -> Expression schema relations grouping params (nullity 'PGtext)
816822lower = unsafeFunction "lower"
817823
818−-- | >>> renderExpression $ upper "eeee"
819−-- "upper(E'eeee')"
824+-- | >>> printSQL $ upper "eeee"
825+-- upper(E'eeee')
820826upper
821827 :: Expression schema relations grouping params (nullity 'PGtext)
822828 -- ^ string to upper case
823829 -> Expression schema relations grouping params (nullity 'PGtext)
824830upper = unsafeFunction "upper"
825831
826−-- | >>> renderExpression $ charLength "four"
827−-- "char_length(E'four')"
832+-- | >>> printSQL $ charLength "four"
833+-- char_length(E'four')
828834charLength
829835 :: Expression schema relations grouping params (nullity 'PGtext)
830836 -- ^ string to measure
@@ -838,8 +844,8 @@ charLength = unsafeFunction "char_length"
838844-- in pattern stands for (matches) any single character; a percent sign (%)
839845-- matches any sequence of zero or more characters.
840846--
841−-- >>> renderExpression $ "abc" `like` "a%"
842−-- "(E'abc' LIKE E'a%')"
847+-- >>> printSQL $ "abc" `like` "a%"
848+-- (E'abc' LIKE E'a%')
843849like
844850 :: Expression schema relations grouping params (nullity 'PGtext)
845851 -- ^ string
@@ -872,9 +878,9 @@ unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
872878-- let
873879-- expression :: Expression schema '[tab ::: '["col" ::: 'Null 'PGnumeric]] ('Grouped bys) params ('Null 'PGnumeric)
874880-- expression = sum_ #col
875−-- in renderExpression expression
881+-- in printSQL expression
876882-- :}
877−-- "sum(\"col\")"
883+-- sum("col")
878884sum_
879885 :: PGNum ty
880886 => Expression schema relations 'Ungrouped params (nullity ty)
@@ -886,9 +892,9 @@ sum_ = unsafeAggregate "sum"
886892-- let
887893-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGnumeric]] ('Grouped bys) params (nullity 'PGnumeric)
888894-- expression = sumDistinct #col
889−-- in renderExpression expression
895+-- in printSQL expression
890896-- :}
891−-- "sum(DISTINCT \"col\")"
897+-- sum(DISTINCT "col")
892898sumDistinct
893899 :: PGNum ty
894900 => Expression schema relations 'Ungrouped params (nullity ty)
@@ -917,9 +923,9 @@ instance PGAvg 'PGinterval 'PGinterval
917923-- let
918924-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGint4]] (Grouped bys) params (nullity 'PGint4)
919925-- expression = bitAnd #col
920−-- in renderExpression expression
926+-- in printSQL expression
921927-- :}
922−-- "bit_and(\"col\")"
928+-- bit_and("col")
923929bitAnd
924930 :: PGIntegral int
925931 => Expression schema relations 'Ungrouped params (nullity int)
@@ -931,9 +937,9 @@ bitAnd = unsafeAggregate "bit_and"
931937-- let
932938-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGint4]] (Grouped bys) params (nullity 'PGint4)
933939-- expression = bitOr #col
934−-- in renderExpression expression
940+-- in printSQL expression
935941-- :}
936−-- "bit_or(\"col\")"
942+-- bit_or("col")
937943bitOr
938944 :: PGIntegral int
939945 => Expression schema relations 'Ungrouped params (nullity int)
@@ -945,9 +951,9 @@ bitOr = unsafeAggregate "bit_or"
945951-- let
946952-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGint4]] (Grouped bys) params (nullity 'PGint4)
947953-- expression = bitAndDistinct #col
948−-- in renderExpression expression
954+-- in printSQL expression
949955-- :}
950−-- "bit_and(DISTINCT \"col\")"
956+-- bit_and(DISTINCT "col")
951957bitAndDistinct
952958 :: PGIntegral int
953959 => Expression schema relations 'Ungrouped params (nullity int)
@@ -959,9 +965,9 @@ bitAndDistinct = unsafeAggregateDistinct "bit_and"
959965-- let
960966-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGint4]] (Grouped bys) params (nullity 'PGint4)
961967-- expression = bitOrDistinct #col
962−-- in renderExpression expression
968+-- in printSQL expression
963969-- :}
964−-- "bit_or(DISTINCT \"col\")"
970+-- bit_or(DISTINCT "col")
965971bitOrDistinct
966972 :: PGIntegral int
967973 => Expression schema relations 'Ungrouped params (nullity int)
@@ -973,9 +979,9 @@ bitOrDistinct = unsafeAggregateDistinct "bit_or"
973979-- let
974980-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
975981-- expression = boolAnd #col
976−-- in renderExpression expression
982+-- in printSQL expression
977983-- :}
978−-- "bool_and(\"col\")"
984+-- bool_and("col")
979985boolAnd
980986 :: Expression schema relations 'Ungrouped params (nullity 'PGbool)
981987 -- ^ what to aggregate
@@ -986,9 +992,9 @@ boolAnd = unsafeAggregate "bool_and"
986992-- let
987993-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
988994-- expression = boolOr #col
989−-- in renderExpression expression
995+-- in printSQL expression
990996-- :}
991−-- "bool_or(\"col\")"
997+-- bool_or("col")
992998boolOr
993999 :: Expression schema relations 'Ungrouped params (nullity 'PGbool)
9941000 -- ^ what to aggregate
@@ -999,9 +1005,9 @@ boolOr = unsafeAggregate "bool_or"
9991005-- let
10001006-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
10011007-- expression = boolAndDistinct #col
1002−-- in renderExpression expression
1008+-- in printSQL expression
10031009-- :}
1004−-- "bool_and(DISTINCT \"col\")"
1010+-- bool_and(DISTINCT "col")
10051011boolAndDistinct
10061012 :: Expression schema relations 'Ungrouped params (nullity 'PGbool)
10071013 -- ^ what to aggregate
@@ -1012,9 +1018,9 @@ boolAndDistinct = unsafeAggregateDistinct "bool_and"
10121018-- let
10131019-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
10141020-- expression = boolOrDistinct #col
1015−-- in renderExpression expression
1021+-- in printSQL expression
10161022-- :}
1017−-- "bool_or(DISTINCT \"col\")"
1023+-- bool_or(DISTINCT "col")
10181024boolOrDistinct
10191025 :: Expression schema relations 'Ungrouped params (nullity 'PGbool)
10201026 -- ^ what to aggregate
@@ -1023,8 +1029,8 @@ boolOrDistinct = unsafeAggregateDistinct "bool_or"
10231029
10241030-- | A special aggregation that does not require an input
10251031--
1026−-- >>> renderExpression countStar
1027−-- "count(*)"
1032+-- >>> printSQL countStar
1033+-- count(*)
10281034countStar
10291035 :: Expression schema relations ('Grouped bys) params ('NotNull 'PGint8)
10301036countStar = UnsafeExpression $ "count(*)"
@@ -1033,9 +1039,9 @@ countStar = UnsafeExpression $ "count(*)"
10331039-- let
10341040-- expression :: Expression schema '[tab ::: '["col" ::: nullity ty]] (Grouped bys) params ('NotNull 'PGint8)
10351041-- expression = count #col
1036−-- in renderExpression expression
1042+-- in printSQL expression
10371043-- :}
1038−-- "count(\"col\")"
1044+-- count("col")
10391045count
10401046 :: Expression schema relations 'Ungrouped params ty
10411047 -- ^ what to count
@@ -1046,9 +1052,9 @@ count = unsafeAggregate "count"
10461052-- let
10471053-- expression :: Expression schema '[tab ::: '["col" ::: nullity ty]] (Grouped bys) params ('NotNull 'PGint8)
10481054-- expression = countDistinct #col
1049−-- in renderExpression expression
1055+-- in printSQL expression
10501056-- :}
1051−-- "count(DISTINCT \"col\")"
1057+-- count(DISTINCT "col")
10521058countDistinct
10531059 :: Expression schema relations 'Ungrouped params ty
10541060 -- ^ what to count
@@ -1061,9 +1067,9 @@ countDistinct = unsafeAggregateDistinct "count"
10611067-- let
10621068-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
10631069-- expression = every #col
1064−-- in renderExpression expression
1070+-- in printSQL expression
10651071-- :}
1066−-- "every(\"col\")"
1072+-- every("col")
10671073every
10681074 :: Expression schema relations 'Ungrouped params (nullity 'PGbool)
10691075 -- ^ what to aggregate
@@ -1076,9 +1082,9 @@ every = unsafeAggregate "every"
10761082-- let
10771083-- expression :: Expression schema '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
10781084-- expression = everyDistinct #col
1079−-- in renderExpression expression
1085+-- in printSQL expression
10801086-- :}
1081−-- "every(DISTINCT \"col\")"
1087+-- every(DISTINCT "col")
10821088everyDistinct
10831089 :: Expression schema relations 'Ungrouped params (nullity 'PGbool)
10841090 -- ^ what to aggregate
squeal-postgresql/src/Squeal/PostgreSQL/Render.hs+12−1
@@ -9,7 +9,8 @@ Rendering helper functions.
99-}
1010
1111{-# LANGUAGE
12− MagicHash
12+ FlexibleContexts
13+ , MagicHash
1314 , OverloadedStrings
1415 , PolyKinds
1516 , RankNTypes
@@ -25,8 +26,11 @@ module Squeal.PostgreSQL.Render
2526 , renderCommaSeparated
2627 , renderCommaSeparatedMaybe
2728 , renderNat
29+ , RenderSQL (..)
30+ , printSQL
2831 ) where
2932
33+import Control.Monad.Base
3034import Data.ByteString (ByteString)
3135import Data.Maybe
3236import Data.Monoid ((<>))
@@ -35,6 +39,7 @@ import GHC.Exts
3539import GHC.TypeLits
3640
3741import qualified Data.ByteString as ByteString
42+import qualified Data.ByteString.Char8 as Char8
3843
3944-- | Parenthesize a `ByteString`.
4045parenthesized :: ByteString -> ByteString
@@ -77,3 +82,9 @@ renderCommaSeparatedMaybe render
7782-- | Render a promoted `Nat`.
7883renderNat :: KnownNat n => proxy n -> ByteString
7984renderNat (_ :: proxy n) = fromString (show (natVal' (proxy# :: Proxy# n)))
85+
86+class RenderSQL sql where
87+ renderSQL :: sql -> ByteString
88+
89+printSQL :: (RenderSQL sql, MonadBase IO io) => sql -> io ()
90+printSQL = liftBase . Char8.putStrLn . renderSQL