s-sql-tables.lua /size: 4619 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['s-languages-counters'] = {
2    version   = 1.001,
3    comment   = "companion to s-languages-counters.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9require("util-tpl")
10require("util-sql")
11require("util-sql-tracers")
12
13moduledata            = moduledata            or { }
14moduledata.sql        = moduledata.sql        or { }
15moduledata.sql.tables = moduledata.sql.tables or { }
16
17local context = context
18
19function moduledata.sql.showfields(specification) -- not that sql specific
20    local data = specification.data
21    if data and #data > 0 then
22        local keys = specification.order or table.sortedkeys(data[1])
23        local align = specification.align
24        local template = "|"
25        if type(align) == "table" then
26            for i=1,#keys do
27                template = template .. (align[keys[i]] or "c") .. "|"
28            end
29        else
30            template = template .. string.rep((align or "c").. "|",#keys)
31        end
32        context.starttabulate { template }
33        context.NC()
34        for i=1,#keys do
35            context(keys[i])
36            context.NC()
37        end
38        context.NR()
39        context.HL()
40        for i=specification.first or 1,specification.last or #data do
41            local d = data[i]
42            context.NC()
43            for i=1,#keys do
44                context(d[keys[i]])
45                context.NC()
46            end
47            context.NR()
48        end
49        context.stoptabulate()
50    end
51end
52
53function moduledata.sql.validpresets(presets)
54    local okay = true
55    if presets.database == "" then
56        context("No database given.")
57        context.blank()
58        okay = false
59    end
60    if presets.password == "" then
61        context("No password given")
62        context.blank()
63        okay = false
64    end
65    return okay
66end
67
68function moduledata.sql.tables.showdefined(presets) -- key=value string | { presets = "name" } | { presets }
69
70    if type(presets) == "string" then
71        local specification = interfaces.checkedspecification(presets)
72        if specification.presets then
73            presets = table.load(specification.presets) or { }
74        end
75    end
76
77    if type(presets.presets) == "string" then
78        presets = table.load(presets.presets) or { }
79    end
80
81    if not moduledata.sql.validpresets(presets) then
82        return
83    end
84
85    local sql_tables = utilities.sql.tracers.gettables(presets)
86
87    context.starttitle { title = presets.database }
88
89        for name, fields in table.sortedhash(sql_tables) do
90
91            context.startsubject { title = name }
92
93                context.starttabulate { format = "|l|l|l|l|l|p|" }
94                context.FL()
95                context.NC() context.bold("field")
96                context.NC() context.bold("type")
97                context.NC() context.bold("default")
98                context.NC() context.bold("null")
99                context.NC() context.bold("key")
100                context.NC() context.bold("extra")
101                context.NC() context.NR()
102                context.TL()
103                for i=1,#fields do
104                    local field = fields[i]
105                    context.NC() context(field.field)
106                    context.NC() context(field.type)
107                    context.NC() context(field.default)
108                    context.NC() context(field.null)
109                    context.NC() context(field.key)
110                    context.NC() context(field.extra)
111                    context.NC() context.NR()
112                end
113                context.LL()
114                context.stoptabulate()
115
116            context.stopsubject()
117        end
118
119    context.stoptitle()
120
121end
122
123function moduledata.sql.tables.showconstants(list)
124
125    context.starttitle { title = "Constants" }
126
127        for name, fields in table.sortedhash(list) do
128
129            if type(fields) == "table" and #fields > 0 then
130
131                context.startsubject { title = name }
132
133                    context.starttabulate { format = "|l|l|" }
134                    for i=0,#fields do
135                        local field = fields[i]
136                        if field then
137                            context.NC() context(i)
138                            context.NC() context(field)
139                            context.NC() context.NR()
140                        end
141                    end
142                    context.stoptabulate()
143
144                context.stopsubject()
145
146            end
147
148        end
149
150    context.stoptitle()
151
152end
153