aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/datamaps/db.go
blob: 296b1b2c38ab95f1915a93d32ea31a616faa4a15 (plain) (blame)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package datamaps

import (
	"database/sql"
	"errors"
	"fmt"
	"log"
	"os"
	"path"
	"time"

	// Needed for the sqlite3 driver
	_ "github.com/mattn/go-sqlite3"
)

// setupDB creates the intitial database
func setupDB(path string) (*sql.DB, error) {
	stmtBase := `DROP TABLE IF EXISTS datamap;
				 DROP TABLE IF EXISTS datamap_line;
				 DROP TABLE IF EXISTS return;
				 DROP TABLE IF EXISTS return_data;

				  CREATE TABLE datamap(
					  id INTEGER PRIMARY KEY,
					  name TEXT,
					  date_created TEXT);

				  CREATE TABLE datamap_line(
					id INTEGER PRIMARY KEY,   
					dm_id INTEGER,            
					key TEXT NOT NULL,        
					sheet TEXT NOT NULL,      
					cellref TEXT,             
					FOREIGN KEY (dm_id)       
					REFERENCES datamap(id) 
					ON DELETE CASCADE      
				  );                        

				 CREATE TABLE return(
					 id INTEGER PRIMARY KEY,
					 name TEXT,
					 date_created TEXT
					);

				 CREATE TABLE return_data(
					 id INTEGER PRIMARY KEY,
					 dml_id INTEGER,
					 ret_id INTEGER,
					 filename TEXT,
					 value TEXT,
					 numfmt TEXT,
					 vFormatted TEXT,
					 FOREIGN KEY (dml_id)
					 REFERENCES datamap_line(id) 
					 ON DELETE CASCADE
					 FOREIGN KEY (ret_id)
					 REFERENCES return(id) 
					 ON DELETE CASCADE
				 );
				 `
	if _, err := os.Create(path); err != nil {
		return nil, err
	}
	db, err := sql.Open("sqlite3", path)
	if err != nil {
		return db, errors.New("Cannot open that damn database file")
	}

	// We probably don't need pragma here but we have it for later.
	pragma := "PRAGMA foreign_keys = ON;"
	_, err = db.Exec(pragma)
	if err != nil {
		// log.Printf("%q: %s\n", err, pragma)
		return nil, err
	}

	_, err = db.Exec(stmtBase)
	if err != nil {
		// log.Printf("%q: %s\n", err, stmt_base)
		return nil, err
	}

	return db, nil
}

// ImportToDB imports a directory of xlsx files to the database, using the datamap
// to filter the data.
func ImportToDB(opts *Options) error {
	log.Printf("Importing files in %s as return named %s using datamap named %s.", opts.XLSXPath, opts.ReturnName, opts.DMName)

	target, err := getTargetFiles(opts.XLSXPath)
	if err != nil {
		return err
	}

	db, err := sql.Open("sqlite3", opts.DBPath)
	if err != nil {
		return err
	}

	for _, vv := range target {
		// TODO: Do the work!

		if err := importXLSXtoDB(opts.DMName, opts.ReturnName, vv, db); err != nil {
			return err
		}
	}
	return nil
}

// DatamapToDB takes a slice of datamapLine and writes it to a sqlite3 db file.
func DatamapToDB(opts *Options) error {
	log.Printf("Importing datamap file %s and naming it %s.\n", opts.DMPath, opts.DMName)

	data, err := ReadDML(opts.DMPath)
	if err != nil {
		log.Fatal(err)
	}

	d, err := sql.Open("sqlite3", opts.DBPath)
	if err != nil {
		return errors.New("Cannot open that damn database file")
	}

	tx, err := d.Begin()
	if err != nil {
		return err
	}

	pragma := "PRAGMA foreign_keys = ON;"
	_, err = d.Exec(pragma)
	if err != nil {
		log.Printf("%q: %s\n", err, pragma)
		return err
	}

	stmtDm, err := tx.Prepare("INSERT INTO datamap (name, date_created) VALUES(?,?)")
	if err != nil {
		return err
	}

	res, err := stmtDm.Exec(opts.DMName, time.Now())
	if err != nil {
		return err
	}

	lastID, err := res.LastInsertId()
	if err != nil {
		return err
	}

	stmtDml, err := tx.Prepare("INSERT INTO datamap_line (dm_id, key, sheet, cellref) VALUES(?,?,?,?);")
	if err != nil {
		return err
	}

	defer stmtDm.Close()
	defer stmtDml.Close()

	for _, dml := range data {
		_, err = stmtDml.Exec(lastID, dml.Key, dml.Sheet, dml.Cellref)
		if err != nil {
			return err
		}
	}
	err = tx.Commit()
	if err != nil {
		return err
	}

	return nil
}

