Skip to content
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
10 changes: 9 additions & 1 deletion d1.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,16 @@ export class D1 {
}

processCol(col, knownTables = [], prefix = null) {
if (typeof col !== 'string') return col
let q0 = col

// Preserve leading unary operators (e.g. '+threads.showAt' or '-threads.score')
let unary = ''
if ((q0.startsWith('+') || q0.startsWith('-')) && q0.length > 1) {
unary = q0[0]
q0 = q0.slice(1)
}

// Auto prefix
if (prefix && !(q0.includes('$') || q0.includes('('))) {
if (!q0.includes('.')) {
Expand Down Expand Up @@ -378,7 +386,7 @@ export class D1 {
q0 = `json_extract(${split[0]}, '$.${split.slice(1).join('.')}')`
}
}
return q0
return unary + q0
}

singleW(q2, knownTables = [], prefix = null) {
Expand Down
28 changes: 1 addition & 27 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions test/test_order.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,46 @@ test('test multiple order clauses', async () => {
expect(r4.users[1].email).toBe(`order_a1_${timestamp}@example.com`)
expect(r4.users[2].email).toBe(`order_b1_${timestamp}@example.com`)
expect(r4.users[3].email).toBe(`order_a2_${timestamp}@example.com`)

// 5. Test unary + prefix ordering with table qualifier and without
Comment thread
treeder marked this conversation as resolved.
let r5 = await c.api.fetch(`/v1/users/query`, {
method: 'POST',
body: {
where: [['email', 'LIKE', `order_%_${timestamp}@example.com`]],
order: ['+users.name', 'asc'],
},
})
expect(r5.users).toBeDefined()
expect(r5.users.length).toBe(4)
expect(r5.users[0].email).toBe(`order_a1_${timestamp}@example.com`)
expect(r5.users[1].email).toBe(`order_a2_${timestamp}@example.com`)

// 6. Test expression with parentheses
let r6 = await c.api.fetch(`/v1/users/query`, {
method: 'POST',
body: {
where: [['email', 'LIKE', `order_%_${timestamp}@example.com`]],
order: ['(+users.name)', 'asc'],
},
})
expect(r6.users).toBeDefined()
expect(r6.users.length).toBe(4)
expect(r6.users[0].email).toBe(`order_a1_${timestamp}@example.com`)
expect(r6.users[1].email).toBe(`order_a2_${timestamp}@example.com`)
})

test('test processCol with unary operators', async () => {
const { D1 } = await import('../d1.js')
const d1 = new D1({})

// Qualified table with unary +
expect(d1.processCol('+threads.showAt', ['threads'], 'threads')).toBe('+threads.showAt')
// Unqualified column with unary + and prefix
expect(d1.processCol('+showAt', ['threads'], 'threads')).toBe('+threads.showAt')
// Qualified table with unary -
expect(d1.processCol('-threads.showAt', ['threads'], 'threads')).toBe('-threads.showAt')
// Parenthesized expression
expect(d1.processCol('(+threads.showAt)', ['threads'], 'threads')).toBe('(+threads.showAt)')
// JSON subfield with unary +
expect(d1.processCol('+data.priority', ['users'], 'users')).toBe("+json_extract(users.data, '$.priority')")
})
Loading