Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query builder #182

Open
fmoor opened this issue Oct 21, 2021 · 7 comments
Open

Query builder #182

fmoor opened this issue Oct 21, 2021 · 7 comments
Assignees

Comments

@fmoor
Copy link
Member

fmoor commented Oct 21, 2021

Something better than string concatenation for dynamic query construction.

@fmoor fmoor self-assigned this Oct 21, 2021
@diogox
Copy link

diogox commented Jan 30, 2022

Hi 👋

Is the string concatenation approach safe? I would assume "sql injection"-like attacks are possible?

@elprans
Copy link
Member

elprans commented Jan 30, 2022

Is the string concatenation approach safe?

In general, it is not. Like with SQL, interpolating unsanitized user input into EdgeQL queries might lead to "EdgeQL injection". Thus, you must always pass input as query arguments.

That said, composing queries from non-arbitrary string pieces is just fine, e.g:

        type Result struct {
                Title   string `edgedb:"title"`
        }
        var results []Result

        query := "SELECT Article { title }"
        args := make([]interface{}, 0)

        if pattern != "" {
            query = query + " FILTER title LIKE <str>$0"
            args = append(args, pattern)
        }

        err = client.Query(ctx, query, &results, args...)

Naturally, this isn't the most ergonomic approach, so we are looking to build generated clients to compose queries in a type-safe manner.

@diogox
Copy link

diogox commented Jan 30, 2022

Ah, alright. Weird though. I tried using the arguments in insertions and it didn't work:

insert users::User {
  email := $0,
  username := $1,
  hashedPassword := $2,
  type := $3
}

Using this in a Query call yields the edgedb.QueryError: missing a type cast before the parameter error.

Not even possible to use it in Execute, because it doesn't take arguments.

Which is annoying, because Query forces you to pass in a pointer to a slice to read the result, even if you don't want the result.

@elprans
Copy link
Member

elprans commented Jan 30, 2022

missing a type cast before the parameter means that you must explicitly specify the types of query arguments with a cast:

insert users::User {
  email := <str>$0,
  username := <str>$1,
  hashedPassword := <str>$2,
  type := <str>$3
}

@diogox
Copy link

diogox commented Jan 30, 2022

Cool, thanks for letting me know!

A better API, or something like sqlc, would be amazing.

Will be following the project, edgedb looks great! :)

@paulm17
Copy link

paulm17 commented Jun 6, 2022

@diogox thanks for creating this issue. Due to the lack of documentation I also followed the string concatenation method from looking at the test files and I also came across the issue that Execute does not take any arguments either.

Still I solved my issues by following the advice from @elprans.

Definitely looking forward to the query builder, when it becomes available. 👍

@endigma
Copy link

endigma commented Jul 29, 2022

As opposed to sqlc, perhaps code-generating the SDK similar to entgo.io would be possible? ent is in my opinion the strongest golang ORM purely based on the fully typed CRUD and hooks and etc. Not all of this would be possible given edgeDB is not entirely the same concept, but following in the footsteps of ent and creating a codegenned-SDK from the source schema instead of from documents like sqlc would be a big difference in my opinion.

fmoor added a commit that referenced this issue Oct 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants