|
| 1 | +'use strict'; |
| 2 | +const Database = require('../lib'); |
| 3 | + |
| 4 | +describe('Database#explain()', function () { |
| 5 | + beforeEach(function () { |
| 6 | + this.db = new Database(util.next()); |
| 7 | + this.db.exec('CREATE TABLE entries (a TEXT, b INTEGER, c REAL)'); |
| 8 | + this.db.exec("INSERT INTO entries VALUES ('foo', 1, 3.14), ('bar', 2, 2.71)"); |
| 9 | + }); |
| 10 | + afterEach(function () { |
| 11 | + this.db.close(); |
| 12 | + }); |
| 13 | + |
| 14 | + it('should throw an exception if a string is not provided', function () { |
| 15 | + expect(() => this.db.explain(123)).to.throw(TypeError); |
| 16 | + expect(() => this.db.explain(0)).to.throw(TypeError); |
| 17 | + expect(() => this.db.explain(null)).to.throw(TypeError); |
| 18 | + expect(() => this.db.explain()).to.throw(TypeError); |
| 19 | + expect(() => this.db.explain(new String('SELECT * FROM entries'))).to.throw(TypeError); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should execute a simple EXPLAIN query without parameters', function () { |
| 23 | + const plan = this.db.explain('SELECT * FROM entries'); |
| 24 | + expect(plan).to.be.an('array'); |
| 25 | + expect(plan.length).to.be.greaterThan(0); |
| 26 | + expect(plan[0]).to.be.an('object'); |
| 27 | + // EXPLAIN output should have columns like 'addr', 'opcode', 'p1', 'p2', 'p3', 'p4', 'p5', 'comment' |
| 28 | + expect(plan[0]).to.have.property('opcode'); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should work with EXPLAIN already in the SQL', function () { |
| 32 | + const plan1 = this.db.explain('SELECT * FROM entries'); |
| 33 | + const plan2 = this.db.explain('EXPLAIN SELECT * FROM entries'); |
| 34 | + expect(plan1).to.deep.equal(plan2); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should work with EXPLAIN QUERY PLAN', function () { |
| 38 | + const plan = this.db.explain("EXPLAIN QUERY PLAN SELECT * FROM entries WHERE a = 'foo'"); |
| 39 | + expect(plan).to.be.an('array'); |
| 40 | + expect(plan.length).to.be.greaterThan(0); |
| 41 | + expect(plan[0]).to.be.an('object'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should handle queries with parameters without throwing errors', function () { |
| 45 | + // This is the key fix - EXPLAIN with parameters should work |
| 46 | + const plan1 = this.db.explain('SELECT * FROM entries WHERE a = ?'); |
| 47 | + expect(plan1).to.be.an('array'); |
| 48 | + expect(plan1.length).to.be.greaterThan(0); |
| 49 | + |
| 50 | + const plan2 = this.db.explain('SELECT * FROM entries WHERE a = :name AND b = :value'); |
| 51 | + expect(plan2).to.be.an('array'); |
| 52 | + expect(plan2.length).to.be.greaterThan(0); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should handle complex queries with multiple parameters', function () { |
| 56 | + const plan = this.db.explain('SELECT * FROM entries WHERE a = ? AND b > ? AND c < ?'); |
| 57 | + expect(plan).to.be.an('array'); |
| 58 | + expect(plan.length).to.be.greaterThan(0); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should work with JOIN queries', function () { |
| 62 | + this.db.exec('CREATE TABLE users (id INTEGER, name TEXT)'); |
| 63 | + const plan = this.db.explain('SELECT * FROM entries JOIN users ON entries.b = users.id WHERE users.name = ?'); |
| 64 | + expect(plan).to.be.an('array'); |
| 65 | + expect(plan.length).to.be.greaterThan(0); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should throw an exception for invalid SQL', function () { |
| 69 | + expect(() => this.db.explain('INVALID SQL')).to.throw(Database.SqliteError); |
| 70 | + expect(() => this.db.explain('SELECT * FROM nonexistent')).to.throw(Database.SqliteError); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should work with case insensitive EXPLAIN', function () { |
| 74 | + const plan1 = this.db.explain('explain SELECT * FROM entries'); |
| 75 | + const plan2 = this.db.explain('ExPlAiN SELECT * FROM entries'); |
| 76 | + const plan3 = this.db.explain('EXPLAIN SELECT * FROM entries'); |
| 77 | + expect(plan1.length).to.equal(plan2.length); |
| 78 | + expect(plan2.length).to.equal(plan3.length); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should respect readonly connections', function () { |
| 82 | + this.db.close(); |
| 83 | + this.db = new Database(util.current(), { readonly: true, fileMustExist: true }); |
| 84 | + const plan = this.db.explain('SELECT * FROM entries WHERE a = ?'); |
| 85 | + expect(plan).to.be.an('array'); |
| 86 | + expect(plan.length).to.be.greaterThan(0); |
| 87 | + }); |
| 88 | +}); |
| 89 | + |
0 commit comments