Method Documentation: executeRaw

Purpose

The executeRaw method is specifically designed to directly execute raw SQL commands, such as INSERT, UPDATE, and DELETE, against the database. This method is crucial for operations that modify the database but do not produce a result set. It provides a direct interface for executing SQL statements, returning the number of rows affected by the command, which serves as immediate feedback on the operation's impact.

Parameters

  • string $sql - The raw SQL command to be executed. This parameter allows the developer to pass in any SQL command as a string.

Return Value

The method returns an integer indicating the number of rows affected by the SQL command. This return value provides a quantitative measure of the effect of the database operation.

Error Handling

If the database operation encounters a failure, the method throws an exception. This exception handling mechanism ensures that errors are not silently ignored but are instead adequately reported for further troubleshooting and resolution.

Example Usage

  use Lib\Prisma\Classes\Prisma;
            
    $prisma = Prisma::getInstance();
    $sql = "UPDATE Users SET status = 'active' WHERE registration_date < '2023-01-01'";
    
    try {
        $affectedRows = $prisma->executeRaw($sql);
        echo "Number of rows affected: " . $affectedRows;
    } catch (\Exception $e) {
        echo "An error occurred: " . $e->getMessage();
    }