From ce81ae254e945bb96db94a9da2f82e5260aee3d5 Mon Sep 17 00:00:00 2001 From: ThinhVu Date: Tue, 21 Mar 2023 00:50:48 +0700 Subject: [PATCH] add sql driver example --- src/executor.sql.test.js | 47 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/executor.sql.test.js diff --git a/src/executor.sql.test.js b/src/executor.sql.test.js new file mode 100644 index 0000000..9816431 --- /dev/null +++ b/src/executor.sql.test.js @@ -0,0 +1,47 @@ +describe('executor', () => { + it('sql driver', () => { + const someNativeDriver = { + execQry (qry) { + return `exec query ${qry.trim()}` + }, + execNonQry(nonQry) { + return `exec non query ${nonQry.trim()}` + } + } + + const makeEntity = (table) => { + let qry = ''; + return { + $() { + let q = qry; + qry = ''; + return someNativeDriver.execQry(q) + }, + $$() { + let q = qry; + qry = ''; + return someNativeDriver.execNonQry(q) + }, + select(cols) { + const projection = typeof cols === 'string' ? cols : cols.join(', ') + qry += `SELECT ${projection} from ${table} +`; + return this; + }, + where(condition) { + qry += +`WHERE ${condition} +`; + return this; + } + } + } + const sqlDriver = { + user: makeEntity('user') + } + + const rs = sqlDriver.user.select(['fullName', 'username']).where('age > 10').$() + expect(rs).toBe('exec query SELECT fullName, username from user\n' + + 'WHERE age > 10') + }) +})