Skip to content

Commit 32d0082

Browse files
authored
Implement SQLite support in Database class
Added support for SQLite database alongside MySQL, including connection handling and table creation.
1 parent 4998d6f commit 32d0082

1 file changed

Lines changed: 166 additions & 21 deletions

File tree

config/database.php

Lines changed: 166 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,172 @@
11
<?php
2-
class Database {
3-
private $host = 'localhost';
4-
private $db_name = 'YOUR_DB_NAME';
5-
private $username = 'YOUR_DB_USERNAME';
6-
private $password = 'YOUR DB PASSWORD';
7-
public $conn;
8-
9-
public function connect() {
10-
try {
11-
$this->conn = new PDO(
12-
"mysql:host={$this->host};dbname={$this->db_name};charset=utf8mb4",
13-
$this->username,
14-
$this->password,
15-
[
16-
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // ✅ fixed
17-
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
18-
]
2+
require_once __DIR__ . '/constants.php';
3+
if(!defined('DB_MODULE')){ exit('CONSTANTS MISSING'); }
4+
5+
if(DB_MODULE == "sqllite"){
6+
// SQLite Database Class
7+
class Database {
8+
private $db_path;
9+
public $conn;
10+
11+
public function __construct() {
12+
// Set SQLite database path relative to this file
13+
$this->db_path = __DIR__ . '/../app-db.sqllite';
14+
}
15+
16+
public function connect() {
17+
try {
18+
// Ensure directory exists
19+
$dir = dirname($this->db_path);
20+
if (!is_dir($dir)) {
21+
mkdir($dir, 0755, true);
22+
}
23+
24+
$this->conn = new PDO(
25+
"sqlite:{$this->db_path}",
26+
null,
27+
null,
28+
[
29+
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
30+
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
31+
PDO::ATTR_PERSISTENT => false
32+
]
33+
);
34+
35+
// Enable foreign keys and set busy timeout
36+
$this->conn->exec('PRAGMA foreign_keys = ON;');
37+
$this->conn->exec('PRAGMA busy_timeout = 5000;');
38+
39+
// Create tables if they don't exist
40+
$this->createTables();
41+
42+
} catch (PDOException $e) {
43+
error_log("SQLite connection failed: " . $e->getMessage());
44+
return false;
45+
}
46+
return $this->conn;
47+
}
48+
49+
// SQLite specific helper methods
50+
public function tableExists($tableName) {
51+
$stmt = $this->conn->prepare(
52+
"SELECT name FROM sqlite_master WHERE type='table' AND name=?"
1953
);
20-
} catch (PDOException $e) {
21-
error_log("Database connection failed: " . $e->getMessage());
22-
return false;
54+
$stmt->execute([$tableName]);
55+
return $stmt->fetch() !== false;
56+
}
57+
58+
public function createTables() {
59+
// Create app_analytics table (matches MySQL structure)
60+
$this->conn->exec("
61+
CREATE TABLE IF NOT EXISTS app_analytics (
62+
id INTEGER PRIMARY KEY AUTOINCREMENT,
63+
site_domain TEXT NOT NULL,
64+
event_type TEXT NOT NULL,
65+
event_data TEXT,
66+
user_agent TEXT,
67+
ip_address TEXT,
68+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
69+
)
70+
");
71+
72+
// Create indexes for app_analytics
73+
$this->conn->exec("CREATE INDEX IF NOT EXISTS idx_analytics_site_domain ON app_analytics(site_domain)");
74+
$this->conn->exec("CREATE INDEX IF NOT EXISTS idx_analytics_event_type ON app_analytics(event_type)");
75+
$this->conn->exec("CREATE INDEX IF NOT EXISTS idx_analytics_created_at ON app_analytics(created_at)");
76+
77+
// Create app_data table (matches MySQL structure)
78+
$this->conn->exec("
79+
CREATE TABLE IF NOT EXISTS app_data (
80+
id INTEGER PRIMARY KEY AUTOINCREMENT,
81+
site_domain TEXT NOT NULL,
82+
data_key TEXT NOT NULL,
83+
data_value TEXT,
84+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
85+
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
86+
UNIQUE(site_domain, data_key)
87+
)
88+
");
89+
90+
// Create indexes for app_data
91+
$this->conn->exec("CREATE INDEX IF NOT EXISTS idx_data_site_domain ON app_data(site_domain)");
92+
$this->conn->exec("CREATE INDEX IF NOT EXISTS idx_data_key ON app_data(data_key)");
93+
94+
// Create sites table (matches your MySQL structure exactly)
95+
$this->conn->exec("
96+
CREATE TABLE IF NOT EXISTS sites (
97+
id INTEGER PRIMARY KEY AUTOINCREMENT,
98+
site_domain TEXT UNIQUE NOT NULL,
99+
site_name TEXT,
100+
access_token TEXT NOT NULL,
101+
scope TEXT,
102+
userid INTEGER,
103+
name TEXT,
104+
accountOwner INTEGER DEFAULT 0,
105+
email TEXT,
106+
currency TEXT,
107+
timezone TEXT,
108+
installed_at DATETIME DEFAULT CURRENT_TIMESTAMP,
109+
uninstalled_at DATETIME NULL,
110+
is_active INTEGER DEFAULT 1,
111+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
112+
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
113+
)
114+
");
115+
116+
// Create sessions table (for session management)
117+
$this->conn->exec("
118+
CREATE TABLE IF NOT EXISTS sessions (
119+
id INTEGER PRIMARY KEY AUTOINCREMENT,
120+
session_id TEXT UNIQUE NOT NULL,
121+
site_domain TEXT NOT NULL,
122+
access_token TEXT NOT NULL,
123+
scope TEXT,
124+
expires_at DATETIME,
125+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
126+
)
127+
");
128+
}
129+
130+
// Helper method to check if database is properly set up
131+
public function checkDatabase() {
132+
$tables = ['app_analytics', 'app_data', 'sites', 'sessions'];
133+
$missing = [];
134+
135+
foreach ($tables as $table) {
136+
if (!$this->tableExists($table)) {
137+
$missing[] = $table;
138+
}
139+
}
140+
141+
return empty($missing) ? true : $missing;
142+
}
143+
}
144+
} else {
145+
// MySQL Database Class
146+
class Database {
147+
private $host = DB_HOST;
148+
private $db_name = DB_NAME;
149+
private $username = DB_USER;
150+
private $password = DB_PASSWORD;
151+
public $conn;
152+
153+
public function connect() {
154+
try {
155+
$this->conn = new PDO(
156+
"mysql:host={$this->host};dbname={$this->db_name};charset=utf8mb4",
157+
$this->username,
158+
$this->password,
159+
[
160+
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
161+
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
162+
]
163+
);
164+
} catch (PDOException $e) {
165+
error_log("Database connection failed: " . $e->getMessage());
166+
return false;
167+
}
168+
return $this->conn;
23169
}
24-
return $this->conn;
25170
}
26171
}
27172
?>

0 commit comments

Comments
 (0)