ジャナ・ビジネス・コンサルティング有限会社トップページへ
/*
 * Berkeley DB XML .NET API
 * 
 * Copyright (C) 2007 Jana Business Consulting Ltd. All rights reserved.
 * 
 * For more information, see http://www.janabiz.com.
 * 
 */

using System;
using JanaBiz.Db;
using JanaBiz.DbXml;
using JanaBiz.DbXml.Exceptions;

public class SimpleQuery
{
    private static string theContainer = "simpleExampleData.dbxml";

    private static void doQuery(XmlManager mgr, string query)
    {
        // Perform a single query against the referenced container.
        Console.WriteLine("Exercising query: '" + query + "'.");
        Console.WriteLine("Return to continue: ");
        Console.ReadLine();

        // Perform the query
        using (XmlQueryContext context = mgr.CreateQueryContext()) {
            XmlResults results = mgr.Query(null, query, context, new XmlDocumentConfig());
            // Iterate over the results
            while (results.MoveNext()) {
                Console.WriteLine(results.Current);
            }
            Console.WriteLine(results.Size + " results returned for query '"
                              + query + "'.");
        }
    }

    public static void Main(string[] args)
    {
        string envdir = parseArguments(args);

        DbEnvironment dbEnv = null;
        XmlManager mgr = null;
        XmlContainer container = null;

        try {
            // Open an environment
            dbEnv = CreateDbEnvironment(envdir);

            // Open a manager
            using (mgr = CreateManager(dbEnv)) {
                // Open a non-transactional container
                using (container = mgr.OpenContainer(null, theContainer)) {
                    //Find all the Vendor documents in the database
                    doQuery(mgr, "collection(\"" + theContainer +
                                 "\")/vendor");

                    //Find all the vendors that are wholesale shops
                    doQuery(mgr, "collection(\"" + theContainer +
                                 "\")/vendor[@type=\"wholesale\"]");

                    //Find the product document for "Lemon Grass"
                    doQuery(mgr, "collection(\"" + theContainer +
                                 "\")/product/item[.=\"Lemon Grass\"]");

                    //Find all the products where the price is less than or equal to 0.11
                    doQuery(mgr, "collection(\"" + theContainer +
                                 "\")/product/inventory[number(price)<=0.11]");

                    //Find all the vegetables where the price is less than or equal to 0.11
                    doQuery(mgr, "collection(\"" + theContainer +
                                 "\")/product[number(inventory/price)<=0.11 and category=\"vegetables\"]");
                }
            }
        } catch (DbXmlException e) {
            Console.WriteLine("Error performing query against " + theContainer);
            Console.WriteLine(e.ToString());
        } finally {
            if (container != null) {
                container.Close();
            }
            if (mgr != null) {
                mgr.Close();
            }
            if (dbEnv != null) {
                dbEnv.Close();
            }
        }
    }

    public static DbEnvironment CreateDbEnvironment(string envdir)
    {
        DbEnvironmentConfig envconf = new DbEnvironmentConfig();
        envconf.CacheSize = 50*1024;
        envconf.AllowCreate = true;
        envconf.InitializeCache = true;
        envconf.Transactional = true;
        envconf.InitializeLocking = true;
        envconf.InitializeLogging = true;
        envconf.RunRecovery = true;
        envconf.ErrorWriter = Console.Out;
        envconf.ErrorPrefix = "LOG";

        DbEnvironment env = new DbEnvironment(envdir, envconf);

        return env;
    }

    public static XmlManager CreateManager(DbEnvironment env)
    {
        XmlManagerConfig mgrconfig = new XmlManagerConfig();
        mgrconfig.AdoptEnvironment = true;
        try {
            return new XmlManager(env, mgrconfig);
        } catch (Exception e) {
            env.Close();
            throw e;
        }
    }

    private static void Usage()
    {
        Console.WriteLine("This program performs various queries against a DBXML container.");
        Console.WriteLine("You should run exampleLoadContainer before running this example.");
        Console.WriteLine("You are only required to pass this command the path location of the database");
        Console.WriteLine("environment that you specified when you loaded the examples data:");
        Console.WriteLine();
        Console.WriteLine("\t-h <dbenv directory>");
        Console.WriteLine("For example:");
        Console.WriteLine("\tsimpleQuery.exe -h examplesEnvironment");

        Environment.Exit(-1);
    }

    private static string parseArguments(string[] args)
    {
        string envdir = null;
        for (int i = 0; i < args.Length; ++i) {
            string arg = args[i];
            if ((arg.StartsWith("-")
#if WIN32
                 || arg.StartsWith("/")
#endif
                ) && arg.Length > 1) {
                switch (arg[1]) {
                    case 'h':
                        {
                            ++i;
                            if (i >= args.Length) {
                                Console.WriteLine("Invalid option: " + arg);
                                Usage();
                            }
                            envdir = args[i];
                            break;
                        }
                    default:
                        {
                            Console.WriteLine("Unknown option: " + arg);
                            Usage();
                            break;
                        }
                }
            } else {
                Console.WriteLine("Too many arguments: " + arg);
                Usage();
            }
        }
        if (envdir == null) {
            Console.WriteLine("Environment directory not found.");
            Usage();
        }
        return envdir;
    }
}