SQL for fetching primary keys, needs merged into column SQL

This commit is contained in:
Martin McKeaveney 2021-06-14 20:05:18 +01:00
parent 4950db460c
commit 760dc88ef3
2 changed files with 23 additions and 6 deletions

View File

@ -8,7 +8,7 @@
const tabs = [ const tabs = [
{ {
title: "Internal", title: "Databases",
key: "table", key: "table",
}, },
{ {
@ -17,7 +17,7 @@
}, },
] ]
let selected = $isActive("./datasource") ? "External" : "Internal" let selected = $isActive("./datasource") ? "External" : "Databases"
function selectFirstTableOrSource({ detail }) { function selectFirstTableOrSource({ detail }) {
const { key } = tabs.find(t => t.title === detail) const { key } = tabs.find(t => t.title === detail)
@ -35,7 +35,7 @@
<div class="root"> <div class="root">
<div class="nav"> <div class="nav">
<Tabs {selected} on:select={selectFirstTableOrSource}> <Tabs {selected} on:select={selectFirstTableOrSource}>
<Tab title="Internal"> <Tab title="Databases">
<div class="tab-content-padding"> <div class="tab-content-padding">
<TableNavigator /> <TableNavigator />
<Modal bind:this={modal}> <Modal bind:this={modal}>

View File

@ -59,6 +59,16 @@ class PostgresPlus extends Sql {
COLUMNS_SQL = COLUMNS_SQL =
"select * from information_schema.columns where table_schema = 'public'" "select * from information_schema.columns where table_schema = 'public'"
PRIMARY_KEYS_SQL = `
select tc.table_schema, tc.table_name, tc.column_name, kc.column_name as primary_key
from information_schema.table_constraints tc
join
information_schema.key_column_usage kc on kc.table_name = tc.table_name
and kc.table_schema = tc.table_schema
and kc.constraint_name = tc.constraint_name
where tc.constraint_type = 'PRIMARY KEY';
`
constructor(config) { constructor(config) {
super("pg") super("pg")
this.config = config this.config = config
@ -70,17 +80,24 @@ class PostgresPlus extends Sql {
} }
async init() { async init() {
const response = await this.client.query(this.COLUMNS_SQL) const primaryKeysResponse = await this.client.query(this.PRIMARY_KEYS_SQL)
const primaryKeys = {}
for (let table of primaryKeysResponse.rows) {
primaryKeys[table.primary_key] = table.column_name
}
const columnsResponse = await this.client.query(this.COLUMNS_SQL)
const tables = {} const tables = {}
for (let column of response.rows) {
for (let column of columnsResponse.rows) {
const tableName = column.table_name const tableName = column.table_name
const columnName = column.column_name const columnName = column.column_name
// table key doesn't exist yet // table key doesn't exist yet
if (!tables[tableName]) { if (!tables[tableName]) {
tables[tableName] = { tables[tableName] = {
_id: "something", _id: primaryKeys[tableName],
name: tableName, name: tableName,
schema: {}, schema: {},
} }