|
| 1 | + |
| 2 | +const CrossDB = require('../index'); |
| 3 | + |
| 4 | +const db = new CrossDB(':memory:'); |
| 5 | + |
| 6 | +try { |
| 7 | + // Create Table |
| 8 | + 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))"); |
| 9 | + db.exec("CREATE TABLE IF NOT EXISTS teacher (id INT PRIMARY KEY, name CHAR(16), age INT, info CHAR(255), INDEX (name))"); |
| 10 | + db.exec("CREATE TABLE IF NOT EXISTS book (id INT PRIMARY KEY, name CHAR(64), author CHAR(32), count INT, INDEX (name))"); |
| 11 | + console.log("Tables 'student','teacher' and 'book' created."); |
| 12 | + // clean table |
| 13 | + db.exec("DELETE FROM student"); |
| 14 | + db.exec("DELETE FROM teacher"); |
| 15 | + db.exec("DELETE FROM book"); |
| 16 | + |
| 17 | + db.begin(); |
| 18 | + console.log("Begin transaction"); |
| 19 | + |
| 20 | + // Insert |
| 21 | + 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)"); |
| 22 | + 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!"); |
| 23 | + 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!"); |
| 24 | + db.exec("INSERT INTO teacher (id,name,age) VALUES (1,'Tomas',40),(2,'Steven',50),(3,'Bill',31),(4,'Lucy',29)"); |
| 25 | + 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)"); |
| 26 | + console.log("Data inserted."); |
| 27 | + |
| 28 | + // Select |
| 29 | + let res = db.exec("SELECT * FROM student"); |
| 30 | + console.log("Select all student: ", res); |
| 31 | + |
| 32 | + // Update |
| 33 | + db.exec("UPDATE student set age=9 WHERE id = 2"); |
| 34 | + console.log("Update age = 9 for student with id = 2"); |
| 35 | + |
| 36 | + res = db.exec("SELECT id,name,age,class,score from student WHERE id = 2"); |
| 37 | + console.log("Select student with id = 2: ", res); |
| 38 | + |
| 39 | + db.exec("DELETE FROM student WHERE id = 3"); |
| 40 | + console.log("User with ID 3 deleted."); |
| 41 | + |
| 42 | + res = db.exec("SELECT * from student WHERE id = 3"); |
| 43 | + console.log("Select student with id = 3: ", res); |
| 44 | + |
| 45 | + // Aggregation function |
| 46 | + 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); |
| 48 | + |
| 49 | + db.commit(); |
| 50 | + console.log("Commit transaction"); |
| 51 | + |
| 52 | + db.begin(); |
| 53 | + console.log("Begin transaction"); |
| 54 | + // Update |
| 55 | + db.exec("UPDATE student set age=15 WHERE id = 2"); |
| 56 | + console.log("Update age = 15 for student with id = 2"); |
| 57 | + // Select |
| 58 | + res = db.exec("SELECT id,name,age,class,score from student WHERE id = 2"); |
| 59 | + console.log("Select student with id = 2: ", res); |
| 60 | + db.rollback(); |
| 61 | + console.log("Rollback transaction"); |
| 62 | + res = db.exec("SELECT id,name,age,class,score from student WHERE id = 2"); |
| 63 | + console.log("Select student with id = 2: ", res); |
| 64 | + |
| 65 | + // Multi-Statements |
| 66 | + res = db.exec("SELECT COUNT(*) as Count FROM student; SELECT id,name FROM student WHERE id=2"); |
| 67 | + console.log("Multi Select student: ", res); |
| 68 | + |
| 69 | +} catch (err) { |
| 70 | + console.error("Error during operation:", err); |
| 71 | + db.rollback(); |
| 72 | + console.log("Rollback transaction"); |
| 73 | +} finally { |
| 74 | + console.log("\nCrossDB Simulation Complete."); |
| 75 | + db.close(); |
| 76 | + console.log("Database connection closed."); |
| 77 | +} |
0 commit comments