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

Support for upsert #1436

Closed
NilsLattek opened this issue Aug 6, 2019 · 19 comments · Fixed by #1582
Closed

Support for upsert #1436

NilsLattek opened this issue Aug 6, 2019 · 19 comments · Fixed by #1582
Labels
component: compiler component: sql-psi Should be implemented in https://github.com/AlecStrong/sql-psi feature

Comments

@NilsLattek
Copy link

It would be cool if SQLDelight would support the upsert command: https://www.sqlite.org/lang_UPSERT.html

Right now the following sql:

upsert:
INSERT INTO stop (id, address_id, name)
VALUES (?, ?, ?) ON CONFLICT(address_id) DO UPDATE SET name=excluded.name;

will produce this error:

',' expected, got 'ON'
62    INSERT INTO stop (id, address_id, name)
63    VALUES (?, ?, ?) ON CONFLICT(address_id) DO
                       ^^
@AlecKazakova
Copy link
Collaborator

yea definitely, my hope is that we can allow specifying the minimum version of sqlite you'll be using which would enable newer language features

@eygraber
Copy link
Contributor

eygraber commented Oct 4, 2019

@AlecStrong @JakeWharton is providing a mechanism to allow specifying the minimum version of sqlite what needs to be built, or does support for the newer language features (e.g. upsert) need to be built as well?

For the former, if you can point me in the general direction of what needs to be done, I can take a stab at it.

For the latter, I'd gladly take a stab at it when I have some free time (which I'm sure is your current position on the matter as well).

@JakeWharton
Copy link
Member

JakeWharton commented Nov 25, 2019 via email

@vincent-paing
Copy link

I'm on 1.3.0 and it's still is showing error at ON

image

@angusholder
Copy link
Contributor

Did you set dialect = "sqlite:3.24" in the Gradle SQLDelight config?

@kkovach
Copy link

kkovach commented May 5, 2020

I have a couple questions concerning this or something similar to this. I've added the dialect line to my config. I'm able to compile, but am getting an "android.database.sqlite.SQLiteException: near "ON": syntax error (code 1 SQLITE_ERROR)" error at runtime. My table and insert statements are as follows...

CREATE TABLE user (
    userId INTEGER NOT NULL UNIQUE,
    name TEXT,
    address TEXT,
    city TEXT,
    state TEXT,
    zip TEXT,
    email TEXT,
    phone TEXT,
    url TEXT,
    isDefault INTEGER as Boolean DEFAULT 0
);

insertUser:
INSERT INTO user(userId, name, address, city, state, zip, email, phone, url, isDefault)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(userId) DO
UPDATE SET name=excluded.name, email=excluded.email, address=excluded.address, city=excluded.city,
state=excluded.state, zip=excluded.zip, phone=excluded.phone, url=excluded.url;

Is there some other change or requirement that I missed? I tried reading everything but cannot seem to find what I might be missing?

Also, are you allowed to have multiple columns that can conflict? E.G. CONFLICT(id, userId)

Thanks!

@JakeWharton
Copy link
Member

JakeWharton commented May 5, 2020 via email

@kkovach
Copy link

kkovach commented May 5, 2020

Sorry, I had read the page you linked. I guess I was hoping that there was something that I was missing to get it working on Android. Are there any/many options for supplying your own SQLite for Android? I've been searching for a couple hours now and have nothing definitive. If you have a suggesting or link on doing so I would appreciate it. Thanks.

@eygraber
Copy link
Contributor

eygraber commented May 5, 2020

I've used Requery and SqlCipher, and they both work well with upsert.

@kihaki
Copy link

kihaki commented Jun 8, 2020

Did you set dialect = "sqlite:3.24" in the Gradle SQLDelight config?

Could you point me to where exactly this needs to go? I have tried:

sqldelight { dialect = "sqlite:3.8" }

Unfortunately this fails

Could not set unknown property 'dialect' for extension 'sqldelight' of type com.squareup.sqldelight.gradle.SqlDelightExtension.

I cannot find documentation on this.

@AlecKazakova
Copy link
Collaborator

It goes inside your database block:

sqldelight {
MyDatabase {
dialect = “sqlite:3.24:
}
}

@ursusursus
Copy link

ursusursus commented Feb 22, 2021

Btw if I use unbundled sqlite and set dialect to 3.25

sqldelight {
    AppDatabase {
        packageName = "sk.foo.bar"
        dialect = "sqlite:3.25"
    }
}

It does compile and work, however I still get red underlines in the IDE (and DO is not highlighted)

image

Does the intellij plugin need upgrading?

@AlecKazakova
Copy link
Collaborator

AlecKazakova commented Feb 22, 2021 via email

@ursusursus
Copy link

when exactly? after typing? IDE didn't complain it needs syncing, so unsure

@AlecKazakova
Copy link
Collaborator

AlecKazakova commented Feb 23, 2021 via email

@ursusursus
Copy link

Yea I just did, and still the same issue

Btw windows 10, AS 4.1.2, plugin 1.4.4

@Ditscheridou
Copy link

I Mean wouldnt it be a workaround here, that you do something like this:
upsert { UPDATE MyTable SET key_=:value, WHERE key_=:key; INSERT OR IGNORE INTO MyTable(key_) VALUES(:key); }
?

@tonisives
Copy link

This doesn't work for postgres dialect:
Screenshot 2022-02-08 at 14 29 29

id "com.squareup.sqldelight" version "1.5.3" apply false

@chardskarth
Copy link

Grouped statements arent supported for mysql dialect yet? I wonder where can we verify if its supported or not? 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: compiler component: sql-psi Should be implemented in https://github.com/AlecStrong/sql-psi feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.