private DataBaseManager()
        {
            _primary = Sqlite.LoadFromFile();

            _primary.ProgressChangedEvent += OnProgressChangedEvent;
            _primary.ProgressFinishEvent  += OnProgressFinishEvent;

            if (AppSettings.Instance.SqlEnabled == "true")
            {
                if (AppSettings.Instance.SqlType == "MySql" && !string.IsNullOrEmpty(AppSettings.Instance.MySqlServer))
                {
                    MySqlConnectionStringBuilder mysqlBuiler = new MySqlConnectionStringBuilder()// Database is set later in constructor!
                    {
                        Server   = AppSettings.Instance.MySqlServer,
                        UserID   = AppSettings.Instance.MySqlUserId,
                        Password = AppSettings.Instance.MySqlPassword,
                        Port     = (uint)AppSettings.Instance.MySqlPort,
                        SslMode  = AppSettings.Instance.MySqlSsl == "true" ? MySqlSslMode.Required : MySqlSslMode.None
                    };
                    try
                    {
                        _secondary = new MySql(mysqlBuiler, MySql.UpdateModes.Async);

                        // Associate events
                        _secondary.TimeDateaUpdate        += OnTimeDateaUpdate;
                        _secondary.ProgressChangedEvent   += OnProgressChangedEvent;
                        _secondary.ProgressFinishEvent    += OnProgressFinishEvent;
                        _secondary.ConnectionChangedEvent += OnConnectionChangedEvent;
                        _secondary.UpdateChangedEvent     += OnUpdateChangedEvent;

                        var daysInWeek = _primary.DaysInRange(StartEndWeek(DateTime.Today)[0], StartEndWeek(DateTime.Today)[1]);

                        if (_secondary.ServerState == State.Connected)
                        {
                            _secondary.Push(daysInWeek);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                        MessageBox.Show(e.Message);
                    }
                }
                else if (AppSettings.Instance.SqlType == "Azure" && !string.IsNullOrEmpty(AppSettings.Instance.AzureDateSource))
                {
                    MessageBox.Show("Azure sql not implemented!");
                }
            }
        }
Exemple #2
0
        public static Sqlite LoadFromFile()
        {
            if (Path.GetExtension(AppSettings.Instance.DataPath) != ".sqlite")
            {
                TimeData data    = TimeData.Load(AppSettings.Instance.DataPath);
                string   newPath = Path.ChangeExtension(AppSettings.Instance.DataPath, ".sqlite");

                if (newPath != null && File.Exists(newPath))
                {
                    File.Delete(newPath);
                }

                SQLiteConnection.CreateFile(newPath);
                Sqlite instance = new Sqlite("Data Source=" + newPath + ";Version=3");

                if (data.Users == null || data.Users.Count == 0)
                {
                    EnterUser enterUser = new EnterUser();
                    enterUser.ShowDialog();
                    List <Day> days = new List <Day>();
                    if (data.Days != null && data.Days.Count > 0)
                    {
                        days = data.Days;
                    }

                    User user = new User(enterUser.UserText, "", days);
                    instance.AddUser(user);
                }
                else
                {
                    foreach (User user in data.Users)
                    {
                        instance.AddUser(user);
                    }
                }

                AppSettings.Instance.DataPath = newPath;
                return(instance);
            }
            if (!File.Exists(AppSettings.Instance.DataPath))
            {
                SQLiteConnection.CreateFile(Path.ChangeExtension(AppSettings.Instance.DataPath, ".sqlite"));
            }
            return(new Sqlite("Data Source=" + AppSettings.Instance.DataPath + ";Version=3"));
        }