[Include] IDB - Remove boilerplate code when using SQLite - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Filterscripts (
https://sampforum.blast.hk/forumdisplay.php?fid=17)
+---- Forum: Includes (
https://sampforum.blast.hk/forumdisplay.php?fid=83)
+---- Thread: [Include] IDB - Remove boilerplate code when using SQLite (
/showthread.php?tid=615438)
IDB - Remove boilerplate code when using SQLite -
Scranton - 22.08.2016
IDB
I created this in an attempt to remove excess code around SQL queries. I come from languages where you can pass anonymous functions, which are damn useful. I've tried to emulate it in PAWN.
The code is on
github, or you can view it
directly.
Readme is on github.
Quick example, loop through all results of a query:
PHP код:
idb_all(values[2][MAX_PLAYER_NAME], "SELECT `name`, `points` FROM `users` WHERE `points` > 100;") {
printf("%s has %s points", values[0], values[1]);
}
Comments & pull requests welcome
Respuesta: IDB - Remove boilerplate code when using SQLite -
DarkChildren - 22.08.2016
great!!
Re: IDB - Remove boilerplate code when using SQLite -
Stinged - 22.08.2016
Looks good but I don't find it appealing for optimization.
I prefer writing a couple of more lines than to use this because of the following:
PHP код:
idb_all(values[3][128], "SELECT `string`, `integer`, `float` FROM `table`")
{
// 128 cells for a string (that needs 128 cells), not wasted
// 128 cells for an integer, wasted
// 128 cells for a float, wasted
// 128 * 2 = 256 cells wasted
}
new DBResult: result = db_query(database, "SELECT `string`, `integer`, `float` FROM `table`");
if (db_num_rows(result))
{
new str[128], ival, Float: fval;
db_get_field(result, 0, str, 128);
ival = db_get_field_int(result, 1);
fval = db_get_field_float(result, 2);
// Nothing is wasted
}
db_free_result(result);
Plus, adding to that, you could also reuse arrays.
PHP код:
new
str[128],
DBResult: result = db_query(database, "SELECT `string`, `integer`, `float` FROM `table`");
if (db_num_rows(result))
{
new ival, Float: fval;
db_get_field(result, 0, str, 128);
ival = db_get_field_int(result, 1);
fval = db_get_field_float(result, 2);
// Nothing is wasted
}
db_free_result(result);
format(str, sizeof (str), "String reused ...", ...);
Re: IDB - Remove boilerplate code when using SQLite -
Scranton - 22.08.2016
Yeah I agree, if you want efficiency you should use the natives directly