sxmc 1.0.9

Sumac: bring out what your tools can do — bridge skills, MCP, and APIs into reusable agent, terminal, and automation workflows
Documentation
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
use postgres::{Client, NoTls};
use rusqlite::Connection;
use serde_json::{json, Value};

use crate::error::{Result, SxmcError};

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
enum DatabaseType {
    Sqlite,
    Postgres,
}

struct DatabaseEntryParts<'a> {
    schema: Option<&'a str>,
    name: &'a str,
    object_type: &'a str,
    sql: Option<Value>,
    columns: Vec<Value>,
    foreign_keys: Vec<Value>,
    indexes: Vec<Value>,
}

pub fn inspect_database(
    source: &str,
    database_type: Option<&str>,
    table: Option<&str>,
    search: Option<&str>,
    compact: bool,
) -> Result<Value> {
    match detect_database_type(source, database_type)? {
        DatabaseType::Sqlite => inspect_sqlite(source, table, search, compact),
        DatabaseType::Postgres => inspect_postgres(source, table, search, compact),
    }
}

fn detect_database_type(source: &str, database_type: Option<&str>) -> Result<DatabaseType> {
    if let Some(database_type) = database_type {
        return match database_type {
            "sqlite" => Ok(DatabaseType::Sqlite),
            "postgres" => Ok(DatabaseType::Postgres),
            other => Err(SxmcError::Other(format!(
                "Unsupported database type '{}'. Use sqlite or postgres.",
                other
            ))),
        };
    }

    let lower = source.to_ascii_lowercase();
    if lower.starts_with("postgres://") || lower.starts_with("postgresql://") {
        return Ok(DatabaseType::Postgres);
    }
    if lower.ends_with(".sqlite") || lower.ends_with(".db") || lower.ends_with(".sqlite3") {
        return Ok(DatabaseType::Sqlite);
    }
    if std::path::Path::new(source).exists() {
        return Ok(DatabaseType::Sqlite);
    }

    Err(SxmcError::Other(format!(
        "Could not determine database type for '{}'. Use --database-type sqlite|postgres.",
        source
    )))
}

fn inspect_sqlite(
    source: &str,
    table: Option<&str>,
    search: Option<&str>,
    compact: bool,
) -> Result<Value> {
    let conn = Connection::open(source).map_err(|e| {
        SxmcError::Other(format!(
            "Failed to open SQLite database '{}': {}",
            source, e
        ))
    })?;

    let mut stmt = conn
        .prepare(
            "SELECT name, type, COALESCE(sql, '') \
             FROM sqlite_schema \
             WHERE type IN ('table', 'view') AND name NOT LIKE 'sqlite_%' \
             ORDER BY name",
        )
        .map_err(|e| SxmcError::Other(format!("Failed to inspect SQLite schema: {}", e)))?;

    let search_lower = search.map(|value| value.to_ascii_lowercase());
    let explicit_table = table.map(|value| value.to_string());
    let rows = stmt
        .query_map([], |row| {
            let name: String = row.get(0)?;
            let object_type: String = row.get(1)?;
            let sql: String = row.get(2)?;
            Ok((name, object_type, sql))
        })
        .map_err(|e| SxmcError::Other(format!("Failed to query SQLite schema: {}", e)))?;

    let mut entries = Vec::new();
    for row in rows {
        let (name, object_type, sql) =
            row.map_err(|e| SxmcError::Other(format!("Failed to read SQLite schema row: {}", e)))?;

        if let Some(expected) = explicit_table.as_deref() {
            if name != expected {
                continue;
            }
        }

        if let Some(pattern) = search_lower.as_deref() {
            let haystack = format!("{} {} {}", name, object_type, sql).to_ascii_lowercase();
            if !haystack.contains(pattern) {
                continue;
            }
        }

        let columns = inspect_sqlite_columns(&conn, &name, &object_type)?;
        let foreign_keys = inspect_sqlite_foreign_keys(&conn, &name, &object_type)?;
        let indexes = inspect_sqlite_indexes(&conn, &name, &object_type)?;
        entries.push(database_entry_value(
            DatabaseEntryParts {
                schema: None,
                name: &name,
                object_type: &object_type,
                sql: if sql.is_empty() {
                    None
                } else {
                    Some(Value::String(sql))
                },
                columns,
                foreign_keys,
                indexes,
            },
            compact,
        ));
    }

    if let Some(expected) = explicit_table {
        if entries.is_empty() {
            return Err(SxmcError::Other(format!(
                "Table or view '{}' was not found in SQLite database '{}'.",
                expected, source
            )));
        }
    }

    Ok(json!({
        "discovery_schema": "sxmc_discover_db_v1",
        "source_type": "database",
        "database_type": "sqlite",
        "source": source,
        "selected_table": table,
        "search": search,
        "compact": compact,
        "count": entries.len(),
        "entries": entries,
    }))
}

