<commit_before>//
//  MyDatabase.swift
//  baranov
//
//  Created by Ivan on 07/07/15.
//  Copyright (c) 2015 Rabotyaga. All rights reserved.
//

import UIKit
import SQLite

class MyDatabase {
    
    static let sharedInstance = MyDatabase()
    
    let dbFilename = "articles.db"
    
    let db: Database
    let articles_table: Query
    var search_history_table: Query
    
    let articles_table_name = "articles"
    let search_history_table_name = "search_history"
    
    let nr = Expression<Int64>("nr")
    let ar_inf = Expression<String>("ar_inf")
    let ar_inf_wo_vowels = Expression<String>("ar_inf_wo_vowels")
    let transcription = Expression<String>("transcription")
    let translation = Expression<String>("translation")
    let root = Expression<String>("root")
    let form = Expression<String>("form")
    let vocalization = Expression<String?>("vocalization")
    let homonym_nr = Expression<Int64?>("homonym_nr")
    let opt = Expression<String>("opt")
    let ar1 = Expression<String>("ar1")
    let ar2 = Expression<String>("ar2")
    let ar3 = Expression<String>("ar3")
    let mn1 = Expression<String>("mn1")
    let mn2 = Expression<String>("mn2")
    let mn3 = Expression<String>("mn3")
    
    let updated_at = Expression<NSDate>("updated_at")
    let search_string = Expression<String>("search_string")
    let details_string = Expression<String>("details_string")
    
    let matchAttr = [NSBackgroundColorAttributeName : UIColor.matchBg()]
    let translationSizeAttr = [NSFontAttributeName : UIFont.translationFont()]
    let arabicAttr = [NSForegroundColorAttributeName : UIColor.arabicText()]
    
    static let arabicVowels = "[\\u064b\\u064c\\u064d\\u064e\\u064f\\u0650\\u0651\\u0652\\u0653\\u0670]"
    let arabicVowelsPattern = "\(arabicVowels)*"
    static let arabicTextPattern = "[\\p{Arabic}]+((\\s*~)*(\\s*[\\p{Arabic}]+)+)*"
    let arabicTextRegex = NSRegularExpression(pattern: arabicTextPattern, options: nil, error: nil)!
    
    static let anyAlifPattern = "[\\u0622\\u0623\\u0625\\u0627]" //alif-madda, alif-hamza, hamza-alif, alif
    static let anyWawPattern = "[\\u0624\\u0648]" //waw-hamza, waw
    static let anyYehPattern = "[\\u0626\\u0649]" //yeh-hamza, yeh
    let anyAlifRegex = NSRegularExpression(pattern: anyAlifPattern, options: nil, error: nil)!
    let anyWawRegex = NSRegularExpression(pattern: anyWawPattern, options: nil, error: nil)!
    let anyYehRegex = NSRegularExpression(pattern: anyYehPattern, options: nil, error: nil)!
    
    let lastArticleNr: Int64?
    
