sqlite.c File Reference
`#include "system.h"`
`#include <rpmio.h>`
`#include <rpmlog.h>`
`#include <rpmmacro.h>`
`#include <rpmurl.h>`
`#include <rpmtag.h>`
`#include <rpmdb.h>`
`#include <sqlite3.h>`
`#include "debug.h"`
Include dependency graph for sqlite.c:

Go to the source code of this file.

## Data Structures

struct | _sql_db_s
struct | _sql_dbcursor_s
union | _dbswap

## Macros

#define | _RPMDB_INTERNAL
#define | UINT32_T rpmuint32_t
#define | _DBSWAP(_a)

## Typedefs

typedef struct _sql_db_s | SQL_DB
typedef struct _sql_dbcursor_s * | SCP_t

## Functions

static void | enterChroot (dbiIndex dbi)
static void | leaveChroot (dbiIndex dbi)
static void | dbg_scp (void *ptr)
static void | dbg_keyval (const char *msg, dbiIndex dbi, DBC *dbcursor, DBT *key, DBT *data, unsigned int flags)
static SCP_t | scpResetKeys (SCP_t scp)
static SCP_t | scpResetAv (SCP_t scp)
static SCP_t | scpReset (SCP_t scp)
static SCP_t | scpFree (SCP_t scp)
static SCP_t | scpNew (DB *dbp)
static int | sql_step (dbiIndex dbi, SCP_t scp)
static int | sql_bind_key (dbiIndex dbi, SCP_t scp, int pos, DBT *key)
static int | sql_bind_data (dbiIndex dbi, SCP_t scp, int pos, DBT *data)
static int | sql_startTransaction (dbiIndex dbi)
static int | sql_endTransaction (dbiIndex dbi)
static int | sql_commitTransaction (dbiIndex dbi, int flag)
static int | sql_busy_handler (void *dbi_void, int time)
static int | sql_initDB (dbiIndex dbi)
| Verify the DB is setup. More...
static int | sql_cclose (dbiIndex dbi, DBC *dbcursor, unsigned int flags)
| Close database cursor. More...
static int | sql_close (dbiIndex dbi, unsigned int flags)
| Close index database, and destroy database handle. More...
static int | sql_open (rpmdb rpmdb, rpmTag rpmtag, dbiIndex *dbip)
| Return handle for an index database. More...
static int | sql_sync (dbiIndex dbi, unsigned int flags)
| Flush pending operations to disk. More...
static int | sql_copen (dbiIndex dbi, DB_TXN *txnid, DBC **dbcp, unsigned int flags)
| Open database cursor. More...
static int | sql_cdel (dbiIndex dbi, DBC *dbcursor, DBT *key, DBT *data, unsigned int flags)
| Delete (key,data) pair(s) using db->del or dbcursor->c_del. More...
static int | sql_cget (dbiIndex dbi, DBC *dbcursor, DBT *key, DBT *data, unsigned int flags)
| Retrieve (key,data) pair using db->get or dbcursor->c_get. More...
static int | sql_cput (dbiIndex dbi, DBC *dbcursor, DBT *key, DBT *data, unsigned int flags)
| Store (key,data) pair using db->put or dbcursor->c_put. More...
static int | sql_byteswapped (dbiIndex dbi)
| Is database byte swapped? More...
static int | sql_associate (dbiIndex dbi, dbiIndex dbisecondary, int(*callback)(DB *, const DBT *, const DBT *, DBT *), unsigned int flags)
| Associate secondary database with primary. More...
static int | sql_join (dbiIndex dbi, DBC **curslist, DBC **dbcp, unsigned int flags)
| Return join cursor for list of cursors. More...
static int | sql_cdup (dbiIndex dbi, DBC *dbcursor, DBC **dbcp, unsigned int flags)
| Duplicate a database cursor. More...
static int | sql_cpget (dbiIndex dbi, DBC *dbcursor, DBT *key, DBT *pkey, DBT *data, unsigned int flags)
| Retrieve (key,data) pair using dbcursor->c_pget. More...
static int | sql_ccount (dbiIndex dbi, DBC *dbcursor, unsigned int *countp, unsigned int flags)
| Retrieve count of (possible) duplicate items using dbcursor->c_count. More...
static int | sql_stat (dbiIndex dbi, unsigned int flags)
| Save statistics in database handle. More...

## Variables

static int | _debug = 0
static unsigned int | endian = 0x11223344
static const char * | sqlCwd = NULL
static int | sqlInRoot = 0
struct _dbiVec | sqlitevec

## Macro Definition Documentation

