Exemplo n.º 1
0
        //public const string settingsFileName = "appsettings.config";
        //private const int settingVersion = 3;


        //private XmlSettingManager<ApplicationSettings> SettingsXml { get; set; }
        //private ApplicationSettings Settings { get; set; }

        //private string GetOldLibraryDirectory()
        //{
        //    var dir = System.Environment.GetFolderPath
        //        (Environment.SpecialFolder.LocalApplicationData);
        //
        //    var saveDirectory =
        //        Path.Combine(dir, @"Packages\60037Boredbone.MikanViewer_8weh06aq8rfkj\LocalState");
        //
        //    return saveDirectory;
        //}

        public async Task ConvertOldLibraryAsync
            (string saveDirectory, string settingFileName, string oldLibraryDirectory, int settingVersion)
        {
            if (saveDirectory == null || settingFileName == null || oldLibraryDirectory == null)
            {
                return;
            }

            Console.WriteLine($"Loading...");

            var config = new LibraryConfiguration(saveDirectory)
            {
                Concurrency    = 512,
                FileTypeFilter = new HashSet <string>(),
                FileSystem     = new FileSystem(),
            };

            LibraryOwner.SetConfig(config);

            using (var library = LibraryOwner.GetCurrent())
            {
                library.InitSettings();
                await library.LoadAsync();

                //ストレージに保存する設定
                var settingsXml = new XmlSettingManager <ApplicationSettings>
                                      (Path.Combine(saveDirectory, settingFileName));

                var settings = settingsXml
                               .LoadXml(XmlLoadingOptions.IgnoreAllException | XmlLoadingOptions.UseBackup)
                               .Value;


                using (var locking = await library.LockAsync())
                {
                    //var saveDirectory = this.GetOldLibraryDirectory();

                    var converter = new LibraryConverter.Compat.Converter();
                    await converter.Start1(oldLibraryDirectory, settings);

                    var data  = library.GetDataForConvert();
                    var count = 0;

                    await converter.Start2(data.Item1, data.Item2, data.Item3,
                                           x => count = x, x =>
                    {
                        Console.CursorLeft = 0;
                        Console.Write($"Importing {x} / {count}");
                    });


                    Console.WriteLine("");

                    library.SaveSettings();
                }


                try
                {
                    settings.Version = settingVersion;
                    settingsXml.SaveXml(settings);
                }
                catch
                {
                }
            }
        }
