0ef31d2004-11-03Martin Nilsson #pike __REAL_VERSION__
e1fb092014-02-14Martin Nilsson #require constant(SQLite.SQLite)
0ef31d2004-11-03Martin Nilsson 
8f7db02017-03-04Henrik Grubbström (Grubba) //! Interface to SQLite3 databases.
e1fb092014-02-14Martin Nilsson // Cannot dump this since the #require check may depend on the // presence of system libs at runtime.
7094252017-03-31Henrik Grubbström (Grubba) optional constant dont_dump_program = 1;
a59c792010-11-02Martin Stjernholm 
8f7db02017-03-04Henrik Grubbström (Grubba) //!
0ef31d2004-11-03Martin Nilsson inherit SQLite.SQLite;
0a5ec32012-04-12Henrik Grubbström (Grubba) void create(string a, void|string b, void|mixed c, void|mixed d, void|mapping options) {
0ef31d2004-11-03Martin Nilsson  if(b) a += "/"+b; ::create(a); }
8f7db02017-03-04Henrik Grubbström (Grubba) //!
a83ab92008-07-31H. William Welliver III array list_fields(string n, string|void wild) { string qry = ""; qry = "PRAGMA table_info(" + n + ")"; array r = query(qry);
d4464b2017-04-01Henrik Grubbström (Grubba)  if (!r || !sizeof(r)) { predef::error("Unknown table: %s\n", n); }
a83ab92008-07-31H. William Welliver III  // now, we weed out the ones that don't match wild, if provided if(wild) {
3524712015-05-26Martin Nilsson  r = filter(r, lambda(mapping row)
a83ab92008-07-31H. William Welliver III  { return (search(row->name, wild) !=-1); } ); } array fields = ({}); foreach(r;; mapping f) { mapping fld = ([]); fld->name = f->name; fld->table = n;
3524712015-05-26Martin Nilsson  string t, l;
a83ab92008-07-31H. William Welliver III  if(!sscanf(f->type, "%s(%s)", t, l)) t = f->type; fld->length = (int)l; switch(t) { case "char": t = "string"; break; case "int": t = "integer"; break; } fld->type = t; fld->flags = (<>); if((int)f->notnull) fld->flags->not_null = 1; if((int)f->pk) { // primary key implies not null. fld->flags->not_null = 1; fld->flags->primary_key = 1; } fld->default = f->dflt_value; fields += ({fld}); } return fields; }
8f7db02017-03-04Henrik Grubbström (Grubba) //!
a83ab92008-07-31H. William Welliver III array list_tables(string|void n) { string qry = ""; if(n)
3524712015-05-26Martin Nilsson  qry = "SELECT name FROM SQLITE_MASTER WHERE name like '" + n + "%' and TYPE='table'";
a83ab92008-07-31H. William Welliver III  else
3524712015-05-26Martin Nilsson  qry = "SELECT name FROM SQLITE_MASTER where TYPE='table'";
a83ab92008-07-31H. William Welliver III  array r = query(qry); array out = ({}); foreach(r;;mapping row) { if(row->name) out += ({row->name}); else out += ({row["sqlite_master.name"] }); } return out; }