From daeca1d601c8032f7c5c5b6fe8b373d0484d8984 Mon Sep 17 00:00:00 2001 From: Mark Chadwick Date: Sat, 23 Nov 2024 15:11:46 +1300 Subject: [PATCH] update --- cmd/deltadb/delta.svg | 57 ++++++ meta/sqlite/db.go | 465 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 522 insertions(+) create mode 100644 cmd/deltadb/delta.svg create mode 100644 meta/sqlite/db.go diff --git a/cmd/deltadb/delta.svg b/cmd/deltadb/delta.svg new file mode 100644 index 000000000..c303a7944 --- /dev/null +++ b/cmd/deltadb/delta.svg @@ -0,0 +1,57 @@ + + + + + + +structs + + + +datum + +datum + +🔑 + +datum_id + +I + +* + +  + +datum + +T + +* + + + +method + +method + +🔑 + +method_id + +I + +* + +  + +method + +T + +* + + + diff --git a/meta/sqlite/db.go b/meta/sqlite/db.go new file mode 100644 index 000000000..67631df12 --- /dev/null +++ b/meta/sqlite/db.go @@ -0,0 +1,465 @@ +package sqlite + +import ( + "context" + "database/sql" + "fmt" + "log" + + "github.com/GeoNet/delta/meta" +) + +type DB struct { + db *sql.DB +} + +func New(db *sql.DB) DB { + return DB{ + db: db, + } +} + +func (d DB) exec(ctx context.Context, tx *sql.Tx, cmds ...string) error { + for _, cmd := range cmds { + if _, err := tx.ExecContext(ctx, cmd); err != nil { + return fmt.Errorf("cmd %q: %w", cmd, err) + } + } + return nil +} + +func (d DB) prepare(ctx context.Context, tx *sql.Tx, cmd string, values ...[]any) error { + + stmt, err := tx.PrepareContext(ctx, cmd) + if err != nil { + return err + } + defer stmt.Close() + + for _, v := range values { + if _, err := stmt.ExecContext(ctx, v...); err != nil { + return fmt.Errorf("%v : %w", v, err) + } + } + + return nil +} + +func (d DB) Init(ctx context.Context, list []meta.TableList) error { + + tables := make(map[string]meta.TableList) + for _, v := range list { + tables[v.Table.Name()] = v + } + + // Get a Tx for making transaction requests. + tx, err := d.db.BeginTx(ctx, nil) + if err != nil { + return err + } + // Defer a rollback in case anything fails, not actually + // worried about any rollback error. + defer func() { _ = tx.Rollback() }() + + // overall lookup tables, this should likely be pre-populated + // to allow for constraints to be applied. + if err := d.exec(ctx, tx, datum.Create); err != nil { + return err + } + + if err := d.exec(ctx, tx, method.Create); err != nil { + return err + } + + /* + if err := d.exec(ctx, tx, placeRoleCreate); err != nil { + return err + } + */ + + for _, l := range list { + + switch l.Table.Name() { + /* + case "Asset": + if err := d.exec(ctx, tx, makeCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, makeInsert(), columns(l, makeFields(), nil, "")...); err != nil { + return err + } + if err := d.exec(ctx, tx, modelCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, modelInsert(), columns(l, modelFields(), nil, "")...); err != nil { + return err + } + if err := d.exec(ctx, tx, assetCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, assetInsert(), columns(l, assetFields(), nil, "")...); err != nil { + return err + } + case "Placename": + if err := d.exec(ctx, tx, placenameCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, placenameInsert(), columns(l, placenameFields(), nil, "")...); err != nil { + return err + } + case "Network": + if err := d.exec(ctx, tx, networkCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, networkInsert(), columns(l, networkFields(), nil, "")...); err != nil { + return err + } + case "Station": + if err := d.prepare(ctx, tx, datumInsert(), columns(l, datumFields(), nil, "")...); err != nil { + return err + } + if err := d.exec(ctx, tx, stationCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, stationInsert(), columns(l, stationFields(), nil, "")...); err != nil { + return err + } + if err := d.exec(ctx, tx, stationNetworkCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, stationNetworkInsert(), columns(l, stationNetworkFields(), stationNetworkNotNulls(), "")...); err != nil { + return err + } + case "Sample": + if err := d.prepare(ctx, tx, datumInsert(), columns(l, datumFields(), nil, "")...); err != nil { + return err + } + if err := d.exec(ctx, tx, sampleCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, sampleInsert(), columns(l, sampleFields(), nil, "")...); err != nil { + return err + } + case "Site": + if err := d.prepare(ctx, tx, datumInsert(), columns(l, datumFields(), nil, "")...); err != nil { + return err + } + if err := d.exec(ctx, tx, siteCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, siteInsert(), columns(l, siteFields(), nil, "")...); err != nil { + return err + } + case "Point": + if err := d.prepare(ctx, tx, datumInsert(), columns(l, datumFields(), nil, "")...); err != nil { + return err + } + if err := d.exec(ctx, tx, pointCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, pointInsert(), columns(l, pointFields(), nil, "")...); err != nil { + return err + } + case "Mark": + if err := d.exec(ctx, tx, markCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, markInsert(), columns(l, markFields(), nil, "")...); err != nil { + return err + } + if err := d.exec(ctx, tx, markNetworkCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, markNetworkInsert(), columns(l, markNetworkFields(), markNetworkNotNulls(), "")...); err != nil { + return err + } + case "Datalogger": + if err := d.prepare(ctx, tx, placeRoleInsert(), columns(l, placeRoleFields(), nil, "")...); err != nil { + return err + } + if err := d.exec(ctx, tx, dataloggerCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, dataloggerInsert(), columns(l, dataloggerFields(), nil, "")...); err != nil { + return err + } + case "Sensor": + if err := d.prepare(ctx, tx, methodInsert(), columns(l, methodFields(), nil, "")...); err != nil { + return err + } + if err := d.exec(ctx, tx, sensorCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, sensorInsert(), columns(l, sensorFields(), nil, "")...); err != nil { + return err + } + case "Recorder": + if err := d.prepare(ctx, tx, methodInsert(), columns(l, methodFields(), nil, "")...); err != nil { + return err + } + if err := d.exec(ctx, tx, recorderCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, recorderModelInsert(), columns(l, recorderModelFields(), nil, "")...); err != nil { + return err + } + if err := d.prepare(ctx, tx, recorderInsert(), columns(l, recorderFields(), nil, "")...); err != nil { + return err + } + case "Monument": + if err := d.exec(ctx, tx, markTypeCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, markTypeInsert(), columns(l, markTypeFields(), nil, "")...); err != nil { + return err + } + if err := d.exec(ctx, tx, monumentTypeCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, monumentTypeInsert(), columns(l, monumentTypeFields(), nil, "")...); err != nil { + return err + } + if err := d.exec(ctx, tx, foundationTypeCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, foundationTypeInsert(), columns(l, foundationTypeFields(), nil, "")...); err != nil { + return err + } + if err := d.exec(ctx, tx, bedrockCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, bedrockInsert(), columns(l, bedrockFields(), nil, "")...); err != nil { + return err + } + if err := d.exec(ctx, tx, geologyCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, geologyInsert(), columns(l, geologyFields(), nil, "")...); err != nil { + return err + } + if err := d.exec(ctx, tx, monumentCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, monumentInsert(), columns(l, monumentFields(), nil, "")...); err != nil { + return err + } + case "Firmware": + if err := d.exec(ctx, tx, firmwareCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, firmwareInsert(), columns(l, firmwareFields(), nil, "")...); err != nil { + return err + } + case "Visibility": + if err := d.exec(ctx, tx, visibilityCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, visibilityInsert(), columns(l, visibilityFields(), nil, "")...); err != nil { + return err + } + case "Timing": + if err := d.exec(ctx, tx, timingCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, timingInsert(), columns(l, timingFields(), nil, "")...); err != nil { + return err + } + case "Telemetry": + if err := d.exec(ctx, tx, telemetryCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, telemetryInsert(), columns(l, telemetryFields(), nil, "")...); err != nil { + return err + } + case "Polarity": + if err := d.exec(ctx, tx, polarityCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, polarityInsert(), columns(l, polarityFields(), nil, "")...); err != nil { + return err + } + case "Gain": + if err := d.exec(ctx, tx, gain.Create); err != nil { + return err + } + if err := d.prepare(ctx, tx, gain.Insert, gain.Columns(l)...); err != nil { + return err + } + case "Preamp": + if err := d.exec(ctx, tx, preampCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, preampInsert(), columns(l, preampFields(), nil, "")...); err != nil { + return err + } + case "Mount": + if err := d.prepare(ctx, tx, datumInsert(), columns(l, datumFields(), nil, "")...); err != nil { + return err + } + if err := d.exec(ctx, tx, mountCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, mountInsert(), columns(l, mountFields(), nil, "")...); err != nil { + return err + } + if err := d.exec(ctx, tx, mountNetworkCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, mountNetworkInsert(), columns(l, mountNetworkFields(), mountNetworkNotNulls(), "")...); err != nil { + return err + } + case "Camera": + if err := d.exec(ctx, tx, cameraCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, cameraInsert(), columns(l, cameraFields(), nil, "")...); err != nil { + return err + } + case "Doas": + if err := d.exec(ctx, tx, doasCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, doasInsert(), columns(l, doasFields(), nil, "")...); err != nil { + return err + } + case "View": + if err := d.exec(ctx, tx, viewCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, viewInsert(), columns(l, viewFields(), nil, "")...); err != nil { + return err + } + case "Antenna": + if err := d.exec(ctx, tx, antennaCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, antennaInsert(), columns(l, antennaFields(), nil, "")...); err != nil { + return err + } + case "MetSensor": + if err := d.exec(ctx, tx, metsensorCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, metsensorInsert(), columns(l, metsensorFields(), nil, "")...); err != nil { + return err + } + case "Radome": + if err := d.exec(ctx, tx, radomeCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, radomeInsert(), columns(l, radomeFields(), nil, "")...); err != nil { + return err + } + case "Receiver": + if err := d.exec(ctx, tx, receiverCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, receiverInsert(), columns(l, receiverFields(), nil, "")...); err != nil { + return err + } + case "Session": + if err := d.exec(ctx, tx, sessionCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, sessionInsert(), columns(l, sessionFields(), nil, "")...); err != nil { + return err + } + case "Stream": + if err := d.exec(ctx, tx, streamCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, streamInsert(), columns(l, streamFields(), nil, "")...); err != nil { + return err + } + case "Channel": + if err := d.exec(ctx, tx, channelCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, channelInsert(), columns(l, channelFields(), nil, "")...); err != nil { + return err + } + case "Component": + if err := d.exec(ctx, tx, componentCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, componentInsert(), columns(l, componentFields(), nil, "")...); err != nil { + return err + } + case "Connection": + if err := d.prepare(ctx, tx, placeRoleInsert(), columns(l, placeRoleFields(), nil, "")...); err != nil { + return err + } + if err := d.exec(ctx, tx, connectionCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, connectionInsert(), columns(l, connectionFields(), nil, "")...); err != nil { + return err + } + case "Feature": + if err := d.exec(ctx, tx, featureCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, featureInsert(), columns(l, featureFields(), nil, "")...); err != nil { + return err + } + case "Citation": + if err := d.exec(ctx, tx, citationCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, citationInsert(), columns(l, citationFields(), nil, "")...); err != nil { + return err + } + case "Calibration": + if err := d.exec(ctx, tx, calibrationCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, calibrationInsert(), columns(l, calibrationFields(), nil, "")...); err != nil { + return err + } + case "Gauge": + if err := d.exec(ctx, tx, gaugeCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, gaugeInsert(), columns(l, gaugeFields(), nil, "")...); err != nil { + return err + } + case "Constituent": + if err := d.exec(ctx, tx, constituentCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, constituentInsert(), columns(l, constituentFields(), nil, "")...); err != nil { + return err + } + case "Dart": + if err := d.exec(ctx, tx, dartCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, dartInsert(), columns(l, dartFields(), nil, "")...); err != nil { + return err + } + case "Class": + if err := d.exec(ctx, tx, classCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, classInsert(), columns(l, classFields(), nil, "")...); err != nil { + return err + } + if err := d.exec(ctx, tx, classCitationCreate); err != nil { + return err + } + if err := d.prepare(ctx, tx, classCitationInsert(), columns(l, classCitationFields(), nil, classCitationUnwrap)...); err != nil { + return err + } + */ + default: + log.Println(l.Table.Name()) + } + } + + // Commit the transaction. + if err = tx.Commit(); err != nil { + return err + } + + return nil +}