MCPcopy Create free account
hub / github.com/bytebase/dbhub / getTableIndexes

Method getTableIndexes

src/connectors/sqlite/index.ts:312–384  ·  view source on GitHub ↗
(tableName: string, schema?: string)

Source from the content-addressed store, hash-verified

310 }
311
312 async getTableIndexes(tableName: string, schema?: string): Promise<TableIndex[]> {
313 if (!this.db) {
314 throw new Error("Not connected to SQLite database");
315 }
316
317 // In SQLite, schema parameter is ignored (no schema concept)
318 try {
319 // Get all indexes for the specified table
320 const indexInfoRows = this.queryAll<{ index_name: string; is_unique: number }>(
321 `
322 SELECT
323 name as index_name,
324 0 as is_unique
325 FROM sqlite_master
326 WHERE type = 'index'
327 AND tbl_name = ?
328 `,
329 tableName
330 );
331
332 // Get unique info from PRAGMA index_list which provides the unique flag
333 // Note: PRAGMA commands require proper identifier quoting for special characters
334 const quotedTableName = quoteIdentifier(tableName, "sqlite");
335 const indexListRows = this.queryAll<{ name: string; unique: number }>(
336 `PRAGMA index_list(${quotedTableName})`
337 );
338
339 // Create a map of index names to unique status
340 const indexUniqueMap = new Map<string, boolean>();
341 for (const indexListRow of indexListRows) {
342 indexUniqueMap.set(indexListRow.name, indexListRow.unique === 1);
343 }
344
345 // Get the primary key info
346 const tableInfo = this.queryAll<SQLiteTableInfo>(`PRAGMA table_info(${quotedTableName})`);
347
348 // Find primary key columns
349 const pkColumns = tableInfo.filter((col) => col.pk > 0).map((col) => col.name);
350
351 const results: TableIndex[] = [];
352
353 // Add regular indexes
354 for (const indexInfo of indexInfoRows) {
355 // Get the columns for this index
356 const quotedIndexName = quoteIdentifier(indexInfo.index_name, "sqlite");
357 const indexDetailRows = this.queryAll<{ name: string }>(
358 `PRAGMA index_info(${quotedIndexName})`
359 );
360 const columnNames = indexDetailRows.map((row) => row.name);
361
362 results.push({
363 index_name: indexInfo.index_name,
364 column_names: columnNames,
365 is_unique: indexUniqueMap.get(indexInfo.index_name) || false,
366 is_primary: false,
367 });
368 }
369

Callers

nothing calls this directly

Calls 2

queryAllMethod · 0.95
quoteIdentifierFunction · 0.85

Tested by

no test coverage detected