rapid-db
Version:
Rapid Database Interface
56 lines (52 loc) • 1.54 kB
JavaScript
/**
* Copyright (c) 2017 Lucky Byte, Inc.
*/
const debug = require('debug')('db');
/**
* 查询卡 BIN 数据
*
* 参数
* card_no: 最多2位数字序号类型,用于增加特殊性(可选)
* db: 数据库对象,如果为空,则使用全局 _unique_db
* 返回
* 卡 BIN 记录
*/
const query = async (card_no, db) => {
db = db || _unique_db;
if (!db) {
throw new Error('数据库对象无效,检查是否已初始化数据库');
}
if (!card_no || card_no.length < 10) {
throw new Error('card_no 参数无效,无法查询卡 BIN 信息');
}
for (let len = 10; len >= 5; len--) {
const bin_num = card_no.substr(0, len);
try {
const result = await db.oneOrNone(`
select *,
case card_table
when 1 then 4
when 2 then 3
when 3 then 1
when 4 then 2
end as ordering
from rapid_cardbin
where bin_len = $1 and bin_num = $2
order by ordering limit 1
`, [
len, bin_num
]);
// 未查询到结果,继续尝试下一种可能
if (!result) {
continue;
}
return result;
} catch (err) {
throw new Error(`查询卡 BIN 数据错: ${err.message}`);
}
}
return null;
}
module.exports = {
query: query
}