Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 7141ef5

Browse files
authored
Merge pull request #48 from garyb/pretty
Add functions for slightly prettier (indented) formatting
2 parents f5e3c78 + 1aed57a commit 7141ef5

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

src/SqlSquared.purs

+13-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ module SqlSquared
33
, SqlQuery
44
, SqlModule
55
, print
6+
, printPretty
67
, printQuery
8+
, printQueryPretty
79
, printModule
10+
, printModulePretty
811
, genSql
912
, genSqlQuery
1013
, genSqlModule
@@ -25,7 +28,7 @@ import Matryoshka (cata, anaM)
2528
import SqlSquared.Constructors (array, as, as', binop, bool, buildSelect, groupBy, having, hugeNum, ident, ident', int, invokeFunction, invokeFunction', let', let_, map_, match, match', null, num, parens, projection, select, select', set, splice, string, switch, switch', then_, unop, var, when) as Constructors
2629
import SqlSquared.Lenses (_ArrayLiteral, _Binop, _BoolLiteral, _Case, _DecimalLiteral, _ExprRelation, _GroupBy, _Identifier, _IntLiteral, _InvokeFunction, _JoinRelation, _Let, _Literal, _MapLiteral, _Match, _NullLiteral, _OrderBy, _Parens, _Projection, _Select, _SetLiteral, _Splice, _StringLiteral, _Switch, _TableRelation, _Unop, _Var, _VarRelation, _alias, _aliasName, _args, _bindTo, _cases, _clause, _cond, _else, _expr, _filter, _groupBy, _having, _ident, _in, _isDistinct, _joinType, _keys, _left, _lhs, _name, _op, _orderBy, _projections, _relations, _rhs, _right, _tablePath) as Lenses
2730
import SqlSquared.Parser (Literal(..), PositionedToken, parse, parseModule, parseQuery, prettyParse) as Parser
28-
import SqlSquared.Signature (type (×), BinaryOperator(..), BinopR, Case(..), ExprRelR, FunctionDeclR, GroupBy(..), Ident(..), InvokeFunctionR, JoinRelR, JoinType(..), LetR, MatchR, OrderBy(..), OrderType(..), Projection(..), Relation(..), SelectR, SqlDeclF(..), SqlF(..), SqlModuleF(..), SqlQueryF(..), SwitchR, TableRelR, UnaryOperator(..), UnopR, VarRelR, binopFromString, binopToString, genBinaryOperator, genCase, genGroupBy, genJoinType, genOrderBy, genOrderType, genProjection, genRelation, genSqlDeclF, genSqlF, genSqlModuleF, genSqlQueryF, genUnaryOperator, joinTypeFromString, orderTypeFromString, printBinaryOperator, printCase, printGroupBy, printIdent, printJoinType, printOrderBy, printOrderType, printProjection, printRelation, printSqlDeclF, printSqlF, printSqlModuleF, printSqlQueryF, printUnaryOperator, unopFromString, unopToString, (×), (∘), (⋙)) as Sig
31+
import SqlSquared.Signature (type (×), BinaryOperator(..), BinopR, Case(..), ExprRelR, FunctionDeclR, GroupBy(..), Ident(..), InvokeFunctionR, JoinRelR, JoinType(..), LetR, MatchR, OrderBy(..), OrderType(..), Projection(..), Relation(..), SelectR, SqlDeclF(..), SqlF(..), SqlModuleF(..), SqlQueryF(..), SwitchR, TableRelR, UnaryOperator(..), UnopR, VarRelR, binopFromString, binopToString, genBinaryOperator, genCase, genGroupBy, genJoinType, genOrderBy, genOrderType, genProjection, genRelation, genSqlDeclF, genSqlF, genSqlModuleF, genSqlQueryF, genUnaryOperator, joinTypeFromString, orderTypeFromString, printBinaryOperator, printCase, printGroupBy, printIdent, printJoinType, printOrderBy, printOrderType, printProjection, printRelation, printSqlDeclF, printSqlF, printSqlFPretty, printSqlModuleF, printSqlQueryF, printUnaryOperator, unopFromString, unopToString, (×), (∘), (⋙)) as Sig
2932

3033
type Sql = Mu (Sig.SqlF EJ.EJsonF)
3134

@@ -42,6 +45,15 @@ printQuery = Sig.printSqlQueryF <<< map print
4245
printModule SqlModule String
4346
printModule = Sig.printSqlModuleF <<< map print
4447

48+
printPretty Sql String
49+
printPretty = cata $ Sig.printSqlFPretty EJ.renderEJsonF
50+
51+
printQueryPretty SqlQuery String
52+
printQueryPretty = Sig.printSqlQueryF <<< map printPretty
53+
54+
printModulePretty SqlModule String
55+
printModulePretty = Sig.printSqlModuleF <<< map printPretty
56+
4557
genSql m. Gen.MonadGen m MonadRec m m Sql
4658
genSql = Gen.sized $ anaM (Sig.genSqlF EJ.arbitraryEJsonF)
4759

src/SqlSquared/Signature.purs

+22-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module SqlSquared.Signature
1212
, SqlQueryF(..)
1313
, SqlModuleF(..)
1414
, printSqlF
15+
, printSqlFPretty
1516
, printSqlDeclF
1617
, printSqlQueryF
1718
, printSqlModuleF
@@ -47,11 +48,12 @@ import Data.Maybe (Maybe(..))
4748
import Data.Newtype (class Newtype)
4849
import Data.NonEmpty ((:|))
4950
import Data.Ord (class Ord1)
51+
import Data.String as String
5052
import Data.String.Gen as GenS
5153
import Data.Traversable as T
5254
import Matryoshka (Algebra, CoalgebraM, class Corecursive, embed)
5355
import SqlSquared.Path as Pt
54-
import SqlSquared.Signature.BinaryOperator (BinaryOperator(..), binopFromString, binopToString, genBinaryOperator, printBinaryOperator) as BO
56+
import SqlSquared.Signature.BinaryOperator (BinaryOperator(..), binopFromString, binopToString, genBinaryOperator, printBinaryOperator, printBinaryOperatorPretty) as BO
5557
import SqlSquared.Signature.Case (Case(..), genCase, printCase) as CS
5658
import SqlSquared.Signature.GroupBy (GroupBy(..), genGroupBy, printGroupBy) as GB
5759
import SqlSquared.Signature.Ident (Ident(..), genIdent, printIdent) as ID
@@ -361,6 +363,25 @@ printSqlF printLiteralF = case _ of
361363
Parens t →
362364
"(" <> t <> ")"
363365

366+
printSqlFPretty l. Algebra l String Algebra (SqlF l) String
367+
printSqlFPretty printLiteralF = case _ of
368+
Binop {lhs, rhs, op} →
369+
BO.printBinaryOperatorPretty lhs rhs op
370+
Select { isDistinct, projections, relations, filter, groupBy, orderBy } →
371+
"SELECT "
372+
<> (if isDistinct then "DISTINCT " else "")
373+
<> "\n "
374+
<> (F.intercalate ", \n " $ map (reindent ∘ PR.printProjection) projections)
375+
<> (relations # F.foldMap \rs → "\nFROM\n " <> reindent (RL.printRelation rs))
376+
<> (filter # F.foldMap \f → "\nWHERE\n " <> f)
377+
<> (groupBy # F.foldMap \gb → "\nGROUP BY\n " <> reindent (GB.printGroupBy gb))
378+
<> (orderBy # F.foldMap \ob → "\nORDER BY\n " <> reindent (OB.printOrderBy ob))
379+
other →
380+
printSqlF printLiteralF other
381+
382+
reindent String String
383+
reindent = String.replaceAll (String.Pattern "\n") (String.Replacement "\n ")
384+
364385
printSqlDeclF Algebra SqlDeclF String
365386
printSqlDeclF = case _ of
366387
FunctionDecl { ident, args, body } →

src/SqlSquared/Signature/BinaryOperator.purs

+5
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,8 @@ printBinaryOperator lhs rhs = case _ of
147147
IntersectAll → lhs <> " INTERSECT ALL " <> rhs
148148
Except → lhs <> " EXCEPT " <> rhs
149149
UnshiftMap"{" <> lhs <> ": " <> rhs <> "...}"
150+
151+
printBinaryOperatorPretty String String BinaryOperator String
152+
printBinaryOperatorPretty lhs rhs = case _ of
153+
UnionAll → lhs <> "\nUNION ALL\n" <> rhs
154+
other → printBinaryOperator lhs rhs other

src/SqlSquared/Signature/Relation.purs

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Data.Either (Either(..), either)
99
import Data.Foldable as F
1010
import Data.Maybe (Maybe)
1111
import Data.NonEmpty ((:|))
12+
import Data.String as String
1213
import Data.String.Gen as GenS
1314
import Data.Traversable as T
1415
import Matryoshka (Algebra, CoalgebraM)
@@ -83,7 +84,10 @@ instance traversableRelation ∷ T.Traversable Relation where
8384
printRelation Algebra Relation String
8485
printRelation = case _ of
8586
ExprRelation { expr, alias } →
86-
"(" <> expr <> ") AS " <> ID.printIdent alias
87+
let
88+
indented = String.contains (String.Pattern "\n") expr
89+
in
90+
"(" <> expr <> (if indented then "\n" else "") <> ") AS " <> ID.printIdent alias
8791
VarRelation { var, alias } →
8892
":" <> ID.printIdent var <> F.foldMap (\a → " AS " <> ID.printIdent a) alias
8993
TableRelation { path, alias } →

0 commit comments

Comments
 (0)