Beispiel #1
0
        public void ResultMap_configuration_via_code_should_override_file_configuration()
        {
            string    uri      = "assembly://Apache.Ibatis.DataMapper.SqlClient.Test/bin.Debug/SqlMap_Test_Configure.config";
            IResource resource = ResourceLoaderRegistry.GetResource(uri);

            // Before have 4 properties
            IConfigurationEngine engine = new DefaultConfigurationEngine();

            engine.RegisterInterpreter(new XmlConfigurationInterpreter(resource));

            engine.BuildMapperFactory();
            IConfiguration resultMap = engine.ConfigurationStore.GetResultMapConfiguration("Account.Category-result");

            Assert.That(resultMap.Children.Count, Is.EqualTo(3));

            // With code configuration override have only 1 properties
            engine = new DefaultConfigurationEngine();
            engine.RegisterInterpreter(new XmlConfigurationInterpreter(resource));
            engine.RegisterModule(new AccountModule());

            engine.BuildMapperFactory();

            resultMap = engine.ConfigurationStore.GetResultMapConfiguration("Account.Category-result");
            Assert.That(resultMap.Children.Count, Is.EqualTo(1));
        }
        protected virtual void SetUpFixture()
        {
            string resource        = "SqlMap_StatementOnly.config";
            string scriptDirectory = Path.Combine(Path.Combine(Path.Combine(Resources.ApplicationBase, ".."), ".."), "Scripts") + Path.DirectorySeparatorChar;

            try
            {
                IConfigurationEngine engine = new DefaultConfigurationEngine();
                engine.RegisterInterpreter(new XmlConfigurationInterpreter(resource));
                engine.RegisterModule(new AccountModule());
                engine.RegisterModule(new DocumentModule());

                IMapperFactory mapperFactory = engine.BuildMapperFactory();

                Console.WriteLine(engine.ConfigurationStore.ToString());

                dataMapper = ((IDataMapperAccessor)mapperFactory).DataMapper;
                ISessionFactory sessionFactory = engine.ModelStore.SessionFactory;
                BaseTest.InitScript(sessionFactory.DataSource, scriptDirectory + "account-init.sql");
            }
            catch (Exception ex)
            {
                Exception e = ex;
                while (e != null)
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine(e.StackTrace);
                    e = e.InnerException;
                }
                throw;
            }
        }
Beispiel #3
0
        public void Test_DefaultConfigurationEngine_via_Assembly_Resource()
        {
            string    uri      = "assembly://MyBatis.DataMapper.SqlClient.Test/bin.Debug/SqlMap_Test_Configure.config";
            IResource resource = ResourceLoaderRegistry.GetResource(uri);

            IConfigurationEngine engine = new DefaultConfigurationEngine();

            engine.RegisterInterpreter(new XmlConfigurationInterpreter(resource));

            engine.BuildMapperFactory();

            CheckResource(engine.ConfigurationStore);
        }
Beispiel #4
0
        public void Test_DefaultConfigurationEngine_via_File_Resource()
        {
            string    uri      = "file://~/SqlMap_Test_Configure.config";
            IResource resource = ResourceLoaderRegistry.GetResource(uri);

            IConfigurationEngine engine = new DefaultConfigurationEngine();

            engine.RegisterInterpreter(new XmlConfigurationInterpreter(resource));

            engine.BuildMapperFactory();

            CheckResource(engine.ConfigurationStore);
        }
Beispiel #5
0
        private void Init()
        {
            ConfigurationSetting configurationSetting = new ConfigurationSetting();
            string resource             = "sqlMapConfig.xml";
            IConfigurationEngine engine = new DefaultConfigurationEngine(configurationSetting);

            engine.RegisterInterpreter(new XmlConfigurationInterpreter(resource));

            IMapperFactory mapperFactory = engine.BuildMapperFactory();

            sessionFactory = engine.ModelStore.SessionFactory;
            dataMapper     = ((IDataMapperAccessor)mapperFactory).DataMapper;
            //dataMapper设置分叶
        }
