Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ndbquery: compatibility with ndb/query #662

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 84 additions & 53 deletions src/cmd/ndb/ndbquery.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,79 +3,110 @@
#include <bio.h>
#include <ndb.h>

static int all, multiple;

/*
* search the database for matches
*/
void
usage(void)
{
fprint(2, "usage: query attr value [returned attribute]\n");
exits("usage");
fprint(2, "usage: ndbquery [-am] [-f ndbfile] attr value [returned attribute]\n");
exits("usage");
}

static void
prmatch(Ndbtuple *nt, char *rattr)
{
for(; nt; nt = nt->entry)
if(strcmp(nt->attr, rattr) == 0)
print("%s\n", nt->val);
}

void
search(Ndb *db, char *attr, char *val, char *rattr)
{
Ndbs s;
Ndbtuple *t;
Ndbtuple *nt;
char *p;
Ndbs s;
Ndbtuple *t;
Ndbtuple *nt;
char *p;

if(rattr && !all){
p = ndbgetvalue(db, &s, attr, val, rattr, &t);
if(multiple)
prmatch(t, rattr);
else if(p){
print("%s\n", p);
}
ndbfree(t);
free(p);
return;
}

if(rattr){
p = ndbgetvalue(db, &s, attr, val, rattr, nil);
if(p){
print("%s\n", p);
free(p);
}
return;
}
/* all entries with matching rattrs */
if(rattr){
t = ndbsearch(db, &s, attr, val);
while(t != nil){
prmatch(t, rattr);
ndbfree(t);
t = ndbsnext(&s, attr, val);
}
return;
}

t = ndbsearch(db, &s, attr, val);
while(t){
for(nt = t; nt; nt = nt->entry)
print("%s=%s ", nt->attr, nt->val);
print("\n");
ndbfree(t);
t = ndbsnext(&s, attr, val);
}
/* all entries */
t = ndbsearch(db, &s, attr, val);
while(t){
for(nt = t; nt; nt = nt->entry)
print("%s=%s ", nt->attr, nt->val);
print("\n");
ndbfree(t);
t = ndbsnext(&s, attr, val);
}
}

void
main(int argc, char **argv)
{
char *rattr = 0;
Ndb *db;
char *dbfile = 0;
int reps = 1;
char *rattr = 0;
Ndb *db;
char *dbfile = 0;
int reps = 1;

ARGBEGIN{
case 'f':
dbfile = ARGF();
break;
}ARGEND;
ARGBEGIN{
case 'a':
all++;
break;
case 'm':
multiple++;
break;
case 'f':
dbfile = ARGF();
break;
}ARGEND;

switch(argc){
case 4:
reps = atoi(argv[3]);
/* fall through */
case 3:
rattr = argv[2];
break;
case 2:
rattr = 0;
break;
default:
usage();
}
switch(argc){
case 4:
reps = atoi(argv[3]);
/* fall through */
case 3:
rattr = argv[2];
break;
case 2:
rattr = 0;
break;
default:
usage();
}

db = ndbopen(dbfile);
if(db == 0){
fprint(2, "no db files\n");
exits("no db");
}
while(reps--)
search(db, argv[0], argv[1], rattr);
ndbclose(db);
db = ndbopen(dbfile);
if(db == 0){
fprint(2, "no db files\n");
exits("no db");
}
while(reps--)
search(db, argv[0], argv[1], rattr);
ndbclose(db);

exits(0);
exits(0);
}