FruityDB - an abstraction layer for MysqlOO

See https://github.com/wyozi/fruitydb/ for code, downloads and usage.

Practical example:



local db = FDB.Connect("sqlite", {})

hook.Add("PlayerInitialSpawn", "PlyInitSpawn", function(ply)
	db:Insert("connections", {
		sid = ply:SteamID(),
		time = os.time()	
	})
end)

concommand.Add("_lastconn", function(ply, cmd, args)
	db:QueryFirstField("SELECT time FROM connections WHERE sid = %s ORDER BY time DESC", {ply:SteamID()}, function(lc)
		ply:ChatPrint("You last connected " .. os.date("%d.%m.%Y %H:%M", lc))
	end)
end)


Where are the callback functions?

I’ll add QueryFirstRow etc. with callback functions later, but I haven’t had a big need for them yet. If you want them, you can fork the repo and send a pull request.

Naa, I already have my own database wrapper. But besides that it seems decent.

[editline]20th July 2013[/editline]

Actually I recommend that people do not use this, at all. All the functions rely on his BlockingQuery function which calls query:wait(). That’ll potentially time out all the people on a server as it halts the execution (pauses the entire server basically) untill the query has completed.

There is a non-blocking query function in there, so it’s inaccurate to say that all of the functions rely on a BlockingQuery.

In most of the cases where BlockingQuery will be used, it will be a negligible amount of time (less than a tenth of a second if it’s a local mysql server) as long as it isn’t overused. Can’t think of any good reason to use it over the non-blocking query, though.

And in case you handle it wrongly you’ll end up timing out everyone. If you think ever entirely halting the execution of your server just so you can not have to deal with callback functions (the proper way) is in any way acceptable then then you must not have a lot of experience with databases nor servers.

It’s outright wrong, there is just no way to justify it. The only time I’d ever recommend it is for the actual connecting to the database part and that’s not even a query that’s just establishing a link.

I wasn’t defending the use of BlockingQuery everywhere, I was bringing to mind the non-blocking query command that is easily available, as well as rationalizing the use of BlockingQuery (but the non-blocking one would be better to use in… just about any case I can think of.)

All the blocking methods are prefixed with either “Blocking” or “B” and you don’t have to use them. Even the documentations on each blocking one mention that they are blocking. I made the library for personal use first and the most annoying function chain caused by MysqlOO was in fact in my CheckPassword hook, which is why there are so many blocking queries due to CheckPassword’s inability to work with callback mysql functions. Query, Insert and Delete are all nonblocking methods and return the mysqloo query object for additional configuring, and still IMO make the library worth using, if you have many mysql calls.

Library updated with tmysql4 support and more sane API in some places. See github README or OP for some example code.

The library supports tmysql4, mysqloo and sqlite. Don’t believe the (unchangeable by me) title’s lies.