Beispiel #6
0
        protected virtual void SetUpFixture()
        {
            //DateTime start = DateTime.Now;

            configurationSetting = new ConfigurationSetting();
            configurationSetting.Properties.Add("collection2Namespace", "Apache.Ibatis.DataMapper.SqlClient.Test.Domain.LineItemCollection2, Apache.Ibatis.DataMapper.SqlClient.Test");
            configurationSetting.Properties.Add("nullableInt", "int?");

            string resource = "sqlmap.config";

            try
            {
                IConfigurationEngine engine = new DefaultConfigurationEngine(configurationSetting);
                engine.RegisterInterpreter(new XmlConfigurationInterpreter(resource));
                engine.RegisterModule(new AliasModule());

                IMapperFactory mapperFactory = engine.BuildMapperFactory();
                sessionFactory = engine.ModelStore.SessionFactory;
                dataMapper     = ((IDataMapperAccessor)mapperFactory).DataMapper;
                sessionStore   = ((IModelStoreAccessor)dataMapper).ModelStore.SessionStore;
            }
            catch (Exception ex)
            {
                Exception e = ex;
                while (e != null)
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine(e.StackTrace);
                    e = e.InnerException;
                }
                throw;
            }

            if (sessionFactory.DataSource.DbProvider.Id.IndexOf("PostgreSql") >= 0)
            {
                ConvertKey = new KeyConvert(Lower);
            }
            else if (sessionFactory.DataSource.DbProvider.Id.IndexOf("oracle") >= 0)
            {
                ConvertKey = new KeyConvert(Upper);
            }
            else
            {
                ConvertKey = new KeyConvert(Normal);
            }

            //			string loadTime = DateTime.Now.Subtract(start).ToString();
            //			Console.WriteLine("Loading configuration time :"+loadTime);
        }
Beispiel #7
0
        public void SetUpFixture()
        {
            string    uri      = "file://~/SqlMap.event.config";
            IResource resource = ResourceLoaderRegistry.GetResource(uri);

            ConfigurationSetting setting = new ConfigurationSetting();
            IConfigurationEngine engine  = new DefaultConfigurationEngine(setting);

            engine.RegisterInterpreter(new XmlConfigurationInterpreter(resource));
            engine.RegisterModule(new EventModule());

            IMapperFactory mapperFactory = engine.BuildMapperFactory();

            sessionFactory = engine.ModelStore.SessionFactory;
            dataMapper     = ((IDataMapperAccessor)mapperFactory).DataMapper;
        }
Beispiel #8
0
        public void Should_contains_ResultMap_DocumentBook_with_5_properties()
        {
            string    uri      = "assembly://Apache.Ibatis.DataMapper.SqlClient.Test/bin.Debug/SqlMap_Test_Configure.config";
            IResource resource = ResourceLoaderRegistry.GetResource(uri);

            IConfigurationEngine engine = new DefaultConfigurationEngine();

            engine.RegisterInterpreter(new XmlConfigurationInterpreter(resource));

            engine.BuildMapperFactory();

            IConfiguration resultMap = engine.ConfigurationStore.GetResultMapConfiguration("Document.book");

            Assert.That(resultMap.Children.Count, Is.EqualTo(4));

            //Console.WriteLine(engine.ModelStore.ToString());
        }
Beispiel #9
0
        public void Should_contains_statement_GetAllAccountsOrderByName_with_2_child()
        {
            string    uri      = "assembly://Apache.Ibatis.DataMapper.SqlClient.Test/bin.Debug/SqlMap_Test_Configure.config";
            IResource resource = ResourceLoaderRegistry.GetResource(uri);

            IConfigurationEngine engine = new DefaultConfigurationEngine();

            engine.RegisterInterpreter(new XmlConfigurationInterpreter(resource));

            engine.BuildMapperFactory();

            IConfiguration statement = engine.ConfigurationStore.GetStatementConfiguration("GetAllAccountsOrderByName");

            Assert.That(statement.Children.Count, Is.EqualTo(2));

            //Console.WriteLine(engine.ConfigurationStore);
        }
Beispiel #10
0
        public void Event_FileReourceLoad_should_be_launch()
        {
            string resource = "SqlMap_Test_Configure.config";

            IConfigurationEngine engine = new DefaultConfigurationEngine();

            engine.FileResourceLoad += FileResourceEventHandler;

            engine.RegisterInterpreter(new XmlConfigurationInterpreter(resource));
            engine.BuildMapperFactory();

            Assert.That(fileNames.Count, Is.EqualTo(6));
            Assert.Contains(resource, fileNames);
            Assert.Contains("database.config", fileNames);
            Assert.Contains("providers.config", fileNames);
            Assert.Contains("Mapping1.xml", fileNames);
            Assert.Contains("Mapping3.xml", fileNames);
            Assert.Contains("Mapping4.xml", fileNames);
        }
