-
Notifications
You must be signed in to change notification settings - Fork 4
/
table_query.go
65 lines (60 loc) · 1.41 KB
/
table_query.go
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
package psutilsql
import (
"runtime"
"github.com/noborus/trdsql"
)
// TableReader return table name as trdsql.SliceReader.
func TableReader() (*trdsql.SliceReader, error) {
type tableNames struct {
name string
}
tables := []tableNames{
{name: psCPUTime},
{name: psCPUInfo},
{name: psCPUPercent},
{name: psDiskPartition},
{name: psDiskUsage},
{name: psDocker},
{name: psHostInfo},
{name: psHostUser},
{name: psHostTemperature},
{name: psLoadAvg},
{name: psLoadMisc},
{name: psVirtualMemory},
{name: psSwapMemory},
{name: psNet},
{name: psProcess},
{name: psProcessEx},
}
if runtime.GOOS == "windows" {
tables = append(tables, tableNames{name: psWinservices})
}
return trdsql.NewSliceReader("pstable", tables), nil
}
func definitionQuery(tableName string, w trdsql.Writer) error {
reader := psutilReader(tableName)
if reader == nil {
return ErrNoSuchTable
}
tn, err := reader.TableName()
if err != nil {
return err
}
query := "SELECT * FROM " + tn + " LIMIT 0"
return readerExec(reader, query, w)
}
// PSTableQuery executes SQL on tables.
func PSTableQuery(tableName string, query string, w trdsql.Writer) error {
if len(tableName) > 0 {
return definitionQuery(tableName, w)
}
reader, err := TableReader()
if err != nil {
return err
}
defaultQuery := "SELECT * FROM pstable ORDER BY name"
if query == "" {
query = defaultQuery
}
return readerExec(reader, query, w)
}