-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpgtokdb.q
94 lines (87 loc) · 3.12 KB
/
pgtokdb.q
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
\d .pgtokdb
//
// Mapping between kdb+ types and Postgres types
//
KPTM:(!/) flip 0N 2#(
"b"; "boolean";
"h"; "smallint";
"i"; "integer";
"j"; "bigint";
"e"; "float4";
"f"; "float8";
"c"; "varchar";
"C"; "varchar";
"g"; "uuid";
"X"; "bytea";
"d"; "date";
"p"; "timestamp";
"s"; "varchar"
);
//
// Some Postgres DDL command templates to manage types and functions
//
DTT:"drop type if exists %f_t;";
DFT:"drop function if exists %f;";
CTT:"create type %f_t as (%c);";
CFT:"create function %f(%a) returns setof %f_t as 'pgtokdb','getset' language c;";
//
// @desc Returns a table with Postgres scripts (DDL)
//
// @param fnname {string} - name of Postgres function to be created
// @param argtypes {string} - Kdb+ datatypes of arguments
// @param tblmeta {table} - result of meta call on kdb+ result table
//
// @returns a table that contains one Postgres command (DDL) per row.
//
// @example
//
// q) qfn:{[n] ([] id:til n;val:n?9999.99;ts:2019.10.01D0+1D*til n)}
// q) .pg.genddl["call_qfn";"i";meta qfn[1]]
// script
// -----------------------------------------------------------------------------------------------------------------------
// "drop function if exists call_qfn;"
// "drop type if exists call_qfn_t;"
// "create type call_qfn_t as (id bigint, val float8, ts timestamp);"
// "create function call_qfn(varchar, integer) returns setof call_qfn_t as 'pgtokdb','getset' language c;""
//
// The rows of this script can be written to a text file and be executed by psql
//
genddl:{[fnname;argtypes;tblmeta]
argtypes:"C",argtypes; / Always prepend the C (varchar) for the kdb+ function
a:2_raze ", ",/: KPTM[count[argtypes]#argtypes];
c:2_raze ", ",/:string[key[tblmeta]`c],'" ",/:KPTM[value[tblmeta]`t];
script:();
script,:enlist ssr[DFT;"%f";fnname];
script,:enlist ssr[DTT;"%f";fnname];
script,:enlist ssr/[CTT;2#v:"%",/:"fca";2#r:(fnname;c;a)];
script,:enlist ssr/[CFT;v;r];
([] script)
}
//
// @desc This is equivalent to <gendll> except it is callable by Postgres
//
// @param fnname {string} - name of Postgres function to be created
// @param argtypes {string} - Kdb+ datatypes of arguments
// @param tblmetaexpr {string} - Executable string that returns the meta
//
// @example
//
// pg# select * from pgtokdb.genddl('.pg.genddle', 'qfn','i','meta qfn[1]');
// script
// ------------------------------------------------------------------------------------------------------------------------
// drop function if exists call_qfn;
// drop type if exists call_qfn_t;
// create type call_qfn_t as (id bigint, val float8, ts timestamp);
// create function call_qfn(varchar, integer) returns setof call_qfn_t as 'pgtokdb','getset' language c;
//
// or generate a script file that can be execute by psql
// pg# copy (select * from genddl('.pg.genddle', 'qfn', 'i', 'meta qfn[1]')) to '/tmp/f.sql';
//
genddle:{[fnname;argtypes;tblmetaexpr]
genddl[fnname;argtypes;value tblmetaexpr]
}
//
// @desc Return single row table returning system information
//
getstatus:{flip `os`version`release`timenow!1#'(.z.o;.z.K;.z.k;.z.p)}
\d .