Skip to content

Commit 32f86d5

Browse files
committed
add comments
1 parent e790a2f commit 32f86d5

File tree

3 files changed

+65
-30
lines changed

3 files changed

+65
-30
lines changed

example/index.js

+32-21
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,88 @@
1-
1+
// Import the CrossDB module
22
const CrossDB = require('../index');
33

4+
// Create an in-memory database instance
45
const db = new CrossDB(':memory:');
56

67
try {
7-
// Create Table
8+
// Create tables if they do not already exist
89
db.exec("CREATE TABLE IF NOT EXISTS student (id INT PRIMARY KEY, name CHAR(16), age INT, class CHAR(16), score FLOAT, info VARCHAR(255))");
910
db.exec("CREATE TABLE IF NOT EXISTS teacher (id INT PRIMARY KEY, name CHAR(16), age INT, info CHAR(255), INDEX (name))");
1011
db.exec("CREATE TABLE IF NOT EXISTS book (id INT PRIMARY KEY, name CHAR(64), author CHAR(32), count INT, INDEX (name))");
1112
console.log("Tables 'student','teacher' and 'book' created.");
12-
// clean table
13+
14+
// Clean (empty) all tables
1315
db.exec("DELETE FROM student");
1416
db.exec("DELETE FROM teacher");
1517
db.exec("DELETE FROM book");
1618

19+
// Start a transaction
1720
db.begin();
1821
console.log("Begin transaction");
1922

20-
// Insert
23+
// Insert sample data into the student, teacher, and book tables
2124
db.exec("INSERT INTO student (id,name,age,class,score) VALUES (1,'jack',10,'3-1',90),(2,'tom',11,'2-5',91),(3,'jack',11,'1-6',92),(4,'rose',10,'4-2',90),(5,'tim',10,'3-1',95)");
2225
db.exec("INSERT INTO student (id,name,age,class,score,info) VALUES (6,'Tony',10,'3-1',95,'%s')", "He is a boy.\nHe likes playing football.\nWe all like him!");
2326
db.exec("INSERT INTO student (id,name,age,class,score,info) VALUES (7,'Wendy',10,'3-1',95,'%s')", "She is a girl.\nShe likes cooking.\nWe all love her!");
2427
db.exec("INSERT INTO teacher (id,name,age) VALUES (1,'Tomas',40),(2,'Steven',50),(3,'Bill',31),(4,'Lucy',29)");
2528
db.exec("INSERT INTO book (id,name,author,count) VALUES (1,'Romeo and Juliet','Shakespeare',10),(2,'Pride and Prejudice','Austen',5),(3,'Great Expectations','Dickens',8),(4,'Sorrows of Young Werther','Von Goethe',4)");
2629
console.log("Data inserted.");
2730

28-
// Select
31+
// Query to select all students
2932
let res = db.exec("SELECT * FROM student");
30-
console.log("Select all student: ", res);
33+
console.log("Select all students: ", res);
3134

32-
// Update
35+
// Update a student's age and query the updated record
3336
db.exec("UPDATE student set age=9 WHERE id = 2");
34-
console.log("Update age = 9 for student with id = 2");
37+
console.log("Updated age to 9 for student with id = 2");
3538

3639
res = db.exec("SELECT id,name,age,class,score from student WHERE id = 2");
3740
console.log("Select student with id = 2: ", res);
3841

42+
// Delete a student and query the deleted record
3943
db.exec("DELETE FROM student WHERE id = 3");
40-
console.log("User with ID 3 deleted.");
44+
console.log("Deleted student with id = 3");
4145

4246
res = db.exec("SELECT * from student WHERE id = 3");
4347
console.log("Select student with id = 3: ", res);
4448

45-
// Aggregation function
49+
// Perform aggregation on the student table
4650
res = db.exec("SELECT COUNT(*),MIN(score),MAX(score),SUM(score),AVG(score) FROM student");
47-
console.log("Select student's AGG COUNT,MIN,MAX,SUM,AVG: ", res);
51+
console.log("Student aggregation (COUNT, MIN, MAX, SUM, AVG): ", res);
4852

53+
// Commit the transaction
4954
db.commit();
50-
console.log("Commit transaction");
55+
console.log("Transaction committed");
5156

57+
// Start a new transaction
5258
db.begin();
5359
console.log("Begin transaction");
54-
// Update
60+
61+
// Update a student's age, query, and roll back the transaction
5562
db.exec("UPDATE student set age=15 WHERE id = 2");
56-
console.log("Update age = 15 for student with id = 2");
57-
// Select
63+
console.log("Updated age to 15 for student with id = 2");
64+
5865
res = db.exec("SELECT id,name,age,class,score from student WHERE id = 2");
5966
console.log("Select student with id = 2: ", res);
67+
6068
db.rollback();
61-
console.log("Rollback transaction");
69+
console.log("Transaction rolled back");
70+
6271
res = db.exec("SELECT id,name,age,class,score from student WHERE id = 2");
63-
console.log("Select student with id = 2: ", res);
72+
console.log("Select student with id = 2 after rollback: ", res);
6473

65-
// Multi-Statements
74+
// Execute multiple statements and query the results
6675
res = db.exec("SELECT COUNT(*) as Count FROM student; SELECT id,name FROM student WHERE id=2");
67-
console.log("Multi Select student: ", res);
76+
console.log("Multi-statement select: ", res);
6877

6978
} catch (err) {
79+
// Handle errors, roll back any pending transaction, and log the error
7080
console.error("Error during operation:", err);
7181
db.rollback();
72-
console.log("Rollback transaction");
82+
console.log("Transaction rolled back due to error");
7383
} finally {
84+
// Ensure the database connection is closed
7485
console.log("\nCrossDB Simulation Complete.");
7586
db.close();
7687
console.log("Database connection closed.");
77-
}
88+
}