#define _DBSWAP | ( |  | _a | ) |

Value:
{ unsigned char _b, *_c = (_a).uc; \
_b = _c[3]; _c[3] = _c[0]; _c[0] = _b; \
_b = _c[2]; _c[2] = _c[1]; _c[1] = _b; \
}

Definition at line 120 of file sqlite.c.

Referenced by sql_bind_key(), and sql_step().

#define _RPMDB_INTERNAL

Definition at line 44 of file sqlite.c.

#define UINT32_T rpmuint32_t

Definition at line 54 of file sqlite.c.

Referenced by sql_cget().

## Typedef Documentation

typedef struct _sql_dbcursor_s* SCP_t

Definition at line 70 of file sqlite.c.

typedef struct _sql_db_s SQL_DB

Definition at line 69 of file sqlite.c.

## Function Documentation

static void dbg_keyval | ( | const char * | msg,
|  | dbiIndex | dbi,
|  | DBC * | dbcursor,
|  | DBT * | key,
|  | DBT * | data,
|  | unsigned int | flags
| ) |  |

static

Definition at line 207 of file sqlite.c.

References _debug, __db_dbt::data, dbg_scp(), RPMTAG_NAME, and __db_dbt::size.

Referenced by sql_cdel(), sql_cget(), and sql_cput().

static void dbg_scp | ( | void * | ptr | ) |

static

Definition at line 196 of file sqlite.c.

References _debug, _sql_dbcursor_s::ac, _sql_dbcursor_s::all, _sql_dbcursor_s::av, _sql_dbcursor_s::avlen, _sql_dbcursor_s::nalloc, _sql_dbcursor_s::nc, _sql_dbcursor_s::nr, and _sql_dbcursor_s::rx.

Referenced by dbg_keyval(), scpReset(), scpResetAv(), and scpResetKeys().

static void enterChroot | ( | dbiIndex | dbi | ) |

static

Definition at line 134 of file sqlite.c.

References _debug, Chdir(), Chroot(), errno, sqlCwd, sqlInRoot, and xrealloc().

Referenced by sql_byteswapped(), sql_cclose(), sql_cdel(), sql_cget(), sql_close(), sql_copen(), sql_cput(), sql_open(), sql_stat(), and sql_sync().

static void leaveChroot | ( | dbiIndex | dbi | ) |

static

Definition at line 169 of file sqlite.c.

References _debug, _free(), Chdir(), Chroot(), sqlCwd, and sqlInRoot.

Referenced by sql_byteswapped(), sql_cclose(), sql_cdel(), sql_cget(), sql_close(), sql_copen(), sql_cput(), sql_open(), sql_stat(), and sql_sync().

static SCP_t scpFree | ( | SCP_t | scp | ) |

static

Definition at line 321 of file sqlite.c.

References _debug, _free(), _sql_dbcursor_s::av, _sql_dbcursor_s::avlen, scpReset(), and scpResetKeys().

Referenced by sql_byteswapped(), sql_cclose(), sql_cdel(), sql_cput(), sql_initDB(), and sql_stat().

static SCP_t scpNew | ( | DB * | dbp | ) |

static

Definition at line 335 of file sqlite.c.

References _debug, _sql_dbcursor_s::dbp, _sql_dbcursor_s::ldata, _sql_dbcursor_s::lkey, _sql_dbcursor_s::used, and xcalloc().

Referenced by sql_byteswapped(), sql_cdel(), sql_copen(), sql_cput(), sql_initDB(), and sql_stat().

static SCP_t scpReset | ( | SCP_t | scp | ) |

static

Definition at line 293 of file sqlite.c.

References _debug, _sql_dbcursor_s::cmd, dbg_scp(), _sql_dbcursor_s::pStmt, rpmlog(), RPMLOG_WARNING, _sql_dbcursor_s::rx, and scpResetAv().

Referenced by scpFree(), and sql_cget().

static SCP_t scpResetAv | ( | SCP_t | scp | ) |

static

Definition at line 254 of file sqlite.c.

References _debug, _free(), _sql_dbcursor_s::ac, _sql_dbcursor_s::av, _sql_dbcursor_s::avlen, dbg_scp(), _sql_dbcursor_s::nalloc, _sql_dbcursor_s::nc, and _sql_dbcursor_s::nr.

Referenced by scpReset(), and sql_cget().

static SCP_t scpResetKeys | ( | SCP_t | scp | ) |

static

Definition at line 232 of file sqlite.c.

References _debug, _free(), __db_dbt::data, dbg_scp(), _sql_dbcursor_s::keys, and _sql_dbcursor_s::nkeys.

