Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonModel makeSelect: add disctinct #36

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/JsonModel/JM-makeSelect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,15 @@ test('sum', async () => {
await m.set({v: '8'})
expect(await m.sum('v')).toBe(13)
})

test('distinct', async () => {
const m = getModel({})
expect(m.makeSelect({distinct: true})).toEqual([
'SELECT DISTINCT tbl."id" AS _i,tbl."json" AS _j FROM "testing" tbl',
[],
undefined,
'SELECT COUNT(*) as t from ( SELECT DISTINCT tbl."id" AS _i,tbl."json" AS _j FROM "testing" tbl )',
[],
false,
])
})
16 changes: 10 additions & 6 deletions src/JsonModel/JsonModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@
'attrs',
'cols',
'cursor',
'distinct',
'join',
'joinVals',
'limit',
Expand All @@ -326,17 +327,18 @@
}
}
let {
cols,
attrs,
cols,
cursor,
distinct,
join,
joinVals,
where: extraWhere,
limit,
offset,
sort,
cursor,
noCursor,
noTotal,
offset,
sort,
where: extraWhere,
} = options

cols = cols || this.selectColNames
Expand Down Expand Up @@ -405,7 +407,9 @@
: cols
.map(c => (this.columns[c] ? this.columns[c].select : c))
.join(',')
const selectQ = `SELECT ${colsSql} FROM ${this.quoted} tbl`
const selectQ = `SELECT${distinct ? ' DISTINCT' : ''} ${colsSql} FROM ${
this.quoted
} tbl`

const vals = []
const conds = []
Expand Down Expand Up @@ -496,7 +500,7 @@
[`SELECT COUNT(*) as t from (`, selectQ, join, whereQ, ')']
.filter(Boolean)
.join(' ')
// TODO next major make this an object

Check warning on line 503 in src/JsonModel/JsonModel.js

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected 'todo' comment: 'TODO next major make this an object'
return [q, qVals, cursorColAliases, totalQ, vals, invert]
}

Expand Down Expand Up @@ -724,8 +728,8 @@
* @param {IDValue[]} ids - the values for the column.
* @param {string} [colName=this.idCol] - the columnname, defaults to the
* ID column.
* @returns {Promise<(Item | null)[]>} - the objects, or null where they don't

Check warning on line 731 in src/JsonModel/JsonModel.js

View workflow job for this annotation

GitHub Actions / lint (16.x)

Delete `·don't`
* exist, in order of their requested ID.

Check warning on line 732 in src/JsonModel/JsonModel.js

View workflow job for this annotation

GitHub Actions / lint (16.x)

Replace `exist,·in·order·of·their` with `don't·exist,·in·order·of·their⏎↹·*····································`
*/
async getAll(ids, colName = this.idCol) {
let {path, _getAllSql} = this.columns[colName]
Expand Down Expand Up @@ -798,7 +802,7 @@
* @param {JMSearchAttrs | RowCallback} attrsOrFn
* @param {RowCallback | JMSearchOptions} [optionsOrFn]
* @param {RowCallback} [fn]
* @returns {Promise<void>} Table iteration completed.

Check warning on line 805 in src/JsonModel/JsonModel.js

View workflow job for this annotation

GitHub Actions / lint (16.x)

Insert `⏎↹·*`
*/
async each(attrsOrFn, optionsOrFn, fn) {
if (!fn) {
Expand Down
Loading