/*-< CLITEST.C >-----------------------------------------------------*--------*
* FastDB Version 1.0 (c) 1999 GARRET * ? *
* (Main Memory Database Management System) * /\| *
* * / \ *
* Created: 13-Jan-2000 K.A. Knizhnik * / [] \ *
* Last update: 13-Jan-2000 K.A. Knizhnik * GARRET *
*-------------------------------------------------------------------*--------*
* Test for FastDB call level interface
* Spawn "subsql clitest.sql" to start CLI server.
*-------------------------------------------------------------------*--------*/
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "cli.h"
typedef struct person {
char name[64];
cli_int8_t salary;
char* address;
cli_real8_t weight;
cli_int4_t n_subordinates;
cli_oid_t* subordinates;
} person;
void* set_subordinates(int var_type, void* var_ptr, int len)
{
person* p = (person*)var_ptr;
if (p->subordinates != NULL) {
free(p->subordinates);
}
p->n_subordinates = len;
p->subordinates = (cli_oid_t*)malloc(len*sizeof(cli_oid_t));
return p->subordinates;
}
void* get_subordinates(int var_type, void* var_ptr, int* len)
{
person* p = (person*)var_ptr;
*len = p->n_subordinates;
return p->subordinates;
}
static cli_field_descriptor person_descriptor[] = {
{cli_asciiz, cli_hashed, "name"},
{cli_int8, cli_indexed, "salary"},
{cli_pasciiz, 0, "address"},
{cli_real8, 0, "weight"},
{cli_array_of_oid, 0, "subordinates", "persons"}
};
int main()
{
char* serverURL = "localhost:6100";
char* databaseName = "clitest";
char* filePath = "clitest.fdb";
int session, statement, statement2, rc, len;
int i, n, salary;
int table_created = 0;
char name[256];
char address[256];
cli_oid_t oid;
person p;
session = cli_open(serverURL, 10, 1);
if (session == cli_bad_address) {
session = cli_create(databaseName, filePath, 0, 0, 4*1024*1024, 4*1024*1024, 512*1024, 0);
}
if (session < 0) {
fprintf(stderr, "cli_open failed with code %d\n", session);
return EXIT_FAILURE;
}
rc = cli_create_table(session, "persons", sizeof(person_descriptor)/sizeof(cli_field_descriptor),
person_descriptor);
if (rc == cli_ok) {
table_created = 1;
rc = cli_alter_index(session, "persons", "salary", cli_indexed);
if (rc != cli_ok) {
fprintf(stderr, "cli_alter_index failed with code %d\n", rc);
return EXIT_FAILURE;
}
rc = cli_alter_index(session, "persons", "name", cli_indexed);
if (rc != cli_ok) {
fprintf(stderr, "cli_alter_index 2 failed with code %d\n", rc);
return EXIT_FAILURE;
}
} else if (rc != cli_table_already_exists && rc != cli_not_implemented) {
fprintf(stderr, "cli_create_table failed with code %d\n", rc);
return EXIT_FAILURE;
}
statement = cli_statement(session, "insert into persons");
if (statement < 0) {
fprintf(stderr, "cli_statement failed with code %d\n", statement);
return EXIT_FAILURE;
}
if ((rc=cli_column(statement, "name", cli_asciiz, NULL, p.name)) != cli_ok
|| (rc=cli_column(statement, "salary", cli_int8, NULL, &p.salary)) != cli_ok
|| (rc=cli_column(statement, "address", cli_pasciiz, &len, &p.address)) != cli_ok
|| (rc=cli_column(statement, "weight", cli_real8, NULL, &p.weight)) != cli_ok
|| (rc=cli_array_column(statement, "subordinates", cli_array_of_oid, &p,
set_subordinates, get_subordinates)) != cli_ok)
{
fprintf(stderr, "cli_column 1 failed with code %d\n", rc);
return EXIT_FAILURE;
}
strcpy(p.name, "John Smith");
p.salary = 75000;
p.address = "1 Guildhall St., Cambridge CB2 3NH, UK";
p.weight = 80.3;
p.n_subordinates = 0;
p.subordinates = NULL;
rc = cli_insert(statement, &oid);
if (rc != cli_ok) {
fprintf(stderr, "cli_insert failed with code %d\n", rc);
return EXIT_FAILURE;
}
strcpy(p.name, "Joe Cooker");
p.salary = 100000;
p.address = "Outlook drive, 15/3";
p.weight = 80.3;
p.n_subordinates = 1;
p.subordinates = &oid;
rc = cli_insert(statement, NULL);
if (rc != cli_ok) {
fprintf(stderr, "cli_insert 2 failed with code %d\n", rc);
return EXIT_FAILURE;
}
rc = cli_free(statement);
if (rc != cli_ok) {
fprintf(stderr, "cli_free failed with code %d\n", rc);
return EXIT_FAILURE;
}
p.subordinates = NULL;
statement = cli_statement(session,
"select * from persons where "
"length(subordinates) < %subordinates and salary > %salary");
if (statement < 0) {
fprintf(stderr, "cli_statement 2 failed with code %d\n", rc);
return EXIT_FAILURE;
}
p.address = address;
len = sizeof(address);
if ((rc=cli_column(statement, "name", cli_asciiz, NULL, p.name)) != cli_ok
|| (rc=cli_column(statement, "salary", cli_int8, NULL, &p.salary)) != cli_ok
|| (rc=cli_column(statement, "address", cli_pasciiz, &len, &p.address)) != cli_ok
|| (rc=cli_column(statement, "weight", cli_real8, NULL, &p.weight)) != cli_ok
|| (rc=cli_array_column(statement, "subordinates", cli_array_of_oid, &p,
set_subordinates, get_subordinates)) != cli_ok)
{
fprintf(stderr, "cli_column 2 failed with code %d\n", rc);
return EXIT_FAILURE;
}
if ((rc = cli_parameter(statement, "%subordinates", cli_int4, &n)) != cli_ok
|| (rc = cli_parameter(statement, "%salary", cli_int4, &salary)) != cli_ok)
{
fprintf(stderr, "cli_parameter failed with code %d\n", rc);
return EXIT_FAILURE;
}
n = 2;
salary = 90000;
rc = cli_fetch(statement, cli_view_only);
if (rc != 1) {
fprintf(stderr, "cli_fetch 1 returns %d instead of 1\n", rc);
return EXIT_FAILURE;
}
n = 10;
salary = 50000;
rc = cli_fetch(statement, cli_for_update);
if (rc != 2) {
fprintf(stderr, "cli_fetch 2 returns %d instead of 2\n", rc);
return EXIT_FAILURE;
}
statement2 = cli_statement(session, "select * from persons where current = %oid");
if (statement2 < 0) {
fprintf(stderr, "cli_statement 3 failed with code %d\n", rc);
return EXIT_FAILURE;
}
if ((rc=cli_column(statement2, "name", cli_asciiz, NULL, name)) != cli_ok) {
fprintf(stderr, "cli_column 3 failed with code %d\n", rc);
return EXIT_FAILURE;
}
if ((rc = cli_parameter(statement2, "%oid", cli_oid, &oid)) != cli_ok) {
fprintf(stderr, "cli_parameter 3 failed with code %d\n", rc);
return EXIT_FAILURE;
}
while ((rc = cli_get_next(statement)) == cli_ok) {
printf("%s\t%ld\t%f\t%s\n", p.name, (long)p.salary, p.weight, p.address);
if (p.n_subordinates > 0) {
printf("Manages:\n");
for (i = 0; i < p.n_subordinates; i++) {
oid = p.subordinates[i];
rc = cli_fetch(statement2, cli_view_only);
if (rc != 1) {
fprintf(stderr, "cli_fetch by oid failed with code %d\n", rc);
return EXIT_FAILURE;
}
if ((rc = cli_get_first(statement2)) != cli_ok) {
fprintf(stderr, "cli_get_first failed with code %d\n", rc);
return EXIT_FAILURE;
}
printf("\t%s\n", name);
}
}
p.salary = p.salary*90/100;
rc = cli_update(statement);
if (rc != cli_ok) {
fprint
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
fastdb-3.05.tar.gz_fastdb_内存数据库 (345个子文件)
makefile.acc 4KB
genparse.bat 458B
runtests.bat 38B
make.bat 36B
makefile.bcc 5KB
FastDB.bpg 2KB
clitest.c 10KB
clitest2.c 7KB
testalter.c 4KB
doxygen.cfg 44KB
CHANGES 26KB
CONTENTS 515B
database.cpp 216KB
subsql.cpp 107KB
bugdb.cpp 78KB
localcli.cpp 78KB
class.cpp 55KB
compiler.cpp 52KB
server.cpp 50KB
cli.cpp 38KB
file.cpp 36KB
scan.cpp 35KB
scan.cpp 35KB
scan.cpp 35KB
clidb.cpp 33KB
ttree.cpp 31KB
gram.tab.cpp 28KB
gram.tab.cpp 28KB
gram.tab.cpp 28KB
w32sock.cpp 26KB
wwwapi.cpp 23KB
cursor.cpp 21KB
sync.cpp 18KB
unisock.cpp 14KB
testdb.cpp 14KB
GiST.cpp 11KB
GiSTnode.cpp 9KB
hashtab.cpp 9KB
query.cpp 9KB
testjoin.cpp 7KB
testtrav.cpp 7KB
repsock.cpp 6KB
testconc.cpp 5KB
testsync.cpp 5KB
RTnode.cpp 5KB
command.cpp 5KB
command.cpp 5KB
command.cpp 5KB
testiref.cpp 4KB
testperf.cpp 4KB
guess.cpp 4KB
guess_std.cpp 4KB
guess2.cpp 4KB
testraw.cpp 3KB
cgistub.cpp 3KB
testddl.cpp 3KB
container.cpp 3KB
GiSTfile.cpp 3KB
testharr.cpp 3KB
BTnode.cpp 3KB
RTnode.cpp 3KB
cleanupsem.cpp 2KB
testidx.cpp 2KB
BTentry.cpp 2KB
testleak.cpp 2KB
symtab.cpp 2KB
testconcur.cpp 2KB
RT.cpp 2KB
RTentry.cpp 2KB
GiSTdb.cpp 1KB
forcerecovery.cpp 1KB
GiSTcursor.cpp 1KB
RTpredicate.cpp 1KB
RTpredicate.cpp 1KB
BTpredicate.cpp 984B
RTentry.cpp 638B
GiSTpredicate.cpp 547B
FastDbCli.cs 48KB
FastDbFields.cs 32KB
FastDbConnection.cs 18KB
FastDbCommand.cs 14KB
Test.cs 4KB
TestIndex.cs 4KB
AssemblyInfo.cs 2KB
FastDbNet.csproj 4KB
Test2.csproj 4KB
Test1.csproj 4KB
doxygen.css 5KB
compiler.d 8KB
fastdb.dsp 6KB
subsql.dsp 4KB
cli.dsp 3KB
fastdb.dsw 858B
gist.gif 2KB
database.h 41KB
class.h 33KB
cli.h 30KB
timeseries.h 25KB
query.h 24KB
sync_unix.h 19KB
共 345 条
- 1
- 2
- 3
- 4
资源评论
钱亚锋
- 粉丝: 86
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功