~ubuntu-branches/ubuntu/jaunty/imms/jaunty

« back to all changes in this revision

Viewing changes to immscore/sqldb2.cc

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Veber
  • Date: 2005-04-13 23:43:11 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050413234311-kzr68z9l7z5mv551
Tags: 2.0.3-2
Build depend on xmms-dev (>= 1.2.10+cvs20050209)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <iostream>
 
2
#include <sstream>
 
3
 
 
4
#include <unistd.h>
 
5
#include <sys/stat.h>
 
6
 
 
7
#include <sqlite3.h>
 
8
 
 
9
#include "strmanip.h"
 
10
#include "sqldb2.h"
 
11
#include "utils.h"
 
12
 
 
13
using std::endl;
 
14
using std::cerr;
 
15
using std::ostringstream;
 
16
 
 
17
// Fuzzy compare function using levenshtein string distance
 
18
static void fuzzy_like(sqlite3_context *context, int argc, sqlite3_value** val)
 
19
{
 
20
    if (argc != 2)
 
21
        throw SQLException("fuzzy_like", "argc != 2");
 
22
    sqlite3_result_int(context, string_like(
 
23
                (char*)sqlite3_value_text(val[0]),
 
24
                (char*)sqlite3_value_text(val[1]), 4));
 
25
}
 
26
 
 
27
extern sqlite3 *db();
 
28
 
 
29
SqlDb::SqlDb()
 
30
{
 
31
    if (!access(get_imms_root("imms.db").c_str(), R_OK)
 
32
            && access(get_imms_root("imms2.db").c_str(), F_OK))
 
33
    {
 
34
        cerr << string(60, '*') << endl;
 
35
        cerr << "Old database format detected, "
 
36
            "will attempt an upgrade..." << endl;
 
37
        ostringstream command;
 
38
        command << "sqlite " << get_imms_root("imms.db")
 
39
            << " .dump | sqlite3 " << get_imms_root("imms2.db") << endl;
 
40
        cerr << "Running: " << command.str() << endl;
 
41
        system(command.str().c_str());
 
42
        cerr << "If you see errors above verify that you have *both*"
 
43
            " sqlite 2.8.x" << endl;
 
44
        cerr << "and 3.0.x installed and rerun the command by hand." << endl;
 
45
        cerr << string(60, '*') << endl;
 
46
    }
 
47
 
 
48
    dbcon.open(get_imms_root("imms2.db"));
 
49
    sqlite3_create_function(db(), "similar", 2, 1, 0, fuzzy_like, 0, 0);
 
50
}
 
51
 
 
52
SqlDb::~SqlDb()
 
53
{
 
54
    close_database();
 
55
}
 
56
 
 
57
void SqlDb::close_database()
 
58
{
 
59
    dbcon.close();
 
60
}
 
61
 
 
62
int SqlDb::changes()
 
63
{
 
64
    return db() ? sqlite3_changes(db()) : 0;
 
65
}