示例#1
0
        public IdentityModelTests()
        {
            this.model = DataAccessModel.BuildDataAccessModel <IdentityModel>(SqliteConfiguration.Create(":memory:", null, BaseTests <IdentityModel> .useMonoData));
            this.model.Create(DatabaseCreationOptions.IfDatabaseNotExist);

            this.userStore = new ShaolinqIdentityUserStore <ShaolinqIdentityUser <Guid>, IdentityModel, Guid, DbUser, DbUserLogin, DbUserClaim, DbUserRole>(this.model);
        }
        public void Test_Distributed_Transaction_TransactionScope_CreateFlushComplete_Calls_DataModelHook(bool flush, bool complete)
        {
            var config2 = this.CreateSqliteClassicInMemoryConfiguration(null);
            var model2  = DataAccessModel.BuildDataAccessModel <ComplexPrimaryKeyDataAccessModel>(config2);
            var hook2   = new TestDataModelHook();

            model2.AddHook(hook2);
            model2.Create(DatabaseCreationOptions.IfDatabaseNotExist);

            using (var scope = new TransactionScope())
            {
                var cat   = this.model.Cats.Create();
                var coord = model2.Coordinates.Create(1);

                Console.WriteLine("===");

                if (flush)
                {
                    scope.Flush();
                }
                if (complete)
                {
                    scope.Complete();
                }
            }

            Assert.AreEqual(complete ? 1 : 0, this.testDataModelHook.CommitCount);
            Assert.AreEqual(complete || !flush ? 0 : 1, this.testDataModelHook.RollbackCount);
            Assert.AreEqual(complete ? 1 : 0, hook2.CommitCount);
            Assert.AreEqual(complete || !flush ? 0 : 1, hook2.RollbackCount);
        }
示例#3
0
        public BaseTests(string providerName)
        {
            this.ProviderName = providerName;

            XmlConfigurator.Configure();

            try
            {
                if (providerName == "default")
                {
                    model = DataAccessModel.BuildDataAccessModel <T>();
                }
                else
                {
                    configuration = this.Create(providerName, this.GetType().Name);
                    model         = DataAccessModel.BuildDataAccessModel <T>(configuration);
                }

                model.Create(DatabaseCreationOptions.DeleteExisting);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception while configuring provider: " + providerName);
                Console.WriteLine(e);
                Console.WriteLine(e.StackTrace);

                throw;
            }
        }
示例#4
0
        public TrinityMysqlDatabaseProvider()
        {
            string Database = TrinityMySqlDatabaseModule.DbAccess.DB;
            string User     = TrinityMySqlDatabaseModule.DbAccess.User;
            string Password = TrinityMySqlDatabaseModule.DbAccess.Password;
            string Host     = TrinityMySqlDatabaseModule.DbAccess.Host;

            try
            {
                var config = MySqlConfiguration.Create(Database, Host, User, Password);
                model = DataAccessModel.BuildDataAccessModel <TrinityDatabase>(config);
                try
                {
                    model.Create(DatabaseCreationOptions.IfDatabaseNotExist);
                }
                catch (Exception)
                {
                    // already exists, its ok
                }
                var temp = GetCreatureTemplates();
            }
            catch (Exception e)
            {
                if (!string.IsNullOrEmpty(Host))
                {
                    MessageBox.Show($"Cannot connect to MySql database: {e.Message} Check your settings.");
                }
                model = null;
            }
        }
示例#5
0
 public MysqlDatabaseProvider()
 {
     try
     {
         string Database = MySqlDatabaseModule.DbAccess.DB;
         string User     = MySqlDatabaseModule.DbAccess.User;
         string Password = MySqlDatabaseModule.DbAccess.Password;
         string Host     = MySqlDatabaseModule.DbAccess.Host;
         var    config   = MySqlConfiguration.Create(Database, Host, User, Password);
         model = DataAccessModel.BuildDataAccessModel <TrinityDatabase>(config);
         try
         {
             model.Create(DatabaseCreationOptions.IfDatabaseNotExist);
         }
         catch (Exception e)
         {
             // already exists, its ok
         }
         var temp = GetCreatureTemplates();
     }
     catch (Exception e)
     {
         Console.WriteLine("Cannot connect to MySql database. Check your settings.");
         model = null;
     }
 }
