int
main(int argc, char **argv)
{
    unsigned opt;
    char *param, *filename=0, *endptr=0;
    unsigned short dbname=0xffff;
    int full=0;

    ham_u16_t names[1024];
    ham_size_t i, names_count=1024;
    ham_status_t st;
    ham_env_t *env;
    ham_db_t *db;

    ham_u32_t maj, min, rev;
    const char *licensee, *product;
    ham_get_license(&licensee, &product);
    ham_get_version(&maj, &min, &rev);

    getopts_init(argc, argv, "ham_info");

    while ((opt=getopts(&opts[0], &param))) {
        switch (opt) {
            case ARG_DBNAME:
                if (!param) {
                    printf("Parameter `dbname' is missing.\n");
                    return (-1);
                }
                dbname=(short)strtoul(param, &endptr, 0);
                if (endptr && *endptr) {
                    printf("Invalid parameter `dbname'; numerical value "
                           "expected.\n");
                    return (-1);
                }
                break;
            case ARG_FULL:
                full=1;
                break;
            case GETOPTS_PARAMETER:
                if (filename) {
                    printf("Multiple files specified. Please specify "
                           "only one filename.\n");
                    return (-1);
                }
                filename=param;
                break;
            case ARG_HELP:
                printf("hamsterdb %d.%d.%d - Copyright (C) 2005-2007 "
                       "Christoph Rupp ([email protected]).\n\n",
                       maj, min, rev);

                if (licensee[0]=='\0')
                    printf(
                       "This program is free software; you can redistribute "
                       "it and/or modify it\nunder the terms of the GNU "
                       "General Public License as published by the Free\n"
                       "Software Foundation; either version 2 of the License,\n"
                       "or (at your option) any later version.\n\n"
                       "See file COPYING.GPL2 and COPYING.GPL3 for License "
                       "information.\n\n");
                else
                    printf("Commercial version; licensed for %s (%s)\n\n",
                            licensee, product);

                printf("usage: ham_info [-db DBNAME] [-f] file\n");
                printf("usage: ham_info -h\n");
                printf("       -h:         this help screen (alias: --help)\n");
                printf("       -db DBNAME: only print info about "
                        "this database (alias: --dbname=<arg>)\n");
                printf("       -f:         print full information "
                        "(alias: --full)\n");
                return (0);
            default:
                printf("Invalid or unknown parameter `%s'. "
                       "Enter `ham_info --help' for usage.", param);
                return (-1);
        }
    }

    if (!filename) {
        printf("Filename is missing. Enter `ham_info --help' for usage.\n");
        return (-1);
    }

    /*
     * open the environment
     */
    st=ham_env_new(&env);
    if (st!=HAM_SUCCESS)
        error("ham_env_new", st);
    st=ham_env_open(env, filename, HAM_READ_ONLY);
    if (st==HAM_FILE_NOT_FOUND) {
        printf("File `%s' not found or unable to open it\n", filename);
        return (-1);
    }
    else if (st!=HAM_SUCCESS)
        error("ham_env_open", st);

    /*
     * print information about the environment
     */
    print_environment(env);

    /*
     * get a list of all databases
     */
    st=ham_env_get_database_names(env, names, &names_count);
    if (st!=HAM_SUCCESS)
        error("ham_env_get_database_names", st);

    /*
     * did the user specify a database name? if yes, show only this database
     */
    if (dbname!=0xffff) {
        st=ham_new(&db);
        if (st)
            error("ham_new", st);
    
        st=ham_env_open_db(env, db, dbname, 0, 0);
        if (st==HAM_DATABASE_NOT_FOUND) {
            printf("Database %u (0x%x) not found\n", dbname, dbname);
            return (-1);
        }
        else if (st)
            error("ham_env_open_db", st);
    
        print_database(db, dbname, full);
    
        st=ham_close(db, 0);
        if (st)
            error("ham_close", st);
        ham_delete(db);
    }
    else {
        /*
         * otherwise: for each database: print information about the database
         */
        for (i=0; i<names_count; i++) {
            st=ham_new(&db);
            if (st)
                error("ham_new", st);
    
            st=ham_env_open_db(env, db, names[i], 0, 0);
            if (st)
                error("ham_env_open_db", st);
    
            print_database(db, names[i], full);
    
            st=ham_close(db, 0);
            if (st)
                error("ham_close", st);
            ham_delete(db);
        }
    } 
    /*
     * clean up
     */
    st=ham_env_close(env, 0);
    if (st!=HAM_SUCCESS)
        error("ham_env_close", st);

    ham_env_delete(env);

    return (0);
}
Пример #2
0
int
main(int argc, char **argv)
{
    int i;
    ham_status_t st;        /* status variable */
    ham_db_t *db[MAX_DBS];  /* hamsterdb database objects */
    ham_env_t *env;         /* hamsterdb environment */
    ham_cursor_t *cursor[MAX_DBS]; /* a cursor for each database */
    ham_key_t key, cust_key, ord_key, c2o_key;
    ham_record_t record, cust_record, ord_record, c2o_record;

    customer_t customers[MAX_CUSTOMERS]={
        { 1, "Alan Antonov Corp." },
        { 2, "Barry Broke Inc." },
        { 3, "Carl Caesar Lat." },
        { 4, "Doris Dove Brd." }
    };

    order_t orders[MAX_ORDERS]={
        { 1, 1, "Joe" },
        { 2, 1, "Tom" },
        { 3, 3, "Joe" },
        { 4, 4, "Tom" },
        { 5, 3, "Ben" },
        { 6, 3, "Ben" },
        { 7, 4, "Chris" },
        { 8, 1, "Ben" }
    };

    memset(&key, 0, sizeof(key));
    memset(&record, 0, sizeof(record));
    memset(&cust_key, 0, sizeof(cust_key));
    memset(&cust_record, 0, sizeof(cust_record));
    memset(&ord_key, 0, sizeof(ord_key));
    memset(&ord_record, 0, sizeof(ord_record));
    memset(&c2o_key, 0, sizeof(c2o_key));
    memset(&c2o_record, 0, sizeof(c2o_record));

    /*
     * first, create a new hamsterdb environment
     */
    st=ham_env_new(&env);
    if (st!=HAM_SUCCESS)
        error("ham_env_new", st);

    /*
     * then create the database objects
     */
    for (i=0; i<MAX_DBS; i++) {
        st=ham_new(&db[i]);
        if (st!=HAM_SUCCESS)
            error("ham_new", st);
    }

    /*
     * Now create a new database file for the Environment
     */
    st=ham_env_create_ex(env, "test.db", 0, 0664, 0);
    if (st!=HAM_SUCCESS)
        error("ham_env_create", st);

    /*
     * Then create the two Databases in this Environment; each Database
     * has a name - the first is our "customer" Database, the second
     * is for the "orders"; the third manages our 1:n relation and
     * therefore needs to enable duplicate keys
     */
    st=ham_env_create_db(env, db[DBIDX_CUSTOMER], DBNAME_CUSTOMER, 0, 0);
    if (st!=HAM_SUCCESS)
        error("ham_env_create_db(customer)", st);
    st=ham_env_create_db(env, db[DBIDX_ORDER], DBNAME_ORDER, 0, 0);
    if (st!=HAM_SUCCESS)
        error("ham_env_create_db(order)", st);
    st=ham_env_create_db(env, db[DBIDX_C2O], DBNAME_C2O,
            HAM_ENABLE_DUPLICATES, 0);
    if (st!=HAM_SUCCESS)
        error("ham_env_create_db(c2o)", st);

    /*
     * create a Cursor for each Database
     */
    for (i=0; i<MAX_DBS; i++) {
        st=ham_cursor_create(db[i], 0, 0, &cursor[i]);
        if (st!=HAM_SUCCESS)
            error("ham_cursor_create" , st);
    }

    /*
     * Insert the customers in the customer table
     *
     * INSERT INTO customers VALUES (1, "Alan Antonov Corp.");
     * INSERT INTO customers VALUES (2, "Barry Broke Inc.");
     * etc
     */
    for (i=0; i<MAX_CUSTOMERS; i++) {
        key.size=sizeof(int);
        key.data=&customers[i].id;

        record.size=sizeof(customer_t);
        record.data=&customers[i];

        st=ham_insert(db[0], 0, &key, &record, 0);
        if (st!=HAM_SUCCESS)
            error("ham_insert (customer)", st);
    }

    /*
     * And now the orders in the second Database; contrary to env1,
     * we only store the assignee, not the whole structure
     *
     * INSERT INTO orders VALUES (1, "Joe");
     * INSERT INTO orders VALUES (2, "Tom");
     */
    for (i=0; i<MAX_ORDERS; i++) {
        key.size=sizeof(int);
        key.data=&orders[i].id;

        record.size=sizeof(orders[i].assignee);
        record.data=orders[i].assignee;

        st=ham_insert(db[1], 0, &key, &record, 0);
        if (st!=HAM_SUCCESS)
            error("ham_insert (order)", st);
    }

    /*
     * and now the 1:n relationships; the flag HAM_DUPLICATE creates
     * a duplicate key, if the key already exists
     *
     * INSERT INTO c2o VALUES (1, 1);
     * INSERT INTO c2o VALUES (2, 1);
     * etc
     */
    for (i=0; i<MAX_ORDERS; i++) {
        key.size=sizeof(int);
        key.data=&orders[i].customer_id;

        record.size=sizeof(int);
        record.data=&orders[i].id;

        st=ham_insert(db[2], 0, &key, &record, HAM_DUPLICATE);
        if (st!=HAM_SUCCESS)
            error("ham_insert(c2o)", st);
    }

    /*
     * now start the query - we want to dump each customer with his
     * orders
     *
     * loop over the customer; for each customer, loop over the 1:n table
     * and pick those orders with the customer id. then load the order
     * and print it
     *
     * the outer loop is similar to
     * SELECT * FROM customers WHERE 1;
     */
    while (1) {
        customer_t *customer;

        st=ham_cursor_move(cursor[0], &cust_key, &cust_record, HAM_CURSOR_NEXT);
        if (st!=HAM_SUCCESS) {
            /* reached end of the database? */
            if (st==HAM_KEY_NOT_FOUND)
                break;
            else
                error("ham_cursor_next(customer)", st);
        }

        customer=(customer_t *)cust_record.data;

        /* print the customer id and name */
        printf("customer %d ('%s')\n", customer->id, customer->name);

        /*
         * loop over the 1:n table
         *
         * before we start the loop, we move the cursor to the
         * first duplicate key
         *
         * SELECT * FROM customers, orders, c2o
         *   WHERE c2o.customer_id=customers.id AND
         *      c2o.order_id=orders.id;
         */
        c2o_key.data=&customer->id;
        c2o_key.size=sizeof(int);
        st=ham_cursor_find(cursor[2], &c2o_key, 0);
        if (st!=HAM_SUCCESS) {
            if (st==HAM_KEY_NOT_FOUND)
                continue;
            error("ham_cursor_find(c2o)", st);
        }
        st=ham_cursor_move(cursor[2], 0, &c2o_record, 0);
        if (st!=HAM_SUCCESS)
            error("ham_cursor_move(c2o)", st);

        do {
            int order_id;

            order_id=*(int *)c2o_record.data;
            ord_key.data=&order_id;
            ord_key.size=sizeof(int);

            /*
             * load the order
             * SELECT * FROM orders WHERE id = order_id;
             */
            st=ham_find(db[1], 0, &ord_key, &ord_record, 0);
            if (st!=HAM_SUCCESS)
                error("ham_find(order)", st);

            printf("  order: %d (assigned to %s)\n",
                    order_id, (char *)ord_record.data);

            /*
             * the flag HAM_ONLY_DUPLICATES restricts the cursor
             * movement to the duplicate list.
             */
            st=ham_cursor_move(cursor[2], &c2o_key,
                    &c2o_record, HAM_CURSOR_NEXT|HAM_ONLY_DUPLICATES);
            if (st!=HAM_SUCCESS) {
                /* reached end of the database? */
                if (st==HAM_KEY_NOT_FOUND)
                    break;
                else
                    error("ham_cursor_next(c2o)", st);
            }
        } while(1);
    }

    /*
     * Now close the Environment handle; the flag
     * HAM_AUTO_CLEANUP will automatically close all Databases and
     * Cursors
     */
    st=ham_env_close(env, HAM_AUTO_CLEANUP);
    if (st!=HAM_SUCCESS)
        error("ham_env_close", st);

    for (i=0; i<MAX_DBS; i++)
         ham_delete(db[i]);

    ham_env_delete(env);


#if UNDER_CE
    error("success", 0);
#endif
    printf("success!\n");
    return (0);
}
Пример #3
0
int 
main(void)
{
    ham_db_t *db;
    ham_env_t *env;
    ham_srv_t *srv;
    ham_srv_config_t cfg;
    ham_status_t st;
    char input[1024];

    ham_env_new(&env);
    st=ham_env_create_ex(env, "env1.db", HAM_ENABLE_TRANSACTIONS, 0644, 0);
    if (st) {
        printf("ham_env_create_ex: %d\n", st);
        exit(-1);
    }

    ham_new(&db);
    st=ham_env_create_db(env, db, 12, HAM_ENABLE_DUPLICATES, 0);
    if (st) {
        printf("ham_env_create_db: %d\n", st);
        exit(-1);
    }
    ham_close(db, 0);

    st=ham_env_create_db(env, db, 13, HAM_ENABLE_DUPLICATES, 0);
    if (st) {
        printf("ham_env_create_db: %d\n", st);
        exit(-1);
    }
    ham_close(db, 0);

    st=ham_env_create_db(env, db, 33, 
                HAM_RECORD_NUMBER|HAM_ENABLE_DUPLICATES, 0);
    if (st) {
        printf("ham_env_create_db: %d\n", st);
        exit(-1);
    }
    ham_close(db, 0);

    memset(&cfg, 0, sizeof(cfg));
    cfg.port=8080;
    ham_srv_init(&cfg, &srv);
    ham_srv_add_env(srv, env, "/env1.db");

    printf("server1%s started - please run sample 'client1%s' for a test\n", 
            EXT, EXT);
    printf("type 'exit' to end the server\n");
    
    while (1) {
        printf("> ");
        scanf("%s", &input[0]);
        if (!strcmp(input, "exit")) {
            printf("exiting...\n");
            break;
        }
        printf("unknown command\n");
    }

    ham_srv_close(srv);
    ham_env_close(env, HAM_AUTO_CLEANUP);
    ham_env_delete(env);
    ham_delete(db);

    return (0);
}