internal LocalDirectory GetLocalDirectory(string path) { if (path.Length > 1 && path.EndsWith("/")) { path = path.Substring(0, path.Length - 1); } var parts = path.Split('/'); if (parts.Length < 3) { return((LocalDirectory)GetDirectory(path)); } return(core.FileSystem.UseConnection(delegate(IDbConnection connection) { var query = "SELECT * FROM directoryitems WHERE type = 'D' AND full_path = @full_path LIMIT 1"; var command = connection.CreateCommand(); command.CommandText = query; core.FileSystem.AddParameter(command, "@full_path", path); var data = core.FileSystem.ExecuteDataSet(command); if (data.Tables[0].Rows.Count > 0) { return LocalDirectory.FromDataRow(this, data.Tables[0].Rows[0]); } return null; })); }
internal static LocalDirectory[] ListByParentId(FileSystemProvider fileSystem, int?parent_id) { return(fileSystem.UseConnection(delegate(IDbConnection connection) { var command = connection.CreateCommand(); string query = null; if (parent_id.Equals(null)) { query = "SELECT * FROM directoryitems WHERE parent_id ISNULL AND type = 'D'"; } else { query = "SELECT * FROM directoryitems WHERE parent_id = @parent_id AND type = 'D'"; fileSystem.AddParameter(command, "@parent_id", (int)parent_id); } command.CommandText = query; var ds = fileSystem.ExecuteDataSet(command); var results = new LocalDirectory[ds.Tables[0].Rows.Count]; for (var x = 0; x < ds.Tables[0].Rows.Count; x++) { results[x] = FromDataRow(fileSystem, ds.Tables[0].Rows[x]); } return results; })); }
private static LocalFile CreateFile(FileSystemProvider fs, LocalDirectory parentDirectory, string name, string localpath, long length) { var lastId = -1; var fullPath = PathUtil.Join(parentDirectory.FullPath, name); if (fullPath.Length > 1 && fullPath.EndsWith("/")) { fullPath = fullPath.Substring(0, fullPath.Length - 1); } fs.UseConnection(connection => { var cmd = connection.CreateCommand(); cmd.CommandText = @"INSERT INTO directoryitems (type, name, local_path, parent_id, length, full_path) VALUES ('F', @name, @local_path, @parent_id, @length, @full_path);" ; fs.AddParameter(cmd, "@name", name); fs.AddParameter(cmd, "@local_path", localpath); fs.AddParameter(cmd, "@parent_id", parentDirectory.Id); fs.AddParameter(cmd, "@length", length); fs.AddParameter(cmd, "@full_path", fullPath); fs.ExecuteNonQuery(cmd); cmd = connection.CreateCommand(); cmd.CommandText = "SELECT last_insert_rowid()"; lastId = Convert.ToInt32(fs.ExecuteScalar(cmd)); }, true); return(new LocalFile(lastId, parentDirectory.Id, name, localpath, length, fullPath)); }
public SearchResultInfo SearchFiles(string query) { string sql; IDbCommand command; DataSet ds; int x; SearchResultInfo result; var directories = new List <string>(); var files = new List <SharedFileListing>(); result = new SearchResultInfo(); var queryNode = UserQueryParser.Parse(query, FieldSet); var queryFragment = queryNode.ToSql(FieldSet); var sb = new StringBuilder(); sb.Append("SELECT * FROM directoryitems WHERE "); sb.Append(queryFragment); sb.AppendFormat(" LIMIT {0}", MAX_RESULTS); UseConnection(delegate(IDbConnection connection) { command = connection.CreateCommand(); command.CommandText = sb.ToString(); ds = ExecuteDataSet(command); for (x = 0; x < ds.Tables[0].Rows.Count; x++) { if (ds.Tables[0].Rows[x]["type"].ToString() == "F") { files.Add(new SharedFileListing(LocalFile.FromDataRow(core.FileSystem, ds.Tables[0].Rows[x]), false)); } else { var dir = LocalDirectory.FromDataRow(this, ds.Tables[0].Rows[x]); // FIXME: Ugly: Remove '/local' from begining of path var path = "/" + string.Join("/", dir.FullPath.Split('/').Slice(2)); directories.Add(path); } } }); result.Files = files.ToArray(); result.Directories = directories.ToArray(); return(result); }
private static LocalDirectory CreateDirectory(FileSystemProvider fileSystem, LocalDirectory parent, string name, string local_path) { string fullPath; var last_id = -1; if (string.IsNullOrEmpty(local_path)) { throw new ArgumentNullException("local_path"); } if (!Directory.Exists(local_path)) { throw new ArgumentException("local_path", $"Directory does not exist: '{local_path}'"); } if (parent != null) { fullPath = PathUtil.Join(parent.FullPath, name); } else { fullPath = PathUtil.Join("/", name); } if (fullPath.Length > 1 && fullPath.EndsWith("/")) { fullPath = fullPath.Substring(0, fullPath.Length - 1); } fileSystem.UseConnection(delegate(IDbConnection connection) { var cmd = connection.CreateCommand(); cmd.CommandText = @"INSERT INTO directoryitems (type, parent_id, name, local_path, full_path) VALUES ('D', @parent_id, @name, @local_path, @full_path);" ; fileSystem.AddParameter(cmd, "@name", name); fileSystem.AddParameter(cmd, "@local_path", local_path); fileSystem.AddParameter(cmd, "@full_path", fullPath); if (parent == null) { fileSystem.AddParameter(cmd, "@parent_id", null); } else { fileSystem.AddParameter(cmd, "@parent_id", parent.Id); } fileSystem.ExecuteNonQuery(cmd); cmd = connection.CreateCommand(); cmd.CommandText = "SELECT last_insert_rowid()"; last_id = Convert.ToInt32(fileSystem.ExecuteScalar(cmd)); }, true); if (parent != null) { parent.InvalidateCache(); } var parentId = (parent == null) ? -1 : parent.Id; return(new LocalDirectory(fileSystem, last_id, parentId, name, local_path, fullPath)); }
internal static LocalFile CreateFile(FileSystemProvider fs, LocalDirectory parentDirectory, FileInfo info) { return(CreateFile(fs, parentDirectory, info.Name, info.FullName, info.Length)); }