示例#6
0
        static AuthDatabase()
        {
            var config = Config.Instance.AuthDatabase;

            DataAccessModelConfiguration dbConfig;

            switch (Config.Instance.AuthDatabase.Engine)
            {
            case DatabaseEngine.MySQL:
                dbConfig = MySqlConfiguration.Create(config.Database, config.Host, config.Username, config.Password, true);
                break;

            case DatabaseEngine.SQLite:
                dbConfig = SqliteConfiguration.Create(config.Filename, null);
                break;

            default:
                Logger.Error()
                .Message("Invalid database engine {0}", Config.Instance.AuthDatabase.Engine)
                .Write();
                Environment.Exit(0);
                return;
            }

            Instance = DataAccessModel.BuildDataAccessModel <Database.Auth.AuthDatabase>(dbConfig);
        }
        private static void RegisterDataModelSingleton <TInterface, TImpl>(this IdentityServerServiceFactory factory, ShaolinqServiceOptions options)
            where TImpl : DataAccessModel, TInterface
            where TInterface : class
        {
            options = options ?? new ShaolinqServiceOptions();

            var configSection = options.DataAccessModelConfigSection;

            if (string.IsNullOrEmpty(configSection))
            {
                configSection = typeof(TImpl).Name;
            }

            var dictionaryKey = GetDataAccessModelDictionaryKey(typeof(TImpl), configSection);

            lock (LockObj)
            {
                DataAccessModel dataModel;

                if (!DataAccessModels.TryGetValue(dictionaryKey, out dataModel))
                {
                    dataModel = DataAccessModel.BuildDataAccessModel <TImpl>();
                    //dataModel.Create(DatabaseCreationOptions.DeleteExistingDatabase);
                    DataAccessModels.Add(dictionaryKey, dataModel);
                    factory.Register(new Registration <TImpl>((TImpl)dataModel));
                }

                factory.Register(new Registration <TInterface>((TImpl)dataModel));
            }
        }
示例#8
0
        public void CreatDataBase()
        {
            var configuration = Shaolinq.Sqlite.SqliteConfiguration.Create(":memory:");

            model = DataAccessModel.BuildDataAccessModel <ExampleModel>(configuration);
            //如果不存在则创建
            model.Create(DatabaseCreationOptions.IfDatabaseNotExist);
        }
示例#9
0
        public void Test_Backup()
        {
            using (var scope = NewTransactionScope())
            {
                var school = this.model.Schools.Create();

                school.Name = "School1";

                scope.Complete();
            }

            var backupFileModel = DataAccessModel.BuildDataAccessModel <TestDataAccessModel>(CreateSqliteConfiguration("backup.sql3"));

            this.model.Backup(backupFileModel);

            var backupModel = DataAccessModel.BuildDataAccessModel <TestDataAccessModel>(CreateSqliteClassicInMemoryConfiguration(null));

            backupFileModel.Backup(backupModel);

            Assert.AreEqual("School1", this.model.Schools.Select(c => c.Name).Single());

            using (var scope = NewTransactionScope())
            {
                var school = this.model.Schools.Create();

                school.Name = "School2";

                scope.Complete();
            }

            Assert.AreEqual("School2", this.model.Schools.Select(c => c.Name).Single(c => c == "School2"));

            using (var scope = NewTransactionScope())
            {
                var school = backupModel.Schools.Create();

                school.Name = "School3";

                school = backupModel.Schools.Create();

                school.Name = "School4";

                scope.Complete();
            }

            Assert.IsNull(this.model.Schools.Select(c => c.Name).FirstOrDefault(c => c == "School3"));
            Assert.IsNull(this.model.Schools.Select(c => c.Name).FirstOrDefault(c => c == "School4"));

            Assert.AreEqual("School3", backupModel.Schools.Select(c => c.Name).Single(c => c == "School3"));
            Assert.AreEqual("School4", backupModel.Schools.Select(c => c.Name).Single(c => c == "School4"));

            Assert.AreEqual(2, this.model.Schools.Count());
            Assert.AreEqual(3, backupModel.Schools.Count());
        }
