Skip to content

Commit

Permalink
refactor: add missing sumDistinct method
Browse files Browse the repository at this point in the history
  • Loading branch information
Alyxsqrd authored Nov 5, 2021
1 parent b178814 commit 3026bb1
Show file tree
Hide file tree
Showing 4 changed files with 312 additions and 0 deletions.
1 change: 1 addition & 0 deletions adonis-typings/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ declare module '@ioc:Adonis/Lucid/Orm' {
min: Aggregate<this>
max: Aggregate<this>
sum: Aggregate<this>
sumDistinct: Aggregate<this>
avg: Aggregate<this>
avgDistinct: Aggregate<this>

Expand Down
1 change: 1 addition & 0 deletions adonis-typings/querybuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,7 @@ declare module '@ioc:Adonis/Lucid/Database' {
min: Aggregate<this>
max: Aggregate<this>
sum: Aggregate<this>
sumDistinct: Aggregate<this>
avg: Aggregate<this>
avgDistinct: Aggregate<this>

Expand Down
9 changes: 9 additions & 0 deletions src/Database/QueryBuilder/Chainable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1582,6 +1582,15 @@ export abstract class Chainable extends Macroable implements ChainableContract {
return this
}

/**
* Make use of distinct `sum` aggregate function
*/
public sumDistinct(columns: any, alias?: any): this {
this.hasAggregates = true
this.knexQuery.sumDistinct(this.normalizeAggregateColumns(columns, alias))
return this
}

/**
* A shorthand for applying offset and limit based upon
* the current page
Expand Down
301 changes: 301 additions & 0 deletions test/database/query-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9556,6 +9556,307 @@ test.group('Query Builder | sum', (group) => {
})
})

test.group('Query Builder | sumDistinct', (group) => {
group.before(async () => {
app = await setupApplication()
await setup()
})

group.after(async () => {
await cleanup()
await fs.cleanup()
})

group.afterEach(async () => {
app.container.use('Adonis/Core/Event').clearListeners('db:query')
await resetTables()
})

test('use sumDistinct function', async (assert) => {
const connection = new Connection('primary', getConfig(), app.logger)
connection.connect()

let db = getQueryBuilder(getQueryClient(connection, app))
const { sql, bindings } = db.from('users').sumDistinct('*', 'sumDistinct').toSQL()

const { sql: knexSql, bindings: knexBindings } = connection
.client!.from('users')
.sumDistinct('*', { as: 'sumDistinct' })
.toSQL()

assert.equal(sql, knexSql)
assert.deepEqual(bindings, knexBindings)

db = getQueryBuilder(getQueryClient(connection, app))
db.keysResolver = (key) => `my_${key}`

const { sql: resolverSql, bindings: resolverBindings } = db
.from('users')
.sumDistinct('*', 'sumDistinct')
.toSQL()

const { sql: knexResolverSql, bindings: knexResolverBindings } = connection
.client!.from('users')
.sumDistinct('*', { as: 'sumDistinct' })
.toSQL()

assert.equal(resolverSql, knexResolverSql)
assert.deepEqual(resolverBindings, knexResolverBindings)

await connection.disconnect()
})

test('use sumDistinct function for multiple times', async (assert) => {
const connection = new Connection('primary', getConfig(), app.logger)
connection.connect()

let db = getQueryBuilder(getQueryClient(connection, app))
const { sql, bindings } = db.from('users').sumDistinct({ u: 'username', e: 'email' }).toSQL()

const { sql: knexSql, bindings: knexBindings } = connection
.client!.from('users')
.sumDistinct({ u: 'username', e: 'email' })
.toSQL()

assert.equal(sql, knexSql)
assert.deepEqual(bindings, knexBindings)

db = getQueryBuilder(getQueryClient(connection, app))
db.keysResolver = (key) => `my_${key}`

const { sql: resolverSql, bindings: resolverBindings } = db
.from('users')
.sumDistinct({ u: 'username', e: 'email' })
.toSQL()

const { sql: knexResolverSql, bindings: knexResolverBindings } = connection
.client!.from('users')
.sumDistinct({ u: 'my_username', e: 'my_email' })
.toSQL()

assert.equal(resolverSql, knexResolverSql)
assert.deepEqual(resolverBindings, knexResolverBindings)

await connection.disconnect()
})

test('use raw queries to compute sumDistinct', async (assert) => {
const connection = new Connection('primary', getConfig(), app.logger)
connection.connect()

let db = getQueryBuilder(getQueryClient(connection, app))
const { sql, bindings } = db
.from('users')
.sumDistinct(
getRawQueryBuilder(
getQueryClient(connection, app),
'select * from profiles where is_verified = ?',
[true]
),
'u'
)
.toSQL()

const { sql: knexSql, bindings: knexBindings } = connection
.client!.from('users')
.sumDistinct({
u: connection.client!.raw('select * from profiles where is_verified = ?', [true]),
})
.toSQL()

assert.equal(sql, knexSql)
assert.deepEqual(bindings, knexBindings)

db = getQueryBuilder(getQueryClient(connection, app))
db.keysResolver = (key) => `my_${key}`

const { sql: resolverSql, bindings: resolverBindings } = db
.from('users')
.sumDistinct(
getRawQueryBuilder(
getQueryClient(connection, app),
'select * from profiles where is_verified = ?',
[true]
),
'u'
)
.toSQL()

const { sql: knexResolverSql, bindings: knexResolverBindings } = connection
.client!.from('users')
.sumDistinct({
u: connection.client!.raw('select * from profiles where is_verified = ?', [true]),
})
.toSQL()

assert.equal(resolverSql, knexResolverSql)
assert.deepEqual(resolverBindings, knexResolverBindings)

await connection.disconnect()
})

test('use subqueries to compute sumDistinct', async (assert) => {
const connection = new Connection('primary', getConfig(), app.logger)
connection.connect()

let db = getQueryBuilder(getQueryClient(connection, app))
const { sql, bindings } = db
.from('users')
.sumDistinct(
getQueryBuilder(getQueryClient(connection, app))
.where('is_verified', true)
.from('profiles'),
'u'
)
.toSQL()

const { sql: knexSql, bindings: knexBindings } = connection
.client!.from('users')
.sumDistinct({
u: connection.client!.where('is_verified', true).from('profiles'),
})
.toSQL()

assert.equal(sql, knexSql)
assert.deepEqual(bindings, knexBindings)

db = getQueryBuilder(getQueryClient(connection, app))
db.keysResolver = (key) => `my_${key}`

const { sql: resolverSql, bindings: resolverBindings } = db
.from('users')
.sumDistinct(
getQueryBuilder(getQueryClient(connection, app))
.where('is_verified', true)
.from('profiles'),
'u'
)
.toSQL()

const { sql: knexResolverSql, bindings: knexResolverBindings } = connection
.client!.from('users')
.sumDistinct({
u: connection.client!.where('is_verified', true).from('profiles'),
})
.toSQL()

assert.equal(resolverSql, knexResolverSql)
assert.deepEqual(resolverBindings, knexResolverBindings)

await connection.disconnect()
})

test('use raw query to compute sumDistinct with multiple columns', async (assert) => {
const connection = new Connection('primary', getConfig(), app.logger)
connection.connect()

let db = getQueryBuilder(getQueryClient(connection, app))
const { sql, bindings } = db
.from('users')
.sumDistinct({
u: getRawQueryBuilder(
getQueryClient(connection, app),
'select * from profiles where is_verified = ?',
[true]
),
e: 'email',
})
.toSQL()

const { sql: knexSql, bindings: knexBindings } = connection
.client!.from('users')
.sumDistinct({
u: connection.client!.raw('select * from profiles where is_verified = ?', [true]),
e: 'email',
})
.toSQL()

assert.equal(sql, knexSql)
assert.deepEqual(bindings, knexBindings)

db = getQueryBuilder(getQueryClient(connection, app))
db.keysResolver = (key) => `my_${key}`

const { sql: resolverSql, bindings: resolverBindings } = db
.from('users')
.sumDistinct({
u: getRawQueryBuilder(
getQueryClient(connection, app),
'select * from profiles where is_verified = ?',
[true]
),
e: 'email',
})
.toSQL()

const { sql: knexResolverSql, bindings: knexResolverBindings } = connection
.client!.from('users')
.sumDistinct({
u: connection.client!.raw('select * from profiles where is_verified = ?', [true]),
e: 'my_email',
})
.toSQL()

assert.equal(resolverSql, knexResolverSql)
assert.deepEqual(resolverBindings, knexResolverBindings)

await connection.disconnect()
})

test('use subquery to compute sumDistinct with multiple columns', async (assert) => {
const connection = new Connection('primary', getConfig(), app.logger)
connection.connect()

let db = getQueryBuilder(getQueryClient(connection, app))
const { sql, bindings } = db
.from('users')
.sumDistinct({
u: getQueryBuilder(getQueryClient(connection, app))
.where('is_verified', true)
.from('profiles'),
e: 'email',
})
.toSQL()

const { sql: knexSql, bindings: knexBindings } = connection
.client!.from('users')
.sumDistinct({
u: connection.client!.where('is_verified', true).from('profiles'),
e: 'email',
})
.toSQL()

assert.equal(sql, knexSql)
assert.deepEqual(bindings, knexBindings)

db = getQueryBuilder(getQueryClient(connection, app))
db.keysResolver = (key) => `my_${key}`

const { sql: resolverSql, bindings: resolverBindings } = db
.from('users')
.sumDistinct({
u: getQueryBuilder(getQueryClient(connection, app))
.where('is_verified', true)
.from('profiles'),
e: 'email',
})
.toSQL()

const { sql: knexResolverSql, bindings: knexResolverBindings } = connection
.client!.from('users')
.sumDistinct({
u: connection.client!.where('is_verified', true).from('profiles'),
e: 'my_email',
})
.toSQL()

assert.equal(resolverSql, knexResolverSql)
assert.deepEqual(resolverBindings, knexResolverBindings)

await connection.disconnect()
})
})

test.group('Query Builder | avg', (group) => {
group.before(async () => {
app = await setupApplication()
Expand Down

0 comments on commit 3026bb1

Please sign in to comment.