Meekro-Like Database Library

I’ve been working on this for a while to make some of my own stuff easier to write, so I thought I might as well release it as a library. It’s very similar to the PHP Meekro library, which aims to make escaping data unobtrusive, and querying more simple.

It has a driver system for adding new modules. I’ve used mysqloo as a demo. The default is always mysqloo, and can be changed by altering the meekro.Driver global. Everything that needs to be escaped, is escaped for you.

License:


—[[

The AceCool License

I WILL NOT sue you.
I WILL NOT be an asshole.
I WILL NOT plague you with SQF style code.
The least you can do is not try and claim you wrote this, but credit is not required - and neither is inclusion of this license in your own source file.

]]—

Changelog:



Version 3:

- Bug fix with format
- Added table exists

Version 2:

- Added delete
- Added last insert
- Added drop table
- Added drop database


Connection:

In order to initialize and connect, run this:

[lua]
meekro.init() – init current driver
meekro.connect(“host”, “username”, “password”, “database”, 3306) – connect, port optional
[/lua]

You can also enable printing of built queries to console to see why MySQL errors are being created.

[lua]
meekro.debug(true)
[/lua]

Formatter:

The available replacements in a formatted string are as following.



%s - escaped string
%i - whole integer
%d - decimal/float
%b - mysql style boolean (1 or 0)
%l - no escaping, to string


Error Handling:

When running a query/insert/update/etc, you handle errors like so.

[lua]
local result = meekro.query(“SELECT * FROM wow”)
if meekro.error then
error(result)
else
PrintTable(result)
end
[/lua]

Last insert

The last insert value is updated after every type of query, regardless of any actual insertions. Reference the following variable.

[lua]
meekro.lastInsert
[/lua]

Download:

Development Version 3
Release Version 3

The release version is minified, coming just over 4500 characters.

Examples:

Insert:

[lua]
meekro.insert(“my_table”, {name = “bill nye”, “age” = 25})
[/lua]

Update:

[lua]
local nameSearch = “bill nye”
meekro.update(“my_table”, {“age” = 30}, “name=%s”, nameSearch)
[/lua]

Query:

[lua]
meekro.query(“SELECT * FROM my_table WHERE age > %i”, 25)
[/lua]

Query Row:

[lua]
meekro.queryRow(“SELECT * FROM my_table WHERE age > %i”, 25)
[/lua]

Delete:

[lua]
meekro.delete(“my_table”,"account_age > 25 OR account_name = %s", “i hate you”)
[/lua]

Drop Table:

[lua]
meekro.dropTable(“my_table”)
[/lua]

Drop Database:

[lua]
meekro.dropDatabase(“my_database”)
[/lua]

Table Exists:

[lua]
meekro.tableExists(“my_database”)
[/lua]

nice license; but I think MIT might be what you’re looking for.

i prefer acecool license

Acecool license looks similar to the license I’ve created for SeriousRP:

I’m impressed with Acecool license since it took me quite a lot more words to get effectively the same thing across…

With my license, you can claim you created it but only if it is different and you’re not allowed to say that mine is an illegal copy of (your edited copy of mine).

If you steal my AceCool license, I’ll sue you!

I really like the way you set up the drivers so you can easily change to another one. (which you should, mysqloo is terrible)
What I don’t like though is that you are using query:wait() after each query.
I guess you did it this way because php is using blocking mysql operations.
The thing is though, most dbms don’t guarantee a realtime response. For all you know it could take ages to complete one query. And during that time waiting the server freezes completely.
Now for most queries that time waiting is insignificant (~1ms), but since there are gmod coders at work, you can bet they won’t set up their indexes properly. Then you can easily get 10-100ms of waiting time per query and at that point you can notice the lag.

Php only gets away with it because the people accessing a homepage need the data before displaying the webpage and because multiple people can still access the homepage concurrently since each sessions can run in different threads.

I did it purely out of simplicity. And so I could add SQLLite, because that has no async support. if you add async support to the library. I’ll gladly implement it.

[lua]
meekro.async(true)
[/lua]

And then activate a callback parameter on each of the query/queryRow functions.