+32
-6
@@ -57,4 +57,28 @@ // 0. @sql-extra/createindex (createIndex) | ||
| insertInto.stream = stream3; | ||
| // 4. @sql-extra/setuptable (setupTable) | ||
| function index4(nam, cols, opt={}, z='') { | ||
| // 4. @sql-extra/matchtsquery (matchTsquery) | ||
| function matchTsquery(tab, wrds, tsv='"tsvector"', opt={}) { | ||
| var col = opt.columns||'*', nrm = opt.normalization||0; | ||
| for(var i=wrds.length, z=''; i>0; i--) { | ||
| var qry = wrds.slice(0, i).join(' ').replace(/([\'\"])/g, '$1$1'); | ||
| z += `SELECT ${col}, '${i}'::INT AS "matchTsquery" FROM "${tab}"`; | ||
| z += ` WHERE ${tsv} @@ plainto_tsquery('${qry}')`; | ||
| if(opt.order) z += ` ORDER BY ts_rank(${tsv}, plainto_tsquery('${qry}'), ${nrm}) DESC`; | ||
| z += ' UNION ALL\n'; | ||
| } | ||
| z = z.substring(0, z.length-11); | ||
| if(opt.limit) z += ` LIMIT ${opt.limit}`; | ||
| z += ';\n'; | ||
| return z; | ||
| }; | ||
| // 5. @sql-extra/selecttsquery (selectTsquery) | ||
| function selectTsquery(tab, qry, tsv='"tsvector"', opt={}) { | ||
| var col = opt.columns||'*', nrm = opt.normalization||0; | ||
| var z = `SELECT ${col} FROM "${tab}" WHERE ${tsv} @@ plainto_tsquery('${qry}')`; | ||
| if(opt.order) z += ` ORDER BY ts_rank(${tsv}, plainto_tsquery('${qry}'), ${nrm}) DESC`; | ||
| if(opt.limit) z += ` LIMIT ${opt.limit}`; | ||
| z += `;\n`; | ||
| return z; | ||
| }; | ||
| // 6. @sql-extra/setuptable (setupTable) | ||
| function index6(nam, cols, opt={}, z='') { | ||
| if(opt.tsvector) { | ||
@@ -77,10 +101,10 @@ var tv = tsvector(opt.tsvector); | ||
| if(vals) z += insertInto(nam, vals, opt); | ||
| return index4(nam, cols, opt, z); | ||
| return index6(nam, cols, opt, z); | ||
| }; | ||
| setupTable.index = index4; | ||
| // 5. @sql-extra/tableexists (tableExists) | ||
| setupTable.index = index6; | ||
| // 7. @sql-extra/tableexists (tableExists) | ||
| function tableExists(nam) { | ||
| return `SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name='${nam}');\n`; | ||
| }; | ||
| // 6. @sql-extra/tsvector (tsvector) | ||
| // 8. @sql-extra/tsvector (tsvector) | ||
| function tsvector(cols) { | ||
@@ -96,4 +120,6 @@ var z = ''; | ||
| exports.insertInto = insertInto; | ||
| exports.matchTsquery = matchTsquery; | ||
| exports.selectTsquery = selectTsquery; | ||
| exports.setupTable = setupTable; | ||
| exports.tableExists = tableExists; | ||
| exports.tsvector = tsvector; |
+3
-1
| { | ||
| "name": "sql-extra", | ||
| "version": "0.1.6", | ||
| "version": "0.1.7", | ||
| "description": "Extra methods for generating SQL commands.", | ||
@@ -28,2 +28,4 @@ "main": "index.js", | ||
| "@sql-extra/insertinto": "^0.1.5", | ||
| "@sql-extra/matchtsquery": "^0.1.1", | ||
| "@sql-extra/selecttsquery": "^0.1.1", | ||
| "@sql-extra/setuptable": "^0.1.7", | ||
@@ -30,0 +32,0 @@ "@sql-extra/tableexists": "^0.1.1", |
+2
-0
@@ -21,1 +21,3 @@ # sql-extra | ||
| - [tableExists](https://www.npmjs.com/package/@sql-extra/tableexists) | ||
| - [selectTsquery](https://www.npmjs.com/package/@sql-extra/selecttsquery) | ||
| - [matchTsquery](https://www.npmjs.com/package/@sql-extra/matchtsquery) |
7227
23.6%122
27.08%23
9.52%9
28.57%