@@ -2,43 +2,43 @@ package postgres
22
33import (
44 "context"
5-
6- "github.com/Masterminds/squirrel"
5+ "strings"
76
87 "github.com/usememos/memos/store"
98)
109
1110func (d * DB ) CreateStorage (ctx context.Context , create * store.Storage ) (* store.Storage , error ) {
12- qb := squirrel . Insert ( "storage" ). Columns ( " name" , "type" , "config" )
13- values := []any {create .Name , create .Type , create .Config }
11+ fields := [] string { " name" , "type" , "config" }
12+ args := []any {create .Name , create .Type , create .Config }
1413
15- qb = qb .Values (values ... ).Suffix ("RETURNING id" )
16- query , args , err := qb .PlaceholderFormat (squirrel .Dollar ).ToSql ()
17- if err != nil {
14+ stmt := "INSERT INTO storage (" + strings .Join (fields , ", " ) + ") VALUES (" + placeholders (len (args )) + ") RETURNING id"
15+ if err := d .db .QueryRowContext (ctx , stmt , args ... ).Scan (
16+ & create .ID ,
17+ ); err != nil {
1818 return nil , err
1919 }
2020
21- err = d .db .QueryRowContext (ctx , query , args ... ).Scan (& create .ID )
22- if err != nil {
23- return nil , err
24- }
25-
26- return create , nil
21+ storage := create
22+ return storage , nil
2723}
2824
2925func (d * DB ) ListStorages (ctx context.Context , find * store.FindStorage ) ([]* store.Storage , error ) {
30- qb := squirrel .Select ("id" , "name" , "type" , "config" ).From ("storage" ).OrderBy ("id DESC" )
31-
26+ where , args := []string {"1 = 1" }, []any {}
3227 if find .ID != nil {
33- qb = qb .Where (squirrel.Eq {"id" : * find .ID })
34- }
35-
36- query , args , err := qb .PlaceholderFormat (squirrel .Dollar ).ToSql ()
37- if err != nil {
38- return nil , err
39- }
40-
41- rows , err := d .db .QueryContext (ctx , query , args ... )
28+ where , args = append (where , "id = " + placeholder (len (args )+ 1 )), append (args , * find .ID )
29+ }
30+
31+ rows , err := d .db .QueryContext (ctx , `
32+ SELECT
33+ id,
34+ name,
35+ type,
36+ config
37+ FROM storage
38+ WHERE ` + strings .Join (where , " AND " )+ `
39+ ORDER BY id DESC` ,
40+ args ... ,
41+ )
4242 if err != nil {
4343 return nil , err
4444 }
@@ -47,7 +47,12 @@ func (d *DB) ListStorages(ctx context.Context, find *store.FindStorage) ([]*stor
4747 list := []* store.Storage {}
4848 for rows .Next () {
4949 storage := & store.Storage {}
50- if err := rows .Scan (& storage .ID , & storage .Name , & storage .Type , & storage .Config ); err != nil {
50+ if err := rows .Scan (
51+ & storage .ID ,
52+ & storage .Name ,
53+ & storage .Type ,
54+ & storage .Config ,
55+ ); err != nil {
5156 return nil , err
5257 }
5358 list = append (list , storage )
@@ -61,60 +66,37 @@ func (d *DB) ListStorages(ctx context.Context, find *store.FindStorage) ([]*stor
6166}
6267
6368func (d * DB ) UpdateStorage (ctx context.Context , update * store.UpdateStorage ) (* store.Storage , error ) {
64- qb := squirrel .Update ("storage" )
65-
69+ set , args := []string {}, []any {}
6670 if update .Name != nil {
67- qb = qb . Set ( "name" , * update .Name )
71+ set , args = append ( set , "name = " + placeholder ( len ( args ) + 1 )), append ( args , * update .Name )
6872 }
6973 if update .Config != nil {
70- qb = qb .Set ("config" , * update .Config )
71- }
72-
73- qb = qb .Where (squirrel.Eq {"id" : update .ID })
74-
75- query , args , err := qb .PlaceholderFormat (squirrel .Dollar ).ToSql ()
76- if err != nil {
77- return nil , err
78- }
79-
80- _ , err = d .db .ExecContext (ctx , query , args ... )
81- if err != nil {
82- return nil , err
74+ set , args = append (set , "config = " + placeholder (len (args )+ 1 )), append (args , * update .Config )
8375 }
8476
77+ stmt := `UPDATE storage SET ` + strings .Join (set , ", " ) + ` WHERE id = ` + placeholder (len (args )+ 1 ) + ` RETURNING id, name, type, config`
78+ args = append (args , update .ID )
8579 storage := & store.Storage {}
86- query , args , err = squirrel .Select ("id" , "name" , "type" , "config" ).
87- From ("storage" ).
88- Where (squirrel.Eq {"id" : update .ID }).
89- PlaceholderFormat (squirrel .Dollar ).
90- ToSql ()
91- if err != nil {
92- return nil , err
93- }
94-
95- if err := d .db .QueryRowContext (ctx , query , args ... ).Scan (& storage .ID , & storage .Name , & storage .Type , & storage .Config ); err != nil {
80+ if err := d .db .QueryRowContext (ctx , stmt , args ... ).Scan (
81+ & storage .ID ,
82+ & storage .Name ,
83+ & storage .Type ,
84+ & storage .Config ,
85+ ); err != nil {
9686 return nil , err
9787 }
9888
9989 return storage , nil
10090}
10191
10292func (d * DB ) DeleteStorage (ctx context.Context , delete * store.DeleteStorage ) error {
103- qb := squirrel .Delete ("storage" ).Where (squirrel.Eq {"id" : delete .ID })
104-
105- query , args , err := qb .PlaceholderFormat (squirrel .Dollar ).ToSql ()
106- if err != nil {
107- return err
108- }
109-
110- result , err := d .db .ExecContext (ctx , query , args ... )
93+ stmt := `DELETE FROM storage WHERE id = $1`
94+ result , err := d .db .ExecContext (ctx , stmt , delete .ID )
11195 if err != nil {
11296 return err
11397 }
114-
11598 if _ , err := result .RowsAffected (); err != nil {
11699 return err
117100 }
118-
119101 return nil
120102}
0 commit comments