generateSqlCall method

String generateSqlCall(
  1. String procedureName,
  2. String dataBaseProvider, [
  3. bool nullAsBlank = false,
  4. dynamic quoteChar,
])

Generates a SQL call string for a given stored procedure and database provider.

This function constructs a SQL call command by wrapping the procedure name with the appropriate quote character and appending the necessary syntax based on the database provider. It extends Map<String, dynamic> to include the parameters for the SQL call.

CAUTION: if yor dataBaseProvider is MySQL or MariaDB, the syntax will not include parameter names and the values will be concatenated in order

procedureName is the name of the stored procedure to be called. dataBaseProvider is a string that specifies the database provider. It supports 'mysql', 'mariadb', 'mssql', and 'sqlserver'. nullAsBlank is an optional boolean flag that, when set to true, will treat null values as blank strings in the SQL call. Defaults to false. quoteChar is an optional string that specifies the character to use for quoting SQL identifiers. If not provided, it defaults to the value of SqlUtil.defaultQuoteChar.

Throws an ArgumentError if the database provider is not recognized.

Returns a String containing the SQL call command.

Implementation

String generateSqlCall(String procedureName, String dataBaseProvider, [bool nullAsBlank = false, dynamic quoteChar]) {
  var sqlCall = '';

  bool isMySql = dataBaseProvider.flatEqualAny(["mysql", "mariadb"]);
  bool isSqlServer = dataBaseProvider.flatEqualAny(["mssql", "sqlserver"]);

  procedureName = SqlUtil.wrap(procedureName, quoteChar, dataBaseProvider);

  if (isMySql) {
    sqlCall += 'CALL $procedureName(';
  } else if (isSqlServer) {
    sqlCall += 'EXEC $procedureName ';
  } else {
    throw ArgumentError("Cannot identify database provider: $dataBaseProvider", "dataBaseProvider");
  }

  sqlCall += entries.map((e) {
    if (isMySql) {
      return SqlUtil.value(e.value, nullAsBlank);
    } else if (isSqlServer) {
      return '@${e.key} = ${SqlUtil.value(e.value, nullAsBlank)}';
    }
  }).join(", ");

  if (isMySql) {
    sqlCall += ');';
  }

  return sqlCall;
}