0.1 5d00907

createdb.go RawHistoryFolder

1package pkgsqlite
2
3import (
4	"database/sql"
5	"fmt"
6	"reflect"
7	"strings"
8)
9
10// CreateTable creates a table in the database based on the given struct.
11func CreateTable(db *sql.DB, table interface{}, tableName string) error {
12	t := reflect.TypeOf(table)
13
14	var fields []string
15	var foreignKeys []string
16
17	for i := 0; i < t.NumField(); i++ {
18		field := t.Field(i)
19		sqlTag := field.Tag.Get("sql")
20		if sqlTag == "" {
21			continue
22		}
23
24		parts := strings.Split(sqlTag, ",")
25		sqlName := parts[0]
26		constraint := ""
27
28		for _, part := range parts[1:] {
29			if part == "primary_key" {
30				constraint = "PRIMARY KEY"
31			} else if strings.HasPrefix(strings.TrimSpace(part), "foreign_key=") {
32				fkParts := strings.Split(part, "=")
33				refTableAndField := strings.Split(fkParts[1], ".")
34				if len(refTableAndField) == 1 {
35					foreignKeys = append(foreignKeys, fmt.Sprintf("FOREIGN KEY (%s) REFERENCES %s", sqlName, fkParts[1]))
36				} else if len(refTableAndField) == 2 {
37					foreignKeys = append(foreignKeys, fmt.Sprintf("FOREIGN KEY (%s) REFERENCES %s(%s)", sqlName, refTableAndField[0], refTableAndField[1]))
38				}
39			}
40		}
41
42		if constraint == "" {
43			constraint = convertGoTypeToSQLType(field.Type.Name())
44		}
45
46		fieldString := fmt.Sprintf("%s %s", sqlName, constraint)
47		fields = append(fields, fieldString)
48	}
49
50	createTableSQL := fmt.Sprintf("CREATE TABLE %s (%s", tableName, strings.Join(fields, ", "))
51	if len(foreignKeys) > 0 {
52		createTableSQL += ", " + strings.Join(foreignKeys, ", ")
53	}
54	createTableSQL += ");"
55
56	_, err := db.Exec(createTableSQL)
57	if err != nil {
58		return fmt.Errorf("failed to create table %s: %v", tableName, err)
59	}
60
61	return nil
62}
63
64func convertGoTypeToSQLType(goType string) string {
65	switch goType {
66	case "int64":
67		return "INTEGER"
68	case "string":
69		return "TEXT"
70	case "bool":
71		return "BOOLEAN"
72	default:
73		return "TEXT"
74	}
75}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close