Exemplo n.º 2
0
        public bool Initialize(string saveDirectory)
        {
            // Set the user interface to display in the same culture as that set in Control Panel.
            System.Threading.Thread.CurrentThread.CurrentUICulture =
                System.Threading.Thread.CurrentThread.CurrentCulture;

            //ストレージに保存する設定
            this.SettingsXml = new XmlSettingManager <ApplicationSettings>
                                   (Path.Combine(saveDirectory, settingsFileName));

            this.Settings = SettingsXml
                            .LoadXml(XmlLoadingOptions.IgnoreAllException | XmlLoadingOptions.UseBackup)
                            .Value;

            this.ImageBuffer = new ImageBuffer().AddTo(this.Disposables);
            this.ImageBuffer.MetaImageExtention = this.MetaImageExtention;

            var config = new LibraryConfiguration(saveDirectory)
            {
                Concurrency    = 512,
                FileTypeFilter = new HashSet <string>(this.FileTypeFilter),
                FileSystem     = new FileSystem(),
            };

            LibraryOwner.SetConfig(config);

            var library = LibraryOwner.GetCurrent();

            library.InitSettings();
            library.LoadAsync().Wait();

            library.AddTo(this.Disposables);

            this.Library = library;


            this.LibraryUpdateHistory = new ReactiveCollection <LibraryUpdateHistoryItem>().AddTo(this.Disposables);

            this.Library.Loaded
            .Subscribe(x => this.LibraryUpdateHistory.AddRangeOnScheduler(
                           x.AddedFiles.Select(y => new LibraryUpdateHistoryItem()
            {
                Date = x.DateTime, Path = y.Key, Type = LibraryUpdateType.Add
            })
                           .Concat(x.RemovedFiles.Select(y => new LibraryUpdateHistoryItem()
            {
                Date = x.DateTime, Path = y.Key, Type = LibraryUpdateType.Remove
            }))
                           .Concat(x.UpdatedFiles.Select(y => new LibraryUpdateHistoryItem()
            {
                Date = x.DateTime, Path = y.Key, Type = LibraryUpdateType.Update
            }))))
            .AddTo(this.Disposables);

            this.SystemNotification = this.Library.Loaded
                                      .Select(x => this.ShowLibraryResult(x))
                                      .Where(x => x != null)
                                      .Publish().RefCount();

            //リソースから文字列を取得
            this.InitializeResourceString();

            //色テーマ
            this.ObserveProperty(x => x.IsDarkTheme)
            .Subscribe(x =>
            {
                ((App)Application.Current).ChangeTheme(x ? darkThemeName : lightThemeName);
            })
            .AddTo(this.Disposables);

            this.ObserveProperty(x => x.BackgroundColor)
            .Subscribe(x =>
            {
                Application.Current.Resources["BasicBackColor"] = new SolidColorBrush(x);
            })
            .AddTo(this.Disposables);

            this.isChanged = true;
            this.PropertyChangedAsObservable().Subscribe(x => this.isChanged = true).AddTo(this.Disposables);


            var libraryHasItem = this.Library.HasItems();

            //ライブラリ更新
            if (libraryHasItem && this.RefreshLibraryOnLaunched)
            {
                this.Library.StartRefreshLibrary(false);
            }

            return(libraryHasItem);
        }
