question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request: generated bind method for model objects on prepared statements

See original GitHub issue

If I have a parametric statement that only consists of fields from one object, it would be nice if SqlDelight generated a bind overload that took a MyTableModel as a parameter to populate the fields. Example:

CREATE TABLE someTable (
    _id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    someString TEXT,
    someInteger INTEGER
);

insertThing:
INSERT INTO someTable (someString, someInteger) VALUES (?, ?);

Could generate a SomeTableModel.InsertThing class with a method like:

public void bind(@NonNull SomeTableModel model) {
    bind(model.someString(), model.someInteger());
}

This would be helpful for cases where you have a lot of parameters where you really just want to insert all the fields from a model impl. Currently this becomes really verbose and potentially error-prone if you have a lot of fields to insert.

The main thing for me at least would be INSERTs and UPDATEs. You might consider applying it to any type of SQL query (e.g. a SELECT from 2 tabes might take two parameters to supply the parameters for fields from the respective table). Of course this is only useful if you assume that every parameter should be mapped to its corresponding field - maybe this is questionable for SELECTs. Even for UPDATEs, it’s questionable if it should correspond to fields in a WHERE clause, so perhaps that should be separate parameters, etc.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:7
  • Comments:9

github_iconTop GitHub Comments

4reactions
dimsuzcommented, Jan 19, 2017

I was just looking for the same thing as the OP and was surprised I didn’t find it in generated code.

I understand all the arguments, but my usecase is the following: I have a table with 24 columns and currently when inserting new objects (I use insert or replace) I have to manually destructure a model to pass all those properties to a bind method.

This is surely doable, but it’s a bit error prone, I could accidentally swap some column names (during some refactoring for example).

It would be great if code generation would help me to stay on the safe side in this case 😃

1reaction
Philipp91commented, Apr 12, 2017

I’d prefer the syntax suggested by @AlecStrong over here #505, since it’s not really a tuple that you’re inserting:

INSERT INTO users VALUES ?;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Feature request: generated bind method for model objects on ...
I see the generated model interface as a description of how to acquire values for SQL table fields on any arbitrary object. It...
Read more >
Using Prepared Statements - JDBC Basics - Oracle Help Center
This JDBC Java tutorial describes how to use JDBC API to create, insert into, update, and query tables. You will also learn how...
Read more >
Model Binding in ASP.NET Core | Microsoft Learn
Retrieves data from various sources such as route data, form fields, and query strings. Provides the data to controllers and Razor pages in ......
Read more >
PHP MySQL Prepared Statements - W3Schools
$stmt->bind_param("sss", $firstname, $lastname, $email); This function binds the parameters to the SQL query and tells the database what the parameters are. ...
Read more >
Prepared Statements - Manual - PHP
Prepare is followed by execute. During execute the client binds parameter values and sends them to the server. The server executes the statement...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found