diff options
Diffstat (limited to 'src/main/java/org/openslx/imagemaster/db/MySQL.java')
-rw-r--r-- | src/main/java/org/openslx/imagemaster/db/MySQL.java | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/src/main/java/org/openslx/imagemaster/db/MySQL.java b/src/main/java/org/openslx/imagemaster/db/MySQL.java index a31046d..ebc44c7 100644 --- a/src/main/java/org/openslx/imagemaster/db/MySQL.java +++ b/src/main/java/org/openslx/imagemaster/db/MySQL.java @@ -23,6 +23,12 @@ class MySQL private static final Logger log = Logger.getLogger( MySQL.class ); private static Database db = null; + /** + * Static initializer for setting up the database connection. + * This gets called implicitly as soon as the clas loader loads + * the class. In most cases that happens when the class is being + * accessed for the first time during run time. + */ static { // Load connection info from class (TODO: Make pretty) @@ -67,17 +73,45 @@ class MySQL } } + /** + * Get a list of objects of the given class from the database. + * The class needs a matching constructor for the query you pass in, i.e. number of + * arguments has to be equal to number of columns returned by query. + * + * @param clazz The class to instanciate for the result(s) + * @param sql The sql query to run + * @param args Any number of arguments to the query (using the '?' placeholder) + * @return A list containing the rows returned by the query, represented by the given class + */ protected static <T> List<T> findAll( final Class<T> clazz, final String sql, final Object... args ) { return db.findAll( clazz, sql, args ); } + /** + * Run a query on the database that will return at most one result. + * If the query returns a row, it will be used to instanciate the given class. If + * it doesn't return a row, null will be returned. + * + * @param clazz The class to instanciate for the result (if any) + * @param sql The sql query to run + * @param args Any number of arguments to the query (using the '?' placeholder) + * @return Instance of clazz or null + */ protected static <T> T findUniqueOrNull( final Class<T> clazz, final String sql, final Object... args ) { return db.findUniqueOrNull( clazz, sql, args ); } - protected static int update(String query, Object... args) { - return db.update(query, args); + /** + * Run an update on the database, return number of rows affected. + * + * @param sql The update/insert query to run + * @param args Any number of arguments to the query (using the '?' placeholder) + * @return Number of rows affected by query + */ + protected static int update( String sql, Object... args) { + return db.update( sql, args ); } } + |