- Notifications
You must be signed in to change notification settings - Fork927
WIP: Using joins to reduce DB round trips#6356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Closed
Uh oh!
There was an error while loading.Please reload this page.
Closed
Changes fromall commits
Commits
Show all changes
12 commits Select commitHold shift + click to select a range
25556f0
chore: Join together tables to prevent excessive queries
Emyrkc5c66dd
WIP: Moving all workspace_data into the same sql function
Emyrke51702e
WIP on SQL
Emyrkfee6bb3
WIP: CompileToSQL allow using namespace/prefix
Emyrkb252b79
This technically works, but really sucks
Emyrk944755e
Comments
Emyrk7b7d98d
Clean up
Emyrkae47e03
Drop GetWorkspaces, should only use GetAuthorizedWorkspaces
Emyrk3ca3b12
WIP
Emyrk472c8ca
Merge remote-tracking branch 'origin/main' into stevenmasley/dbauthz_…
Emyrk129fe01
Remove sqlc queries
Emyrk6adacd7
Reuse GetWorkspaces
EmyrkFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
5 changes: 2 additions & 3 deletionscoderd/autobuild/executor/lifecycle_executor.go
This file contains hidden or 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
80 changes: 80 additions & 0 deletionscoderd/database/bindvars.go
This file contains hidden or 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,80 @@ | ||
package database | ||
import ( | ||
"database/sql/driver" | ||
"fmt" | ||
"reflect" | ||
"regexp" | ||
"strings" | ||
"github.com/google/uuid" | ||
"github.com/lib/pq" | ||
"github.com/jmoiron/sqlx/reflectx" | ||
"github.com/coder/coder/coderd/util/slice" | ||
) | ||
var nameRegex = regexp.MustCompile(`@([a-zA-Z0-9_]+)`) | ||
// dbmapper grabs struct 'db' tags. | ||
var dbmapper = reflectx.NewMapper("db") | ||
var sqlValuer = reflect.TypeOf((*driver.Valuer)(nil)).Elem() | ||
// bindNamed is an implementation that improves on the SQLx implementation. This | ||
// adjusts the query to use "$#" syntax for arguments instead of "@argument". The | ||
// returned args are the values of the struct fields that match the names in the | ||
// correct order and indexing. | ||
// | ||
// 1. SQLx does not reuse arguments, so "@arg, @arg" will result in two arguments | ||
// "$1, $2" instead of "$1, $1". | ||
// 2. SQLx does not handle uuid arrays. | ||
// 3. SQLx only supports ":name" style arguments and breaks "::" type casting. | ||
func bindNamed(query string, arg interface{}) (newQuery string, args []interface{}, err error) { | ||
// We do not need to implement a sql parser to extract and replace the variable names. | ||
// All names follow a simple regex. | ||
names := nameRegex.FindAllString(query, -1) | ||
// Get all unique names | ||
names = slice.Unique(names) | ||
// Replace all names with the correct index | ||
for i, name := range names { | ||
rpl := fmt.Sprintf("$%d", i+1) | ||
query = strings.ReplaceAll(query, name, rpl) | ||
// Remove the "@" prefix to match to the "db" struct tag. | ||
names[i] = strings.TrimPrefix(name, "@") | ||
} | ||
arglist := make([]interface{}, 0, len(names)) | ||
// This comes straight from SQLx's implementation to get the values | ||
// of the struct fields. | ||
v := reflect.ValueOf(arg) | ||
for v = reflect.ValueOf(arg); v.Kind() == reflect.Ptr; { | ||
v = v.Elem() | ||
} | ||
err = dbmapper.TraversalsByNameFunc(v.Type(), names, func(i int, t []int) error { | ||
if len(t) == 0 { | ||
return fmt.Errorf("could not find name %s in %#v", names[i], arg) | ||
} | ||
val := reflectx.FieldByIndexesReadOnly(v, t) | ||
// Handle some custom types to make arguments easier to use. | ||
switch val.Interface().(type) { | ||
// Feel free to add more types here as needed. | ||
case []uuid.UUID: | ||
arglist = append(arglist, pq.Array(val.Interface())) | ||
default: | ||
arglist = append(arglist, val.Interface()) | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
return query, arglist, nil | ||
} |
10 changes: 5 additions & 5 deletionscoderd/database/dbauthz/querier.go
This file contains hidden or 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
132 changes: 68 additions & 64 deletionscoderd/database/dbfake/databasefake.go
This file contains hidden or 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
25 changes: 4 additions & 21 deletionscoderd/database/modelmethods.go
This file contains hidden or 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
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.