func importXLSXtoDB(dmName string, returnName string, file string, db *sql.DB) error {
	// d, err := ExtractDBDatamap(dmName, file, db)
	_, filename := path.Split(file)
	d, err := ExtractDBDatamap(dmName, file, db)
	if err != nil {
		return err
	}
	log.Printf("Extracting from %s.\n", file)

	// If there is already a return with a matching name, use that.
	rtnQuery, err := db.Prepare("select id from return where (return.name=?)")
	if err != nil {
		fmt.Errorf("cannot create a query to get the return - %v", err)
	}
	defer rtnQuery.Close()

	var retID int64
	err = rtnQuery.QueryRow(returnName).Scan(&retID)
	if err != nil {
		log.Printf("Couldn't find an existing return named '%s' so let's create it.\n", returnName)
	}

	if retID == 0 {
		stmtReturn, err := db.Prepare("insert into return(name, date_created) values(?,?)")
		if err != nil {
			fmt.Errorf("cannot prepare a statement to create a new return - %v", err)
		}
		defer stmtReturn.Close()

		res, err := stmtReturn.Exec(returnName, time.Now())
		if err != nil {
			err := fmt.Errorf("%v\nCannot create %s", err, returnName)
			log.Println(err.Error())
			os.Exit(1)
		}

		retID, err = res.LastInsertId()
		if err != nil {
			fmt.Errorf("cannot get id of return - %v", err)
		}
	}

	// We're going to need a transaction for the big stuff
	tx, err := db.Begin()
	if err != nil {
		fmt.Errorf("cannot start a database transaction - %v", err)
	}

	for sheetName, sheetData := range d {

		for cellRef, cellData := range sheetData {

			dmlQuery, err := db.Prepare("select id from datamap_line where (sheet=? and cellref=?)")
			if err != nil {
				fmt.Errorf("cannot prepare a statement to get the datamap line - %v", err)
			}
			defer dmlQuery.Close()
			dmlIDRow := dmlQuery.QueryRow(sheetName, cellRef)

			var dmlID *int

			if err := dmlIDRow.Scan(&dmlID); err != nil {
				err := fmt.Errorf("cannot find a datamap_line row for %s and %s: %s", sheetName, cellRef, err)
				log.Println(err.Error())
			}

			insertStmt, err := db.Prepare("insert into return_data (dml_id, ret_id, filename, value, numfmt, vFormatted) values(?,?,?,?,?,?)")
			if err != nil {
				fmt.Errorf("cannot insert row into return_data - %v", err)
			}
			defer insertStmt.Close()

			// Hack to fix bug in Libreoffice numformats for dates
			if cellData.NumFmt == "DD/MM/YY" {
				cellData.SetFormat("dd/mm/yy")
			}
			fValue, err := cellData.FormattedValue()
			if err != nil {
				fmt.Errorf("cannot get the formatted value for %#v, %v", cellData, err)
			}

			_, err = insertStmt.Exec(dmlID, retID, filename, cellData.Value, cellData.NumFmt, fValue)
			if err != nil {
				fmt.Errorf("cannot execute statement to insert return data - %v", err)
			}
		}
	}

	err = tx.Commit()
	if err != nil {
		return err
	}
	return nil
}