Skip to content

Provide DROP TABLE IF EXISTS wrapper #1191

@vadz

Description

@vadz

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions