我在运行Node的JS中构建一个简单的API。它连接到MS SQL DB以获取文章并将其作为JSON返回。这一切都很完美,但我觉得我可以更简单地编写查询。
目前我有几个if语句。有没有办法使查询动态,所以我不必为每个查询都有一个if语句?
...
app.get('/articles', function (req, res) {
// Our URL parameters
var articles = req.query.articles;
var countryID = req.query.countryID;
var offset = req.query.offset;
// Set up the database connection
var connection = new sql.Connection(db_config, function(err) {
// Log any errors to the console
if (err) console.log(err);
// Connect using prepared statement
var ps = new sql.PreparedStatement(connection);
// Our variable is an Int
ps.input('articles', sql.Int);
ps.input('countryID', sql.Int);
ps.input('offset', sql.Int);
ps.input('live', sql.VarChar(5));
// Build the query and pass in the parameters
// @articles, @countryID and @offset are empty
if (isEmpty(articles) && isEmpty(countryID) && isEmpty(offset)) {
var query = 'SELECT TOP 15 * FROM dbo.articlesCountryTown WHERE articleLive = (@live) ORDER BY PublishDate DESC';
//console.log('Using query 1');
// @articles and @offset are empty, @countryID is present
} else if (isEmpty(articles) && countryID && isEmpty(offset)) {
var query = 'SELECT TOP 15 * FROM dbo.articlesCountryTown WHERE CntryId = (@countryID) AND articleLive = (@live) ORDER BY PublishDate DESC';
//console.log('Using query 2');
// @articles is present, @countryID and @offset are empty
} else if (articles && isEmpty(countryID) && isEmpty(offset)) {
var query = 'SELECT TOP (@articles) * FROM dbo.articlesCountryTown WHERE articleLive = (@live) ORDER BY PublishDate DESC';
//console.log('Using query 3');
// @articles and @countryID are empty, @offset is present
} else if (isEmpty(articles) && isEmpty(countryID) && offset) {
var query = 'SELECT TOP 15 * FROM (select *, ROW_NUMBER() OVER (ORDER BY PublishDate DESC) as r_n_n from dbo.articlesCountryTown WHERE articleLive = (@live)) xx WHERE r_n_n >= (@offset + 1)';
//console.log('Using query 4');
// @articles and @countryID are present, @offset is empty
} else if (articles && countryID && isEmpty(offset)) {
var query = 'SELECT TOP (@articles) * FROM dbo.articlesCountryTown WHERE CntryId = (@countryID) AND articleLive = (@live) ORDER BY PublishDate DESC';
//console.log('Using query 5');
// @articles and @offset are present, @countryID is empty
} else if (articles && isEmpty(countryID) && offset) {
var query = 'SELECT TOP (@articles) * FROM (select *, ROW_NUMBER() OVER (ORDER BY PublishDate DESC) as r_n_n from dbo.articlesCountryTown WHERE articleLive = (@live)) xx WHERE r_n_n >= (@offset + 1)';
//console.log('Using query 6');
// @articles, @countryID and @offset are all present
} else {
var query = 'SELECT TOP (@articles) * FROM (select *, ROW_NUMBER() OVER (ORDER BY PublishDate DESC) as r_n_n from dbo.articlesCountryTown WHERE CntryId = (@countryID) AND articleLive = (@live)) xx WHERE r_n_n >= (@offset + 1)';
//console.log('Using query 7');
}
// Prepare the query
ps.prepare(query, function(err) {
// Log any errors to the console
if (err) console.log(err);
// Pass in the parameters and execute the query
ps.execute({articles: req.query.articles, countryID: req.query.countryID, offset: req.query.offset, live: 'true'}, function(err, recordset) {
// Return the JSON
res.json(recordset);
// Close the connection
ps.unprepare(function(err) {
// Log any errors to the console
if (err) console.log(err);
});
});
});
});
});
...
答案 0 :(得分:3)
如果你能够将空值传递给@ article,@ countryId和@offset,那么只需一个查询就可以了:
SELECT TOP (ISNULL(@articles, 15)) * FROM (
SELECT *, ROW_NUMBER() OVER (ORDER BY PublishDate DESC) as r_n_n from dbo.articlesCountryTown
WHERE CntryId = CASE WHEN @countryId IS NULL THEN CntryId ELSE @countryId END
AND articleLive = (@live)
) xx
WHERE r_n_n >= (ISNULL(@offset, 0) + 1)
更好的是,将其包装到存储过程中,参数默认为null。使用存储过程总是比在服务器级别使用字符串更好。
答案 1 :(得分:0)
尝试节点模块 node-mysql 。 node-mysql module link
示例:
DB.format('select * from users where id = ?' [userId]);