Referenced by scpFree(), and sql_cget().

static int sql_associate | ( | dbiIndex | dbi,
|  | dbiIndex | dbisecondary,
|  | int(*)(DB *, const DBT *, const DBT *, DBT *) | callback,
|  | unsigned int | flags
| ) |  |

static

Associate secondary database with primary.

Parameters

  dbi | index database handle
  dbisecondary | secondary index database handle
  callback | create secondary key from primary (NULL if DB_RDONLY)
  flags | DB_CREATE or 0

Returns
  0 on success

Definition at line 1435 of file sqlite.c.

References _debug.

static int sql_bind_data | ( | dbiIndex | dbi,
|  | SCP_t | scp,
|  | int | pos,
|  | DBT * | data
| ) |  |

static

Definition at line 589 of file sqlite.c.

References __db_dbt::data, _sql_dbcursor_s::pStmt, and __db_dbt::size.

Referenced by sql_cdel(), and sql_cput().

static int sql_bind_key | ( | dbiIndex | dbi,
|  | SCP_t | scp,
|  | int | pos,
|  | DBT * | key
| ) |  |

static

Definition at line 517 of file sqlite.c.

References _DBSWAP, __db_dbt::data, _sql_dbcursor_s::pStmt, RPM_BIN_TYPE, RPM_I18NSTRING_TYPE, RPM_MASK_TYPE, RPM_STRING_ARRAY_TYPE, RPM_STRING_TYPE, RPM_UINT16_TYPE, RPM_UINT32_TYPE, RPM_UINT64_TYPE, RPM_UINT8_TYPE, RPMDBI_PACKAGES, __db_dbt::size, tagType(), and _dbswap::ui.

Referenced by sql_cdel(), sql_cget(), and sql_cput().

static int sql_busy_handler | ( | void * | dbi_void,
|  | int | time
| ) |  |

static

Definition at line 677 of file sqlite.c.

References _, rpmlog(), and RPMLOG_WARNING.

Referenced by sql_open().

static int sql_byteswapped | ( | dbiIndex | dbi | ) |

static

Is database byte swapped?

Parameters

  dbi | index database handle

Returns
  0 no

Definition at line 1380 of file sqlite.c.

References D_, _sql_db_s::db, endian, enterChroot(), leaveChroot(), rpmlog(), RPMLOG_DEBUG, RPMLOG_WARNING, scpFree(), scpNew(), and _dbswap::uc.

static int sql_cclose | ( | dbiIndex | dbi,
|  | DBC * | dbcursor,
|  | unsigned int | flags
| ) |  |

static

Close database cursor.

Parameters

  dbi | index database handle
  dbcursor | database cursor
  flags | (unused)

Returns
  0 on success

Definition at line 821 of file sqlite.c.

References _debug, _free(), DB_WRITECURSOR, enterChroot(), _sql_dbcursor_s::ldata, leaveChroot(), _sql_dbcursor_s::lkey, scpFree(), sql_commitTransaction(), and sql_endTransaction().

Referenced by sql_copen().

static int sql_ccount | ( | dbiIndex | dbi,
|  | DBC * | dbcursor,
|  | unsigned int * | countp,
|  | unsigned int | flags
| ) |  |

static

Retrieve count of (possible) duplicate items using dbcursor->c_count.

Parameters

  dbi | index database handle
  dbcursor | database cursor
  countp | address of count
  flags | (unused)

Returns
  0 on success

Definition at line 1518 of file sqlite.c.

References _debug.

static int sql_cdel | ( | dbiIndex | dbi,
|  | DBC * | dbcursor,
|  | DBT * | key,
|  | DBT * | data,
|  | unsigned int | flags
| ) |  |

static

Delete (key,data) pair(s) using db->del or dbcursor->c_del.

Parameters

  dbi | index database handle
  dbcursor | database cursor (NULL will use db->del)
  key | delete key value/length/flags
  data | delete data value/length/flags
  flags | (unused)

Returns
  0 on success

Definition at line 1115 of file sqlite.c.

References _sql_db_s::db, dbg_keyval(), enterChroot(), leaveChroot(), rpmlog(), RPMLOG_WARNING, scpFree(), scpNew(), sql_bind_data(), sql_bind_key(), and sql_step().

static int sql_cdup | ( | dbiIndex | dbi,
|  | DBC * | dbcursor,
|  | DBC ** | dbcp,
|  | unsigned int | flags
| ) |  |

static

Duplicate a database cursor.

Parameters

  dbi | index database handle
  dbcursor | database cursor

