|
| 1 | +// Copyright (c) 2026 Databricks, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import Int64 from 'node-int64'; |
| 16 | +import { DBSQLParameter, DBSQLParameterValue } from '../DBSQLParameter'; |
| 17 | +import ParameterError from '../errors/ParameterError'; |
| 18 | + |
| 19 | +/** |
| 20 | + * Coerce an empty-string metadata argument to `undefined`. |
| 21 | + * |
| 22 | + * The kernel's `Identifier` / `LikePattern` reject empty strings with |
| 23 | + * `InvalidArgument`, whereas the Thrift backend forwards `""` to the server |
| 24 | + * which treats it as "unspecified" (match-all / session default). To keep the |
| 25 | + * SEA metadata surface behaviourally identical to Thrift, the SEA adapter |
| 26 | + * maps `""` → `undefined` before crossing the napi boundary so the kernel |
| 27 | + * sees "argument omitted" rather than "empty identifier". |
| 28 | + */ |
| 29 | +export function emptyToUndefined(value: string | undefined | null): string | undefined { |
| 30 | + return value == null || value === '' ? undefined : value; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Walk a SQL string counting `?` parameter markers, ignoring markers inside |
| 35 | + * string literals (`'...'`, `"..."`), backtick-quoted identifiers, and |
| 36 | + * comments (`-- ...`, `/* ... */`). Mirrors the kernel's |
| 37 | + * `statement::params::count_parameter_markers` state machine so the JS-side |
| 38 | + * arity check matches what the kernel binds. |
| 39 | + */ |
| 40 | +export function countParameterMarkers(sql: string): number { |
| 41 | + let count = 0; |
| 42 | + let i = 0; |
| 43 | + const n = sql.length; |
| 44 | + type State = 'normal' | 'single' | 'double' | 'backtick' | 'line' | 'block'; |
| 45 | + let state: State = 'normal'; |
| 46 | + while (i < n) { |
| 47 | + const c = sql[i]; |
| 48 | + const next = i + 1 < n ? sql[i + 1] : ''; |
| 49 | + switch (state) { |
| 50 | + case 'normal': |
| 51 | + if (c === '?') { |
| 52 | + count += 1; |
| 53 | + } else if (c === "'") { |
| 54 | + state = 'single'; |
| 55 | + } else if (c === '"') { |
| 56 | + state = 'double'; |
| 57 | + } else if (c === '`') { |
| 58 | + state = 'backtick'; |
| 59 | + } else if (c === '-' && next === '-') { |
| 60 | + state = 'line'; |
| 61 | + i += 1; |
| 62 | + } else if (c === '/' && next === '*') { |
| 63 | + state = 'block'; |
| 64 | + i += 1; |
| 65 | + } |
| 66 | + break; |
| 67 | + case 'single': |
| 68 | + if (c === "'" && next === "'") i += 1; // escaped '' |
| 69 | + else if (c === "'") state = 'normal'; |
| 70 | + break; |
| 71 | + case 'double': |
| 72 | + if (c === '"' && next === '"') i += 1; // escaped "" |
| 73 | + else if (c === '"') state = 'normal'; |
| 74 | + break; |
| 75 | + case 'backtick': |
| 76 | + if (c === '`') state = 'normal'; |
| 77 | + break; |
| 78 | + case 'line': |
| 79 | + if (c === '\n') state = 'normal'; |
| 80 | + break; |
| 81 | + case 'block': |
| 82 | + if (c === '*' && next === '/') { |
| 83 | + state = 'normal'; |
| 84 | + i += 1; |
| 85 | + } |
| 86 | + break; |
| 87 | + } |
| 88 | + i += 1; |
| 89 | + } |
| 90 | + return count; |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Reject a parameter value that cannot be bound as a scalar. Arrays and plain |
| 95 | + * objects stringify to garbage (e.g. `[1,2,3]` → `"1,2,3"`) that the server |
| 96 | + * fails to coerce — on the Thrift path the operation never returns to |
| 97 | + * FINISHED (a DoS hazard), and on SEA it surfaces an opaque server error. We |
| 98 | + * fail fast at bind time instead, mirroring the kernel's compound-type |
| 99 | + * rejection. `DBSQLParameter`, `Int64`, `Date`, and JS primitives are allowed. |
| 100 | + */ |
| 101 | +export function assertBindableValue(value: DBSQLParameter | DBSQLParameterValue, label: string): void { |
| 102 | + if (value instanceof DBSQLParameter) return; |
| 103 | + if (value === null || value === undefined) return; |
| 104 | + if (Array.isArray(value)) { |
| 105 | + throw new ParameterError( |
| 106 | + `${label} is an array; compound types (ARRAY/MAP/STRUCT) are not bindable as a parameter value`, |
| 107 | + ); |
| 108 | + } |
| 109 | + if (typeof value === 'object' && !(value instanceof Date) && !(value instanceof Int64)) { |
| 110 | + throw new ParameterError( |
| 111 | + `${label} is an object; only scalar values (string/number/bigint/boolean), Date, and Int64 are bindable`, |
| 112 | + ); |
| 113 | + } |
| 114 | +} |
0 commit comments