using System;
using System.Collections.Generic;
using System.Text;
using OSGeo.FDO;
using OSGeo.FDO.ClientServices;
using OSGeo.FDO.Connections;
using OSGeo.FDO.Commands.Schema;
using OSGeo.FDO.Schema;
using OSGeo.FDO.Commands;
using System.Diagnostics;

namespace EnhancedDescribeSchemaTest
{
    class Program
    {
        //Change as appropriate
        static readonly string dataSource = "Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\\fdo-trunk\\Providers\\GenericRdbms\\Src\\UnitTest\\MSTest.mdb";
        static readonly string schemaName = "Fdo";
        static readonly string className = "Cities";
        static readonly string providerName = "OSGeo.ODBC";

        static void Main(string[] args)
        {
            DoTest();
            Console.WriteLine("DONE!");
            Console.Read();
        }

        private static void DoTest()
        {
            IConnectionManager connMgr = FeatureAccessManager.GetConnectionManager();
            using (IConnection conn = connMgr.CreateConnection(providerName))
            {
                conn.ConnectionInfo.ConnectionProperties.SetProperty("ConnectionString", "\"" + dataSource + "\"");

                int[] commands = conn.CommandCapabilities.Commands;
                bool supported = Array.IndexOf<int>(commands, (int)CommandType.CommandType_GetClassNames) >= 0
                              && Array.IndexOf<int>(commands, (int)CommandType.CommandType_GetSchemaNames) >= 0;

                Debug.Assert(supported);

                if (conn.Open() == ConnectionState.ConnectionState_Open)
                {
                    using (IDescribeSchema describe = conn.CreateCommand(OSGeo.FDO.Commands.CommandType.CommandType_DescribeSchema) as IDescribeSchema)
                    {
                        describe.SchemaName = schemaName;
                        OSGeo.FDO.Common.StringElement el = new OSGeo.FDO.Common.StringElement(className);
                        describe.ClassNames.Add(el); // <----- FAIL. StringElement garbles the .net string that is assigned to it. You can check by inspecting el.String when debugging

                        FeatureSchemaCollection schemas = describe.Execute();
                        Debug.Assert(schemas != null);

                        conn.Close();
                    }
                }

                conn.Dispose();
            }
        }
    }
}

