PDO4You\PDO4You::execute PHP Метод

execute() публичный статический Метод

Method to execute a statement in the database
public static execute ( string $json, string $use = null ) : array
$json string SQL statement in JSON format
$use string OPTIONAL Name of the database defined as a new connection instance
Результат array Returns an array with the number of rows affected by type of operation
    public static function execute($json, $use = null)
    {
        // Finds a word that is at the beginning, in quotation marks or not
        $match = null;
        preg_match('~["]?([[:alnum:]]+)["]?[\\s\\n\\r\\t]{0,}?:~', $json, $match);
        try {
            // Checks the word if found, is among the allowed commands for execution
            $command = $match[1];
            if (!in_array($command, array('insert', 'update', 'delete', 'query'))) {
                throw new \PDOException(self::$exception['not-implemented'] . ' PDO4You::' . $command . '()');
            } else {
                return self::executeQuery($json, $command, $use);
            }
        } catch (\PDOException $e) {
            self::stackTrace($e, false);
        }
    }

Usage Example

Пример #1
0
 public function testMultipleDeleteInJsonFormat()
 {
     // SQL Delete in JSON format
     $json = '
     delete: [
         { table: "books" , where: { id: 200 } } ,
         { table: "users" , where: { id: 1 } } ,
         { table: "users" , where: { id: 300 } } 
     ]
     ';
     // Executes the SQL and stores the result
     $result = test::execute($json);
     $this->assertEquals(1, self::getNumRowsAffected($result), 'Test with Delete command');
 }
All Usage Examples Of PDO4You\PDO4You::execute