ethanak / SimplePgSQL

Simple PostgreSQL connector for Arduino and ESP8266
GNU Lesser General Public License v2.1
49 stars 31 forks source link

SimplePgSQL

Simple PostgreSQL connector for Arduino, ESP32 and ESP8266.

Only simple queries are implemented. COPY is not implemented due to code size limit, but probably will be for ESP32 only. Large objects are not implemented as obsolete and never be.

Available authorization method are trust, password and md5. Due to code size limit, md5 method may be disabled in compilation time, decreasing code size for Arduino by some kilobytes.

All methods are asynchronous, but sometimes may block for a while in case of poor network connection.

As column names and notifications are rarely needed in microcontrollers applications, may be disabled. In this case only number of fields will be fetched from row description packet, and notification rows will be simply skipped.

All methods uses single internal buffer, allocated at setDbLogin and freed on close. It's possible to provide external (statically allocated) buffer, which may be reused in rest of application.

Parameter progmem has no meaning for ESP32.

Class and Methods

PGconnection

PGconnection(Client *c, int flags = 0, int memory = 0, char *foreignBuffer = NULL);

Class constructor.

Parameters:

setDbLogin

int setDbLogin(IPAddress server,
            const char *user,
            const char *passwd = NULL,
            const char *db = NULL,
            const char *charset = NULL,
            int port = 5432);

Initialize connection.

Parameters:

Returns:

connection status (see below)

status

int status(void);

Check current connection status and perform authorization action if needed. Must be called after setDbLogin until CONNECTION_OK, CONNECTION_BAD or CONNECTION_NEEDED.

Returns

Current connection status. May be one of:

close

void close(void);

Send termination command if needed and close connection. Free internal buffers.

execute

execute(const char *query, int progmem = 0);

Send query to backend. Invalidates data in internal buffer.

Parameters:

Returns

Negative value on error or zero on success. In case of error you must check connection status - some errors invalidates connection.

getData

int getData(void);

Retrieve any response from backend. Must be called after execute() until PG_RSTAT_READY. May be called even in READY state and fetch possible notifications. Each call invalidates data in internal buffer, so values returned by getColumn(), getValue() and getMessage() will be valid only until next getData() or execute() call.

Returns:

dataStatus

int dataStatus(void);

Get current data status.

Returns

Combination of bits:

getColumn

char *getColumn(int n);

Get n-th column name if available

Returns

getValue

char *getValue(int n);

Get n-th row value if available

Returns

getMessage

char *getMessage(void);

Get current error or notification message

Returns

Pointer to message text in internal buffer or NULL if no message.

nfields

int nfields(void);

Get column count in result. Valid only after PG_RSTAT_HAVE_COLUMNS or first PG_RSTAT_HAVE_ROW.

Returns

Column count or zero if not known.

ntuples

int ntuples(void);

Get number of tuples in result. Valid only after PG_RSTAT_HAVE_SUMMARY.

Returns

pgEscapeString

int escapeString(const char *inbuf, char *outbuf);

Creates escaped version of literal. Single quotes and 'E' marker (if needed) will be added.

Parameters:

Returns

Length of escaped string

escapeName

int escapeName(const char *inbuf, char *outbuf);

Creates escaped version of name. Double quotes will be added.

Parameters:

Returns

Length of escaped string

executeFormat

int executeFormat(int progmem, const char *format, ...);

Send formatted query to backend. Format string may be placed in Flash memory. Formatting sequences are:

Any % character not followed by 's', 'n', 'd', 'l' or '%' causes error. Query may be long, but results of any formatted value must fit in internal buffer.

Parameters:

Returns

Zero on success or negative value on error.