/////////////////////////////////////////////////////////////////// public FileAttributes Read(string path) { // Sanitize the path; remove the last '/' if (path != null && path != "/" && path.EndsWith("/")) { path = path.TrimEnd('/'); } if (!GetPathFlag(path)) { return(null); } FileAttributes attr = null; // We need to quote any 's that appear in the strings // (int particular, in the path) string directory = FileSystem.GetDirectoryNameRootOk(path).Replace("'", "''"); string filename = Path.GetFileName(path).Replace("'", "''"); lock (connection) { ReadCommand.Parameters.AddWithValue("@dir", directory); ReadCommand.Parameters.AddWithValue("@fname", filename); using (SqliteDataReader reader = SqliteUtils.ExecuteReaderOrWait(ReadCommand)) { if (SqliteUtils.ReadOrWait(reader)) { attr = GetFromReader(reader); } } } return(attr); }
private static List <FavoriteEntry> GetBookMarksFromDBPath(string dbPath) { var favorites = new List <FavoriteEntry>(); DataTable favoriteDt = null; try { var getBookmarksCommand = new SQLiteCommand( @"select bookmarks.title,places.url,bookmarks.dateAdded from moz_bookmarks as bookmarks, moz_places as places on bookmarks.fk = places.id;"); favoriteDt = SqliteUtils.QueryDataTable(dbPath, getBookmarksCommand); for (int i = 0; i < favoriteDt.Rows.Count; i++) { var row = favoriteDt.Rows[i]; var title = row["title"].ToString(); var time = DateUtils.GetTimeFromNewCentery((long)row["dateAdded"] / TimeToSecondDevideCount); var uri = new Uri(row["url"].ToString()); var favorite = new FavoriteEntry(uri, title, time); favorites.Add(favorite); } } catch (Exception ex) { Logger.WriteLine(ex.Message); return(favorites); } finally { favoriteDt?.Dispose(); } return(favorites); }
// FIXME: I don't really like this, the collection could be huge public ArrayList GetOlderThan(DateTime dt) { SqliteCommand command; SqliteDataReader reader; lock (connection) { command = new SqliteCommand(); command.Connection = connection; command.CommandText = "SELECT uid FROM mapping WHERE last_seen < " + StringFu.DateTimeToString(dt); reader = SqliteUtils.ExecuteReaderOrWait(command); ArrayList uids = new ArrayList(); while (SqliteUtils.ReadOrWait(reader)) { uids.Add(reader.GetString(0)); } reader.Close(); command.Dispose(); return(uids); } }
public bool Get(string uid, out uint flags) { SqliteCommand command; SqliteDataReader reader; lock (connection) { command = new SqliteCommand(); command.Connection = connection; command.CommandText = String.Format( "SELECT flags FROM mapping WHERE uid='{0}'", uid.Replace("'", "''")); reader = SqliteUtils.ExecuteReaderOrWait(command); try { if (SqliteUtils.ReadOrWait(reader)) { flags = (uint)reader.GetInt32(0); return(true); } else { flags = 0; return(false); } } finally { reader.Close(); command.Dispose(); } } }
/////////////////////////////////////////////////////////////////// // Return all attributes in the attributes database, used for merging private ICollection ReadAllAttributes() { ArrayList attributes = new ArrayList(); SqliteCommand command; SqliteDataReader reader; lock (connection) { command = new SqliteCommand(); command.Connection = connection; command.CommandText = "SELECT unique_id, directory, filename, last_mtime, last_attrtime, filter_name, filter_version " + "FROM file_attributes"; reader = SqliteUtils.ExecuteReaderOrWait(command); while (SqliteUtils.ReadOrWait(reader)) { attributes.Add(GetFromReader(reader)); } reader.Close(); command.Dispose(); } return(attributes); }
private void MaybeStartTransaction_Unlocked() { if (transaction_state == TransactionState.Requested) { SqliteUtils.DoNonQuery(connection, "BEGIN"); } transaction_state = TransactionState.Started; }
public void Checkpoint() { lock (connection) { // Commit any outstanding changes and open a new transaction SqliteUtils.DoNonQuery(connection, "COMMIT"); SqliteUtils.DoNonQuery(connection, "BEGIN"); } }
public EvolutionSummaryTracker(string directory, string account_name, string folder_name) { // Make the on-disk files for folders have sane names folder_name = folder_name.Replace('/', '-'); folder_name = folder_name.Replace(':', '_'); folder_name = folder_name.Replace(',', ' '); // Causes problems with the ConnectionString otherwise string filename = Path.Combine(directory, String.Format("SummaryTracker-{0}-{1}.db", account_name, folder_name)); bool create_new_db = !File.Exists(filename); bool purge_old_db = false; connection = GetConnection(filename); try { connection.Open(); } catch (ApplicationException) { purge_old_db = true; } if (!create_new_db && !purge_old_db) { // Run a dummy SELECT statement to catch more errors // indicating sqlite version mismatches. using (SqliteCommand command = new SqliteCommand()) { command.Connection = connection; command.CommandText = "SELECT flags FROM mapping WHERE uid = 'fo/ky'"; SqliteDataReader reader; try { reader = SqliteUtils.ExecuteReaderOrWait(command); reader.Close(); } catch (ApplicationException) { purge_old_db = true; } } } if (purge_old_db) { connection.Dispose(); // Purge the old database and create a new one File.Delete(filename); connection = GetConnection(filename); connection.Open(); create_new_db = true; } if (create_new_db) { CreateDatabase(); } // Start a transaction for any updates SqliteUtils.DoNonQuery(connection, "BEGIN"); }
public void AddLinks(Uri uri, IList <string> links) { lock (connection) { MaybeStartTransaction_Unlocked(); UpdateLinksCommand.Parameters.AddWithValue("@uri", UriToString(uri)); UpdateLinksCommand.Parameters.AddWithValue("@links", GetLinksText(links)); SqliteUtils.DoNonQuery(UpdateLinksCommand); } }
public void CommitTransaction() { if (transaction_state == TransactionState.Started) { lock (connection) SqliteUtils.DoNonQuery(connection, "COMMIT"); } transaction_state = TransactionState.None; }
public void Remove(string uid) { lock (connection) { SqliteUtils.DoNonQuery(connection, "DELETE FROM mapping WHERE uid=@uid", new string [] { "@uid" }, new object [] { uid }); } }
public void Close() { lock (connection) { // Commit any outstanding changes SqliteUtils.DoNonQuery(connection, "COMMIT"); connection.Close(); connection = null; } }
private void Insert(Uri uri, string filename, byte[] data) { lock (connection) { MaybeStartTransaction_Unlocked(); InsertCommand.Parameters.AddWithValue("@uri", UriToString(uri)); InsertCommand.Parameters.AddWithValue("@filename", filename); InsertCommand.Parameters.AddWithValue("@data", data); SqliteUtils.DoNonQuery(InsertCommand); } }
public void Update(string uid, uint flags) { lock (connection) { SqliteUtils.DoNonQuery(connection, "INSERT OR REPLACE INTO mapping " + " (uid, flags, last_seen) " + " VALUES (@uid, @flags, @last_seen)", new string [] { "@uid", "@flags", "@last_seen" }, new object [] { uid, flags, StringFu.DateTimeToString(DateTime.UtcNow) }); } }
public void Flush() { lock (connection) { if (transaction_count > 0) { Logger.Log.Debug("Flushing requested -- committing sqlite transaction"); SqliteUtils.DoNonQuery(connection, "COMMIT"); transaction_count = 0; } } }
private void CreateDatabase() { SqliteUtils.DoNonQuery(connection, "CREATE TABLE mapping ( " + " uid TEXT UNIQUE, " + " flags INTEGER NOT NULL, " + " last_seen TEXT NOT NULL " + ")"); SqliteUtils.DoNonQuery(connection, "CREATE INDEX mapping_uid on mapping (uid)"); }
static Metric() { var time = new Func <double, string> (d => String.Format("{0:N0}", SqliteUtils.FromDbFormat(typeof(DateTime), d))); var duration = new Func <double, string> (d => String.Format("{0:N0}", TimeSpan.FromMilliseconds(d))); var duration_s = new Func <double, string> (d => String.Format("{0:N0}", TimeSpan.FromSeconds(d))); var px = new Func <double, string> (d => String.Format("{0:N0} px", d)); Add( "/AvgBitRate", new Func <double, string> (d => String.Format("{0:N0} kbps", d)), "/AvgScore", "/BpmTrackCount", "/ComposerTrackCount", "/ErrorTrackCount", "/GroupingTrackCount", "/LicenseUriTrackCount", "/RatedTrackCount", "/TotalFileSize", new Func <double, string> (d => new FileSizeQueryValue((long)d).ToUserQuery()), "/TotalPlayCount", "/TotalPlaySeconds", duration_s, "/TotalSkipCount", "/TrackCount", "/UnplayedTrackCount", "Banshee/BuildTime", time, "Banshee/Configuration/browser/position", px, "Banshee/Configuration/player_window/height", px, "Banshee/Configuration/player_window/source_view_row_height", px, "Banshee/Configuration/player_window/source_view_row_padding", px, "Banshee/Configuration/player_window/source_view_width", px, "Banshee/Configuration/player_window/width", px, "Banshee/Configuration/player_window/x_pos", "Banshee/Configuration/player_window/y_pos", "Banshee/Configuration/plugins.mtp/albumart_max_width", px, "Banshee/Configuration/plugins.play_queue/played_songs_number", "Banshee/Configuration/plugins.play_queue/upcoming_songs_number", "Banshee/Display/NScreens", "Banshee/Screen/Height", px, "Banshee/Screen/Width", px, "Banshee/Screen/NMonitors", "Banshee/ShutdownAt", time, "Banshee/StartedAt", time, "Env/Processor Count", "Banshee/RunDuration", duration ); }
public ICollection <HistoryEntry> GetBrowserHistories(DateTime?startTime, DateTime?endTime) { List <HistoryEntry> entryList = new List <HistoryEntry>(); string query = string.Format("SELECT url, title, visit_count, datetime(visit_date/1000000,'unixepoch') AS last_visit " + "FROM {0}, {1} " + "WHERE {0}.id = {1}.place_id", TABLE_NAME, VISITS_TABLE_NAME); foreach (string dbPath in _firefoxDatabasePaths) { DataTable historyDt = SqliteUtils.QueryDataTable(dbPath, new SQLiteCommand(query)); foreach (DataRow row in historyDt.Rows) { Uri uri; DateTime lastVisit; string title; int? visitCount; lastVisit = DateTime.Parse(row["last_visit"].ToString()).ToLocalTime(); if (!DateUtils.IsEntryInTimelimit(lastVisit, startTime, endTime)) { continue; } try { uri = new Uri(row["url"].ToString(), UriKind.Absolute); } catch (UriFormatException) { continue; } title = row["title"].ToString(); title = string.IsNullOrEmpty(title) ? null : title; visitCount = int.TryParse(row["visit_count"].ToString(), out int outVal) ? (int?)outVal : null; HistoryEntry entry = new HistoryEntry(uri, title, lastVisit, visitCount, Browser.Firefox); entryList.Add(entry); } } return(entryList); }
// Returns raw path as stored in the db i.e. relative path wrt the text_cache_dir private string LookupPathRawUnlocked(Uri uri) { //SqliteCommand command; string path = null; LookupPathCommand.Parameters.AddWithValue("@uri", UriToString(uri)); using (SqliteDataReader reader = SqliteUtils.ExecuteReaderOrWait(LookupPathCommand)) { if (SqliteUtils.ReadOrWait(reader)) { path = reader.GetString(0); } } return(path); }
public void Delete(Uri uri) { lock (connection) { string path = LookupPathRawUnlocked(uri); if (path != null) { MaybeStartTransaction_Unlocked(); DeleteCommand.Parameters.AddWithValue("@uri", UriToString(uri)); SqliteUtils.DoNonQuery(DeleteCommand); if (path != SELF_CACHE_TAG && path != BLOB_TAG) { File.Delete(Path.Combine(text_cache_dir, path)); } } } }
public override void Step(object[] args, int stepNumber, ref object contextData) { List <T> list = null; if (contextData == null) { contextData = list = new List <T> (); } else { list = contextData as List <T>; } var val = (T)SqliteUtils.FromDbFormat(typeof(T), args[0]); list.Add(val); }
public static IServiceProvider ConfigureServices() { //setup our DI var serviceProvider = new ServiceCollection() .AddLogging() .AddDbContext <DataContext>(options => options.UseSqlite(SqliteConnection)) .AddScoped(typeof(IRepository <>), typeof(RepositoryImpl <>)) .AddScoped(typeof(IRepository <,>), typeof(RepositoryImpl <,>)) .BuildServiceProvider(); // database var context = serviceProvider.GetService <DataContext>(); SqliteUtils.CreateDatabaseIfNotExists(context, DATABASE_NAME); return(serviceProvider); }
void Awake() { current_ = this; PostAwake( ); if (!SqliteUtils.IsInitialised( )) { if (SqliteUtils.DEBUG_SQL) { Debug.Log("No SqliteUtils in " + this.GetType( ).ToString( )); } SqliteUtils.Instance.databaseLoadComplete += OnDatabasesLoaded; SqliteUtils.Instance.initialiseDatabases("English"); } else { OnDatabasesLoaded( ); } }
protected IEnumerator CheckForUpdates() { var targetVersion = this.config.Version; var versionKey = this.config.VersionPrefsKey; var currentVersion = PlayerPrefs.GetInt(versionKey); if (currentVersion == targetVersion) { yield break; } //Open connection: var connectionUri = SqliteUtils.GetConnectionUri(this.config.DatabaseName); var connection = new SqliteConnection(connectionUri); connection.Open(); if (connection.State != ConnectionState.Open) { throw new Exception($"Can't connect to db {connectionUri}!"); } //Update database version: for (var version = currentVersion + Int.ONE; version <= targetVersion; version++) { var hasError = new Reference <bool>(); yield return(this.UpdateDatabase(version, connection, hasError)); if (hasError.value) { connection.Close(); connection.Dispose(); throw new Exception($"Can't update db to {version} version!"); } PlayerPrefs.SetInt(versionKey, targetVersion); } //Close connection: connection.Close(); connection.Dispose(); }
protected virtual IEnumerator InitializeInternal() { var databaseName = this.config.DatabaseName; var versionKey = this.config.VersionPrefsKey; var originPath = SqliteUtils.GetOriginPath(databaseName); var targetPath = SqliteUtils.GetTargetPath(databaseName); if (!PlayerPrefs.HasKey(versionKey)) { yield return(SqliteUtils.ReinstallDatabase(originPath, targetPath)); PlayerPrefs.SetInt(versionKey, this.config.Version); yield break; } yield return(SqliteUtils.InstallDatabaseIfAbsent(originPath, targetPath)); yield return(this.CheckForUpdates()); }
public bool Write(FileAttributes fa) { SetPathFlag(fa.Path); int ret = 0; string filter_name; // We need to quote any 's that appear in the strings // (in particular, in the path) lock (connection) { // If a transaction has been requested, start it now. MaybeStartTransaction(); filter_name = fa.FilterName; if (filter_name == null) { filter_name = ""; } filter_name = filter_name.Replace("'", "''"); string[] param = new string [] { "@unique_id", "@directory", "@filename", "@last_mtime", "@last_attrtime", "@filter_name", "@filter_version" }; object[] vals = new object [] { GuidFu.ToShortString(fa.UniqueId), fa.Directory.Replace("'", "''"), fa.Filename.Replace("'", "''"), StringFu.DateTimeToString(fa.LastWriteTime), StringFu.DateTimeToString(fa.LastAttrTime), filter_name, fa.FilterVersion }; for (int i = 0; i < param.Length; i++) { InsertCommand.Parameters.AddWithValue(param[i], vals[i]); } ret = SqliteUtils.DoNonQuery(InsertCommand); } return(ret != 0); }
public void Drop(string path) { // Sanitize the path; remove the last '/' if (path != null && path != "/" && path.EndsWith("/")) { path = path.TrimEnd('/'); } // We don't want to "UnSetPathFlag" here, since we have no way of knowing // if another path hashes to the same value as this one. // We need to quote any 's that appear in the strings // (in particular, in the path) string directory = FileSystem.GetDirectoryNameRootOk(path).Replace("'", "''"); string filename = Path.GetFileName(path).Replace("'", "''"); lock (connection) { // If a transaction has been requested, start it now. MaybeStartTransaction(); DeleteCommand.Parameters.AddWithValue("@directory", directory); DeleteCommand.Parameters.AddWithValue("@filename", filename); SqliteUtils.DoNonQuery(DeleteCommand); } }
public IList <string> GetLinks(Uri uri) { string links_text = null; List <string> links = null; lock (connection) { LookupLinksCommand.Parameters.AddWithValue("@uri", UriToString(uri)); using (SqliteDataReader reader = SqliteUtils.ExecuteReaderOrWait(LookupLinksCommand)) { if (!SqliteUtils.ReadOrWait(reader)) { return(null); } links_text = reader.GetString(0); } } if (String.IsNullOrEmpty(links_text)) { return(null); } return(links_text.Split(links_separator, StringSplitOptions.RemoveEmptyEntries)); }
// If self_cache is true when called, then self_cache will be set upon return public TextReader GetReader(Uri uri, ref bool self_cache) { byte[] blob = null; string filename = null; lock (connection) { LookupDataCommand.Parameters.AddWithValue("@uri", UriToString(uri)); using (SqliteDataReader reader = SqliteUtils.ExecuteReaderOrWait(LookupDataCommand)) { if (!SqliteUtils.ReadOrWait(reader)) { if (self_cache) { self_cache = false; } return(null); } filename = reader.GetString(0); if (!reader.IsDBNull(1)) { blob = reader.GetValue(1) as byte []; } } } if (filename == SELF_CACHE_TAG) { if (self_cache) { self_cache = true; return(null); } if (!uri.IsFile) { string msg = String.Format("non-file uri {0} flagged as self-cached", uri); throw new Exception(msg); } return(new StreamReader(uri.LocalPath)); } if (self_cache) { self_cache = false; } if (filename == BLOB_TAG && (blob == null || blob.Length == 0)) { return(null); } Stream stream; if (filename == BLOB_TAG) { stream = new MemoryStream(blob); } else { stream = new FileStream(Path.Combine(text_cache_dir, filename), FileMode.Open, FileAccess.Read, FileShare.ReadWrite); } stream = new GZipInputStream(stream); TextReader text_reader = new StreamReader(new BufferedStream(stream)); return(text_reader); }
public TextCache(string storage_dir, bool read_only) { text_cache_dir = Path.Combine(storage_dir, "TextCache"); if (!Directory.Exists(text_cache_dir)) { Directory.CreateDirectory(text_cache_dir); // Create our cache subdirectories. for (int i = 0; i < 256; ++i) { string subdir = i.ToString("x"); if (i < 16) { subdir = "0" + subdir; } subdir = Path.Combine(text_cache_dir, subdir); Directory.CreateDirectory(subdir); } } // Create our Sqlite database string db_filename = Path.Combine(text_cache_dir, "TextCache.db"); bool create_new_db = false; if (!File.Exists(db_filename)) { create_new_db = true; } // Funky logic here to deal with sqlite versions. // // When sqlite 3 tries to open an sqlite 2 database, // it will throw an SqliteException with SqliteError // NOTADB when trying to execute a command. // // When sqlite 2 tries to open an sqlite 3 database, // it will throw an ApplicationException when it // tries to open the database. try { connection = Open(db_filename); } catch (ApplicationException) { Logger.Log.Warn("Likely sqlite database version mismatch trying to open {0}. Purging.", db_filename); create_new_db = true; } if (!create_new_db) { // Run a dummy query to see if we get a NOTADB error. Sigh. SqliteCommand command; SqliteDataReader reader = null; command = new SqliteCommand(); command.Connection = connection; command.CommandText = "SELECT filename FROM textcache_data WHERE uri='blah'"; try { reader = SqliteUtils.ExecuteReaderOrWait(command); } catch (ApplicationException ex) { Logger.Log.Warn("Likely sqlite database version mismatch trying to read from {0}. Purging.", db_filename); create_new_db = true; } catch (SqliteException ex) { // When the table name changed from 0.2.18 -> 0.3.0. Logger.Log.Warn("Sqlite error: {0}. Purging textcache.", ex.Message); create_new_db = true; } if (reader != null) { reader.Dispose(); } command.Dispose(); } if (create_new_db) { if (connection != null) { connection.Dispose(); } if (read_only) { throw new UnauthorizedAccessException(String.Format("Unable to create read only text cache {0}", db_filename)); } File.Delete(db_filename); try { connection = Open(db_filename); } catch (Exception e) { Log.Debug(e, "Exception opening text cache {0}", db_filename); } // Database schema: uri, filename, data SqliteUtils.DoNonQuery(connection, "CREATE TABLE textcache_data ( " + " uri TEXT UNIQUE NOT NULL, " + " filename TEXT NOT NULL, " + " data BLOB " + ")"); } #if ENABLE_RDF_ADAPTER try { SqliteUtils.DoNonQuery(connection, "CREATE TABLE IF NOT EXISTS links_data ( " + " uri TEXT UNIQUE NOT NULL, "+ " links TEXT "+ ")"); } catch (SqliteException) { } #endif this.InitCommands(); }