existsTable method

Future<bool> existsTable(
  1. MySQLConnection conn
)

Checks if the table exists in the MySQL database.

This method queries the MySQL information schema to determine if a table with the current name exists in the database.

Parameters:

  • conn - The active MySQL database connection

Returns true if the table exists, false otherwise.

Example:

if (await table.existsTable(conn)) {
  print('Table already exists');
} else {
  await table.createTable(conn);
}

Implementation

Future<bool> existsTable(MySQLConnection conn) async {
  Sqler sqler = Sqler()
    ..from(QField('information_schema.tables'))
    ..selects([
      QSelect('table_name'),
      SQL.count(QField('table_name', as: 'count')),
    ])
    ..where(WhereOne(
      QField('table_name'),
      QO.EQ,
      QVar(name),
    ));
  var result = await execute(conn, sqler.toSQL());
  var count = result.rows.first.colByName('count')?.toInt(def: 0);
  return (count ?? 0) > 0;
}