Mailing List Archive

[PATCH] big cleanup of dbmysql.c
The attached patch tries to make dbmysql.c more maintainable by grouping
duplicated code into functions and rewriting some SQL statements to use
JOIN. In total, it removes 877 lines of code without making any
user-visible changes.

I made the db_select() function as a wrapper to snprintf, db_query and
mysql_store_result. Before this patch, every function with a SELECT
query had almost the same ~12 lines of code to:
1. snprintf the sql statement into query. Some of them trace()d it
too, which isn't needed because db_query() does this. None of
them checked the return value of snprintf, which could lead to
security holes.
2. call db_query and handle errors. Some functions just returned on
error, others trace()d the same error message that db_query()
had just traced() before returning.
3. call mysql_store_result() and trace() an error if something went
wrong. Some functions used mysql_error() here, others didn't.
All those places are now changed to call db_select(), which cuts down
many lines of code and makes them all behave identically.

The db_icheck_*() functions contained both duplicated and unneeded code.
By rewriting them to use a bit more advanced SQL syntax I was able to
save hundreds of lines of C code.

The diff was made against the 20030420 CVS snapshot with the
dbmail-filters patch I sent yesterday applied. Although it weighs in at
50KB it's non-intrusive, but it increases maintainability a lot.

Please apply.

--
Jonas Jensen <jbj@knef.dk>
Re: [PATCH] big cleanup of dbmysql.c [ In reply to ]
Wow! sounds great!

i don't have the time to look into the new code (easter blahblah) but i
certainly will shortly. Cleaning up the code was one of our objectives
but due to lack of time we never came to it..

regards roel


Jonas Jensen heeft op zondag, 20 apr 2003 om 18:08 (Europe/Amsterdam)
het volgende geschreven:

> The attached patch tries to make dbmysql.c more maintainable by
> grouping
> duplicated code into functions and rewriting some SQL statements to use
> JOIN. In total, it removes 877 lines of code without making any
> user-visible changes.
>
> I made the db_select() function as a wrapper to snprintf, db_query and
> mysql_store_result. Before this patch, every function with a SELECT
> query had almost the same ~12 lines of code to:
> 1. snprintf the sql statement into query. Some of them trace()d it
> too, which isn't needed because db_query() does this. None of
> them checked the return value of snprintf, which could lead to
> security holes.
> 2. call db_query and handle errors. Some functions just returned
> on
> error, others trace()d the same error message that db_query()
> had just traced() before returning.
> 3. call mysql_store_result() and trace() an error if something
> went
> wrong. Some functions used mysql_error() here, others didn't.
> All those places are now changed to call db_select(), which cuts down
> many lines of code and makes them all behave identically.
>
> The db_icheck_*() functions contained both duplicated and unneeded
> code.
> By rewriting them to use a bit more advanced SQL syntax I was able to
> save hundreds of lines of C code.
>
> The diff was made against the 20030420 CVS snapshot with the
> dbmail-filters patch I sent yesterday applied. Although it weighs in at
> 50KB it's non-intrusive, but it increases maintainability a lot.
>
> Please apply.
>
> --
> Jonas Jensen <jbj@knef.dk>
> <dbmail-mysql-cleanup.patch.gz>

_________________________
R.A. Rozendaal
ICT Manager
IC&S
T: +31 30 2322878
F: +31 30 2322305
www.ic-s.nl
Re: [PATCH] big cleanup of dbmysql.c [ In reply to ]
Here is the second part of the cleanup patch, introducing db_select() in
the files I missed the first time.

I also made some functions static in dbsearchmysql.c because they were
only used in that one file.

--
Jonas Jensen <jbj@knef.dk>