/// Initializes an empty database. /// /// \param db The database to initialize. /// /// \return The metadata record written into the new database. /// /// \throw store::error If there is a problem initializing the database. store::metadata store::detail::initialize(sqlite::database& db) { PRE(empty_database(db)); const fs::path schema = schema_file(); std::ifstream input(schema.c_str()); if (!input) throw error(F("Cannot open database schema '%s'") % schema); LI(F("Populating new database with schema from %s") % schema); const std::string schema_string = utils::read_stream(input); try { db.exec(schema_string); const metadata metadata = metadata::fetch_latest(db); LI(F("New metadata entry %s") % metadata.timestamp()); if (metadata.schema_version() != detail::current_schema_version) { UNREACHABLE_MSG(F("current_schema_version is out of sync with " "%s") % schema); } return metadata; } catch (const store::integrity_error& e) { // Could be raised by metadata::fetch_latest. UNREACHABLE_MSG("Inconsistent code while creating a database"); } catch (const sqlite::error& e) { throw error(F("Failed to initialize database: %s") % e.what()); } }
int DeviceDatabaseBackend::AddDevice(const Device& device) { QMutexLocker l(db_->Mutex()); QSqlDatabase db(db_->Connect()); ScopedTransaction t(&db); // Insert the device into the devices table QSqlQuery q("INSERT INTO devices (" " unique_id, friendly_name, size, icon," " transcode_mode, transcode_format)" " VALUES (:unique_id, :friendly_name, :size, :icon," " :transcode_mode, :transcode_format)", db); q.bindValue(":unique_id", device.unique_id_); q.bindValue(":friendly_name", device.friendly_name_); q.bindValue(":size", device.size_); q.bindValue(":icon", device.icon_name_); q.bindValue(":transcode_mode", device.transcode_mode_); q.bindValue(":transcode_format", device.transcode_format_); q.exec(); if (db_->CheckErrors(q)) return -1; int id = q.lastInsertId().toInt(); // Create the songs tables for the device QString filename(":schema/device-schema.sql"); QFile schema_file(filename); if (!schema_file.open(QIODevice::ReadOnly)) qFatal("Couldn't open schema file %s", filename.toUtf8().constData()); QString schema = QString::fromUtf8(schema_file.readAll()); schema.replace("%deviceid", QString::number(id)); db_->ExecSchemaCommands(db, schema, 0, true); t.Commit(); return id; }
void Database::ExecSchemaCommandsFromFile(QSqlDatabase& db, const QString& filename, int schema_version, bool in_transaction) { // Open and read the database schema QFile schema_file(filename); if (!schema_file.open(QIODevice::ReadOnly)) qFatal("Couldn't open schema file %s", filename.toUtf8().constData()); ExecSchemaCommands(db, QString::fromUtf8(schema_file.readAll()), schema_version, in_transaction); }
bool XMLValidator::isValid(const String & filename, const String & schema, std::ostream & os) { filename_ = filename; os_ = &os; //try to open file if (!File::exists(filename)) { throw Exception::FileNotFound(__FILE__, __LINE__, __PRETTY_FUNCTION__, filename); } // initialize parser try { XMLPlatformUtils::Initialize(); } catch (const XMLException & toCatch) { throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, "", String("Error during initialization: ") + Internal::StringManager().convert(toCatch.getMessage())); } SAX2XMLReader * parser = XMLReaderFactory::createXMLReader(); parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true); parser->setFeature(XMLUni::fgSAX2CoreValidation, true); parser->setFeature(XMLUni::fgXercesDynamic, false); parser->setFeature(XMLUni::fgXercesSchema, true); parser->setFeature(XMLUni::fgXercesSchemaFullChecking, true); //set this class as error handler parser->setErrorHandler(this); parser->setContentHandler(NULL); parser->setEntityResolver(NULL); //load schema LocalFileInputSource schema_file(Internal::StringManager().convert(schema)); parser->loadGrammar(schema_file, Grammar::SchemaGrammarType, true); parser->setFeature(XMLUni::fgXercesUseCachedGrammarInParse, true); // try to parse file LocalFileInputSource source(Internal::StringManager().convert(filename.c_str())); try { parser->parse(source); delete(parser); } catch (...) { /// nothing to do here } return valid_; }