Added ? operator for nullable types
Getty Ritter
8 years ago
25 | 25 |
null int integer real text blob date
|
26 | 26 |
~~~
|
27 | 27 |
|
28 | |
or the name of a table declared in the source file.
|
| 28 |
or the name of a table declared in the source file. By default
|
| 29 |
all values are given the `NOT NULL` qualifier, but this can be
|
| 30 |
avoided by ending a type name with a `?` operator.
|
29 | 31 |
|
30 | 32 |
All tables get an implicit `id` column of the form
|
31 | 33 |
|
|
47 | 49 |
|
48 | 50 |
authors
|
49 | 51 |
name: text
|
50 | |
gender: blob
|
| 52 |
gender: blob?
|
51 | 53 |
~~~
|
52 | 54 |
|
53 | 55 |
Produces the following SQLite table declarations:
|
|
55 | 57 |
~~~.sql
|
56 | 58 |
CREATE TABLE books
|
57 | 59 |
( id INTEGER PRIMARY KEY ASC
|
58 | |
, title TEXT
|
59 | |
, author_name INTEGER
|
60 | |
, published DATE
|
| 60 |
, title TEXT NOT NULL
|
| 61 |
, author_name INTEGER NOT NULL
|
| 62 |
, published DATE NOT NULL
|
61 | 63 |
, FOREIGN KEY(author_name) REFERENCES authors(id)
|
62 | 64 |
);
|
63 | 65 |
CREATE TABLE authors
|
64 | 66 |
( id INTEGER PRIMARY KEY ASC
|
65 | |
, name TEXT
|
| 67 |
, name TEXT NOT NULL
|
66 | 68 |
, gender BLOB
|
67 | 69 |
);
|
68 | 70 |
~~~
|
13 | 13 |
data Field = Field
|
14 | 14 |
{ fName :: Text
|
15 | 15 |
, fType :: Text
|
| 16 |
, fNull :: Bool
|
16 | 17 |
} deriving (Eq, Show)
|
17 | 18 |
|
18 | 19 |
uncomment :: Text -> Text
|
|
28 | 29 |
| T.length (T.takeWhile isSpace l) > 0 =
|
29 | 30 |
let (n, t) = T.break (== ':') (T.strip l)
|
30 | 31 |
f = Field { fName = T.strip n
|
31 | |
, fType = T.strip (T.drop 1 t)
|
| 32 |
, fType = T.strip (T.drop 1 (T.filter (/= '?') t))
|
| 33 |
, fNull = T.any (== '?') t
|
32 | 34 |
}
|
33 | 35 |
in case ds of
|
34 | 36 |
(d:ds') -> go (d { dFields = f : dFields d } : ds') ls
|
|
75 | 77 |
mapM_ printField fs
|
76 | 78 |
mapM_ printForeign fs
|
77 | 79 |
T.putStrLn " );"
|
78 | |
where printField Field { fName = f, fType = t } = do
|
| 80 |
where printField Field { fName = f
|
| 81 |
, fType = t
|
| 82 |
, fNull = l
|
| 83 |
} = do
|
79 | 84 |
T.putStr " , "
|
80 | 85 |
T.putStr f
|
81 | 86 |
T.putStr " "
|
82 | |
T.putStrLn (typeName t)
|
| 87 |
T.putStr (typeName t)
|
| 88 |
if not l
|
| 89 |
then T.putStrLn " NOT NULL"
|
| 90 |
else T.putStrLn ""
|
83 | 91 |
printForeign Field { fName = f, fType = t }
|
84 | 92 |
| t `elem` builtins = return ()
|
85 | 93 |
| otherwise = do
|