Beispiel #11
0
        private static IDataMapper InitMybatis(string path, ref ISessionFactory factory)
        {
            string config = databaseSelector + path;//MyBatis的Config文件
            IConfigurationEngine engine = new DefaultConfigurationEngine();

            try
            {
                engine.RegisterInterpreter(new XmlConfigurationInterpreter(config));
            }
            catch (Exception e)
            {
                string error = e.Message;
            }
            IMapperFactory mapperFactory = engine.BuildMapperFactory();

            factory = engine.ModelStore.SessionFactory;
            IDataMapper dataMapper = ((IDataMapperAccessor)mapperFactory).DataMapper;

            return(dataMapper);
        }
Beispiel #12
0
        protected void InitDataMapper()
        {
            Assembly     assembly     = this.GetType().Assembly;
            AssemblyName assemblyName = assembly.GetName();
            Stream       stream       = assembly.GetManifestResourceStream(assemblyName.Name + ".Configs.SqlMap.config");
            XmlDocument  document     = new XmlDocument();

            document.Load(stream);
            stream.Close();
            stream.Dispose();

            string[] manifestResourceNames = assembly.GetManifestResourceNames();

            System.Text.StringBuilder builder = new System.Text.StringBuilder();
            foreach (string manifestResourceName in manifestResourceNames)
            {
                if (manifestResourceName.ToLower().EndsWith(ext))
                {
                    Match m = regex.Match(manifestResourceName);

                    string namespaceName = m.Result("${namespaceName}");
                    string resourceName  = m.Result("${resourceName}");
                    builder.Append(String.Format("<sqlMap uri=\"assembly://CySoft.DAL/{0}/{1}\" />", namespaceName, resourceName));
                }
            }

            document.GetElementsByTagName("sqlMaps")[0].InnerXml = builder.ToString();

            ConfigurationSetting configurationSetting = new ConfigurationSetting();
            IConfigurationEngine engine   = new DefaultConfigurationEngine(configurationSetting);
            IResource            resource = new StaticContentResource(document.InnerXml);

            engine.RegisterInterpreter(new XmlConfigurationInterpreter(resource));

            IMapperFactory mapperFactory = engine.BuildMapperFactory();

            sessionFactory = engine.ModelStore.SessionFactory;
            dataMapper     = ((IDataMapperAccessor)mapperFactory).DataMapper;
        }
Beispiel #13
0
        public static DataSourceConfig GetInstance()
        {
            if (_dataSourceConfig == null)
            {
                lock (LockObj)
                {
                    if (_dataSourceConfig == null)
                    {
                        _dataSourceConfig = new DataSourceConfig();

                        #region 初始化其他变量
                        IConfigurationEngine engine = new DefaultConfigurationEngine();
                        engine.RegisterInterpreter(new XmlConfigurationInterpreter("SqlMap.config"));
                        IMapperFactory mapperFactory = engine.BuildMapperFactory();
                        _dataSourceConfig._sessionFactory = engine.ModelStore.SessionFactory;
                        _dataSourceConfig._dataMapper     = ((IDataMapperAccessor)mapperFactory).DataMapper;
                        #endregion
                    }
                }
            }

            return(_dataSourceConfig);
        }
Beispiel #14
0
        public void Can_plug_HybridWebThreadSessionStore()
        {
            string    uri      = "assembly://Apache.Ibatis.DataMapper.SqlClient.Test/bin.Debug/SqlMap_Test_Configure.config";
            IResource resource = ResourceLoaderRegistry.GetResource(uri);

            ConfigurationSetting setting = new ConfigurationSetting();

            setting.SessionStore             = new HybridWebThreadSessionStore("test");
            setting.ValidateMapperConfigFile = true;

            IConfigurationEngine engine = new DefaultConfigurationEngine(setting);

            engine.RegisterInterpreter(new XmlConfigurationInterpreter(resource));

            IMapperFactory mapperFactory = engine.BuildMapperFactory();
            IDataMapper    dataMapper    = ((IDataMapperAccessor)mapperFactory).DataMapper;

            Account account = dataMapper.QueryForObject <Account>("GetAccountViaResultClass", 1);

            Assert.AreEqual(1, account.Id, "account.Id");
            Assert.AreEqual("Joe", account.FirstName, "account.FirstName");
            Assert.AreEqual("Dalton", account.LastName, "account.LastName");
            Assert.AreEqual("*****@*****.**", account.EmailAddress, "account.EmailAddress");
        }