fn inspect_postgres(
    source: &str,
    table: Option<&str>,
    search: Option<&str>,
    compact: bool,
) -> Result<Value> {
    let mut client = Client::connect(source, NoTls).map_err(|e| {
        SxmcError::Other(format!(
            "Failed to connect to PostgreSQL database '{}': {}",
            source, e
        ))
    })?;

    let search_lower = search.map(|value| value.to_ascii_lowercase());
    let explicit_table = table.map(|value| value.to_string());
    let rows = client
        .query(
            "SELECT table_schema, table_name, table_type \
             FROM information_schema.tables \
             WHERE table_schema NOT IN ('pg_catalog', 'information_schema') \
             ORDER BY table_schema, table_name",
            &[],
        )
        .map_err(|e| SxmcError::Other(format!("Failed to inspect PostgreSQL schema: {}", e)))?;

    let mut entries = Vec::new();
    for row in rows {
        let schema: String = row.get(0);
        let name: String = row.get(1);
        let table_type: String = row.get(2);
        let object_type = if table_type.eq_ignore_ascii_case("VIEW") {
            "view"
        } else {
            "table"
        };
        let qualified_name = format!("{}.{}", schema, name);

        if let Some(expected) = explicit_table.as_deref() {
            if name != expected && qualified_name != expected {
                continue;
            }
        }

        if let Some(pattern) = search_lower.as_deref() {
            let haystack = format!("{} {} {}", schema, name, object_type).to_ascii_lowercase();
            if !haystack.contains(pattern) {
                continue;
            }
        }

        let columns = inspect_postgres_columns(&mut client, &schema, &name)?;
        let foreign_keys = inspect_postgres_foreign_keys(&mut client, &schema, &name)?;
        let indexes = inspect_postgres_indexes(&mut client, &schema, &name)?;
        entries.push(database_entry_value(
            DatabaseEntryParts {
                schema: Some(&schema),
                name: &name,
                object_type,
                sql: None,
                columns,
                foreign_keys,
                indexes,
            },
            compact,
        ));
    }

    if let Some(expected) = explicit_table {
        if entries.is_empty() {
            return Err(SxmcError::Other(format!(
                "Table or view '{}' was not found in PostgreSQL database '{}'.",
                expected, source
            )));
        }
    }

    Ok(json!({
        "discovery_schema": "sxmc_discover_db_v1",
        "source_type": "database",
        "database_type": "postgres",
        "source": source,
        "selected_table": table,
        "search": search,
        "compact": compact,
        "count": entries.len(),
        "entries": entries,
    }))
}

fn database_entry_value(entry: DatabaseEntryParts<'_>, compact: bool) -> Value {
    let qualified_name = entry
        .schema
        .map(|schema| format!("{}.{}", schema, entry.name))
        .unwrap_or_else(|| entry.name.to_string());
    let mut value = json!({
        "schema": entry.schema,
        "name": entry.name,
        "qualified_name": qualified_name,
        "object_type": entry.object_type,
        "sql": entry.sql.unwrap_or(Value::Null),
        "column_count": entry.columns.len(),
        "foreign_key_count": entry.foreign_keys.len(),
        "index_count": entry.indexes.len(),
    });

    if !compact {
        value["columns"] = Value::Array(entry.columns);
        value["foreign_keys"] = Value::Array(entry.foreign_keys);
        value["indexes"] = Value::Array(entry.indexes);
    }

    value
}

