yada 0.9.1

Short Contents

Table of Contents


Up: (dir)

yada

This manual is for yada (yet another database abstraction) version 0.9.1.


Next: , Previous: Top, Up: Top

1 Introduction

Yada is a c library that abstracts SQL databases aiming at allowing transparent use of multiple databases. The goal in mind is to make a common interface to multiple databases, thus stopping the need for application developers to write different modules for each SQL backend they want to support. It's also useful for caching data in different database types.

To accomplish this, it uses a compatibility layer of functions to bind input and output variables, prepare statements, and retrieve data. Functionality and concept are somewhat like perl's DBI, and hopefully somewhat improved.

Currently there are modules for MySQL, Oracle, PostgreSQL, and SQLite3. Most features work in most modules, but all don't work everywhere yet, so please see the file README.modules from the source distribution for details.


Next: , Up: Introduction

1.1 Where to get

The latest version of yada is always available via http from http://oss.devit.com/yada or via ftp from ftp://ftp.devit.com/yada/.


Next: , Previous: Where to get, Up: Introduction

1.2 Reporting bugs

We welcome bug reports and suggestions for any aspect of yada, please email them to yada@devit.com.

For bug reports, please include enough information for us to reproduce the problem. Generally speaking, that means:

When in doubt whether something is needed or not, include it. It's better to include too much than to leave out something important.


Previous: Reporting bugs, Up: Introduction

1.3 Contributing

