Пример #1
0
Ref<Database> PackageService::buildLookupDB(const String& path, StreamSourceMap& packCfgs)
{
	Ref<Database> db = Database::open(path);

	db->exec("DROP TABLE IF EXISTS packs; CREATE TABLE packs (name PRIMARY KEY, timestamp INT)");
	db->exec("DROP TABLE IF EXISTS lookup; CREATE TABLE lookup (type, key, subtype, pack, entry, PRIMARY KEY (type, key))");
	db->exec("DROP INDEX IF EXISTS lookup_packidx; CREATE INDEX lookup_packidx ON lookup (pack ASC)");

	Ref<Database::Query> packInsert = 
		db->prepare("INSERT INTO packs (name, timestamp) VALUES (?, ?)");

	Ref<Database::Query> lookupInsert = 
		db->prepare("INSERT INTO lookup (type, key, subtype, pack, entry) VALUES ($type, $key, $subtype, $pack, $entry)");

	for (StreamSourceMap::iterator itr = packCfgs.begin(), end = packCfgs.end(); itr != end; ++itr)
	{
		const String& packName = itr->first;
		Ref<Settings> cfg = Settings::load(itr->second);

		if (cfg == NULL)
		{
			LOG(0, "*** can't load '%s'\n", cfg->getUrl().c_str());
			continue;
		}

		Timestamp time = itr->second->getTimestamp();

		packInsert->reset();
		packInsert->bind(1, packName);
		packInsert->bind(2, time.getUnixTime64());

		packInsert->step();

		Ref<Settings> section = cfg->getSection("lookup");
		if (section == NULL)
			continue;

		for (Settings::Iterator itr = section->begin(), end = section->end(); itr != end; ++itr)
		{
			String type = itr->first;
			String key = itr->second;

			String sub, entry;

			splitLookupEntry(type, key, sub, entry);

			lookupInsert->reset();
			lookupInsert->bind("$type", type);
			lookupInsert->bind("$key", key);
			lookupInsert->bind("$subtype", sub);
			lookupInsert->bind("$entry", entry);
			lookupInsert->bind("$pack", packName);

			lookupInsert->step();
		}
	}

	return db;
}