fn inspect_sqlite_columns(
    conn: &Connection,
    table_name: &str,
    object_type: &str,
) -> Result<Vec<Value>> {
    if object_type != "table" {
        return Ok(Vec::new());
    }

    let pragma = format!(
        "PRAGMA table_info({})",
        sqlite_identifier_literal(table_name)
    );
    let mut stmt = conn.prepare(&pragma).map_err(|e| {
        SxmcError::Other(format!(
            "Failed to inspect columns for '{}': {}",
            table_name, e
        ))
    })?;

    let rows = stmt
        .query_map([], |row| {
            let name: String = row.get(1)?;
            let data_type: String = row.get(2)?;
            let not_null: i64 = row.get(3)?;
            let default_value: Option<String> = row.get(4)?;
            let primary_key_position: i64 = row.get(5)?;
            Ok(json!({
                "name": name,
                "data_type": data_type,
                "not_null": not_null != 0,
                "default": default_value,
                "primary_key": primary_key_position != 0,
                "primary_key_position": primary_key_position,
            }))
        })
        .map_err(|e| {
            SxmcError::Other(format!(
                "Failed to read columns for '{}': {}",
                table_name, e
            ))
        })?;

    collect_rows(rows, table_name, "column")
}

fn inspect_sqlite_foreign_keys(
    conn: &Connection,
    table_name: &str,
    object_type: &str,
) -> Result<Vec<Value>> {
    if object_type != "table" {
        return Ok(Vec::new());
    }

    let pragma = format!(
        "PRAGMA foreign_key_list({})",
        sqlite_identifier_literal(table_name)
    );
    let mut stmt = conn.prepare(&pragma).map_err(|e| {
        SxmcError::Other(format!(
            "Failed to inspect foreign keys for '{}': {}",
            table_name, e
        ))
    })?;

    let rows = stmt
        .query_map([], |row| {
            let id: i64 = row.get(0)?;
            let seq: i64 = row.get(1)?;
            let ref_table: String = row.get(2)?;
            let from: String = row.get(3)?;
            let to: String = row.get(4)?;
            let on_update: String = row.get(5)?;
            let on_delete: String = row.get(6)?;
            Ok(json!({
                "id": id,
                "sequence": seq,
                "column": from,
                "references_table": ref_table,
                "references_column": to,
                "on_update": on_update,
                "on_delete": on_delete,
            }))
        })
        .map_err(|e| {
            SxmcError::Other(format!(
                "Failed to read foreign keys for '{}': {}",
                table_name, e
            ))
        })?;

    collect_rows(rows, table_name, "foreign key")
}

fn inspect_sqlite_indexes(
    conn: &Connection,
    table_name: &str,
    object_type: &str,
) -> Result<Vec<Value>> {
    if object_type != "table" {
        return Ok(Vec::new());
    }

    let pragma = format!(
        "PRAGMA index_list({})",
        sqlite_identifier_literal(table_name)
    );
    let mut stmt = conn.prepare(&pragma).map_err(|e| {
        SxmcError::Other(format!(
            "Failed to inspect indexes for '{}': {}",
            table_name, e
        ))
    })?;

    let rows = stmt
        .query_map([], |row| {
            let name: String = row.get(1)?;
            let unique: i64 = row.get(2)?;
            let origin: String = row.get(3)?;
            let partial: i64 = row.get(4)?;
            Ok(json!({
                "name": name,
                "unique": unique != 0,
                "origin": origin,
                "partial": partial != 0,
            }))
        })
        .map_err(|e| {
            SxmcError::Other(format!(
                "Failed to read indexes for '{}': {}",
                table_name, e
            ))
        })?;

    collect_rows(rows, table_name, "index")
}

