Skip to content

Commit

Permalink
Merge pull request #63 from hasura/djh/table-introspection
Browse files Browse the repository at this point in the history
Basic table introspection
  • Loading branch information
danieljharvey authored Nov 9, 2023
2 parents 359eb9a + 0705070 commit 4617bd9
Show file tree
Hide file tree
Showing 11 changed files with 2,914 additions and 561 deletions.
115 changes: 0 additions & 115 deletions crates/ndc-sqlserver/src/configuration.rs

This file was deleted.

49 changes: 49 additions & 0 deletions crates/ndc-sqlserver/src/configuration/introspection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//! Configuration and state for our connector.
use serde::Deserialize;

#[derive(Deserialize, Debug)]
pub struct IntrospectionTable {
pub name: String,
pub type_desc: String,
pub joined_sys_schema: IntrospectionSchema,
pub joined_sys_column: Vec<IntrospectionColumn>,
pub joined_sys_primary_key: Option<IntrospectionPrimaryKey>,
}

#[derive(Deserialize, Debug)]
pub struct IntrospectionColumn {
pub name: String,
pub is_nullable: bool,
pub is_identity: bool,
pub is_computed: bool,
pub joined_sys_type: IntrospectionType,
pub joined_foreign_key_columns: Vec<IntrospectionForeignKeyColumn>,
}

#[derive(Deserialize, Debug)]
pub struct IntrospectionType {
pub name: String,
}

#[derive(Deserialize, Debug)]
pub struct IntrospectionPrimaryKey {
pub name: String,
pub columns: Vec<IntrospectionPrimaryKeyColumn>,
}

#[derive(Deserialize, Debug)]
pub struct IntrospectionPrimaryKeyColumn {
pub name: String,
}

#[derive(Deserialize, Debug)]
pub struct IntrospectionForeignKeyColumn {
pub joined_referenced_table_name: String,
pub joined_referenced_column_name: String,
pub joined_referenced_sys_schema: IntrospectionSchema,
}

#[derive(Deserialize, Debug)]
pub struct IntrospectionSchema {
pub name: String,
}
7 changes: 7 additions & 0 deletions crates/ndc-sqlserver/src/configuration/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub mod introspection;
pub mod version1;

pub use version1::{
configure, create_state, occurring_scalar_types, validate_raw_configuration, Configuration,
InitializationError, RawConfiguration, State,
};
60 changes: 60 additions & 0 deletions crates/ndc-sqlserver/src/configuration/table_configuration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
-- SCHEMA_NAME(..)
SELECT ISNULL(
(SELECT object.name, object.schema_id, object.object_id, object.type_desc,
JSON_QUERY([schema].json) AS [joined_sys_schema],
JSON_QUERY([column].json) AS [joined_sys_column],
JSON_QUERY([primary_key].json) AS [joined_sys_primary_key]
FROM sys.objects object
CROSS APPLY (SELECT [column].name, [column].column_id, [column].is_nullable, [column].is_identity, [column].is_computed, [column].user_type_id,
JSON_QUERY([types].json) AS [joined_sys_type],
JSON_QUERY(ISNULL([relationships].json,'[]')) AS [joined_foreign_key_columns]
FROM sys.columns [column]
CROSS APPLY (SELECT name, schema_id, user_type_id FROM sys.types [type]
WHERE [type].user_type_id = [column].user_type_id
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER)
AS [types](json)
CROSS APPLY (SELECT fk.*,
referenced_table.name AS joined_referenced_table_name,
referenced_column.name AS joined_referenced_column_name,
JSON_QUERY([schema].json) AS [joined_referenced_sys_schema]
FROM sys.foreign_key_columns AS [fk],
sys.columns AS referenced_column,
sys.objects AS referenced_table
CROSS APPLY (SELECT [schema].name, [schema].schema_id
FROM sys.schemas [schema]
WHERE [schema].schema_id = [referenced_table].schema_id
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER)
AS [schema](json)
WHERE [object].object_id = fk.parent_object_id
AND [referenced_table].object_id = fk.referenced_object_id
AND [referenced_column].object_id = [referenced_table].object_id
AND [referenced_column].column_id = fk.referenced_column_id
AND [column].column_id = fk.parent_column_id
FOR JSON PATH)
AS [relationships](json)
WHERE [column].object_id = object.object_id
FOR JSON PATH)
AS [column](json)
CROSS APPLY (SELECT [schema].name, [schema].schema_id
FROM sys.schemas [schema]
WHERE [schema].schema_id = object.schema_id
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER)
AS [schema](json)
CROSS APPLY (SELECT pk.name, pk.index_id, JSON_QUERY([cols].json) AS columns
FROM sys.indexes pk
CROSS APPLY (SELECT col.name
FROM sys.index_columns ic
INNER JOIN sys.columns col
ON col.column_id = ic.column_id
AND col.object_id = ic.object_id
WHERE ic.object_id = pk.object_id
AND ic.index_id = pk.index_id
FOR JSON PATH)
AS [cols](json)
WHERE pk.object_id = object.object_id
AND pk.is_primary_key = 1
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER)
AS [primary_key](json)
WHERE object.type_desc IN ('USER_TABLE', 'VIEW')
FOR JSON PATH)
, '[]')
Loading

0 comments on commit 4617bd9

Please sign in to comment.