• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ngageoint / geopackage-js / 4026249674

pending completion
4026249674

push

github

Christopher Caldwell
update typescript, fix linter errors and remove module comments

3590 of 8008 branches covered (44.83%)

Branch coverage included in aggregate %.

1218 of 1218 new or added lines in 92 files covered. (100.0%)

15084 of 20419 relevant lines covered (73.87%)

1570.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.71
/lib/db/master/sqliteMaster.ts
1
/**
2
 * SQLite Master table queries (sqlite_master)
3
 */
4
import { SQLiteMasterColumn } from './sqliteMasterColumn';
1✔
5
import { SQLiteMasterType } from './sqliteMasterType';
1✔
6
import { TableConstraints } from '../table/tableConstraints';
1✔
7
import { ConstraintParser } from '../table/constraintParser';
1✔
8
import { SQLiteMasterQuery } from './sqliteMasterQuery';
1✔
9
import type { GeoPackageConnection } from '../geoPackageConnection';
10
import { GeoPackageException } from '../../geoPackageException';
1✔
11

12
export class SQLiteMaster {
1✔
13
  /**
14
   * Table Name
15
   */
16
  static TABLE_NAME = 'sqlite_master';
1✔
17

18
  /**
19
   * SQLiteMaster query results
20
   */
21
  _results: any[];
22

23
  /**
24
   * Mapping between result columns and indices
25
   */
26
  _columns = {};
11,915✔
27

28
  /**
29
   * Query result count
30
   */
31
  _count: number;
32

33
  /**
34
   * Constructor
35
   * @param results query results
36
   * @param columns query columns
37
   */
38
  constructor(results: any[], columns: SQLiteMasterColumn[]) {
39
    if (columns !== null && columns !== undefined && columns.length > 0) {
11,915✔
40
      this._results = results;
5,234✔
41
      this._count = results.length;
5,234✔
42
      for (let i = 0; i < columns.length; i++) {
5,234✔
43
        this._columns[SQLiteMasterColumn.nameFromType(columns[i])] = i;
26,033✔
44
      }
45
    } else {
46
      // Count only result
47
      this._results = [];
6,681✔
48
      this._count = results[0].cnt;
6,681✔
49
    }
50
  }
51

52
  /**
53
   * Result count
54
   * @return count
55
   */
56
  count(): number {
1✔
57
    return this._count;
17,161✔
58
  }
59

60
  /**
61
   * Get the columns in the result
62
   * @return columns
63
   */
64
  columns(): SQLiteMasterColumn[] {
1✔
65
    return Object.keys(this._columns).map((key) => SQLiteMasterColumn.fromName(key));
×
66
  }
67

68
  /**
69
   * Get the type
70
   * @param row row index
71
   * @return type
72
   */
73
  getType(row: number): SQLiteMasterType {
1✔
74
    return SQLiteMasterType.fromName(this.getTypeString(row).toUpperCase());
5,223✔
75
  }
76

77
  /**
78
   * Get the type string
79
   * @param row row index
80
   * @return type string
81
   */
82
  getTypeString(row: number): string {
1✔
83
    return this.getValueForRowAndColumn(row, SQLiteMasterColumn.TYPE);
5,223✔
84
  }
85

86
  /**
87
   * Get the name
88
   * @param row row index
89
   * @return name
90
   */
91
  getName(row: number): string {
1✔
92
    return this.getValueForRowAndColumn(row, SQLiteMasterColumn.NAME);
60✔
93
  }
94

95
  /**
96
   * Get the table name
97
   * @param row row index
98
   * @return name
99
   */
100
  getTableName(row: number): string {
1✔
101
    return this.getValueForRowAndColumn(row, SQLiteMasterColumn.TBL_NAME);
5✔
102
  }
103

104
  /**
105
   * Get the rootpage
106
   * @param row row index
107
   * @return name
108
   */
109
  getRootpage(row: number): number {
1✔
110
    return this.getValueForRowAndColumn(row, SQLiteMasterColumn.ROOTPAGE);
×
111
  }
112

113
  /**
114
   * Get the sql
115
   * @param row row index
116
   * @return name
117
   */
118
  getSql(row: number): string {
1✔
119
    return this.getValueForRowAndColumn(row, SQLiteMasterColumn.SQL);
5,206✔
120
  }
121

122
  /**
123
   * Get the value of the column at the row index
124
   *
125
   * @param row row index
126
   * @param column column type
127
   * @return value
128
   */
129
  getValueForRowAndColumn(row: number, column: SQLiteMasterColumn): any {
1✔
130
    return SQLiteMaster.getValue(this.getRow(row), column);
10,494✔
131
  }
132

133
  /**
134
   * Get the row at the row index
135
   * @param row row index
136
   * @return row column values
137
   */
138
  getRow(row: number): any[] {
1✔
139
    if (row < 0 || row >= this._results.length) {
10,494!
140
      let message;
×
141
      if (this._results.length === 0) {
×
142
        message = 'Results are empty';
×
143
      } else {
144
        message = 'Row index: ' + row + ', not within range 0 to ' + (this._results.length - 1);
×
145
      }
146
      throw new GeoPackageException(message);
×
147
    }
148
    return this._results[row];
10,494✔
149
  }
150

151
  /**
152
   * Get the value in the row at the column index
153
   * @param row row
154
   * @param column column type
155
   * @return value
156
   */
157
  static getValue(row: any[], column: SQLiteMasterColumn): any {
1✔
158
    return row[SQLiteMasterColumn.nameFromType(column).toLowerCase()];
10,494✔
159
  }
160

161
  /**
162
   * Get the constraints from table SQL
163
   * @param row row index
164
   * @return constraints
165
   */
166
  getConstraints(row: number): TableConstraints {
1✔
167
    let constraints = new TableConstraints();
5,180✔
168
    if (this.getType(row) === SQLiteMasterType.TABLE) {
5,180!
169
      const sql = this.getSql(row);
5,180✔
170
      if (sql !== null && sql !== undefined) {
5,180!
171
        constraints = ConstraintParser.getConstraints(sql);
5,180✔
172
      }
173
    }
174
    return constraints;
5,180✔
175
  }
176

177
  /**
178
   * Count the sqlite_master table
179
   *
180
   * @param db connection
181
   * @param types result types
182
   * @param query query
183
   * @return count
184
   */
185
  static count(db: GeoPackageConnection, types: SQLiteMasterType[], query: SQLiteMasterQuery): number {
1✔
186
    return SQLiteMaster.query(db, null, types, query).count();
6,681✔
187
  }
188

189
  /**
190
   * Query the sqlite_master table
191
   *
192
   * @param db connection
193
   * @param columns result columns
194
   * @param types result types
195
   * @param query query
196
   * @return SQLiteMaster result
197
   */
198
  static query(
1✔
199
    db: GeoPackageConnection,
200
    columns: SQLiteMasterColumn[],
201
    types: SQLiteMasterType[],
202
    query: SQLiteMasterQuery,
203
  ): SQLiteMaster {
204
    let sql = 'SELECT ';
11,915✔
205
    const args = [];
11,915✔
206

207
    if (columns !== null && columns !== undefined && columns.length > 0) {
11,915✔
208
      for (let i = 0; i < columns.length; i++) {
5,234✔
209
        if (i > 0) {
26,033✔
210
          sql = sql.concat(', ');
20,799✔
211
        }
212
        sql = sql.concat(SQLiteMasterColumn.nameFromType(columns[i]).toLowerCase());
26,033✔
213
      }
214
    } else {
215
      sql = sql.concat('count(*) as cnt');
6,681✔
216
    }
217

218
    sql = sql.concat(' FROM ');
11,915✔
219
    sql = sql.concat(SQLiteMaster.TABLE_NAME);
11,915✔
220

221
    const hasQuery = query !== null && query !== undefined && query.has();
11,915✔
222
    const hasTypes = types !== null && types !== undefined && types.length > 0;
11,915✔
223

224
    if (hasQuery || hasTypes) {
11,915✔
225
      sql = sql.concat(' WHERE ');
11,914✔
226

227
      if (hasQuery) {
11,914!
228
        sql = sql.concat(query.buildSQL());
11,914✔
229
        args.push(...query.getArguments());
11,914✔
230
      }
231

232
      if (hasTypes) {
11,914✔
233
        if (hasQuery) {
11,906!
234
          sql = sql.concat(' AND');
11,906✔
235
        }
236

237
        sql = sql.concat(' type IN (');
11,906✔
238
        for (let i = 0; i < types.length; i++) {
11,906✔
239
          if (i > 0) {
16,568✔
240
            sql = sql.concat(', ');
4,662✔
241
          }
242
          sql = sql.concat('?');
16,568✔
243
          args.push(SQLiteMasterType.nameFromType(types[i]).toLowerCase());
16,568✔
244
        }
245
        sql = sql.concat(')');
11,906✔
246
      }
247
    }
248

249
    const results = db.all(sql, args);
11,915✔
250
    return new SQLiteMaster(results, columns);
11,915✔
251
  }
252

253
  /**
254
   * Query the sqlite_master views on the table
255
   * @param db connection
256
   * @param columns result columns
257
   * @param tableName table name
258
   * @return SQLiteMaster result
259
   */
260
  static queryViewsOnTable(db: GeoPackageConnection, columns: SQLiteMasterColumn[], tableName: string): SQLiteMaster {
1✔
261
    return SQLiteMaster.query(db, columns, [SQLiteMasterType.VIEW], SQLiteMasterQuery.createTableViewQuery(tableName));
23✔
262
  }
263

264
  /**
265
   * Count the sqlite_master views on the table
266
   * @param db connection
267
   * @param tableName table name
268
   * @return count
269
   */
270
  static countViewsOnTable(db: GeoPackageConnection, tableName: string): number {
1✔
271
    return SQLiteMaster.count(db, [SQLiteMasterType.VIEW], SQLiteMasterQuery.createTableViewQuery(tableName));
1,483✔
272
  }
273

274
  /**
275
   * Query for the table constraints
276
   * @param db  connection
277
   * @param tableName able name
278
   * @return SQL constraints
279
   */
280
  static queryForConstraints(db: GeoPackageConnection, tableName: string): TableConstraints {
1✔
281
    const constraints = new TableConstraints();
5,181✔
282
    const tableMaster = SQLiteMaster.query(
5,181✔
283
      db,
284
      [
285
        SQLiteMasterColumn.TYPE,
286
        SQLiteMasterColumn.NAME,
287
        SQLiteMasterColumn.TBL_NAME,
288
        SQLiteMasterColumn.ROOTPAGE,
289
        SQLiteMasterColumn.SQL,
290
      ],
291
      [SQLiteMasterType.TABLE],
292
      SQLiteMasterQuery.createForColumnValue(SQLiteMasterColumn.TBL_NAME, tableName),
293
    );
294
    for (let i = 0; i < tableMaster.count(); i++) {
5,181✔
295
      constraints.addTableConstraints(tableMaster.getConstraints(i).constraints);
5,180✔
296
    }
297
    return constraints;
5,181✔
298
  }
299
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc