File size: 3,961 Bytes
bf47729 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
<?php
/**
* SQLite integration for WordPress
* This file enables WordPress to use SQLite database instead of MySQL
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
// Check if SQLite extension is available
if (!extension_loaded('pdo_sqlite')) {
wp_die('SQLite PDO extension is not available. Please install php-sqlite3.');
}
// Define SQLite database path
if (!defined('DB_FILE')) {
define('DB_FILE', ABSPATH . 'wp-content/database/wordpress.db');
}
// Create database directory if it doesn't exist
$db_dir = dirname(DB_FILE);
if (!file_exists($db_dir)) {
wp_mkdir_p($db_dir);
}
/**
* Custom database class for SQLite
*/
class SQLite_DB {
private $pdo;
private $last_error = '';
private $last_insert_id = 0;
private $num_rows = 0;
public function __construct() {
$this->connect();
}
private function connect() {
try {
$this->pdo = new PDO('sqlite:' . DB_FILE);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->pdo->exec('PRAGMA foreign_keys = ON');
} catch (PDOException $e) {
$this->last_error = $e->getMessage();
wp_die('Database connection failed: ' . $this->last_error);
}
}
public function query($sql) {
try {
// Convert MySQL syntax to SQLite
$sql = $this->mysql_to_sqlite($sql);
$stmt = $this->pdo->prepare($sql);
$result = $stmt->execute();
if ($result) {
$this->last_insert_id = $this->pdo->lastInsertId();
$this->num_rows = $stmt->rowCount();
return $stmt;
}
return false;
} catch (PDOException $e) {
$this->last_error = $e->getMessage();
return false;
}
}
private function mysql_to_sqlite($sql) {
// Basic MySQL to SQLite conversion
$sql = str_replace('`', '"', $sql);
$sql = preg_replace('/AUTO_INCREMENT/i', 'AUTOINCREMENT', $sql);
$sql = preg_replace('/INT\(\d+\)/i', 'INTEGER', $sql);
$sql = preg_replace('/TINYINT\(\d+\)/i', 'INTEGER', $sql);
$sql = preg_replace('/BIGINT\(\d+\)/i', 'INTEGER', $sql);
$sql = preg_replace('/VARCHAR\((\d+)\)/i', 'TEXT', $sql);
$sql = preg_replace('/LONGTEXT/i', 'TEXT', $sql);
$sql = preg_replace('/DATETIME/i', 'TEXT', $sql);
$sql = preg_replace('/TIMESTAMP/i', 'TEXT', $sql);
return $sql;
}
public function get_results($sql, $output = OBJECT) {
$stmt = $this->query($sql);
if (!$stmt) return null;
$results = [];
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
if ($output === OBJECT) {
$results[] = (object) $row;
} else {
$results[] = $row;
}
}
return $results;
}
public function get_row($sql, $output = OBJECT) {
$stmt = $this->query($sql);
if (!$stmt) return null;
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$row) return null;
if ($output === OBJECT) {
return (object) $row;
}
return $row;
}
public function get_var($sql) {
$stmt = $this->query($sql);
if (!$stmt) return null;
$row = $stmt->fetch(PDO::FETCH_NUM);
return $row ? $row[0] : null;
}
public function insert_id() {
return $this->last_insert_id;
}
public function last_error() {
return $this->last_error;
}
public function num_rows() {
return $this->num_rows;
}
}
// Initialize SQLite database connection
$wpdb = new SQLite_DB();
// WordPress database compatibility
if (!isset($GLOBALS['wpdb'])) {
$GLOBALS['wpdb'] = $wpdb;
} |