asDeleteTopCommand method

String asDeleteTopCommand(
  1. String tableName,
  2. int count,
  3. String idColumn,
  4. bool asc,
  5. String dataBaseProvider, [
  6. bool nullAsBlank = false,
  7. dynamic quoteChar,
])

Generates a DELETE command for deleting the top count rows from the given table.

The tableName parameter specifies the name of the table to delete from. The count parameter specifies the number of rows to delete. The idColumn parameter specifies the name of the column used for ordering. The asc parameter determines whether to delete rows in ascending order. The dataBaseProvider parameter specifies the database provider. The nullAsBlank parameter determines whether null values should be treated as blank strings. The quoteChar parameter specifies the character used for quoting identifiers.

Returns the generated DELETE command as a string.

Implementation

String asDeleteTopCommand(
  String tableName,
  int count,
  String idColumn,
  bool asc,
  String dataBaseProvider, [
  bool nullAsBlank = false,
  dynamic quoteChar,
]) {
  String whereClause = asWhereClausule(
    nullAsBlank: nullAsBlank,
    quoteChar: quoteChar,
    dataBaseProvider: dataBaseProvider,
  );

  return """DELETE FROM ${SqlUtil.wrap(tableName, quoteChar, dataBaseProvider)} WHERE $idColumn in (
              SELECT ${SqlUtil.isSqlServer(dataBaseProvider) ? "TOP($count)" : ""} $idColumn
              FROM ${SqlUtil.wrap(tableName, quoteChar, dataBaseProvider)}
              WHERE $whereClause
              ORDER BY $idColumn ${asc ? "ASC" : "DESC"} ${SqlUtil.isMySql(dataBaseProvider) ? "LIMIT $count" : ""}
            );""";
}