1 /**
    2  * WARNING: automatically generated by ort-nodejs 0.10.2.
    3  * DO NOT EDIT!
    4  * @packageDocumentation
    5  */
    6 
    7 import bcrypt from 'bcrypt';
    8 import Database from 'better-sqlite3';
    9 
   10 /**
   11  * Namespace for data interfaces and representative classes.  The 
   12  * interfaces are for the data itself, while the classes manage roles and 
   13  * metadata.
   14  */
   15 export namespace ortns {
   16 	/**
   17 	 * Birthsex of individual
   18 	 */
   19 	export enum sex {
   20 		/**
   21 		 * Male
   22 		 */
   23 		male = '0',
   24 		/**
   25 		 * Female
   26 		 */
   27 		female = '1',
   28 		/**
   29 		 * Other
   30 		 */
   31 		other = '2'
   32 	}
   33 
   34 	/**
   35 	 * Controlling organisation.
   36 	 */
   37 	export interface companyData {
   38 		/**
   39 		 * Name of the organisation.
   40 		 */
   41 		name: string;
   42 		id: BigInt;
   43 		/**
   44 		 * Simply a check for null values.
   45 		 */
   46 		somenum: BigInt|null;
   47 	}
   48 
   49 	function db_export_company(role: string, obj: companyData): any
   50 	{
   51 		const res: any = {}
   52 
   53 		res['name'] = obj['name'];
   54 		res['id'] = obj['id'].toString();
   55 		res['somenum'] = (obj['somenum'] === null) ?
   56 			null : obj['somenum'].toString();
   57 
   58 		return res;
   59 	}
   60 
   61 	/**
   62 	 * Class instance of {@link ortns.companyData}.
   63 	 */
   64 	export class company {
   65 		readonly #role: string;
   66 		readonly obj: ortns.companyData;
   67 
   68 		/**
   69 		 * A {@link ortns.companyData} as extracted from the database in 
   70 		 * a particular role.
   71 		 * @param role The role in which this was extracted from the 
   72 		 * database. When exported, this role will be checked for 
   73 		 * permission to export.
   74 		 * @param obj The raw data.
   75 		 */
   76 		constructor(role: string, obj: ortns.companyData)
   77 		{
   78 			this.#role = role;
   79 			this.obj = obj;
   80 		}
   81 
   82 		/**
   83 		 * Export the contained {@link ortns.companyData} respecting 
   84 		 * fields not exported, roles, etc.  It's safe to call 
   85 		 * `JSON.stringify()` on the returned object to write responses.
   86 		 */
   87 		export(): any
   88 		{
   89 			return db_export_company(this.#role, this.obj);
   90 		}
   91 	}
   92 
   93 	/**
   94 	 * A regular user.
   95 	 */
   96 	export interface userData {
   97 		/**
   98 		 * This struct will be filled in from an inner join
   99 		 * on the "cid" variable.
  100 		 */
  101 		company: ortns.companyData;
  102 		/**
  103 		 * A foreign key reference.
  104 		 */
  105 		cid: BigInt;
  106 		/**
  107 		 * User's birth sex.
  108 		 */
  109 		sex: ortns.sex;
  110 		/**
  111 		 * Password hash.
  112 		 * This is passed to inserts and updates as a password,
  113 		 * then hashed within the implementation and extracted
  114 		 * (in listings and searches) as the hash value.
  115 		 */
  116 		hash: string;
  117 		/**
  118 		 * Unique e-mail address.
  119 		 */
  120 		email: string;
  121 		/**
  122 		 * A PNG image or something.
  123 		 */
  124 		image: Buffer|null;
  125 		/**
  126 		 * User's full name.
  127 		 */
  128 		name: string;
  129 		uid: BigInt;
  130 	}
  131 
  132 	function db_export_user(role: string, obj: userData): any
  133 	{
  134 		const res: any = {}
  135 
  136 		res['company'] = db_export_company(role, obj['company'])
  137 		res['cid'] = obj['cid'].toString();
  138 		res['sex'] = obj['sex'];
  139 		/**
  140 		 * Don't output hash: password.
  141 		 */
  142 		res['email'] = obj['email'];
  143 		res['image'] = (obj['image'] === null) ?
  144 			null : obj['image'].toString('base64');
  145 		res['name'] = obj['name'];
  146 		res['uid'] = obj['uid'].toString();
  147 
  148 		return res;
  149 	}
  150 
  151 	/**
  152 	 * Class instance of {@link ortns.userData}.
  153 	 */
  154 	export class user {
  155 		readonly #role: string;
  156 		readonly obj: ortns.userData;
  157 
  158 		/**
  159 		 * A {@link ortns.userData} as extracted from the database in a 
  160 		 * particular role.
  161 		 * @param role The role in which this was extracted from the 
  162 		 * database. When exported, this role will be checked for 
  163 		 * permission to export.
  164 		 * @param obj The raw data.
  165 		 */
  166 		constructor(role: string, obj: ortns.userData)
  167 		{
  168 			this.#role = role;
  169 			this.obj = obj;
  170 		}
  171 
  172 		/**
  173 		 * Export the contained {@link ortns.userData} respecting fields 
  174 		 * not exported, roles, etc.  It's safe to call 
  175 		 * `JSON.stringify()` on the returned object to write responses.
  176 		 */
  177 		export(): any
  178 		{
  179 			return db_export_user(this.#role, this.obj);
  180 		}
  181 	}
  182 
  183 	/**
  184 	 * Authenticated session.
  185 	 */
  186 	export interface sessionData {
  187 		user: ortns.userData;
  188 		/**
  189 		 * Associated user.
  190 		 */
  191 		userid: BigInt;
  192 		/**
  193 		 * Random cookie.
  194 		 */
  195 		token: BigInt;
  196 		mtime: BigInt;
  197 		id: BigInt;
  198 	}
  199 
  200 	function db_export_session(role: string, obj: sessionData): any
  201 	{
  202 		const res: any = {}
  203 
  204 		res['user'] = db_export_user(role, obj['user'])
  205 		res['userid'] = obj['userid'].toString();
  206 		res['token'] = obj['token'].toString();
  207 		res['mtime'] = obj['mtime'].toString();
  208 		res['id'] = obj['id'].toString();
  209 
  210 		return res;
  211 	}
  212 
  213 	/**
  214 	 * Class instance of {@link ortns.sessionData}.
  215 	 */
  216 	export class session {
  217 		readonly #role: string;
  218 		readonly obj: ortns.sessionData;
  219 
  220 		/**
  221 		 * A {@link ortns.sessionData} as extracted from the database in 
  222 		 * a particular role.
  223 		 * @param role The role in which this was extracted from the 
  224 		 * database. When exported, this role will be checked for 
  225 		 * permission to export.
  226 		 * @param obj The raw data.
  227 		 */
  228 		constructor(role: string, obj: ortns.sessionData)
  229 		{
  230 			this.#role = role;
  231 			this.obj = obj;
  232 		}
  233 
  234 		/**
  235 		 * Export the contained {@link ortns.sessionData} respecting 
  236 		 * fields not exported, roles, etc.  It's safe to call 
  237 		 * `JSON.stringify()` on the returned object to write responses.
  238 		 */
  239 		export(): any
  240 		{
  241 			return db_export_session(this.#role, this.obj);
  242 		}
  243 	}
  244 }
  245 
  246 /**
  247  * Primary database object. Only one of these should exist per running 
  248  * node.js server.
  249  */
  250 export class ortdb {
  251 	db: Database.Database;
  252 	/**
  253 	 * The ort-nodejs version used to produce this file.
  254 	 */
  255 	readonly version: string = '0.10.2';
  256 	/**
  257 	 * The numeric (monotonically increasing) ort-nodejs version used to 
  258 	 * produce this file.
  259 	 */
  260 	readonly vstamp: number = 11103;
  261 
  262 	/**
  263 	 * @param dbname The file-name of the database relative to the 
  264 	 * running application.
  265 	 */
  266 	constructor(dbname: string) {
  267 		this.db = new Database(dbname);
  268 		this.db.defaultSafeIntegers(true);
  269 	}
  270 
  271 	/**
  272 	 * Create a connection to the database. This should be called for 
  273 	 * each sequence representing a single operator. In web applications, 
  274 	 * for example, this should be called for each request.
  275 	 */
  276 	connect(): ortctx
  277 	{
  278 		return new ortctx(this);
  279 	}
  280 }
  281 
  282 namespace ortstmt {
  283 	export enum ortstmt {
  284 		STMT_company_BY_UNIQUE_id,
  285 		STMT_company_BY_SEARCH_0,
  286 		STMT_company_INSERT,
  287 		STMT_company_DELETE_0,
  288 		STMT_user_BY_UNIQUE_email,
  289 		STMT_user_BY_UNIQUE_uid,
  290 		STMT_user_BY_SEARCH_0,
  291 		STMT_user_BY_SEARCH_1,
  292 		STMT_user_BY_SEARCH_2,
  293 		STMT_user_INSERT,
  294 		STMT_user_UPDATE_0,
  295 		STMT_user_UPDATE_1,
  296 		STMT_user_DELETE_0,
  297 		STMT_session_BY_UNIQUE_id,
  298 		STMT_session_BY_SEARCH_0,
  299 		STMT_session_INSERT,
  300 		STMT_session_DELETE_0,
  301 	}
  302 
  303 	export function stmtBuilder(idx: ortstmt): string
  304 	{
  305 		return ortstmts[idx];
  306 	}
  307 
  308 	const ortstmts: readonly string[] = [
  309 		/* STMT_company_BY_UNIQUE_id */
  310 		'SELECT ' + ort_schema_company('company') + ' FROM company WHERE company.id = ?',
  311 		/* STMT_company_BY_SEARCH_0 */
  312 		'SELECT ' + ort_schema_company('company') + ' FROM company '
  313 			+ 'WHERE company.somenum ISNULL',
  314 		/* STMT_company_INSERT */
  315 		'INSERT INTO company (name,somenum) VALUES (?,?)',
  316 		/* STMT_company_DELETE_0 */
  317 		'DELETE FROM company',
  318 		/* STMT_user_BY_UNIQUE_email */
  319 		'SELECT ' + ort_schema_user('user') + ',' + ort_schema_company('_a') + ' FROM user '
  320 			+ 'INNER JOIN company AS _a ON _a.id=user.cid '
  321 			+ 'WHERE user.email = ?',
  322 		/* STMT_user_BY_UNIQUE_uid */
  323 		'SELECT ' + ort_schema_user('user') + ',' + ort_schema_company('_a') + ' FROM user '
  324 			+ 'INNER JOIN company AS _a ON _a.id=user.cid '
  325 			+ 'WHERE user.uid = ?',
  326 		/* STMT_user_BY_SEARCH_0 */
  327 		'SELECT ' + ort_schema_user('user') + ',' + ort_schema_company('_a') + ' FROM user '
  328 			+ 'INNER JOIN company AS _a ON _a.id=user.cid '
  329 			+ 'WHERE user.name = ? LIMIT 5',
  330 		/* STMT_user_BY_SEARCH_1 */
  331 		'SELECT ' + ort_schema_user('user') + ',' + ort_schema_company('_a') + ' FROM user '
  332 			+ 'INNER JOIN company AS _a ON _a.id=user.cid '
  333 			+ 'WHERE user.email = ?',
  334 		/* STMT_user_BY_SEARCH_2 */
  335 		'SELECT ' + ort_schema_user('user') + ',' + ort_schema_company('_a') + ' FROM user '
  336 			+ 'INNER JOIN company AS _a ON _a.id=user.cid '
  337 			+ 'WHERE user.uid = ? ORDER BY _a.name ASC',
  338 		/* STMT_user_INSERT */
  339 		'INSERT INTO user (cid,sex,hash,email,image,name) VALUES '
  340 			+ '(?,?,?,?,?,?)',
  341 		/* STMT_user_UPDATE_0 */
  342 		'UPDATE user SET hash = ? WHERE uid = ?',
  343 		/* STMT_user_UPDATE_1 */
  344 		'UPDATE user SET email = ? WHERE uid = ?',
  345 		/* STMT_user_DELETE_0 */
  346 		'DELETE FROM user',
  347 		/* STMT_session_BY_UNIQUE_id */
  348 		'SELECT ' + ort_schema_session('session') + ',' + ort_schema_user('_b') + ',' 
  349 			+ ort_schema_company('_c') + ' FROM session '
  350 			+ 'INNER JOIN user AS _b ON _b.uid=session.userid '
  351 			+ 'INNER JOIN company AS _c ON _c.id=_b.cid '
  352 			+ 'WHERE session.id = ?',
  353 		/* STMT_session_BY_SEARCH_0 */
  354 		'SELECT ' + ort_schema_session('session') + ',' + ort_schema_user('_b') + ',' 
  355 			+ ort_schema_company('_c') + ' FROM session '
  356 			+ 'INNER JOIN user AS _b ON _b.uid=session.userid '
  357 			+ 'INNER JOIN company AS _c ON _c.id=_b.cid '
  358 			+ 'WHERE _c.name = ? AND session.mtime = ?',
  359 		/* STMT_session_INSERT */
  360 		'INSERT INTO session (userid,token,mtime) VALUES (?,?,?)',
  361 		/* STMT_session_DELETE_0 */
  362 		'DELETE FROM session WHERE id = ?',
  363 	];
  364 
  365 	function ort_schema_company(v: string): string
  366 	{
  367 		return v + '.name' + ',' +
  368 		       v + '.id' + ',' +
  369 		       v + '.somenum';
  370 	}
  371 
  372 	function ort_schema_user(v: string): string
  373 	{
  374 		return v + '.cid' + ',' +
  375 		       v + '.sex' + ',' +
  376 		       v + '.hash' + ',' +
  377 		       v + '.email' + ',' +
  378 		       v + '.image' + ',' +
  379 		       v + '.name' + ',' +
  380 		       v + '.uid';
  381 	}
  382 
  383 	function ort_schema_session(v: string): string
  384 	{
  385 		return v + '.userid' + ',' +
  386 		       v + '.token' + ',' +
  387 		       v + '.mtime' + ',' +
  388 		       v + '.id';
  389 	}
  390 }
  391 
  392 
  393 /**
  394  * Manages all access to the database. This object should be used for the 
  395  * lifetime of a single 'request', such as a request for a web 
  396  * application.
  397  */
  398 export class ortctx {
  399 	#role: string = 'default';
  400 	readonly #o: ortdb;
  401 
  402 	constructor(o: ortdb) {
  403 		this.#o = o;
  404 	}
  405 
  406 	db_trans_open_immediate(id: number): void
  407 	{
  408 		this.#o.db.exec('BEGIN TRANSACTION IMMEDIATE');
  409 	}
  410 
  411 	db_trans_open_deferred(id: number): void
  412 	{
  413 		this.#o.db.exec('BEGIN TRANSACTION DEFERRED');
  414 	}
  415 
  416 	db_trans_open_exclusive(id: number): void
  417 	{
  418 		this.#o.db.exec('BEGIN TRANSACTION EXCLUSIVE');
  419 	}
  420 
  421 	db_trans_rollback(id: number): void
  422 	{
  423 		this.#o.db.exec('ROLLBACK TRANSACTION');
  424 	}
  425 
  426 	db_trans_commit(id: number): void
  427 	{
  428 		this.#o.db.exec('COMMIT TRANSACTION');
  429 	}
  430 
  431 	private db_company_fill(data: {row: any[], pos: number}):
  432 		ortns.companyData
  433 	{
  434 		const obj: ortns.companyData = {
  435 			'name': <string>data.row[data.pos + 0],
  436 			'id': <BigInt>data.row[data.pos + 1],
  437 			'somenum': <BigInt|null>data.row[data.pos + 2],
  438 		};
  439 		data.pos += 3;
  440 		return obj;
  441 	}
  442 
  443 	/**
  444 	 * Insert a new row into the database. Only native (and non-rowid) 
  445 	 * fields may be set.
  446 	 * @param v1 name
  447 	 * @param v2 somenum
  448 	 * @return New row's identifier on success or <0 otherwise.
  449 	 */
  450 	db_company_insert(v1: string, v2: BigInt|null): BigInt
  451 	{
  452 		const parms: any[] = [];
  453 		let info: Database.RunResult;
  454 		const stmt: Database.Statement =
  455 			this.#o.db.prepare(ortstmt.stmtBuilder
  456 			(ortstmt.ortstmt.STMT_company_INSERT));
  457 
  458 		parms.push(v1);
  459 		parms.push(v2);
  460 
  461 		try {
  462 			info = stmt.run(parms);
  463 		} catch (er) {
  464 			return BigInt(-1);
  465 		}
  466 
  467 		return BigInt(info.lastInsertRowid);
  468 	}
  469 
  470 	/**
  471 	 * Search for a set of {@link ortns.company}.
  472 	 * The following fields are constrained by unary operations: 
  473 	 * somenum (checked is null)
  474 	 * @return Result of null if no results found.
  475 	 */
  476 	db_company_list_by_somenum_isnull(): ortns.company[]
  477 	{
  478 		const parms: any[] = [];
  479 		const stmt: Database.Statement =
  480 			this.#o.db.prepare(ortstmt.stmtBuilder
  481 			(ortstmt.ortstmt.STMT_company_BY_SEARCH_0));
  482 		stmt.raw(true);
  483 
  484 
  485 		const rows: any[] = stmt.all(parms);
  486 		const objs: ortns.company[] = [];
  487 		let i: number;
  488 
  489 		for (i = 0; i < rows.length; i++) {
  490 			const obj: ortns.companyData =
  491 				this.db_company_fill({row: <any[]>rows[i], pos: 0});
  492 			objs.push(new ortns.company(this.#role, obj));
  493 		}
  494 		return objs;
  495 	}
  496 
  497 	/**
  498 	 * 
  499 	 */
  500 	db_company_delete(): void
  501 	{
  502 		const parms: any[] = [];
  503 		let info: Database.RunResult;
  504 		const stmt: Database.Statement =
  505 			this.#o.db.prepare(ortstmt.stmtBuilder
  506 			(ortstmt.ortstmt.STMT_company_DELETE_0));
  507 
  508 
  509 		stmt.run(parms);
  510 	}
  511 
  512 	private db_user_fill(data: {row: any[], pos: number}):
  513 		ortns.userData
  514 	{
  515 		const obj: ortns.userData = {
  516 			/* A dummy value for now. */
  517 			'company': <ortns.companyData>{},
  518 			'cid': <BigInt>data.row[data.pos + 0],
  519 			'sex': <ortns.sex>data.row[data.pos + 1].toString(),
  520 			'hash': <string>data.row[data.pos + 2],
  521 			'email': <string>data.row[data.pos + 3],
  522 			'image': <Buffer|null>data.row[data.pos + 4],
  523 			'name': <string>data.row[data.pos + 5],
  524 			'uid': <BigInt>data.row[data.pos + 6],
  525 		};
  526 		data.pos += 7;
  527 		obj.company = this.db_company_fill(data);
  528 		return obj;
  529 	}
  530 
  531 	/**
  532 	 * Insert a new row into the database. Only native (and non-rowid) 
  533 	 * fields may be set.
  534 	 * @param v1 cid
  535 	 * @param v2 sex
  536 	 * @param v3 hash
  537 	 * @param v4 email
  538 	 * @param v5 image
  539 	 * @param v6 name
  540 	 * @return New row's identifier on success or <0 otherwise.
  541 	 */
  542 	db_user_insert(v1: BigInt, v2: ortns.sex, v3: string, v4: string,
  543 		v5: Buffer|null, v6: string): BigInt
  544 	{
  545 		const parms: any[] = [];
  546 		let info: Database.RunResult;
  547 		const stmt: Database.Statement =
  548 			this.#o.db.prepare(ortstmt.stmtBuilder
  549 			(ortstmt.ortstmt.STMT_user_INSERT));
  550 
  551 		parms.push(v1);
  552 		parms.push(v2);
  553 		parms.push(bcrypt.hashSync(v3, bcrypt.genSaltSync()));
  554 		parms.push(v4);
  555 		parms.push(v5);
  556 		parms.push(v6);
  557 
  558 		try {
  559 			info = stmt.run(parms);
  560 		} catch (er) {
  561 			return BigInt(-1);
  562 		}
  563 
  564 		return BigInt(info.lastInsertRowid);
  565 	}
  566 
  567 	/**
  568 	 * Create a function that searches for users by a given
  569 	 * name; and, when found, invokes a callback function
  570 	 * provided the user structure.
  571 	 * This callback function is called during an implicit transaction: 
  572 	 * thus, it should not invoke any database modifications or risk 
  573 	 * deadlock.
  574 	 * @param v1 name
  575 	 * @param cb Callback with retrieved data.
  576 	 */
  577 	db_user_iterate_by_name_eq(v1: string,
  578 		cb: (res: ortns.user) => void): void
  579 	{
  580 		const parms: any[] = [];
  581 		const stmt: Database.Statement =
  582 			this.#o.db.prepare(ortstmt.stmtBuilder
  583 			(ortstmt.ortstmt.STMT_user_BY_SEARCH_0));
  584 		stmt.raw(true);
  585 
  586 		parms.push(v1);
  587 
  588 		for (const cols of stmt.iterate(parms)) {
  589 			const obj: ortns.userData =
  590 				this.db_user_fill({row: <any>cols, pos: 0});
  591 			cb(new ortns.user(this.#role, obj));
  592 		}
  593 	}
  594 
  595 	/**
  596 	 * Search for a unique user with their e-mail and
  597 	 * password.
  598 	 * This is a quick way to verify that a user has entered
  599 	 * the correct password for logging in.
  600 	 * @param v1 email
  601 	 * @param v2 hash (hashed password)
  602 	 * @return Result or null if no results found.
  603 	 */
  604 	db_user_get_creds(v1: string, v2: string): ortns.user|null
  605 	{
  606 		const parms: any[] = [];
  607 		const stmt: Database.Statement =
  608 			this.#o.db.prepare(ortstmt.stmtBuilder
  609 			(ortstmt.ortstmt.STMT_user_BY_SEARCH_1));
  610 		stmt.raw(true);
  611 
  612 		parms.push(v1);
  613 		parms.push(bcrypt.hashSync(v2, bcrypt.genSaltSync()));
  614 
  615 		const cols: any = stmt.get(parms);
  616 
  617 		if (typeof cols === 'undefined')
  618 			return null;
  619 		const obj: ortns.userData = 
  620 			this.db_user_fill({row: <any[]>cols, pos: 0});
  621 		return new ortns.user(this.#role, obj);
  622 	}
  623 
  624 	/**
  625 	 * Lookup by unique identifier.
  626 	 * @param v1 uid
  627 	 * @return Result or null if no results found.
  628 	 */
  629 	db_user_get_by_uid_eq(v1: BigInt): ortns.user|null
  630 	{
  631 		const parms: any[] = [];
  632 		const stmt: Database.Statement =
  633 			this.#o.db.prepare(ortstmt.stmtBuilder
  634 			(ortstmt.ortstmt.STMT_user_BY_SEARCH_2));
  635 		stmt.raw(true);
  636 
  637 		parms.push(v1);
  638 
  639 		const cols: any = stmt.get(parms);
  640 
  641 		if (typeof cols === 'undefined')
  642 			return null;
  643 		const obj: ortns.userData = 
  644 			this.db_user_fill({row: <any[]>cols, pos: 0});
  645 		return new ortns.user(this.#role, obj);
  646 	}
  647 
  648 	/**
  649 	 * 
  650 	 */
  651 	db_user_delete(): void
  652 	{
  653 		const parms: any[] = [];
  654 		let info: Database.RunResult;
  655 		const stmt: Database.Statement =
  656 			this.#o.db.prepare(ortstmt.stmtBuilder
  657 			(ortstmt.ortstmt.STMT_user_DELETE_0));
  658 
  659 
  660 		stmt.run(parms);
  661 	}
  662 
  663 	/**
  664 	 * @param v1 update hash (hashed)
  665 	 * @param v2 uid (eq)
  666 	 * @return False on constraint violation, true on success.
  667 	 */
  668 	db_user_update_hash_set_by_uid_eq(v1: string, v2: BigInt): boolean
  669 	{
  670 		const parms: any[] = [];
  671 		let info: Database.RunResult;
  672 		const stmt: Database.Statement =
  673 			this.#o.db.prepare(ortstmt.stmtBuilder
  674 			(ortstmt.ortstmt.STMT_user_UPDATE_0));
  675 
  676 		parms.push(bcrypt.hashSync(v1, bcrypt.genSaltSync()));
  677 		parms.push(v2);
  678 
  679 		try {
  680 			info = stmt.run(parms);
  681 		} catch (er) {
  682 			return false;
  683 		}
  684 
  685 		return true;
  686 	}
  687 
  688 	/**
  689 	 * @param v1 update email
  690 	 * @param v2 uid (eq)
  691 	 * @return False on constraint violation, true on success.
  692 	 */
  693 	db_user_update_email_set_by_uid_eq(v1: string, v2: BigInt):
  694 		boolean
  695 	{
  696 		const parms: any[] = [];
  697 		let info: Database.RunResult;
  698 		const stmt: Database.Statement =
  699 			this.#o.db.prepare(ortstmt.stmtBuilder
  700 			(ortstmt.ortstmt.STMT_user_UPDATE_1));
  701 
  702 		parms.push(v1);
  703 		parms.push(v2);
  704 
  705 		try {
  706 			info = stmt.run(parms);
  707 		} catch (er) {
  708 			return false;
  709 		}
  710 
  711 		return true;
  712 	}
  713 
  714 	private db_session_fill(data: {row: any[], pos: number}):
  715 		ortns.sessionData
  716 	{
  717 		const obj: ortns.sessionData = {
  718 			/* A dummy value for now. */
  719 			'user': <ortns.userData>{},
  720 			'userid': <BigInt>data.row[data.pos + 0],
  721 			'token': <BigInt>data.row[data.pos + 1],
  722 			'mtime': <BigInt>data.row[data.pos + 2],
  723 			'id': <BigInt>data.row[data.pos + 3],
  724 		};
  725 		data.pos += 4;
  726 		obj.user = this.db_user_fill(data);
  727 		return obj;
  728 	}
  729 
  730 	/**
  731 	 * Insert a new row into the database. Only native (and non-rowid) 
  732 	 * fields may be set.
  733 	 * @param v1 userid
  734 	 * @param v2 token
  735 	 * @param v3 mtime
  736 	 * @return New row's identifier on success or <0 otherwise.
  737 	 */
  738 	db_session_insert(v1: BigInt, v2: BigInt, v3: BigInt): BigInt
  739 	{
  740 		const parms: any[] = [];
  741 		let info: Database.RunResult;
  742 		const stmt: Database.Statement =
  743 			this.#o.db.prepare(ortstmt.stmtBuilder
  744 			(ortstmt.ortstmt.STMT_session_INSERT));
  745 
  746 		parms.push(v1);
  747 		parms.push(v2);
  748 		parms.push(v3);
  749 
  750 		try {
  751 			info = stmt.run(parms);
  752 		} catch (er) {
  753 			return BigInt(-1);
  754 		}
  755 
  756 		return BigInt(info.lastInsertRowid);
  757 	}
  758 
  759 	/**
  760 	 * Search for company's logged-in users.
  761 	 * This callback function is called during an implicit transaction: 
  762 	 * thus, it should not invoke any database modifications or risk 
  763 	 * deadlock.
  764 	 * @param v1 user.company.name
  765 	 * @param v2 mtime
  766 	 * @param cb Callback with retrieved data.
  767 	 */
  768 	db_session_iterate_foo(v1: string, v2: BigInt,
  769 		cb: (res: ortns.session) => void): void
  770 	{
  771 		const parms: any[] = [];
  772 		const stmt: Database.Statement =
  773 			this.#o.db.prepare(ortstmt.stmtBuilder
  774 			(ortstmt.ortstmt.STMT_session_BY_SEARCH_0));
  775 		stmt.raw(true);
  776 
  777 		parms.push(v1);
  778 		parms.push(v2);
  779 
  780 		for (const cols of stmt.iterate(parms)) {
  781 			const obj: ortns.sessionData =
  782 				this.db_session_fill({row: <any>cols, pos: 0});
  783 			cb(new ortns.session(this.#role, obj));
  784 		}
  785 	}
  786 
  787 	/**
  788 	 * @param v1 id (eq)
  789 	 * 
  790 	 */
  791 	db_session_delete_by_id_eq(v1: BigInt): void
  792 	{
  793 		const parms: any[] = [];
  794 		let info: Database.RunResult;
  795 		const stmt: Database.Statement =
  796 			this.#o.db.prepare(ortstmt.stmtBuilder
  797 			(ortstmt.ortstmt.STMT_session_DELETE_0));
  798 
  799 		parms.push(v1);
  800 
  801 		stmt.run(parms);
  802 	}
  803 }
  804 
  805 /**
  806  * Instance an application-wide context. This should only be called once 
  807  * per server, with the {@link ortdb.connect} method used for sequences 
  808  * of operations.
  809  */
  810 export function ort(dbname: string): ortdb
  811 {
  812 	return new ortdb(dbname);
  813 }