Patches are most welcome; if possible, please make them with `diff -c' and include ChangeLog entries.

When sending patches, if possible please do not encode or split them in any way; it's much easier to deal with one plain text message, however large, than many small ones.


Next: , Previous: Introduction, Up: Top

2 Installing

To use the default options, simply type

./configure

make

make install

2.1 configuring

configure with options you want

Some notable ones are:

--enable-debug
Compiles with -g and enables debugging messages written to stderr. To just compile with -g, set the environment variable CFLAGS accordingly.
--enable-ltdl-install
Force the installation of the bundled libltdl

Currently supported database types:

--with[out]-mysql
MySQL
--with[out]-oracle
Oracle
--with[out]-pgsql
PostgreSQL
--with[out]-sqlite3
SQLite 3

2.2 Configuring for different database types

--with-db_type[=DIR]
Look under DIR for includes and libs
--with-db_type-incdir=DIR
Look in DIR for includes
--with-db_type-libdir=DIR
Look in DIR for libs

There are a few options for defining the proper locations for database libraries. --with-db_type, the default, will search common installation locations. If you set --with-db_type=DIR it will look in DIR/include and DIR/lib for headers and libraries respectively. To get more specific, you may use --with-db_type-incdir=DIR --with-db_type-libdir=DIR to set each of the directories where the files can be found.

When configure is done, to install simply:

make

make install


Next: , Previous: Installing, Up: Top

3 API

To use yada, you must first initialize the library with yada_init() which will return a pointer to a yada object. The object may then be used to call the other functions as pointers from the base obj (i.e., yada->connect()).

If at any time a yada function returns an error, error code may be checked at yada->error and error message at yada->errmsg, except for yada_init(), in which case error will be stored in errno.


Next: , Up: API

3.1 Control functions


Next: , Up: Control functions

yada_t* yada_init(char *dbstr, unsigned int flags)

Parameters
dbstr
a string defining the yada database to initialize
flags
currently unused
Returns
on success
yada struct pointer
on failure
NULL

Initializes the yada library and returns a pointer to a yada object of the database type specified in db_str, a string made up of database type:[type specific connection options]. Once this is done, the other functions may be called from this base obj (i.e., yada->connect()).


Next: , Previous: yada_init, Up: Control functions

int connect(yada_t *yada, char *user, char *pass)

Parameters
user
username for database connection, if needed
pass
password for database connection, if needed
Returns
on success
non-zero
on failure
0

Attempts to connect to the database defined in the db_str passed to yada_init(), using user and pass if needed by the database type being connected to.


Next: , Previous: connect, Up: Control functions

void disconnect(yada_t *yada)

Disconnect from the database.


Next: , Previous: disconnect, Up: Control functions

void destroy(yada_t* yada)

Closes the database connection, frees all memory used by yada, and therefore invalidates the yada struct.


Next: , Previous: destroy, Up: Control functions

void free(yada_t *yada, yada_rc_t *yada_rc)

Parameters
yada_rc
pointer to the yada resource to free

Frees the resource pointed to by yada_rc.


Previous: free, Up: Control functions

void freeall(yada_t *yada, int type)

Parameters
type
OR-able set of Resource types or -1 for all

Frees all memory used by yada resources of type type, an OR-able set of Resource types. If type is -1, it will free all resources.


Next: , Previous: Control functions, Up: API

3.2 Statement preparation

These functions prepare a string for execution, allowing mapping of input variables to be passed to execute. Preparing will be done either by native database prepares, or by yada prepare.

Yada prepare is a bit less strict that most databases are, so it's possible to put a placeholder anywhere, including SQL commands or table names.


Next: , Up: Statement preparation

yada_rc_t* prepare(yada_t *yada, char *str, int strlen)

Parameters
str
statement to be prepared
strlen
length of the string, or 0 for a null terminated string
Returns
on success
yada resource pointing to the prepared statement
on failure
NULL

Prepares a string for execution using the native database prepared statements if available, if not falls back to yprepare().


Next: , Previous: prepare, Up: Statement preparation

yada_rc_t* preparef(yada_t *yada, char *format, ...)

Parameters
format
format of string to be prepared
...
arguments to format
Returns
on success
yada resource pointing to the prepared statement
on failure
NULL

Creates a string from format and it's arguments using standard printf tokens and then uses it to prepares a statement. Uses the database's native prepared statements if available, if not falls back to ypreparef().


Next: , Previous: preparef, Up: Statement preparation

yada_rc_t* yprepare(yada_t *yada, char *str, int strlen)

Parameters
str
statement to be prepared
strlen
length of the string, or 0 for a null terminated string
Returns
on success
yada resource pointing to the prepared statement
on failure
NULL

Prepares a string for execution using yada prepared statements.


Next: , Previous: yprepare, Up: Statement preparation

yada_rc_t* ypreparef(yada_t *yada, char *format, ...)

Parameters
format
format of string to be prepared
...
arguments to format
Returns
on success
yada resource pointing to the prepared statement
on failure
NULL

Creates a string from format and it's arguments using standard printf tokens and then uses it to prepares a statement. Uses the yada prepared statements.


Previous: ypreparef, Up: Statement preparation

yada_rc_t* xprepare(yada_t *yada, int flags, char *str, ...)

Parameters
flags
prepare options
str
statement to be prepared
...
arguments to prepare
Returns
on success
yada resource pointing to the prepared statement
on failure
NULL

Extensible prepare, uses flags options to decide how to prepare the statement. This is for future use, as right now the only flag combinations can be done with the above 4 statements.


Next: , Previous: Statement preparation, Up: API

3.3 Statement execution


Next: , Up: Statement execution

int execute(yada_t *yada, void *magic, ...)

Parameters
magic
Statement to execute, either a statement string or a prepared yada_rc*
...
if magic is a string, should be it's length or 0 for NULL terminated

if magic is a prepared yada_rc*, placeholder variables should follow

Returns
on success
number of rows affected
on failure
-1

Executes the statement, doesn't not return results so it shouldn't be used for select type statements;


Next: , Previous: execute, Up: Statement execution

yada_rc_t* query(yada_t *yada, void *magic, ...)

Parameters
magic
Statement to execute, either a statement string or a prepared resource(yada_rc*)
...
if magic is a string, should be it's length or 0 for NULL terminated

if magic is a prepared resource, placeholder variables should follow

Returns
on success
resource containing a result set
on failure
NULL

Executes the statement and returns a result set resource


Previous: query, Up: Statement execution

char* dumpexec(yada_t *yada, int *retlen, yada_rc_t *prep, ...)

Note only works with yprepared statements

Parameters
retlen
if not NULL, the length of the returned string will be put into it
prep
...
placeholder variables if needed for prep
Returns
on success
string of the processed statement
on failure
NULL

Functions exactly the same as execute, only instead of executing the statement once it's compiled, it returns it as a string.


Next: , Previous: Statement execution, Up: API

3.4 Result retrieval


Next: , Up: Result retrieval

yada_rc_t* bind(yada_t *yada, char *map, ...)

Parameters
map
string containing a list of the variable types
...
list of the variables to bind
Returns
on success
resource containing a bind set
on failure
NULL

Makes a mapping of variables to results. It expects pointers to actual storage space to put the variable into, or if the type is preceded by `p', expects a pointer to a pointer which will be set to the retrieved data that will be stored internally.

For example, to bind directly to an int and as a pointer to a string, you would do something like this:

     brc = yada->bind(yada, "?d?ps", (int *)&i, (char **)&str);

See Token types, for a list of the supported variables types.

See fetch, for information on how fetch treats these variables when a column is null, see


Previous: bind, Up: Result retrieval

