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.

Drop the Marshal in favour of bindings

See original GitHub issue

Current idea of what binding support will look like:

CREATE TABLE customer (
  _id INTEGER PRIMARY KEY AUTOINCREMENT,
  first_name TEXT NOT NULL,
  last_name TEXT,
  is_cool INTEGER DEFAULT 1
);

customer_for_id:
SELECT *
FROM customer
WHERE _id = ?;

insert_customer:
INSERT INTO customer (first_name, last_name)
VALUES (?, ?);

generates:

interface Customer {

  // SQL strings and interface methods

  class CustomerFactory {
    CustomerFactory(Customer.Creator creator, <Adapters needed for Customer>);

    public RowMapper<Customer> customerForIdMapper();

    public String[] bindCustomerForid(int _id);

    public void bindInsertCustomer(SQLiteStatement statement, String first_name, String last_name);
  }
}

If a named sql statement in a .sq file is a SELECT, then a custom mapper is generated. If a named sql statement in a .sq file is an insert/update/delete and contains bind arguments, then a bind compiled statement method is generated. If a named sql statement in a .sq file is a SELECT statement and has bind args then a bind string array method is generated (see #73).

I like using compiled statements over the SQLiteDatabase.insert/delete/update apis because it keeps the arguments and the query itself in one object and is a lot more efficient. But the real benefit is it eliminates the runtime-ish nature of ContentValues. There’s no room for mistakes in terms of satisfying all of the table requirements. If in my example i changed is_cool to not have a default value then the statement wouldn’t compile anymore. Similarly if I added a new column to customer than insert_customer wouldn’t compile.

To maintain compatibility with the current stuff there could be an extra method generated to replicate the behavior of the copy constructor:

public void bindInsertCustomer(SQLiteStatement statement, Customer customer);

which would pull out the first_name and last_name fields and bind them.

Also the whole factory thing is just to save plugging in adaptors/creators all over the place.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
AlecStrongcommented, Jun 4, 2016

You could also use this for CREATE TABLE statements since there are cases where binding is necessary. Eg:

CREATE TABLE test (
  _id INTEGER PRIMARY KEY AUTOINCREMENT,
  date TEXT AS Date DEFAULT ?
);

generates

public void bindCreateTable(SQLiteStatement statement, Date date);

so you can provide default values for columns that require an adapter.

0reactions
AlecStrongcommented, Sep 24, 2016

Bind args will be in next release. I’m leaving the Marshal there too for now to make migrating easier but the plan is still to deprecate it soon.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Drop the Marshal in favour of bindings · Issue #311 · cashapp ...
I like using compiled statements over the SQLiteDatabase.insert/delete/update apis because it keeps the arguments and the query itself in one object and is...
Read more >
JAX-RS Data Bindings - Apache CXF
JAXB support. The request and response can be marshalled and unmarshalled to/from a Java object using JAXB. There are a number of ways...
Read more >
PART II RULES FOR COURTS–MARTIAL CHAPTER I ...
Rule 103. Definitions and rules of construction. The following definitions and rules of construc- tion apply throughout this Manual, unless otherwise.
Read more >
The Supreme Court's Overruling of Constitutional Precedent
No. Overruling Decision Date of Overruling Decision Date... 1 The Propeller Genesee Chief, 53 U.S. (12 How.) 443 1851 1837... 3 Kilbourn v. Thompson, 103...
Read more >
Opening Statement before the International Military Tribunal
It may be that these men of troubled conscience, whose only wish is that the world forget them, do not regard a trial...
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