1 line
7.3 KiB
JSON
1 line
7.3 KiB
JSON
|
{"ast":null,"code":"import { sqlToProto } from \"./sql.js\";\nimport { valueToProto } from \"./value.js\";\n/** A statement that can be evaluated by the database. Besides the SQL text, it also contains the positional\n * and named arguments. */\nexport class Stmt {\n /** The SQL statement text. */\n sql;\n /** @private */\n _args;\n /** @private */\n _namedArgs;\n /** Initialize the statement with given SQL text. */\n constructor(sql) {\n this.sql = sql;\n this._args = [];\n this._namedArgs = new Map();\n }\n /** Binds positional parameters from the given `values`. All previous positional bindings are cleared. */\n bindIndexes(values) {\n this._args.length = 0;\n for (const value of values) {\n this._args.push(valueToProto(value));\n }\n return this;\n }\n /** Binds a parameter by a 1-based index. */\n bindIndex(index, value) {\n if (index !== (index | 0) || index <= 0) {\n throw new RangeError(\"Index of a positional argument must be positive integer\");\n }\n while (this._args.length < index) {\n this._args.push(null);\n }\n this._args[index - 1] = valueToProto(value);\n return this;\n }\n /** Binds a parameter by name. */\n bindName(name, value) {\n this._namedArgs.set(name, valueToProto(value));\n return this;\n }\n /** Clears all bindings. */\n unbindAll() {\n this._args.length = 0;\n this._namedArgs.clear();\n return this;\n }\n}\nexport function stmtToProto(sqlOwner, stmt, wantRows) {\n let inSql;\n let args = [];\n let namedArgs = [];\n if (stmt instanceof Stmt) {\n inSql = stmt.sql;\n args = stmt._args;\n for (const [name, value] of stmt._namedArgs.entries()) {\n namedArgs.push({\n name,\n value\n });\n }\n } else if (Array.isArray(stmt)) {\n inSql = stmt[0];\n if (Array.isArray(stmt[1])) {\n args = stmt[1].map(arg => valueToProto(arg));\n } else {\n namedArgs = Object.entries(stmt[1]).map(([name, value]) => {\n return {\n name,\n value: valueToProto(value)\n };\n });\n }\n } else {\n inSql = stmt;\n }\n const {\n sql,\n sqlId\n } = sqlToProto(sqlOwner, inSql);\n return {\n sql,\n sqlId,\n args,\n namedArgs,\n wantRows\n };\n}","map":{"version":3,"names":["sqlToProto","valueToProto","Stmt","sql","_args","_namedArgs","constructor","Map","bindIndexes","values","length","value","push","bindIndex","index","RangeError","bindName","name","set","unbindAll","clear","stmtToProto","sqlOwner","stmt","wantRows","inSql","args","namedArgs","entries","Array","isArray","map","arg","Object","sqlId"],"sources":["/Users/shoofle/Projects/the-forest/node_modules/@libsql/hrana-client/lib-esm/stmt.js"],"sourcesContent":["import { sqlToProto } from \"./sql.js\";\nimport { valueToProto } from \"./value.js\";\n/** A statement that can be evaluated by the database. Besides the SQL text, it also contains the positional\n * and named arguments. */\nexport class Stmt {\n /** The SQL statement text. */\n sql;\n /** @private */\n _args;\n /** @private */\n _namedArgs;\n /** Initialize the statement with given SQL text. */\n constructor(sql) {\n this.sql = sql;\n this._args = [];\n this._namedArgs = new Map();\n }\n /** Binds positional parameters from the given `values`. All previous positional bindings are cleared. */\n bindIndexes(values) {\n this._args.length = 0;\n for (const value of values) {\n this._args.push(valueToProto(value));\n }\n return this;\n }\n /** Binds a parameter by a 1-based index. */\n bindIndex(index, value) {\n if (index !== (index | 0) || index <= 0) {\n throw new RangeError(\"Index of a positional argument must be positive integer\");\n }\n while (this._args.length < index) {\n this._args.push(null);\n }\n this._args[index - 1] = valueToProto(value);\n return this;\n }\n /** Binds a parameter by name. */\n bindName(name, value)
|