Exemplo n.º 3
0
        public async Task Method1()
        {
            using (var connection = this.database.Connect())
            {
                this.table1.Drop(connection);

                await this.database.InitializeAsync(connection);
            }


            LibraryOwner.SetConfig(new LibraryConfiguration(""));

            /*
             * using (var connection = this.database.Connect())
             * {
             *  try
             *  {
             *      var info = this.table1.GetColumnInformations(connection);
             *      foreach (var item in info)
             *      {
             *          Console.WriteLine(item);
             *      }
             *  }
             *  catch (Exception e)
             *  {
             *      Console.WriteLine(e);
             *  }
             *
             * }*/


            //Console.ReadLine();

            //var items = new[]
            //{
            //    new Table1() { Id = GetId(), Name = "Name-1" }.AsNumbered().WithTimeStamp(),
            //    new Table1() { Id = GetId(), Name = "Name-2" }.AsNumbered().WithTimeStamp(),
            //    new Table1() { Id = GetId(), Name = "Name-3",IsEnabled=true }.AsNumbered().WithTimeStamp(),
            //};
            var items = new[]
            {
                new Record(GetId()),
                new Record(GetId()),
                new Record(GetId()),
            };

            var testName = "n[Am]e'j)(i%2@2_h'''l @p1";

            items[0].SetName(testName);


            var testName2 = testName.Replace('_', 'f').Replace(']', '-');

            items[1].SetName(testName2);

            var currentTime = DateTimeOffset.Now;

            items[1].DateModified = currentTime - TimeSpan.FromDays(2);
            items[2].DateModified = currentTime;

            await this.database.RequestTransactionAsync(async context =>
            {
                foreach (var item in items)
                {
                    await this.table1.AddAsync(item, context);
                }

                await this.table1.AddAsync(new Record(testName), context);
                await this.table1.AddAsync(new Record(testName2), context);
            });

            /*
             * this.database.RequestTransaction(context =>
             * {
             *  this.table1.Execute
             *  ($"UPDATE {this.table1.Name} SET ()=()",
             *      context);
             * });*/


            using (var connection = this.database.Connect())
            {
                var first = this.table1.AsQueryable(connection).FirstOrDefault();
                //var mappingObj = connection.Query<Table1>("SELECT * FROM table1 LIMIT 1").FirstOrDefault();

                Assert.AreEqual(items[0].Id, first.Id);
                Assert.AreEqual(testName, first.FileName);


                Console.WriteLine("Id: {0}", first.Id);
                Console.WriteLine("Name: {0}", first.FileName);
                {
                    var results2 = await this.SearchAsync(connection, $"{nameof(Record.FileName)} LIKE '%2_h%'");

                    Console.WriteLine("LIKE:");


                    foreach (var item in results2)
                    {
                        Console.WriteLine(item.ToString());
                    }

                    Assert.AreEqual(4, results2.Length);

                    Assert.AreEqual(items[0].Id, results2[0].Id);
                    Assert.AreEqual(items[1].Id, results2[1].Id);
                    Assert.AreEqual(testName, results2[2].Id);
                    Assert.AreEqual(testName2, results2[3].Id);

                    var results3 = await this.SearchAsync(connection, $"{nameof(Record.FileName)} GLOB '*2_h*'");

                    Console.WriteLine("MATCH:");

                    foreach (var item in results3)
                    {
                        Console.WriteLine(item.ToString());
                    }


                    Assert.AreEqual(2, results3.Length);

                    Assert.AreEqual(items[0].Id, results3[0].Id);
                    Assert.AreEqual(testName, results3[1].Id);
                }

                {
                    var results2 = await this.SearchAsync(connection, $"{nameof(Record.FileName)} LIKE '%[am]%'");

                    Console.WriteLine("LIKE:");

                    foreach (var item in results2)
                    {
                        Console.WriteLine(item.ToString());
                    }

                    Assert.AreEqual(2, results2.Length);
                    Assert.AreEqual(items[0].Id, results2[0].Id);
                    Assert.AreEqual(testName, results2[1].Id);

                    var results3 = await this.SearchAsync
                                       (connection, $"lower({nameof(Record.FileName)}) GLOB '*[[]am]*'");

                    Console.WriteLine("MATCH:");

                    foreach (var item in results3)
                    {
                        Console.WriteLine(item.ToString());
                    }

                    Assert.AreEqual(2, results3.Length);
                    Assert.AreEqual(items[0].Id, results3[0].Id);
                    Assert.AreEqual(testName, results3[1].Id);
                }

                {
                    var results3 = await this.SearchAsync
                                       (connection, $"{nameof(Record.FileName)} GLOB '* @p1*'");

                    Assert.AreEqual(4, results3.Length);
                }

                {
                    var sql = $"\\n \n \"uu\"u \r\n <4 >0?? '' ';\"' select * from table1 where Id== @p1*--";

                    var results3 = await this.SearchAsync
                                       (connection, $"Id == '{items[0]}' OR {nameof(Record.FileName)} == '{sql.Replace("'", "''")}'");

                    Assert.AreEqual(1, results3.Length);

                    var results4 = await this.SearchAsync
                                       (connection, $"{nameof(Record.FileName)} == '{sql.Replace("'", "''")}' OR Id == '{items[0]}'");

                    Assert.AreEqual(1, results4.Length);


                    Assert.AreEqual(results3[0].Id, results4[0].Id);
                }

                var results = await this.SearchAsync
                                  (connection, $"{nameof(Record.FileName)} LIKE '{testName.Replace("'", "''")}'");

                // connection.Query<Table1>("SELECT * FROM table1");

                Console.WriteLine("A:");

                foreach (var item in results)
                {
                    Console.WriteLine(item.ToString());
                }

                Assert.AreEqual(2, results.Length);
                Assert.AreEqual(items[0].Id, results[0].Id);
                Assert.AreEqual(testName, results[1].Id);

                var rec = this.table1.GetRecordFromKeyAsync(connection, testName).Result;

                Console.WriteLine(rec.ToString());
                Assert.AreEqual(testName, rec.Id);
            }


            Record[] items2 = null;
            using (var connection = this.database.Connect())
            {
                var results = this.table1.AsQueryable(connection).ToArray();

                items2 = results;

                Console.WriteLine("B:");

                foreach (var item in results)
                {
                    Console.WriteLine(item.ToString());
                }

                Assert.AreEqual(5, results.Length);
                Assert.AreEqual(items[0].Id, results[0].Id);
                Assert.AreEqual(items[1].Id, results[1].Id);
                Assert.AreEqual(items[2].Id, results[2].Id);
                Assert.AreEqual(testName, results[3].Id);
                Assert.AreEqual(testName2, results[4].Id);
            }


            await this.database.RequestTransactionAsync(async context =>
            {
                items2[1].SetName("Edited1");
                items2[1].Height = 22;
                await this.table1.UpdateAsync(items2[1], context);

                items2[2].SetName("Edited2");
                items2[2].Height = 58;
                await this.table1.UpdateAsync(items2[2], context, nameof(Record.FileName));
            });



            using (var connection = this.database.Connect())
            {
                var results = this.table1.AsQueryable(connection).ToArray();

                Console.WriteLine("C:");

                foreach (var item in results)
                {
                    Console.WriteLine(item.ToString());
                }
                Assert.AreEqual(5, results.Length);
                Assert.AreEqual(items[0].Id, results[0].Id);
                Assert.AreEqual(items[1].Id, results[1].Id);
                Assert.AreEqual(items[2].Id, results[2].Id);
                Assert.AreEqual(testName, results[3].Id);
                Assert.AreEqual(testName2, results[4].Id);
            }

            await this.database.RequestTransactionAsync(async context =>
            {
                await this.table1.UpdateAsync(items2[2], context);
            });


            using (var connection = this.database.Connect())
            {
                var results = this.table1.AsQueryable(connection).ToArray();

                Console.WriteLine("D:");

                foreach (var item in results)
                {
                    Console.WriteLine(item.ToString());
                }
                Assert.AreEqual(5, results.Length);
                Assert.AreEqual(items[0].Id, results[0].Id);
                Assert.AreEqual(items[1].Id, results[1].Id);
                Assert.AreEqual(items[2].Id, results[2].Id);
                Assert.AreEqual(testName, results[3].Id);
                Assert.AreEqual(testName2, results[4].Id);
            }


            /*
             * using (var connection = this.database.Connect())
             * {
             *  try
             *  {
             *      var info = this.table1.GetColumnInformations(connection);
             *      foreach (var item in info)
             *      {
             *          Console.WriteLine(item);
             *      }
             *  }
             *  catch (Exception e)
             *  {
             *      Console.WriteLine(e);
             *  }
             *
             * }*/
        }
Exemplo n.º 4
0
        private async Task <Library> CreateLibraryAsync()
        {
            LibraryOwner.Reset();
            //this.settings?.Folders?.Clear();
            this.FileEnumerator?.Clear();

            var path   = System.IO.Directory.GetCurrentDirectory();
            var config = new LibraryConfigurationDummy(path);

            this.FileEnumerator = new Dictionary <string, FolderContainerDummy>();// = new FolderAccesserDummy();
            //config.FolderAccesser = this.FileEnumerator;

            config.GetChildFoldersFunction = s =>
            {
                var key = s.TrimEnd(System.IO.Path.DirectorySeparatorChar);

                if (!this.FileEnumerator.ContainsKey(key))
                {
                    Debug.WriteLine(key);
                    this.FileEnumerator.ForEach(x => Debug.WriteLine(x.Key + "," + x.Value.Path));
                    return(null);
                }
                return(this.FileEnumerator[key]
                       .Folders?.Select(x => x.Value.Path) ?? new string[0]);
            };

            config.GetFolderFunction = s =>
            {
                var key = s.TrimEnd(System.IO.Path.DirectorySeparatorChar);
                FolderContainerDummy f;
                this.FileEnumerator.TryGetValue(key, out f);
                return(f);
            };

            this.config = config;
            LibraryOwner.SetConfig(config);

            //config.Folders.Add(folder);
            //config.FileExistingChecker = _ => false;

            var library = LibraryOwner.GetCurrent();

            this.settings = new LibrarySettings()
            {
                Version = 10,
            };


            library.InitializeLibrarySettings(settings);

            await library.LoadAsync();


            await library.ClearAsync();

            data.ForEach((x, c) =>
            {
                var f = LoadTestData(x);
                this.AddFolder(f);
                library.Folders.Add(new FolderInformation(f.Path));
            });

            await library.RefreshLibraryAsync(true);

            var search = new SearchInformation(new ComplexSearch(false));

            library.Searcher.AddSearchToDictionary(search);

            return(library);
        }
Exemplo n.º 5
0
        public async Task Method2(TimeSpan offset)
        {
            //if (offset != DateTimeOffset.Now.Offset)
            {
                DatabaseFunction.SetDateOffset(offset);
            }

            using (var connection = this.database.Connect())
            {
                this.table1.Drop(connection);

                await this.database.InitializeAsync(connection);
            }


            LibraryOwner.SetConfig(new LibraryConfiguration(""));

            var items = new[]
            {
                new Record(GetId()),
                new Record(GetId()),
                new Record(GetId()),
            };

            var testName = "n[Am]e'j)(i%2@2_h'''l @p1";

            items[0].SetName(testName);


            var testName2 = testName.Replace('_', 'f').Replace(']', '-');

            items[1].SetName(testName2);

            var currentTime = DateTimeOffset.Now.ToOffset(offset);

            items[1].DateModified = currentTime - TimeSpan.FromDays(2);
            items[2].DateModified = currentTime;

            await this.database.RequestTransactionAsync(async context =>
            {
                foreach (var item in items)
                {
                    await this.table1.AddAsync(item, context);
                }

                await this.table1.AddAsync(new Record(testName), context);
                await this.table1.AddAsync(new Record(testName2), context);
            });


            using (var connection = this.database.Connect())
            {
                Console.WriteLine("a");
                //0001-01-01 00:00:00 +00:00
                Assert.AreEqual("0",//"0001-01-01 00:00:00 +00:00",
                                this.table1.AsQueryable(connection)
                                .Select <string>(nameof(Record.DateModified))
                                .ToArray()
                                .First());

                Console.WriteLine("b");
                //0001-01-01
                Assert.AreEqual((-offset.TotalSeconds).ToString(),
                                this.table1.AsQueryable(connection)
                                .Select <string>(DatabaseFunction.GetDate(nameof(Record.DateModified)))
                                .ToArray()
                                .First());

                Console.WriteLine("c");
                ////00:00:00
                //Assert.AreEqual("00:00:00",
                //    this.table1.AsQueryable(connection)
                //    .Select<string>($"time({nameof(Record.DateModified)})")
                //    .ToArray()
                //    .First());

                //0001/01/01 0:00:00 +00:00
                Assert.AreEqual(DateTimeOffset.FromUnixTimeSeconds(0), this.table1.AsQueryable(connection)
                                .Select <DateTimeOffset>(nameof(Record.DateModified))
                                .ToArray()
                                .First());


                Console.WriteLine("d");
                var today = DateTimeOffset.Now.ToOffset(offset).ToDate();


                var mods = this.table1.AsQueryable(connection)
                           .Where(DatabaseExpression.AreEqual
                                      (DatabaseFunction.GetDate(nameof(Record.DateModified)),
                                      new DatabaseReference(UnixTime.FromDateTime(today).ToString())))
                           .Select <DateTimeOffset>($"{nameof(Record.DateModified)}")
                           .Take(2)
                           .ToArray();

                Console.WriteLine("e");
                Assert.AreEqual(1, mods.Length);

                Console.WriteLine("f");
                this.AreEqual(new DateTimeOffset(currentTime.Year, currentTime.Month, currentTime.Day,
                                                 currentTime.Hour, currentTime.Minute, currentTime.Second, offset),
                              mods[0]);

                Console.WriteLine("g");
            }
        }