fn inspect_postgres_columns(
    client: &mut Client,
    schema: &str,
    table_name: &str,
) -> Result<Vec<Value>> {
    let rows = client
        .query(
            "SELECT column_name, udt_name, is_nullable, column_default \
             FROM information_schema.columns \
             WHERE table_schema = $1 AND table_name = $2 \
             ORDER BY ordinal_position",
            &[&schema, &table_name],
        )
        .map_err(|e| {
            SxmcError::Other(format!(
                "Failed to inspect PostgreSQL columns for '{}.{}': {}",
                schema, table_name, e
            ))
        })?;

    Ok(rows
        .into_iter()
        .map(|row| {
            json!({
                "name": row.get::<_, String>(0),
                "data_type": row.get::<_, String>(1),
                "not_null": row.get::<_, String>(2).eq_ignore_ascii_case("NO"),
                "default": row.get::<_, Option<String>>(3),
            })
        })
        .collect())
}

fn inspect_postgres_foreign_keys(
    client: &mut Client,
    schema: &str,
    table_name: &str,
) -> Result<Vec<Value>> {
    let rows = client
        .query(
            "SELECT tc.constraint_name, kcu.column_name, ccu.table_schema, ccu.table_name, ccu.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 \
             JOIN information_schema.constraint_column_usage ccu \
               ON tc.constraint_name = ccu.constraint_name AND tc.table_schema = ccu.table_schema \
             WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_schema = $1 AND tc.table_name = $2 \
             ORDER BY tc.constraint_name, kcu.ordinal_position",
            &[&schema, &table_name],
        )
        .map_err(|e| {
            SxmcError::Other(format!(
                "Failed to inspect PostgreSQL foreign keys for '{}.{}': {}",
                schema, table_name, e
            ))
        })?;

    Ok(rows
        .into_iter()
        .map(|row| {
            json!({
                "name": row.get::<_, String>(0),
                "column": row.get::<_, String>(1),
                "references_schema": row.get::<_, String>(2),
                "references_table": row.get::<_, String>(3),
                "references_column": row.get::<_, String>(4),
            })
        })
        .collect())
}

fn inspect_postgres_indexes(
    client: &mut Client,
    schema: &str,
    table_name: &str,
) -> Result<Vec<Value>> {
    let rows = client
        .query(
            "SELECT indexname, indexdef \
             FROM pg_indexes \
             WHERE schemaname = $1 AND tablename = $2 \
             ORDER BY indexname",
            &[&schema, &table_name],
        )
        .map_err(|e| {
            SxmcError::Other(format!(
                "Failed to inspect PostgreSQL indexes for '{}.{}': {}",
                schema, table_name, e
            ))
        })?;

    Ok(rows
        .into_iter()
        .map(|row| {
            json!({
                "name": row.get::<_, String>(0),
                "definition": row.get::<_, String>(1),
            })
        })
        .collect())
}

fn collect_rows<T>(
    rows: rusqlite::MappedRows<'_, impl FnMut(&rusqlite::Row<'_>) -> rusqlite::Result<T>>,
    table_name: &str,
    row_type: &str,
) -> Result<Vec<T>> {
    let mut values = Vec::new();
    for row in rows {
        values.push(row.map_err(|e| {
            SxmcError::Other(format!(
                "Failed to decode SQLite {} for '{}': {}",
                row_type, table_name, e
            ))
        })?);
    }
    Ok(values)
}

fn sqlite_identifier_literal(value: &str) -> String {
    format!("\"{}\"", value.replace('"', "\"\""))
}

#[cfg(test)]
mod tests {
    use super::{detect_database_type, DatabaseType};

    #[test]
    fn detects_postgres_urls() {
        assert_eq!(
            detect_database_type("postgres://demo:demo@localhost/test", None).unwrap(),
            DatabaseType::Postgres
        );
        assert_eq!(
            detect_database_type("postgresql://demo:demo@localhost/test", None).unwrap(),
            DatabaseType::Postgres
        );
    }

    #[test]
    fn detects_sqlite_paths() {
        assert_eq!(
            detect_database_type("/tmp/demo.sqlite", None).unwrap(),
            DatabaseType::Sqlite
        );
        assert_eq!(
            detect_database_type("/tmp/demo.db", None).unwrap(),
            DatabaseType::Sqlite
        );
    }
}