Return values

  dbcp | address of new database cursor

Parameters

  flags | DB_POSITION for same position, 0 for uninitialized

Returns
  0 on success

Definition at line 1474 of file sqlite.c.

References _debug.

static int sql_cget | ( | dbiIndex | dbi,
|  | DBC * | dbcursor,
|  | DBT * | key,
|  | DBT * | data,
|  | unsigned int | flags
| ) |  |

static

Retrieve (key,data) pair using db->get or dbcursor->c_get.

Parameters

  dbi | index database handle
  dbcursor | database cursor (NULL will use db->get)
  key | retrieve key value/length/flags
  data | retrieve data value/length/flags
  flags | (unused)

Returns
  0 on success

Definition at line 1156 of file sqlite.c.

References _debug, _free(), __db_dbt::data, _sql_db_s::db, DB_DBT_MALLOC, DB_NOTFOUND, DB_SET, dbg_keyval(), enterChroot(), __db_dbt::flags, leaveChroot(), RPMDBI_PACKAGES, rpmlog(), RPMLOG_WARNING, scpReset(), scpResetAv(), scpResetKeys(), __db_dbt::size, sql_bind_key(), sql_step(), UINT32_T, xcalloc(), and xmalloc().

static int sql_close | ( | dbiIndex | dbi,
|  | unsigned int | flags
| ) |  |

static

Close index database, and destroy database handle.

Parameters

  dbi | index database handle
  flags | (unused)

Returns
  0 on success

Definition at line 860 of file sqlite.c.

References _free(), D_, _sql_db_s::db, DB_PRIVATE, enterChroot(), leaveChroot(), rpmGenPath(), rpmlog(), RPMLOG_DEBUG, sql_commitTransaction(), Unlink(), and urlPath().

Referenced by sql_open().

static int sql_commitTransaction | ( | dbiIndex | dbi,
|  | int | flag
| ) |  |

static

Definition at line 652 of file sqlite.c.

References _debug, _sql_db_s::db, sql_startTransaction(), and _sql_db_s::transaction.

Referenced by sql_cclose(), sql_close(), and sql_sync().

static int sql_copen | ( | dbiIndex | dbi,
|  | DB_TXN * | txnid,
|  | DBC ** | dbcp,
|  | unsigned int | flags
| ) |  |

static

Open database cursor.

Parameters

  dbi | index database handle
  txnid | database transaction handle

Return values

  dbcp | address of new database cursor

Parameters

  flags | DB_WRITECURSOR or 0

Returns
  0 on success

Definition at line 1077 of file sqlite.c.

References _debug, DB_WRITECURSOR, enterChroot(), leaveChroot(), RPM_MASK_TYPE, scpNew(), sql_cclose(), sql_startTransaction(), tagName(), and tagType().

static int sql_cpget | ( | dbiIndex | dbi,
|  | DBC * | dbcursor,
|  | DBT * | key,
|  | DBT * | pkey,
|  | DBT * | data,
|  | unsigned int | flags
| ) |  |

static

Retrieve (key,data) pair using dbcursor->c_pget.

Parameters

  dbi | index database handle
  dbcursor | database cursor
  key | secondary retrieve key value/length/flags
  pkey | primary retrieve key value/length/flags
  data | primary retrieve data value/length/flags
  flags | DB_NEXT, DB_SET, or 0

Returns
  0 on success

Definition at line 1496 of file sqlite.c.

References _debug.

static int sql_cput | ( | dbiIndex | dbi,
|  | DBC * | dbcursor,
|  | DBT * | key,
|  | DBT * | data,
|  | unsigned int | flags
| ) |  |

static

Store (key,data) pair using db->put or dbcursor->c_put.

Parameters

  dbi | index database handle
  dbcursor | database cursor (NULL will use db->put)
  key | store key value/length/flags
  data | store data value/length/flags
  flags | (unused)

Returns
  0 on success

Definition at line 1338 of file sqlite.c.

References _sql_db_s::db, dbg_keyval(), enterChroot(), leaveChroot(), rpmlog(), RPMLOG_WARNING, scpFree(), scpNew(), sql_bind_data(), sql_bind_key(), and sql_step().

static int sql_endTransaction | ( | dbiIndex | dbi | ) |

static

Definition at line 630 of file sqlite.c.

References _debug, _sql_db_s::db, and _sql_db_s::transaction.

Referenced by sql_cclose().

static int sql_initDB | ( | dbiIndex | dbi | ) |

static

Verify the DB is setup.

. if not initialize it

Create the table.. create the db_info

Definition at line 699 of file sqlite.c.

