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