@@ -20,12 +20,12 @@ import (
2020 "context"
2121 "database/sql"
2222 "errors"
23- "github.com/iancoleman/orderedmap"
2423 "strings"
2524 "time"
2625
26+ "github.com/iancoleman/orderedmap"
27+
2728 "github.com/gofiber/fiber/v2"
28- "github.com/proofrock/crypgo"
2929)
3030
3131// Catches the panics and converts the argument in a struct that Fiber uses to
@@ -50,50 +50,6 @@ func errHandler(c *fiber.Ctx, err error) error {
5050 return c .Status (ret .Code ).JSON (ret )
5151}
5252
53- // Scans the values for a db request and encrypts them as needed
54- func encrypt (encoder requestItemCrypto , values map [string ]interface {}) error {
55- for i := range encoder .Fields {
56- sval , ok := values [encoder.Fields [i ]].(string )
57- if ! ok {
58- return errors .New ("attempting to encrypt a non-string field" )
59- }
60- var eval string
61- var err error
62- if encoder .CompressionLevel < 1 {
63- eval , err = crypgo .Encrypt (encoder .Password , sval )
64- } else if encoder .CompressionLevel < 20 {
65- eval , err = crypgo .CompressAndEncrypt (encoder .Password , sval , encoder .CompressionLevel )
66- } else {
67- return errors .New ("compression level is in the range 0-19" )
68- }
69- if err != nil {
70- return err
71- }
72- values [encoder.Fields [i ]] = eval
73- }
74- return nil
75- }
76-
77- // Scans the results from a db request and decrypts them as needed
78- func decrypt (decoder requestItemCrypto , results * orderedmap.OrderedMap ) error {
79- if decoder .CompressionLevel > 0 {
80- return errors .New ("cannot specify compression level for decryption" )
81- }
82- for i := range decoder .Fields {
83- // sval, ok := results[decoder.Fields[i]].(string)
84- sval , ok := results .Get (decoder .Fields [i ])
85- if ! ok {
86- return errors .New ("attempting to decrypt a non-string field" )
87- }
88- dval , err := crypgo .Decrypt (decoder .Password , sval .(string ))
89- if err != nil {
90- return err
91- }
92- results .Set (decoder .Fields [i ], dval )
93- }
94- return nil
95- }
96-
9753// For a single query item, deals with a failure, determining if it must invalidate all of the transaction
9854// or just report an error in the single query. In the former case, fails fast (panics), else it appends
9955// the error to the response items, so the caller needs to return7continue
@@ -107,7 +63,7 @@ func reportError(err error, code int, reqIdx int, noFail bool, results []respons
10763// Processes a query, and returns a suitable responseItem
10864//
10965// This method is needed to execute properly the defers.
110- func processWithResultSet (tx * sql.Tx , query string , decoder * requestItemCrypto , values map [string ]interface {}) (* responseItem , error ) {
66+ func processWithResultSet (tx * sql.Tx , query string , values map [string ]interface {}) (* responseItem , error ) {
11167 resultSet := make ([]orderedmap.OrderedMap , 0 )
11268
11369 rows , err := tx .Query (query , vals2nameds (values )... )
@@ -132,11 +88,6 @@ func processWithResultSet(tx *sql.Tx, query string, decoder *requestItemCrypto,
13288 toAdd .Set (fields [i ], values [i ])
13389 }
13490
135- if decoder != nil {
136- if err := decrypt (* decoder , toAdd ); err != nil {
137- return nil , err
138- }
139- }
14091 resultSet = append (resultSet , * toAdd )
14192 }
14293
@@ -264,16 +215,6 @@ func handler(databaseId string) func(c *fiber.Ctx) error {
264215
265216 hasResultSet := txItem .Query != ""
266217
267- if hasResultSet && txItem .Encoder != nil {
268- reportError (errors .New ("cannot specify an encoder for a query" ), fiber .StatusBadRequest , i , txItem .NoFail , ret .Results )
269- continue
270- }
271-
272- if ! hasResultSet && txItem .Decoder != nil {
273- reportError (errors .New ("cannot specify a decoder for a statement" ), fiber .StatusBadRequest , i , txItem .NoFail , ret .Results )
274- continue
275- }
276-
277218 if len (txItem .Values ) != 0 && len (txItem .ValuesBatch ) != 0 {
278219 reportError (errors .New ("cannot specify both values and valuesBatch" ), fiber .StatusBadRequest , i , txItem .NoFail , ret .Results )
279220 continue
@@ -323,13 +264,6 @@ func handler(databaseId string) func(c *fiber.Ctx) error {
323264 continue
324265 }
325266
326- if txItem .Encoder != nil {
327- if err := encrypt (* txItem .Encoder , values ); err != nil {
328- reportError (err , fiber .StatusInternalServerError , i , txItem .NoFail , ret .Results )
329- continue
330- }
331- }
332-
333267 valuesBatch = append (valuesBatch , values )
334268 }
335269
@@ -348,17 +282,10 @@ func handler(databaseId string) func(c *fiber.Ctx) error {
348282 continue
349283 }
350284
351- if txItem .Encoder != nil {
352- if err := encrypt (* txItem .Encoder , values ); err != nil {
353- reportError (err , fiber .StatusInternalServerError , i , txItem .NoFail , ret .Results )
354- continue
355- }
356- }
357-
358285 if hasResultSet {
359286 // Query
360287 // Externalized in a func so that defer rows.Close() actually runs
361- retWR , err := processWithResultSet (tx , sqll , txItem . Decoder , values )
288+ retWR , err := processWithResultSet (tx , sqll , values )
362289 if err != nil {
363290 reportError (err , fiber .StatusInternalServerError , i , txItem .NoFail , ret .Results )
364291 continue
0 commit comments