Пример #1
0
function test_type(&$db, $offset, $sql_type, $value, $ret_value = NULL, $pattern = NULL, $alternative_type = NULL)
{
    $db->exec('DROP TABLE IF EXISTS test');
    $sql = sprintf('CREATE TABLE test(id INT, label %s) ENGINE=%s', $sql_type, MySQLPDOTest::getTableEngine());
    @$db->exec($sql);
    if ($db->errorCode() != 0) {
        // not all MySQL Server versions and/or engines might support the type
        return true;
    }
    $stmt = $db->prepare('INSERT INTO test(id, label) VALUES (?, ?)');
    $stmt->bindValue(1, $offset);
    $stmt->bindValue(2, $value);
    if (!$stmt->execute()) {
        printf("[%03d + 1] INSERT failed, %s\n", $offset, var_export($stmt->errorInfo(), true));
        return false;
    }
    $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
    $stmt = $db->query('SELECT  id, label FROM test');
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    $stmt->closeCursor();
    if (!isset($row['id']) || !isset($row['label'])) {
        printf("[%03d + 2] Fetched result seems wrong, dumping result: %s\n", $offset, var_export($row, true));
        return false;
    }
    if ($row['id'] != $offset) {
        printf("[%03d + 3] Expecting %s got %s\n", $offset, $row['id']);
        return false;
    }
    if (!is_null($pattern)) {
        if (!preg_match($pattern, $row['label'])) {
            printf("[%03d + 5] Value seems wrong, accepting pattern %s got %s, check manually\n", $offset, $pattern, var_export($row['label'], true));
            return false;
        }
    } else {
        $exp = $value;
        if (!is_null($ret_value)) {
            // we expect a different return value than our input value
            // typically the difference is only the type
            $exp = $ret_value;
        }
        if ($row['label'] !== $exp && !is_null($alternative_type) && gettype($row['label']) != $alternative_type) {
            printf("[%03d + 4] %s - input = %s/%s, output = %s/%s (alternative type: %s)\n", $offset, $sql_type, var_export($exp, true), gettype($exp), var_export($row['label'], true), gettype($row['label']), $alternative_type);
            return false;
        }
    }
    $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
    $stmt = $db->query('SELECT id, label FROM test');
    $row_string = $stmt->fetch(PDO::FETCH_ASSOC);
    $stmt->closeCursor();
    if (is_null($pattern) && $row['label'] != $row_string['label']) {
        printf("%s - STRINGIGY = %s, NATIVE = %s\n", $sql_type, var_export($row_string['label'], true), var_export($row['label'], true));
        return false;
    } else {
        if (!is_null($pattern) && !preg_match($pattern, $row_string['label'])) {
            printf("%s - STRINGIGY = %s, NATIVE = %s, pattern '%s'\n", $sql_type, var_export($row_string['label'], true), var_export($row['label'], true), $pattern);
            return false;
        }
    }
    return true;
}
function pdo_mysql_stmt_bindparam_types_do($db, $offset, $native, $sql_type, $value)
{
    if ($native) {
        $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 0);
    } else {
        $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 1);
    }
    $db->exec('DROP TABLE IF EXISTS test');
    $sql = sprintf('CREATE TABLE test(id INT, label %s) ENGINE=%s', $sql_type, MySQLPDOTest::getTableEngine());
    if (!($stmt = @$db->prepare($sql)) || !@$stmt->execute()) {
        // Server might not support column type - skip it
        return true;
    }
    $stmt = $db->prepare('INSERT INTO test(id, label) VALUES (1, ?)');
    if (!$stmt->bindParam(1, $value)) {
        printf("[%03d/%s + 1] %s\n", $offset, $native ? 'native' : 'emulated', var_export($stmt->errorInfo(), true));
        return false;
    }
    if (!$stmt->execute()) {
        printf("[%03d/%s + 2] %s\n", $offset, $native ? 'native' : 'emulated', var_export($stmt->errorInfo(), true));
        return false;
    }
    $stmt = $db->query('SELECT id, label FROM test');
    $id = $label = null;
    if (!$stmt->bindColumn(1, $id)) {
        printf("[%03d/%s + 3] %s\n", $offset, $native ? 'native' : 'emulated', var_export($stmt->errorInfo(), true));
        return false;
    }
    if (!$stmt->bindColumn(2, $label)) {
        printf("[%03d/%s + 4] %s\n", $offset, $native ? 'native' : 'emulated', var_export($stmt->errorInfo(), true));
        return false;
    }
    if (!$stmt->fetch(PDO::FETCH_BOUND)) {
        printf("[%03d/%s + 5] %s\n", $offset, $native ? 'native' : 'emulated', var_export($stmt->errorInfo(), true));
        return false;
    }
    $stmt->closeCursor();
    if ($label != $value) {
        printf("[%03d/%s + 6] Got %s expecting %s - plase check manually\n", $offset, $native ? 'native' : 'emulated', var_export($label, true), var_export($value, true));
        // fall through
    }
    $stmt->execute();
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    if (empty($row)) {
        printf("[%03d/%s + 7] %s\n", $offset, $native ? 'native' : 'emulated', var_export($stmt->errorInfo(), true));
        return false;
    }
    if ($row['label'] != $value) {
        printf("[%03d/%s + 8] Got %s expecting %s - plase check manually\n", $offset, $native ? 'native' : 'emulated', var_export($row['label'], true), var_export($value, true));
        return false;
    }
    if ($row['label'] != $label) {
        printf("[%03d/%s + 9] Got %s from FETCH_ASSOC and %s from FETCH_BOUND- plase check manually\n", $offset, $native ? 'native' : 'emulated', var_export($row['label'], true), var_export($value, true));
        return false;
    }
    $db->exec('DROP TABLE IF EXISTS test');
    return true;
}
 function test_meta(&$db, $offset, $sql_type, $value, $native_type, $pdo_type)
 {
     $db->exec('DROP TABLE IF EXISTS test');
     $sql = sprintf('CREATE TABLE test(id INT, label %s) ENGINE=%s', $sql_type, MySQLPDOTest::getTableEngine());
     if (!($stmt = @$db->prepare($sql)) || !@$stmt->execute()) {
         // Some engines and/or MySQL server versions might not support the data type
         return true;
     }
     if (!$db->exec(sprintf("INSERT INTO test(id, label) VALUES (1, '%s')", $value))) {
         printf("[%03d] + 1] Insert failed, %d - %s\n", $offset, $db->errorCode(), var_export($db->errorInfo(), true));
         return false;
     }
     $stmt = $db->prepare('SELECT id, label FROM test');
     $stmt->execute();
     $meta = $stmt->getColumnMeta(1);
     $row = $stmt->fetch(PDO::FETCH_ASSOC);
     if (empty($meta)) {
         printf("[%03d + 2] getColumnMeta() failed, %d - %s\n", $offset, $stmt->errorCode(), var_export($stmt->errorInfo(), true));
         return false;
     }
     $elements = array('flags', 'table', 'name', 'len', 'precision', 'pdo_type');
     foreach ($elements as $k => $element) {
         if (!isset($meta[$element])) {
             printf("[%03d + 3] Element %s missing, %s\n", $offset, $element, var_export($meta, true));
             return false;
         }
     }
     if ($meta['table'] != 'test' || $meta['name'] != 'label') {
         printf("[%03d + 4] Table or field name is wrong, %s\n", $offset, var_export($meta, true));
         return false;
     }
     if (!is_null($native_type)) {
         if (!isset($meta['native_type'])) {
             printf("[%03d + 5] Element native_type missing, %s\n", $offset, var_export($meta, true));
             return false;
         }
         if (!is_array($native_type)) {
             $native_type = array($native_type);
         }
         $found = false;
         foreach ($native_type as $k => $type) {
             if ($meta['native_type'] == $type) {
                 $found = true;
                 break;
             }
         }
         if (!$found) {
             printf("[%03d + 6] Expecting native type %s, %s\n", $offset, var_export($native_type, true), var_export($meta, true));
             return false;
         }
     }
     if (!is_null($pdo_type) && $meta['pdo_type'] != $pdo_type) {
         printf("[%03d + 6] Expecting PDO type %s got %s (%s)\n", $offset, $pdo_type, var_export($meta, true), var_export($meta['native_type']));
         return false;
     }
     return true;
 }