int fetch(yada_t *yada, yada_rc_t *res, yada_rc_t *bindset)

Parameters
res
result set resource
bindset
bind set resource
Returns
on success
non zero
on failure
0

Retrieves the next row of results into the variables mapped by bindset.

If a column is NULL:


Next: , Previous: Result retrieval, Up: API

3.5 Transactions


Next: , Up: Transactions

int trx(yada_t* yada, int flags)

Parameters
flags
unused
Returns
on success
0
on failure
non-zero

Begins a transaction, flags (currently unused) will allow you to start different types of transactions.


Next: , Previous: trx, Up: Transactions

int commit(yada_t* yada)

Returns
on success
0
on failure
non-zero

Commit the current transaction causing any changes made to be saved.


Previous: commit, Up: Transactions

int rollback(yada_t* yada, int flags)

Parameters
flags
unused
Returns
on success
0
on failure
non-zero

Rollback the current transaction undoing any transactional changes.


Next: , Previous: Transactions, Up: API

3.6 Variables

Read only variables available on the yada struct.


Next: , Up: Variables

int error

Error code of the last error that occurred.


Previous: error, Up: Variables

char *errmsg

Error message for the last error that occurred.


Next: , Previous: Variables, Up: API

3.7 Resource types

YADA_STATEMENT
prepared statements returned from prepare()


YADA_RESULT
result sets returned from query()


YADA_BINDSET
bind sets as returned from bind()


Previous: Resource types, Up: API

3.8 Token types

Yada tokens are used to define variable types in both prepare() and bind(). Tokens are specified with a question mark (`?') followed by the letter for the type. To specify the variable is a pointer, use a question mark followed by a `p' and then followed by the type letter. Currently this is only used in bind().

Token types are as follows:

a
escaped binary string
b
binary string - a binary string is a pair of variables, a char* string, and an unsigned long length. Whenever you need to pass the string variable, you should pass the length variable next. When used with bind() and an execute function is called, the len variable is always an unsigned long and never a pointer, even if the binary string is a pointer (`?pb').
d
32 bit integer, signed or unsigned
e
escaped string
l
64 bit integer, signed or unsigned
s
string - NULL terminated char*
v
variable - a variable is like an escaped string except yada will surround it with single quotes (`'') if it's set, or replace it with the with the string `NULL' if null.

Escaped variables are the same as the variable, except any characters needing escaping are escaped (this differs with the type of database it is).


Next: , Previous: API, Up: Top

4 Module API

This is where the documentation and internal API description for making yada modules will go. Until then, grab an already made module and start from there, the code is fairly well documented.


Next: , Previous: Module API, Up: Top

5 Examples

Here is a simple example showing how to do a basic prepared insert, and bind to an output variable. For more examples, see test/yada_test.c in the source distribution.

  int intcol;
  yada_t *yada;
  yada_rc_t *brc, *res, *stmt;


  /* init and connect to yada */
  if(!(yada = yada_init("mysql:localhost", 0))) {
    errmsg("Failed to initialize yada: %s\n", strerror(errno));
    return(1);
  }

  /* prepare insert statement */
  if(!(stmt = yada->yprepare(yada, "insert into test (intcol) values (?d)", 0))) {
    errmsg("Failed to prepare insert: %s\n", yada->errmsg);
    yada->destroy(yada);
    return(1);
  }

  /* bind column to intcol */
  if(!(brc = yada->bind(yada, "?d", &intcol))) {
  }

  /* connect to database */
  if(!yada->connect(yada, "scott", "tiger")) {
    errmsg("Failed to connect: %s\n", yada->errmsg);
    yada->destroy(yada);
    return(1);
  }

  /* insert a row */
  if(yada->execute(yada, stmt, 5) == -1) {
    errmsg("Failed to insert: %s\n", yada->errmsg);
    yada->destroy(yada);
    return(1);
  }

  /* free prepared insert statement */
  yada->free(stmt);

  /* query all rows */
  if(!(res = yada->query(yada, "select intcol from yada_test", 0))) {
    errmsg("Failed to insert: %s\n", yada->errmsg);
    yada->destroy(yada);
    return(1);
  }

  /* fetch rows one at a time */
  while(yada->fetch(yada, res, brc))
    printf("intcol: %d\n", intcol);

  /* check for error */
  if(yada->error) {
    errmsg("error fetching row: %s", yada->errmsg);
    yada->destroy(yada);
    return(1);
  }

  /* disconnect and free everything */
  yada->destroy(yada);
  return(0);


Previous: Examples, Up: Top

6 Index