#ifndef SCORPIOX_SSMS_CLIENT_H #define SCORPIOX_SSMS_CLIENT_H #include "platform.h" #include "scorpiox-io.h" /* * scorpiox-ssms-client — remote SQL Studio client library. * * Pure C99, yyjson (bundled) + pluggable transport (scorpiox_io vtable): * libcurl on Linux/macOS, WinHTTP on Windows (zero external DLLs), or a * host-supplied backend (URLSession on iOS/macOS, mock in unit tests). * * Talks to a scorpiox-ssms-api node (default http://127.0.0.1:8180) or the * scorpiox-ssms-web edge (api proxy) over LAN or Mesh. Route contracts * match the SSMS ecosystem tracker: * GET /ping * GET|POST /connections?action=list|save|delete|test * GET|POST /tree?server&node&db&schema&obj * POST /query {server|conn, db, sql, max_rows?, timeout_s?, query_token?} * POST /query?action=cancel {server|conn, query_token?|spid?} * GET|POST /editgrid?action=load|apply * GET|POST /script?action&kind&db&schema&obj * GET|POST /activity?action=sessions|waits|expensive|blocking|kill * GET|POST /storage?action=files|history|shrink|backup * GET|POST /admin?action=jobs|job_control|logins|db_users|errorlog|querystore * * Auth (any combination the server accepts): * - X-Bot-Password header (ssms_client_set_password) * - Authorization: Bearer (ssms_client_set_jwt) * * All API functions return 0 on success, non-zero on failure (message via * ssms_client_last_error()). On success the full JSON response document is * available from ssms_client_result(). Grid helpers (row_count / col_count / * cell) parse the LAST result into a flat row/column view: * top-level "rows" (editgrid/activity/storage/admin) else * batches[0].results[last].rows (query). * Every const char* return points into static/thread-owned scratch — valid * only until the next call. Never free them. */ #define SSMS_CLIENT_VERSION "1.0.0" /* Initialize global transport state. Call once at startup. 0 on success. */ EXPORT int ssms_client_init(void); /* Release global state. Call once at shutdown. */ EXPORT void ssms_client_cleanup(void); /* * Set the API base URL (default http://127.0.0.1:8180, no trailing slash). * Pass "" or NULL to restore the default. */ EXPORT void ssms_client_set_host(const char *base_url); /* Set the node password (sent as X-Bot-Password on every request). */ EXPORT void ssms_client_set_password(const char *pwd); /* Set a JWT/session token (sent as Authorization: Bearer on every request). */ EXPORT void ssms_client_set_jwt(const char *token); /* * Install the IO backend (transport). C code owns URLs + bodies + parsing; * the backend owns HTTP only. NULL restores the built-in default * (libcurl on Linux/macOS, WinHTTP on Windows; absent on iOS where the app * must install its own backend). Call once before any high-level function. */ EXPORT void ssms_client_set_io(const struct scorpiox_io *io); /* ── High-level calls ─────────────────────────────────────────────────── */ /* GET /ping -> result = {"ok":true,"service":"scorpiox-ssms-api",...} */ EXPORT int ssms_client_ping(void); /* GET /connections?action=list -> result = {"ok":true,"servers":[...]} */ EXPORT int ssms_client_list_connections(void); /* POST /connections?action=save with profile JSON body (id?,name,host,port, * user,password,default_db,read_only,encrypt,auth_mode) -> safe profile */ EXPORT int ssms_client_save_connection(const char *profile_json); /* POST /connections?action=delete {id} */ EXPORT int ssms_client_delete_connection(const char *id); /* POST /connections?action=test with profile JSON (password may be inline) */ EXPORT int ssms_client_test_connection(const char *profile_json); /* * GET /tree?server&node&db&schema&obj — one lazy Object Explorer expand. * Any of db/schema/obj may be "" when not applicable. * -> result = {"ok":true,"nodes":[{id,kind,label,sub,has_children,meta}]} */ EXPORT int ssms_client_tree(const char *server, const char *node_kind, const char *db, const char *schema, const char *obj); /* * POST /query — GO-batched multi-resultset execution. * max_rows <= 0 -> server default; timeout_s <= 0 -> server default. * query_token: "" or NULL to omit; non-empty registers the spid for cancel. */ EXPORT int ssms_client_query(const char *server, const char *db, const char *sql, int max_rows, int timeout_s, const char *query_token); /* POST /query?action=cancel {server, query_token} */ EXPORT int ssms_client_cancel(const char *server, const char *query_token); /* POST /query?action=cancel {server, spid} — cancel by session id */ EXPORT int ssms_client_cancel_spid(const char *server, int spid); /* * GET /editgrid?action=load&server&db&table[&schema][&top] * schema "" -> dbo; top <= 0 -> 200. * -> {ok,columns,pk_columns,rows,read_only_reason} */ EXPORT int ssms_client_editgrid_load(const char *server, const char *db, const char *schema, const char *table, int top_rows); /* * POST /editgrid?action=apply — ops JSON body: * {"server":"","db":"","table":"","schema":"", * "ops":[{"op":"update|insert|delete","pk":{...},"values":{...}}], * "dry_run":false} */ EXPORT int ssms_client_editgrid_apply(const char *apply_json); /* * GET /script?server&action&kind&db&schema&obj — "Script Object as" family. * action: select_top|ddl|create|alter|drop|drop_create|insert|update| * delete|exec_params|table_props * kind: table|view|proc|function|trigger */ EXPORT int ssms_client_script(const char *server, const char *action, const char *kind, const char *db, const char *schema, const char *obj); /* * GET /activity?server&action — sessions|waits|expensive|blocking. * action NULL/"" defaults to sessions. */ EXPORT int ssms_client_activity(const char *server, const char *action); /* POST /activity?action=kill {server, spid} */ EXPORT int ssms_client_activity_kill(const char *server, int spid); /* * GET /storage?server&action[&db] — files|history. * shrink/backup take a JSON body: * shrink {"server":..,"db":..,"file":..} * backup {"server":..,"db":..,"path":..} */ EXPORT int ssms_client_storage(const char *server, const char *action, const char *db); EXPORT int ssms_client_storage_post(const char *action, const char *json_body); /* * GET|POST /admin?server&action[&db][&extra] — unified admin routes: * jobs | logins | server_roles | db_users | permissions | errorlog | * agentlog | querystore | history (pass job id in extra when needed) * extra may be "" (e.g. job_id for agent history). */ EXPORT int ssms_client_admin(const char *server, const char *action, const char *db, const char *extra); /* POST /admin?action=job_control {server, job_id, verb} */ EXPORT int ssms_client_job_control(const char *server, const char *job_id, const char *verb); /* ── Results ──────────────────────────────────────────────────────────── */ /* Full JSON body of the last successful call ("" when none). Static buf. */ EXPORT const char *ssms_client_result(void); /* Last error message ("" when none). Static buf. */ EXPORT const char *ssms_client_last_error(void); /* Rows in the grid view of the last result, -1 when not applicable. */ EXPORT int ssms_client_row_count(void); /* Columns in the grid view of the last result, -1 when not applicable. */ EXPORT int ssms_client_col_count(void); /* * Cell (row, col) of the grid view rendered as text: * numbers plain, strings raw, true/false as "true"/"false", NULL -> "". * "" when out of range. Pointer valid until the next cell()/result call. */ EXPORT const char *ssms_client_cell(int row, int col); #endif /* SCORPIOX_SSMS_CLIENT_H */