Data Structures | Defines | Typedefs | Functions | Variables

# rpmdb/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
## Defines
#define | _RPMDB_INTERNAL
#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.
static int | sql_cclose (dbiIndex dbi, DBC *dbcursor, unsigned int flags)
| Close database cursor.
static int | sql_close (dbiIndex dbi, unsigned int flags)
| Close index database, and destroy database handle.
static int | sql_open (rpmdb rpmdb, rpmTag rpmtag, dbiIndex *dbip)
| Return handle for an index database.
static int | sql_sync (dbiIndex dbi, unsigned int flags)
| Flush pending operations to disk.
static int | sql_copen (dbiIndex dbi, DB_TXN *txnid, DBC **dbcp, unsigned int flags)
| Open database cursor.
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.
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.
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.
static int | sql_byteswapped (dbiIndex dbi)
| Is database byte swapped?
static int | sql_associate (dbiIndex dbi, dbiIndex dbisecondary, int(*callback)(DB *, const DBT *, const DBT *, DBT *), unsigned int flags)
| Associate secondary database with primary.
static int | sql_join (dbiIndex dbi, DBC **curslist, DBC **dbcp, unsigned int flags)
| Return join cursor for list of cursors.
static int | sql_cdup (dbiIndex dbi, DBC *dbcursor, DBC **dbcp, unsigned int flags)
| Duplicate a database cursor.
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.
static int | sql_ccount (dbiIndex dbi, DBC *dbcursor, unsigned int *countp, unsigned int flags)
| Retrieve count of (possible) duplicate items using dbcursor->c_count.
static int | sql_stat (dbiIndex dbi, unsigned int flags)
| Save statistics in database handle.
## Variables
static int | _debug = 0
static unsigned int | endian = 0x11223344
static const char * | sqlCwd = NULL
static int | sqlInRoot = 0
struct _dbiVec | sqlitevec

## Define 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 107 of file sqlite.c.

Referenced by sql_bind_key(), and sql_step().

#define _RPMDB_INTERNAL

Definition at line 44 of file sqlite.c.

## Typedef Documentation

typedef struct _sql_dbcursor_s* SCP_t

Definition at line 59 of file sqlite.c.

typedef struct _sql_db_s SQL_DB

Definition at line 58 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 194 of file sqlite.c.

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

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

static void dbg_scp | ( | void * | ptr ) | `[static]`

Definition at line 183 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 121 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 156 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 308 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 322 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 280 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 241 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 219 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 1412 of file sqlite.c.

References _debug.

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

Definition at line 576 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 504 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 663 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 1357 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 806 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 1486 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 1094 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 1448 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 1135 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(), 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 845 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 638 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 1057 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 1468 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 1315 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 616 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 685 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 1430 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

Returns:
  0 on success

Definition at line 892 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 594 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 340 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 1036 of file sqlite.c.

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

## Variable Documentation

int _debug = 0`[static]`

Definition at line 55 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 114 of file sqlite.c.

Referenced by sql_byteswapped(), and sql_initDB().

const char* sqlCwd = NULL`[static]`

Definition at line 117 of file sqlite.c.

Referenced by enterChroot(), and leaveChroot().

int sqlInRoot = 0`[static]`

Definition at line 119 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 1557 of file sqlite.c.

Referenced by sql_open().