Beispiel #15
0
        public void Should_contains_ParameterMap_OrderInsertExtend_with_10_properties()
        {
            string    uri      = "assembly://Apache.Ibatis.DataMapper.SqlClient.Test/bin.Debug/SqlMap_Test_Configure.config";
            IResource resource = ResourceLoaderRegistry.GetResource(uri);

            IConfigurationEngine engine = new DefaultConfigurationEngine();

            engine.RegisterInterpreter(new XmlConfigurationInterpreter(resource));

            engine.BuildMapperFactory();

            IConfiguration parameterMap = engine.ConfigurationStore.GetParameterMapConfiguration("Order.insert-extend");

            Assert.That(parameterMap.Children.Count, Is.EqualTo(10));

            int index = parameterMap.Children.FindIndex(
                delegate(IConfiguration paramater)
                { return(paramater.Id == "CardNumber"); }
                );

            Assert.That(index, Is.EqualTo(5));

            //Console.WriteLine(engine.ConfigurationStore);
        }
        public void CodeConfigurationMatchesSqlMapConfig()
        {
            #region SqlMapConfig.xml

            /*
             * <sqlMapConfig xmlns="http://ibatis.apache.org/dataMapper"
             * xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
             *      <settings>
             *              <setting useStatementNamespaces="true"/>
             *              <setting cacheModelsEnabled="false"/>
             *              <setting validateSqlMap="false"/>
             *              <setting useReflectionOptimizer="false"/>
             *              <setting preserveWhitespace="false"/>
             *      </settings>
             *      <providers uri="file://providers.config"/>
             *      <database>
             *              <provider name="SQLite3"/>
             *              <dataSource name="ibatisnet.sqlmap" connectionString="Data Source=ibatisnet.sqlite;Version=3;"/>
             *      </database>
             *      <alias>
             *              <typeAlias alias="Account" type="Apache.Ibatis.DataMapper.Sqlite.Test.Domain.Account, Apache.Ibatis.DataMapper.Sqlite.Test"/>
             *      </alias>
             * <sqlMaps>
             *              <sqlMap uri="file://../../Maps/Account.xml"/>
             * </sqlMaps>
             * </sqlMapConfig>
             */
            #endregion

            // slightly awkward to creating ConfigurationSetting, then engine, then interpreter ???

            ConfigurationSetting settings = new ConfigurationSetting();
            settings.UseStatementNamespaces   = true;
            settings.IsCacheModelsEnabled     = false;
            settings.ValidateMapperConfigFile = false;
            settings.UseReflectionOptimizer   = false;
            settings.PreserveWhitespace       = false;

            var engine = new DefaultConfigurationEngine(settings);

            CodeConfigurationInterpreter codeConfig = new CodeConfigurationInterpreter(engine.ConfigurationStore);
            codeConfig.AddDatabase(new SqliteDbProvider(), "Data Source=ibatisnet.sqlite;Version=3;");
            codeConfig.AddAlias(typeof(Account), "Account");
            codeConfig.AddSqlMap("file://../../Maps/Account.xml", true);

            engine.RegisterInterpreter(codeConfig);
            IMapperFactory mapperFactory   = engine.BuildMapperFactory();
            IDataMapper    localDataMapper = ((IDataMapperAccessor)mapperFactory).DataMapper;

            IConfigurationStore store     = engine.ConfigurationStore;
            IConfigurationStore baseStore = ConfigurationEngine.ConfigurationStore;

            assertConfiguration(baseStore.Properties, store.Properties);
            // assertConfiguration(baseStore.Settings, store.Settings);
            assertConfiguration(baseStore.Databases, store.Databases);
            assertConfiguration(baseStore.TypeHandlers, store.TypeHandlers);
            assertConfiguration(baseStore.Alias, store.Alias);
            assertConfiguration(baseStore.CacheModels, store.CacheModels);
            assertConfiguration(baseStore.ResultMaps, store.ResultMaps);
            assertConfiguration(baseStore.Statements, store.Statements);
            assertConfiguration(baseStore.ParameterMaps, store.ParameterMaps);

            InitScript(SessionFactory.DataSource, "../../Scripts/account-init.sql");

            ICollection items = localDataMapper.QueryForList("Account.GetAllAccounts1", null);
            Assert.IsTrue(items.Count > 1);

            items = localDataMapper.QueryForList("Account.GetAllAccounts2", null);
            Assert.IsTrue(items.Count > 1);
        }