Пример #4
0
function test_type(&$db, $offset, $sql_type, $value, $ret_value = NULL, $pattern = NULL)
{
    $db->exec('DROP TABLE IF EXISTS test');
    $sql = sprintf('CREATE TABLE test(id INT, label %s) ENGINE=%s', $sql_type, MySQLPDOTest::getTableEngine());
    @$db->exec($sql);
    if ($db->errorCode() != 0) {
        // not all MySQL Server versions and/or engines might support the type
        return true;
    }
    $stmt = $db->prepare('INSERT INTO test(id, label) VALUES (?, ?)');
    $stmt->bindValue(1, $offset);
    $stmt->bindValue(2, $value);
    if (!$stmt->execute()) {
        printf("[%03d + 1] INSERT failed, %s\n", $offset, var_export($stmt->errorInfo(), true));
        return false;
    }
    $stmt = $db->query('SELECT  id, label FROM test');
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    var_dump($row);
    var_dump($value);
    return true;
}
     public function serialize()
     {
         printf("%s()\n", __METHOD__);
         return 'Data from serialize';
     }
     public function unserialize($data)
     {
         printf("%s(%s)\n", __METHOD__, var_export($data, true));
     }
 }
 $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 0);
 if (0 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY)) {
     printf("[002] Unable to turn off emulated prepared statements\n");
 }
 $db->exec('DROP TABLE IF EXISTS test');
 $db->exec(sprintf('CREATE TABLE test(id INT, myobj BLOB) ENGINE=%s', MySQLPDOTest::getTableEngine()));
 printf("Creating an object, serializing it and writing it to DB...\n");
 $id = 1;
 $obj = myclass::singleton('Creating object');
 $myobj = serialize($obj);
 $stmt = $db->prepare('INSERT INTO test(id, myobj) VALUES (?, ?)');
 $stmt->bindValue(1, $id);
 $stmt->bindValue(2, $myobj);
 $stmt->execute();
 printf("\nUnserializing the previously serialized object...\n");
 var_dump(unserialize($myobj));
 printf("\nUsing PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE to fetch the object from DB and unserialize it...\n");
 $stmt = $db->prepare('SELECT myobj FROM test');
 $stmt->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_SERIALIZE, 'myclass', array('PDO shall not call __construct()'));
 $stmt->execute();
 var_dump($stmt->fetch());