Skip to content

Commit 6f1b9cd

Browse files
committed
Implement sqlite "without rowid" clauses
1 parent 606ae6d commit 6f1b9cd

File tree

7 files changed

+69
-21
lines changed

7 files changed

+69
-21
lines changed

Language/SQL/SimpleSQL/Dialect.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ data Dialect = Dialect
8080
,diAtIdentifier :: Bool
8181
-- | allow identifiers with a leading \# \#example
8282
,diHashIdentifier :: Bool
83-
-- | allow positional identifiers like this: $1
83+
-- | allow positional identifiers like this: $1
8484
,diPositionalArg :: Bool
8585
-- | allow postgres style dollar strings
8686
,diDollarString :: Bool
@@ -96,6 +96,8 @@ data Dialect = Dialect
9696
,diAutoincrement :: Bool
9797
-- | allow omitting the comma between constraint clauses
9898
,diNonCommaSeparatedConstraints :: Bool
99+
-- | allow marking tables as "without rowid"
100+
,diWithoutRowidTables :: Bool
99101
}
100102
deriving (Eq,Show,Read,Data,Typeable)
101103

@@ -117,9 +119,10 @@ ansi2011 = Dialect {diKeywords = ansi2011ReservedKeywords
117119
,diEString = False
118120
,diPostgresSymbols = False
119121
,diSqlServerSymbols = False
120-
,diConvertFunction = False
122+
,diConvertFunction = False
121123
,diAutoincrement = False
122124
,diNonCommaSeparatedConstraints = False
125+
,diWithoutRowidTables = False
123126
}
124127

125128
-- | mysql dialect

Language/SQL/SimpleSQL/Parse.hs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ import Text.Megaparsec
198198
,hidden
199199
,failure
200200
,ErrorItem(..)
201-
201+
202202
,(<|>)
203203
,token
204204
,choice
@@ -233,11 +233,11 @@ import Control.Applicative ((<**>))
233233
import Data.Char (isDigit)
234234
import Data.List (sort,groupBy)
235235
import Data.Function (on)
236-
import Data.Maybe (catMaybes, isJust, mapMaybe)
236+
import Data.Maybe (catMaybes, isJust, mapMaybe, fromMaybe)
237237
import Data.Text (Text)
238238
import qualified Data.Text as T
239239

240-
import Language.SQL.SimpleSQL.Syntax
240+
import Language.SQL.SimpleSQL.Syntax
241241
import Language.SQL.SimpleSQL.Dialect
242242
import qualified Language.SQL.SimpleSQL.Lex as L
243243
--import Text.Megaparsec.Debug (dbg)
@@ -332,7 +332,7 @@ wrapParse :: Parser a
332332
-> Either ParseError a
333333
wrapParse parser d f p src = do
334334
lx <- either (Left . LexError) Right $ L.lexSQLWithPositions d True f p src
335-
either (Left . ParseError) Right $
335+
either (Left . ParseError) Right $
336336
runReader (runParserT (parser <* (hidden eof)) (T.unpack f)
337337
$ L.SQLStream (T.unpack src) $ filter notSpace lx) d
338338
where
@@ -584,7 +584,7 @@ typeName' hideArg =
584584
reservedTypeNames = do
585585
stn <- askDialect diSpecialTypeNames
586586
(:[]) . Name Nothing . T.unwords <$> makeKeywordTree stn
587-
587+
588588

