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

Wrap table names in double quotes #48

Merged
merged 1 commit into from
Jun 22, 2015
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
2 changes: 1 addition & 1 deletion src/Opaleye/Internal/HaskellDB/Sql.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import qualified Data.List.NonEmpty as NEL
-- * SQL data type
-----------------------------------------------------------

type SqlTable = String
newtype SqlTable = SqlTable String deriving Show

newtype SqlColumn = SqlColumn String deriving Show

Expand Down
8 changes: 4 additions & 4 deletions src/Opaleye/Internal/HaskellDB/Sql/Default.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,23 @@ defaultSqlUpdate :: SqlGenerator
-> Assoc -- ^ Update the data with this.
-> SqlUpdate
defaultSqlUpdate gen name criteria assigns
= SqlUpdate name (toSqlAssoc gen assigns) (map (sqlExpr gen) criteria)
= SqlUpdate (SqlTable name) (toSqlAssoc gen assigns) (map (sqlExpr gen) criteria)


defaultSqlInsert :: SqlGenerator
-> TableName
-> [Attribute]
-> NEL.NonEmpty [PrimExpr]
-> SqlInsert
defaultSqlInsert gen table attrs exprs =
SqlInsert table (map toSqlColumn attrs) ((fmap . map) (sqlExpr gen) exprs)
defaultSqlInsert gen name attrs exprs =
SqlInsert (SqlTable name) (map toSqlColumn attrs) ((fmap . map) (sqlExpr gen) exprs)

defaultSqlDelete :: SqlGenerator
-> TableName -- ^ Name of the table
-> [PrimExpr] -- ^ Criteria which must all be true for a row
-- to be deleted.
-> SqlDelete
defaultSqlDelete gen name criteria = SqlDelete name (map (sqlExpr gen) criteria)
defaultSqlDelete gen name criteria = SqlDelete (SqlTable name) (map (sqlExpr gen) criteria)


defaultSqlExpr :: SqlGenerator -> PrimExpr -> SqlExpr
Expand Down
15 changes: 9 additions & 6 deletions src/Opaleye/Internal/HaskellDB/Sql/Print.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ module Opaleye.Internal.HaskellDB.Sql.Print (
ppWhere,
ppGroupBy,
ppOrderBy,
ppTable,
ppAs,
commaV,
commaH
) where

import Opaleye.Internal.HaskellDB.Sql (SqlColumn(..), SqlDelete(..),
SqlExpr(..), SqlOrder(..), SqlInsert(..),
SqlUpdate(..))
SqlUpdate(..), SqlTable(..))
import qualified Opaleye.Internal.HaskellDB.Sql as Sql

import Data.List (intersperse)
Expand Down Expand Up @@ -64,22 +65,22 @@ ppAs alias expr | null alias = expr


ppUpdate :: SqlUpdate -> Doc
ppUpdate (SqlUpdate name assigns criteria)
= text "UPDATE" <+> text name
ppUpdate (SqlUpdate table assigns criteria)
= text "UPDATE" <+> ppTable table
$$ text "SET" <+> commaV ppAssign assigns
$$ ppWhere criteria
where
ppAssign (c,e) = ppColumn c <+> equals <+> ppSqlExpr e


ppDelete :: SqlDelete -> Doc
ppDelete (SqlDelete name criteria) =
text "DELETE FROM" <+> text name $$ ppWhere criteria
ppDelete (SqlDelete table criteria) =
text "DELETE FROM" <+> ppTable table $$ ppWhere criteria


ppInsert :: SqlInsert -> Doc
ppInsert (SqlInsert table names values)
= text "INSERT INTO" <+> text table
= text "INSERT INTO" <+> ppTable table
<+> parens (commaV ppColumn names)
$$ text "VALUES" <+> commaV (\v -> parens (commaV ppSqlExpr v))
(NEL.toList values)
Expand All @@ -92,6 +93,8 @@ ppInsert (SqlInsert table names values)
ppColumn :: SqlColumn -> Doc
ppColumn (SqlColumn s) = doubleQuotes (text s)

ppTable :: SqlTable -> Doc
ppTable (SqlTable s) = doubleQuotes (text s)

ppSqlExpr :: SqlExpr -> Doc
ppSqlExpr expr =
Expand Down
10 changes: 6 additions & 4 deletions src/Opaleye/Internal/Print.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import qualified Opaleye.Internal.HaskellDB.Sql.Print as HPrint
import Text.PrettyPrint.HughesPJ (Doc, ($$), (<+>), text, empty,
parens)

type TableAlias = String

ppSql :: Select -> Doc
ppSql (SelectFrom s) = ppSelectFrom s
ppSql (Table name) = text name
ppSql (Table table) = HPrint.ppTable table
ppSql (SelectJoin j) = ppSelectJoin j
ppSql (SelectValues v) = ppSelectValues v
ppSql (SelectBinary v) = ppSelectBinary v
Expand Down Expand Up @@ -71,13 +73,13 @@ ppTables :: [Select] -> Doc
ppTables [] = empty
ppTables ts = text "FROM" <+> HPrint.commaV ppTable (zipWith tableAlias [1..] ts)

tableAlias :: Int -> Select -> (HSql.SqlTable, Select)
tableAlias :: Int -> Select -> (TableAlias, Select)
tableAlias i select = ("T" ++ show i, select)

-- TODO: duplication with ppSql
ppTable :: (HSql.SqlTable, Select) -> Doc
ppTable :: (TableAlias, Select) -> Doc
ppTable (alias, select) = case select of
Table name -> HPrint.ppAs alias (text name)
Table table -> HPrint.ppAs alias (HPrint.ppTable table)
SelectFrom selectFrom -> HPrint.ppAs alias (parens (ppSelectFrom selectFrom))
SelectJoin slj -> HPrint.ppAs alias (parens (ppSelectJoin slj))
SelectValues slv -> HPrint.ppAs alias (parens (ppSelectValues slv))
Expand Down
2 changes: 1 addition & 1 deletion src/Opaleye/Internal/Sql.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ unit = SelectFrom newSelect { attrs = [(HSql.ConstSqlExpr "0", Nothing)] }
baseTable :: String -> [(Symbol, HPQ.PrimExpr)] -> Select
baseTable name columns = SelectFrom $
newSelect { attrs = map sqlBinding columns
, tables = [Table name] }
, tables = [Table (HSql.SqlTable name)] }

product :: NEL.NonEmpty Select -> [HPQ.PrimExpr] -> Select
product ss pes = SelectFrom $
Expand Down