Skip to content

Commit

Permalink
auto creation of DB for main playdar app, helper script to convert .s…
Browse files Browse the repository at this point in the history
…ql to .h
  • Loading branch information
RJ committed Apr 28, 2009
1 parent 51a9a69 commit 4405044
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 16 deletions.
15 changes: 9 additions & 6 deletions etc/gen_schema.h.sh
@@ -1,29 +1,32 @@
#!/bin/bash
schema=$1
guard="__PLAYDAR_SCHEMA_H__"
if [ -e "$schema" ]
then
cat <<EOF
#ifndef $guard
#define $guard
/*
This file was automatically generated from $schema on `date`.
*/
#include <string>
namespace playdar {
namespace sql {
std::string playdar_db()
{
std::string sql =
static const char * sql =
EOF
awk '!/^-/ && length($0) {gsub("\"","\\\"",$0); printf("\"%s\"\n",$0);}' "$schema"
awk '!/^-/ && length($0) {gsub(/[ \t]+$/, "", $0); gsub("\"","\\\"",$0); printf("\"%s\"\n",$0);}' "$schema"
cat <<EOF
;
const char * get_sql()
{
return sql;
}
}} // namespace
#endif
EOF
else
echo "Usage: $0 <schema.sql>"
Expand Down
26 changes: 17 additions & 9 deletions etc/schema.sql
Expand Up @@ -41,7 +41,7 @@ CREATE TABLE IF NOT EXISTS track_search_index (
ngram TEXT NOT NULL,
id INTEGER NOT NULL REFERENCES track(id) ON DELETE CASCADE ON UPDATE CASCADE,
num INTEGER NOT NULL DEFAULT 1
);
);
CREATE UNIQUE INDEX track_search_index_ngram_track ON track_search_index(ngram, id);


Expand Down Expand Up @@ -85,14 +85,22 @@ CREATE TABLE IF NOT EXISTS playdar_auth (
permissions TEXT NOT NULL
);

-- Settings
-- Schema version, and misc playdar settings

CREATE TABLE IF NOT EXISTS playdar_settings (
ns TEXT,
name TEXT NOT NULL,
value TEXT,
defaultvalue TEXT NOT NULL,
description TEXT NOT NULL
CREATE TABLE IF NOT EXISTS playdar_system (
key TEXT NOT NULL PRIMARY KEY,
value TEXT NOT NULL DEFAULT ''
);
CREATE UNIQUE INDEX playdar_settings_idx ON playdar_settings(ns,value);
INSERT INTO playdar_system(key,value) VALUES('schema_version', '1');

-- Settings NOT USED

--CREATE TABLE IF NOT EXISTS playdar_settings (
-- ns TEXT,
-- name TEXT NOT NULL,
-- value TEXT,
-- defaultvalue TEXT NOT NULL,
-- description TEXT NOT NULL
--);
--CREATE UNIQUE INDEX playdar_settings_idx ON playdar_settings(ns,value);

2 changes: 2 additions & 0 deletions includes/playdar/library.h
Expand Up @@ -136,6 +136,8 @@ class Library
template <typename T> T db_get_one(std::string sql, T def);

private:
void check_db();
void create_db_schema();
sqlite3pp::database m_db;
boost::mutex m_mut;
std::string m_dbfilepath;
Expand Down
60 changes: 59 additions & 1 deletion src/library.cpp
Expand Up @@ -11,23 +11,81 @@

#include "playdar/application.h"
#include "playdar/playable_item.hpp"
#include "playdar/library_sql.h"

using namespace std;

namespace playdar {


Library::Library(const string& dbfilepath)
: m_db(dbfilepath.c_str())
: m_db( dbfilepath.c_str() )
{
m_dbfilepath = dbfilepath;
// confirm DB is correct version, or create schema if first run
check_db();
cout << "library DB opened ok" << endl;
}

Library::~Library()
{
cout << "DTOR library" << endl;
}

void
Library::check_db()
{
try
{
sqlite3pp::query qry(m_db, "SELECT value FROM playdar_system WHERE key = 'schema_version'");
sqlite3pp::query::iterator i = qry.begin();
if( i == qry.end() )
{
// unusual - table exists but doesn't contain this row.
// could have been created wrongly
cerr << "Errror, playdar_system table missing schema_version key!" << endl
<< "Maybe you created the database wrong, or it's corrupt." << endl
<< "Try deleting it and re-scanning?" << endl;
throw; // not caught here.
}
string val = (*i).get<string>(0);
cout << "Database schema detected as version " << val << endl;
// check the schema version is what we expect
// TODO auto-upgrade to newest schema version as needed.
if( val != "1" )
{
cerr << "Schema version too old. TODO handle auto-upgrades" << endl;
throw; // not caught here
}
// OK.
}
catch(sqlite3pp::database_error err)
{
// probably doesn't exist yet, try and create it
//
cout << "database_error: " << err.what() << endl;
create_db_schema();
}

}

void
Library::create_db_schema()
{
cout << "Attempting to create DB schema..." << endl;
string sql( playdar::sql::get_sql() );
vector<string> statements;
boost::split( statements, sql, boost::is_any_of(";") );
BOOST_FOREACH( string s, statements )
{
boost::trim( s );
if(s.empty()) continue;
cout << "Executing: " << s << endl;
sqlite3pp::command cmd(m_db, s.c_str());
cmd.execute();
}
cout << "Schema created." << endl;
}

bool
Library::remove_file( const string& url )
Expand Down

0 comments on commit 4405044

Please sign in to comment.