import * as path from 'path' ; import { INode } from "./INode" ; import { IConnection } from "../common/IConnection" ; import { TreeItem , TreeItemCollapsibleState } from "vscode" ; import { Database } from '../common/database' ; import { InfoNode } from './infoNode' ; import { ColumnNode } from './columnNode' ; import { Global } from '../common/global' ; import { QueryResult } from 'pg' ; import { SqlQueryManager } from '../queries' ; export class TableNode implements INode { constructor ( public readonly connection , public readonly table , public readonly is_table , public readonly is_foreign , public readonly schema ? ) { } public getQuotedTableName ( ) { let quotedSchema = this . schema && this . schema !== 'public' ? Database . getQuotedIdent ( this . schema ) : null ; let quotedTable = Database . getQuotedIdent ( this . table ) ; return quotedSchema ? `${ quotedSchema }.${ quotedTable }` : quotedTable ; } public getTreeItem ( ) { let iconName = 'table' ; if ( this . is_table && this . is_foreign ) iconName = 'fdw_table' ; else if ( ! this . is_table ) iconName = 'view' ; return { label : this . table , collapsibleState : TreeItemCollapsibleState . Collapsed , contextValue : 'vscode-postgres.tree.table' , iconPath : { light : path . join ( __dirname , `../../resources/light/${ iconName }.svg` ) , dark : path . join ( __dirname , `../../resources/dark/${ iconName }.svg` ) } } ; } public async getChildren ( ) { const connection = await Database . createConnection ( this . connection ) ; const configSort = Global . Configuration . get < string > ( "tableColumnSortOrder" ) ; const sortOptions = { "db-order" : 'a.attnum' , "alpha" : 'a.attname' , "reverse-alpha" : 'a.attname DESC' } ; if ( ! sortOptions [ configSort ] ) sortOptions [ configSort ] = 'a.attnum' ; let tableSchema = this . schema ? this . schema : 'public' ; let query = SqlQueryManager . getVersionQueries ( connection . pg_version ) ; try { let res = null ; res = await connection . query ( query . format ( query . TableColumns , sortOptions [ configSort ] ) , [ this . getQuotedTableName ( ) , this . connection . database , tableSchema , this . table ] ) ; return res . rows . map < ColumnNode > ( column => { return new ColumnNode ( this . connection , this . table , column ) ; } ) ; } catch ( err ) { return [ new InfoNode ( err ) ] ; } finally { await connection . end ( ) ; } } }