comparison src/sql.sml @ 2294:f8903af753ff

Support nested queries but disable UrFlow for now.
author Ziv Scully <ziv@mit.edu>
date Thu, 19 Nov 2015 01:59:00 -0500
parents f81f1930c5d6
children e6c5bb62fef8
comparison
equal deleted inserted replaced
2293:8be54d7bd06e 2294:f8903af753ff
380 380
381 val select = log "select" 381 val select = log "select"
382 (wrap (follow (const "SELECT ") (list sitem)) 382 (wrap (follow (const "SELECT ") (list sitem))
383 (fn ((), ls) => ls)) 383 (fn ((), ls) => ls))
384 384
385 val fitem = wrap (follow uw_ident 385 datatype jtype = Inner | Left | Right | Full
386 (follow (const " AS ") 386
387 t_ident)) 387 val jtype = wrap (ws (follow (opt (altL [wrap (const "LEFT") (fn () => Left),
388 (fn (t, ((), f)) => (t, f)) 388 wrap (const "RIGHT") (fn () => Right),
389 389 wrap (const "FULL") (fn () => Full)]))
390 val from = log "from" 390 (const " JOIN ")))
391 (wrap (follow (const "FROM ") (list fitem)) 391 (fn (SOME jt, ()) => jt | (NONE, ()) => Inner)
392 (fn ((), ls) => ls)) 392
393 datatype fitem =
394 Table of string * string (* table AS name *)
395 | Join of jtype * fitem * fitem * sqexp
396 | Nested of query * string (* query AS name *)
397
398 and query =
399 Query1 of {Select : sitem list, From : fitem list, Where : sqexp option}
400 | Union of query * query
393 401
394 val wher = wrap (follow (ws (const "WHERE ")) sqexp) 402 val wher = wrap (follow (ws (const "WHERE ")) sqexp)
395 (fn ((), ls) => ls) 403 (fn ((), ls) => ls)
396
397 type query1 = {Select : sitem list,
398 From : (string * string) list,
399 Where : sqexp option}
400
401 val query1 = log "query1"
402 (wrap (follow (follow select from) (opt wher))
403 (fn ((fs, ts), wher) => {Select = fs, From = ts, Where = wher}))
404
405 datatype query =
406 Query1 of query1
407 | Union of query * query
408 404
409 val orderby = log "orderby" 405 val orderby = log "orderby"
410 (wrap (follow (ws (const "ORDER BY ")) 406 (wrap (follow (ws (const "ORDER BY "))
411 (follow (list sqexp) 407 (follow (list sqexp)
412 (opt (ws (const "DESC"))))) 408 (opt (ws (const "DESC")))))
413 ignore) 409 ignore)
414 410
415 fun query chs = log "query" 411 fun fitem chs = altL [wrap (follow uw_ident
416 (wrap 412 (follow (const " AS ")
417 (follow 413 t_ident))
418 (alt (wrap (follow (const "((") 414 (fn (t, ((), f)) => Table (t, f)),
419 (follow query 415 wrap (follow (const "(")
420 (follow (const ") UNION (") 416 (follow fitem
421 (follow query (const "))"))))) 417 (follow jtype
422 (fn ((), (q1, ((), (q2, ())))) => Union (q1, q2))) 418 (follow fitem
423 (wrap query1 Query1)) 419 (follow (const " ON ")
424 (opt orderby)) 420 (follow sqexp
425 #1) 421 (const ")")))))))
426 chs 422 (fn ((), (fi1, (jt, (fi2, ((), (se, ())))))) =>
423 Join (jt, fi1, fi2, se)),
424 wrap (follow (const "(")
425 (follow query
426 (follow (const ") AS ") t_ident)))
427 (fn ((), (q, ((), f))) => Nested (q, f))]
428 chs
429
430 and query1 chs = log "query1"
431 (wrap (follow (follow select from) (opt wher))
432 (fn ((fs, ts), wher) => {Select = fs, From = ts, Where = wher}))
433 chs
434
435 and from chs = log "from"
436 (wrap (follow (const "FROM ") (list fitem))
437 (fn ((), ls) => ls))
438 chs
439
440 and query chs = log "query"
441 (wrap (follow
442 (alt (wrap (follow (const "((")
443 (follow query
444 (follow (const ") UNION (")
445 (follow query (const "))")))))
446 (fn ((), (q1, ((), (q2, ())))) => Union (q1, q2)))
447 (wrap query1 Query1))
448 (opt orderby))
449 #1)
450 chs
427 451
428 datatype dml = 452 datatype dml =
429 Insert of string * (string * sqexp) list 453 Insert of string * (string * sqexp) list
430 | Delete of string * sqexp 454 | Delete of string * sqexp
431 | Update of string * (string * sqexp) list * sqexp 455 | Update of string * (string * sqexp) list * sqexp