private static void SaveRouteAssetsBulkToDatabase(RouteModel route,
      List<RouteAssetsModel> routeAssets)
      {
      // Step 1: add all assets tot he assets table
      // Step 2: insert provider products in the table
      // Step 3: get for each asset the id from the assets table (all assets will be there)
      // Step 4: insert route Id and asset id in the 

      List<AssetModel> assets = routeAssets.Select(x => x.Asset).ToList();
      List<ProviderProductModel> providerProducts =
        routeAssets.Select(x => x.Asset.ProviderProduct).DistinctBy(x => x.ProviderProduct)
          .ToList();
      ProviderProductCollectionDataAccess.SaveProviderProductsBulk(providerProducts);
      AssetCollectionDataAccess.SaveAssetsBulk(assets);

      providerProducts = assets.Select(x => x.ProviderProduct).ToList();
      // make sure all providerProducts have an Id 
      ProviderProductCollectionDataAccess.UpdateListIdsFromDatabase(providerProducts);
      // Bulk insert
      using (IDbConnection connection =
        new SQLiteConnection(AssetDatabaseAccess.GetConnectionString()))
        {
        connection.Open();
        using (IDbTransaction transaction = connection.BeginTransaction())
          {
          string sqlStatement = @$"INSERT OR IGNORE INTO RouteAssets (RouteId,AssetId)
                                    SELECT Routes.Id, Assets.Id FROM Routes, Assets
	                                  WHERE Routes.Id= (SELECT Id FROM Routes WHERE RouteGuid= @RouteGuid) AND
                                    Assets.Id= (SELECT Id FROM Assets WHERE BlueprintPath=@BluePrintPath AND 
                                    Assets.ProvProdId=@Id); ";
          try
            {
            foreach (var item in routeAssets)
              {
              connection.Execute(sqlStatement, new { item.Route.RouteGuid, item.Asset.BluePrintPath, item.Asset.ProviderProduct.Id }, transaction);
              }

            transaction.Commit();
            }
          catch (Exception e)
            {
            transaction.Rollback();
            Log.Trace($"Bulk save in database for assets failed, rolled back", e,
              LogEventType.Error);
            }
          }
        }
      }
Beispiel #2
0
        public static void SaveAllUnpackedAssets(string assetBasePath, bool InGame, bool InArchive)
        {
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();
            try
            {
                List <ProviderProductModel> ProviderProductList = ProviderProductCollectionDataAccess.ReadAllProviderProductsFromDatabase();
                ProviderProductList = ProviderProductList.Where(x => x.Pack.Length == 0).ToList();

                ProgressPercentageModel progress = new ProgressPercentageModel()
                {
                    TotalWork       = ProviderProductList.Count,
                    CurrentProgress = 0,
                    Message         = "Saving assets progress"
                };

                foreach (ProviderProductModel providerProduct in ProviderProductList)
                {
                    providerProduct.Id = providerProduct.GetDatabaseRecordId();
                    String tempPath = $@"{assetBasePath}{providerProduct.ArchiveFileName}";
                    if (Directory.Exists(tempPath))
                    {
                        DirectoryInfo     providerProductDir = new DirectoryInfo(tempPath);
                        List <AssetModel> assets             = null;
                        assets =
                            ReadAllAssetsFromDirectory(providerProductDir, providerProduct, tempPath.Length, InGame, InArchive);
                        SaveAssetsBulk(assets, providerProduct.Id);
                        UpdateBulkStatus(assets, Converters.LocationToString(InGame, InArchive));
                        progress.CurrentProgress++;
                        SaveAssetProgressReporter?.Invoke(null, progress);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Trace($"Saving unpacked assets failed ", ex, LogEventType.Error);
            }
        }