示例#10
0
        public DatabaseModel()
        {
            //var configuration = SqliteConfiguration.Create("database.sqlite", null);
            var configuration = MySqlConfiguration.Create("wow", "127.0.0.1", "homestead", "secret");

            try
            {
                Model = DataAccessModel.BuildDataAccessModel <T>(configuration);
            }
            catch (Exception e)
            {
                var trace = new StackTrace(e, true);
                Log.Print(LogType.Error, $"{e.Message}: {e.Source}\n{trace.GetFrame(trace.FrameCount - 1).GetFileName()}:{trace.GetFrame(trace.FrameCount - 1).GetFileLineNumber()}");
            }
        }
示例#11
0
        public BaseTests(string providerName, bool alwaysSubmitDefaultValues = false, bool valueTypesAutoImplicitDefault = true, bool includeImplicitDefaultsInSchema = true)
        {
            this.valueTypesAutoImplicitDefault   = valueTypesAutoImplicitDefault;
            this.includeImplicitDefaultsInSchema = includeImplicitDefaultsInSchema;
            this.alwaysSubmitDefaultValues       = alwaysSubmitDefaultValues;

            var ss = providerName.Split(':');

            if (ss.Length <= 1)
            {
                this.ProviderName = providerName;
            }
            else
            {
                this.ProviderName       = ss[0];
                this.useDataAccessScope = ss[1] == "DataAccessScope";
            }

            XmlConfigurator.Configure();

            try
            {
                if (providerName == "default")
                {
                    this.model = DataAccessModel.BuildDataAccessModel <T>();
                }
                else
                {
                    var configuration = Create(this.ProviderName, GetType().Name);
                    configuration.AlwaysSubmitDefaultValues       = alwaysSubmitDefaultValues;
                    configuration.ValueTypesAutoImplicitDefault   = valueTypesAutoImplicitDefault;
                    configuration.IncludeImplicitDefaultsInSchema = includeImplicitDefaultsInSchema;
                    this.model = DataAccessModel.BuildDataAccessModel <T>(configuration);
                }

                this.model.Create(DatabaseCreationOptions.DeleteExistingDatabase);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception while configuring provider: " + providerName);
                Console.WriteLine(e);
                Console.WriteLine(e.StackTrace);

                throw;
            }
        }
示例#12
0
        public ShaolinqErrorLog(IDictionary config)
        {
            var dataAccessModelTypeName = config.Find("dataAccessModelType", string.Empty);

            if (string.IsNullOrEmpty(dataAccessModelTypeName))
            {
                throw new ApplicationException("DataAccessModelType not specified");
            }

            var modelType = Type.GetType(dataAccessModelTypeName);

            if (modelType == null)
            {
                throw new ApplicationException($"Could not find type {dataAccessModelTypeName}");
            }

            if (!modelType.GetInterfaces().Contains(typeof(IElmahDataAccessModel)))
            {
                throw new ApplicationException("DataAccessModelType must implement IElmahDataAccessModel");
            }

            var dataAccessModelConfigSection = config.Find("dataAccessModelConfigSection", modelType.Name);

            var dataAccessModelConfiguration = ConfigurationBlock <DataAccessModelConfiguration> .Load(dataAccessModelConfigSection);

            this.dataModel = (IElmahDataAccessModel)DataAccessModel.BuildDataAccessModel(
                Type.GetType(dataAccessModelTypeName),
                dataAccessModelConfiguration);

            // Set the application name as this implementation provides per-application isolation over a single store.
            // Use application name of "*" to disable per-application isolation.
            var appName = config.Find("applicationName", string.Empty);

            if (appName.Length > MaxAppNameLength)
            {
                throw new ApplicationException($"Application name is too long. Maximum length allowed is {MaxAppNameLength.ToString("N0")} characters.");
            }

            ApplicationName = appName;
        }
