-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexporter_sql.go
71 lines (60 loc) · 1.73 KB
/
exporter_sql.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
66
67
68
69
70
71
package ortfodb
import (
"fmt"
"os"
"strings"
)
type SqlExporterOptions struct {
Language string `yaml:"language"`
Output string `yaml:"output,omitempty"`
}
type SqlExporter struct {
result string
}
func (e *SqlExporter) OptionsType() any {
return SqlExporterOptions{}
}
func (e *SqlExporter) Name() string {
return "sql"
}
func (e *SqlExporter) Description() string {
return "Export the database as SQL statements. Rudimentary for now."
}
func (e *SqlExporter) Before(ctx *RunContext, opts ExporterOptions) error {
e.result = ""
if !fileExists(e.outputFilename(ctx)) {
e.result += `CREATE TABLE works (
id TEXT PRIMARY KEY,
title TEXT,
summary TEXT,
start_date TEXT,
end_date TEXT,
tags TEXT,
technologies TEXT
);`
}
return nil
}
func (e *SqlExporter) Export(ctx *RunContext, opts ExporterOptions, work *Work) error {
options := GetExporterOptions[SqlExporterOptions](e, opts)
_, summary := work.FirstParagraph(options.Language)
e.result += fmt.Sprintf(
"INSERT INTO works (id, title, summary, start_date, end_date, tags, technologies) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s');\n",
work.ID,
work.Content.Localize(options.Language).Title,
summary.Content.Markdown(),
work.Metadata.Started,
work.Metadata.Finished,
strings.Join(work.Metadata.Tags, ","),
strings.Join(work.Metadata.MadeWith, ","),
)
return nil
}
func (e *SqlExporter) outputFilename(ctx *RunContext) string {
return strings.Replace(ctx.OutputDatabaseFile, ".json", ".sql", 1)
}
func (e *SqlExporter) After(ctx *RunContext, opts ExporterOptions, built *Database) error {
os.WriteFile(e.outputFilename(ctx), []byte(e.result), 0o644)
ExporterLogCustom(e, "Exported", "green", "SQL file to %s", e.outputFilename(ctx))
return nil
}