SQL Reference

Complete API Reference

All 24+ SQL functions, macros, and table-valued functions exposed by tak-cot-sender, organized by functional area. Click any entry to expand its parameter table and usage example.

📐

CoT Event Construction

cot_event(uid, type, lat, lon, hae, ce, le, time, start, stale) → STRUCT
Scalar function

Constructs a validated CoT event struct from individual field values. All field validation is applied — invalid inputs throw SQL errors.

ParameterTypeRequiredNotes
uidVARCHARYesUnique identifier for this event/sender
typeVARCHARYesCoT type string (dot-notation e.g. a-f-G-U-C)
latDOUBLEYesLatitude ∈ [-90, 90]
lonDOUBLEYesLongitude ∈ [-180, 180]
haeDOUBLENoHeight above ellipsoid (m). NULL → 9999999.0
ceDOUBLENoCircular error (m). NULL → 9999999.0
leDOUBLENoLinear error (m). NULL → 9999999.0
timeTIMESTAMPTZYesEvent time (UTC)
startTIMESTAMPTZYesValid start time
staleTIMESTAMPTZYesExpiry time. Must be > time.
SQL
SELECT cot_event(
  'UNIT-001', cot_type('a','f','G-U-C'),
  34.0522, -118.2437, 71.0, 10.0, 10.0,
  now(), now(), now() + INTERVAL '5 min'
);
cot_type(affiliation, battle_dimension, function_id) → VARCHAR
Scalar function

Builds a MIL-STD-2525B–compatible CoT type string in dot-notation from its three component parts.

ParameterTypeExamples
affiliationVARCHARa=atom, b=bits
battle_dimensionVARCHARf=friendly, h=hostile, n=neutral
function_idVARCHARG-U-C=ground unit combat
SQL
SELECT cot_type('a', 'f', 'G-U-C');  -- → 'a-f-G-U-C'
SELECT cot_type('a', 'h', 'G');      -- → 'a-h-G'  (hostile ground)
SELECT cot_type('a', 'n', 'A');      -- → 'a-n-A'  (neutral aircraft)
cot_detail(key1, value1 [, key2, value2, ...]) → STRUCT
Variadic scalar function

Constructs a Detail_Builder struct from variadic key-value pairs. Must receive an even number of arguments. Attach the result to a cot_event via struct_update(event, detail := cot_detail(...)).

SQL
SELECT cot_detail(
  'callsign', 'ALPHA-1',
  'battery',  '87',
  'group',    'Cyan'
);
📄

Serialization

cot_to_xml(event STRUCT) → VARCHAR
Scalar function

Serializes a CoT event struct to a UTF-8 XML string conforming to CoT base schema version 2.0. Includes XML declaration, all required attributes on <event>, and a <point> child element. If the event has a detail field, a <detail> block is appended.

SQL
SELECT cot_to_xml(cot_event(...));
cot_to_protobuf(event STRUCT) → BLOB
Scalar function  Roadmap

Serializes a CoT event to a TAK-framed Protocol Buffers binary blob. The framing header consists of magic byte 0xbf followed by a varint-encoded payload length. Detail fields are encoded in the xdetail field as an XML fragment string.

⚠️

Protobuf encoding is designed and specified but not yet implemented in the current MVP. Accepting tak_set_format('protobuf') is the currently implemented behavior.

tak_set_format(format VARCHAR) → VARCHAR
Macro

Sets the active serialization format for the current session. Valid values: 'xml', 'protobuf'. Any other value raises an error.

SQL
SELECT tak_set_format('protobuf');
SELECT tak_set_format('xml');        -- revert
⚙️

Configuration

tak_configure(host VARCHAR, port INTEGER, protocol VARCHAR) → VARCHAR
Macro — persists to __tak_config

Stores TAK Server connection settings. Protocol must be one of tcp, tcp+tls, udp. Port must be in [1, 65535].