References _debug, _free(), _sql_db_s::db, DB_EXCL, endian, RPM_BIN_TYPE, RPM_I18NSTRING_TYPE, RPM_MASK_TYPE, RPM_STRING_ARRAY_TYPE, RPM_STRING_TYPE, RPM_UINT16_TYPE, RPM_UINT32_TYPE, RPM_UINT64_TYPE, RPM_UINT8_TYPE, RPMDBI_PACKAGES, rpmGenPath(), rpmlog(), RPMLOG_WARNING, scpFree(), scpNew(), tagName(), tagType(), and _dbswap::uc.

Referenced by sql_open().

static int sql_join | ( | dbiIndex | dbi,
|  | DBC ** | curslist,
|  | DBC ** | dbcp,
|  | unsigned int | flags
| ) |  |

static

Return join cursor for list of cursors.

Parameters

  dbi | index database handle
  curslist | NULL terminated list of database cursors

Return values

  dbcp | address of join database cursor

Parameters

  flags | DB_JOIN_NOSORT or 0

Returns
  0 on success

Definition at line 1454 of file sqlite.c.

References _debug.

static int sql_open | ( | rpmdb | rpmdb,
|  | rpmTag | rpmtag,
|  | dbiIndex * | dbip
| ) |  |

static

Return handle for an index database.

Parameters

  rpmdb | rpm database
  rpmtag | rpm tag

Return values

  *dbip | index database handle

Returns
  0 on success

Definition at line 908 of file sqlite.c.

References _free(), Chmod(), D_, _sql_db_s::db, DB_PRIVATE, enterChroot(), leaveChroot(), rpmGenPath(), rpmioMkpath(), rpmlog(), RPMLOG_DEBUG, sql_busy_handler(), sql_close(), sql_initDB(), sqlitevec, stpcpy(), tagName(), _sql_db_s::transaction, urlPath(), xcalloc(), and xstrdup().

static int sql_startTransaction | ( | dbiIndex | dbi | ) |

static

Definition at line 608 of file sqlite.c.

References _debug, _sql_db_s::db, and _sql_db_s::transaction.

Referenced by sql_commitTransaction(), and sql_copen().

static int sql_step | ( | dbiIndex | dbi,
|  | SCP_t | scp
| ) |  |

static

Definition at line 353 of file sqlite.c.

References _DBSWAP, _debug, _sql_dbcursor_s::ac, _sql_dbcursor_s::av, _sql_dbcursor_s::avlen, _sql_dbcursor_s::cmd, _sql_dbcursor_s::nalloc, _sql_dbcursor_s::nc, _sql_dbcursor_s::nr, _sql_dbcursor_s::pStmt, _dbswap::ui, xmalloc(), xrealloc(), and xstrdup().

Referenced by sql_cdel(), sql_cget(), and sql_cput().

static int sql_sync | ( | dbiIndex | dbi,
|  | unsigned int | flags
| ) |  |

static

Flush pending operations to disk.

Parameters

  dbi | index database handle
  flags | (unused)

Returns
  0 on success

Definition at line 1056 of file sqlite.c.

References enterChroot(), leaveChroot(), and sql_commitTransaction().

## Variable Documentation

int _debug = 0

static

Definition at line 66 of file sqlite.c.

Referenced by dbg_keyval(), dbg_scp(), enterChroot(), leaveChroot(), scpFree(), scpNew(), scpReset(), scpResetAv(), scpResetKeys(), sql_associate(), sql_cclose(), sql_ccount(), sql_cdup(), sql_cget(), sql_commitTransaction(), sql_copen(), sql_cpget(), sql_endTransaction(), sql_initDB(), sql_join(), sql_startTransaction(), and sql_step().

unsigned int endian = 0x11223344

static

Definition at line 127 of file sqlite.c.

Referenced by sql_byteswapped(), and sql_initDB().

const char* sqlCwd = NULL

static

Definition at line 130 of file sqlite.c.

Referenced by enterChroot(), and leaveChroot().

int sqlInRoot = 0

static

Definition at line 132 of file sqlite.c.

Referenced by enterChroot(), and leaveChroot().

struct _dbiVec sqlitevec

Initial value:
= {
0, 0, 0,
sql_open,
sql_close,
sql_sync,
sql_associate,
sql_join,
sql_copen,
sql_cclose,
sql_cdup,
sql_cdel,
sql_cget,
sql_cpget,
sql_cput,
sql_ccount,
sql_byteswapped,
sql_stat
}

Definition at line 1590 of file sqlite.c.

Referenced by sql_open().