index.js

+32-8
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,53 @@
1-
const crossdb = require('bindings')('crossdb'); // Collega l'addon compilato
1+
// Import the 'bindings' module to link the compiled native addon 'crossdb'
2+
const crossdb = require('bindings')('crossdb'); // Link the compiled addon
23

4+
// Define the CrossDB class to manage database operations
35
class CrossDB {
6+
/**
7+
* Constructor: initializes a new database connection
8+
* @param {string} dbPath - The path to the database file
9+
*/
410
constructor(dbPath) {
5-
this.conn = new crossdb.Connection(dbPath);
11+
this.conn = new crossdb.Connection(dbPath); // Create a new connection to the database
612
}
713

14+
/**
15+
* Executes an SQL query on the database
16+
* @param {string} sql - The SQL query string to execute
17+
* @returns {*} - The result of the query execution
18+
*/
819
exec(sql) {
9-
return this.conn.exec(sql);
20+
return this.conn.exec(sql); // Execute the SQL query and return the result
1021
}
1122

23+
/**
24+
* Closes the current database connection
25+
*/
1226
close() {
13-
this.conn.close();
27+
this.conn.close(); // Close the connection
1428
}
1529

30+
/**
31+
* Begins a new database transaction
32+
*/
1633
begin() {
17-
this.conn.begin();
34+
this.conn.begin(); // Start a new transaction
1835
}
1936

37+
/**
38+
* Commits the current transaction
39+
*/
2040
commit() {
21-
this.conn.commit();
41+
this.conn.commit(); // Commit the transaction
2242
}
2343

44+
/**
45+
* Rollsback the current transaction
46+
*/
2447
rollback() {
25-
this.conn.rollback();
48+
this.conn.rollback(); // Rollback the transaction
2649
}
2750
}
2851

29-
module.exports = CrossDB;
52+
// Export the CrossDB class as a module
53+
module.exports = CrossDB;

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@croosdb/crossdb-nodejs",
3-
"version": "1.2.8",
3+
"version": "1.2.9",
44
"main": "index.js",
55
"author": "Efrem Ropelato",
66
"contributors":[

0 commit comments

Comments
 (0)