SQL
SELECT tak_configure('tak.example.mil', 8089, 'tcp+tls');
tak_configure_tls(client_cert VARCHAR, client_key VARCHAR, ca_cert VARCHAR) → VARCHAR
Macro — persists PEM material to __tak_config

Stores PEM-encoded certificate material for mTLS. Both client_cert and client_key must be non-NULL and non-empty when calling this function.

SQL
SELECT tak_configure_tls(
  read_text('client.pem'),
  read_text('client.key'),
  read_text('ca.pem')
);
tak_config_show() → TABLE(key VARCHAR, value VARCHAR)
Table function

Returns all configuration entries as a two-column table. Certificate values are masked to the first 32 characters followed by ....

tak_config_clear() → BOOLEAN
Macro

Removes all entries from __tak_config. Returns true on success.

🔌

Connection Management

tak_connect() → VARCHAR
Scalar function

Establishes a TCP or TCP+TLS connection using the active configuration. Performs mTLS handshake if certificate material is configured. Returns a status string on success; throws on failure.

tak_disconnect() → BOOLEAN
Scalar function

Closes the active connection and releases all resources. Returns true if a connection was open, false if already disconnected.

tak_connection_status() → STRUCT(state, host, port, protocol, last_error)
Scalar function

Returns the current connection state. state is one of connected, disconnected, or error. last_error is NULL unless the last operation failed.

Transmission

tak_send(event STRUCT) → BIGINT
Scalar function

Serializes the event to the configured format (XML or protobuf) and sends it over the active connection. Returns the number of bytes written to the socket. Throws if not connected.

SQL
SELECT tak_send(cot_event(...));   -- → 248 (bytes sent)
tak_send_batch(events STRUCT[]) → TABLE(index, uid, status, error_message)
Table function

Sends a list of CoT events sequentially. Returns one result row per input event. A transmission error on one row sets its status to error and populates error_message, but does not stop processing of subsequent events.

Output columnTypeDescription
indexBIGINT0-based position in input list
uidVARCHARUID from the CoT event struct
statusVARCHARok or error
error_messageVARCHARNULL on success; error details on failure
🗺️

GIS / Spatial

cot_from_geometry(geom GEOMETRY, uid VARCHAR, type VARCHAR) → STRUCT
Scalar function — requires spatial extension

Converts a DuckDB GEOMETRY value to a CoT event struct. For POINT: extracts X=lon, Y=lat. For LINESTRING/POLYGON: uses centroid and encodes original shape as WKT in a <shape> detail element.

tak_send_features(query VARCHAR, uid_column VARCHAR, type_column VARCHAR) → TABLE(index, uid, status, error_message)
Table function — requires spatial extension + active connection

Executes query, expects a result column named geom of type GEOMETRY, translates each row to a CoT event via cot_from_geometry, and sends them. Returns the same result shape as tak_send_batch.

📋

Logging

tak_log([limit INTEGER = 100]) → TABLE(timestamp, level, operation, message)
Table function

Returns the most recent limit entries from the in-memory ring-buffer (capacity: 10,000). Entries are ordered oldest-first. Levels: INFO, WARN, ERROR.

SQL
SELECT * FROM tak_log();        -- last 100
SELECT * FROM tak_log(500);     -- last 500
SELECT * FROM tak_log(10000);   -- entire buffer
📦

Data Types

TypeDuckDB representationDescription
CoT_Event STRUCT(uid VARCHAR, type VARCHAR, lat DOUBLE, lon DOUBLE, hae DOUBLE, ce DOUBLE, le DOUBLE, time TIMESTAMPTZ, start TIMESTAMPTZ, stale TIMESTAMPTZ, detail STRUCT) The core CoT event struct returned by cot_event()
CoT_Detail STRUCT(pairs VARCHAR[][]) Key-value detail sub-element builder returned by cot_detail()
ConnectionStatus STRUCT(state VARCHAR, host VARCHAR, port INTEGER, protocol VARCHAR, last_error VARCHAR) Returned by tak_connection_status()