    private init() {
        let sourceFilename = NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent(dbFilename)
        let destinationPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as! String
        let destinationFilename = destinationPath.stringByAppendingPathComponent(dbFilename)
        var error: NSError?
        
        // nested func to allow code reuse in init
        // before stored properties db & articles_table are initialized
        func copyDatabase() {
            if(!NSFileManager.defaultManager().copyItemAtPath(sourceFilename, toPath: destinationFilename, error: &error)) {
                println("Couldn't copy database: \(error!.localizedDescription)")
            }
        }
        
        if (!NSFileManager.defaultManager().fileExistsAtPath(destinationFilename)) {
            copyDatabase()
        } else {
            let documentsDb = Database(destinationFilename)
            let resourcesDb = Database(sourceFilename)
            if (resourcesDb.userVersion > documentsDb.userVersion) {
                if(!NSFileManager.defaultManager().removeItemAtPath(destinationFilename, error: &error)) {
                    println("Couldn't remove old database in Documents directory: \(error!.localizedDescription)")
                }
                copyDatabase()
            }<commit_msg>setting NSURLIsExcludedFromBackupKey for db after copy<commit_after>//
//  MyDatabase.swift
//  baranov
//
//  Created by Ivan on 07/07/15.
//  Copyright (c) 2015 Rabotyaga. All rights reserved.
//

import UIKit
import SQLite

class MyDatabase {
    
    static let sharedInstance = MyDatabase()
    
    let dbFilename = "articles.db"
    
    let db: Database
    let articles_table: Query
    var search_history_table: Query
    
    let articles_table_name = "articles"
    let search_history_table_name = "search_history"
    
    let nr = Expression<Int64>("nr")
    let ar_inf = Expression<String>("ar_inf")
    let ar_inf_wo_vowels = Expression<String>("ar_inf_wo_vowels")
    let transcription = Expression<String>("transcription")
    let translation = Expression<String>("translation")
    let root = Expression<String>("root")
    let form = Expression<String>("form")
    let vocalization = Expression<String?>("vocalization")
    let homonym_nr = Expression<Int64?>("homonym_nr")
    let opt = Expression<String>("opt")
    let ar1 = Expression<String>("ar1")
    let ar2 = Expression<String>("ar2")
    let ar3 = Expression<String>("ar3")
    let mn1 = Expression<String>("mn1")
    let mn2 = Expression<String>("mn2")
    let mn3 = Expression<String>("mn3")
    
    let updated_at = Expression<NSDate>("updated_at")
    let search_string = Expression<String>("search_string")
    let details_string = Expression<String>("details_string")
    
    let matchAttr = [NSBackgroundColorAttributeName : UIColor.matchBg()]
    let translationSizeAttr = [NSFontAttributeName : UIFont.translationFont()]
    let arabicAttr = [NSForegroundColorAttributeName : UIColor.arabicText()]
    
    static let arabicVowels = "[\\u064b\\u064c\\u064d\\u064e\\u064f\\u0650\\u0651\\u0652\\u0653\\u0670]"
    let arabicVowelsPattern = "\(arabicVowels)*"
    static let arabicTextPattern = "[\\p{Arabic}]+((\\s*~)*(\\s*[\\p{Arabic}]+)+)*"
    let arabicTextRegex = NSRegularExpression(pattern: arabicTextPattern, options: nil, error: nil)!
    
    static let anyAlifPattern = "[\\u0622\\u0623\\u0625\\u0627]" //alif-madda, alif-hamza, hamza-alif, alif
    static let anyWawPattern = "[\\u0624\\u0648]" //waw-hamza, waw
    static let anyYehPattern = "[\\u0626\\u0649]" //yeh-hamza, yeh
    let anyAlifRegex = NSRegularExpression(pattern: anyAlifPattern, options: nil, error: nil)!
    let anyWawRegex = NSRegularExpression(pattern: anyWawPattern, options: nil, error: nil)!
    let anyYehRegex = NSRegularExpression(pattern: anyYehPattern, options: nil, error: nil)!
    
    let lastArticleNr: Int64?
    
    private init() {
        let sourceFilename = NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent(dbFilename)
        let destinationPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as! String
        let destinationFilename = destinationPath.stringByAppendingPathComponent(dbFilename)
        var error: NSError?
        
        // nested func to allow code reuse in init
        // before stored properties db & articles_table are initialized
        func copyDatabase() {
            if (!NSFileManager.defaultManager().copyItemAtPath(sourceFilename, toPath: destinationFilename, error: &error)) {
                println("Couldn't copy database: \(error!.localizedDescription)")
            }
            let Url = NSURL.fileURLWithPath(destinationFilename)!
            if (!Url.setResourceValue(true, forKey: NSURLIsExcludedFromBackupKey, error: &error)) {
                println("Error excluding \(Url.lastPathComponent) from backup \(error!.localizedDescription)")
            }
        }
        
        if (!NSFileManager.defaultManager().fileExistsAtPath(destinationFilename)) {
            copyDatabase()
        } else {
            let documentsDb = Database(destinationFilename)
            let resourcesDb = Database(sourceFilename)
            if (resourcesDb.userVersion > documentsDb.userVersion) {
                if(!NSFileManager.defaultManager().removeItemAtPath(destinationFilename, error: &error)) {
                    println("Couldn't remove old database in Documents directory: \(error!.localizedDescription)")
                }
                copyDatabase()
            }
{'fraction_non_alphanumeric': 0.11715226136779228, 'fraction_numerical': 0.023162257710593687, 'mean_word_length': 3.3247232472324724, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 43, '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': '887826', 'n_tokens_mistral': 2549, 'n_tokens_neox': 2418, 'n_words': 515}