on dev help sign in
docs
commit922f826b75da06ac7ac92555337b7facf212dcab authorEitan Chatav <eitan.chatav@gmail.com> dateThu 21 Jun 2018, 20:41 (8 years ago) parent51d36f2
1 file changed+25−24
squeal-postgresql/src/Squeal/PostgreSQL.hs+25−24
squeal-postgresql/src/Squeal/PostgreSQL.hs+25−24
@@ -36,28 +36,32 @@ type Schema =
3636 '[ "users" ::: 'Table (
3737 '[ "pk_users" ::: 'PrimaryKey '["id"] ] :=>
3838 '[ "id" ::: 'Def :=> 'NotNull 'PGint4
39− , "name" ::: 'NoDef :=> 'NotNull 'PGtext
40− ])
39+ , "name" ::: 'NoDef :=> 'NotNull 'PGtext
40+ ])
4141 , "emails" ::: 'Table (
4242 '[ "pk_emails" ::: 'PrimaryKey '["id"]
43− , "fk_user_id" ::: 'ForeignKey '["user_id"] "users" '["id"]
44− ] :=>
43+ , "fk_user_id" ::: 'ForeignKey '["user_id"] "users" '["id"]
44+ ] :=>
4545 '[ "id" ::: 'Def :=> 'NotNull 'PGint4
46− , "user_id" ::: 'NoDef :=> 'NotNull 'PGint4
47− , "email" ::: 'NoDef :=> 'Null 'PGtext
48− ])
46+ , "user_id" ::: 'NoDef :=> 'NotNull 'PGint4
47+ , "email" ::: 'NoDef :=> 'Null 'PGtext
48+ ])
4949 ]
5050:}
5151
52−Notice the use of type operators. `:::` is used
53−to pair an alias `Symbol` with either a `TableType` or a `ColumnType`.
54−`:=>` is used to pair a `TableConstraint`s with a `ColumnsType`,
52+Notice the use of type operators.
53+
54+`:::` is used to pair an alias `GHC.TypeLits.Symbol` with a `SchemumType`,
55+a `TableConstraint` or a `ColumnType`. It is intended to connote Haskell's @::@
56+operator.
57+
58+`:=>` is used to pair `TableConstraints` with a `ColumnsType`,
5559yielding a `TableType`, or to pair a `ColumnConstraint` with a `NullityType`,
56−yielding a `ColumnType`.
60+yielding a `ColumnType`. It is intended to connote Haskell's @=>@ operator
5761
5862Next, we'll write `Definition`s to set up and tear down the schema. In
59−Squeal, a `Definition` is a `createTable`, `alterTable` or `dropTable`
60−command and has two type parameters, corresponding to the schema
63+Squeal, a `Definition` like `createTable`, `alterTable` or `dropTable`
64+has two type parameters, corresponding to the schema
6165before being run and the schema after. We can compose definitions using `>>>`.
6266Here and in the rest of our commands we make use of overloaded
6367labels to refer to named tables and columns in our schema.
@@ -101,12 +105,11 @@ DROP TABLE "emails";
101105DROP TABLE "users";
102106
103107Next, we'll write `Manipulation`s to insert data into our two tables.
104−A `Manipulation` is an `insertRow` (or other inserts), `update`
105−or `deleteFrom` command and
108+A `Manipulation` like `insertRow`, `update` or `deleteFrom`
106109has three type parameters, the schema it refers to, a list of parameters
107110it can take as input, and a list of columns it produces as output. When
108111we insert into the users table, we will need a parameter for the @name@
109−field but not for the @id@ field. Since it's optional, we can use a default
112+field but not for the @id@ field. Since it's serial, we can use a default
110113value. However, since the emails table refers to the users table, we will
111114need to retrieve the user id that the insert generates and insert it into
112115the emails table. Take a careful look at the type and definition of both
@@ -144,7 +147,7 @@ need to use an inner join to get the right result. A `Query` is like a
144147let
145148 getUsers :: Query Schema '[]
146149 '[ "userName" ::: 'NotNull 'PGtext
147− , "userEmail" ::: 'Null 'PGtext ]
150+ , "userEmail" ::: 'Null 'PGtext ]
148151 getUsers = select
149152 (#u ! #name `As` #userName :* #e ! #email `As` #userEmail :* Nil)
150153 ( from (table (#users `As` #u)
@@ -195,13 +198,11 @@ let
195198 usersResult <- runQuery getUsers
196199 usersRows <- getRows usersResult
197200 liftBase $ print (usersRows :: [User])
198−:}
199−
200−>>> :{
201−void . withConnection "host=localhost port=5432 dbname=exampledb" $
202− define setup
203− & pqThen session
204− & pqThen (define teardown)
201+in
202+ void . withConnection "host=localhost port=5432 dbname=exampledb" $
203+ define setup
204+ & pqThen session
205+ & pqThen (define teardown)
205206:}
206207[User {userName = "Alice", userEmail = Just "alice@gmail.com"},User {userName = "Bob", userEmail = Nothing},User {userName = "Carole", userEmail = Just "carole@hotmail.com"}]
207208-}