Skip to content

Commit

Permalink
refactor(pglite/parse): Simplify message filtering and processing log…
Browse files Browse the repository at this point in the history
…ic in parseResults
  • Loading branch information
jeet-dhandha committed Dec 18, 2024
1 parent 9aff673 commit 8c54ae5
Showing 1 changed file with 46 additions and 39 deletions.
85 changes: 46 additions & 39 deletions packages/pglite/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,53 +22,60 @@ export function parseResults(
let currentResultSet: Results = { rows: [], fields: [] }
let affectedRows = 0
const parsers = { ...defaultParsers, ...options?.parsers }

const VALID_MESSAGE_TYPES = new Set(['rowDescription', 'dataRow', 'commandComplete'])

const filteredMessages = messages.filter(
(msg) =>
msg.name === 'rowDescription' ||
msg.name === 'dataRow' ||
msg.name === 'commandComplete',
)
(msg) => VALID_MESSAGE_TYPES.has(msg.name)
);

filteredMessages.forEach((message, index) => {
if (message.name === 'rowDescription') {
const msg = message as RowDescriptionMessage
currentResultSet.fields = msg.fields.map((field) => ({
name: field.name,
dataTypeID: field.dataTypeID,
}))
} else if (message.name === 'dataRow' && currentResultSet) {
const msg = message as DataRowMessage
if (options?.rowMode === 'array') {
currentResultSet.rows.push(
msg.fields.map((field, i) =>
parseType(field, currentResultSet!.fields[i].dataTypeID, parsers),
),
)
} else {
// rowMode === "object"
currentResultSet.rows.push(
Object.fromEntries(
msg.fields.map((field, i) => [
currentResultSet!.fields[i].name,
switch (message.name) {
case 'rowDescription': {
const msg = message as RowDescriptionMessage
currentResultSet.fields = msg.fields.map((field) => ({
name: field.name,
dataTypeID: field.dataTypeID,
}))
break
}
case 'dataRow': {
if (!currentResultSet) break
const msg = message as DataRowMessage
if (options?.rowMode === 'array') {
currentResultSet.rows.push(
msg.fields.map((field, i) =>
parseType(field, currentResultSet!.fields[i].dataTypeID, parsers),
]),
),
)
),
)
} else {
// rowMode === "object"
currentResultSet.rows.push(
Object.fromEntries(
msg.fields.map((field, i) => [
currentResultSet!.fields[i].name,
parseType(field, currentResultSet!.fields[i].dataTypeID, parsers),
]),
),
)
}
break
}
} else if (message.name === 'commandComplete') {
const msg = message as CommandCompleteMessage
affectedRows += retrieveRowCount(msg)
case 'commandComplete': {
const msg = message as CommandCompleteMessage
affectedRows += retrieveRowCount(msg)

if (index === filteredMessages.length - 1)
resultSets.push({
...currentResultSet,
affectedRows,
...(blob ? { blob } : {}),
})
else resultSets.push(currentResultSet)
if (index === filteredMessages.length - 1)
resultSets.push({
...currentResultSet,
affectedRows,
...(blob ? { blob } : {}),
})
else resultSets.push(currentResultSet)

currentResultSet = { rows: [], fields: [] }
currentResultSet = { rows: [], fields: [] }
break
}
}
})

Expand Down

0 comments on commit 8c54ae5

Please sign in to comment.