diff options
Diffstat (limited to '')
-rw-r--r-- | auth.go | 6 | ||||
-rw-r--r-- | go.mod | 24 | ||||
-rw-r--r-- | go.sum | 61 | ||||
-rw-r--r-- | model/db.go (renamed from model/db_sqlite.go) | 2 | ||||
-rw-r--r-- | model/db_mysql.go | 33 | ||||
-rw-r--r-- | model/models.go (renamed from model/models_mysql.go) | 14 | ||||
-rw-r--r-- | model/models_sqlite.go | 49 | ||||
-rw-r--r-- | model/sexps.sql.go (renamed from model/sexps.sql.mysql.go) | 2 | ||||
-rw-r--r-- | model/sexps.sql.sqlite.go | 52 | ||||
-rw-r--r-- | model/users.sql.go (renamed from model/users.sql.mysql.go) | 2 | ||||
-rw-r--r-- | model/users.sql.sqlite.go | 95 | ||||
-rw-r--r-- | mysql.go | 2 | ||||
-rw-r--r-- | session.go | 2 | ||||
-rw-r--r-- | sql/ddl/mysql.sql | 91 | ||||
-rw-r--r-- | sqlc.yaml | 22 | ||||
-rw-r--r-- | sqlite.go | 46 | ||||
-rw-r--r-- | sqlite_linux.go | 9 | ||||
-rw-r--r-- | sqlite_win.go | 9 |
18 files changed, 74 insertions, 447 deletions
@@ -42,7 +42,7 @@ func RequireAuth(next http.Handler) http.Handler { }) } -func checkLogin(ctx context.Context, user User) (bool, int64) { +func checkLogin(ctx context.Context, user User) (bool, int32) { dbUser, err := Q.GetUserByName(ctx, user.Name) if err == nil { hash := []byte(dbUser.Pwd) @@ -124,6 +124,6 @@ func loginPage() http.HandlerFunc { } } -func userId(r *http.Request) int64 { - return r.Context().Value(userContextKey).(int64) +func userId(r *http.Request) int32 { + return r.Context().Value(userContextKey).(int32) } @@ -11,29 +11,7 @@ require ( github.com/gorilla/securecookie v1.1.2 github.com/gorilla/sessions v1.2.2 github.com/joho/godotenv v1.5.1 - github.com/mattn/go-sqlite3 v1.14.22 - github.com/simukti/sqldb-logger v0.0.0-20230108155151-646c1a075551 golang.org/x/crypto v0.19.0 - modernc.org/sqlite v1.28.0 ) -require ( - github.com/dustin/go-humanize v1.0.1 // indirect - github.com/felixge/httpsnoop v1.0.3 // indirect - github.com/google/uuid v1.3.0 // indirect - github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect - github.com/mattn/go-isatty v0.0.20 // indirect - github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect - golang.org/x/mod v0.8.0 // indirect - golang.org/x/sys v0.17.0 // indirect - golang.org/x/tools v0.1.12 // indirect - lukechampine.com/uint128 v1.2.0 // indirect - modernc.org/cc/v3 v3.40.0 // indirect - modernc.org/ccgo/v3 v3.16.13 // indirect - modernc.org/libc v1.29.0 // indirect - modernc.org/mathutil v1.6.0 // indirect - modernc.org/memory v1.7.2 // indirect - modernc.org/opt v0.1.3 // indirect - modernc.org/strutil v1.1.3 // indirect - modernc.org/token v1.0.1 // indirect -) +require github.com/felixge/httpsnoop v1.0.3 // indirect @@ -1,9 +1,5 @@ github.com/Necoro/form v0.0.0-20240211223301-6fa9f8196e1e h1:v3DDTGBMt9pclCdG7jRyNAABmtJw3uky/Xoi/DfbWNs= github.com/Necoro/form v0.0.0-20240211223301-6fa9f8196e1e/go.mod h1:JxpmgZ5hjL6fyhBoZ4HAUadkp7DNqWlHbFL7l8oic4Y= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= -github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBdXk= github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= @@ -12,10 +8,6 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ= -github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo= -github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= -github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/csrf v1.7.2 h1:oTUjx0vyf2T+wkrx09Trsev1TE+/EbDAeHtSTbtC2eI= github.com/gorilla/csrf v1.7.2/go.mod h1:F1Fj3KG23WYHE6gozCmBAezKookxbIvUJT+121wTuLk= github.com/gorilla/handlers v1.5.2 h1:cLTUSsNkgcwhgRqvCNmdbRWG0A3N4F+M2nWKdScwyEE= @@ -28,58 +20,5 @@ github.com/gorilla/sessions v1.2.2 h1:lqzMYz6bOfvn2WriPUjNByzeXIlVzURcPmgMczkmTj github.com/gorilla/sessions v1.2.2/go.mod h1:ePLdVu+jbEgHH+KWw8I1z2wqd0BAdAQh/8LRvBeoNcQ= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= -github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs= -github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= -github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= -github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= -github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= -github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= -github.com/simukti/sqldb-logger v0.0.0-20230108155151-646c1a075551 h1:+EXKKt7RC4HyE/iE8zSeFL+7YBL8Z7vpBaEE3c7lCnk= -github.com/simukti/sqldb-logger v0.0.0-20230108155151-646c1a075551/go.mod h1:ztTX0ctjRZ1wn9OXrzhonvNmv43yjFUXJYJR95JQAJE= -github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= -github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo= golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= -golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8= -golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= -golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -lukechampine.com/uint128 v1.2.0 h1:mBi/5l91vocEN8otkC5bDLhi2KdCticRiwbdB0O+rjI= -lukechampine.com/uint128 v1.2.0/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk= -modernc.org/cc/v3 v3.40.0 h1:P3g79IUS/93SYhtoeaHW+kRCIrYaxJ27MFPv+7kaTOw= -modernc.org/cc/v3 v3.40.0/go.mod h1:/bTg4dnWkSXowUO6ssQKnOV0yMVxDYNIsIrzqTFDGH0= -modernc.org/ccgo/v3 v3.16.13 h1:Mkgdzl46i5F/CNR/Kj80Ri59hC8TKAhZrYSaqvkwzUw= -modernc.org/ccgo/v3 v3.16.13/go.mod h1:2Quk+5YgpImhPjv2Qsob1DnZ/4som1lJTodubIcoUkY= -modernc.org/ccorpus v1.11.6 h1:J16RXiiqiCgua6+ZvQot4yUuUy8zxgqbqEEUuGPlISk= -modernc.org/ccorpus v1.11.6/go.mod h1:2gEUTrWqdpH2pXsmTM1ZkjeSrUWDpjMu2T6m29L/ErQ= -modernc.org/httpfs v1.0.6 h1:AAgIpFZRXuYnkjftxTAZwMIiwEqAfk8aVB2/oA6nAeM= -modernc.org/httpfs v1.0.6/go.mod h1:7dosgurJGp0sPaRanU53W4xZYKh14wfzX420oZADeHM= -modernc.org/libc v1.29.0 h1:tTFRFq69YKCF2QyGNuRUQxKBm1uZZLubf6Cjh/pVHXs= -modernc.org/libc v1.29.0/go.mod h1:DaG/4Q3LRRdqpiLyP0C2m1B8ZMGkQ+cCgOIjEtQlYhQ= -modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4= -modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo= -modernc.org/memory v1.7.2 h1:Klh90S215mmH8c9gO98QxQFsY+W451E8AnzjoE2ee1E= -modernc.org/memory v1.7.2/go.mod h1:NO4NVCQy0N7ln+T9ngWqOQfi7ley4vpwvARR+Hjw95E= -modernc.org/opt v0.1.3 h1:3XOZf2yznlhC+ibLltsDGzABUGVx8J6pnFMS3E4dcq4= -modernc.org/opt v0.1.3/go.mod h1:WdSiB5evDcignE70guQKxYUl14mgWtbClRi5wmkkTX0= -modernc.org/sqlite v1.28.0 h1:Zx+LyDDmXczNnEQdvPuEfcFVA2ZPyaD7UCZDjef3BHQ= -modernc.org/sqlite v1.28.0/go.mod h1:Qxpazz0zH8Z1xCFyi5GSL3FzbtZ3fvbjmywNogldEW0= -modernc.org/strutil v1.1.3 h1:fNMm+oJklMGYfU9Ylcywl0CO5O6nTfaowNsh2wpPjzY= -modernc.org/strutil v1.1.3/go.mod h1:MEHNA7PdEnEwLvspRMtWTNnp2nnyvMfkimT1NKNAGbw= -modernc.org/tcl v1.15.2 h1:C4ybAYCGJw968e+Me18oW55kD/FexcHbqH2xak1ROSY= -modernc.org/tcl v1.15.2/go.mod h1:3+k/ZaEbKrC8ePv8zJWPtBSW0V7Gg9g8rkmhI1Kfs3c= -modernc.org/token v1.0.1 h1:A3qvTqOwexpfZZeyI0FeGPDlSWX5pjZu9hF4lU+EKWg= -modernc.org/token v1.0.1/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM= -modernc.org/z v1.7.3 h1:zDJf6iHjrnB+WRD88stbXokugjyc0/pB91ri1gO6LZY= -modernc.org/z v1.7.3/go.mod h1:Ipv4tsdxZRbQyLq9Q1M6gdbkxYzdlrciF2Hi/lS7nWE= diff --git a/model/db_sqlite.go b/model/db.go index 97e8f76..2e42192 100644 --- a/model/db_sqlite.go +++ b/model/db.go @@ -1,5 +1,3 @@ -//go:build sqlite - // Code generated by sqlc. DO NOT EDIT. // versions: // sqlc v1.25.0 diff --git a/model/db_mysql.go b/model/db_mysql.go deleted file mode 100644 index b236214..0000000 --- a/model/db_mysql.go +++ /dev/null @@ -1,33 +0,0 @@ -//go:build !sqlite - -// Code generated by sqlc. DO NOT EDIT. -// versions: -// sqlc v1.25.0 - -package model - -import ( - "context" - "database/sql" -) - -type DBTX interface { - ExecContext(context.Context, string, ...interface{}) (sql.Result, error) - PrepareContext(context.Context, string) (*sql.Stmt, error) - QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error) - QueryRowContext(context.Context, string, ...interface{}) *sql.Row -} - -func New(db DBTX) *Queries { - return &Queries{db: db} -} - -type Queries struct { - db DBTX -} - -func (q *Queries) WithTx(tx *sql.Tx) *Queries { - return &Queries{ - db: tx, - } -} diff --git a/model/models_mysql.go b/model/models.go index 359e583..be09076 100644 --- a/model/models_mysql.go +++ b/model/models.go @@ -1,5 +1,3 @@ -//go:build !sqlite - // Code generated by sqlc. DO NOT EDIT. // versions: // sqlc v1.25.0 @@ -22,23 +20,23 @@ type ConstExpense struct { ID int32 Description sql.NullString Expense string - Months int32 + Months uint8 Start time.Time End time.Time PrevID sql.NullInt32 - UserID int32 CategoryID int32 + UserID int32 } type SingleExpense struct { ID int32 Description sql.NullString Expense string - Year int32 - Month int32 - Day int32 - UserID int32 + Year uint16 + Month uint8 + Day uint8 CategoryID int32 + UserID int32 } type User struct { diff --git a/model/models_sqlite.go b/model/models_sqlite.go deleted file mode 100644 index 892cdd7..0000000 --- a/model/models_sqlite.go +++ /dev/null @@ -1,49 +0,0 @@ -//go:build sqlite - -// Code generated by sqlc. DO NOT EDIT. -// versions: -// sqlc v1.25.0 - -package model - -import ( - "database/sql" - "time" -) - -type Category struct { - ID int64 - Name string - ParentID sql.NullInt64 - UserID int64 -} - -type ConstExpense struct { - ID int64 - Description sql.NullString - Expense interface{} - Months int64 - Start time.Time - End time.Time - PrevID sql.NullInt64 - UserID int64 - CategoryID int64 -} - -type SingleExpense struct { - ID int64 - Description sql.NullString - Expense interface{} - Year int64 - Month int64 - Day int64 - UserID int64 - CategoryID int64 -} - -type User struct { - ID int64 - Name string - Pwd string - Description sql.NullString -} diff --git a/model/sexps.sql.mysql.go b/model/sexps.sql.go index 70bc93e..1108e55 100644 --- a/model/sexps.sql.mysql.go +++ b/model/sexps.sql.go @@ -1,5 +1,3 @@ -//go:build !sqlite - // Code generated by sqlc. DO NOT EDIT. // versions: // sqlc v1.25.0 diff --git a/model/sexps.sql.sqlite.go b/model/sexps.sql.sqlite.go deleted file mode 100644 index 6853390..0000000 --- a/model/sexps.sql.sqlite.go +++ /dev/null @@ -1,52 +0,0 @@ -//go:build sqlite - -// Code generated by sqlc. DO NOT EDIT. -// versions: -// sqlc v1.25.0 -// source: sexps.sql - -package model - -import ( - "context" - "database/sql" -) - -const getSingleExpenses = `-- name: GetSingleExpenses :many -SELECT id, description - FROM single_expenses - WHERE user_id = ? -` - -type GetSingleExpensesRow struct { - ID int64 - Description sql.NullString -} - -// GetSingleExpenses -// -// SELECT id, description -// FROM single_expenses -// WHERE user_id = ? -func (q *Queries) GetSingleExpenses(ctx context.Context, userID int64) ([]GetSingleExpensesRow, error) { - rows, err := q.db.QueryContext(ctx, getSingleExpenses, userID) - if err != nil { - return nil, err - } - defer rows.Close() - var items []GetSingleExpensesRow - for rows.Next() { - var i GetSingleExpensesRow - if err := rows.Scan(&i.ID, &i.Description); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Close(); err != nil { - return nil, err - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} diff --git a/model/users.sql.mysql.go b/model/users.sql.go index acc462d..87ae961 100644 --- a/model/users.sql.mysql.go +++ b/model/users.sql.go @@ -1,5 +1,3 @@ -//go:build !sqlite - // Code generated by sqlc. DO NOT EDIT. // versions: // sqlc v1.25.0 diff --git a/model/users.sql.sqlite.go b/model/users.sql.sqlite.go deleted file mode 100644 index 4662ca4..0000000 --- a/model/users.sql.sqlite.go +++ /dev/null @@ -1,95 +0,0 @@ -//go:build sqlite - -// Code generated by sqlc. DO NOT EDIT. -// versions: -// sqlc v1.25.0 -// source: users.sql - -package model - -import ( - "context" -) - -const getUserById = `-- name: GetUserById :one -SELECT id, name, pwd, description - FROM users - WHERE id = ? -` - -// GetUserById -// -// SELECT id, name, pwd, description -// FROM users -// WHERE id = ? -func (q *Queries) GetUserById(ctx context.Context, id int64) (User, error) { - row := q.db.QueryRowContext(ctx, getUserById, id) - var i User - err := row.Scan( - &i.ID, - &i.Name, - &i.Pwd, - &i.Description, - ) - return i, err -} - -const getUserByName = `-- name: GetUserByName :one -SELECT id, name, pwd, description - FROM users - WHERE LOWER(name) = LOWER(?) -` - -// GetUserByName -// -// SELECT id, name, pwd, description -// FROM users -// WHERE LOWER(name) = LOWER(?) -func (q *Queries) GetUserByName(ctx context.Context, lower string) (User, error) { - row := q.db.QueryRowContext(ctx, getUserByName, lower) - var i User - err := row.Scan( - &i.ID, - &i.Name, - &i.Pwd, - &i.Description, - ) - return i, err -} - -const getUsers = `-- name: GetUsers :many -SELECT id, name, pwd, description - FROM users -` - -// GetUsers -// -// SELECT id, name, pwd, description -// FROM users -func (q *Queries) GetUsers(ctx context.Context) ([]User, error) { - rows, err := q.db.QueryContext(ctx, getUsers) - if err != nil { - return nil, err - } - defer rows.Close() - var items []User - for rows.Next() { - var i User - if err := rows.Scan( - &i.ID, - &i.Name, - &i.Pwd, - &i.Description, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Close(); err != nil { - return nil, err - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} @@ -1,5 +1,3 @@ -//go:build !sqlite - package main import ( @@ -29,7 +29,7 @@ type Session struct { } type SessionData struct { - UserID int64 + UserID int32 Authenticated bool } diff --git a/sql/ddl/mysql.sql b/sql/ddl/mysql.sql index 47315f9..5b867c7 100644 --- a/sql/ddl/mysql.sql +++ b/sql/ddl/mysql.sql @@ -1,41 +1,56 @@ create table users ( - id INTEGER not null + id int auto_increment primary key, - name VARCHAR(50) not null - unique, - pwd VARCHAR(255) not null, - description VARCHAR(100) + name varchar(50) not null, + pwd varchar(255) not null, + description varchar(100) null, + constraint name + unique (name) ); create table categories ( - id INTEGER not null + id int auto_increment primary key, - name VARCHAR(50) not null, - parent_id INTEGER - references categories, - user_id INTEGER not null - references users + name varchar(50) not null, + parent_id int null, + user_id int not null, + constraint categories_ibfk_1 + foreign key (parent_id) references categories (id), + constraint categories_ibfk_2 + foreign key (user_id) references users (id) ); +create index parent_id + on categories (parent_id); + +create index user_id + on categories (user_id); + create table const_expenses ( - id INTEGER not null + id int auto_increment primary key, - description VARCHAR(50), - expense NUMERIC(10, 2) not null, - months SMALLINT not null, - start DATE not null, - end DATE not null, - prev_id INTEGER - references const_expenses, - user_id INTEGER not null - references users, - category_id INTEGER not null - references categories + description varchar(50) null, + expense decimal(10, 2) not null, + months tinyint unsigned not null, + start date not null, + end date not null, + prev_id int null, + category_id int not null, + user_id int not null, + constraint const_expenses_ibfk_1 + foreign key (prev_id) references const_expenses (id), + constraint const_expenses_ibfk_2 + foreign key (category_id) references categories (id), + constraint const_expenses_ibfk_3 + foreign key (user_id) references users (id) ); +create index category_id + on const_expenses (category_id); + create index idx_start_end on const_expenses (user_id, start, end); @@ -45,20 +60,28 @@ create index ix_constexpense_end create index ix_constexpense_start on const_expenses (start); +create index prev_id + on const_expenses (prev_id); + create table single_expenses ( - id INTEGER not null + id int auto_increment primary key, - description VARCHAR(50), - expense NUMERIC(10, 2) not null, - year INTEGER not null, - month SMALLINT not null, - day SMALLINT not null, - user_id INTEGER not null - references users, - category_id INTEGER not null - references categories + description varchar(50) null, + expense decimal(10, 2) not null, + year year not null, + month tinyint(6) unsigned not null, + day tinyint(6) unsigned not null, + category_id int not null, + user_id int not null, + constraint single_expenses_ibfk_1 + foreign key (category_id) references categories (id), + constraint single_expenses_ibfk_2 + foreign key (user_id) references users (id) ); +create index category_id + on single_expenses (category_id); + create index idx_single_date - on single_expenses (user_id, year, month); + on single_expenses (user_id, year, month);
\ No newline at end of file @@ -1,17 +1,5 @@ version: "2" sql: - - engine: "sqlite" - queries: "sql/*.sql" - schema: "sql/ddl/sqlite.sql" - gen: - go: - out: "model" - emit_sql_as_comment: true - build_tags: "sqlite" - output_db_file_name: "db_sqlite.go" - output_models_file_name: "models_sqlite.go" - output_files_suffix: ".sqlite" - - engine: "mysql" queries: "sql/*.sql" schema: "sql/ddl/mysql.sql" @@ -19,7 +7,9 @@ sql: go: out: "model" emit_sql_as_comment: true - build_tags: "!sqlite" - output_db_file_name: "db_mysql.go" - output_models_file_name: "models_mysql.go" - output_files_suffix: ".mysql"
\ No newline at end of file + overrides: + - db_type: "year" + go_type: "uint16" + - db_type: "tinyint" + unsigned: true + go_type: "uint8"
\ No newline at end of file diff --git a/sqlite.go b/sqlite.go deleted file mode 100644 index 8ee6325..0000000 --- a/sqlite.go +++ /dev/null @@ -1,46 +0,0 @@ -//go:build sqlite - -package main - -import ( - "context" - "database/sql" - "log" - "log/slog" - - sqldblogger "github.com/simukti/sqldb-logger" -) - -type logger struct { -} - -func (l logger) Log(ctx context.Context, level sqldblogger.Level, msg string, data map[string]interface{}) { - attrs := make([]slog.Attr, 0, len(data)) - - for k, v := range data { - attrs = append(attrs, slog.Any(k, v)) - } - - var lvl slog.Level - switch level { - case sqldblogger.LevelDebug, sqldblogger.LevelTrace: - lvl = slog.LevelDebug - case sqldblogger.LevelInfo: - lvl = slog.LevelInfo - case sqldblogger.LevelError: - lvl = slog.LevelError - } - - slog.LogAttrs(ctx, lvl, msg, attrs...) -} - -func openDB(dsn string) *sql.DB { - db, err := sql.Open(driverName, dsn) - if err != nil { - log.Fatal(err) - } - slog.SetLogLoggerLevel(slog.LevelDebug) - return sqldblogger.OpenDriver(dsn, db.Driver(), logger{}, - sqldblogger.WithSQLQueryAsMessage(false), - sqldblogger.WithMinimumLevel(sqldblogger.LevelTrace)) -} diff --git a/sqlite_linux.go b/sqlite_linux.go deleted file mode 100644 index cf65cb2..0000000 --- a/sqlite_linux.go +++ /dev/null @@ -1,9 +0,0 @@ -//go:build sqlite && !windows - -package main - -import ( - _ "github.com/mattn/go-sqlite3" -) - -const driverName = "sqlite3" diff --git a/sqlite_win.go b/sqlite_win.go deleted file mode 100644 index f01541a..0000000 --- a/sqlite_win.go +++ /dev/null @@ -1,9 +0,0 @@ -//go:build sqlite && windows - -package main - -import ( - _ "modernc.org/sqlite" -) - -const driverName = "sqlite" |