示例#13
0
        public BaseTests(string providerName)
        {
            var ss = providerName.Split(':');

            if (ss.Length <= 1)
            {
                this.ProviderName = providerName;
            }
            else
            {
                this.ProviderName       = ss[0];
                this.useDataAccessScope = ss[1] == "DataAccessScope";
            }

            XmlConfigurator.Configure();

            try
            {
                if (providerName == "default")
                {
                    this.model = DataAccessModel.BuildDataAccessModel <T>();
                }
                else
                {
                    var configuration = this.Create(this.ProviderName, this.GetType().Name);
                    this.model = DataAccessModel.BuildDataAccessModel <T>(configuration);
                }

                this.model.Create(DatabaseCreationOptions.DeleteExistingDatabase);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception while configuring provider: " + providerName);
                Console.WriteLine(e);
                Console.WriteLine(e.StackTrace);

                throw;
            }
        }
示例#14
0
        public void StressTest()
        {
            var config = PostgresConfiguration.Create("StressTest", "localhost", "postgres", "postgres");

            (config.SqlDatabaseContextInfos[0] as PostgresSqlDatabaseContextInfo).MaxPoolSize = 2;
            (config.SqlDatabaseContextInfos[0] as PostgresSqlDatabaseContextInfo).KeepAlive   = 0;

            var dataModel = DataAccessModel.BuildDataAccessModel <TestDataAccessModel>(config);

            dataModel.Create(DatabaseCreationOptions.DeleteExistingDatabase);

            Console.WriteLine(dataModel.GetCurrentSqlDatabaseContext().ConnectionString);

            using (var scope = new DataAccessScope())
            {
                var school  = dataModel.Schools.Create();
                var student = dataModel.Students.Create();

                student.School    = school;
                student.Firstname = "Tum";
                student.Lastname  = "Nguyen";

                for (var i = 0; i < 10000; i++)
                {
                    var s2 = dataModel.Students.Create();

                    s2.School    = school;
                    s2.Firstname = "Student " + i;
                }

                scope.Complete();
            }

            const int numThreads = 10;

            var cancellationTokenSource = new CancellationTokenSource();

            var resetEvents = new List <WaitHandle>();

            for (var i = 0; i < numThreads; i++)
            {
                var resetEvent = new ManualResetEvent(false);
                resetEvents.Add(resetEvent);

                var dispatchThread = new Thread(_ =>
                {
                    while (!cancellationTokenSource.Token.IsCancellationRequested)
                    {
                        try
                        {
                            dataModel.Students.ToList();
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Test error: {0}", ex);
                        }
                    }

                    resetEvent.Set();
                })
                {
                    Name = $"Thread: {i + 1}"
                };

                dispatchThread.Start();
            }

            Thread.Sleep(10000);

            cancellationTokenSource.Cancel();

            WaitHandle.WaitAll(resetEvents.ToArray());
        }
示例#15
0
        public void TestCreateDataAccessModel()
        {
            var dataModel = DataAccessModel.BuildDataAccessModel <IdentityServerDataAccessModel>(SqliteConfiguration.Create(":memory:", null));

            dataModel.Create(DatabaseCreationOptions.DeleteExistingDatabase);
        }
示例#16
0
 public void Connect()
 {
     var configuration = PostgresConfiguration.Create(ConnectionString);
     var model         = DataAccessModel.BuildDataAccessModel <MldbModel>(configuration);
 }