(table: string)
| 176 | } |
| 177 | |
| 178 | async getColumns(table: string): Promise<Column[]> { |
| 179 | try { |
| 180 | const db = this.getConnection(); |
| 181 | if (!db) { |
| 182 | return Promise.reject('Cannot connect to database') |
| 183 | } |
| 184 | |
| 185 | type TableColumn = { "type": string, name: string, notnull: number, pk: number } |
| 186 | |
| 187 | const columnsPromise = new Promise<TableColumn[]>((resolve, reject) => { |
| 188 | db.all(`PRAGMA table_info(${this.escapeIdentifier(table)})`, (err, rows) => { |
| 189 | if (err) { |
| 190 | reportError(`SQLite get columns error: ${err}`); |
| 191 | reject(err); |
| 192 | return; |
| 193 | } |
| 194 | resolve(rows); |
| 195 | }); |
| 196 | }); |
| 197 | |
| 198 | // Get foreign key information |
| 199 | const foreignKeysPromise = new Promise<any[]>((resolve, reject) => { |
| 200 | db.all(`PRAGMA foreign_key_list(${this.escapeIdentifier(table)})`, (err, rows) => { |
| 201 | if (err) { |
| 202 | reportError(`SQLite get foreign keys error: ${err}`); |
| 203 | reject(err); |
| 204 | return; |
| 205 | } |
| 206 | resolve(rows); |
| 207 | }); |
| 208 | }); |
| 209 | |
| 210 | const [columns, foreignKeys] = await Promise.all([columnsPromise, foreignKeysPromise]); |
| 211 | |
| 212 | const editableColumnTypeNamesLowercase = this.getEditableColumnTypeNamesLowercase() |
| 213 | |
| 214 | return columns.map((column): Column => { |
| 215 | const foreignKey = foreignKeys.find(fk => fk.from === column.name); |
| 216 | |
| 217 | const col: Column = { |
| 218 | name: column.name, |
| 219 | type: column.type, |
| 220 | isNullable: column.notnull === 0, |
| 221 | isPrimaryKey: column.pk > 0, |
| 222 | isNumeric: this.getNumericColumnTypeNamesLowercase().includes(column.type.toLowerCase()), |
| 223 | isPlainTextType: this.getPlainStringTypes().includes(column.type.toLowerCase()), |
| 224 | isEditable: editableColumnTypeNamesLowercase.includes(column.type.toLowerCase()) || editableColumnTypeNamesLowercase.some(edtiableColumn => column.type.toLowerCase().startsWith(edtiableColumn)), |
| 225 | foreignKey: foreignKey ? { |
| 226 | table: foreignKey.table, |
| 227 | column: foreignKey.to |
| 228 | } : undefined |
| 229 | } |
| 230 | |
| 231 | return col |
| 232 | }); |
| 233 | } catch (err) { |
| 234 | reportError(`SQLite get columns error: ${err}`); |
| 235 | return []; |
nothing calls this directly
no test coverage detected