SQL for fetching primary keys, needs merged into column SQL
This commit is contained in:
parent
11c52f26ad
commit
f62c55c58f
|
@ -8,7 +8,7 @@
|
|||
|
||||
const tabs = [
|
||||
{
|
||||
title: "Internal",
|
||||
title: "Databases",
|
||||
key: "table",
|
||||
},
|
||||
{
|
||||
|
@ -17,7 +17,7 @@
|
|||
},
|
||||
]
|
||||
|
||||
let selected = $isActive("./datasource") ? "External" : "Internal"
|
||||
let selected = $isActive("./datasource") ? "External" : "Databases"
|
||||
|
||||
function selectFirstTableOrSource({ detail }) {
|
||||
const { key } = tabs.find(t => t.title === detail)
|
||||
|
@ -35,7 +35,7 @@
|
|||
<div class="root">
|
||||
<div class="nav">
|
||||
<Tabs {selected} on:select={selectFirstTableOrSource}>
|
||||
<Tab title="Internal">
|
||||
<Tab title="Databases">
|
||||
<div class="tab-content-padding">
|
||||
<TableNavigator />
|
||||
<Modal bind:this={modal}>
|
||||
|
|
|
@ -59,6 +59,16 @@ class PostgresPlus extends Sql {
|
|||
COLUMNS_SQL =
|
||||
"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) {
|
||||
super("pg")
|
||||
this.config = config
|
||||
|
@ -70,17 +80,24 @@ class PostgresPlus extends Sql {
|
|||
}
|
||||
|
||||
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 = {}
|
||||
for (let column of response.rows) {
|
||||
|
||||
for (let column of columnsResponse.rows) {
|
||||
const tableName = column.table_name
|
||||
const columnName = column.column_name
|
||||
|
||||
// table key doesn't exist yet
|
||||
if (!tables[tableName]) {
|
||||
tables[tableName] = {
|
||||
_id: "something",
|
||||
_id: primaryKeys[tableName],
|
||||
name: tableName,
|
||||
schema: {},
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue