| |
1 package main |
| |
2 |
| |
3 import ( |
| |
4 "database/sql" |
| |
5 "log" |
| |
6 "path" |
| |
7 |
| |
8 _ "modernc.org/sqlite" |
| |
9 ) |
| |
10 |
| |
11 func db_init() *sql.DB { |
| |
12 db, err := sql.Open("sqlite", "uwbuild.db") |
| |
13 if err != nil { |
| |
14 log.Fatal(err) |
| |
15 } |
| |
16 |
| |
17 err = db_prepare(db) |
| |
18 if err != nil { |
| |
19 log.Fatal(err) |
| |
20 } |
| |
21 |
| |
22 return db |
| |
23 } |
| |
24 |
| |
25 func db_prepare(db *sql.DB) error { |
| |
26 _, err := db.Exec("select id from Repository") |
| |
27 if err == nil { |
| |
28 return nil |
| |
29 } |
| |
30 |
| |
31 _, err = db.Exec(` |
| |
32 create table Repository ( |
| |
33 id integer primary key autoincrement, |
| |
34 path text not null unique, |
| |
35 name text not null |
| |
36 ); |
| |
37 create table CommitInfo ( |
| |
38 id integer primary key autoincrement, |
| |
39 repository_id integer not null references Repository(id), |
| |
40 node text, |
| |
41 node_short text, |
| |
42 author text, |
| |
43 description text, |
| |
44 date text |
| |
45 ); |
| |
46 create table BuildEnv ( |
| |
47 id integer primary key autoincrement, |
| |
48 name text not null unique, |
| |
49 host text, |
| |
50 os text |
| |
51 ); |
| |
52 create table BuildEnvResult ( |
| |
53 id integer primary key autoincrement, |
| |
54 buildenv_id integer not null references BuildEnv(id), |
| |
55 commit_id integer not null references CommitInfo(id) |
| |
56 ); |
| |
57 create table BuildResult ( |
| |
58 id integer primary key autoincrement, |
| |
59 buildenvresult_id integer not null references BuildEnvResult(id), |
| |
60 name text, |
| |
61 isplatform text, |
| |
62 notplatform text, |
| |
63 config text, |
| |
64 configure_cmd text, |
| |
65 compile_cmd text, |
| |
66 check_cmd text, |
| |
67 configure_result integer, |
| |
68 compile_result integer, |
| |
69 check_result integer |
| |
70 ); |
| |
71 |
| |
72 `) |
| |
73 return err |
| |
74 } |
| |
75 |
| |
76 func db_load_config(db *sql.DB, config *Config) { |
| |
77 for _, repo := range config.Repository { |
| |
78 _, err := db.Exec(` |
| |
79 insert into Repository(path, name) |
| |
80 values (?, ?) |
| |
81 on conflict(path) do nothing`, repo.Path, path.Base(repo.Path)) |
| |
82 if err != nil { |
| |
83 log.Fatal(err) |
| |
84 } |
| |
85 } |
| |
86 |
| |
87 for _, buildenv := range config.BuildEnv { |
| |
88 _, err := db.Exec(` |
| |
89 insert into BuildEnv(name, host, os) |
| |
90 values (?, ?, ?) |
| |
91 on conflict(name) do update set |
| |
92 host = ?2, os = ?3;`, buildenv.Name, buildenv.Host, buildenv.OS) |
| |
93 if err != nil { |
| |
94 log.Fatal(err) |
| |
95 } |
| |
96 } |
| |
97 } |
| |
98 |
| |
99 func db_store_results(db *sql.DB, repo *Repository, commit *Commit, result []BuildEnvResult) error { |
| |
100 r, err := db.Exec(` |
| |
101 insert into CommitInfo(repository_id, node, node_short, author, description, date) |
| |
102 values ((select id from repository where path = ?), ?, ?, ?, ?, ?) |
| |
103 `, repo.Path, commit.Node, commit.NodeShort, commit.Author, commit.Desc, commit.Date) |
| |
104 if err != nil { |
| |
105 log.Fatal(err) |
| |
106 } |
| |
107 repo_id, err := r.LastInsertId() |
| |
108 if err != nil { |
| |
109 log.Fatal(err) |
| |
110 } |
| |
111 |
| |
112 for _, res := range result { |
| |
113 r, err := db.Exec(` |
| |
114 insert into BuildEnvResult(buildenv_id, commit_id) |
| |
115 values ((select id from BuildEnv where name = ?), ?) |
| |
116 `, res.Env.Name, repo_id) |
| |
117 if err != nil { |
| |
118 return err |
| |
119 } |
| |
120 buildenvresult_id, err := r.LastInsertId() |
| |
121 |
| |
122 for _, build := range res.Results { |
| |
123 _, err = db.Exec(` |
| |
124 insert into BuildResult(buildenvresult_id, name, isplatform, notplatform, config, |
| |
125 configure_cmd, compile_cmd, check_cmd, configure_result, compile_result, check_result) |
| |
126 values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) |
| |
127 `, buildenvresult_id, build.Build.Name, build.Build.IsPlatform, build.Build.NotPlatform, "", |
| |
128 build.Build.Configure, build.Build.Compile, build.Build.Check, |
| |
129 build.Configure, build.Compile, build.Check) |
| |
130 } |
| |
131 |
| |
132 } |
| |
133 |
| |
134 return nil |
| |
135 } |