constselect=this.options.dialect==='mysql'?`SELECT \`${this.options.table}\`.* FROM \`${this.options.table}\` WHERE (\`${this.options.table}\`.\`key\` = '${key}')`:`SELECT "${this.options.table}".* FROM "${this.options.table}" WHERE ("${this.options.table}"."key" = '${key}')`
returnthis.query(select)
.then(rows=>{
constrow=rows[0]
if(row===undefined){
returnundefined
}
constselect=
this.options.dialect==='mysql'
?`SELECT \`${this.options.table}\`.* FROM \`${this.options.table}\` WHERE (\`${this.options.table}\`.\`key\` = '${key}')`
:`SELECT "${this.options.table}".* FROM "${this.options.table}" WHERE ("${this.options.table}"."key" = '${key}')`
returnthis.query(select).then(rows=>{
constrow=rows[0]
if(row===undefined){
returnundefined
}
returnrow.value
})
returnrow.value
})
}
set(key,value){
@ -40,39 +52,61 @@ class KeyvSql extends EventEmitter {
value=value.replace(/\\/g,'\\\\')
}
constupsert=this.options.dialect==='postgres'
?`INSERT INTO "${this.options.table}" ("key", "value") VALUES ('${key}', '${value}') ON CONFLICT ("key") DO UPDATE SET "value" = EXCLUDED."value"`
:(this.options.dialect==='mysql'
?`REPLACE INTO \`${this.options.table}\` (\`key\`, \`value\`) VALUES ('${key}', '${value}')`
:`REPLACE INTO "${this.options.table}" ("key", "value") VALUES ('${key}', '${value}')`)
constupsert=
this.options.dialect==='postgres'
?`INSERT INTO "${this.options.table}" ("key", "value") VALUES ('${key}', '${value}') ON CONFLICT ("key") DO UPDATE SET "value" = EXCLUDED."value"`
:this.options.dialect==='mysql'
?`REPLACE INTO \`${this.options.table}\` (\`key\`, \`value\`) VALUES ('${key}', '${value}')`
:`REPLACE INTO "${this.options.table}" ("key", "value") VALUES ('${key}', '${value}')`
returnthis.query(upsert)
}
delete(key){
constselect=this.options.dialect==='mysql'?`SELECT \`${this.options.table}\`.* FROM \`${this.options.table}\` WHERE (\`${this.options.table}\`.\`key\` = '${key}')`:`SELECT "${this.options.table}".* FROM "${this.options.table}" WHERE ("${this.options.table}"."key" = '${key}')`
constdel=this.options.dialect==='mysql'?`DELETE FROM \`${this.options.table}\` WHERE (\`${this.options.table}\`.\`key\` = '${key}')`:`DELETE FROM "${this.options.table}" WHERE ("${this.options.table}"."key" = '${key}')`
returnthis.query(select)
.then(rows=>{
constrow=rows[0]
if(row===undefined){
returnfalse
}
constselect=
this.options.dialect==='mysql'
?`SELECT \`${this.options.table}\`.* FROM \`${this.options.table}\` WHERE (\`${this.options.table}\`.\`key\` = '${key}')`
:`SELECT "${this.options.table}".* FROM "${this.options.table}" WHERE ("${this.options.table}"."key" = '${key}')`
constdel=
this.options.dialect==='mysql'
?`DELETE FROM \`${this.options.table}\` WHERE (\`${this.options.table}\`.\`key\` = '${key}')`
:`DELETE FROM "${this.options.table}" WHERE ("${this.options.table}"."key" = '${key}')`
returnthis.query(select).then(rows=>{
constrow=rows[0]
if(row===undefined){
returnfalse
}
returnthis.query(del)
.then(()=>true)
})
returnthis.query(del).then(()=>true)
})
}
clear(){
constdel=this.options.dialect==='mysql'?`DELETE FROM \`${this.options.table}\` WHERE (\`${this.options.table}\`.\`key\` LIKE '${this.namespace?this.namespace+':':''}%')`:`DELETE FROM "${this.options.table}" WHERE ("${this.options.table}"."key" LIKE '${this.namespace?this.namespace+':':''}%')`
returnthis.query(del)
.then(()=>undefined)
constdel=
this.options.dialect==='mysql'
?`DELETE FROM \`${this.options.table}\` WHERE (\`${
this.options.table
}\`.\`key\` LIKE '${this.namespace?this.namespace+':':''}%')`
constselectChunk=this.options.dialect==='mysql'?`SELECT * FROM \`${this.options.table}\` WHERE (\`${this.options.table}\`.\`key\` LIKE '${this.namespace?this.namespace+':':''}%') LIMIT ${limit} OFFSET `:`SELECT * FROM "${this.options.table}" WHERE ("${this.options.table}"."key" LIKE '${this.namespace?this.namespace+':':''}%') LIMIT ${limit} OFFSET `
constselectChunk=
this.options.dialect==='mysql'
?`SELECT * FROM \`${this.options.table}\` WHERE (\`${
this.options.table
}\`.\`key\` LIKE '${
this.namespace?this.namespace+':':''
}%')LIMIT${limit}OFFSET`
:`SELECT * FROM "${this.options.table}" WHERE ("${