Пример #1
0
        public File GetFile(string path)
        {
            var relativeToTaxonomyRootPath = PathExt.GetRelativePath(RootPath, Path.GetFullPath(path));

            using (var transaction = connection.BeginTransaction())
            {
                File file;
                using (
                    var command = connection.CreateCommand())
                {
                    command.CommandText = @"SELECT * FROM files WHERE path = @path";
                    command.Parameters.Add(facade.Parameter("@path", DbType.String));
                    command.Parameters["@path"].Value = relativeToTaxonomyRootPath;
                    file = LookupFiles(command).FirstOrDefault();
                }
                if (file != null)
                {
                    return(file);
                }

                long id;
                var  tagCollection = new Lazy <ICollection <Tag> >(() => new ObservableSet <Tag>());
                file = new File(0, RootPath, relativeToTaxonomyRootPath, tagCollection);
                using (
                    var command = connection.CreateCommand())
                {
                    command.CommandText = @"INSERT INTO files (path, hash) VALUES (@path, @hash)";
                    command.Parameters.Add(facade.Parameter("@path", DbType.String));
                    command.Parameters.Add(facade.Parameter("@hash", DbType.Binary));
                    command.Parameters["@path"].Value = file.RelativePath;
                    command.Parameters["@hash"].Value = file.Hash;
                    command.ExecuteNonQuery();
                    id = facade.LastInsertRowId(connection);
                }
                file.Id = id;
                transaction.Commit();
                ObserveTagCollectionForFile(file.Id, (ObservableSet <Tag>)tagCollection.Value);
                fileCache.Add(file.Id, new WeakReference <File>(file));
                return(file);
            }
        }
Пример #2
0
 // set a path: can be relative or absolute
 private void SetPath(string newPath)
 {
     relativePath = Path.IsPathRooted(newPath)
                         ? PathExt.GetRelativePath(RootPath, newPath)
                         : PathExt.GetRelativePath(RootPath, Path.Combine(Directory.GetCurrentDirectory(), newPath));
 }