summaryrefslogtreecommitdiff
path: root/tex/context/modules/mkiv/s-sql-tables.lua
blob: 43410354134a0945113939d4df77e214f03dbbb5 (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
if not modules then modules = { } end modules ['s-languages-counters'] = {
    version   = 1.001,
    comment   = "companion to s-languages-counters.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

require("util-tpl")
require("util-sql")
require("util-sql-tracers")

moduledata            = moduledata            or { }
moduledata.sql        = moduledata.sql        or { }
moduledata.sql.tables = moduledata.sql.tables or { }

local context = context

function moduledata.sql.showfields(specification) -- not that sql specific
    local data = specification.data
    if data and #data > 0 then
        local keys = specification.order or table.sortedkeys(data[1])
        local align = specification.align
        local template = "|"
        if type(align) == "table" then
            for i=1,#keys do
                template = template .. (align[keys[i]] or "c") .. "|"
            end
        else
            template = template .. string.rep((align or "c").. "|",#keys)
        end
        context.starttabulate { template }
        context.NC()
        for i=1,#keys do
            context(keys[i])
            context.NC()
        end
        context.NR()
        context.HL()
        for i=specification.first or 1,specification.last or #data do
            local d = data[i]
            context.NC()
            for i=1,#keys do
                context(d[keys[i]])
                context.NC()
            end
            context.NR()
        end
        context.stoptabulate()
    end
end

function moduledata.sql.validpresets(presets)
    local okay = true
    if presets.database == "" then
        context("No database given.")
        context.blank()
        okay = false
    end
    if presets.password == "" then
        context("No password given")
        context.blank()
        okay = false
    end
    return okay
end

function moduledata.sql.tables.showdefined(presets) -- key=value string | { presets = "name" } | { presets }

    if type(presets) == "string" then
        local specification = interfaces.checkedspecification(presets)
        if specification.presets then
            presets = table.load(specification.presets) or { }
        end
    end

    if type(presets.presets) == "string" then
        presets = table.load(presets.presets) or { }
    end

    if not moduledata.sql.validpresets(presets) then
        return
    end

    local sql_tables = utilities.sql.tracers.gettables(presets)

    context.starttitle { title = presets.database }

        for name, fields in table.sortedhash(sql_tables) do

            context.startsubject { title = name }

                context.starttabulate { format = "|l|l|l|l|l|p|" }
                context.FL()
                context.NC() context.bold("field")
                context.NC() context.bold("type")
                context.NC() context.bold("default")
                context.NC() context.bold("null")
                context.NC() context.bold("key")
                context.NC() context.bold("extra")
                context.NC() context.NR()
                context.TL()
                for i=1,#fields do
                    local field = fields[i]
                    context.NC() context(field.field)
                    context.NC() context(field.type)
                    context.NC() context(field.default)
                    context.NC() context(field.null)
                    context.NC() context(field.key)
                    context.NC() context(field.extra)
                    context.NC() context.NR()
                end
                context.LL()
                context.stoptabulate()

            context.stopsubject()
        end

    context.stoptitle()

end

function moduledata.sql.tables.showconstants(list)

    context.starttitle { title = "Constants" }

        for name, fields in table.sortedhash(list) do

            if type(fields) == "table" and #fields > 0 then

                context.startsubject { title = name }

                    context.starttabulate { format = "|l|l|" }
                    for i=0,#fields do
                        local field = fields[i]
                        if field then
                            context.NC() context(i)
                            context.NC() context(field)
                            context.NC() context.NR()
                        end
                    end
                    context.stoptabulate()

                context.stopsubject()

            end

        end

    context.stoptitle()

end