PluginProbe
SQLite Database Integration / 2.2.21
SQLite Database Integration v2.2.21
3.0.2 3.0.1 trunk 2.1.13 2.1.14 2.1.15 2.1.16 2.2.0 2.2.1 2.2.10 2.2.11 2.2.12 2.2.13 2.2.14 2.2.15 2.2.16 2.2.17 2.2.18 2.2.19 2.2.2 2.2.20 2.2.21 2.2.22 2.2.23 2.2.3 All 32 releases
sqlite-database-integration / wp-includes / sqlite / class-wp-sqlite-crosscheck-db.php

class-wp-sqlite-crosscheck-db.php in SQLite Database Integration 2.2.21, at wp-includes/sqlite/class-wp-sqlite-crosscheck-db.php

128 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class WP_SQLite_Crosscheck_DB_ extends WP_SQLite_DB {
4
5 public function __construct( string $dbname ) {
6 parent::__construct( $dbname );
7 $GLOBALS['sqlite'] = $this;
8 $GLOBALS['mysql'] = new wpdb(
9 DB_USER,
10 DB_PASSWORD,
11 DB_NAME,
12 DB_HOST
13 );
14 // $this->resetDatabases();
15 }
16
17 private function resetDatabases() {
18 if ( file_exists( FQDB ) ) {
19 unlink( FQDB );
20 }
21 $GLOBALS['mysql']->query( 'DROP DATABASE IF EXISTS ' . DB_NAME );
22 $GLOBALS['mysql']->query( 'CREATE DATABASE ' . DB_NAME );
23 $GLOBALS['mysql']->query( 'USE ' . DB_NAME );
24 }
25
26 public function query( $query ) {
27 /**
28 * In MySQL, AUTO_INCREMENT columns don't reuse IDs assigned in rollback transactions
29 * In SQLite, AUTOINCREMENT columns do reuse IDs assigned in rollback transactions
30 *
31 * Let's store the current AUTOINCREMENT value for each table, and restore it afterwards.
32 */
33 if ( preg_match( '/^\s*rollback/i', $query ) ) {
34 $autoincrements = array();
35 $tables = $GLOBALS['@pdo']->query( "SELECT name as `table` FROM sqlite_master WHERE type='table' ORDER BY name" )->fetchAll();
36 foreach ( $tables as $table ) {
37 $table = $table['table'];
38 $autoincrement = $GLOBALS['@pdo']->query( "SELECT seq FROM sqlite_sequence WHERE name = '$table'" )->fetchColumn();
39 $autoincrements[ $table ] = $autoincrement ?: 1;
40 }
41 }
42 $sqlite_retval = parent::query( $query );
43 if ( preg_match( '/^\s*rollback/i', $query ) ) {
44 foreach ( $autoincrements as $table => $autoincrement ) {
45 $GLOBALS['@pdo']->query( "UPDATE sqlite_sequence SET seq = $autoincrement WHERE name = '$table'" );
46 }
47 }
48 $this->crosscheck( $query, $sqlite_retval );
49 return $sqlite_retval;
50 }
51
52 private function crosscheck( $query, $sqlite_retval ) {
53 // echo $query."\n\n";
54 // Be lenient on cross-checking some query types
55 if ( preg_match( '/^\s*SET storage_engine/i', $query ) ) {
56 return;
57 }
58 $this->show_errors = false;
59 $this->suppress_errors = true;
60 $GLOBALS['mysql']->show_errors = false;
61 $GLOBALS['mysql']->suppress_errors = true;
62
63 ob_start();
64 $mysql_retval = $GLOBALS['mysql']->query( $query );
65 ob_end_clean();
66
67 $tests = array(
68 array( 'retval', $mysql_retval, $sqlite_retval ),
69 array( 'num_rows', $GLOBALS['mysql']->num_rows, $GLOBALS['sqlite']->num_rows ),
70 array( 'insert_id', $GLOBALS['mysql']->insert_id, $GLOBALS['sqlite']->insert_id ),
71 array( 'rows_affected', $GLOBALS['mysql']->rows_affected, $GLOBALS['sqlite']->rows_affected ),
72 );
73
74 foreach ( $tests as $test ) {
75 list($factor, $mysql, $sqlite) = $test;
76 if ( $mysql !== $sqlite ) {
77 if ( 'insert_id' === $factor ) {
78 // On multi-inserts MySQL returns the first inserted ID
79 // while SQLite returns the last one. The cached insert_id
80 // value stays the same for a number of subsequent queries.
81 // Let's forgive this for now.
82 continue;
83 }
84 if ( 'rows_affected' === $factor && $mysql_retval === $mysql ) {
85 // SQLite doesn't provide the rowcount() functionality
86 continue;
87 }
88 if ( 'retval' === $factor && $GLOBALS['mysql']->rows_affected === $mysql ) {
89 // SQLite doesn't provide the rowcount() functionality
90 continue;
91 }
92 echo "======================================================\n";
93 echo "======== *** $factor *** differed for query ========= \n";
94 echo "======================================================\n";
95 echo "MySQL query: \n";
96 echo "$query\n\n";
97
98 echo "SQLite queries: \n";
99 foreach ( $this->dbh->last_translation->queries as $query ) {
100 echo $query->sql . "\n";
101 }
102 echo "\n";
103
104 $this->report_factor(
105 'error',
106 $GLOBALS['mysql']->last_error,
107 $GLOBALS['sqlite']->last_error
108 );
109 foreach ( $tests as $test ) {
110 $this->report_factor(
111 $test[0],
112 $test[1],
113 $test[2]
114 );
115 }
116 // throw new Exception();
117 break;
118 }
119 }
120 }
121
122 private function report_factor( $factor, $mysql, $sqlite ) {
123 echo "$factor: \n";
124 echo ' MySQL: ' . var_export( $mysql, true ) . "\n";
125 echo ' SQLite: ' . var_export( $sqlite, true ) . "\n\n";
126 }
127 }
128