rpm  5.4.15
libsqlio.c
Go to the documentation of this file.
1 #include "system.h"
2 
3 #define _RPMSQL_INTERNAL
4 #define _RPMVT_INTERNAL
5 #define _RPMVC_INTERNAL
6 #include <rpmsql.h>
7 
8 #include <sqlite3.h> /* XXX sqlite3_module */
9 
10 #include "debug.h"
11 
12 /*==============================================================*/
13 #ifdef REFERENCE
14 /* --- MacPorts registry schema */
15 
16 BEGIN;
17 
18 /* -- metadata table */
19 CREATE TABLE registry.metadata (
20  key UNIQUE,
21  value
22 );
23 INSERT INTO registry.metadata (key, value) VALUES ('version', 1.000);
24 INSERT INTO registry.metadata (key, value) VALUES ('created', strftime('%s', 'now'));
25 
26 /* -- ports table */
27 CREATE TABLE registry.ports (
28  id INTEGER PRIMARY KEY AUTOINCREMENT,
29  name TEXT COLLATE NOCASE,
30  portfile CLOB,
31  url TEXT,
32  location TEXT,
33  epoch INTEGER,
34  version TEXT COLLATE VERSION,
35  revision INTEGER,
36  variants TEXT,
37  negated_variants TEXT,
38  state TEXT,
39  date DATETIME,
40  installtype TEXT,
41  archs TEXT,
42  requested INT,
43  os_platform TEXT,
44  os_major INTEGER,
45  UNIQUE (name, epoch, version, revision, variants),
46  UNIQUE (url, epoch, version, revision, variants)
47 );
48 CREATE INDEX registry.port_name ON ports
49  (name, epoch, version, revision, variants);
50 CREATE INDEX registry.port_url ON ports
51  (url, epoch, version, revision, variants);
52 CREATE INDEX registry.port_state ON ports (state);
53 
54 /* -- file map */
55 CREATE TABLE registry.files (
56  id INTEGER,
57  path TEXT,
58  actual_path TEXT,
59  active INT,
60  mtime DATETIME,
61  md5sum TEXT,
62  editable INT,
63  FOREIGN KEY(id) REFERENCES ports(id));
64 CREATE INDEX registry.file_port ON files (id);
65 CREATE INDEX registry.file_path ON files(path);
66 CREATE INDEX registry.file_actual ON files(actual_path);
67 
68 /* -- dependency map */
69 CREATE TABLE registry.dependencies (
70  id INTEGER,
71  name TEXT,
72  variants TEXT,
73  FOREIGN KEY(id) REFERENCES ports(id));
74 CREATE INDEX registry.dep_name ON dependencies (name);
75 
76 COMMIT;
77 
78 /* -- MacPorts temporaray tables. */
79 BEGIN;
80 
81 /* -- items cache */
82 CREATE TEMPORARY TABLE items (refcount, proc UNIQUE, name, url, path,
83  worker, options, variants);
84 
85 /* -- indexes list */
86 CREATE TEMPORARY TABLE indexes (file, name, attached);
87 
88 COMMIT;
89 
90 #endif
91 
92 /*==============================================================*/
93 #ifdef REFERENCE
94 /* --- Nix sqlite schema */
95 create table if not exists ValidPaths (
96  id integer primary key autoincrement not null,
97  path text unique not null,
98  hash text not null,
99  registrationTime integer not null,
100  deriver text
101 );
102 
103 create table if not exists Refs (
104  referrer integer not null,
105  reference integer not null,
106  primary key (referrer, reference),
107  foreign key (referrer) references ValidPaths(id) on delete cascade,
108  foreign key (reference) references ValidPaths(id) on delete restrict
109 );
110 
111 create index if not exists IndexReferrer on Refs(referrer);
112 create index if not exists IndexReference on Refs(reference);
113 
114 -- Paths can refer to themselves, causing a tuple (N, N) in the Refs
115 -- table. This causes a deletion of the corresponding row in
116 -- ValidPaths to cause a foreign key constraint violation (due to `on
117 -- delete restrict' on the `reference' column). Therefore, explicitly
118 -- get rid of self-references.
119 create trigger if not exists DeleteSelfRefs before delete on ValidPaths
120  begin
121  delete from Refs where referrer = old.id and reference = old.id;
122  end;
123 
124 create table if not exists DerivationOutputs (
125  drv integer not null,
126  id text not null, -- symbolic output id, usually "out"
127  path text not null,
128  primary key (drv, id),
129  foreign key (drv) references ValidPaths(id) on delete cascade
130 );
131 
132 create index if not exists IndexDerivationOutputs on DerivationOutputs(path);
133 
134 create table if not exists FailedPaths (
135  path text primary key not null,
136  time integer not null
137 );
138 #endif
139 
140 static struct rpmvd_s _nixdbVD = {
141  .dbpath = "/nix/var/nix/db/db.sqlite",
142  /* XXX glob needs adjustment for base32 and possibly length. */
143  .prefix = "/nix/store/[a-z0-9]*-",
144  .split = "/-",
145  .parse = "dir/instance-name",
146  .regex = "^(.+/)([^-]+)-(.*)$",
147  .idx = 2,
148 };
149 
150 static int nixdbCreateConnect(void * _db, void * pAux,
151  int argc, const char *const * argv,
152  rpmvt * vtp, char ** pzErr)
153 {
154  return rpmvtLoadArgv(rpmvtNew(_db, pAux, argv, &_nixdbVD), vtp);
155 }
156 
157 struct sqlite3_module nixdbModule = {
158  .xCreate = (void *) nixdbCreateConnect,
159  .xConnect = (void *) nixdbCreateConnect,
160 };
161 
162 /*==============================================================*/
163 
164 static struct rpmsqlVMT_s __VMT[] = {
165  { "Nixdb", &nixdbModule, NULL },
166  { NULL, NULL, NULL }
167 };
168 
169 extern int sqlite3_extension_init(void * _db);
170 int sqlite3_extension_init(void * _db)
171 {
172  int rc = 0; /* SQLITE_OK */
173 SQLDBG((stderr, "--> %s(%p)\n", __FUNCTION__, _db));
174  rc = _rpmsqlLoadVMT(_db, __VMT);
175 SQLDBG((stderr, "<-- %s(%p) rc %d\n", __FUNCTION__, _db, rc));
176  return rc;
177 }
#define INT
Definition: fnmatch.c:156
Definition: libsql.c:29
const char int time
Definition: bson.h:1005
#define VERSION
Definition: config.h:1310
static struct rpmvd_s _nixdbVD
Definition: libsqlio.c:140
static int nixdbCreateConnect(void *_db, void *pAux, int argc, const char *const *argv, rpmvt *vtp, char **pzErr)
Definition: libsqlio.c:150
struct rpmvt_s * rpmvt
Definition: rpmsql.h:19
int sqlite3_extension_init(void *_db)
Definition: libsqlio.c:170
rpmvt rpmvtNew(void *db, void *pModule, const char *const *argv, rpmvd vd)
Definition: rpmsql.c:108
struct rpmsw_s begin
Definition: rpmsw.h:40
static unsigned int hash(const char *str)
Definition: set.c:1346
static const char * file
Definition: parseFiles.c:20
static struct rpmsqlVMT_s __VMT[]
Definition: libsqlio.c:164
static void output(int indent, int *offset, const char *fmt,...)
Definition: rpmmtree.c:2497
const char const bson * key
Definition: mongo.h:717
const char const bson const bson bson * out
Definition: mongo.h:678
static Item * items
Definition: rpmcache.c:80
const bson * in
Definition: bson.h:746
struct sqlite3_module nixdbModule
Definition: libsqlio.c:157
static const char * name
struct key_s KEY
const char const bson const bson int int int options
Definition: mongo.h:569