shsql: c language api

SQL database system
| Manual page for C(language)

## C language API

shsql can be accessed via this C language API. Applications should link to `libshsql.a`. There are no header files required.`` In order to successfully access the database, applications must conform to the requirements of the shsql environment.``

## Considerations

shsql's idiosynchrasies as described elsewhere in this manual apply to data retrieved via the API:

- Retrieved `NULL` fields will contain the data file `NULL` symbol as defined in your project config file (by default this is `=`).``
- Any embedded underscores present in retrieved fields will have been converted to spaces, and it will be the responsibility of the application to convert spaces to underscores as appropriate.``

## Function library

Most of these functions take an argument called dbc which is an integer selector that allows multiple channels of concurrent SQL access. Normally 0 is used, but if a retrieval on channel 0 is currently in progress, a channel of 1 or higher value may be used. Up to 4 channels may be active simultaneously (shsql.h MAXCONNECTS).``

All functions are of type `int`. See simple.c in the source code directory which demonstrates the use of this API. There is no concept of `connect` or `disconnect`.``

`SHSQL_allconfig()`

  Read project config file and initialize.`` This function must be called before any other.`` Function returns 0 if OK, or a non-zero error code.``

`SHSQL_sql( int dbc, char *sql )`

  Submit an SQL command. `dbc` is the channel selector. `sql` is an SQL command. For SELECT commands, subsequent call(s) to `SHSQL_getrow()` will be required to fetch the results.`` Function returns 0 if the SQL command was processed normally, or an error code.``

`SHSQL_getrow( int dbc, char *fields[], int *n )`

  Get one row of results from the most recently submitted SELECT command on channel `dbc`. Each `fields` pointer will be set to reference one result field; there should be enough array members to accomodate your retrievals (80 recommended).`` The pointers reference memory that will be available until the next SQL command is submitted on the channel.`` All result fields are represented as character strings, including `NULL` which will be indicated using the internal null symbol (equals-sign (=) by default).`` `n` will be set to the number of fields in the result.`` Function returns 0 if a row was fetched, 1 if there are no more rows, or an error code.``

`SHSQL_pushrow( int dbc )`

  Causes the next call to `SHSQL_getrow()` to get the same row again.``

`SHSQL_getnames( int dbc, char *fields[], int *n )`

  Get the names of the result fields from the most recently submitted SELECT command on channel `dbc`. Each `fields` pointer will be set to reference one result field name; there should be enough array members to accomodate your retrievals (shsql.h MAXITEMS) The pointers reference memory that will be available until the next SQL command is submitted on the channel.`` `n` will be set to the number of fields in the result.`` Function returns 0 if OK, or a non-zero error code.``

`SHSQL_tabdef( char *table, char *fields[], int *n )`

  Get the names of fields in table.

`SHSQL_getnrows( int dbc )`

  Return the number of rows retrieved (or inserted, updated, or deleted) by the most recently submitted SQL command on channel `dbc`.`` For SELECT commands, the row count will be exact after all rows are fetched via `SHSQL_getrow()` but it may not be exact before this time because of late processing related to DISTINCT, GROUP BY, or LIMIT. However, it is always true that a count of zero indicates no rows found and a count greater than zero indicates at least one row found.`` The row count associated with a SELECT INTO is always exact.``

`SHSQL_writable()`

  Returns 0 if the current process can write to the database directory.`` Returns 1 if `readonly` is set in the config file.`` Returns 2 if writing is prevented by unix file or directory permissions.``

## Convenience routines

`SHSQL_getitem( char *sql, char *result )`

  Get a single item from the database on channel 0. `sql` is an SQL SELECT command that retrieves one field, and the result field will be copied into `result`. Function returns 0 on success, or a non-zero error code.``

`SHSQL_getitems( char *sql, char *fields[] )`

  Get multiple items from the database on channel 0.`` `sql` is an SQL SELECT command that retrieves one or more fields.`` `fields` is an array of character pointers which will be set to reference the result fields in memory that will continue to be available until the next SQL command is submitted on channel 0.`` Function returns 0 on success, or a non-zero error code.``
  Copyright Steve Grubb

Markup created by unroff 1.0, `` `` April 13, 2004.