⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simple.cpp

📁 全新的纯内存式实时数据库
💻 CPP
字号:
/******************************************************************* *                                                                 * *  simple.cpp                                                      * *                                                                 * *  This file is a part of the eXtremeDB source code               * *  Copyright (c) 2001-2007 McObject LLC                           *  *  All Rights Reserved                                            * *                                                                 * *******************************************************************/#include "mcosql.h"using namespace McoSql;size_t const PAGE_SIZE = 1024/*128*/; // define eXtremeDB page sizesize_t const DATABASE_SIZE = 4*1024*1024; // define size of eXtrmeDB databasevoid*  const MAP_ADDRESS = (void*)0x20000000; // definemapping address for eXtremeDB database// Define structure correponding to database record // (will be done automatically by mcocomp in futurestruct Person {     char const* name;    int         age;    float       weight;};// Forward declaration of function describing database schemaGET_DICTIONARY(simpledb);McoSqlEngine engine; // McoSQL engine // This function will add new record in databasevoid addPerson(Person* p) {    // Add new record to the table.     // Record will be initialized with data passed throgh Person struct.    engine.executeStatement("insert into Person %r", p);} // This function select all records from the tablevoid listPersons()  {    // Execute query and store returned data sourse in QueryResult object     // which destructor will automatically release this data source after return from     // the function    QueryResult result(engine.executeQuery("select * from Person order by age"));    // Get cursor    Cursor* cursor = result->records();        // Loop for all records    while (cursor->hasNext()) {         Person p;        // Get next record        Record* rec = cursor->next();        // Extract record to the correspondent struct        result->extract(rec, &p, sizeof(p));            printf("%s %d %f\n", p.name, p.age, p.weight);    }}// Serach person by name pattern void searchPersonByName(char const* name) {    // Excecute query with given parameter "name"    QueryResult result(engine.executeQuery("select * from Person where name like %s", name));    // Get cursor    Cursor* cursor = result->records();        // Loop for all records    while (cursor->hasNext()) {         Person p;        // Get next record        Record* rec = cursor->next();        // Extract record to the correspondent struct        result->extract(rec, &p, sizeof(p));            printf("%s %d %f\n", p.name, p.age, p.weight);    }}// Calculate average age of persons using aggregate functions void calculateAverageAge(){    // Execute query calculating average age of all persons    QueryResult result(engine.executeQuery("select avg(age) from Person"));    // Get cursor    Cursor* cursor = result->records();    // Result data source consists of one record with single column.    // Indices of columns starts from 0    Value* avgAge = cursor->next()->get(0);    printf("Average age: %d\n", (int)avgAge->intValue());}// Update recordvoid updatePersonData(char const* name, int age, float weight){    // Update person with specified name (passed as query parameter).    // Result of this statement execution is number of updated records.    int ret = engine.executeStatement("update Person set age=%i, weight=%f where name=%s",                                       age, weight, name);    if (ret == 0) {         printf("Person %s not found\n", name);    }}   // Delete record from the tablevoid deletePerson(char const* name){    // Delete person with specified name (passed as query parameter).    // Result of this statement execution is number of removed records.    int ret = engine.executeStatement("delete from Person where name=%s", name);    if (ret == 0) {         printf("Person %s not found\n", name);    }}int main(){    Person p;    // Open eXtremeDB database and SQL engine    engine.open("simpledb", // database name                simpledb_get_dictionary(), // database dictionary                DATABASE_SIZE, // database size                PAGE_SIZE,  // page size                MAP_ADDRESS); // mapping address for shared memory mode    p.name = "John Smith";    p.age = 35;    p.weight = 72.1f;        addPerson(&p);    p.name = "Peter Brown";    p.age = 40;    p.weight = 62.1f;        addPerson(&p);    listPersons();        searchPersonByName("John%");    searchPersonByName("%Brown%");        calculateAverageAge();    updatePersonData("John Smith", 36, 75.2f);    updatePersonData("Peter Brown", 41, 65.0f);    listPersons();    deletePerson("John Smith");    deletePerson("Peter Brown");    // Close daatbase and SQL engine    engine.close();    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -