-
Notifications
You must be signed in to change notification settings - Fork 434
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added db util support to account and alert
- Loading branch information
1 parent
38d4a02
commit 98c966e
Showing
5 changed files
with
195 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package models | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
|
||
"github.com/uptrace/bun" | ||
) | ||
|
||
type QueryType string | ||
|
||
const ( | ||
RAW QueryType = "RAW" | ||
SELECT QueryType = "SELECT" | ||
INSERT QueryType = "INSERT" | ||
DELETE QueryType = "DELETE" | ||
UPDATE QueryType = "UPDATE" | ||
) | ||
|
||
type DbHandler struct { | ||
Db *bun.DB | ||
} | ||
|
||
func NewDbHandler(db *bun.DB) DbHandler { | ||
return DbHandler{ | ||
Db: db, | ||
} | ||
} | ||
|
||
type Object struct { | ||
Query string `json:"query"` | ||
Type QueryType `json:"type"` | ||
Params []string `json:"params"` | ||
} | ||
|
||
type Data map[string]Object | ||
|
||
func HandleQuery(db *bun.DB, ctx context.Context, queryTitle string, schema interface{}, additionals map[string]string) (sql.Result, error) { | ||
var resp sql.Result | ||
var err error | ||
switch Queries[queryTitle].Type { | ||
case RAW: | ||
err = executeRaw(db, ctx, Queries[queryTitle].Query, schema, additionals) | ||
if err != nil { | ||
return resp, err | ||
} | ||
case SELECT: | ||
err = executeSelect(db, ctx, Queries[queryTitle].Query, schema, additionals) | ||
if err != nil { | ||
return resp, err | ||
} | ||
case INSERT: | ||
resp, err = executeInsert(db, ctx, schema, additionals) | ||
if err != nil { | ||
return resp, err | ||
} | ||
case DELETE: | ||
resp, err = executeDelete(db, ctx, schema, Queries[queryTitle].Query, additionals) | ||
if err != nil { | ||
return resp, err | ||
} | ||
case UPDATE: | ||
resp, err = executeUpdate(db, ctx, schema, Queries[queryTitle].Query, Queries[queryTitle].Params, additionals) | ||
if err != nil { | ||
return resp, err | ||
} | ||
} | ||
return resp, nil | ||
} | ||
|
||
func executeRaw(db *bun.DB, ctx context.Context, query string, schema interface{}, additionals map[string]string) error { | ||
if len(additionals) > 0 { | ||
query = fmt.Sprintf("%s where", query) | ||
} | ||
|
||
for key, value := range additionals { | ||
query = fmt.Sprintf("%s %s = '%s' and", query, key, value) | ||
} | ||
|
||
if len(additionals) > 0 { | ||
query = query[:len(query)-4] | ||
} | ||
|
||
err := db.NewRaw(query).Scan(ctx, schema) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func executeSelect(db *bun.DB, ctx context.Context, query string, schema interface{}, additionals map[string]string) error { | ||
updatedQuery := db.NewSelect().Model(schema) | ||
for key, value := range additionals { | ||
updatedQuery = updatedQuery.Where(fmt.Sprintf("%s = ?", key), value) | ||
} | ||
|
||
err := updatedQuery.Scan(ctx, schema) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func executeInsert(db *bun.DB, ctx context.Context, schema interface{}, additionals map[string]string) (sql.Result, error) { | ||
resp, err := db.NewInsert().Model(schema).Exec(ctx) | ||
if err != nil { | ||
return resp, err | ||
} | ||
return resp, nil | ||
} | ||
|
||
func executeDelete(db *bun.DB, ctx context.Context, schema interface{}, query string, additionals map[string]string) (sql.Result, error) { | ||
resp, err := db.NewDelete().Model(schema).Where("id = ?", additionals["id"]).Exec(ctx) | ||
if err != nil { | ||
return resp, err | ||
} | ||
return resp, nil | ||
} | ||
|
||
func executeUpdate(db *bun.DB, ctx context.Context, schema interface{}, query string, columns []string, additionals map[string]string) (sql.Result, error) { | ||
updatedQuery := db.NewUpdate().Model(schema).Column(columns...) | ||
|
||
for key, value := range additionals { | ||
updatedQuery = updatedQuery.Where(fmt.Sprintf("%s = ?", key), value) | ||
} | ||
|
||
updatedQuery = updatedQuery.Returning("*") | ||
resp, err := updatedQuery.Exec(ctx) | ||
if err != nil { | ||
return resp, err | ||
} | ||
return resp, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package models | ||
|
||
var Queries = Data{ | ||
"LIST": Object{ | ||
Type: SELECT, | ||
}, | ||
"INSERT": Object{ | ||
Type: INSERT, | ||
}, | ||
"DELETE": Object{ | ||
Type: DELETE, | ||
}, | ||
"UPDATE_ACCOUNT": Object{ | ||
Type: UPDATE, | ||
Params: []string{"name", "provider", "credentials"}, | ||
}, | ||
"UPDATE_ALERT": Object{ | ||
Type: UPDATE, | ||
Params: []string{"name", "type", "budget", "usage", "endpoint", "secret"}, | ||
}, | ||
"RE_SCAN_ACCOUNT": Object{ | ||
Type: UPDATE, | ||
Params: []string{"status"}, | ||
}, | ||
"RESOURCE_COUNT": Object{ | ||
Query: "SELECT COUNT(*) as total FROM resources", | ||
Type: RAW, | ||
}, | ||
} |