From c6ebde6b7b5653f55d058065f45afe3f9c46f925 Mon Sep 17 00:00:00 2001 From: AustrianToast Date: Fri, 27 Sep 2024 16:33:15 +0200 Subject: [PATCH] started rewrite into golang --- go.mod | 11 +++++++ go.sum | 8 +++++ main.go | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..9b25a1b --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module hentai + +go 1.23.1 + +require github.com/ncruces/go-sqlite3 v0.18.3 + +require ( + github.com/ncruces/julianday v1.0.0 // indirect + github.com/tetratelabs/wazero v1.8.0 // indirect + golang.org/x/sys v0.25.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..8924ce1 --- /dev/null +++ b/go.sum @@ -0,0 +1,8 @@ +github.com/ncruces/go-sqlite3 v0.18.3 h1:tyMa75uh7LcINcfo0WrzOvcTkfz8Hqu0TEPX+KVyes4= +github.com/ncruces/go-sqlite3 v0.18.3/go.mod h1:HAwOtA+cyEX3iN6YmkpQwfT4vMMgCB7rQRFUdOgEFik= +github.com/ncruces/julianday v1.0.0 h1:fH0OKwa7NWvniGQtxdJRxAgkBMolni2BjDHaWTxqt7M= +github.com/ncruces/julianday v1.0.0/go.mod h1:Dusn2KvZrrovOMJuOt0TNXL6tB7U2E8kvza5fFc9G7g= +github.com/tetratelabs/wazero v1.8.0 h1:iEKu0d4c2Pd+QSRieYbnQC9yiFlMS9D+Jr0LsRmcF4g= +github.com/tetratelabs/wazero v1.8.0/go.mod h1:yAI0XTsMBhREkM/YDAK/zNou3GoiAce1P6+rp/wQhjs= +golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= +golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= diff --git a/main.go b/main.go new file mode 100644 index 0000000..0d57f5e --- /dev/null +++ b/main.go @@ -0,0 +1,90 @@ +package main + +import ( + "log" + + "github.com/ncruces/go-sqlite3" + _ "github.com/ncruces/go-sqlite3/embed" +) + +const memory = ":memory:" + +func main() { + db, err := sqlite3.Open(memory) + if err != nil { + log.Fatal(err) + } + + /* Not necessary because I am currently saving the db in memory + sqlstmt := ` + DROP TABLE IF EXISTS artists; + DROP TABLE IF EXISTS websites; + DROP TABLE IF EXISTS links; + ` + err = db.Exec(sqlstmt) + if err != nil { + log.Fatal(err) + } + */ + + sqlstmt := ` + CREATE TABLE IF NOT EXISTS artists ( + id integer NOT NULL CONSTRAINT artists_pk PRIMARY KEY AUTOINCREMENT, + name text NOT NULL + ); + CREATE TABLE IF NOT EXISTS websites ( + id integer NOT NULL CONSTRAINT websites_pk PRIMARY KEY AUTOINCREMENT, + name text NOT NULL + ); + CREATE TABLE IF NOT EXISTS links ( + id integer NOT NULL CONSTRAINT links_pk PRIMARY KEY AUTOINCREMENT, + url text NOT NULL, + websites_id integer NOT NULL, + artists_id integer NOT NULL, + CONSTRAINT links_artists FOREIGN KEY (artists_id) + REFERENCES artists (id), + CONSTRAINT links_websites FOREIGN KEY (websites_id) + REFERENCES websites (id) + ); + ` + err = db.Exec(sqlstmt) + if err != nil { + log.Fatal(err) + } + + /* + err = db.Exec(`INSERT INTO users (id, name) VALUES (0, 'go'), (1, 'zig'), (2, 'whatever')`) + if err != nil { + log.Fatal(err) + } + */ + + sqlstmt = ` + SELECT name + FROM sqlite_schema + WHERE type ='table' AND name NOT LIKE 'sqlite_%'; + ` + + stmt, _, err := db.Prepare(sqlstmt) + if err != nil { + log.Fatal(err) + } + defer stmt.Close() + + for stmt.Step() { + log.Println("created table:", stmt.ColumnText(0)) + } + if err := stmt.Err(); err != nil { + log.Fatal(err) + } + + err = stmt.Close() + if err != nil { + log.Fatal(err) + } + + err = db.Close() + if err != nil { + log.Fatal(err) + } +}