@gjsify/sqlite 0.3.21 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ var e=Object.defineProperty,__name=(t,n)=>e(t,`name`,{value:n,configurable:!0});export{__name};
@@ -1 +1 @@
1
- import{OutOfRangeError as e}from"./errors.js";function convertValue(t,n){if(t==null)return null;if(typeof t==`number`){if(Number.isInteger(t)&&!Number.isSafeInteger(t)){if(!n)throw new e(`Value is too large to be represented as a JavaScript number: ${t}`);return BigInt(t)}return n&&Number.isInteger(t)?BigInt(t):t}if(typeof t==`bigint`){if(!n){if(t>BigInt(2**53-1)||t<BigInt(-(2**53-1)))throw new e(`Value is too large to be represented as a JavaScript number: ${t}`);return Number(t)}return t}return typeof t==`string`||t instanceof Uint8Array?t:t&&typeof t.toArray==`function`?new Uint8Array(t.toArray()):t}function readRow(e,t,n){let r=e.get_n_columns();if(n.returnArrays){let i=[];for(let a=0;a<r;a++){let r=e.get_value_at(a,t);i.push(convertValue(r,n.readBigInts))}return i}let i=Object.create(null);for(let a=0;a<r;a++){let r=e.get_column_name(a);i[r]=convertValue(e.get_value_at(a,t),n.readBigInts)}return i}function readAllRows(e,t){let n=e.get_n_rows(),r=[];for(let i=0;i<n;i++){let n=readRow(e,i,t);n!==void 0&&r.push(n)}return r}function readFirstRow(e,t){if(e.get_n_rows()!==0)return readRow(e,0,t)}export{readAllRows,readFirstRow,readRow};
1
+ import"./_virtual/_rolldown/runtime.js";import{OutOfRangeError as e}from"./errors.js";function convertValue(t,n){if(t==null)return null;if(typeof t==`number`){if(Number.isInteger(t)&&!Number.isSafeInteger(t)){if(!n)throw new e(`Value is too large to be represented as a JavaScript number: ${t}`);return BigInt(t)}return n&&Number.isInteger(t)?BigInt(t):t}if(typeof t==`bigint`){if(!n){if(t>BigInt(2**53-1)||t<BigInt(-(2**53-1)))throw new e(`Value is too large to be represented as a JavaScript number: ${t}`);return Number(t)}return t}return typeof t==`string`||t instanceof Uint8Array?t:t&&typeof t.toArray==`function`?new Uint8Array(t.toArray()):t}function readRow(e,t,n){let r=e.get_n_columns();if(n.returnArrays){let i=[];for(let a=0;a<r;a++){let r=e.get_value_at(a,t);i.push(convertValue(r,n.readBigInts))}return i}let i=Object.create(null);for(let a=0;a<r;a++){let r=e.get_column_name(a);i[r]=convertValue(e.get_value_at(a,t),n.readBigInts)}return i}function readAllRows(e,t){let n=e.get_n_rows(),r=[];for(let i=0;i<n;i++){let n=readRow(e,i,t);n!==void 0&&r.push(n)}return r}function readFirstRow(e,t){if(e.get_n_rows()!==0)return readRow(e,0,t)}export{readAllRows,readFirstRow,readRow};
@@ -1 +1 @@
1
- import{ConstructCallRequiredError as e,InvalidArgTypeError as t,InvalidStateError as n,InvalidUrlSchemeError as r,SqliteError as i}from"./errors.js";import{StatementSync as a}from"./statement-sync.js";import o from"@girs/gda-6.0";const s=Symbol.for(`sqlite-type`);function parsePath(e){if(typeof e==`string`){if(e.includes(`\0`))throw new t(`The "path" argument must be a string, Uint8Array, or URL without null bytes.`);return e}if(e instanceof URL){if(e.protocol!==`file:`)throw new r;let n=e.pathname;if(n.includes(`\0`))throw new t(`The "path" argument must be a string, Uint8Array, or URL without null bytes.`);return n}if(e instanceof Uint8Array){for(let n=0;n<e.length;n++)if(e[n]===0)throw new t(`The "path" argument must be a string, Uint8Array, or URL without null bytes.`);return new TextDecoder().decode(e)}throw new t(`The "path" argument must be a string, Uint8Array, or URL without null bytes.`)}function validateOptions(e){if(e===void 0)return{};if(typeof e!=`object`||!e)throw new t(`The "options" argument must be an object.`);let n=e,r={};if(n.open!==void 0){if(typeof n.open!=`boolean`)throw new t(`The "options.open" argument must be a boolean.`);r.open=n.open}if(n.readOnly!==void 0){if(typeof n.readOnly!=`boolean`)throw new t(`The "options.readOnly" argument must be a boolean.`);r.readOnly=n.readOnly}if(n.timeout!==void 0){if(typeof n.timeout!=`number`||!Number.isInteger(n.timeout))throw new t(`The "options.timeout" argument must be an integer.`);r.timeout=n.timeout}if(n.enableForeignKeyConstraints!==void 0){if(typeof n.enableForeignKeyConstraints!=`boolean`)throw new t(`The "options.enableForeignKeyConstraints" argument must be a boolean.`);r.enableForeignKeyConstraints=n.enableForeignKeyConstraints}if(n.enableDoubleQuotedStringLiterals!==void 0){if(typeof n.enableDoubleQuotedStringLiterals!=`boolean`)throw new t(`The "options.enableDoubleQuotedStringLiterals" argument must be a boolean.`);r.enableDoubleQuotedStringLiterals=n.enableDoubleQuotedStringLiterals}if(n.readBigInts!==void 0){if(typeof n.readBigInts!=`boolean`)throw new t(`The "options.readBigInts" argument must be a boolean.`);r.readBigInts=n.readBigInts}if(n.returnArrays!==void 0){if(typeof n.returnArrays!=`boolean`)throw new t(`The "options.returnArrays" argument must be a boolean.`);r.returnArrays=n.returnArrays}if(n.allowBareNamedParameters!==void 0){if(typeof n.allowBareNamedParameters!=`boolean`)throw new t(`The "options.allowBareNamedParameters" argument must be a boolean.`);r.allowBareNamedParameters=n.allowBareNamedParameters}if(n.allowUnknownNamedParameters!==void 0){if(typeof n.allowUnknownNamedParameters!=`boolean`)throw new t(`The "options.allowUnknownNamedParameters" argument must be a boolean.`);r.allowUnknownNamedParameters=n.allowUnknownNamedParameters}if(n.defensive!==void 0){if(typeof n.defensive!=`boolean`)throw new t(`The "options.defensive" argument must be a boolean.`);r.defensive=n.defensive}if(n.allowExtension!==void 0){if(typeof n.allowExtension!=`boolean`)throw new t(`The "options.allowExtension" argument must be a boolean.`);r.allowExtension=n.allowExtension}return r}function convertParameterSyntax(e){let t=[],n=0,r=``,i=0;for(;i<e.length;){if(e[i]===`'`){let t=i;for(i++;i<e.length&&e[i]!==`'`;){if(e[i]===`'`&&e[i+1]===`'`){i+=2;continue}i++}i<e.length&&i++,r+=e.substring(t,i);continue}if(e[i]===`?`){i++;let a=``;for(;i<e.length&&e[i]>=`0`&&e[i]<=`9`;)a+=e[i],i++;let o=a?parseInt(a,10)-1:n;n=a?n:n+1;let s=`p${o}`;t.push({gdaId:s,originalName:a?`?${a}`:`?`,position:o}),r+=`##${s}::string`;continue}if((e[i]===`$`||e[i]===`:`||e[i]===`@`)&&i+1<e.length&&/[a-zA-Z_]/.test(e[i+1])){let n=e[i];i++;let a=``;for(;i<e.length&&/[a-zA-Z0-9_]/.test(e[i]);)a+=e[i],i++;let o=a;t.push({gdaId:o,originalName:`${n}${a}`,position:-1}),r+=`##${o}::string`;continue}r+=e[i],i++}return[r,t]}var DatabaseSync=class{#e=null;#t=null;#n;#r;#i;#a=!1;constructor(t,n){if(!new.target)throw new e(`DatabaseSync`);this.#n=parsePath(t),this.#r=validateOptions(n),this.#i=this.#n===`:memory:`,this.#r.open!==!1&&this.open()}get[s](){return`node:sqlite`}get isOpen(){return this.#e!==null&&this.#e.is_opened()}get isTransaction(){return this.#o(),this.#a}open(){if(this.isOpen)throw new n(`database is already open`);try{if(this.#i)this.#e=o.Connection.new_from_string(`SQLite`,`DB_DIR=;DB_NAME=:memory:`,null,o.ConnectionOptions.NONE);else{let e=this.#n.lastIndexOf(`/`),t=`DB_DIR=${e>=0?this.#n.substring(0,e):`.`};DB_NAME=${e>=0?this.#n.substring(e+1):this.#n}`,n=this.#r.readOnly?o.ConnectionOptions.READ_ONLY:o.ConnectionOptions.NONE;this.#e=o.Connection.new_from_string(`SQLite`,t,null,n)}this.#e.open()}catch(e){throw this.#e=null,new i(e instanceof Error?e.message:String(e))}this.#t=this.#e.create_parser()??new o.SqlParser,this.#s()}close(){if(!this.isOpen)throw new n(`database is not open`);this.#e.close(),this.#e=null,this.#t=null,this.#a=!1}sqlExec(e){if(this.#o(),typeof e!=`string`)throw new t(`The "sql" argument must be a string.`);try{let t=this.#l(e);for(let e of t){let[t]=this.#t.parse_string(e);t&&this.#c(t)}}catch(e){throw e instanceof i||e instanceof n||e instanceof t?e:new i(e instanceof Error?e.message:String(e))}this.#u(e)}prepare(e,n){if(this.#o(),typeof e!=`string`)throw new t(`The "sql" argument must be a string.`);let[,r]=convertParameterSyntax(e);try{let t=r.length>0?e.replace(/\?(\d+)?/g,`NULL`).replace(/[\$:@][a-zA-Z_][a-zA-Z0-9_]*/g,`NULL`):e,[n]=this.#t.parse_string(t);if(!n)throw new i(`Failed to parse SQL statement`)}catch(e){throw e instanceof i||e instanceof t?e:new i(e instanceof Error?e.message:String(e))}let o={readBigInts:this.#r.readBigInts??!1,returnArrays:this.#r.returnArrays??!1,allowBareNamedParameters:this.#r.allowBareNamedParameters??!0,allowUnknownNamedParameters:this.#r.allowUnknownNamedParameters??!1};return a._create(this.#e,e,o,r,this.#t)}location(e){if(this.#o(),e!==void 0&&typeof e!=`string`)throw new t(`The "dbName" argument must be a string.`);return this.#i?null:this.#n}[Symbol.dispose](){if(this.isOpen)try{this.close()}catch{}}#o(){if(!this.isOpen)throw new n(`database is not open`)}#s(){let e=this.#e,runPragma=t=>{let[n]=this.#t.parse_string(t);n&&e.statement_execute_select(n,null)};this.#r.enableForeignKeyConstraints===!1?runPragma(`PRAGMA foreign_keys = OFF`):runPragma(`PRAGMA foreign_keys = ON`),this.#r.timeout!==void 0&&this.#r.timeout>0&&runPragma(`PRAGMA busy_timeout = ${this.#r.timeout}`)}#c(e){if(e.get_statement_type()===o.SqlStatementType.SELECT)this.#e.statement_execute_select(e,null);else try{this.#e.statement_execute_non_select(e,null)}catch{this.#e.statement_execute_select(e,null)}}#l(e){let t=[],n=``,r=!1;for(let i=0;i<e.length;i++){let a=e[i];if(a===`'`&&!r)r=!0,n+=a;else if(a===`'`&&r)e[i+1]===`'`?(n+=`''`,i++):(r=!1,n+=a);else if(a===`;`&&!r){let e=n.trim();e.length>0&&t.push(e),n=``}else n+=a}let i=n.trim();return i.length>0&&t.push(i),t}#u(e){let t=e.trim().toUpperCase();/\bBEGIN\b/.test(t)&&(this.#a=!0),(/\bCOMMIT\b/.test(t)||/\bROLLBACK\b/.test(t))&&(this.#a=!1)}};DatabaseSync.prototype.exec=DatabaseSync.prototype.sqlExec;export{DatabaseSync};
1
+ import"./_virtual/_rolldown/runtime.js";import{ConstructCallRequiredError as e,InvalidArgTypeError as t,InvalidStateError as n,InvalidUrlSchemeError as r,SqliteError as i}from"./errors.js";import{StatementSync as a}from"./statement-sync.js";import o from"@girs/gda-6.0";const s=Symbol.for(`sqlite-type`);function parsePath(e){if(typeof e==`string`){if(e.includes(`\0`))throw new t(`The "path" argument must be a string, Uint8Array, or URL without null bytes.`);return e}if(e instanceof URL){if(e.protocol!==`file:`)throw new r;let n=e.pathname;if(n.includes(`\0`))throw new t(`The "path" argument must be a string, Uint8Array, or URL without null bytes.`);return n}if(e instanceof Uint8Array){for(let n=0;n<e.length;n++)if(e[n]===0)throw new t(`The "path" argument must be a string, Uint8Array, or URL without null bytes.`);return new TextDecoder().decode(e)}throw new t(`The "path" argument must be a string, Uint8Array, or URL without null bytes.`)}function validateOptions(e){if(e===void 0)return{};if(typeof e!=`object`||!e)throw new t(`The "options" argument must be an object.`);let n=e,r={};if(n.open!==void 0){if(typeof n.open!=`boolean`)throw new t(`The "options.open" argument must be a boolean.`);r.open=n.open}if(n.readOnly!==void 0){if(typeof n.readOnly!=`boolean`)throw new t(`The "options.readOnly" argument must be a boolean.`);r.readOnly=n.readOnly}if(n.timeout!==void 0){if(typeof n.timeout!=`number`||!Number.isInteger(n.timeout))throw new t(`The "options.timeout" argument must be an integer.`);r.timeout=n.timeout}if(n.enableForeignKeyConstraints!==void 0){if(typeof n.enableForeignKeyConstraints!=`boolean`)throw new t(`The "options.enableForeignKeyConstraints" argument must be a boolean.`);r.enableForeignKeyConstraints=n.enableForeignKeyConstraints}if(n.enableDoubleQuotedStringLiterals!==void 0){if(typeof n.enableDoubleQuotedStringLiterals!=`boolean`)throw new t(`The "options.enableDoubleQuotedStringLiterals" argument must be a boolean.`);r.enableDoubleQuotedStringLiterals=n.enableDoubleQuotedStringLiterals}if(n.readBigInts!==void 0){if(typeof n.readBigInts!=`boolean`)throw new t(`The "options.readBigInts" argument must be a boolean.`);r.readBigInts=n.readBigInts}if(n.returnArrays!==void 0){if(typeof n.returnArrays!=`boolean`)throw new t(`The "options.returnArrays" argument must be a boolean.`);r.returnArrays=n.returnArrays}if(n.allowBareNamedParameters!==void 0){if(typeof n.allowBareNamedParameters!=`boolean`)throw new t(`The "options.allowBareNamedParameters" argument must be a boolean.`);r.allowBareNamedParameters=n.allowBareNamedParameters}if(n.allowUnknownNamedParameters!==void 0){if(typeof n.allowUnknownNamedParameters!=`boolean`)throw new t(`The "options.allowUnknownNamedParameters" argument must be a boolean.`);r.allowUnknownNamedParameters=n.allowUnknownNamedParameters}if(n.defensive!==void 0){if(typeof n.defensive!=`boolean`)throw new t(`The "options.defensive" argument must be a boolean.`);r.defensive=n.defensive}if(n.allowExtension!==void 0){if(typeof n.allowExtension!=`boolean`)throw new t(`The "options.allowExtension" argument must be a boolean.`);r.allowExtension=n.allowExtension}return r}function convertParameterSyntax(e){let t=[],n=0,r=``,i=0;for(;i<e.length;){if(e[i]===`'`){let t=i;for(i++;i<e.length&&e[i]!==`'`;){if(e[i]===`'`&&e[i+1]===`'`){i+=2;continue}i++}i<e.length&&i++,r+=e.substring(t,i);continue}if(e[i]===`?`){i++;let a=``;for(;i<e.length&&e[i]>=`0`&&e[i]<=`9`;)a+=e[i],i++;let o=a?parseInt(a,10)-1:n;n=a?n:n+1;let s=`p${o}`;t.push({gdaId:s,originalName:a?`?${a}`:`?`,position:o}),r+=`##${s}::string`;continue}if((e[i]===`$`||e[i]===`:`||e[i]===`@`)&&i+1<e.length&&/[a-zA-Z_]/.test(e[i+1])){let n=e[i];i++;let a=``;for(;i<e.length&&/[a-zA-Z0-9_]/.test(e[i]);)a+=e[i],i++;let o=a;t.push({gdaId:o,originalName:`${n}${a}`,position:-1}),r+=`##${o}::string`;continue}r+=e[i],i++}return[r,t]}var DatabaseSync=class{#e=null;#t=null;#n;#r;#i;#a=!1;constructor(t,n){if(!new.target)throw new e(`DatabaseSync`);this.#n=parsePath(t),this.#r=validateOptions(n),this.#i=this.#n===`:memory:`,this.#r.open!==!1&&this.open()}get[s](){return`node:sqlite`}get isOpen(){return this.#e!==null&&this.#e.is_opened()}get isTransaction(){return this.#o(),this.#a}open(){if(this.isOpen)throw new n(`database is already open`);try{if(this.#i)this.#e=o.Connection.new_from_string(`SQLite`,`DB_DIR=;DB_NAME=:memory:`,null,o.ConnectionOptions.NONE);else{let e=this.#n.lastIndexOf(`/`),t=`DB_DIR=${e>=0?this.#n.substring(0,e):`.`};DB_NAME=${e>=0?this.#n.substring(e+1):this.#n}`,n=this.#r.readOnly?o.ConnectionOptions.READ_ONLY:o.ConnectionOptions.NONE;this.#e=o.Connection.new_from_string(`SQLite`,t,null,n)}this.#e.open()}catch(e){throw this.#e=null,new i(e instanceof Error?e.message:String(e))}this.#t=this.#e.create_parser()??new o.SqlParser,this.#s()}close(){if(!this.isOpen)throw new n(`database is not open`);this.#e.close(),this.#e=null,this.#t=null,this.#a=!1}sqlExec(e){if(this.#o(),typeof e!=`string`)throw new t(`The "sql" argument must be a string.`);try{let t=this.#l(e);for(let e of t){let[t]=this.#t.parse_string(e);t&&this.#c(t)}}catch(e){throw e instanceof i||e instanceof n||e instanceof t?e:new i(e instanceof Error?e.message:String(e))}this.#u(e)}prepare(e,n){if(this.#o(),typeof e!=`string`)throw new t(`The "sql" argument must be a string.`);let[,r]=convertParameterSyntax(e);try{let t=r.length>0?e.replace(/\?(\d+)?/g,`NULL`).replace(/[\$:@][a-zA-Z_][a-zA-Z0-9_]*/g,`NULL`):e,[n]=this.#t.parse_string(t);if(!n)throw new i(`Failed to parse SQL statement`)}catch(e){throw e instanceof i||e instanceof t?e:new i(e instanceof Error?e.message:String(e))}let o={readBigInts:this.#r.readBigInts??!1,returnArrays:this.#r.returnArrays??!1,allowBareNamedParameters:this.#r.allowBareNamedParameters??!0,allowUnknownNamedParameters:this.#r.allowUnknownNamedParameters??!1};return a._create(this.#e,e,o,r,this.#t)}location(e){if(this.#o(),e!==void 0&&typeof e!=`string`)throw new t(`The "dbName" argument must be a string.`);return this.#i?null:this.#n}[Symbol.dispose](){if(this.isOpen)try{this.close()}catch{}}#o(){if(!this.isOpen)throw new n(`database is not open`)}#s(){let e=this.#e,runPragma=t=>{let[n]=this.#t.parse_string(t);n&&e.statement_execute_select(n,null)};this.#r.enableForeignKeyConstraints===!1?runPragma(`PRAGMA foreign_keys = OFF`):runPragma(`PRAGMA foreign_keys = ON`),this.#r.timeout!==void 0&&this.#r.timeout>0&&runPragma(`PRAGMA busy_timeout = ${this.#r.timeout}`)}#c(e){if(e.get_statement_type()===o.SqlStatementType.SELECT)this.#e.statement_execute_select(e,null);else try{this.#e.statement_execute_non_select(e,null)}catch{this.#e.statement_execute_select(e,null)}}#l(e){let t=[],n=``,r=!1;for(let i=0;i<e.length;i++){let a=e[i];if(a===`'`&&!r)r=!0,n+=a;else if(a===`'`&&r)e[i+1]===`'`?(n+=`''`,i++):(r=!1,n+=a);else if(a===`;`&&!r){let e=n.trim();e.length>0&&t.push(e),n=``}else n+=a}let i=n.trim();return i.length>0&&t.push(i),t}#u(e){let t=e.trim().toUpperCase();/\bBEGIN\b/.test(t)&&(this.#a=!0),(/\bCOMMIT\b/.test(t)||/\bROLLBACK\b/.test(t))&&(this.#a=!1)}};DatabaseSync.prototype.exec=DatabaseSync.prototype.sqlExec;export{DatabaseSync};
package/lib/esm/errors.js CHANGED
@@ -1 +1 @@
1
- var SqliteError=class extends Error{code=`ERR_SQLITE_ERROR`;errcode;errstr;constructor(e,t=0,n=``){super(e),this.name=`SqliteError`,this.errcode=t,this.errstr=n||e}},InvalidStateError=class extends Error{code=`ERR_INVALID_STATE`;constructor(e){super(e),this.name=`InvalidStateError`}},InvalidArgTypeError=class extends TypeError{code=`ERR_INVALID_ARG_TYPE`;constructor(e){super(e),this.name=`InvalidArgTypeError`}},InvalidArgValueError=class extends Error{code=`ERR_INVALID_ARG_VALUE`;constructor(e){super(e),this.name=`InvalidArgValueError`}},OutOfRangeError=class extends RangeError{code=`ERR_OUT_OF_RANGE`;constructor(e){super(e),this.name=`OutOfRangeError`}},ConstructCallRequiredError=class extends TypeError{code=`ERR_CONSTRUCT_CALL_REQUIRED`;constructor(e){super(`Cannot call constructor without \`new\`: ${e}`),this.name=`ConstructCallRequiredError`}},InvalidUrlSchemeError=class extends TypeError{code=`ERR_INVALID_URL_SCHEME`;constructor(e=`The URL must be of scheme file:`){super(e),this.name=`InvalidUrlSchemeError`}},IllegalConstructorError=class extends TypeError{code=`ERR_ILLEGAL_CONSTRUCTOR`;constructor(){super(`Illegal constructor`),this.name=`IllegalConstructorError`}};export{ConstructCallRequiredError,IllegalConstructorError,InvalidArgTypeError,InvalidArgValueError,InvalidStateError,InvalidUrlSchemeError,OutOfRangeError,SqliteError};
1
+ import"./_virtual/_rolldown/runtime.js";var SqliteError=class extends Error{code=`ERR_SQLITE_ERROR`;errcode;errstr;constructor(e,t=0,n=``){super(e),this.name=`SqliteError`,this.errcode=t,this.errstr=n||e}},InvalidStateError=class extends Error{code=`ERR_INVALID_STATE`;constructor(e){super(e),this.name=`InvalidStateError`}},InvalidArgTypeError=class extends TypeError{code=`ERR_INVALID_ARG_TYPE`;constructor(e){super(e),this.name=`InvalidArgTypeError`}},InvalidArgValueError=class extends Error{code=`ERR_INVALID_ARG_VALUE`;constructor(e){super(e),this.name=`InvalidArgValueError`}},OutOfRangeError=class extends RangeError{code=`ERR_OUT_OF_RANGE`;constructor(e){super(e),this.name=`OutOfRangeError`}},ConstructCallRequiredError=class extends TypeError{code=`ERR_CONSTRUCT_CALL_REQUIRED`;constructor(e){super(`Cannot call constructor without \`new\`: ${e}`),this.name=`ConstructCallRequiredError`}},InvalidUrlSchemeError=class extends TypeError{code=`ERR_INVALID_URL_SCHEME`;constructor(e=`The URL must be of scheme file:`){super(e),this.name=`InvalidUrlSchemeError`}},IllegalConstructorError=class extends TypeError{code=`ERR_ILLEGAL_CONSTRUCTOR`;constructor(){super(`Illegal constructor`),this.name=`IllegalConstructorError`}};export{ConstructCallRequiredError,IllegalConstructorError,InvalidArgTypeError,InvalidArgValueError,InvalidStateError,InvalidUrlSchemeError,OutOfRangeError,SqliteError};
@@ -1 +1 @@
1
- import{InvalidArgTypeError as e,InvalidArgValueError as t,InvalidStateError as n,SqliteError as r}from"./errors.js";function validateBindValue(t,n){if(t==null)return;let r=typeof t;if(!(r===`number`||r===`bigint`||r===`string`||r===`boolean`)&&!(t instanceof Uint8Array||t instanceof ArrayBuffer)&&!ArrayBuffer.isView(t))throw new e(`Provided value cannot be bound to SQLite parameter ${n}.`)}function setHolderValue(e,n){if(n==null){e.set_value(null);return}if(typeof n==`number`){e.set_value(n);return}if(typeof n==`bigint`){if(n>9223372036854775807n||n<-9223372036854775808n)throw new t(`BigInt value is too large to bind.`);e.set_value(Number(n));return}if(typeof n==`string`){e.set_value(n);return}if(typeof n==`boolean`){e.set_value(+!!n);return}if(n instanceof Uint8Array||ArrayBuffer.isView(n)){let t=n instanceof Uint8Array?n:new Uint8Array(n.buffer);e.set_value(t);return}e.set_value(n)}function bindParameters(e,t,i,a){if(!e){if(t.length>0)throw new r(`column index out of range`,25,`column index out of range`);return}let o=e.get_holders();if(i){let e=new Set;for(let t of o){let r=t.get_id(),s,c=!1;if(r in i&&(s=i[r],e.add(r),c=!0),!c&&a.allowBareNamedParameters){let t=r.replace(/^[\$:@]/,``);t in i&&(s=i[t],e.add(t),c=!0)}if(!c&&!a.allowBareNamedParameters){let e=r.replace(/^[\$:@]/,``);if(e in i)throw new n(`Unknown named parameter '${e}'`)}if(c){let e=o.indexOf(t)+1;validateBindValue(s,e),setHolderValue(t,s)}else setHolderValue(t,null)}if(!a.allowUnknownNamedParameters){for(let t of Object.keys(i))if(!e.has(t)&&!o.some(e=>{let n=e.get_id();return n===t||n.replace(/^[\$:@]/,``)===t}))throw new n(`Unknown named parameter '${t}'`)}}else{if(t.length>o.length)throw new r(`column index out of range`,25,`column index out of range`);for(let e=0;e<o.length;e++){let n=e<t.length?t[e]:void 0;validateBindValue(n,e+1),setHolderValue(o[e],n??null)}}}export{bindParameters};
1
+ import"./_virtual/_rolldown/runtime.js";import{InvalidArgTypeError as e,InvalidArgValueError as t,InvalidStateError as n,SqliteError as r}from"./errors.js";function validateBindValue(t,n){if(t==null)return;let r=typeof t;if(!(r===`number`||r===`bigint`||r===`string`||r===`boolean`)&&!(t instanceof Uint8Array||t instanceof ArrayBuffer)&&!ArrayBuffer.isView(t))throw new e(`Provided value cannot be bound to SQLite parameter ${n}.`)}function setHolderValue(e,n){if(n==null){e.set_value(null);return}if(typeof n==`number`){e.set_value(n);return}if(typeof n==`bigint`){if(n>9223372036854775807n||n<-9223372036854775808n)throw new t(`BigInt value is too large to bind.`);e.set_value(Number(n));return}if(typeof n==`string`){e.set_value(n);return}if(typeof n==`boolean`){e.set_value(+!!n);return}if(n instanceof Uint8Array||ArrayBuffer.isView(n)){let t=n instanceof Uint8Array?n:new Uint8Array(n.buffer);e.set_value(t);return}e.set_value(n)}function bindParameters(e,t,i,a){if(!e){if(t.length>0)throw new r(`column index out of range`,25,`column index out of range`);return}let o=e.get_holders();if(i){let e=new Set;for(let t of o){let r=t.get_id(),s,c=!1;if(r in i&&(s=i[r],e.add(r),c=!0),!c&&a.allowBareNamedParameters){let t=r.replace(/^[\$:@]/,``);t in i&&(s=i[t],e.add(t),c=!0)}if(!c&&!a.allowBareNamedParameters){let e=r.replace(/^[\$:@]/,``);if(e in i)throw new n(`Unknown named parameter '${e}'`)}if(c){let e=o.indexOf(t)+1;validateBindValue(s,e),setHolderValue(t,s)}else setHolderValue(t,null)}if(!a.allowUnknownNamedParameters){for(let t of Object.keys(i))if(!e.has(t)&&!o.some(e=>{let n=e.get_id();return n===t||n.replace(/^[\$:@]/,``)===t}))throw new n(`Unknown named parameter '${t}'`)}}else{if(t.length>o.length)throw new r(`column index out of range`,25,`column index out of range`);for(let e=0;e<o.length;e++){let n=e<t.length?t[e]:void 0;validateBindValue(n,e+1),setHolderValue(o[e],n??null)}}}export{bindParameters};
@@ -1 +1 @@
1
- import{IllegalConstructorError as e,InvalidArgTypeError as t,InvalidArgValueError as n,SqliteError as r}from"./errors.js";import{readAllRows as i,readFirstRow as a}from"./data-model-reader.js";import o from"@girs/gda-6.0";const s=Symbol(`StatementSync.internal`);function validateBindValue(e,n){if(e===null)return;let r=typeof e;if(!(r===`number`||r===`bigint`||r===`string`||r===`boolean`)&&!(e instanceof Uint8Array||e instanceof ArrayBuffer)&&!ArrayBuffer.isView(e))throw new t(`Provided value cannot be bound to SQLite parameter ${n}.`)}function sqlEscapeValue(e){if(e==null)return`NULL`;if(typeof e==`number`)return String(e);if(typeof e==`boolean`)return e?`1`:`0`;if(typeof e==`bigint`){if(e>9223372036854775807n||e<-9223372036854775808n)throw new n(`BigInt value is too large to bind.`);return String(e)}if(typeof e==`string`)return`'`+e.replace(/'/g,`''`)+`'`;if(e instanceof Uint8Array){let t=``;for(let n=0;n<e.length;n++)t+=e[n].toString(16).padStart(2,`0`);return`X'`+t+`'`}return ArrayBuffer.isView(e)?sqlEscapeValue(new Uint8Array(e.buffer)):`NULL`}var c=class StatementSync{#e;#t;#n;#r;#i;#a;#o;#s;constructor(t,n,r,i,a,o){if(t!==s)throw new e;this.#e=n,this.#t=r,this.#n=a,this.#r=i.readBigInts??!1,this.#i=i.returnArrays??!1,this.#a=i.allowBareNamedParameters??!0,this.#o=i.allowUnknownNamedParameters??!1,this.#s=o}static _create(e,t,n,r,i){return new StatementSync(s,e,t,n,r,i)}get sourceSQL(){return this.#t}get expandedSQL(){return this.#t}#c(){return{readBigInts:this.#r,returnArrays:this.#i}}#l(e){if(this.#n.length===0){if(e.length>0&&!(e.length>0&&e[0]!==null&&typeof e[0]==`object`&&!(e[0]instanceof Uint8Array)&&!ArrayBuffer.isView(e[0])))throw new r(`column index out of range`,25,`column index out of range`);return this.#t}let t=null,n=e;e.length>0&&e[0]!==null&&typeof e[0]==`object`&&!(e[0]instanceof Uint8Array)&&!ArrayBuffer.isView(e[0])&&(t=e[0],n=e.slice(1));let i=new Map,a={};if(t){for(let e of this.#n){if(e.position>=0)continue;let n=e.originalName,a,o=!1;if(n in t&&(a=t[n],o=!0),!o&&this.#a){let e=n.replace(/^[\$:@]/,``);e in t&&(a=t[e],o=!0)}if(!o&&!this.#a){let e=n.replace(/^[\$:@]/,``);if(e in t)throw new r(`Unknown named parameter '${e}'`,0,`Unknown named parameter '${e}'`)}o?(validateBindValue(a,this.#n.indexOf(e)+1),i.set(e.originalName,sqlEscapeValue(a))):i.set(e.originalName,`NULL`)}let e=this.#n.filter(e=>e.position>=0);for(let t=0;t<n.length;t++){if(t>=e.length)throw new r(`column index out of range`,25,`column index out of range`);validateBindValue(n[t],t+1),a[e[t].position]=sqlEscapeValue(n[t])}}else{let e=this.#n.filter(e=>e.position>=0);if(n.length>e.length&&e.length>0)throw new r(`column index out of range`,25,`column index out of range`);for(let t=0;t<e.length;t++)t<n.length?(validateBindValue(n[t],t+1),a[e[t].position]=sqlEscapeValue(n[t])):a[e[t].position]=`NULL`}let o=this.#t,s=this.#n.filter(e=>e.position<0).sort((e,t)=>t.originalName.length-e.originalName.length);for(let e of s){let t=i.get(e.originalName)??`NULL`;o=o.split(e.originalName).join(t)}if(Object.keys(a).length>0){let e=``,t=0,n=0;for(;n<o.length;){if(o[n]===`'`){let t=n;for(n++;n<o.length&&o[n]!==`'`;){if(o[n]===`'`&&o[n+1]===`'`){n+=2;continue}n++}n<o.length&&n++,e+=o.substring(t,n);continue}if(o[n]===`?`){n++;let r=``;for(;n<o.length&&o[n]>=`0`&&o[n]<=`9`;)r+=o[n],n++;let i=r?parseInt(r,10)-1:t;t=r?t:t+1,e+=i in a?a[i]:`NULL`;continue}e+=o[n],n++}o=e}return o}#u(e){let[t]=this.#s.parse_string(e);if(!t)throw new r(`Failed to parse SQL statement`);if(t.get_statement_type()===o.SqlStatementType.SELECT)return{model:this.#e.statement_execute_select(t,null),isSelect:!0};try{return this.#e.statement_execute_non_select(t,null),{model:null,isSelect:!1}}catch{return{model:this.#e.statement_execute_select(t,null),isSelect:!0}}}run(...e){let t=this.#l(e);this.#u(t);let n=0,r=0;try{let e=this.#e.execute_select_command(`SELECT changes()`);e&&e.get_n_rows()>0&&(n=e.get_value_at(0,0))}catch{}try{let e=this.#e.execute_select_command(`SELECT last_insert_rowid()`);e&&e.get_n_rows()>0&&(r=e.get_value_at(0,0))}catch{}return this.#r&&(n=BigInt(n),r=BigInt(r)),{changes:n,lastInsertRowid:r}}get(...e){let t=this.#l(e);try{let{model:e}=this.#u(t);return!e||e.get_n_rows()===0?void 0:a(e,this.#c())}catch{return}}all(...e){let t=this.#l(e);try{let{model:e}=this.#u(t);return e?i(e,this.#c()):[]}catch{return[]}}setReadBigInts(e){if(typeof e!=`boolean`)throw new t(`The "readBigInts" argument must be a boolean.`);this.#r=e}setReturnArrays(e){if(typeof e!=`boolean`)throw new t(`The "returnArrays" argument must be a boolean.`);this.#i=e}setAllowBareNamedParameters(e){if(typeof e!=`boolean`)throw new t(`The "allowBareNamedParameters" argument must be a boolean.`);this.#a=e}setAllowUnknownNamedParameters(e){if(typeof e!=`boolean`)throw new t(`The "allowUnknownNamedParameters" argument must be a boolean.`);this.#o=e}};export{c as StatementSync};
1
+ import"./_virtual/_rolldown/runtime.js";import{IllegalConstructorError as e,InvalidArgTypeError as t,InvalidArgValueError as n,SqliteError as r}from"./errors.js";import{readAllRows as i,readFirstRow as a}from"./data-model-reader.js";import o from"@girs/gda-6.0";const s=Symbol(`StatementSync.internal`);function validateBindValue(e,n){if(e===null)return;let r=typeof e;if(!(r===`number`||r===`bigint`||r===`string`||r===`boolean`)&&!(e instanceof Uint8Array||e instanceof ArrayBuffer)&&!ArrayBuffer.isView(e))throw new t(`Provided value cannot be bound to SQLite parameter ${n}.`)}function sqlEscapeValue(e){if(e==null)return`NULL`;if(typeof e==`number`)return String(e);if(typeof e==`boolean`)return e?`1`:`0`;if(typeof e==`bigint`){if(e>9223372036854775807n||e<-9223372036854775808n)throw new n(`BigInt value is too large to bind.`);return String(e)}if(typeof e==`string`)return`'`+e.replace(/'/g,`''`)+`'`;if(e instanceof Uint8Array){let t=``;for(let n=0;n<e.length;n++)t+=e[n].toString(16).padStart(2,`0`);return`X'`+t+`'`}return ArrayBuffer.isView(e)?sqlEscapeValue(new Uint8Array(e.buffer)):`NULL`}var c=class StatementSync{#e;#t;#n;#r;#i;#a;#o;#s;constructor(t,n,r,i,a,o){if(t!==s)throw new e;this.#e=n,this.#t=r,this.#n=a,this.#r=i.readBigInts??!1,this.#i=i.returnArrays??!1,this.#a=i.allowBareNamedParameters??!0,this.#o=i.allowUnknownNamedParameters??!1,this.#s=o}static _create(e,t,n,r,i){return new StatementSync(s,e,t,n,r,i)}get sourceSQL(){return this.#t}get expandedSQL(){return this.#t}#c(){return{readBigInts:this.#r,returnArrays:this.#i}}#l(e){if(this.#n.length===0){if(e.length>0&&!(e.length>0&&e[0]!==null&&typeof e[0]==`object`&&!(e[0]instanceof Uint8Array)&&!ArrayBuffer.isView(e[0])))throw new r(`column index out of range`,25,`column index out of range`);return this.#t}let t=null,n=e;e.length>0&&e[0]!==null&&typeof e[0]==`object`&&!(e[0]instanceof Uint8Array)&&!ArrayBuffer.isView(e[0])&&(t=e[0],n=e.slice(1));let i=new Map,a={};if(t){for(let e of this.#n){if(e.position>=0)continue;let n=e.originalName,a,o=!1;if(n in t&&(a=t[n],o=!0),!o&&this.#a){let e=n.replace(/^[\$:@]/,``);e in t&&(a=t[e],o=!0)}if(!o&&!this.#a){let e=n.replace(/^[\$:@]/,``);if(e in t)throw new r(`Unknown named parameter '${e}'`,0,`Unknown named parameter '${e}'`)}o?(validateBindValue(a,this.#n.indexOf(e)+1),i.set(e.originalName,sqlEscapeValue(a))):i.set(e.originalName,`NULL`)}let e=this.#n.filter(e=>e.position>=0);for(let t=0;t<n.length;t++){if(t>=e.length)throw new r(`column index out of range`,25,`column index out of range`);validateBindValue(n[t],t+1),a[e[t].position]=sqlEscapeValue(n[t])}}else{let e=this.#n.filter(e=>e.position>=0);if(n.length>e.length&&e.length>0)throw new r(`column index out of range`,25,`column index out of range`);for(let t=0;t<e.length;t++)t<n.length?(validateBindValue(n[t],t+1),a[e[t].position]=sqlEscapeValue(n[t])):a[e[t].position]=`NULL`}let o=this.#t,s=this.#n.filter(e=>e.position<0).sort((e,t)=>t.originalName.length-e.originalName.length);for(let e of s){let t=i.get(e.originalName)??`NULL`;o=o.split(e.originalName).join(t)}if(Object.keys(a).length>0){let e=``,t=0,n=0;for(;n<o.length;){if(o[n]===`'`){let t=n;for(n++;n<o.length&&o[n]!==`'`;){if(o[n]===`'`&&o[n+1]===`'`){n+=2;continue}n++}n<o.length&&n++,e+=o.substring(t,n);continue}if(o[n]===`?`){n++;let r=``;for(;n<o.length&&o[n]>=`0`&&o[n]<=`9`;)r+=o[n],n++;let i=r?parseInt(r,10)-1:t;t=r?t:t+1,e+=i in a?a[i]:`NULL`;continue}e+=o[n],n++}o=e}return o}#u(e){let[t]=this.#s.parse_string(e);if(!t)throw new r(`Failed to parse SQL statement`);if(t.get_statement_type()===o.SqlStatementType.SELECT)return{model:this.#e.statement_execute_select(t,null),isSelect:!0};try{return this.#e.statement_execute_non_select(t,null),{model:null,isSelect:!1}}catch{return{model:this.#e.statement_execute_select(t,null),isSelect:!0}}}run(...e){let t=this.#l(e);this.#u(t);let n=0,r=0;try{let e=this.#e.execute_select_command(`SELECT changes()`);e&&e.get_n_rows()>0&&(n=e.get_value_at(0,0))}catch{}try{let e=this.#e.execute_select_command(`SELECT last_insert_rowid()`);e&&e.get_n_rows()>0&&(r=e.get_value_at(0,0))}catch{}return this.#r&&(n=BigInt(n),r=BigInt(r)),{changes:n,lastInsertRowid:r}}get(...e){let t=this.#l(e);try{let{model:e}=this.#u(t);return!e||e.get_n_rows()===0?void 0:a(e,this.#c())}catch{return}}all(...e){let t=this.#l(e);try{let{model:e}=this.#u(t);return e?i(e,this.#c()):[]}catch{return[]}}setReadBigInts(e){if(typeof e!=`boolean`)throw new t(`The "readBigInts" argument must be a boolean.`);this.#r=e}setReturnArrays(e){if(typeof e!=`boolean`)throw new t(`The "returnArrays" argument must be a boolean.`);this.#i=e}setAllowBareNamedParameters(e){if(typeof e!=`boolean`)throw new t(`The "allowBareNamedParameters" argument must be a boolean.`);this.#a=e}setAllowUnknownNamedParameters(e){if(typeof e!=`boolean`)throw new t(`The "allowUnknownNamedParameters" argument must be a boolean.`);this.#o=e}};export{c as StatementSync};
package/package.json CHANGED
@@ -1,44 +1,47 @@
1
1
  {
2
- "name": "@gjsify/sqlite",
3
- "version": "0.3.21",
4
- "description": "Node.js sqlite module for Gjs using libgda",
5
- "type": "module",
6
- "module": "lib/esm/index.js",
7
- "types": "lib/types/index.d.ts",
8
- "exports": {
9
- ".": {
10
- "types": "./lib/types/index.d.ts",
11
- "default": "./lib/esm/index.js"
2
+ "name": "@gjsify/sqlite",
3
+ "version": "0.4.3",
4
+ "description": "Node.js sqlite module for Gjs using libgda",
5
+ "type": "module",
6
+ "module": "lib/esm/index.js",
7
+ "types": "lib/types/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./lib/types/index.d.ts",
11
+ "default": "./lib/esm/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "lib"
16
+ ],
17
+ "scripts": {
18
+ "clear": "rm -rf lib tsconfig.tsbuildinfo tsconfig.types.tsbuildinfo test.gjs.mjs test.node.mjs || exit 0",
19
+ "check": "tsc --noEmit",
20
+ "build": "gjsify run build:gjsify && gjsify run build:types",
21
+ "build:gjsify": "gjsify build --library 'src/**/*.{ts,js}' --exclude 'src/**/*.spec.{mts,ts}' 'src/test.{mts,ts}'",
22
+ "build:types": "tsc",
23
+ "build:test": "gjsify run build:test:gjs && gjsify run build:test:node",
24
+ "build:test:gjs": "gjsify build src/test.mts --app gjs --outfile test.gjs.mjs",
25
+ "build:test:node": "gjsify build src/test.mts --app node --outfile test.node.mjs",
26
+ "test": "gjsify run build:gjsify && gjsify run build:test && gjsify run test:node && gjsify run test:gjs",
27
+ "test:gjs": "gjsify run test.gjs.mjs",
28
+ "test:node": "node test.node.mjs"
29
+ },
30
+ "keywords": [
31
+ "gjs",
32
+ "node",
33
+ "sqlite",
34
+ "libgda"
35
+ ],
36
+ "devDependencies": {
37
+ "@gjsify/cli": "workspace:^",
38
+ "@gjsify/unit": "workspace:^",
39
+ "@types/node": "^25.6.2",
40
+ "typescript": "^6.0.3"
41
+ },
42
+ "dependencies": {
43
+ "@girs/gda-6.0": "6.0.0-4.0.0-rc.15",
44
+ "@girs/glib-2.0": "2.88.0-4.0.0-rc.15",
45
+ "@girs/gobject-2.0": "2.88.0-4.0.0-rc.15"
12
46
  }
13
- },
14
- "scripts": {
15
- "clear": "rm -rf lib tsconfig.tsbuildinfo tsconfig.types.tsbuildinfo test.gjs.mjs test.node.mjs || exit 0",
16
- "check": "tsc --noEmit",
17
- "build": "yarn build:gjsify && yarn build:types",
18
- "build:gjsify": "gjsify build --library 'src/**/*.{ts,js}' --exclude 'src/**/*.spec.{mts,ts}' 'src/test.{mts,ts}'",
19
- "build:types": "tsc",
20
- "build:test": "yarn build:test:gjs && yarn build:test:node",
21
- "build:test:gjs": "gjsify build src/test.mts --app gjs --outfile test.gjs.mjs",
22
- "build:test:node": "gjsify build src/test.mts --app node --outfile test.node.mjs",
23
- "test": "yarn build:gjsify && yarn build:test && yarn test:node && yarn test:gjs",
24
- "test:gjs": "gjsify run test.gjs.mjs",
25
- "test:node": "node test.node.mjs"
26
- },
27
- "keywords": [
28
- "gjs",
29
- "node",
30
- "sqlite",
31
- "libgda"
32
- ],
33
- "devDependencies": {
34
- "@gjsify/cli": "^0.3.21",
35
- "@gjsify/unit": "^0.3.21",
36
- "@types/node": "^25.6.2",
37
- "typescript": "^6.0.3"
38
- },
39
- "dependencies": {
40
- "@girs/gda-6.0": "6.0.0-4.0.0-rc.14",
41
- "@girs/glib-2.0": "2.88.0-4.0.0-rc.14",
42
- "@girs/gobject-2.0": "2.88.0-4.0.0-rc.14"
43
- }
44
- }
47
+ }
package/src/constants.ts DELETED
@@ -1,101 +0,0 @@
1
- // SQLite constants matching Node.js node:sqlite constants
2
- // Reference: Node.js lib/sqlite.js
3
-
4
- // Changeset conflict constants
5
- export const SQLITE_CHANGESET_OMIT = 0;
6
- export const SQLITE_CHANGESET_REPLACE = 1;
7
- export const SQLITE_CHANGESET_ABORT = 2;
8
- export const SQLITE_CHANGESET_DATA = 1;
9
- export const SQLITE_CHANGESET_NOTFOUND = 2;
10
- export const SQLITE_CHANGESET_CONFLICT = 3;
11
- export const SQLITE_CHANGESET_CONSTRAINT = 4;
12
- export const SQLITE_CHANGESET_FOREIGN_KEY = 5;
13
-
14
- // Authorization result constants
15
- export const SQLITE_OK = 0;
16
- export const SQLITE_DENY = 1;
17
- export const SQLITE_IGNORE = 2;
18
-
19
- // Authorization action code constants
20
- export const SQLITE_CREATE_INDEX = 1;
21
- export const SQLITE_CREATE_TABLE = 2;
22
- export const SQLITE_CREATE_TEMP_INDEX = 3;
23
- export const SQLITE_CREATE_TEMP_TABLE = 4;
24
- export const SQLITE_CREATE_TEMP_TRIGGER = 5;
25
- export const SQLITE_CREATE_TEMP_VIEW = 6;
26
- export const SQLITE_CREATE_TRIGGER = 7;
27
- export const SQLITE_CREATE_VIEW = 8;
28
- export const SQLITE_DELETE = 9;
29
- export const SQLITE_DROP_INDEX = 10;
30
- export const SQLITE_DROP_TABLE = 11;
31
- export const SQLITE_DROP_TEMP_INDEX = 12;
32
- export const SQLITE_DROP_TEMP_TABLE = 13;
33
- export const SQLITE_DROP_TEMP_TRIGGER = 14;
34
- export const SQLITE_DROP_TEMP_VIEW = 15;
35
- export const SQLITE_DROP_TRIGGER = 16;
36
- export const SQLITE_DROP_VIEW = 17;
37
- export const SQLITE_INSERT = 18;
38
- export const SQLITE_PRAGMA = 19;
39
- export const SQLITE_READ = 20;
40
- export const SQLITE_SELECT = 21;
41
- export const SQLITE_TRANSACTION = 22;
42
- export const SQLITE_UPDATE = 23;
43
- export const SQLITE_ATTACH = 24;
44
- export const SQLITE_DETACH = 25;
45
- export const SQLITE_ALTER_TABLE = 26;
46
- export const SQLITE_REINDEX = 27;
47
- export const SQLITE_ANALYZE = 28;
48
- export const SQLITE_CREATE_VTABLE = 29;
49
- export const SQLITE_DROP_VTABLE = 30;
50
- export const SQLITE_FUNCTION = 31;
51
- export const SQLITE_SAVEPOINT = 32;
52
- export const SQLITE_COPY = 0;
53
- export const SQLITE_RECURSIVE = 33;
54
-
55
- export const constants = {
56
- SQLITE_CHANGESET_OMIT,
57
- SQLITE_CHANGESET_REPLACE,
58
- SQLITE_CHANGESET_ABORT,
59
- SQLITE_CHANGESET_DATA,
60
- SQLITE_CHANGESET_NOTFOUND,
61
- SQLITE_CHANGESET_CONFLICT,
62
- SQLITE_CHANGESET_CONSTRAINT,
63
- SQLITE_CHANGESET_FOREIGN_KEY,
64
- SQLITE_OK,
65
- SQLITE_DENY,
66
- SQLITE_IGNORE,
67
- SQLITE_CREATE_INDEX,
68
- SQLITE_CREATE_TABLE,
69
- SQLITE_CREATE_TEMP_INDEX,
70
- SQLITE_CREATE_TEMP_TABLE,
71
- SQLITE_CREATE_TEMP_TRIGGER,
72
- SQLITE_CREATE_TEMP_VIEW,
73
- SQLITE_CREATE_TRIGGER,
74
- SQLITE_CREATE_VIEW,
75
- SQLITE_DELETE,
76
- SQLITE_DROP_INDEX,
77
- SQLITE_DROP_TABLE,
78
- SQLITE_DROP_TEMP_INDEX,
79
- SQLITE_DROP_TEMP_TABLE,
80
- SQLITE_DROP_TEMP_TRIGGER,
81
- SQLITE_DROP_TEMP_VIEW,
82
- SQLITE_DROP_TRIGGER,
83
- SQLITE_DROP_VIEW,
84
- SQLITE_INSERT,
85
- SQLITE_PRAGMA,
86
- SQLITE_READ,
87
- SQLITE_SELECT,
88
- SQLITE_TRANSACTION,
89
- SQLITE_UPDATE,
90
- SQLITE_ATTACH,
91
- SQLITE_DETACH,
92
- SQLITE_ALTER_TABLE,
93
- SQLITE_REINDEX,
94
- SQLITE_ANALYZE,
95
- SQLITE_CREATE_VTABLE,
96
- SQLITE_DROP_VTABLE,
97
- SQLITE_FUNCTION,
98
- SQLITE_SAVEPOINT,
99
- SQLITE_COPY,
100
- SQLITE_RECURSIVE,
101
- };
@@ -1,106 +0,0 @@
1
- // Convert Gda.DataModel results to JavaScript objects/arrays
2
- // Reference: Node.js lib/sqlite.js
3
- // Reimplemented for GJS using Gda-6.0
4
-
5
- import Gda from '@girs/gda-6.0';
6
- import GObject from '@girs/gobject-2.0';
7
- import { OutOfRangeError } from './errors.ts';
8
-
9
- export interface ReadOptions {
10
- readBigInts: boolean;
11
- returnArrays: boolean;
12
- }
13
-
14
- function convertValue(value: unknown, readBigInts: boolean): unknown {
15
- if (value === null || value === undefined) {
16
- return null;
17
- }
18
- if (typeof value === 'number') {
19
- if (Number.isInteger(value) && !Number.isSafeInteger(value)) {
20
- if (!readBigInts) {
21
- throw new OutOfRangeError(
22
- `Value is too large to be represented as a JavaScript number: ${value}`
23
- );
24
- }
25
- return BigInt(value);
26
- }
27
- if (readBigInts && Number.isInteger(value)) {
28
- return BigInt(value);
29
- }
30
- return value;
31
- }
32
- if (typeof value === 'bigint') {
33
- if (!readBigInts) {
34
- if (value > BigInt(Number.MAX_SAFE_INTEGER) || value < BigInt(-Number.MAX_SAFE_INTEGER)) {
35
- throw new OutOfRangeError(
36
- `Value is too large to be represented as a JavaScript number: ${value}`
37
- );
38
- }
39
- return Number(value);
40
- }
41
- return value;
42
- }
43
- if (typeof value === 'string') {
44
- return value;
45
- }
46
- // Handle GLib.Bytes or Uint8Array (BLOB)
47
- if (value instanceof Uint8Array) {
48
- return value;
49
- }
50
- // GLib.Bytes from Gda
51
- if (value && typeof (value as any).toArray === 'function') {
52
- return new Uint8Array((value as any).toArray());
53
- }
54
- return value;
55
- }
56
-
57
- export function readRow(
58
- model: Gda.DataModel,
59
- row: number,
60
- options: ReadOptions,
61
- ): Record<string, unknown> | unknown[] | undefined {
62
- const nCols = model.get_n_columns();
63
-
64
- if (options.returnArrays) {
65
- const arr: unknown[] = [];
66
- for (let col = 0; col < nCols; col++) {
67
- const val = model.get_value_at(col, row);
68
- arr.push(convertValue(val, options.readBigInts));
69
- }
70
- return arr;
71
- }
72
-
73
- const obj = Object.create(null) as Record<string, unknown>;
74
- for (let col = 0; col < nCols; col++) {
75
- const name = model.get_column_name(col);
76
- const val = model.get_value_at(col, row);
77
- obj[name] = convertValue(val, options.readBigInts);
78
- }
79
- return obj;
80
- }
81
-
82
- export function readAllRows(
83
- model: Gda.DataModel,
84
- options: ReadOptions,
85
- ): (Record<string, unknown> | unknown[])[] {
86
- const nRows = model.get_n_rows();
87
- const rows: (Record<string, unknown> | unknown[])[] = [];
88
- for (let row = 0; row < nRows; row++) {
89
- const r = readRow(model, row, options);
90
- if (r !== undefined) {
91
- rows.push(r);
92
- }
93
- }
94
- return rows;
95
- }
96
-
97
- export function readFirstRow(
98
- model: Gda.DataModel,
99
- options: ReadOptions,
100
- ): Record<string, unknown> | unknown[] | undefined {
101
- const nRows = model.get_n_rows();
102
- if (nRows === 0) {
103
- return undefined;
104
- }
105
- return readRow(model, 0, options);
106
- }
@@ -1,144 +0,0 @@
1
- // Ported from refs/node-test/parallel/test-sqlite-data-types.js
2
- // Original: MIT license, Node.js contributors
3
-
4
- import { describe, it, expect } from '@gjsify/unit';
5
- import { join } from 'node:path';
6
- import { tmpdir } from 'node:os';
7
- import { mkdirSync, rmSync } from 'node:fs';
8
- import { DatabaseSync } from 'node:sqlite';
9
-
10
- let cnt = 0;
11
- const testDir = join(tmpdir(), 'gjsify-sqlite-types-test-' + Date.now());
12
-
13
- function setup() {
14
- try { mkdirSync(testDir, { recursive: true }); } catch {}
15
- }
16
-
17
- function cleanup() {
18
- try { rmSync(testDir, { recursive: true, force: true }); } catch {}
19
- }
20
-
21
- function nextDb(): string {
22
- return join(testDir, `database-${cnt++}.db`);
23
- }
24
-
25
- export default async () => {
26
- setup();
27
-
28
- await describe('sqlite data types', async () => {
29
- await it('supports INTEGER, REAL, TEXT, BLOB, and NULL', async () => {
30
- const u8a = new TextEncoder().encode('a☃b☃c');
31
- const db = new DatabaseSync(nextDb());
32
- db.exec(`
33
- CREATE TABLE types(
34
- key INTEGER PRIMARY KEY,
35
- int INTEGER,
36
- double REAL,
37
- text TEXT,
38
- buf BLOB
39
- ) STRICT;
40
- `);
41
- const stmt = db.prepare('INSERT INTO types (key, int, double, text, buf) VALUES (?, ?, ?, ?, ?)');
42
-
43
- const r1 = stmt.run(1, 42, 3.14159, 'foo', u8a);
44
- expect(r1.changes).toBe(1);
45
- expect(r1.lastInsertRowid).toBe(1);
46
-
47
- const r2 = stmt.run(2, null, null, null, null);
48
- expect(r2.changes).toBe(1);
49
- expect(r2.lastInsertRowid).toBe(2);
50
-
51
- const query = db.prepare('SELECT * FROM types WHERE key = ?');
52
-
53
- const row1 = query.get(1) as Record<string, unknown>;
54
- expect(row1.key).toBe(1);
55
- expect(row1.int).toBe(42);
56
- expect(row1.double).toBe(3.14159);
57
- expect(row1.text).toBe('foo');
58
-
59
- const row2 = query.get(2) as Record<string, unknown>;
60
- expect(row2.key).toBe(2);
61
- expect(row2.int).toBeNull();
62
- expect(row2.double).toBeNull();
63
- expect(row2.text).toBeNull();
64
- expect(row2.buf).toBeNull();
65
-
66
- db.close();
67
- });
68
-
69
- await it('rejects unsupported data types', async () => {
70
- const db = new DatabaseSync(nextDb());
71
- db.exec('CREATE TABLE types(key INTEGER PRIMARY KEY, val INTEGER) STRICT;');
72
-
73
- const unsupported = [
74
- undefined,
75
- () => {},
76
- Symbol(),
77
- /foo/,
78
- Promise.resolve(),
79
- new Map(),
80
- new Set(),
81
- ];
82
-
83
- for (const val of unsupported) {
84
- expect(() => {
85
- // Intentionally pass non-SQLInputValue values to verify runtime rejection.
86
- db.prepare('INSERT INTO types (key, val) VALUES (?, ?)').run(1, val as any);
87
- }).toThrow();
88
- }
89
-
90
- db.close();
91
- });
92
-
93
- await it('supports BigInt binding', async () => {
94
- const db = new DatabaseSync(nextDb());
95
- db.exec('CREATE TABLE types(key INTEGER PRIMARY KEY, val INTEGER) STRICT;');
96
- const stmt = db.prepare('INSERT INTO types (key, val) VALUES (?, ?)');
97
- const result = stmt.run(4, 99n);
98
- expect(result.changes).toBe(1);
99
-
100
- const query = db.prepare('SELECT * FROM types WHERE key = ?');
101
- const row = query.get(4) as Record<string, unknown>;
102
- expect(row.val).toBe(99);
103
- db.close();
104
- });
105
-
106
- await it('throws when binding a BigInt that is too large', async () => {
107
- const max = 9223372036854775807n;
108
- const db = new DatabaseSync(nextDb());
109
- db.exec('CREATE TABLE types(key INTEGER PRIMARY KEY, val INTEGER) STRICT;');
110
- const stmt = db.prepare('INSERT INTO types (key, val) VALUES (?, ?)');
111
-
112
- // Max should succeed
113
- stmt.run(1, max);
114
-
115
- // Max + 1 should throw
116
- expect(() => {
117
- stmt.run(2, max + 1n);
118
- }).toThrow();
119
- db.close();
120
- });
121
-
122
- await it('statements are unbound on each call', async () => {
123
- const db = new DatabaseSync(nextDb());
124
- db.exec('CREATE TABLE data(key INTEGER PRIMARY KEY, val INTEGER) STRICT;');
125
- const stmt = db.prepare('INSERT INTO data (key, val) VALUES (?, ?)');
126
-
127
- const r1 = stmt.run(1, 5);
128
- expect(r1.changes).toBe(1);
129
-
130
- // Second call without params — params should be unbound (NULL)
131
- const r2 = stmt.run();
132
- expect(r2.changes).toBe(1);
133
-
134
- const rows = db.prepare('SELECT * FROM data ORDER BY key').all() as Record<string, unknown>[];
135
- expect(rows.length).toBe(2);
136
- expect(rows[0].key).toBe(1);
137
- expect(rows[0].val).toBe(5);
138
- expect(rows[1].val).toBeNull();
139
- db.close();
140
- });
141
- });
142
-
143
- cleanup();
144
- };