• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codenotary / immudb / 6850968753

13 Nov 2023 01:58PM UTC coverage: 89.235% (-0.3%) from 89.556%
6850968753

push

gh-ci

jeroiraz
feat(embedded/sql): show table stmt

Signed-off-by: Jeronimo Irazabal <jeronimo.irazabal@gmail.com>

82 of 169 new or added lines in 2 files covered. (48.52%)

643 existing lines in 10 files now uncovered.

33838 of 37920 relevant lines covered (89.24%)

144254.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.87
/pkg/server/multidb_handler.go
1
/*
2
Copyright 2022 Codenotary Inc. All rights reserved.
3

4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7

8
        http://www.apache.org/licenses/LICENSE-2.0
9

10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16

17
package server
18

19
import (
20
        "context"
21
        "fmt"
22

23
        "github.com/codenotary/immudb/embedded/sql"
24
        "github.com/codenotary/immudb/pkg/api/schema"
25
        "github.com/codenotary/immudb/pkg/auth"
26
)
27

28
type multidbHandler struct {
29
        s *ImmuServer
30
}
31

32
func (s *ImmuServer) multidbHandler() sql.MultiDBHandler {
589✔
33
        return &multidbHandler{s}
589✔
34
}
589✔
35

36
func (h *multidbHandler) UseDatabase(ctx context.Context, db string) error {
2✔
37
        if auth.GetAuthTypeFromContext(ctx) != auth.SessionAuth {
2✔
38
                return fmt.Errorf("%w: database selection from SQL statements requires session based authentication", ErrNotSupported)
×
39
        }
×
40

41
        _, err := h.s.UseDatabase(ctx, &schema.Database{DatabaseName: db})
2✔
42
        return err
2✔
43
}
44

45
func (h *multidbHandler) CreateDatabase(ctx context.Context, db string, ifNotExists bool) error {
9✔
46
        _, err := h.s.CreateDatabaseV2(ctx, &schema.CreateDatabaseRequest{
9✔
47
                Name:        db,
9✔
48
                IfNotExists: ifNotExists,
9✔
49
        })
9✔
50
        return err
9✔
51
}
9✔
52

53
func (h *multidbHandler) ListDatabases(ctx context.Context) ([]string, error) {
1✔
54
        res, err := h.s.DatabaseList(ctx, nil)
1✔
55
        if err != nil {
1✔
56
                return nil, err
×
57
        }
×
58

59
        dbs := make([]string, len(res.Databases))
1✔
60

1✔
61
        for i, db := range res.Databases {
3✔
62
                dbs[i] = db.DatabaseName
2✔
63
        }
2✔
64

65
        return dbs, nil
1✔
66
}
67

68
func (h *multidbHandler) ListUsers(ctx context.Context) ([]sql.User, error) {
5✔
69
        db, err := h.s.getDBFromCtx(ctx, "ListUsers")
5✔
70
        if err != nil {
5✔
UNCOV
71
                return nil, err
×
UNCOV
72
        }
×
73

74
        res, err := h.s.ListUsers(ctx, nil)
5✔
75
        if err != nil {
5✔
UNCOV
76
                return nil, err
×
77
        }
×
78

79
        users := make([]sql.User, 0, len(res.Users))
5✔
80

5✔
81
        for _, user := range res.Users {
22✔
82
                if !user.Active {
19✔
83
                        continue
2✔
84
                }
85

86
                var hasPermission *schema.Permission
15✔
87

15✔
88
                if string(user.User) == auth.SysAdminUsername {
20✔
89
                        hasPermission = &schema.Permission{Database: db.GetName()}
5✔
90
                } else {
15✔
91
                        for _, perm := range user.Permissions {
20✔
92
                                if perm.Database == db.GetName() {
19✔
93
                                        hasPermission = perm
9✔
94
                                        break
9✔
95
                                }
96
                        }
97
                }
98

99
                if hasPermission != nil {
29✔
100
                        users = append(users, &User{username: string(user.User), perm: hasPermission.Permission})
14✔
101
                }
14✔
102
        }
103

104
        return users, nil
5✔
105
}
106

107
type User struct {
108
        username string
109
        perm     uint32
110
}
111

112
func (usr *User) Username() string {
14✔
113
        return usr.username
14✔
114
}
14✔
115

116
func (usr *User) Permission() uint32 {
14✔
117
        return usr.perm
14✔
118
}
14✔
119

120
func permCode(permission sql.Permission) uint32 {
5✔
121
        switch permission {
5✔
122
        case sql.PermissionReadOnly:
2✔
123
                {
4✔
124
                        return 1
2✔
125
                }
2✔
126
        case sql.PermissionReadWrite:
2✔
127
                {
4✔
128
                        return 2
2✔
129
                }
2✔
130
        case sql.PermissionAdmin:
1✔
131
                {
2✔
132
                        return 254
1✔
133
                }
1✔
134
        }
UNCOV
135
        return 0
×
136
}
137

138
func (h *multidbHandler) CreateUser(ctx context.Context, username, password string, permission sql.Permission) error {
4✔
139
        db, err := h.s.getDBFromCtx(ctx, "CreateUser")
4✔
140
        if err != nil {
4✔
UNCOV
141
                return err
×
UNCOV
142
        }
×
143

144
        _, err = h.s.CreateUser(ctx, &schema.CreateUserRequest{
4✔
145
                User:       []byte(username),
4✔
146
                Password:   []byte(password),
4✔
147
                Database:   db.GetName(),
4✔
148
                Permission: permCode(permission),
4✔
149
        })
4✔
150

4✔
151
        return err
4✔
152
}
153

154
func (h *multidbHandler) AlterUser(ctx context.Context, username, password string, permission sql.Permission) error {
2✔
155
        _, user, err := h.s.getLoggedInUserdataFromCtx(ctx)
2✔
156
        if err != nil {
2✔
UNCOV
157
                return err
×
UNCOV
158
        }
×
159

160
        db, err := h.s.getDBFromCtx(ctx, "ChangePassword")
2✔
161
        if err != nil {
2✔
UNCOV
162
                return err
×
UNCOV
163
        }
×
164

165
        _, err = h.s.SetActiveUser(ctx, &schema.SetActiveUserRequest{
2✔
166
                Username: username,
2✔
167
                Active:   true,
2✔
168
        })
2✔
169
        if err != nil {
3✔
170
                return err
1✔
171
        }
1✔
172

173
        _, err = h.s.ChangePassword(ctx, &schema.ChangePasswordRequest{
1✔
174
                User:        []byte(username),
1✔
175
                OldPassword: []byte(user.HashedPassword),
1✔
176
                NewPassword: []byte(password),
1✔
177
        })
1✔
178
        if err != nil {
1✔
UNCOV
179
                return err
×
UNCOV
180
        }
×
181

182
        for _, perm := range user.Permissions {
2✔
183
                if perm.Database == db.GetName() {
1✔
UNCOV
184
                        _, err := h.s.ChangePermission(ctx, &schema.ChangePermissionRequest{
×
UNCOV
185
                                Username:   username,
×
UNCOV
186
                                Database:   perm.Database,
×
UNCOV
187
                                Action:     schema.PermissionAction_REVOKE,
×
UNCOV
188
                                Permission: perm.Permission,
×
UNCOV
189
                        })
×
UNCOV
190
                        if err != nil {
×
UNCOV
191
                                return err
×
UNCOV
192
                        }
×
193
                }
194
        }
195

196
        _, err = h.s.ChangePermission(ctx, &schema.ChangePermissionRequest{
1✔
197
                Username:   username,
1✔
198
                Database:   db.GetName(),
1✔
199
                Action:     schema.PermissionAction_GRANT,
1✔
200
                Permission: permCode(permission),
1✔
201
        })
1✔
202

1✔
203
        return err
1✔
204
}
205

206
func (h *multidbHandler) DropUser(ctx context.Context, username string) error {
1✔
207
        _, err := h.s.SetActiveUser(ctx, &schema.SetActiveUserRequest{
1✔
208
                Username: username,
1✔
209
                Active:   false,
1✔
210
        })
1✔
211
        return err
1✔
212
}
1✔
213

214
func (h *multidbHandler) ExecPreparedStmts(
215
        ctx context.Context,
216
        opts *sql.TxOptions,
217
        stmts []sql.SQLStmt,
218
        params map[string]interface{},
219
) (ntx *sql.SQLTx, committedTxs []*sql.SQLTx, err error) {
1✔
220

1✔
221
        db, err := h.s.getDBFromCtx(ctx, "SQLExec")
1✔
222
        if err != nil {
1✔
UNCOV
223
                return nil, nil, err
×
UNCOV
224
        }
×
225

226
        tx, err := db.NewSQLTx(ctx, opts)
1✔
227
        if err != nil {
1✔
UNCOV
228
                return nil, nil, err
×
UNCOV
229
        }
×
230

231
        return db.SQLExecPrepared(ctx, tx, stmts, params)
1✔
232
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc