<commit_before>//
//  SQLiteHistoryExtension.swift
//  Client
//
//  Created by <NAME> on 11/18/15.
//  Copyright © 2015 Cliqz. All rights reserved.
//

import Foundation
import Shared
extension SQLiteHistory: ExtendedBrowserHistory {
    
    public func count() -> Int {
        var count = 0
        self.getOldestVisitDate()
        let countSQL = "SELECT COUNT(\(TableHistory).id) AS rowCount FROM \(TableHistory) "
        
        let resultSet = db.runQuery(countSQL, args: nil, factory: SQLiteHistory.countFactory).value
        if let data = resultSet.successValue {
            count = data[0]!
        }
        return count
    }
    
    public func getOldestVisitDate() -> NSDate? {
        var oldestVisitDate: NSDate?
        
        let visitsSQL = "SELECT MIN(\(TableVisits).date) AS date FROM \(TableVisits) "
        
        let resultSet = db.runQuery(visitsSQL, args: nil, factory: SQLiteHistory.dateFactory).value
        if let data = resultSet.successValue {
            oldestVisitDate = data[0]
        }
        return oldestVisitDate
    }
    
    public func alterVisitsTableAddFavoriteColumn() {
        
        if !isColumnExist(TableVisits, columnName: "favorite") {
            self.db.run([
                ("ALTER TABLE \(TableVisits) ADD COLUMN 'favorite' BOOL NOT NULL DEFAULT 0", nil)
                ])
        }
    }
    
    public func setHistoryFavorite(ids: [Int], value: Bool) -> Success {
        let idsCommaSeparated = ids.map{String($0)}.joinWithSeparator(",")
        let favorite = value ? 1:0
        return self.db.run([
            ("UPDATE \(TableVisits) SET favorite = \(favorite) WHERE id in (\(idsCommaSeparated))", nil)
            ])
    }
    
    public func clearHistory(favorite: Int) -> Success {
        return self.db.run([
            ("DELETE FROM \(TableVisits) WHERE favorite = \(favorite)", nil),
            ("DELETE FROM \(TableHistory) WHERE id NOT IN (SELECT distinct(siteID) FROM \(TableVisits))", nil),
            ("DELETE FROM \(TableDomains) WHERE id NOT IN (SELECT (domain_id) FROM \(TableHistory))", nil),
            self.favicons.getCleanupCommands(),
            ])
            // We've probably deleted a lot of stuff. Vacuum now to recover the space.
            >>> effect(self.db.vacuum)
    }
    
    public func removeHistory(ids: [Int]) -> Success {
        let idsCommaSeparated = ids.map{String($0)}.joinWithSeparator(",")

        return self.db.run([
            ("DELETE FROM \(TableVisits) WHERE id in \(idsCommaSeparated)", nil),
            ("DELETE FROM \(TableHistory) WHERE id NOT IN (SELECT distinct(siteID) FROM \(TableVisits))", nil),
            ("DELETE FROM \(TableDomains) WHERE id NOT IN (SELECT (domain_id) FROM \(TableHistory))", nil),
            self.favicons.getCleanupCommands(),
            ])
    }
    
    public func getHistoryVisits(limit: Int) -> Deferred<Maybe<Cursor<Site>>> {
        let args: Args?
        args = []
        
        let historySQL =
        "SELECT \(TableVisits).id, \(TableVisits).date, \(TableVisits).favorite, \(TableHistory).url, \(TableHistory).title " +
            "FROM \(TableHistory) " +
            "INNER JOIN \(TableVisits) ON \(TableVisits).siteID = \(TableHistory).id " +
            "ORDER BY \(TableVisits).id DESC " +<commit_msg>Fixed bug related to removing history urls.<commit_after>//
//  SQLiteHistoryExtension.swift
//  Client
//
//  Created by <NAME> on 11/18/15.
//  Copyright © 2015 Cliqz. All rights reserved.
//

import Foundation
import Shared
extension SQLiteHistory: ExtendedBrowserHistory {
    
    public func count() -> Int {
        var count = 0
        self.getOldestVisitDate()
        let countSQL = "SELECT COUNT(\(TableHistory).id) AS rowCount FROM \(TableHistory) "
        
        let resultSet = db.runQuery(countSQL, args: nil, factory: SQLiteHistory.countFactory).value
        if let data = resultSet.successValue {
            count = data[0]!
        }
        return count
    }
    
    public func getOldestVisitDate() -> NSDate? {
        var oldestVisitDate: NSDate?
        
        let visitsSQL = "SELECT MIN(\(TableVisits).date) AS date FROM \(TableVisits) "
        
        let resultSet = db.runQuery(visitsSQL, args: nil, factory: SQLiteHistory.dateFactory).value
        if let data = resultSet.successValue {
            oldestVisitDate = data[0]
        }
        return oldestVisitDate
    }
    
    public func alterVisitsTableAddFavoriteColumn() {
        
        if !isColumnExist(TableVisits, columnName: "favorite") {
            self.db.run([
                ("ALTER TABLE \(TableVisits) ADD COLUMN 'favorite' BOOL NOT NULL DEFAULT 0", nil)
                ])
        }
    }
    
    public func setHistoryFavorite(ids: [Int], value: Bool) -> Success {
        let idsCommaSeparated = ids.map{String($0)}.joinWithSeparator(",")
        let favorite = value ? 1:0
        return self.db.run([
            ("UPDATE \(TableVisits) SET favorite = \(favorite) WHERE id in (\(idsCommaSeparated))", nil)
            ])
    }
    
    public func clearHistory(favorite: Int) -> Success {
        return self.db.run([
            ("DELETE FROM \(TableVisits) WHERE favorite = \(favorite)", nil),
            ("DELETE FROM \(TableHistory) WHERE id NOT IN (SELECT distinct(siteID) FROM \(TableVisits))", nil),
            ("DELETE FROM \(TableDomains) WHERE id NOT IN (SELECT (domain_id) FROM \(TableHistory))", nil),
            self.favicons.getCleanupCommands(),
            ])
            // We've probably deleted a lot of stuff. Vacuum now to recover the space.
            >>> effect(self.db.vacuum)
    }
    
    public func removeHistory(ids: [Int]) -> Success {
        let idsCommaSeparated = ids.map{String($0)}.joinWithSeparator(",")

        return self.db.run([
            ("DELETE FROM \(TableVisits) WHERE id in (\(idsCommaSeparated))", nil),
            ("DELETE FROM \(TableHistory) WHERE id NOT IN (SELECT distinct(siteID) FROM \(TableVisits))", nil),
            ("DELETE FROM \(TableDomains) WHERE id NOT IN (SELECT (domain_id) FROM \(TableHistory))", nil),
            self.favicons.getCleanupCommands(),
            ])
    }
    
    public func getHistoryVisits(limit: Int) -> Deferred<Maybe<Cursor<Site>>> {
        let args: Args?
        args = []
        
        let historySQL =
        "SELECT \(TableVisits).id, \(TableVisits).date, \(TableVisits).favorite, \(TableHistory).url, \(TableHistory).title " +
            "FROM \(TableHistory) " +
            "INNER JOIN \(TableVisits) ON \(TableVisits).siteID = \(TableHistory).id " +
            "ORDER BY \(TableVisits).id DESC " +
{'fraction_non_alphanumeric': 0.12398312744802652, 'fraction_numerical': 0.005423320277191925, 'mean_word_length': 2.219689621726479, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4220897', 'n_tokens_mistral': 1930, 'n_tokens_neox': 1881, 'n_words': 514}