From 32f86d5ae1354ccd63b61d26436e729b1a78cbe6 Mon Sep 17 00:00:00 2001 From: Efrem Ropelato Date: Wed, 20 Nov 2024 23:44:25 +0100 Subject: [PATCH] add comments --- example/index.js | 53 +++++++++++++++++++++++++++++------------------- index.js | 40 ++++++++++++++++++++++++++++-------- package.json | 2 +- 3 files changed, 65 insertions(+), 30 deletions(-) diff --git a/example/index.js b/example/index.js index 4c36b08..5f781b0 100644 --- a/example/index.js +++ b/example/index.js @@ -1,23 +1,26 @@ - +// Import the CrossDB module const CrossDB = require('../index'); +// Create an in-memory database instance const db = new CrossDB(':memory:'); try { - // Create Table + // Create tables if they do not already exist 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))"); db.exec("CREATE TABLE IF NOT EXISTS teacher (id INT PRIMARY KEY, name CHAR(16), age INT, info CHAR(255), INDEX (name))"); db.exec("CREATE TABLE IF NOT EXISTS book (id INT PRIMARY KEY, name CHAR(64), author CHAR(32), count INT, INDEX (name))"); console.log("Tables 'student','teacher' and 'book' created."); - // clean table + + // Clean (empty) all tables db.exec("DELETE FROM student"); db.exec("DELETE FROM teacher"); db.exec("DELETE FROM book"); + // Start a transaction db.begin(); console.log("Begin transaction"); - // Insert + // Insert sample data into the student, teacher, and book tables 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)"); 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!"); 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!"); @@ -25,53 +28,61 @@ try { 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)"); console.log("Data inserted."); - // Select + // Query to select all students let res = db.exec("SELECT * FROM student"); - console.log("Select all student: ", res); + console.log("Select all students: ", res); - // Update + // Update a student's age and query the updated record db.exec("UPDATE student set age=9 WHERE id = 2"); - console.log("Update age = 9 for student with id = 2"); + console.log("Updated age to 9 for student with id = 2"); res = db.exec("SELECT id,name,age,class,score from student WHERE id = 2"); console.log("Select student with id = 2: ", res); + // Delete a student and query the deleted record db.exec("DELETE FROM student WHERE id = 3"); - console.log("User with ID 3 deleted."); + console.log("Deleted student with id = 3"); res = db.exec("SELECT * from student WHERE id = 3"); console.log("Select student with id = 3: ", res); - // Aggregation function + // Perform aggregation on the student table res = db.exec("SELECT COUNT(*),MIN(score),MAX(score),SUM(score),AVG(score) FROM student"); - console.log("Select student's AGG COUNT,MIN,MAX,SUM,AVG: ", res); + console.log("Student aggregation (COUNT, MIN, MAX, SUM, AVG): ", res); + // Commit the transaction db.commit(); - console.log("Commit transaction"); + console.log("Transaction committed"); + // Start a new transaction db.begin(); console.log("Begin transaction"); - // Update + + // Update a student's age, query, and roll back the transaction db.exec("UPDATE student set age=15 WHERE id = 2"); - console.log("Update age = 15 for student with id = 2"); - // Select + console.log("Updated age to 15 for student with id = 2"); + res = db.exec("SELECT id,name,age,class,score from student WHERE id = 2"); console.log("Select student with id = 2: ", res); + db.rollback(); - console.log("Rollback transaction"); + console.log("Transaction rolled back"); + res = db.exec("SELECT id,name,age,class,score from student WHERE id = 2"); - console.log("Select student with id = 2: ", res); + console.log("Select student with id = 2 after rollback: ", res); - // Multi-Statements + // Execute multiple statements and query the results res = db.exec("SELECT COUNT(*) as Count FROM student; SELECT id,name FROM student WHERE id=2"); - console.log("Multi Select student: ", res); + console.log("Multi-statement select: ", res); } catch (err) { + // Handle errors, roll back any pending transaction, and log the error console.error("Error during operation:", err); db.rollback(); - console.log("Rollback transaction"); + console.log("Transaction rolled back due to error"); } finally { + // Ensure the database connection is closed console.log("\nCrossDB Simulation Complete."); db.close(); console.log("Database connection closed."); -} +} \ No newline at end of file diff --git a/index.js b/index.js index 82f29a8..f99f99c 100644 --- a/index.js +++ b/index.js @@ -1,29 +1,53 @@ -const crossdb = require('bindings')('crossdb'); // Collega l'addon compilato +// Import the 'bindings' module to link the compiled native addon 'crossdb' +const crossdb = require('bindings')('crossdb'); // Link the compiled addon +// Define the CrossDB class to manage database operations class CrossDB { + /** + * Constructor: initializes a new database connection + * @param {string} dbPath - The path to the database file + */ constructor(dbPath) { - this.conn = new crossdb.Connection(dbPath); + this.conn = new crossdb.Connection(dbPath); // Create a new connection to the database } + /** + * Executes an SQL query on the database + * @param {string} sql - The SQL query string to execute + * @returns {*} - The result of the query execution + */ exec(sql) { - return this.conn.exec(sql); + return this.conn.exec(sql); // Execute the SQL query and return the result } + /** + * Closes the current database connection + */ close() { - this.conn.close(); + this.conn.close(); // Close the connection } + /** + * Begins a new database transaction + */ begin() { - this.conn.begin(); + this.conn.begin(); // Start a new transaction } + /** + * Commits the current transaction + */ commit() { - this.conn.commit(); + this.conn.commit(); // Commit the transaction } + /** + * Rollsback the current transaction + */ rollback() { - this.conn.rollback(); + this.conn.rollback(); // Rollback the transaction } } -module.exports = CrossDB; +// Export the CrossDB class as a module +module.exports = CrossDB; \ No newline at end of file diff --git a/package.json b/package.json index 9f3acc7..9847093 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@croosdb/crossdb-nodejs", - "version": "1.2.8", + "version": "1.2.9", "main": "index.js", "author": "Efrem Ropelato", "contributors":[