1 line
28 KiB
JSON
1 line
28 KiB
JSON
|
{"ast":null,"code":"import { ProtoError, MisuseError } from \"./errors.js\";\nimport { stmtResultFromProto, rowsResultFromProto, rowResultFromProto, valueResultFromProto, errorFromProto } from \"./result.js\";\nimport { stmtToProto } from \"./stmt.js\";\nimport { impossible } from \"./util.js\";\n/** A builder for creating a batch and executing it on the server. */\nexport class Batch {\n /** @private */\n _stream;\n #useCursor;\n /** @private */\n _steps;\n #executed;\n /** @private */\n constructor(stream, useCursor) {\n this._stream = stream;\n this.#useCursor = useCursor;\n this._steps = [];\n this.#executed = false;\n }\n /** Return a builder for adding a step to the batch. */\n step() {\n return new BatchStep(this);\n }\n /** Execute the batch. */\n execute() {\n if (this.#executed) {\n throw new MisuseError(\"This batch has already been executed\");\n }\n this.#executed = true;\n const batch = {\n steps: this._steps.map(step => step.proto)\n };\n if (this.#useCursor) {\n return executeCursor(this._stream, this._steps, batch);\n } else {\n return executeRegular(this._stream, this._steps, batch);\n }\n }\n}\nfunction executeRegular(stream, steps, batch) {\n return stream._batch(batch).then(result => {\n for (let step = 0; step < steps.length; ++step) {\n const stepResult = result.stepResults.get(step);\n const stepError = result.stepErrors.get(step);\n steps[step].callback(stepResult, stepError);\n }\n });\n}\nasync function executeCursor(stream, steps, batch) {\n const cursor = await stream._openCursor(batch);\n try {\n let nextStep = 0;\n let beginEntry = undefined;\n let rows = [];\n for (;;) {\n const entry = await cursor.next();\n if (entry === undefined) {\n break;\n }\n if (entry.type === \"step_begin\") {\n if (entry.step < nextStep || entry.step >= steps.length) {\n throw new ProtoError(\"Server produced StepBeginEntry for unexpected step\");\n } else if (beginEntry !== undefined) {\n throw new ProtoError(\"Server produced StepBeginEntry before terminating previous step\");\n }\n for (let step = nextStep; step < entry.step; ++step) {\n steps[step].callback(undefined, undefined);\n }\n nextStep = entry.step + 1;\n beginEntry = entry;\n rows = [];\n } else if (entry.type === \"step_end\") {\n if (beginEntry === undefined) {\n throw new ProtoError(\"Server produced StepEndEntry but no step is active\");\n }\n const stmtResult = {\n cols: beginEntry.cols,\n rows,\n affectedRowCount: entry.affectedRowCount,\n lastInsertRowid: entry.lastInsertRowid\n };\n steps[beginEntry.step].callback(stmtResult, undefined);\n beginEntry = undefined;\n rows = [];\n } else if (entry.type === \"step_error\") {\n if (beginEntry === undefined) {\n if (entry.step >= steps.length) {\n throw new ProtoError(\"Server produced StepErrorEntry for unexpected step\");\n }\n for (let step = nextStep; step < entry.step; ++step) {\n steps[step].callback(undefined, undefined);\n }\n } else {\n if (entry.step !== beginEntry.step) {\n throw new ProtoError(\"Server produced StepErrorEntry for unexpected step\");\n }\n beginEntry = undefined;\n rows = [];\n }\n steps[entry.step].callback(undefined, entry.error);\n nextStep = entry.step + 1;\n } else if (entry.type === \"row\") {\n if (beginEntry === undefined) {\n throw new ProtoError(\"Server produced RowEntry but no step is active\");\n }\n rows.push(entry.row);\n } else if (entry.type === \"error\") {\n throw errorFromProto(entry.error);\n } else if (entry.type === \"none\") {\n throw new ProtoError(\"Server produced unrecognized CursorEntry\");\n } else {\n throw impo
|