MCPcopy Create free account
hub / github.com/bytebase/bytebase / rowValueToSQLLiteral

Function rowValueToSQLLiteral

frontend/src/utils/v1/sql.ts:271–319  ·  view source on GitHub ↗
(
  value: RowValue | undefined,
  engine: Engine
)

Source from the content-addressed store, hash-verified

269// stay bare, strings/JSON/timestamps are single-quoted and escaped, booleans
270// follow the engine convention, and NULL is emitted for null/unset cells.
271export const rowValueToSQLLiteral = (
272 value: RowValue | undefined,
273 engine: Engine
274): string => {
275 if (value === undefined || value.kind?.case === undefined) {
276 return "NULL";
277 }
278 const { kind } = value;
279 switch (kind.case) {
280 case "nullValue":
281 return "NULL";
282 case "boolValue":
283 if (isMySQLFamilyEngine(engine)) {
284 return kind.value ? "1" : "0";
285 }
286 return kind.value ? "TRUE" : "FALSE";
287 case "int32Value":
288 case "uint32Value":
289 case "doubleValue":
290 case "floatValue":
291 return String(kind.value);
292 case "int64Value":
293 case "uint64Value":
294 return kind.value.toString();
295 case "stringValue":
296 return escapeSQLStringLiteral(kind.value, engine);
297 case "bytesValue": {
298 const hex = bytesToHex(kind.value);
299 // Postgres-family bytea literal vs MySQL/MSSQL 0x literal.
300 if (
301 engine === Engine.POSTGRES ||
302 engine === Engine.COCKROACHDB ||
303 engine === Engine.REDSHIFT
304 ) {
305 return `'\\x${hex}'`;
306 }
307 return `0x${hex}`;
308 }
309 default: {
310 // valueValue (JSON / composite), timestamps, and anything else fall
311 // back to their plain display form, single-quoted.
312 const plain = extractSQLRowValuePlain(value);
313 if (plain === null || plain === undefined) {
314 return "NULL";
315 }
316 return escapeSQLStringLiteral(String(plain), engine);
317 }
318 }
319};
320
321// generateInsertStatementFromRows builds a single batched INSERT statement for
322// the given rows. Each `rows[i]` is the cell array of one result row, aligned

Callers 2

sql.test.tsFile · 0.90

Calls 5

isMySQLFamilyEngineFunction · 0.85
escapeSQLStringLiteralFunction · 0.85
bytesToHexFunction · 0.85
extractSQLRowValuePlainFunction · 0.85
toStringMethod · 0.65

Tested by

no test coverage detected