Skip to content

Ablility to use :memory: and URI filename in db_open - #547

Closed
AmyrAhmady wants to merge 2 commits into
masterfrom
amir/inmemory-db
Closed

AmyrAhmady wants to merge 2 commits into
masterfrom
amir/inmemory-db

Conversation

@AmyrAhmady

@AmyrAhmady AmyrAhmady commented Dec 26, 2022 •

Copy link
Copy Markdown
Member

Ablility to use :memory: and URI filename in db_open.
for example: db_open("file::memory:?cache=shared") to have a shared-cached in-memory database

@AmyrAhmady AmyrAhmady changed the title add db_open_raw and DB_OpenRaw Ablility to use :memory: and URI filename in db_open Dec 28, 2022
@Y-Less

Y-Less commented Dec 28, 2022 •

Copy link
Copy Markdown
Collaborator

I was working on a version of this as well:

SCRIPT_API(db_open, int(const std::string& name))
{
	int flags = 0x02 /* SQLITE_OPEN_READWRITE */ | 0x04 /* SQLITE_OPEN_CREATE */;
	size_t start;
	size_t end;

	// Get the flags.
	auto params = GetParams();
	if ((params[0] / sizeof(cell)) >= 2)
	{
		// Optional parameter.
		flags = params[2];
	}

	// Get the protocol.
	String protocol = name.substr(0, 5);
	if (protocol == "file:")
	{
		// We always use an aboslute path, even when a relative path is given.
		protocol = "file:///";
		// URI mode.
		if (name[5] == '/' && name[6] == '/')
		{
			// Skip the next `/`.
			start = name.find('/', 7) + 1;
			if (start == 0)
			{
				return 0;
			}
		}
		else
		{
			start = 5;
		}
	}
	else
	{
		protocol = "";
		start = 0;
	}

	// Get the parameters.
	String parameters;
	end = name.find('?', start);
	if (end == std::string::npos)
	{
		parameters = "";
		end = name.length();
	}
	else
	{
		// There are parameters.
		parameters = name.substr(end);
	}

	// Get the path.
	String path = name.substr(start, end - start);

	// Get the database.
	IDatabaseConnection* database_connection;
	if ((flags & 0x80 /* SQLITE_OPEN_MEMORY */) != 0 || path == ":memory:" || path == "" || parameters.find("mode=memory") != std::string::npos)
	{
		// All the ways of opening an in-memory database that I know of.
		database_connection = PawnManager::Get()->databases->open(name);
	}
	else
	{
		// TODO: Pass in the flags.
		ghc::filesystem::path dbFilePath = ghc::filesystem::absolute("scriptfiles/" + path);
		database_connection = PawnManager::Get()->databases->open(protocol + dbFilePath.string() + parameters);
	}
	return database_connection ? database_connection->getID() : 0;
}

@AmyrAhmady

Copy link
Copy Markdown
Member Author

The reason I didn't go that way to support all possible in-memory database methods, is to support opening databases in a more standard way, support everything and not just absolute DB path on disk
https://www.sqlite.org/c3ref/open.html#urifilenameexamples
https://www.sqlite.org/inmemorydb.html

@Y-Less Y-Less mentioned this pull request Dec 28, 2022
@AmyrAhmady

Copy link
Copy Markdown
Member Author

Closing this in favor of #553

@AmyrAhmady AmyrAhmady closed this Dec 28, 2022
@AmyrAhmady
AmyrAhmady deleted the amir/inmemory-db branch December 28, 2022 13:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants