/// <inheritdoc /> public async Task <bool> SetConfigValueAsync <T>(string name, T value, IConnectionFactory connectionFactory, CancellationToken token) { // we will need the string var stringValue = ConfigTypeToString(value); var current = await GetConfigValueAsync <object>(name, null, connectionFactory, token).ConfigureAwait(false); var sql = null == current ? $"INSERT INTO {Tables.Config} (name, value) values (@name, @value)" : $"UPDATE {Tables.Config} SET value=@value WHERE name=@name"; // do it now. using (var cmd = connectionFactory.CreateCommand(sql)) { var pName = cmd.CreateParameter(); pName.DbType = DbType.String; pName.ParameterName = "@name"; cmd.Parameters.Add(pName); var pValue = cmd.CreateParameter(); pValue.DbType = DbType.String; pValue.ParameterName = "@value"; cmd.Parameters.Add(pValue); pName.Value = name; pValue.Value = stringValue; return(0 != await connectionFactory.ExecuteWriteAsync(cmd, token).ConfigureAwait(false)); } }
/// <inheritdoc /> public async Task <T> GetConfigValueAsync <T>(string name, T defaultValue, IConnectionFactory connectionFactory, CancellationToken token) { var sql = $"SELECT value FROM {Tables.Config} WHERE name=@name"; using (var cmd = connectionFactory.CreateCommand(sql)) { var pName = cmd.CreateParameter(); pName.DbType = DbType.String; pName.ParameterName = "@name"; cmd.Parameters.Add(pName); pName.Value = name; var value = await connectionFactory.ExecuteReadOneAsync(cmd, token).ConfigureAwait(false); if (null == value || value == DBNull.Value) { return(defaultValue); } // the value is saved as a string so we need to convert it to whatever. // otherwise we will just throw... return(ConfigStringToType <T>((string)value)); } }
/// <summary> /// </summary> /// <param name="sql"></param> /// <param name="connectionFactory"></param> /// <param name="token"></param> /// <returns></returns> private async Task <bool> ExecuteNonQueryAsync(string sql, IConnectionFactory connectionFactory, CancellationToken token = default(CancellationToken)) { try { using (var command = connectionFactory.CreateCommand(sql)) { await connectionFactory.ExecuteWriteAsync(command, token).ConfigureAwait(false); } return(true); } catch (Exception ex) { // log this _logger.Exception(ex); // did not work. return(false); } }
/// <summary> /// Check if the table exists. /// </summary> /// <param name="name"></param> /// <param name="connectionFactory"></param> /// <returns></returns> protected bool TableExists(string name, IConnectionFactory connectionFactory) { var sql = $"SELECT name FROM sqlite_master WHERE type='table' AND name='{name}';"; using (var command = connectionFactory.CreateCommand(sql)) using (var reader = connectionFactory.ExecuteReadAsync(command, default(CancellationToken)).GetAwaiter().GetResult()) { try { while (reader.Read()) { return(true); } } finally { reader.Close(); } } return(false); }
/// <inheritdoc /> public async Task <IList <IPendingFolderUpdate> > GetPendingFolderUpdatesAsync(long limit, IConnectionFactory factory, CancellationToken token) { if (null == factory) { throw new ArgumentNullException(nameof(factory), "You have to be within a tansaction when calling this function."); } // the pending updates var pendingUpdates = new List <IPendingFolderUpdate>(); try { // we want to get the latest updated folders. var sql = $"SELECT fu.folderid as folderid, fu.type as type, f.path as path FROM {Tables.FolderUpdates} fu, {Tables.Folders} f WHERE f.id=fu.folderid " + $"ORDER BY fu.ticks DESC LIMIT { limit}"; using (var cmd = factory.CreateCommand(sql)) using (var reader = await factory.ExecuteReadAsync(cmd, token).ConfigureAwait(false)) { var folderIdPos = reader.GetOrdinal("folderid"); var pathPos = reader.GetOrdinal("path"); var typePos = reader.GetOrdinal("type"); while (reader.Read()) { // get out if needed. token.ThrowIfCancellationRequested(); // the folder id var folderId = (long)reader[folderIdPos]; // the directory for that folder var directory = new DirectoryInfo((string)reader[pathPos]); // the update type var type = (UpdateType)(long)reader[typePos]; // Get the files currently on record this can be null if we have nothing. // if the folder was just created we are not going to bother getting more data. var filesOnRecord = type == UpdateType.Created ? new List <FileInfo>() : await _files.GetFilesAsync(folderId, token).ConfigureAwait(false); // add this update pendingUpdates.Add(new PendingFolderUpdate( folderId, directory, filesOnRecord, type )); } } } catch (OperationCanceledException) { _logger.Warning("Received cancellation request - Get pending folder updates"); throw; } catch (Exception e) { _logger.Exception(e); return(null); } // return whatever we found return(pendingUpdates); }
/// <inheritdoc /> public async Task <IList <IPendingFileUpdate> > GetPendingFileUpdatesAsync(long limit, IConnectionFactory connectionFactory, CancellationToken token) { if (null == connectionFactory) { throw new ArgumentNullException(nameof(connectionFactory), "You have to be within a tansaction when calling this function."); } // the pending updates var pendingUpdates = new List <IPendingFileUpdate>(); try { // we want to get the latest updated folders. var sql = "SELECT fu.fileid as fileid, fu.type as type, f.name as name, fo.path FROM " + $"{Tables.FileUpdates} fu, {Tables.Files} f, {Tables.Folders} fo " + $"WHERE f.id = fu.fileid and fo.id = f.folderid ORDER BY fu.ticks DESC LIMIT {limit}"; using (var cmd = connectionFactory.CreateCommand(sql)) using (var reader = await connectionFactory.ExecuteReadAsync(cmd, token).ConfigureAwait(false)) { var fileIdPos = reader.GetOrdinal("fileid"); var typePos = reader.GetOrdinal("type"); var pathPos = reader.GetOrdinal("path"); var namePos = reader.GetOrdinal("name"); while (reader.Read()) { // get out if needed. token.ThrowIfCancellationRequested(); // the file id var fileid = (long)reader[fileIdPos]; // the update type var type = (UpdateType)(long)reader[typePos]; // the folder path var path = (string)reader[pathPos]; // the file name var name = (string)reader[namePos]; // add this update pendingUpdates.Add(new PendingFileUpdate( fileid, new FileInfo(Path.Combine(path, name)), type )); } } // deleted files are not found here, this is because // the previous query looks for left join for the file name/directory name // if the files is deleted... they do not exist anymore. if (pendingUpdates.Count < limit) { sql = $"SELECT fileid FROM {Tables.FileUpdates} WHERE type={(int)UpdateType.Deleted} ORDER BY ticks DESC LIMIT {limit - pendingUpdates.Count}"; using (var cmd = connectionFactory.CreateCommand(sql)) using (var reader = await connectionFactory.ExecuteReadAsync(cmd, token).ConfigureAwait(false)) { var fileIdPos = reader.GetOrdinal("fileid"); while (reader.Read()) { // get out if needed. token.ThrowIfCancellationRequested(); // the file id var fileid = (long)reader[fileIdPos]; // add this update pendingUpdates.Add(new PendingFileUpdate( fileid, null, UpdateType.Deleted )); } } } // return whatever we found return(pendingUpdates); } catch (OperationCanceledException) { _logger.Warning("Received cancellation request - Getting pending updates."); throw; } catch (Exception e) { _logger.Exception(e); return(null); } }