2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 namespace Tizen.Content.MediaContent
22 /// Provides the commands to manage external storages in the database.
25 /// The internal storage is not managed.
27 /// <seealso cref="Storage"/>
28 public class StorageCommand : MediaCommand
31 /// Initializes a new instance of the <see cref="StorageCommand"/> class with the specified <see cref="MediaDatabase"/>.
33 /// <param name="database">The <see cref="MediaDatabase"/> that the commands run on.</param>
34 /// <exception cref="ArgumentNullException"><paramref name="database"/> is null.</exception>
35 /// <exception cref="ObjectDisposedException"><paramref name="database"/> has already been disposed of.</exception>
36 public StorageCommand(MediaDatabase database) : base(database)
41 /// Retrieves the number of storages.
43 /// <returns>The number of storages.</returns>
44 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
45 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
46 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
47 public int Count() => Count(arguments: null);
50 /// Retrieves the number of storages with the <see cref="CountArguments"/>.
52 /// <param name="arguments">The criteria to use to filter. This value can be null.</param>
53 /// <returns>The number of storages filtered.</returns>
54 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
55 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
56 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
57 public int Count(CountArguments arguments)
61 return CommandHelper.Count(Interop.Storage.GetStorageCountFromDb, arguments);
65 /// Retrieves the storage with the specified ID.
67 /// <param name="storageId">The storage ID to select.</param>
68 /// <returns>The <see cref="Storage"/> instance if the matched record was found in the database, otherwise null.</returns>
69 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
70 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
71 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
72 /// <exception cref="ArgumentNullException"><paramref name="storageId"/> is null.</exception>
73 /// <exception cref="ArgumentException"><paramref name="storageId"/> is a zero-length string, contains only white space.</exception>
74 public Storage Select(string storageId)
78 ValidationUtil.ValidateNotNullOrEmpty(storageId, nameof(storageId));
80 IntPtr handle = IntPtr.Zero;
84 Interop.Storage.GetStorageInfoFromDb(storageId, out handle).ThrowIfError("Failed to query");
86 return handle == IntPtr.Zero ? null : new Storage(handle);
90 if (handle != IntPtr.Zero)
92 Interop.Storage.Destroy(handle);
98 /// Retrieves all the storages.
100 /// <returns>The <see cref="MediaDataReader{TRecord}"/> containing the results.</returns>
101 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
102 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
103 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
104 public MediaDataReader<Storage> Select() => Select(arguments: null);
107 /// Retrieves the storages with the <see cref="SelectArguments"/>.
109 /// <param name="arguments">The criteria to use to filter. This value can be null.</param>
110 /// <returns>The <see cref="MediaDataReader{TRecord}"/> containing the results.</returns>
111 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
112 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
113 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
114 public MediaDataReader<Storage> Select(SelectArguments arguments)
118 return CommandHelper.Select(arguments, Interop.Storage.ForeachStorageFromDb, Storage.FromHandle);
121 private bool Exists(string storageId)
125 ValidationUtil.ValidateNotNullOrEmpty(storageId, nameof(storageId));
127 return Count(new CountArguments { FilterExpression = $"{StorageColumns.Id}='{storageId}'" }) != 0;
131 /// Retrieves the number of media information of the storage.
133 /// <param name="storageId">The storage ID.</param>
134 /// <returns>The number of media information.</returns>
135 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
136 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
137 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
138 /// <exception cref="ArgumentNullException"><paramref name="storageId"/> is null.</exception>
139 /// <exception cref="ArgumentException"><paramref name="storageId"/> is a zero-length string, contains only white space.</exception>
140 public int CountMedia(string storageId) => CountMedia(storageId, null);
143 /// Retrieves the number of media information of the storage with the <see cref="CountArguments"/>.
145 /// <param name="storageId">The storage ID to query with.</param>
146 /// <param name="arguments">The criteria to use to filter. This value can be null.</param>
147 /// <returns>The number of media information.</returns>
148 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
149 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
150 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
151 /// <exception cref="ArgumentNullException"><paramref name="storageId"/> is null.</exception>
152 /// <exception cref="ArgumentException"><paramref name="storageId"/> is a zero-length string, contains only white space.</exception>
153 public int CountMedia(string storageId, CountArguments arguments)
155 if (Exists(storageId) == false)
160 return CommandHelper.Count(Interop.Storage.GetMediaCountFromDb, storageId, arguments);
164 /// Retrieves the media information of the storage.
166 /// <param name="storageId">The storage ID.</param>
167 /// <returns>The <see cref="MediaDataReader{TRecord}"/> containing the results.</returns>
168 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
169 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
170 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
171 /// <exception cref="ArgumentNullException"><paramref name="storageId"/> is null.</exception>
172 /// <exception cref="ArgumentException"><paramref name="storageId"/> is a zero-length string, contains only white space.</exception>
173 public MediaDataReader<MediaInfo> SelectMedia(string storageId) => SelectMedia(storageId, null);
176 /// Retrieves the media information of the storage with the <see cref="SelectArguments"/>.
178 /// <param name="storageId">The storage ID.</param>
179 /// <param name="filter">The criteria to use to filter. This value can be null.</param>
180 /// <returns>The <see cref="MediaDataReader{TRecord}"/> containing the results.</returns>
181 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
182 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
183 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
184 /// <exception cref="ArgumentNullException"><paramref name="storageId"/> is null.</exception>
185 /// <exception cref="ArgumentException"><paramref name="storageId"/> is a zero-length string, contains only white space.</exception>
186 public MediaDataReader<MediaInfo> SelectMedia(string storageId, SelectArguments filter)
188 if (Exists(storageId) == false)
190 return new MediaDataReader<MediaInfo>(new MediaInfo[0]);
193 return CommandHelper.SelectMedia(Interop.Storage.ForeachMediaFromDb, storageId, filter);