Skip to content

Commit 964964c

Browse files
committed
fix: order with empty array
1 parent e8d8056 commit 964964c

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

lib/operator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ proto._orders = function (orders) {
242242
}
243243
}
244244
}
245-
return ' ORDER BY ' + values.join(', ');
245+
return values.length ? ' ORDER BY ' + values.join(', ') : '';
246246
};
247247

248248
proto._limit = function (limit, offset) {

test/operator.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,23 @@ describe('operator.test.js', function () {
6868
}
6969
});
7070
});
71+
72+
describe('_orders()', function () {
73+
it('should get order sql', function* () {
74+
let op = new Operator();
75+
assert.equal(op._orders(), '');
76+
assert.equal(op._orders([]), '');
77+
assert.equal(op._orders([null]), '');
78+
assert.equal(op._orders([function() {}]), '');
79+
assert.equal(op._orders(['id']), ' ORDER BY `id`');
80+
assert.equal(op._orders(['test.id']), ' ORDER BY `test`.`id`');
81+
assert.equal(op._orders(['id', 'name']), ' ORDER BY `id`, `name`');
82+
assert.equal(op._orders(['id', null]), ' ORDER BY `id`');
83+
assert.equal(op._orders(['id', ['name', 'desc']]), ' ORDER BY `id`, `name` DESC');
84+
assert.equal(op._orders(['id', ['name', 'ASC']]), ' ORDER BY `id`, `name` ASC');
85+
assert.equal(op._orders(['id', ['name', 'other']]), ' ORDER BY `id`, `name`');
86+
assert.equal(op._orders([['id', 'asc'], ['name', 'desc']]), ' ORDER BY `id` ASC, `name` DESC');
87+
assert.equal(op._orders([['id', 'asc'], ['name'], 'type']), ' ORDER BY `id` ASC, `name`, `type`');
88+
});
89+
});
7190
});

0 commit comments

Comments
 (0)