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

Render SQL statements in a single line. #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Test/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ table4columndata :: [(Column O.PGInt4, Column O.PGInt4)]
table4columndata = table4dataG

table6data :: [(String, String)]
table6data = [("xy", "a"), ("z", "a"), ("more text", "a")]
table6data = [("xy", "a"), ("z", "a"), ("more \n text", "a")]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor test to check whether newlines are preserved in strings. They are.


table6columndata :: [(Column O.PGText, Column O.PGText)]
table6columndata = map (\(column1, column2) -> (O.pgString column1, O.pgString column2)) table6data
Expand Down
6 changes: 6 additions & 0 deletions src/Opaleye/Internal/Print.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ import Opaleye.Internal.Sql (Select(SelectFrom, Table,
import qualified Opaleye.Internal.HaskellDB.Sql as HSql
import qualified Opaleye.Internal.HaskellDB.Sql.Print as HPrint

import qualified Text.PrettyPrint.HughesPJ as PP
import Text.PrettyPrint.HughesPJ (Doc, ($$), (<+>), text, empty,
parens)

import qualified Data.List.NonEmpty as NEL


renderDoc :: Doc -> String
renderDoc = PP.renderStyle (PP.style {PP.mode = PP.OneLineMode})

type TableAlias = String

ppSql :: Select -> Doc
Expand Down
12 changes: 6 additions & 6 deletions src/Opaleye/Manipulation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ arrangeInsert :: T.Table columns a -> columns -> HSql.SqlInsert
arrangeInsert t c = arrangeInsertMany t (return c)

arrangeInsertSql :: T.Table columns a -> columns -> String
arrangeInsertSql = show . HPrint.ppInsert .: arrangeInsert
arrangeInsertSql = Print.renderDoc . HPrint.ppInsert .: arrangeInsert

runInsert :: PGS.Connection -> T.Table columns columns' -> columns -> IO Int64
runInsert conn = PGS.execute_ conn . fromString .: arrangeInsertSql
Expand All @@ -43,7 +43,7 @@ arrangeInsertMany (T.Table tableName (TI.TableProperties writer _)) columns = in
tableName columnNames columnExprs

arrangeInsertManySql :: T.Table columns a -> NEL.NonEmpty columns -> String
arrangeInsertManySql = show . HPrint.ppInsert .: arrangeInsertMany
arrangeInsertManySql = Print.renderDoc . HPrint.ppInsert .: arrangeInsertMany

runInsertMany :: PGS.Connection
-> T.Table columns columns'
Expand All @@ -68,7 +68,7 @@ arrangeUpdate (TI.Table tableName (TI.TableProperties writer (TI.View tableCols)
arrangeUpdateSql :: T.Table columnsW columnsR
-> (columnsR -> columnsW) -> (columnsR -> Column PGBool)
-> String
arrangeUpdateSql = show . HPrint.ppUpdate .:. arrangeUpdate
arrangeUpdateSql = Print.renderDoc . HPrint.ppUpdate .:. arrangeUpdate

runUpdate :: PGS.Connection -> T.Table columnsW columnsR
-> (columnsR -> columnsW) -> (columnsR -> Column PGBool)
Expand All @@ -82,7 +82,7 @@ arrangeDelete (TI.Table tableName (TI.TableProperties _ (TI.View tableCols)))
where Column condExpr = cond tableCols

arrangeDeleteSql :: T.Table a columnsR -> (columnsR -> Column PGBool) -> String
arrangeDeleteSql = show . HPrint.ppDelete .: arrangeDelete
arrangeDeleteSql = Print.renderDoc . HPrint.ppDelete .: arrangeDelete

runDelete :: PGS.Connection -> T.Table a columnsR -> (columnsR -> Column PGBool)
-> IO Int64
Expand All @@ -105,7 +105,7 @@ arrangeInsertReturningSql :: U.Unpackspec returned ignored
-> columnsW
-> (columnsR -> returned)
-> String
arrangeInsertReturningSql = show
arrangeInsertReturningSql = Print.renderDoc
. Print.ppInsertReturning
.:: arrangeInsertReturning

Expand Down Expand Up @@ -155,7 +155,7 @@ arrangeUpdateReturningSql :: U.Unpackspec returned ignored
-> (columnsR -> Column PGBool)
-> (columnsR -> returned)
-> String
arrangeUpdateReturningSql = show
arrangeUpdateReturningSql = Print.renderDoc
. Print.ppUpdateReturning
.::. arrangeUpdateReturning

Expand Down
2 changes: 1 addition & 1 deletion src/Opaleye/Sql.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ showSqlForPostgresUnoptExplicit :: U.Unpackspec columns b -> Q.Query columns ->
showSqlForPostgresUnoptExplicit = formatAndShowSQL .: Q.runQueryArrUnpack

formatAndShowSQL :: ([HPQ.PrimExpr], PQ.PrimQuery, T.Tag) -> String
formatAndShowSQL = show . Pr.ppSql . Sql.sql
formatAndShowSQL = Pr.renderDoc . Pr.ppSql . Sql.sql