myadm

Simple MySQL client for the terminal
git clone git://git.bitsmanent.org/myadm
Log | Files | Refs | README | LICENSE

commit d237edbb05fb09afee7584402e7b8ba468b447d7
parent 22580e3e391aaa2af82bc04e07b5124ce379ebbe
Author: Claudio Alessi <smoppy@gmail.com>
Date:   Sat, 14 May 2016 16:50:30 +0200

Simplify mksql_* routines.

Diffstat:
Mmyadm.c | 19++++++++-----------
1 file changed, 8 insertions(+), 11 deletions(-)

diff --git a/myadm.c b/myadm.c @@ -431,7 +431,7 @@ mksql_alter_table(char *sql, char *tbl) { MYSQL_RES *res; Item *items, *item; char sqlfds[MAXQUERYLEN+1]; - int fi = 0, size = MAXQUERYLEN+1, len, r; + int size = MAXQUERYLEN+1, len = 0, r; r = mysql_exec("describe `%s`", tbl); if(r == -1 || !(res = mysql_store_result(mysql))) @@ -439,14 +439,13 @@ mksql_alter_table(char *sql, char *tbl) { mysql_items(res, &items); mysql_free_result(res); for(item = items; item; item = item->next) { - /* XXX key and extra (auto_increment) */ - len = snprintf(&sqlfds[fi], size, "\n%cMODIFY %s %s %sNULL%s%s%s%s", - fi ? ',' : ' ', item->cols[0], item->cols[1], + /* XXX missing keys and indexes */ + /* XXX quote default when needed */ + len += snprintf(&sqlfds[len], size - len, "\n%cMODIFY %s %s %sNULL%s%s%s%s", + len ? ',' : ' ', item->cols[0], item->cols[1], (!strcmp(item->cols[2], "NO") ? "NOT " : ""), *item->cols[4] ? " DEFAULT " : "", item->cols[4], *item->cols[5] ? " " : "", item->cols[5]); - size -= len; - fi += len; } cleanupitems(&items); snprintf(sql, MAXQUERYLEN+1, "ALTER TABLE `%s`%s", tbl, sqlfds); @@ -456,16 +455,14 @@ void mksql_update_record(char *sql, Item *item, Field *fields, char *tbl, char *pk) { Field *fld; char *pkv = NULL, sqlfds[MAXQUERYLEN+1], col[MAXQUERYLEN*2+1]; - int size = MAXQUERYLEN+1, fi = 0, len, i; + int size = MAXQUERYLEN+1, len, i; for(i = 0, fld = fields; fld; fld = fld->next, ++i) { if(!pkv && !strncmp(pk, fld->name, fld->len)) pkv = item->cols[i]; escape(col, item->cols[i], item->lens[i], '\'', 0); - len = snprintf(&sqlfds[fi], size, "\n%c`%s` = '%s'", - fi ? ',' : ' ', fld->name, col); - size -= len; - fi += len; + len += snprintf(&sqlfds[len], size - len, "\n%c`%s` = '%s'", + len ? ',' : ' ', fld->name, col); } snprintf(sql, MAXQUERYLEN+1, "UPDATE `%s` SET%s\nWHERE `%s` = '%s'", tbl, sqlfds, pk, pkv);