[Ur] SQL Question
Burton Samograd
burton.samograd at gmail.com
Fri Oct 3 14:34:04 EDT 2014
Sorry about the missing semicolons. Here's a non-ad hock typed up example:
--------------------------------------------------------------------------------------------------------
table posts : { Id : int, Title : string, Body : string, Created : time }
PRIMARY KEY Id
table userIdToPostIds : { UserId : int, PostId : int }
PRIMARY KEY UserId
table users : { Id : int, Username : string }
PRIMARY KEY Id
fun blog () : transaction page =
posts <- queryX (SELECT Posts.Title, Posts.Body, Users.Username,
Posts.Created
FROM posts, users, userIdToPostIds
WHERE users.Id = userIdToPostIds.UserId
AND posts.Id = userIdToPostIds.PostId)
(fn row => post' row.Posts.Title row.Posts.Body row.Users.Username
row.Posts.Created);
posts2 <- queryX (SELECT Posts.Title, Posts.Body, Users.Username,
Posts.Created
FROM posts, users, userIdToPostIds
WHERE users.Id = userIdToPostIds.UserId
AND posts.Id = userIdToPostIds.PostId)
(fn row => post' row.Posts.Title row.Posts.Body row.Users.Username
row.Posts.Created);
return (Page.page' blogName
<xml>
{topbar' ()}
{posts' posts}
{posts' posts2}
</xml>)
and topbar' () : xbody =
<xml><div class="topbar">{[blogName]}</div></xml>
and posts' (posts) : xbody =
<xml><div class="postsList">
{posts}
</div></xml>
--------------------------------------------------------------------------------------------------------
and comments () : transaction page = error <xml>/comments not
implemented.</xml>
I've simply duplicated the query into a new variable 'posts2' and called
the post generation again using it (to avoid a possible unused variable
warning/error), hence my question about a single query per transaction.
When I compile I get:
/home/burton/blog/blog.ur:20:0: (to 64:3) Some constructor unification
variables are undetermined in declaration
(look for them as "<UNIF:...>")
Decl:
.....
[],
Users = ([Id = int]) ++ ([Username = string]) ++ [],
UserIdToPostIds =
([UserId = int]) ++ ([PostId = int]) ++ []]] [[]]
[bool]
Basis.sql_prim [bool] Basis.sql_bool Basis.True,
SelectFields =
Basis.sql_subset
[[Posts =
(([Created = time]) ++
([Body = string]) ++ [Title = string], ([Id = int])
++ []),
Users = ([Username = string], ([Id = int]) ++ []),
UserIdToPostIds =
([], ([UserId = int]) ++ ([PostId = int]) ++ [])]],
SelectExps = {}},
OrderBy =
Basis.sql_order_by_Nil
[([]) ++
[Posts =
([Id = int]) ++
([Created = time, Body = string, Title = string]) ++ [],
Users = ([Id = int]) ++ ([Username = string]) ++ [],
UserIdToPostIds = ([UserId = int]) ++ ([PostId = int]) ++
[]]]
[[]], Limit = Basis.sql_no_limit, Offset =
Basis.sql_no_offset})
(fn row :
$(([Posts =
$(([Title = string]) ++
([Body = string]) ++ ([Created = time]) ++ [])]) ++
(([Users = $(([Username = string]) ++ [])]) ++ []) ++ []) =>
post' row.#Posts.#Title row.#Posts.#Body row.#Users.#Username
row.#Posts.#Created))
(fn posts : xml ([Dyn = (), MakeForm = (), Body = ()]) ([]) ([]) =>
Basis.bind [transaction]
[xml ([Dyn = (), MakeForm = (), Body = ()]) ([]) ([])]
[xml ([Html = ()]) ([]) ([])] Basis.transaction_monad
(queryX
[[Posts =
(fn fields :: ({Type} * {Type}) => fields.1)
(([Created = time]) ++ ([Body = string]) ++ [Title =
string],
([Id = int]) ++ <UNIF:U314::{Type}>),
Users =
(fn fields :: ({Type} * {Type}) => fields.1)
([Username = string], ([Id = int]) ++ [])]] [[]]
[[Dyn = (), MakeForm = (), Body = ()]] [[]]
(Basis.sql_query [[]] [[]]
[[Posts =
([Id = int]) ++
([Created = time, Body = string, Title = string]) ++
<UNIF:U314::{Type}>,
Users = ([Id = int]) ++ ([Username = string]) ++ [],
UserIdToPostIds = ([UserId = int]) ++ ([PostId = int]) ++
[]]]
[[Posts =
(fn fields :: ({Type} * {Type}) => fields.1)
(([Created = time]) ++ ([Body = string]) ++ [Title =
string],
([Id = int]) ++ <UNIF:U314::{Type}>),
Users =
(fn fields :: ({Type} * {Type}) => fields.1)
([Username = string], ([Id = int]) ++ [])]] [[]]
{Rows =
Basis.sql_query1 [[]] [[]]
[[Posts =
([Id = int]) ++
([Created = time, Body = string, Title = string]) ++
<UNIF:U314::{Type}>,
Users = ([Id = int]) ++ ([Username = string]) ++ [],
UserIdToPostIds = ([UserId = int]) ++ ([PostId = int])
++ []]]
[[Posts =
([Id = int]) ++
([Created = time, Body = string, Title = string]) ++
<UNIF:U314::{Type}>,
Users = ([Id = int]) ++ ([Username = string]) ++ [],
UserIdToPostIds = ([UserId = int]) ++ ([PostId = int])
++ []]]
[[Posts =
(fn fields :: ({Type} * {Type}) => fields.1)
(([Created = time]) ++
([Body = string]) ++ [Title = string],
([Id = int]) ++ <UNIF:U314::{Type}>),
Users =
(fn fields :: ({Ty
Without the duplicated query I can compile without any warnings or errors.
Burton
On Fri, Oct 3, 2014 at 12:23 PM, Adam Chlipala <adamc at csail.mit.edu> wrote:
> On 10/03/2014 02:21 PM, Burton Samograd wrote:
>
>> Are you limited to one query per transaction?
>>
>
> No. Your code example below seems to be missing semicolons at line ends.
>
> This type of code causes compiler errors where a single query doesn't:
>>
>> fun page () : transaction xbody =
>>
>> data <- (SELECT * FROM table ...) (fn row => ...)
>> data2 <- (SELECT * FROM table2 ...) (fn row => ...)
>>
>> (* Generate part of the page with data *)
>> {header data}
>>
>> (* Generate another section with data2 *)
>> {footer data2}
>>
>
> _______________________________________________
> Ur mailing list
> Ur at impredicative.com
> http://www.impredicative.com/cgi-bin/mailman/listinfo/ur
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.impredicative.com/pipermail/ur/attachments/20141003/c2f7ff4e/attachment-0001.html>
More information about the Ur
mailing list