Exemplo n.º 1
0
        //public abstract bool IsServerRunning();

        private void LoadDatabaseConfig()
        {
            DatabaseConfiguration dbConfiguration = ConfigManager.xmlConfiguration.Database;

            // Determine which database is using and create the database connection
            switch (dbConfiguration.Type)
            {
            case DatabaseEngine.Mysql:
                databaseDriver = new MySqlDatabaseDriver(string.Format("Server={0};Database={1};Uid={2};Pwd={3};Port={4};SslMode={5};SslCert={6};SslKey={7};SslCa={8}", dbConfiguration.Hostname, dbConfiguration.Databasename, dbConfiguration.Username, dbConfiguration.Password, dbConfiguration.Port, dbConfiguration.SslMode, dbConfiguration.SslCert, dbConfiguration.SslKey, dbConfiguration.SslCa));    // if using mysql we set this to null to make sure this value is null
                break;

            case DatabaseEngine.Sqlite:
                databaseDriver = new SqliteDatabaseDriver("Data Source=" + dbConfiguration.Databasename + ";Version=3;New=False");
                break;

            default:
                throw new Exception("Unknown database engine!");
            }
            try
            {
                databaseDriver?.Connect();
            }
            catch (Exception ex)
            {
                LogWriter.Log.Write(ex.Message, LogLevel.Fatal);
                throw ex;
            }
            LogWriter.Log.Write(LogLevel.Info, "Successfully connected to the {0}!", dbConfiguration.Type);
        }
        public void InsertTest1()
        {
            DatabaseDriver databaseDriver = new DatabaseDriver(
                Database.DataSource,
                Database.InitialCatalog,
                Database.UserId,
                Database.Pwd,
                Database.PersistSecurityInfo
                );

            databaseDriver.Connect();
            DateTime timeStamp = new DateTime(1970, 1, 1);  //得到1970年的时间戳
            long     now       = (DateTime.UtcNow.Ticks - timeStamp.Ticks) / 10000000;

            Console.WriteLine(now);
            List <Dictionary <string, object> > account = new List <Dictionary <string, object> >()
            {
                new Dictionary <string, object>
                {
                    { "account_name", "Coordinate35" },
                    { "passwd", "123456" },
                    { "register_time", now }
                },
                new Dictionary <string, object>
                {
                    { "account_name", "Coordinate" },
                    { "passwd", "123456" },
                    { "register_time", now }
                }
            };
            bool result = databaseDriver.Insert("account", account);

            Console.WriteLine(databaseDriver.LastError);
            Assert.IsTrue(result);
        }
        private void LoadDatabaseConfig()
        {
            DatabaseConfiguration databaseConfiguration = ConfigManager.xmlConfiguration.Database;

            // Determine which database is using and create the database connection
            switch (databaseConfiguration.Type)
            {
            case DatabaseEngine.Mysql:
                databaseDriver = null; // if using mysql we set this to null to make sure this value is null
                break;                 // We don't need to create the connection here because each server will automaticly create it's own MySQL connection.

            case DatabaseEngine.Sqlite:
                databaseDriver = new SqliteDatabaseDriver("Data Source=" + databaseConfiguration.Databasename + ";Version=3;New=False");
                break;

            default:
                throw new Exception("Unknown database engine!");
            }
            try
            {
                databaseDriver?.Connect();
            }
            catch (Exception ex)
            {
                LogWriter.Log.Write(ex.Message, LogLevel.Fatal);
                throw ex;
            }
            LogWriter.Log.Write(LogLevel.Info, "Successfully connected to the {0} database!", databaseConfiguration.Type);
        }
        public void UpdateTest()
        {
            DatabaseDriver databaseDriver = new DatabaseDriver(
                Database.DataSource,
                Database.InitialCatalog,
                Database.UserId,
                Database.Pwd,
                Database.PersistSecurityInfo
                );

            databaseDriver.Connect();
            Dictionary <string, object> account = new Dictionary <string, object>()
            {
                { "account_name", "Coordinate35" },
                { "passwd", "13579" }
            };

            databaseDriver.SetUpdate(Database.TableAccount);
            databaseDriver.SetSet(account);
            databaseDriver.SetAndWhere("account_id", 2);
            bool result = databaseDriver.Update();

            Console.WriteLine(databaseDriver.LastQuery);
            Assert.IsTrue(result);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Gets the country code for a string IP address
        /// </summary>
        /// <param name="IP"></param>
        /// <returns></returns>
        public static string GetCountryCode(IPAddress IP)
        {
            // Return default config Country Code
            if (IPAddress.IsLoopback(IP) || HttpServer.LocalIPs.Contains(IP))
            {
                return(Program.Config.ASP_LocalIpCountryCode);
            }

            try
            {
                using (DatabaseDriver Driver = new DatabaseDriver(DatabaseEngine.Sqlite, ConnectionString))
                {
                    // Fetch country code from Ip2Nation
                    Driver.Connect();
                    List <Dictionary <string, object> > Rows = Driver.Query(
                        "SELECT country FROM ip2nation WHERE ip < @P0 ORDER BY ip DESC LIMIT 1",
                        Networking.IP2Long(IP.ToString())
                        );
                    string CC = (Rows.Count == 0) ? "xx" : Rows[0]["country"].ToString();

                    // Fix country!
                    return((CC == "xx" || CC == "01") ? Program.Config.ASP_LocalIpCountryCode : CC);
                }
            }
            catch
            {
                return(Program.Config.ASP_LocalIpCountryCode);
            }
        }
        public void ConnectTest()
        {
            DatabaseDriver databaseDriver = new DatabaseDriver(
                Database.DataSource,
                Database.InitialCatalog,
                Database.UserId,
                Database.Pwd,
                Database.PersistSecurityInfo
                );

            Assert.IsTrue(databaseDriver.Connect());
        }
        public void GetTest()
        {
            DatabaseDriver databaseDriver = new DatabaseDriver(
                Database.DataSource,
                Database.InitialCatalog,
                Database.UserId,
                Database.Pwd,
                Database.PersistSecurityInfo
                );

            databaseDriver.Connect();
            databaseDriver.SetSelect("*");
            databaseDriver.SetFrom("account");
            databaseDriver.SetAndWhere("account_id", 1);
            Dictionary <int, Dictionary <string, Object> > result = databaseDriver.Get();

            Console.WriteLine(databaseDriver.LastQuery);
            Dictionary <int, Dictionary <string, Object> > testCase = new Dictionary <int, Dictionary <string, object> >()
            {
                {
                    0,

                    /*
                     * new Dictionary<string, object>() {
                     *  { "account_id", Convert.ChangeType("1", typeof(Object)) },
                     *  { "account_name", Convert.ChangeType("Coordinate35", typeof(Object)) },
                     *  { "passwd", Convert.ChangeType("123456", typeof(Object)) },
                     *  { "register_time", Convert.ChangeType("123456", typeof(Object)) },
                     *  { "available", Convert.ChangeType("True", typeof(Object)) }
                     * }
                     */
                    new Dictionary <string, object>()
                    {
                        { "account_id", (Int16)1 },
                        { "account_name", "Coordinate35" },
                        { "passwd", "1234dsfadfas5asdfa" },
                        { "register_time", 1482891426 },
                        { "privilege", 1 },
                        { "available", true }
                    }
                }
            };

            Assert.AreEqual(result.Count, testCase.Count);
            for (int i = 0; i < testCase.Count; i++)
            {
                Assert.AreEqual(result[i].Count, testCase[i].Count);
                foreach (string key in testCase[i].Keys)
                {
                    Assert.AreEqual(result[i][key], testCase[i][key]);
                }
            }
        }
        public void AddAccountTest()
        {
            DatabaseDriver databaseDriver = new DatabaseDriver(
                Database.DataSource,
                Database.InitialCatalog,
                Database.UserId,
                Database.Pwd,
                Database.PersistSecurityInfo
                );

            databaseDriver.Connect();
            AccountService accountService = new AccountService(ref databaseDriver);
            bool           result         = accountService.AddAccount("Coordinate35", "1234dsfadfas5asdfa", Database.AccountPrivilegeAdmin);

            Assert.IsTrue(result);
        }
        public void GetAccountByNameTest1()
        {
            DatabaseDriver databaseDriver = new DatabaseDriver(
                Database.DataSource,
                Database.InitialCatalog,
                Database.UserId,
                Database.Pwd,
                Database.PersistSecurityInfo
                );

            databaseDriver.Connect();
            AccountService accountService       = new AccountService(ref databaseDriver);
            Dictionary <string, object> account = accountService.GetAccountByName("matri");

            Assert.IsNull(account);
        }
Exemplo n.º 10
0
        public void GetAccountByNameTest1()
        {
            DatabaseDriver databaseDriver = new DatabaseDriver(
                Database.DataSource,
                Database.InitialCatalog,
                Database.UserId,
                Database.Pwd,
                Database.PersistSecurityInfo
                );

            databaseDriver.Connect();
            AccountModel accountModel = new AccountModel(ref databaseDriver);

            Dictionary <int, Dictionary <string, object> > account = accountModel.GetAccountByName("ABC");
            bool hasContent = account.Count > 0;

            Assert.IsFalse(hasContent);
        }
Exemplo n.º 11
0
        public void GetAccountByNameTest()
        {
            DatabaseDriver databaseDriver = new DatabaseDriver(
                Database.DataSource,
                Database.InitialCatalog,
                Database.UserId,
                Database.Pwd,
                Database.PersistSecurityInfo
                );

            databaseDriver.Connect();
            AccountModel accountModel = new AccountModel(ref databaseDriver);

            Dictionary <int, Dictionary <string, object> > account = accountModel.GetAccountByName("Coordinate35");

            Console.WriteLine(databaseDriver.LastQuery);
            bool hasContent = account.Count > 0;

            Assert.IsTrue(hasContent);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Fethces the full country name from a country code supplied from GetCountryCode()
        /// </summary>
        /// <param name="Code"></param>
        /// <returns></returns>
        public static string GetCountyNameFromCode(string Code)
        {
            try
            {
                using (DatabaseDriver Driver = new DatabaseDriver("Sqlite", ConnectionString))
                {
                    // Fetch country code from Ip2Nation
                    Driver.Connect();
                    List <Dictionary <string, object> > Rows = Driver.Query(
                        "SELECT country FROM ip2nationcountries WHERE iso_code_2 = @P0", Code.ToUpper()
                        );

                    return((Rows.Count == 0) ? Code: Rows[0]["country"].ToString());
                }
            }
            catch
            {
                return(Code);
            }
        }
Exemplo n.º 13
0
 /// <summary>
 /// Gets the country code for a string IP address
 /// </summary>
 /// <param name="IP"></param>
 /// <returns></returns>
 public static string GetCountryCode(IPAddress IP)
 {
     try
     {
         using (DatabaseDriver Driver = new DatabaseDriver("Sqlite", ConnectionString))
         {
             // Fetch country code from Ip2Nation
             Driver.Connect();
             List <Dictionary <string, object> > Rows = Driver.Query(
                 "SELECT country FROM ip2nation WHERE ip < @P0 ORDER BY ip DESC LIMIT 1",
                 IP2Long(IP.ToString())
                 );
             return((Rows.Count == 0) ? "??" : Rows[0]["country"].ToString());
         }
     }
     catch
     {
         return("??");
     }
 }
        public void LoginTest1()
        {
            DatabaseDriver databaseDriver = new DatabaseDriver(
                Database.DataSource,
                Database.InitialCatalog,
                Database.UserId,
                Database.Pwd,
                Database.PersistSecurityInfo
                );

            databaseDriver.Connect();
            AccountController           accountController = new AccountController(ref databaseDriver);
            Dictionary <string, string> account           = new Dictionary <string, string>()
            {
                { "accountName", "matrix67" },
                { "passwd", "12345" }
            };
            Dictionary <string, object> accountInfo = accountController.Login(account);

            Assert.IsNull(accountInfo);
        }
        public void AddAccountTest()
        {
            DatabaseDriver databaseDriver = new DatabaseDriver(
                Database.DataSource,
                Database.InitialCatalog,
                Database.UserId,
                Database.Pwd,
                Database.PersistSecurityInfo
                );

            databaseDriver.Connect();
            AccountController           accountController = new AccountController(ref databaseDriver);
            Dictionary <string, string> account           = new Dictionary <string, string>()
            {
                { "accountName", "matrix67" },
                { "passwd", "123456" }
            };
            int result = accountController.AddAccount(account);

            Console.WriteLine(databaseDriver.LastError);
            Assert.AreEqual(result, ControllerReturnCode.ACCOUNTADDACCOUNTDUPLICATE);
        }
Exemplo n.º 16
0
        /// <summary>
        /// Creates the server factory
        /// </summary>
        /// <param name="engine">The database engine</param>
        public void Create(DatabaseEngine engine, string connectionString)
        {
            // Determine which database is using and create the database connection
            switch (engine)
            {
            case DatabaseEngine.Mysql:
                databaseDriver = new MySqlDatabaseDriver(connectionString);
                break;

            case DatabaseEngine.Sqlite:
                databaseDriver = new SqliteDatabaseDriver(connectionString);
                break;

            default:
                throw new Exception("Unknown database engine!");
            }

            try
            {
                databaseDriver.Connect();
            }
            catch (Exception ex)
            {
                LogWriter.Log.Write(ex.Message, LogLevel.Fatal);
                Environment.Exit(0); // Without database the server cannot start
            }

            LogWriter.Log.Write("Successfully connected to the database!", LogLevel.Information);

            if (engine == DatabaseEngine.Sqlite)
            {
                CreateDatabaseTables();
            }

            // Add all servers
            servers.Add("GPSP", new GPSPServer(databaseDriver));
            servers.Add("GPCM", new GPCMServer(databaseDriver));
        }
Exemplo n.º 17
0
        public void AccountModelTest()
        {
            DatabaseDriver databaseDriver = new DatabaseDriver(
                Database.DataSource,
                Database.InitialCatalog,
                Database.UserId,
                Database.Pwd,
                Database.PersistSecurityInfo
                );

            databaseDriver.Connect();
            AccountModel accountModel           = new AccountModel(ref databaseDriver);
            Dictionary <string, object> account = new Dictionary <string, object>()
            {
                { "account_name", "Coordinate35" },
                { "passwd", "123456" },
                { "register_time", Helper.Helper.time() }
            };
            bool result = accountModel.InsertEntry(account);

            Console.WriteLine(databaseDriver.LastError);
            Assert.IsTrue(result);
        }
Exemplo n.º 18
0
        private bool InitDatabaseDriver(string databaseDriverName)
        {
            bool isSuccess = true;

            switch (databaseDriverName)
            {
            case "DatabaseDriver":
                databaseDriver = new DatabaseDriver(
                    Database.DataSource,
                    Database.InitialCatalog,
                    Database.UserId,
                    Database.Pwd,
                    Database.PersistSecurityInfo
                    );
                break;

            default:
                lastError = ErrorMessage.NoSuchDatabaseDriver;
                isSuccess = false;
                break;
            }
            if (!isSuccess)
            {
                return(false);
            }
            if (!databaseDriver.Connect())
            {
                lastError = ErrorMessage.ConnectDatabaseError;
                isSuccess = false;
            }
            if (!databaseDriver.Use(Database.DatabaseName))
            {
                lastError = ErrorMessage.UseDatabaseError;
                isSuccess = false;
            }
            return(isSuccess);
        }