?? cppdemo_main.cpp
字號:
/************************************************************
* *
* Copyright (c) 2001-2007 McObject LLC. All Right Reserved.*
* *
************************************************************/
/*
"This sample demonstartes -hpp eXtremeDB schema compiler option \n"
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cppdemo.hpp"
const char* dbname = "demo";
const int SEGSZ = 1024 * 2000;
const uint2 PAGESIZE = 128;
void _SH_(void)
{
char text[] =
{
"\nThis sample demonstartes -hpp eXtremeDB schema compiler option \n"
};
char text1[] =
{
"Copyright (c) 2001-2007 McObject LLC. All Right Reserved.\n\n"
};
printf("%s\neXtremeDB runtime version %d.%d, build %d\n%s\n\nPress Enter to start", text, MCO_COMP_VER_MAJOR,
MCO_COMP_VER_MINOR, MCO_COMP_BUILD_NUM, text1);
getchar();
}
inline void checkrc(MCO_RET rc)
{
if (rc != MCO_S_OK)
{
printf("Unexpected error: %d\n", rc);
exit( - 1);
}
}
class MyPart: public cppdemo::Part
{
public:
MCO_RET stype_put(const char* s)
{
return type_put(s, (uint2)strlen(s) + 1);
}
MCO_RET sname_put(const char* s)
{
return name_put(s, (uint2)strlen(s) + 1);
}
void print(mco_trans_h t);
const char* name()
{
static char bf[200];
uint2 len;
name_get(bf, sizeof(bf), len);
bf[len] = 0;
return bf;
}
const char* type()
{
static char bf[200];
uint2 len;
type_get(bf, sizeof(bf), len);
bf[len] = 0;
return bf;
}
};
void MyPart::print(mco_trans_h t)
{
if (!is_valid())
{
return ;
}
uint4 code;
code_get(code);
printf("\n[%d] %s : %s\n", code, name(), type());
cppdemo::Dimensions dim;
dim_read(dim);
float w, h, l;
dim.width_get(w);
dim.length_get(l);
dim.height_get(h);
printf("%g x %g x %g \n", l, w, h);
}
static const char* i2s(int i)
{
static char bf[20];
sprintf(bf, "%d", i);
return bf;
}
static int rand2(int lowlimit, int uplimit)
{
int n = rand();
return ((n % (uplimit - lowlimit + 1)) + lowlimit);
}
void populate_db(mco_db_h db)
{
mco_trans_h t = 0;
MCO_RET rc = MCO_S_OK;
int j;
for (j = 0; j < 10000; j++)
{
MyPart part;
cppdemo::Dimensions dim;
char temp[200];
rc = mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t);
if (rc)
{
break;
}
part.create(t);
part.stype_put(i2s(j % 1000));
sprintf(temp, "part # %d", j);
part.sname_put(temp);
part.price_put((float)(2.0 + j % 1000 / 2000.0));
part.code_put(1000000+j);
part.dim_write(dim);
dim.height_put(rand2(20, 40));
dim.width_put(rand2(20, 50));
dim.length_put(rand2(35, 90));
rc = mco_trans_commit(t);
t = 0;
if (rc)
{
break;
}
}
if (rc)
{
printf("Error in populate_db(): %d\n", rc);
if (t)
{
mco_trans_rollback(t);
}
}
else
{
if (!rc)
{
printf("Inserted %d parts to the database\n", j);
}
}
}
// print all parts with given type
MCO_RET printPartsOfTheType(mco_db_h db, const char* type)
{
mco_cursor_t csr;
mco_trans_h t;
MCO_RET rc = MCO_S_OK;
printf("Parts with type = %s :\n", type);
rc = mco_trans_start(db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &t);
if (rc)
{
return rc;
}
rc = cppdemo::Part::ByType::cursor(t, &csr);
if (rc)
{
return rc;
}
rc = cppdemo::Part::ByType::search(t, &csr, MCO_GE, type, (uint2)strlen(type) + 1, "", 0);
// empty name here for positioninig
for (; rc == MCO_S_OK; rc = mco_cursor_next(t, &csr))
{
MyPart part;
part.from_cursor(t, &csr);
if (strcmp(part.type(), type) != 0)
{
break;
}
part.print(t);
}
mco_trans_rollback(t); // read-only trn
return rc;
}
int main(void)
{
MCO_RET rc;
mco_db_h db1;
_SH_();
printf("I am a CPP demo\n");
printf("\neXtremeDB runtime version %d.%d, build %d\n\n", MCO_COMP_VER_MAJOR, MCO_COMP_VER_MINOR,
MCO_COMP_BUILD_NUM);
char* start_mem = new char[SEGSZ];
mco_runtime_start();
rc = mco_db_open(dbname, cppdemo_get_dictionary(), start_mem, SEGSZ, PAGESIZE);
checkrc(rc);
rc = mco_db_connect(dbname, &db1);
checkrc(rc);
populate_db(db1);
printPartsOfTheType(db1, "23");
rc = mco_db_disconnect(db1);
rc = mco_db_close(dbname);
mco_runtime_stop();
delete [](start_mem);
printf("Bye now!\n");
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -