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

Provide DROP TABLE IF EXISTS wrapper #1191

Open
vadz opened this issue Jan 13, 2025 · 0 comments
Open

Provide DROP TABLE IF EXISTS wrapper #1191

vadz opened this issue Jan 13, 2025 · 0 comments

Comments

@vadz
Copy link
Member

vadz commented Jan 13, 2025

It would be nice to have a way to drop a table which may not exist other than

    try {
        sql << "drop table soci_test";
    }
    catch (...) {}

which is used now in SOCI's own tests. Of course, most databases support DROP TABLE IF EXISTS nowadays, but some of them don't and I wonder if we can provide workarounds for them.

  1. DB2: no if exists, can do something like SELECT * FROM information_schema.tables WHERE TABLE_SCHEMA = 'MY_SCHEMA' AND TABLE_NAME = 'MY_TABLE'; according to this SO answer.
  2. Firebird: doesn't seem to have support for if exists, does have RECREATE TABLE.
  3. MySQL: supports if exists.
  4. Oracle: only supports if exists since 23c, this SO answer shows how to ignore exception from DROP TABLE.
  5. PostgreSQL: supports if exists.
  6. SQLite: supports if exists.
  7. SQL Server (via ODBC): can use select object_id('table_name','u') to check if it exists.

Thinking more about this, maybe what we really want is a way to execute an SQL statement without throwing an exception on error, so that we could use it to ignore the error from drop table without throwing (and catching).

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

1 participant