[Ur] Correct way to use "computed table names" `{{c}}` & "computed field names" `{c}` in functions?

Stefan Scott stefanscottalexx at gmail.com
Thu Aug 20 19:54:44 EDT 2015


Page 48 of the manual mentions an interesting syntax for:

- "computed table names" - eg: `{{c}}`

- "computed column names" - eg: `{c}`

...both of kind `Name`.

Also, pages 13 and 15 mention syntax for:

- field names - eg: `#X`

- record field projection - eg: `e.c`

---

I'm attempting create a simple demo using this syntax.

The demo should define a function (eg `table_to_xml aTable aField`) which
accepts a table name and a field name.

Then this function is called using different table names and field names,
to run some SQL queries and generate some XML.

I haven't found any concrete examples of this sort of thing in the manual
or online, so the following is just a wild guess:

  (* INCORRECT/tableToXml.ur *)

  table t1 : { F1 : string }
  table t2 : { F2 : string }

  fun table_to_xml
    aTable       (** OK? **)
    aField       (** OK? **)
  =
    queryX1
      ( SELECT {aField}     (** OK? **)
        FROM  {{aTable}} )  (** OK? **)
      ( fn r =>
          <xml>
            {[r.{aField}]}    (** Q (1) **)
            <br/>
          </xml> )

  fun main () =
    t1x <- table_to_xml t1 #F1  (** Q (2) **)
    ;
    t2x <- table_to_xml t2 #F2  (** Q (2) **)
    ;
    return
    <xml><body>
      Table 1:<br/>
      {t1x}
      <br/><br/>
      Table 2:<br/>
      {t2x}
    </body></xml>

This is of course giving compile errors.

---

My two main questions (flagged with comments in the code above) are:

Question (1):

  ( fn r => <xml> {[r.{aField}]} <br/> </xml> )

The part `r.{aField}` in the body of the anonymous function above appears
to be incorrect.

What is the correct way to use `aField` (the actual parameter of the
function `table_to_xml`, which calls `queryX1`) in the body of this
anonymous function definition?

---

Question (2):

  t1x <- table_to_xml t1 #F1 ;

The syntax for the field name `#F1` in the function call above appears to
be incorrect (and the syntax for the table name `t1` might also be
incorrect!).

What is the correct way to pass the table name `t1` and the field name `F1`
to this function call?

---

By the way, as a test, I initially did an even simpler demo using
*constant* table names and field names, and that worked fine - eg:

  (* CORRECT/tableToXml.ur *)

  table t1 : { F1 : string }
  table t2 : { F2 : string }

  fun t1_to_xml () =
    queryX1
      ( SELECT t1.F1 FROM t1 )
      ( fn r => <xml>{[r.F1]}<br/></xml> )

  fun t2_to_xml () =
    queryX1
      ( SELECT t2.F2 FROM t2 )
      ( fn r => <xml>{[r.F2]}<br/></xml> )

  fun main () =
    t1x <- t1_to_xml ();
    t2x <- t2_to_xml ();
    return
    <xml><body>
      Table 1:<br/>
      {t1x}
      <br/><br/>
      Table 2:<br/>
      {t2x}
    </body></xml>

---

So I'm looking for examples of using "computed table names" and "computed
field names" - specifically the syntax to use in the following situations:

(1) a field name argument `aField` captured in a closure and used in a
field projection `{[r.{aField}]}` inside an anonymous function passed to
another function `queryX1` - eg:

  queryX1
    ( SELECT ... )
    ( fn r => <xml> {[r.{aField}]} <br/> </xml> )
                      ^^^^^^^^^^
(2) table name and field name arguments `t1` and `#F1` passed to a function
invocation - eg:

  t1x <- table_to_xml t1 #F1 ;
                      ^^ ^^^
Thanks for any help!

###
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.impredicative.com/pipermail/ur/attachments/20150820/f523e257/attachment.html>


More information about the Ur mailing list