/// <summary> /// Получение метаданных папок из БД. /// </summary> /// <param name="query">Запрос на получение метаданных.</param> /// <returns></returns> internal DBCollection <FolderMetadata> GetFolders(string query) { DBCollection <FolderMetadata> folders = new DBCollection <FolderMetadata>(); string resultQuery = null; if (String.IsNullOrEmpty(query)) { resultQuery = this.SelectQuery; } else { resultQuery = @"{SelectQuery} WHERE {Query}" .ReplaceKey("SelectQuery", this.SelectQuery) .ReplaceKey("Query", query); } DataTable resultTable = this.DataAdapter.GetDataTable(resultQuery); if (resultTable != null) { foreach (DataRow row in resultTable.Rows) { FolderMetadata metadata = new FolderMetadata(row); folders.Add(metadata); } } return(folders); }
private DBCollection <SqlParameter> CreateParameters(T metadataObject, DBCollection <MetadataPropertyDefinition> metadataProperties) { if (metadataObject == null) { throw new ArgumentNullException("metadataObject"); } DBCollection <SqlParameter> parameters = new DBCollection <SqlParameter>(); foreach (MetadataPropertyDefinition property in metadataProperties) { object value = property.Property.GetValue(metadataObject, null); DBColumnSchema columnSchema = this.DBSchemaAdapter.TableSchema.GetColumn(property.ColumnName, true); SqlParameter param = new SqlParameter(property.ParameterName, columnSchema.Type); param.Value = this.EnsureParameterValue(value); parameters.Add(param); } return(parameters); }
/// <summary> /// Получение коллекции объектов хранилища по условию. /// </summary> /// <param name="condition">Условие.</param> /// <returns></returns> internal DBCollection <StorageMetadata> GetStorages(string condition) { string resultQuery = this.SelectQuery; if (!string.IsNullOrEmpty(condition)) { resultQuery += string.Format("WHERE {0}", condition); } DataTable resultTable = this.DataAdapter.GetDataTable(resultQuery); DBCollection <StorageMetadata> result = new DBCollection <StorageMetadata>(); if (resultTable != null) { foreach (DataRow resultRow in resultTable.Rows) { StorageMetadata metadata = new StorageMetadata(resultRow); result.Add(metadata); } } return(result); }
/// <summary> /// Возвращает коллекцию папок репликации. /// </summary> /// <param name="condition">Условие на выборку метаданных.</param> /// <returns></returns> internal DBCollection <ReplicationFolderMetadata> GetReplicationFolders(string condition = null) { DBCollection <ReplicationFolderMetadata> replicationFolders = new DBCollection <ReplicationFolderMetadata>(); string resultQuery = null; if (string.IsNullOrEmpty(condition)) { resultQuery = this.SelectQuery; } else { resultQuery = @"{SelectQuery} WHERE {Condition}" .ReplaceKey("SelectQuery", this.SelectQuery) .ReplaceKey("Condition", condition); } DataTable resultTable = this.DataAdapter.GetDataTable(resultQuery); if (resultTable != null) { //собираем все идентификаторы папок и узлов хранилища //чтобы получить их за отдельный запрос Dictionary <int, FolderMetadata> uniqueFolders = new Dictionary <int, FolderMetadata>(); Dictionary <int, StorageMetadata> uniqueStorages = new Dictionary <int, StorageMetadata>(); //сначала заполняем ключи словарей foreach (DataRow row in resultTable.Rows) { int folderID = DataRowReader.GetIntegerValue(row, "FolderID"); if (folderID > 0 && !uniqueFolders.ContainsKey(folderID)) { uniqueFolders.Add(folderID, null); } int storageID = DataRowReader.GetIntegerValue(row, "StorageID"); if (storageID > 0 && !uniqueStorages.ContainsKey(storageID)) { uniqueStorages.Add(storageID, null); } } //потом получаем объекты по этим ключам и "дозаполняем" словарь if (uniqueFolders.Count > 0) { string foldersIdentities = string.Join(",", uniqueFolders.Keys.ToArray()); string foldersCondition = string.Format("[ID] IN ({0})", foldersIdentities); DBCollection <FolderMetadata> folders = this.MetadataAdapter.FolderAdapter.GetFolders(foldersCondition); foreach (FolderMetadata folder in folders) { if (uniqueFolders.ContainsKey(folder.ID)) { uniqueFolders[folder.ID] = folder; } } } if (uniqueStorages.Count > 0) { string storagesIdentities = string.Join(",", uniqueStorages.Keys.ToArray()); string storagesCondition = string.Format("[ID] IN ({0})", storagesIdentities); DBCollection <StorageMetadata> storages = this.MetadataAdapter.StorageAdapter.GetStorages(storagesCondition); foreach (StorageMetadata storage in storages) { if (uniqueStorages.ContainsKey(storage.ID)) { uniqueStorages[storage.ID] = storage; } } } foreach (DataRow row in resultTable.Rows) { FolderMetadata folder = null; StorageMetadata storage = null; int folderID = DataRowReader.GetIntegerValue(row, "FolderID"); if (folderID > 0 && uniqueFolders.ContainsKey(folderID)) { folder = uniqueFolders[folderID]; } int storageID = DataRowReader.GetIntegerValue(row, "StorageID"); if (storageID > 0 && uniqueStorages.ContainsKey(storageID)) { storage = uniqueStorages[storageID]; } if (folder == null) { int rowID = DataRowReader.GetIntegerValue(row, "ID"); throw new Exception(string.Format("Для строки метаданных папки репликации с идентификатором {0} не найдена папка с идентификатором {1}", rowID, folderID)); } if (storage == null) { int rowID = DataRowReader.GetIntegerValue(row, "ID"); throw new Exception(string.Format("Для строки метаданных папки репликации с идентификатором {0} не найден узел хранилища с идентификатором {1}", rowID, storageID)); } ReplicationFolderMetadata metadata = new ReplicationFolderMetadata(row, folder, storage); replicationFolders.Add(metadata); } } return(replicationFolders); }