src/nimodbclite

Source   Edit  

Types

OdbcConnectionObj = object
Source   Edit  
OdbcDriverInfo = object
  name*: string
  attributes*: seq[tuple[key: string, value: string]]
Source   Edit  
OdbcException = object of CatchableError
  sqlState*: string
  nativeError*: int
Source   Edit  
ResultSet = object
  columns*: seq[SqlColumn]
  rows*: seq[seq[string]]
Source   Edit  
SqlColumn = object
  name*: string
  sqlType*: SqlDataType
  size*: int
Source   Edit  

Procs

proc exec(conn: OdbcConnection; query: string): int64 {.
    ...raises: [OdbcException, OdbcException], tags: [], forbids: [].}

Executes a SQL statement that does not return a result set.

This proc is intended for DDL statements (CREATE, DROP, ALTER) and DML statements (INSERT, UPDATE, DELETE) that modify data but don't return rows.

Parameters:

  • conn: An active OdbcConnection.
  • query: The SQL statement to execute.

Returns: The number of rows affected by the statement. For DDL statements, this may be 0 or implementation-defined.

Raises: OdbcException if the execution fails.

Example:

let rowsAffected = db.exec("INSERT INTO users (name, age) VALUES ('Alice', 30)")
echo "Inserted ", rowsAffected, " row(s)"

Source   Edit  
proc listOdbcDrivers(): seq[OdbcDriverInfo] {....raises: [OdbcException], tags: [],
    forbids: [].}

Lists all installed ODBC drivers on the system.

This proc enumerates all available ODBC drivers by using the ODBC Driver Manager. It returns information about each driver including its name and attributes (such as file paths, setup DLL, etc.).

Returns: A sequence of OdbcDriverInfo objects, each containing:

- name: The driver name (e.g., "PostgreSQL Unicode", "SQLite3")
- attributes: A sequence of key-value tuples with driver attributes

Raises: OdbcException if the driver enumeration fails.

Example:

let drivers = listOdbcDrivers()
for driver in drivers:
  echo "Driver: ", driver.name
  for attr in driver.attributes:
    echo "  ", attr.key, " = ", attr.value

Source   Edit  
proc newOdbcConnection(connectionString: string): OdbcConnection {.
    ...raises: [OdbcException], tags: [], forbids: [].}

Creates a new ODBC connection using the provided connection string.

This proc allocates an ODBC environment handle, sets the ODBC version to 3.x, allocates a connection handle, and establishes a connection to the database.

Parameters:

  • connectionString: An ODBC connection string specifying the driver, server, database, credentials, and other options.

Returns: An OdbcConnection object that can be used to execute queries.

Raises: OdbcException if the connection cannot be established.

Examples:

  • Windows SQL Server:

    let db = newOdbcConnection("Driver={SQL Server 17};Server=myServer;Database=myDB;Uid=user;Pwd=pass;")

  • Linux PostgreSQL:

    let db = newOdbcConnection("DRIVER={PostgreSQL};SERVER=localhost;PORT=5432;DATABASE=mydb;UID=postgres;PWD=password")

  • SQLite:

    let db = newOdbcConnection("DRIVER={SQLite3};Database=test.db")

Source   Edit  
proc query(conn: OdbcConnection; query: string): ResultSet {.
    ...raises: [OdbcException, OdbcException], tags: [], forbids: [].}

Executes a SQL query and returns results with metadata and all data as strings.

This proc retrieves both column metadata (name, SQL type, size) and all row data. All column values are returned as strings, regardless of their actual SQL type. NULL values are represented as empty strings.

Parameters:

  • conn: An active OdbcConnection.
  • query: The SQL SELECT statement to execute.

Returns: A ResultSet containing:

- columns: Sequence of SqlColumn with name, sqlType, and size.
- rows: Sequence of sequences of strings, where each inner sequence is a row.

Raises: OdbcException if the query execution fails.

Example:

let results = db.query("SELECT id, name, age FROM users")
for col in results.columns:
  echo "Column: ", col.name, " Type: ", col.sqlType
for row in results.rows:
  echo row  # Each row is a seq[string]

Source   Edit  
proc queryJson(conn: OdbcConnection; query: string): seq[JsonNode] {.
    ...raises: [OdbcException, OdbcException], tags: [], forbids: [].}

Executes a SQL query and returns results as a sequence of JSON objects.

This proc converts each row into a JsonNode object, with column names as keys. It performs type-aware conversion:

  • Integer types (SqlTypeInteger, SqlTypeSmallInt) -> JInt
  • Floating point types (SqlTypeFloat, SqlTypeReal, SqlTypeDouble) -> JFloat
  • Other types (varchar, date, time, etc.) -> JString
  • NULL values -> JNull

Parameters:

  • conn: An active OdbcConnection.
  • query: The SQL SELECT statement to execute.

Returns: A sequence of JsonNode objects, where each JsonNode represents a row as a JSON object with column names as keys.

Raises: OdbcException if the query execution fails.

Example:

let jsonResults = db.queryJson("SELECT id, name, score FROM users")
for row in jsonResults:
  echo row.pretty  # Pretty-print the JSON
# Output: {"id": 1, "name": "Alice", "score": 95.5}

Source   Edit