@@ -89,8 +89,23 @@ export type QueryParameterSet =
8989 * Name of a column in a database query.
9090 */
9191export interface ColumnName {
92+ /**
93+ * Name of the returned column.
94+ */
9295 name : string ;
96+ /**
97+ * Name of the database column that stores
98+ * the data returned from this query.
99+ *
100+ * This might be different from `name` if a
101+ * columns was renamed using e.g. as in
102+ * `SELECT foo AS bar FROM table`.
103+ */
93104 originName : string ;
105+ /**
106+ * Name of the table that stores the data
107+ * returned from this query.
108+ */
94109 tableName : string ;
95110}
96111
@@ -118,10 +133,7 @@ export class PreparedQuery<
118133 private _finalized : boolean ;
119134
120135 /**
121- * A prepared query which can be executed many
122- * times.
123- *
124- * The constructor should never be used directly.
136+ * This constructor should never be used directly.
125137 * Instead a prepared query can be obtained by
126138 * calling `DB.prepareQuery`.
127139 */
@@ -321,21 +333,32 @@ export class PreparedQuery<
321333 * rows into memory and hence allows to process a large
322334 * number of rows.
323335 *
324- * # Example:
336+ * Calling `iter`, `all`, or `first` invalidates any iterators
337+ * previously returned from this prepared query.
338+ *
339+ * # Examples
340+ *
325341 * ```typescript
326342 * const query = db.prepareQuery<[number, string]>("SELECT id, name FROM people");
327343 * for (const [id, name] of query.iter()) {
328344 * // ...
329345 * }
330346 * ```
331347 *
332- * Calling `iter` invalidates any iterators previously returned
333- * from this prepared query. Using an invalidated iterator is a bug.
334- *
335348 * To avoid SQL injection, user-provided values
336349 * should always be passed to the database through
337350 * a query parameter.
338351 *
352+ * ```typescript
353+ * const query = db.prepareQuery("SELECT id FROM people WHERE name = ?");
354+ * preparedQuery.iter([name]);
355+ * ```
356+ *
357+ * ```typescript
358+ * const query = db.prepareQuery("SELECT id FROM people WHERE name = :name");
359+ * preparedQuery.iter({ name });
360+ * ```
361+ *
339362 * See `QueryParameterSet` for documentation on
340363 * how values can be bound to SQL statements.
341364 *
@@ -357,6 +380,15 @@ export class PreparedQuery<
357380 /**
358381 * Like `iter` except each row is returned
359382 * as an object containing key-value pairs.
383+ *
384+ * # Examples
385+ *
386+ * ```typescript
387+ * const query = db.prepareQuery<_, { id: number, name: string }>("SELECT id, name FROM people");
388+ * for (const { id, name } of query.iter()) {
389+ * // ...
390+ * }
391+ * ```
360392 */
361393 iterEntries ( params ?: P ) : RowsIterator < O > {
362394 this . iter ( params ) ;
@@ -401,14 +433,28 @@ export class PreparedQuery<
401433 * and returns an array containing all resulting
402434 * rows.
403435 *
404- * Calling `all` invalidates any iterators
405- * previously returned by calls to `iter`.
406- * Using an invalidated iterator is a bug.
436+ * # Examples
437+ *
438+ * ```typescript
439+ * const query = db.prepareQuery<[number, string]>("SELECT id, name FROM people");
440+ * const rows = query.all();
441+ * // [[1, "Peter"], ...]
442+ * ```
407443 *
408444 * To avoid SQL injection, user-provided values
409445 * should always be passed to the database through
410446 * a query parameter.
411447 *
448+ * ```typescript
449+ * const query = db.prepareQuery("SELECT id FROM people WHERE name = ?");
450+ * preparedQuery.all([name]);
451+ * ```
452+ *
453+ * ```typescript
454+ * const query = db.prepareQuery("SELECT id FROM people WHERE name = :name");
455+ * preparedQuery.all({ name });
456+ * ```
457+ *
412458 * See `QueryParameterSet` for documentation on
413459 * how values can be bound to SQL statements.
414460 *
@@ -432,6 +478,14 @@ export class PreparedQuery<
432478 /**
433479 * Like `all` except each row is returned
434480 * as an object containing key-value pairs.
481+ *
482+ * # Examples
483+ *
484+ * ```typescript
485+ * const query = db.prepareQuery<_, { id: number, name: string }>("SELECT id, name FROM people");
486+ * const rows = query.all();
487+ * // [{ id: 1, name: "Peter" }, ...]
488+ * ```
435489 */
436490 allEntries ( params ?: P ) : Array < O > {
437491 return this . all ( params ) . map ( ( row ) => this . makeRowObject ( row ) ) ;
@@ -443,14 +497,34 @@ export class PreparedQuery<
443497 * `undefined` when there are no rows returned
444498 * by the query.
445499 *
446- * Calling `first` invalidates any iterators
447- * previously returned by calls to `iter`.
448- * Using an invalidated iterator is a bug.
500+ * # Examples
501+ *
502+ * ```typescript
503+ * const query = db.prepareQuery<[number, string]>("SELECT id, name FROM people");
504+ * const person = query.first();
505+ * // [1, "Peter"]
506+ * ```
507+ *
508+ * ```typescript
509+ * const query = db.prepareQuery("SELECT id, name FROM people WHERE name = ?");
510+ * const person = query.first(["not a name"]);
511+ * // undefined
512+ * ```
449513 *
450514 * To avoid SQL injection, user-provided values
451515 * should always be passed to the database through
452516 * a query parameter.
453517 *
518+ * ```typescript
519+ * const query = db.prepareQuery("SELECT id FROM people WHERE name = ?");
520+ * preparedQuery.first([name]);
521+ * ```
522+ *
523+ * ```typescript
524+ * const query = db.prepareQuery("SELECT id FROM people WHERE name = :name");
525+ * preparedQuery.first({ name });
526+ * ```
527+ *
454528 * See `QueryParameterSet` for documentation on
455529 * how values can be bound to SQL statements.
456530 *
@@ -479,14 +553,22 @@ export class PreparedQuery<
479553 /**
480554 * Like `first` except the row is returned
481555 * as an object containing key-value pairs.
556+ *
557+ * # Examples
558+ *
559+ * ```typescript
560+ * const query = db.prepareQuery<_, { id: number, name: string }>("SELECT id, name FROM people");
561+ * const person = query.first();
562+ * // { id: 1, name: "Peter" }
563+ * ```
482564 */
483565 firstEntry ( params ?: P ) : O | undefined {
484566 const row = this . first ( params ) ;
485567 return row === undefined ? undefined : this . makeRowObject ( row ) ;
486568 }
487569
488570 /**
489- * Deprecated, prefer `first`.
571+ * ** Deprecated:** prefer `first`.
490572 */
491573 one ( params ?: P ) : R {
492574 const rows = this . all ( params ) ;
@@ -501,7 +583,7 @@ export class PreparedQuery<
501583 }
502584
503585 /**
504- * Deprecated, prefer `firstEntry`.
586+ * ** Deprecated:** prefer `firstEntry`.
505587 */
506588 oneEntry ( params ?: P ) : O {
507589 return this . makeRowObject ( this . one ( params ) ) ;
@@ -516,16 +598,23 @@ export class PreparedQuery<
516598 * rows returned by a query are not needed or
517599 * the query does not return any rows.
518600 *
519- * Calling `execute` invalidates any iterators
520- * previously returned by calls to `iter`.
521- * Using an invalidated iterator is a bug.
601+ * # Examples
522602 *
523- * To avoid SQL injection, user-provided values
524- * should always be passed to the database through
525- * a query parameter.
603+ * ```typescript
604+ * const query = db.prepareQuery<_, _, [string]>("INSERT INTO people (name) VALUES (?)");
605+ * query.execute(["Peter"]);
606+ * ```
607+ *
608+ * ```typescript
609+ * const query = db.prepareQuery<_, _, { name: string }>("INSERT INTO people (name) VALUES (:name)");
610+ * query.execute({ name: "Peter" });
611+ * ```
526612 *
527613 * See `QueryParameterSet` for documentation on
528614 * how values can be bound to SQL statements.
615+ *
616+ * See `QueryParameter` for documentation on how
617+ * values are returned from the database.
529618 */
530619 execute ( params ?: P ) {
531620 this . startQuery ( params ) ;
@@ -544,10 +633,11 @@ export class PreparedQuery<
544633 * to avoid leaking resources.
545634 *
546635 * After a prepared query has been finalized,
547- * trying to call `iter`, `all`, `one`,
548- * `execute`, or `columns`, or using iterators which where
549- * previously obtained from the finalized query
550- * is a bug.
636+ * calls to `iter`, `all`, `first`, `execute`,
637+ * or `columns` will fail.
638+ *
639+ * Using iterators which were previously returned
640+ * from the finalized query will fail.
551641 *
552642 * `finalize` may safely be called multiple
553643 * times.
0 commit comments