-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/*! | ||
* This source file is part of the EdgeDB open source project. | ||
* | ||
* Copyright 2019-present MagicStack Inc. and the EdgeDB authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import type { ICodec, uuid, CodecKind } from "./ifaces"; | ||
import { Codec } from "./ifaces"; | ||
import { ReadBuffer, WriteBuffer } from "../primitives/buffer"; | ||
Check failure on line 21 in packages/driver/src/codecs/record.ts GitHub Actions / test (18, ubuntu-latest, stable)
Check failure on line 21 in packages/driver/src/codecs/record.ts GitHub Actions / test (20, ubuntu-latest, stable)
Check failure on line 21 in packages/driver/src/codecs/record.ts GitHub Actions / test (22, ubuntu-latest, stable)
Check failure on line 21 in packages/driver/src/codecs/record.ts GitHub Actions / test (ubuntu-latest, 20, nightly)
Check failure on line 21 in packages/driver/src/codecs/record.ts GitHub Actions / test (ubuntu-latest, 20, 3)
|
||
import { | ||
InvalidArgumentError, | ||
ProtocolError, | ||
} from "../errors"; | ||
|
||
export class RecordCodec extends Codec implements ICodec { | ||
private subCodecs: ICodec[]; | ||
private names: string[]; | ||
|
||
constructor( | ||
tid: uuid, | ||
codecs: ICodec[], | ||
names: string[], | ||
) { | ||
super(tid); | ||
this.subCodecs = codecs; | ||
this.names = names; | ||
} | ||
|
||
encode(buf: WriteBuffer, object: any): void { | ||
Check failure on line 41 in packages/driver/src/codecs/record.ts GitHub Actions / test (18, ubuntu-latest, stable)
Check failure on line 41 in packages/driver/src/codecs/record.ts GitHub Actions / test (18, ubuntu-latest, stable)
Check failure on line 41 in packages/driver/src/codecs/record.ts GitHub Actions / test (20, ubuntu-latest, stable)
Check failure on line 41 in packages/driver/src/codecs/record.ts GitHub Actions / test (20, ubuntu-latest, stable)
Check failure on line 41 in packages/driver/src/codecs/record.ts GitHub Actions / test (22, ubuntu-latest, stable)
Check failure on line 41 in packages/driver/src/codecs/record.ts GitHub Actions / test (22, ubuntu-latest, stable)
Check failure on line 41 in packages/driver/src/codecs/record.ts GitHub Actions / test (ubuntu-latest, 20, nightly)
Check failure on line 41 in packages/driver/src/codecs/record.ts GitHub Actions / test (ubuntu-latest, 20, nightly)
Check failure on line 41 in packages/driver/src/codecs/record.ts GitHub Actions / test (ubuntu-latest, 20, 3)
Check failure on line 41 in packages/driver/src/codecs/record.ts GitHub Actions / test (ubuntu-latest, 20, 3)
Check failure on line 41 in packages/driver/src/codecs/record.ts GitHub Actions / test (ubuntu-latest, 20, 2)
|
||
throw new InvalidArgumentError( | ||
"SQL records cannot be passed as arguments"); | ||
} | ||
|
||
decode(buf: ReadBuffer): any { | ||
const els = buf.readUInt32(); | ||
const subCodecs = this.subCodecs; | ||
if (els !== subCodecs.length) { | ||
throw new ProtocolError( | ||
`cannot decode Record: expected ` + | ||
`${subCodecs.length} elements, got ${els}`, | ||
); | ||
} | ||
|
||
const elemBuf = ReadBuffer.alloc(); | ||
const result: any[] = new Array(els); | ||
for (let i = 0; i < els; i++) { | ||
buf.discard(4); // reserved | ||
const elemLen = buf.readInt32(); | ||
let val = null; | ||
if (elemLen !== -1) { | ||
buf.sliceInto(elemBuf, elemLen); | ||
val = subCodecs[i].decode(elemBuf); | ||
elemBuf.finish(); | ||
} | ||
result[i] = val; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
getSubcodecs(): ICodec[] { | ||
return Array.from(this.subCodecs); | ||
} | ||
|
||
getNames(): string[] { | ||
return Array.from(this.names); | ||
} | ||
|
||
getKind(): CodecKind { | ||
return "record"; | ||
} | ||
} |