]> saetta.ns0.it Git - zakgrid_go/commitdiff
First commit. Draft.
authorAndrea Zagli <azagli@libero.it>
Sat, 21 Aug 2021 05:51:26 +0000 (07:51 +0200)
committerAndrea Zagli <azagli@libero.it>
Sat, 21 Aug 2021 05:51:26 +0000 (07:51 +0200)
.gitignore [new file with mode: 0644]
go.mod [new file with mode: 0644]
grid.db [new file with mode: 0644]
main.go [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..3325d94
--- /dev/null
@@ -0,0 +1,2 @@
+*.*~
+go.sum
diff --git a/go.mod b/go.mod
new file mode 100644 (file)
index 0000000..0ac1ed5
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,5 @@
+module examples.com/zakgrid_go
+
+go 1.16
+
+require github.com/mattn/go-sqlite3 v1.14.8
diff --git a/grid.db b/grid.db
new file mode 100644 (file)
index 0000000..96ad34c
Binary files /dev/null and b/grid.db differ
diff --git a/main.go b/main.go
new file mode 100644 (file)
index 0000000..83bf1ed
--- /dev/null
+++ b/main.go
@@ -0,0 +1,109 @@
+package main
+
+import (
+       "fmt"
+       "log"
+       "database/sql"
+       _ "github.com/mattn/go-sqlite3"
+)
+
+type column struct {
+       name string
+       title string
+       align int
+}
+
+type grid struct {
+       cols []column
+}
+
+func (g *grid) addColumn (c column) {
+       g.cols = append(g.cols, c)
+}
+
+func (g *grid) getHeader () string {
+       header := "<tr>\n"
+
+       for i, v := range g.cols {
+               header += fmt.Sprintf("\t<th>%s</th>\n", v.title)
+               fmt.Println(i, v)
+       }
+
+       header += "</tr>\n"
+
+       return header
+}
+
+func (g *grid) getRowFromDb (m map[string]interface{}) string {
+       ret := "<tr>\n"
+
+       for _, c := range g.cols {
+               ret += fmt.Sprintf("\t<td>%v</td>\n", m[c.name])
+       }
+
+       ret += "</tr>\n"
+
+       return ret
+}
+
+func main() {
+       var g grid
+
+       c := column{name: "id", title: "ID"}
+       g.addColumn(c)
+
+       c = column{name: "name", title: "Name"}
+       g.addColumn(c)
+
+       c = column{name: "surname", title: "Surname"}
+       g.addColumn(c)
+
+       c = column{name: "age", title: "Age"}
+       g.addColumn(c)
+
+       fmt.Println (g)
+
+       header := g.getHeader()
+
+       fmt.Println(header)
+
+       db, err := sql.Open("sqlite3", "./grid.db")
+       if err != nil {
+               // handle err
+               log.Fatal(err)
+       }
+
+       rows, err := db.Query("SELECT * FROM clients")
+       if err != nil {
+               // handle err
+               log.Fatal(err)
+       }
+       cols, err := rows.Columns()
+       if err != nil {
+               // handle err
+               log.Fatal(err)
+       }
+
+       fmt.Println(cols)
+
+       columns := make([]interface{}, len(cols))
+    columnPointers := make([]interface{}, len(cols))
+    for i, _ := range columns {
+        columnPointers[i] = &columns[i]
+    }
+
+       for rows.Next() {
+               err = rows.Scan(columnPointers...)
+               fmt.Println(columnPointers)
+
+               m := make(map[string]interface{})
+               for i, colName := range cols {
+                       val := columnPointers[i].(*interface{})
+                       m[colName] = *val
+               }
+
+               fmt.Println(m)
+
+               fmt.Println(g.getRowFromDb(m))
+       }
+}