Example #1
0
        /// <summary>
        /// Create an instance of the database client.
        /// </summary>
        /// <param name="dbType">The type of database.</param>
        /// <param name="serverIp">The IP address or hostname of the database server.</param>
        /// <param name="serverPort">The TCP port of the database server.</param>
        /// <param name="username">The username to use when authenticating with the database server.</param>
        /// <param name="password">The password to use when authenticating with the database server.</param>
        /// <param name="instance">The instance on the database server (for use with Microsoft SQL Server).</param>
        /// <param name="database">The name of the database with which to connect.</param>
        public DatabaseClient(
            DbTypes dbType,
            string serverIp,
            int serverPort,
            string username,
            string password,
            string instance,
            string database)
        {
            if (String.IsNullOrEmpty(serverIp))
            {
                throw new ArgumentNullException(nameof(serverIp));
            }
            if (serverPort < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(serverPort));
            }
            if (String.IsNullOrEmpty(database))
            {
                throw new ArgumentNullException(nameof(database));
            }

            _DbType       = dbType;
            _ServerIp     = serverIp;
            _ServerPort   = serverPort;
            _Username     = username;
            _Password     = password;
            _Instance     = instance;
            _DatabaseName = database;
            _Header       = "[DatabaseWrapper." + _DbType.ToString() + "] ";

            switch (_DbType)
            {
            case DbTypes.Sqlite:
                throw new ArgumentException("Unable to use this constructor with 'DbTypes.Sqlite'.");

            case DbTypes.Mysql:
                _Mysql = new Mysql.DatabaseClient(_ServerIp, _ServerPort, _Username, _Password, _DatabaseName);
                break;

            case DbTypes.Postgresql:
                _Postgresql = new Postgresql.DatabaseClient(_ServerIp, _ServerPort, _Username, _Password, _DatabaseName);
                break;

            case DbTypes.SqlServer:
                _SqlServer = new SqlServer.DatabaseClient(_ServerIp, _ServerPort, _Username, _Password, _Instance, _DatabaseName);
                break;

            default:
                throw new ArgumentException("Unknown database type '" + _DbType.ToString() + "'.");
            }
        }
        /// <summary>
        /// Create an instance of the database client.
        /// </summary>
        /// <param name="dbType">The type of database.</param>
        /// <param name="serverIp">The IP address or hostname of the database server.</param>
        /// <param name="serverPort">The TCP port of the database server.</param>
        /// <param name="username">The username to use when authenticating with the database server.</param>
        /// <param name="password">The password to use when authenticating with the database server.</param>
        /// <param name="database">The name of the database with which to connect.</param>
        public DatabaseClient(
            DbTypes dbType,
            string serverIp,
            int serverPort,
            string username,
            string password,
            string database)
        {
            if (String.IsNullOrEmpty(serverIp))
            {
                throw new ArgumentNullException(nameof(serverIp));
            }
            if (serverPort < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(serverPort));
            }
            if (String.IsNullOrEmpty(database))
            {
                throw new ArgumentNullException(nameof(database));
            }

            _Settings = new DatabaseSettings(dbType, serverIp, serverPort, username, password, database);
            _Header   = "[DatabaseWrapper." + _Settings.Type.ToString() + "] ";

            switch (_Settings.Type)
            {
            case DbTypes.Sqlite:
                throw new ArgumentException("Unable to use this constructor with 'DbTypes.Sqlite'.");

            case DbTypes.Mysql:
                _Mysql = new Mysql.DatabaseClient(_Settings);
                break;

            case DbTypes.Postgresql:
                _Postgresql = new Postgresql.DatabaseClient(_Settings);
                break;

            case DbTypes.SqlServer:
                _SqlServer = new SqlServer.DatabaseClient(_Settings);
                break;

            default:
                throw new ArgumentException("Unknown database type '" + _Settings.Type.ToString() + "'.");
            }
        }
        /// <summary>
        /// Create an instance of the database client.
        /// </summary>
        /// <param name="settings">Database settings.</param>
        public DatabaseClient(DatabaseSettings settings)
        {
            _Settings = settings ?? throw new ArgumentNullException(nameof(settings));

            if (_Settings.Type == DbTypes.Sqlite && String.IsNullOrEmpty(_Settings.Filename))
            {
                throw new ArgumentException("Filename must be populated in database settings of type 'Sqlite'.");
            }

            if (_Settings.Type != DbTypes.SqlServer && !String.IsNullOrEmpty(_Settings.Instance))
            {
                throw new ArgumentException("Instance can only be used in database settings of type 'SqlServer'.");
            }

            _Header = "[DatabaseWrapper." + _Settings.Type.ToString() + "] ";

            switch (_Settings.Type)
            {
            case DbTypes.Sqlite:
                _Sqlite = new Sqlite.DatabaseClient(_Settings);
                break;

            case DbTypes.Mysql:
                _Mysql = new Mysql.DatabaseClient(_Settings);
                break;

            case DbTypes.Postgresql:
                _Postgresql = new Postgresql.DatabaseClient(_Settings);
                break;

            case DbTypes.SqlServer:
                _SqlServer = new SqlServer.DatabaseClient(_Settings);
                break;

            default:
                throw new ArgumentException("Unknown database type '" + _Settings.Type.ToString() + "'.");
            }
        }