public Query(Database database)
        {
            if (database == null) {
                throw new QueryException(1, "Database is null.");
            }

            this.database = database;

            mysqlConnection = new MySqlConnection(this.database.getConnectionString());
        }
        public FormMain(Company company, User user)
        {
            InitializeComponent();

            this.DoubleBuffered = true;
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

            this.user = user;
            this.company = company;

            database = new Database(Settings.Default.DatabaseHost, Settings.Default.DatabaseUsername, Settings.Default.DatabasePassword, 30);
        }
        //User account;
        public Query(string sql, Database database)
        {
            if (database == null) {
                throw new QueryException(1, "Database is null.");
            }
            if (String.IsNullOrEmpty(sql)) {
                throw new QueryException(1, "Sql is null or empty.");
            }
            this.database = database;
            this.sql = sql;

            mysqlConnection = new MySqlConnection(this.database.getConnectionString());
        }
        private void threadMysqlTestFunc()
        {
            try {
                Database database = new Database(Settings.Default.DatabaseHost, Settings.Default.DatabaseUsername, Settings.Default.DatabasePassword, Settings.Default.DatabasePort, "");

                database.checkConnection();
                this.Invoke(new Action(() => {
                    MessageBox.Show("Successful", "Successful", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }));
            } catch (DatabaseException databaseException) {
                this.Invoke(new Action(() => {
                    MessageBox.Show(databaseException.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }));
            } catch (Exception exception) {
                this.Invoke(new Action(() => {
                    MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }));
            }
        }
        private void comboBoxReportType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxReportType.SelectedIndex == -1)
                return;

            checkedListBoxColumnName.Items.Clear();

            ReportType reportType = (ReportType)Enum.Parse(typeof(ReportType), comboBoxReportType.Text);

            Database database = new Database(Settings.Default.DatabaseHost, Settings.Default.DatabaseUsername, Settings.Default.DatabasePassword, 30);
            Query query = new Query(database);
            DataTable dataTable = query.getReportTable(reportType);
            foreach (DataColumn dataColumn in dataTable.Columns) {
                checkedListBoxColumnName.Items.Add(dataColumn.ColumnName);
            }

            uint setting = 0;
            switch (reportType) {
                case ReportType.HISTORICAL:
                    setting = Settings.Default.tableHistorical;
                    break;
                case ReportType.RUNNING:
                    setting = Settings.Default.tableRunning;
                    break;
                case ReportType.IDLING:
                    setting = Settings.Default.tableIdle;
                    break;
                case ReportType.ACC:
                    setting = Settings.Default.tableAcc;
                    break;
                case ReportType.GEOFENCE:
                    setting = Settings.Default.tableGeofence;
                    break;
                case ReportType.OVERSPEED:
                    setting = Settings.Default.tableOverspeed;
                    break;
                case ReportType.EXTERNAL_POWER_CUT:
                    setting = Settings.Default.tableExternalPowerCut;
                    break;
                case ReportType.TRACKERS:
                    setting = Settings.Default.tableTrackers;
                    break;
                case ReportType.TRACKERS_GEOFENCE:
                    setting = Settings.Default.tableTrackersGeofence;
                    break;
                case ReportType.TRACKERS_HISTORICAL:
                    setting = Settings.Default.tableTrackersHistorical;
                    break;
                case ReportType.ALL_TRACKERS:
                    setting = Settings.Default.tableAllTrackers;
                    break;
                case ReportType.ALL_COMPANIES:
                    setting = Settings.Default.tableAllCompanies;
                    break;
            }

            int totalItems = dataTable.Columns.Count;
            for (int index = 0; index < totalItems; index++) {
                uint status = Converter.getBit(setting, index);
                bool flag = (status == 1) ? true : false;
                checkedListBoxColumnName.SetItemChecked(index, flag);

            }

            checkedListBoxColumnNameUpdate();
        }
        void run(object state)
        {
            pictureBoxLoading.Invoke(new MethodInvoker(delegate {
                pictureBoxLoading.Visible = true;
            }));

            try {

                database = new Database(Settings.Default.DatabaseHost, Settings.Default.DatabaseUsername, Settings.Default.DatabasePassword);

                Query query = new Query(database);

                query.getCompany(company);
                query.getUser(company, user);

                if (user.AccessLevel != 1) {

                    int isExpired = company.DateTimeExpired.CompareTo(DateTime.Now);

                    if (isExpired == -1)
                        throw new QueryException(1, "This company is expired.");
                    if (!company.IsActive)
                        throw new QueryException(1, "Company is deactivated.");

                    isExpired = user.DateTimeExpired.CompareTo(DateTime.Now);

                    if (isExpired == -1)
                        throw new QueryException(1, "This user is expired.");
                    if (!user.IsActive)
                        throw new QueryException(1, "User is deactivated.");
                }
                //=============================Login successful

                query.fillGeofences(company);

                query.fillUsers(company, user);

                query.fillCollection(company);

                query.fillTrackers(company);

                query.fillPois(company);

                this.Invoke(new MethodInvoker(delegate {
                    Settings.Default.accountRememberMe = (bool)user.RememberMe;
                    Settings.Default.accountCompanyUsername = company.Username;
                    Settings.Default.accountUsername = user.Username;
                    Settings.Default.accountPassword = user.Password;
                    Settings.Default.Save();

                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }));
            } catch (QueryException queryException) {
                this.Invoke(new MethodInvoker(delegate {
                    MessageBox.Show(this, queryException.Message, "Query Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    Log log = new Log(LogFileType.TXT, LogType.EXCEPTION);
                    string logData = DateTime.Now.ToString() + "\t\t queryException \t\t" + queryException.Message;
                    log.write(logData);
                }));
            } catch (DatabaseException databaseException){
                this.Invoke(new MethodInvoker(delegate {
                    MessageBox.Show(this, databaseException.Message, "DatabaseException", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    Log log = new Log(LogFileType.TXT, LogType.EXCEPTION);
                    string logData = DateTime.Now.ToString() + "\t\t DatabaseException \t\t" + databaseException.Message;
                    log.write(logData);
                }));

            } finally {
                try {
                    pictureBoxLoading.Invoke(new MethodInvoker(delegate {
                        pictureBoxLoading.Visible = false;
                    }));
                } catch {

                }
            }
        }