-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.php
More file actions
157 lines (134 loc) · 6.42 KB
/
Copy pathexample.php
File metadata and controls
157 lines (134 loc) · 6.42 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/*
dobu {
file:id(`example-17380`) {
ascoos {
logo {`
__ _ ___ ___ ___ ___ ___ ___ ___
/ _` |/ / / __/ _ \ / _ \ / / / _ \ / /
| (_| |\ \| (_| (_) | (_) |\ \ | (_) |\ \
\__,_|/__/ \___\___/ \___/ /__/ \___/ /__/
`},
name {`ASCOOS OS`},
version {`1.0.0`},
},
example {
class {`TJSQLDBHandler`},
methods {`createDatabase(), createUser(), selectDatabase(), setSQLQuery(), bind(), execute(), getResults(), getLastError(), close()`},
source {`example3.php`},
category:langs {
en {`Databases`},
el {`Βάσεις Δεδομένων`}
},
subcategory:langs {
en {`JSQLDB`},
el {`JSQLDB`}
},
summary:langs {
en {`Example demonstrating database creation, user assignment, table creation, batch inserts with prepared statements, and data retrieval using the JSQLDB engine.`},
el {`Παράδειγμα που δείχνει τη δημιουργία βάσης, αντιστοίχιση χρήστη, δημιουργία πίνακα, μαζικές εισαγωγές με prepared statements και ανάκτηση δεδομένων μέσω της JSQLDB.`}
},
desc:langs {
en {`This example showcases a complete workflow using the TJSQLDBHandler:
- initializing the database handler,
- creating a database and user,
- creating a table using SQL DDL,
- inserting multiple records using batch prepared statements,
- executing a SELECT query with ordering and limits,
- retrieving results,
- and properly closing all database resources.
It demonstrates real‑world usage of JSQLDB inside Ascoos OS, including error handling and logging integration.`},
el {`Αυτό το παράδειγμα παρουσιάζει ένα πλήρες workflow με το TJSQLDBHandler:
- αρχικοποίηση του database handler,
- δημιουργία βάσης και χρήστη,
- δημιουργία πίνακα μέσω SQL DDL,
- εισαγωγή πολλαπλών εγγραφών με batch prepared statements,
- εκτέλεση SELECT με ταξινόμηση και όριο,
- ανάκτηση αποτελεσμάτων,
- και σωστό κλείσιμο όλων των πόρων της βάσης.
Αναδεικνύει πραγματική χρήση της JSQLDB μέσα στο Ascoos OS, συμπεριλαμβανομένου του error handling και του logging.`}
},
keywords {`jsqldb, database, json-sql, sql-engine, ascoos-os,prepared-statements, batch-insert, select-query,table-creation, utf8, grapheme, example`},
tags {`example, jsqldb, database, insert, select, handler,prepared, batch, migration, demo`},
author {`Drogidis Christos`},
license {`AGL (ASCOOS General License)`},
since {`1.0.0`},
sincePHP {`8.4.0`}
}
}
}
*/
declare(strict_types=1);
use ASCOOS\OS\Kernel\DB\JSQLDB\TJSQLDBHandler;
use ASCOOS\OS\Kernel\Core\TError;
global $conf, $my;
$properties = [
'tables_prefix' => 'ascoos_', // final prefix e.g. ascoos_articles
'default_compression' => true // compression in TEXT
];
// Initialization of the database object
$jsqldb = new TJSQLDBHandler($conf, $properties);
// 1. Create or select a database (usually you do it once or during the install)
try {
// Database creation
$jsqldb->createDatabase('test_db');
// User creation and mapping to a database
$jsqldb->createUser('user', 'pass', 'test_db');
// Select current database
$jsqldb->selectDatabase('test_db');
} catch (Exception $e) {
$jsqldb->logger->log("Database init error: " . $e->getMessage(), $jsqldb::DEBUG_LEVEL_ERROR);
$jsqldb->close();
new TError("Database initialization problem.", E_ASCOOS_DB_JSQLDB_NOT_INIT);
}
// 2. Creating a table (usually in the migration/install script)
$sql = "CREATE TABLE IF NOT EXISTS `#__articles` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`article_id` INT UNSIGNED NOT NULL DEFAULT 0,
`cat_id` INT UNSIGNED NOT NULL DEFAULT 0,
`user_id` INT UNSIGNED NOT NULL DEFAULT 0,
`lang_id` INT UNSIGNED NOT NULL DEFAULT 0,
`title` VARCHAR(200) NOT NULL,
`content` TEXT NULL COMPRESSED,
`created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP(),
`updated` DATETIME NULL ON UPDATE CURRENT_TIMESTAMP()
);";
$jsqldb->setSQLQuery($sql);
if (!$jsqldb->execute()) {
$jsqldb->close();
new TError("Failed to create table: " . $jsqldb->logger->get_last_log(), E_ASCOOS_DB_JSQLDB_CREATE_TABLE);
}
// 3. Insert record (prepared statement)
$insertQuery = "INSERT INTO #__articles (article_id, cat_id, user_id, lang_id, title, content)
VALUES (?, ?, ?, ?, ?, ?)
";
$data = [
[1, 1, 1, 1, 'Title 1', 'Test Content 1'],
[1, 1, 1, 2, 'Title 2', 'Test Content 2'],
[2, 2, 1, 1, 'Title 3', 'Test Content 3'],
[3, 3, 1, 1, 'Title 4', 'Test Content 4'],
[3, 3, 1, 2, 'Title 5', 'Test Content 5']
];
$types = 'iiiiss'; // int, int, int, int, string, string
$jsqldb->bind($types, $data, $insertQuery); // notice: array inside an array for batch
if (!$jsqldb->execute()) {
$jsqldb->close();
new TError("Insert failed: " . $jsqldb->getLastError(), E_ASCOOS_DB_JSQLDB_INSERT_DATA);
}
// 4. Search - simple
$selectQuery = "SELECT article_id AS aid, title, content AS doc
FROM #__articles
WHERE user_id = ? AND lang_id = ?
ORDER BY created DESC
LIMIT 10
";
$jsqldb->bind('ii', [$my->id, 1], $selectQuery);
if (!$jsqldb->execute()) {
$jsqldb->close();
new TError("Insert failed: " . $jsqldb->getLastError(), E_ASCOOS_DB_JSQLDB_SELECT_QUERY);
}
$data = $jsqldb->getResults();
// 5. Closing all open database resources
$jsqldb->close();
print_r($data);
?>