makeCall

Parent Previous Next


语法


makeCall(F, args, ...)


参数


F是一个函数。


areg, ...F需要的参数。


详情


它使用指定参数调用一个函数并生成脚本。模板函数callmakeCall的区别是,makeCall不执行脚本。


例子


在下列例子中,我们创建了generateReport函数。它能够以指定格式显示表的某些列。



def generateReport(tbl, colNames, colFormat): sql(sqlColAlias(each(makeCall{format},sqlCol(colNames),colFormat),colNames), tbl).eval();


t = table(1..100 as id, (1..100 + 2018.01.01) as date, rand(100.0, 100) as price, rand(10000, 100) as qty, rand(`A`B`C`D`E`F`G, 100) as city);

t;

id date       price     qty  city

-- ---------- --------- ---- ----

1  2018.01.02 99.662328 6002 D

2  2018.01.03 24.965461 5836 B

3  2018.01.04 77.006418 5    G

4  2018.01.05 93.930603 5141 G

5  2018.01.06 70.994914 5778 C

6  2018.01.07 11.221769 9403 G

7  2018.01.08 59.387621 4632 G

8  2018.01.09 86.830752 4574 C

9  2018.01.10 53.928317 8397 G

10 2018.01.11 21.123212 6615 B

...


generateReport(t, ["id", "date","price","qty"], ["0", "MM/dd/yyyy", "0.00", "#,###"]);

id date       price qty

-- ---------- ----- -----

1  01/02/2018 16.32 9,972

2  01/03/2018 63.10 7,785

3  01/04/2018 30.43 3,629

4  01/05/2018 33.57 2,178

5  01/06/2018 61.12 9,406

6  01/07/2018 43.29 6,955

7  01/08/2018 54.97 9,914

8  01/09/2018 86.20 6,696

9  01/10/2018 90.82 6,141

10 01/11/2018 4.29  3,774

...



与之等效的SQL脚本是:



def generateReportSQL(tbl, colNames, colFormat): sql(sqlColAlias(each(makeCall{format},sqlCol(colNames),colFormat),colNames), tbl)

generateReportSQL(t, ["id", "date","price","qty"], ["0", "MM/dd/yyyy", "0.00", "#,###"]);

< select format(id, "0") as id,format(date, "MM/dd/yyyy") as date,format(price, "0.00") as price,format(qty, "#,###") as qty from t >