feat(spanner): add Google Cloud Spanner support

This commit is contained in:
Frost authored and GitHub committed 2026-08-20 16:33:09 +08:00
1 parent 2cc8d78e64
commit f494f90c30
74 files changed
+2920 -53

No files matched your search

@@ -2,6 +2,9 @@ import type { DatabaseType } from "@/types/database";
export const SCHEMA_AWARE_TYPES = new Set<DatabaseType>([
"postgres",
// Cloud Spanner supports named schemas; GoogleSQL's default schema is the empty string, which
// `spannerObjectTreeSchema` keeps intact through the `schema || database` fallbacks.
"spanner",
"sqlserver",
"oracle",
"redshift",
@@ -55,6 +58,12 @@ export const FETCH_FIRST_TYPES = new Set<DatabaseType>(["oracle", "dameng"]);
export const TREE_SCHEMA_TYPES = new Set<DatabaseType>([
"postgres",
// Cloud Spanner needs the schema level for the same reason it is in SCHEMA_AWARE_TYPES: named
// schemas exist and are queryable, so the tree has to expose them. Membership here is what makes
// a database node load schemas instead of tables; SCHEMA_AWARE_TYPES alone only reaches the
// schema pickers in dialogs. The sites gated on `database === ""` stay unreachable because a
// Spanner database is always the resource path.
"spanner",
"redshift",
"sqlserver",
"db2",
@@ -64,8 +64,33 @@ export function databaseObjectTreeQuerySchema(dbType: DatabaseType | undefined,
return schema || database;
}
/**
* Cloud Spanner is the one schema-aware type whose default schema is the empty string: that is the
* literal name of GoogleSQL's user schema, and the agent forwards it to the driver verbatim. Every
* `schema || database` fallback therefore has to be bypassed, because `database` holds a resource
* path (`projects/…/databases/db`) that is never a schema name and matches no metadata.
*
* Named schemas (Spanner 2024+) pass through unchanged. Callers that already collapsed
* `schema || node.database` are normalized back to the blank schema, which is safe because a Spanner
* schema identifier is letters, digits and underscores and can never contain the path separator.
*/
export function spannerObjectTreeSchema(schema?: string): string {
return schema && !schema.includes("/") ? schema : "";
}
/**
* Whether a schema tree node carries a name its children can be loaded for. Cloud Spanner is the one
* type where the empty string is a real schema name (GoogleSQL's user schema), so a plain truthiness
* check would leave that node expandable but permanently empty. Every other type keeps the
* truthiness test, which also filters the undefined schema on nodes that have no schema level.
*/
export function schemaNodeHasLoadableName(dbType: DatabaseType | undefined, schema?: string): boolean {
return dbType === "spanner" ? schema != null : !!schema;
}
export function databaseObjectTreeNodeSchema(dbType: DatabaseType | undefined, database: string, schema?: string): string | undefined {
if (usesDatabaseObjectTreeMode(dbType)) return undefined;
if (dbType === "spanner") return spannerObjectTreeSchema(schema);
if (schema) return schema;
return isSchemaAware(dbType) ? database : undefined;
}
@@ -75,6 +75,7 @@ export const DATABASE_NAMESPACE_CREATION_MATRIX = {
neo4j: { deferred: "database creation depends on edition/admin privileges" },
cassandra: { deferred: "keyspace creation requires replication options" },
bigquery: { deferred: "dataset creation needs project/location options" },
spanner: { deferred: "database creation requires the Cloud Spanner Admin API" },
kylin: { deferred: "project/model lifecycle is not SQL database creation" },
ignite: { deferred: "schema lifecycle is managed through cluster/cache configuration" },
ignite3: { deferred: "schema lifecycle is managed through cluster configuration" },
@@ -77,6 +77,9 @@ const DATABASE_TYPE_OBJECTS = new Map<DatabaseType, SidebarObjectKind[]>([
["prestosql", TABLE_VIEW_OBJECTS],
["cassandra", TABLE_VIEW_OBJECTS],
["bigquery", TABLE_VIEW_OBJECTS],
// Cloud Spanner has no triggers, routines, sequences, or synonyms; without an
// explicit entry the sidebar would fall back to ROUTINE_OBJECTS.
["spanner", TABLE_VIEW_OBJECTS],
["kylin", TABLE_VIEW_OBJECTS],
["ignite", TABLE_VIEW_OBJECTS],
["ignite3", TABLE_VIEW_OBJECTS],
@@ -75,6 +75,7 @@ export const DATABASE_PROPERTY_EDITING_MATRIX = {
neo4j: { deferred: "database properties depend on edition/admin privileges" },
cassandra: { deferred: "keyspace properties require replication option handling" },
bigquery: { deferred: "dataset properties need project/location-specific handling" },
spanner: { deferred: "database/schema properties are Cloud Spanner Admin API operations" },
kylin: { deferred: "project/model lifecycle is not SQL database property editing" },
ignite: { deferred: "schema properties need product-specific handling" },
ignite3: { deferred: "schema properties need product-specific handling" },
@@ -84,6 +84,7 @@ const NAVICAT_STYLE_TABLE_DATA_TYPES = new Set<DatabaseType>([
"db2",
"informix",
"bigquery",
"spanner",
"sundb",
"oscar",
"databend",
+10 -2
View File
@@ -19,14 +19,22 @@ export function sortSidebarDatabases(databases: readonly DatabaseInfo[]): Databa
return [...databases].sort((left, right) => sidebarNameCollator.compare(left.name, right.name));
}
export function buildDatabaseTreeNodes(connectionId: string, databases: DatabaseInfo[], options: { includeDefaultWhenEmpty?: boolean } = {}): TreeNode[] {
export function buildDatabaseTreeNodes(
connectionId: string,
databases: DatabaseInfo[],
// `displayLabel` only rewrites the visible text. `id` and `database` keep the
// backend-reported name because that value round-trips to the agent — Cloud
// Spanner reports the full `projects/../instances/../databases/..` resource
// path and would break URL building if the shortened label were sent back.
options: { includeDefaultWhenEmpty?: boolean; displayLabel?: (name: string) => string } = {},
): TreeNode[] {
const nodes = sortSidebarDatabases(databases).flatMap((db) => {
const name = db.name;
if (!name.trim()) return [];
return [
{
id: `${connectionId}:${name}`,
label: name,
label: options.displayLabel?.(name) || name,
type: "database" as const,
connectionId,
database: name,
@@ -9,6 +9,10 @@ export interface DropObjectSqlOptions {
schema?: string | null;
name: string;
signature?: string | null;
/** Quote character reported by the connected server, for types whose quote is not fixed by the
* database type alone (Cloud Spanner's two dialects differ). Mirrors `identifierQuote` on the
* table-data SQL options. */
identifierQuote?: string;
}
export interface TableAdminSqlOptions {
@@ -16,6 +20,10 @@ export interface TableAdminSqlOptions {
schema?: string | null;
tableName: string;
cascade?: boolean;
/** Quote character reported by the connected server, for types whose quote is not fixed by the
* database type alone (Cloud Spanner's two dialects differ). Mirrors `identifierQuote` on the
* table-data SQL options. */
identifierQuote?: string;
}
export interface VacuumTableSqlOptions {
@@ -75,6 +83,10 @@ export interface DuplicateTableStructureSqlOptions {
targetName: string;
tableComment?: string | null;
columnComments?: Array<{ name: string; comment: string }>;
/** Quote character reported by the connected server, for types whose quote is not fixed by the
* database type alone (Cloud Spanner's two dialects differ). Mirrors `identifierQuote` on the
* table-data SQL options. */
identifierQuote?: string;
}
export interface DuplicateTableStructurePlanOptions extends DuplicateTableStructureSqlOptions {
@@ -259,6 +271,7 @@ export async function buildDuplicateTableStructurePlan(options: DuplicateTableSt
targetName: options.targetName,
tableComment: options.tableComment,
columnComments: [],
identifierQuote: options.identifierQuote,
});
return { sql, sourceColumns: options.sourceColumns, executeAsScript: duplicateTableStructureRequiresScript(sql) };
}
@@ -272,6 +285,10 @@ export interface CopyTableDataSqlOptions {
postgresOverridingSystemValue?: boolean;
sqlserverIdentityInsert?: boolean;
normalizeNewTargetName?: boolean;
/** Quote character reported by the connected server, for types whose quote is not fixed by the
* database type alone (Cloud Spanner's two dialects differ). Mirrors `identifierQuote` on the
* table-data SQL options. */
identifierQuote?: string;
}
export function buildDropObjectSql(options: DropObjectSqlOptions): Promise<string> {
+23 -2
View File
@@ -1,5 +1,5 @@
import type { ConnectionConfig, DatabaseType } from "@/types/database";
import { isSchemaAware, usesDatabaseObjectTreeMode, usesTreeSchemaMode } from "@/lib/database/databaseFeatureSupport";
import { isSchemaAware, spannerObjectTreeSchema, usesDatabaseObjectTreeMode, usesTreeSchemaMode } from "@/lib/database/databaseFeatureSupport";
import type { CodeMirrorSqlDialectName } from "@/lib/editor/codemirrorSqlDialect";
type JdbcDialectConnection = Partial<Pick<ConnectionConfig, "db_type" | "driver_profile" | "driver_label" | "connection_string" | "url_params" | "jdbc_driver_class" | "jdbc_driver_paths" | "database_info" | "external_config">>;
@@ -81,6 +81,12 @@ export function connectionShouldLoadIdentifierQuote(connection: JdbcDialectConne
if (!connection) return false;
if (connection.db_type === "gbase" && isGbase8sProfile(connection.driver_profile)) return true;
if (connection.db_type === "kingbase") return true;
// Cloud Spanner is dual-dialect: the agent reports a backtick for GoogleSQL and
// a double quote for PostgreSQL-dialect databases. The backend counts Spanner
// unconditionally in `uses_connection_identifier_quote`, so the UI must fetch
// the reported quote or every locally built statement would use the GoogleSQL
// default against a PostgreSQL-dialect database.
if (connection.db_type === "spanner") return true;
if (gaussdbIdentifierQuoteStyle(connection) !== "auto") return false;
if (connection.db_type === "gaussdb") return true;
if (connection.db_type !== "jdbc") return false;
@@ -207,10 +213,24 @@ export function connectionQueryExecutionSchema(connection: JdbcDialectConnection
export function connectionObjectTreeQuerySchema(connection: JdbcDialectConnection | undefined, database: string, schema?: string): string {
if (connection?.db_type === "jdbc" && inferJdbcDialect(connection) === "databend") return schema || database;
if (connectionUsesDatabaseObjectTreeMode(connection)) return "";
if (effectiveDatabaseTypeForConnection(connection) === "informix") return schema || "";
const type = effectiveDatabaseTypeForConnection(connection);
if (type === "informix") return schema || "";
if (type === "spanner") return spannerObjectTreeSchema(schema);
return schema || database;
}
/**
* Metadata schema for query paths that treat the database name as the schema when a
* connection exposes no schema level (MySQL, HBase, and the other flat engines).
* Cloud Spanner is the exception: its database is a resource path, so the blank
* GoogleSQL schema has to be sent instead of the path. Behavior for every other
* database type is exactly `schema || database`.
*/
export function connectionDatabaseMetadataSchema(connection: JdbcDialectConnection | undefined, database: string, schema?: string): string {
if (schema) return schema;
return effectiveDatabaseTypeForConnection(connection) === "spanner" ? "" : database;
}
export function metadataSchemaForConnection(connection: JdbcDialectConnection | undefined, database: string, schema?: string): string {
const type = effectiveDatabaseTypeForConnection(connection);
if (type === "sqlserver") return schema || "dbo";
@@ -223,6 +243,7 @@ export function connectionObjectTreeNodeSchema(connection: JdbcDialectConnection
const type = effectiveDatabaseTypeForConnection(connection);
if (type === "informix") return schema || undefined;
if (type === "sqlite") return schema || database;
if (type === "spanner") return spannerObjectTreeSchema(schema);
if (!type) return schema;
return isSchemaAware(type) ? schema || database : undefined;
}