-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_connectors.R
More file actions
284 lines (270 loc) · 10.1 KB
/
db_connectors.R
File metadata and controls
284 lines (270 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# ============================================================
# db_connectors.R — Database Connection Functions
# Table Relationship Explorer
# ============================================================
# ── Supported database types ───────────────────────────────────
db_types <- c(
"PostgreSQL" = "postgres",
"MySQL / MariaDB" = "mysql",
"SQL Server" = "sqlserver",
"Snowflake" = "snowflake",
"BigQuery" = "bigquery",
"Redshift" = "redshift",
"SQLite" = "sqlite"
)
# ── Connect to a database ─────────────────────────────────────
db_connect <- function(type, host = "", port = NULL, dbname = "",
user = "", password = "", schema = "",
driver = "", project = "", dataset = "",
path = "", notify_fn = message) {
tryCatch(
switch(type,
postgres = {
if (!requireNamespace("RPostgres", quietly = TRUE)) {
stop("Install 'RPostgres': install.packages('RPostgres')")
}
DBI::dbConnect(
RPostgres::Postgres(),
host = host, port = port %||% 5432L,
dbname = dbname, user = user, password = password
)
},
mysql = {
if (!requireNamespace("RMariaDB", quietly = TRUE)) {
stop("Install 'RMariaDB': install.packages('RMariaDB')")
}
DBI::dbConnect(
RMariaDB::MariaDB(),
host = host, port = port %||% 3306L,
dbname = dbname, user = user, password = password
)
},
sqlserver = {
if (!requireNamespace("odbc", quietly = TRUE)) {
stop("Install 'odbc': install.packages('odbc')")
}
drv_name <- if (nzchar(driver)) driver else "ODBC Driver 17 for SQL Server"
DBI::dbConnect(
odbc::odbc(),
Driver = drv_name,
Server = host, Port = port %||% 1433L,
Database = dbname, UID = user, PWD = password
)
},
snowflake = {
if (!requireNamespace("odbc", quietly = TRUE)) {
stop("Install 'odbc': install.packages('odbc')")
}
drv_name <- if (nzchar(driver)) driver else "Snowflake"
DBI::dbConnect(
odbc::odbc(),
Driver = drv_name,
Server = host,
Database = dbname, Schema = schema,
UID = user, PWD = password
)
},
bigquery = {
if (!requireNamespace("bigrquery", quietly = TRUE)) {
stop("Install 'bigrquery': install.packages('bigrquery')")
}
DBI::dbConnect(
bigrquery::bigquery(),
project = project,
dataset = dataset
)
},
redshift = {
if (!requireNamespace("RPostgres", quietly = TRUE)) {
stop("Install 'RPostgres': install.packages('RPostgres')")
}
DBI::dbConnect(
RPostgres::Redshift(),
host = host, port = port %||% 5439L,
dbname = dbname, user = user, password = password
)
},
sqlite = {
DBI::dbConnect(RSQLite::SQLite(), dbname = path)
},
stop(paste0("Unsupported database type: ", type))
),
error = function(e) {
notify_fn(paste0("Connection failed: ", conditionMessage(e)))
NULL
}
)
}
# ── Introspect database metadata ──────────────────────────────
db_introspect <- function(conn, type, schema = "public") {
result <- list(tables = character(0), pks = list(), fks = list())
# Get table list
result$tables <- tryCatch(
{
if (type %in% c("postgres", "redshift", "mysql", "sqlserver", "snowflake")) {
q <- switch(type,
postgres = , redshift = paste0(
"SELECT table_name FROM information_schema.tables ",
"WHERE table_schema = '", schema, "' AND table_type = 'BASE TABLE'"
),
mysql = paste0(
"SELECT table_name FROM information_schema.tables ",
"WHERE table_schema = DATABASE() AND table_type = 'BASE TABLE'"
),
sqlserver = paste0(
"SELECT table_name FROM information_schema.tables ",
"WHERE table_schema = '", schema, "' AND table_type = 'BASE TABLE'"
),
snowflake = paste0(
"SELECT table_name FROM information_schema.tables ",
"WHERE table_schema = '", toupper(schema), "' AND table_type = 'BASE TABLE'"
)
)
res <- DBI::dbGetQuery(conn, q)
res[[1]]
} else {
DBI::dbListTables(conn)
}
},
error = function(e) {
DBI::dbListTables(conn)
}
)
# Get primary keys
result$pks <- tryCatch(
{
if (type %in% c("postgres", "redshift", "mysql", "sqlserver")) {
q <- switch(type,
postgres = , redshift = paste0(
"SELECT tc.table_name, kcu.column_name ",
"FROM information_schema.table_constraints tc ",
"JOIN information_schema.key_column_usage kcu ",
" ON tc.constraint_name = kcu.constraint_name ",
" AND tc.table_schema = kcu.table_schema ",
"WHERE tc.constraint_type = 'PRIMARY KEY' ",
"AND tc.table_schema = '", schema, "'"
),
mysql = paste0(
"SELECT tc.table_name, kcu.column_name ",
"FROM information_schema.table_constraints tc ",
"JOIN information_schema.key_column_usage kcu ",
" ON tc.constraint_name = kcu.constraint_name ",
" AND tc.table_schema = kcu.table_schema ",
"WHERE tc.constraint_type = 'PRIMARY KEY' ",
"AND tc.table_schema = DATABASE()"
),
sqlserver = paste0(
"SELECT tc.table_name, kcu.column_name ",
"FROM information_schema.table_constraints tc ",
"JOIN information_schema.key_column_usage kcu ",
" ON tc.constraint_name = kcu.constraint_name ",
" AND tc.table_schema = kcu.table_schema ",
"WHERE tc.constraint_type = 'PRIMARY KEY' ",
"AND tc.table_schema = '", schema, "'"
)
)
pk_df <- DBI::dbGetQuery(conn, q)
if (nrow(pk_df) > 0) {
split(pk_df[[2]], pk_df[[1]])
} else {
list()
}
} else {
list()
}
},
error = function(e) list()
)
# Get foreign keys
result$fks <- tryCatch(
{
if (type %in% c("postgres", "redshift", "mysql", "sqlserver")) {
q <- switch(type,
postgres = , redshift = paste0(
"SELECT ",
" kcu.table_name AS from_table, ",
" kcu.column_name AS from_col, ",
" ccu.table_name AS to_table, ",
" ccu.column_name AS to_col ",
"FROM information_schema.referential_constraints rc ",
"JOIN information_schema.key_column_usage kcu ",
" ON rc.constraint_name = kcu.constraint_name ",
" AND rc.constraint_schema = kcu.constraint_schema ",
"JOIN information_schema.constraint_column_usage ccu ",
" ON rc.unique_constraint_name = ccu.constraint_name ",
" AND rc.unique_constraint_schema = ccu.constraint_schema ",
"WHERE rc.constraint_schema = '", schema, "'"
),
mysql = paste0(
"SELECT ",
" kcu.table_name AS from_table, ",
" kcu.column_name AS from_col, ",
" kcu.referenced_table_name AS to_table, ",
" kcu.referenced_column_name AS to_col ",
"FROM information_schema.key_column_usage kcu ",
"WHERE kcu.referenced_table_name IS NOT NULL ",
"AND kcu.table_schema = DATABASE()"
),
sqlserver = paste0(
"SELECT ",
" fk_tab.name AS from_table, ",
" fk_col.name AS from_col, ",
" pk_tab.name AS to_table, ",
" pk_col.name AS to_col ",
"FROM sys.foreign_key_columns fkc ",
"JOIN sys.tables fk_tab ON fkc.parent_object_id = fk_tab.object_id ",
"JOIN sys.columns fk_col ON fkc.parent_object_id = fk_col.object_id ",
" AND fkc.parent_column_id = fk_col.column_id ",
"JOIN sys.tables pk_tab ON fkc.referenced_object_id = pk_tab.object_id ",
"JOIN sys.columns pk_col ON fkc.referenced_object_id = pk_col.object_id ",
" AND fkc.referenced_column_id = pk_col.column_id"
)
)
fk_df <- DBI::dbGetQuery(conn, q)
if (nrow(fk_df) > 0) {
lapply(seq_len(nrow(fk_df)), function(i) {
list(
from_table = fk_df$from_table[i],
from_col = fk_df$from_col[i],
to_table = fk_df$to_table[i],
to_col = fk_df$to_col[i],
detected_by = "schema",
confidence = "high",
score = 1.0,
signals = list(schema = 1.0),
reasons = "database constraint"
)
})
} else {
list()
}
} else {
list()
}
},
error = function(e) list()
)
result
}
# ── Load a single table from the database ─────────────────────
db_load_table <- function(conn, table_name, schema = "", limit = 10000) {
q <- if (nzchar(schema)) {
paste0("SELECT * FROM \"", schema, "\".\"", table_name, "\" LIMIT ", limit)
} else {
paste0("SELECT * FROM \"", table_name, "\" LIMIT ", limit)
}
tryCatch(
DBI::dbGetQuery(conn, q),
error = function(e) {
# Fallback: try without schema quoting (for MySQL, SQLite, etc.)
tryCatch(
DBI::dbGetQuery(conn, paste0("SELECT * FROM `", table_name, "` LIMIT ", limit)),
error = function(e2) NULL
)
}
)
}
# ── Close a database connection ───────────────────────────────
db_close <- function(conn) {
tryCatch(DBI::dbDisconnect(conn), error = function(e) NULL)
}