589589
{-
590590
= Scalar expressions
@@ -1589,7 +1589,7 @@ queryExpr :: Parser QueryExpr
15891589
queryExpr = label "query expr" $ E.makeExprParser qeterm qeOpTable
15901590
where
15911591
qeterm = label "query expr" (with <|> select <|> table <|> values)
1592-
1592+
15931593
select = keyword_ "select" >>
15941594
mkSelect
15951595
<$> hoption SQDefault duplicates
@@ -1615,7 +1615,7 @@ queryExpr = label "query expr" $ E.makeExprParser qeterm qeOpTable
16151615
cq o d c q0 q1 = QueryExprSetOp q0 o d c q1
16161616
corr = hoption Respectively (Corresponding <$ keyword_ "corresponding")
16171617

1618-
1618+
16191619
{-
16201620
local data type to help with parsing the bit after the select list,
16211621
called 'table expression' in the ansi sql grammar. Maybe this should
@@ -1707,21 +1707,25 @@ createSchema = keyword_ "schema" >>
17071707
CreateSchema <$> names "schema name"
17081708

17091709
createTable :: Parser Statement
1710-
createTable = do
1710+
createTable = do
17111711
d <- askDialect id
1712-
let
1713-
parseColumnDef = TableColumnDef <$> columnDef
1712+
let
1713+
parseColumnDef = TableColumnDef <$> columnDef
17141714
parseConstraintDef = uncurry TableConstraintDef <$> tableConstraintDef
17151715
separator = if diNonCommaSeparatedConstraints d
17161716
then optional comma
17171717
else Just <$> comma
17181718
constraints = sepBy parseConstraintDef (hidden separator)
17191719
entries = ((:) <$> parseColumnDef <*> ((comma >> entries) <|> pure [])) <|> constraints
1720+
withoutRowid = if diWithoutRowidTables d
1721+
then fromMaybe False <$> optional (keywords_ ["without", "rowid"] >> pure True)
1722+
else pure False
17201723

17211724
keyword_ "table" >>
17221725
CreateTable
17231726
<$> names "table name"
17241727
<*> parens entries
1728+
<*> withoutRowid
17251729

17261730
createIndex :: Parser Statement
17271731
createIndex =
@@ -1804,9 +1808,9 @@ colConstraintDef =
18041808
notNull = ColNotNullConstraint <$ keywords_ ["not", "null"]
18051809
unique = ColUniqueConstraint <$ keyword_ "unique"
18061810
primaryKey = do
1807-
keywords_ ["primary", "key"]
1811+
keywords_ ["primary", "key"]
18081812
d <- askDialect id
1809-
autoincrement <- if diAutoincrement d
1813+
autoincrement <- if diAutoincrement d
18101814
then optional (keyword_ "autoincrement")
18111815
else pure Nothing
18121816
pure $ ColPrimaryKeyConstraint $ isJust autoincrement
@@ -1998,7 +2002,7 @@ delete = keywords_ ["delete","from"] >>
19982002
<*> optional (hoptional (keyword_ "as") *> name "alias")
19992003
<*> optional (keyword_ "where" *> scalarExpr)
20002004

2001-
truncateSt :: Parser Statement
2005+
truncateSt :: Parser Statement
20022006
truncateSt = keywords_ ["truncate", "table"] >>
20032007
Truncate
20042008
<$> names "table name"
@@ -2011,7 +2015,7 @@ insert = keywords_ ["insert", "into"] >>
20112015
Insert
20122016
<$> names "table name"
20132017
<*> (hoptional (parens $ commaSep1 $ name "column name"))
2014-
<*>
2018+
<*>
20152019
-- slight hack
20162020
(DefaultInsertValues <$ label "values" (keywords_ ["default", "values"])
20172021
<|> InsertQuery <$> queryExpr)

Language/SQL/SimpleSQL/Pretty.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,9 +491,10 @@ statement :: Dialect -> Statement -> Doc a
491491
statement _ (CreateSchema nm) =
492492
pretty "create" <+> pretty "schema" <+> names nm
493493

494-
statement d (CreateTable nm cds) =
494+
statement d (CreateTable nm cds withoutRowid) =
495495
pretty "create" <+> pretty "table" <+> names nm
496496
<+> parens (commaSep $ map cd cds)
497+
<+> (if withoutRowid then texts [ "without", "rowid" ] else mempty)
497498
where
498499
cd (TableConstraintDef n con) =
499500
maybe mempty (\s -> pretty "constraint" <+> names s) n
@@ -723,7 +724,7 @@ columnDef d (ColumnDef n t mdef cons) =
723724
pcon ColNotNullConstraint = texts ["not","null"]
724725
pcon ColNullableConstraint = texts ["null"]
725726
pcon ColUniqueConstraint = pretty "unique"
726-
pcon (ColPrimaryKeyConstraint autoincrement) =
727+
pcon (ColPrimaryKeyConstraint autoincrement) =
727728
texts $ ["primary","key"] <> ["autoincrement"|autoincrement]
728729
--pcon ColPrimaryKeyConstraint = texts ["primary","key"]
729730
pcon (ColCheckConstraint v) = pretty "check" <+> parens (scalarExpr d v)

Language/SQL/SimpleSQL/Syntax.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ data Statement =
443443
-- ddl
444444
CreateSchema [Name]
445445
| DropSchema [Name] DropBehaviour
446-
| CreateTable [Name] [TableElement]
446+
| CreateTable [Name] [TableElement] Bool
447447
| AlterTable [Name] AlterTableAction
448448
| DropTable [Name] DropBehaviour
449449
| CreateIndex Bool [Name] [Name] [Name]

expected-parse-errors/golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5668,7 +5668,7 @@ statement
56685668
ansi2011
56695669
create table t (
56705670
)
5671-
CreateTable [ Name Nothing "t" ] []
5671+
CreateTable [ Name Nothing "t" ] [] False
56725672

56735673
statement
56745674
ansi2011

tests/Language/SQL/SimpleSQL/Oracle.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ oracleLobUnits = Group "oracleLobUnits"
2727
[TableColumnDef $ ColumnDef (Name Nothing "a")
2828
(PrecLengthTypeName [Name Nothing "varchar2"] 55 Nothing (Just PrecOctets))
2929
Nothing []]
30+
False
3031
]
31-
32+

0 commit comments

Comments
 (0)