kiurubeans
Class SimpleDb

java.lang.Object
  |
  +--kiurubeans.SimpleDb

public class SimpleDb
extends java.lang.Object

Facilitates executing simple ad hoc queries using kotkabeans.DB.


Constructor Summary
SimpleDb()
          Creates new SimpleDb
 
Method Summary
static java.lang.String addSqlDeleted(StringPair[] tables)
          Returns "deleted" part in where clause.
static java.lang.String addSqlFrom(StringPair[] tables)
          returns "from" part of SQL
static boolean checkIfEditable(RS2 rs)
          Checks if user can edit current record & moves RS cursor to first record This should be called after every query before properties are assigned to bean.
static boolean executeTransaction(java.util.ArrayList a, java.lang.String dbLog)
          Like ExecuteTransaction(Statementcontainer[],String), but takes list instead of array.
static boolean executeTransaction(StatementContainer[] sc, java.lang.String dbLog)
          Executes array of prepared statements in a transaction.
static boolean executeTransaction(StatementContainer[] sc, java.lang.String dbLog, DB db)
          Executes array of prepared statements in a transaction.
static java.lang.String formatOrSet(java.lang.String fieldname, java.lang.String[] ids)
          Formats SQL search string from array of id numbers.
static java.lang.String formatSearchString(java.lang.String fieldName, java.lang.String searchStr)
          Formats SQL search string from user's input.
static java.lang.String formatSearchString(java.lang.String fieldName, java.lang.String searchStr, boolean beginAnd)
          Formats SQL search string from user's input.
static java.lang.String getDateTimeString(RS2 rs, java.lang.String column)
          Returns value of a timestamp field in compact notation.
static java.lang.String getString(RS2 rs, java.lang.String column)
          Filters off 'null' result strings from RS2.getString
static java.lang.String getString(RS2 rs, java.lang.String[] column)
          Filters off 'null' result strings from RS2.getString and combines them with space as delimiter
static boolean isEmpty(RS2 rs)
          Tests if rs2 is NULL or empty.
static boolean recordExists(DB db, java.lang.String table, java.lang.String idname, int idvalue)
          Checks if record exists of the given table.
static boolean recordExists(DB db, java.lang.String table, StringPair[] values)
          Checks if record exists of the given table.
static java.lang.String[] RS2toArray(RS2 rs, java.lang.String colname)
          Converts given column in RS2 to string array
static RS2 simpleQuery(java.lang.String logstring, java.lang.String querystring)
          Executes simple database query.
static int simpleUpdate(java.lang.String logstring, java.lang.String updatestring)
          Executes simple database update.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleDb

public SimpleDb()
Creates new SimpleDb

Method Detail

RS2toArray

public static java.lang.String[] RS2toArray(RS2 rs,
                                            java.lang.String colname)
                                     throws java.lang.Exception
Converts given column in RS2 to string array

Parameters:
rs - RS2 containing records
colname - Column name where we fetch values from
Returns:
String[]-array containing all values from column colname.
Throws:
java.lang.Exception - Something went wrong when handling RS2

simpleQuery

public static RS2 simpleQuery(java.lang.String logstring,
                              java.lang.String querystring)
                       throws java.lang.Exception
Executes simple database query.

Parameters:
logstring - Information for logging purposes.
querystring - Actual query.
Returns:
RS2 typed ResultSet containing results for the query.
Throws:
java.lang.Exception - Database query couldn't made successfully.

simpleUpdate

public static int simpleUpdate(java.lang.String logstring,
                               java.lang.String updatestring)
                        throws java.lang.Exception
Executes simple database update.

Parameters:
logstring - Information for logging purposes.
updatestring - Actual update sql clause.
Returns:
Returns amount of updated rows.
Throws:
java.lang.Exception - Database query couldn't made successfully.

recordExists

public static boolean recordExists(DB db,
                                   java.lang.String table,
                                   java.lang.String idname,
                                   int idvalue)
                            throws java.lang.Exception
Checks if record exists of the given table. Returns true even if there are deleted records. Assumes that db is valid and open! Caller is responsible for closing db!

Parameters:
db - Open database connection
table - Name of the table we are looking at.
idname - Column name of the primary key.
idvalue - Value of the primary key.
Returns:
True if record with given id exists in given table.
Throws:
java.lang.Exception - Something illegal happened during database query.

recordExists

public static boolean recordExists(DB db,
                                   java.lang.String table,
                                   StringPair[] values)
                            throws java.lang.Exception
Checks if record exists of the given table. (like recordExists(db,table,idname,value, but multiple id/value pairs) Assumes that db is valid and open! Caller is responsible for closing db!

Parameters:
db - Open database connection
table - Name of the table we are looking at.
values - array of StringPair containing column names and values
Returns:
True if record with given id exists in given table.
Throws:
java.lang.Exception - Something illegal happened during database query.

executeTransaction

public static boolean executeTransaction(java.util.ArrayList a,
                                         java.lang.String dbLog)
Like ExecuteTransaction(Statementcontainer[],String), but takes list instead of array. Assumes arraylist contains only StatementContainers

Parameters:
a - Arraylist containing StatementContainers.
dbLog - Log string
Returns:
Returns true, if transaction was successful.

executeTransaction

public static boolean executeTransaction(StatementContainer[] sc,
                                         java.lang.String dbLog)
Executes array of prepared statements in a transaction. Returns true on success

Parameters:
sc - StatementContainer containing database statements
dbLog - Log string
Returns:
Returns true, if transaction was successful.

executeTransaction

public static boolean executeTransaction(StatementContainer[] sc,
                                         java.lang.String dbLog,
                                         DB db)
Executes array of prepared statements in a transaction. Returns true on success

Parameters:
sc - StatementContainer containing database statements
dbLog - Log string
db - Open database connection
Returns:
Returns true, if transaction was successful.

checkIfEditable

public static boolean checkIfEditable(RS2 rs)
                               throws java.lang.Exception
Checks if user can edit current record & moves RS cursor to first record This should be called after every query before properties are assigned to bean.

Parameters:
rs - RS2
Returns:
true if properties can be assigned, false is RS2 is empty of record is deleted
Throws:
java.lang.Exception - Something illegal happened during database operations.

getString

public static java.lang.String getString(RS2 rs,
                                         java.lang.String column)
                                  throws java.lang.Exception
Filters off 'null' result strings from RS2.getString

Parameters:
rs - RS2 containing results
column - Which column we are interested in
Returns:
Returns value of the column or '-' if it is null
Throws:
java.lang.Exception - Something illegal happened during database operations

getDateTimeString

public static java.lang.String getDateTimeString(RS2 rs,
                                                 java.lang.String column)
                                          throws java.lang.Exception
Returns value of a timestamp field in compact notation.

Parameters:
rs - current resultset
column - column in resultset (should be of type timestamp)
Returns:
field value
Throws:
java.lang.Exception - Something illegal happened during database operations.

getString

public static java.lang.String getString(RS2 rs,
                                         java.lang.String[] column)
                                  throws java.lang.Exception
Filters off 'null' result strings from RS2.getString and combines them with space as delimiter

Parameters:
rs - RS2 containing results
column - Column names of the columns we are interested in.
Returns:
Values of the columns separated with spaces. Nulls are replaced with '-'s.
Throws:
java.lang.Exception - Something illegal happened during database operations.

formatSearchString

public static java.lang.String formatSearchString(java.lang.String fieldName,
                                                  java.lang.String searchStr)
Formats SQL search string from user's input. Assumes searchStr is not null. Result can be appended to SQL clause.

Parameters:
fieldName - Field name the user is searching.
searchStr - Search string
Returns:
Returns string that can be inserted in sql query clause.

formatSearchString

public static java.lang.String formatSearchString(java.lang.String fieldName,
                                                  java.lang.String searchStr,
                                                  boolean beginAnd)
Formats SQL search string from user's input. Assumes searchStr is not null. Result can be appended to SQL clause.

Parameters:
fieldName - Field name the user is searching.
searchStr - Search string
beginAnd - Do we begin our search string with 'and'
Returns:
Returns string that can be inserted in sql query clause.

formatOrSet

public static java.lang.String formatOrSet(java.lang.String fieldname,
                                           java.lang.String[] ids)
Formats SQL search string from array of id numbers. Result can be appended to SQL clause.

Parameters:
fieldname - The name of the column we are searching
ids - ids of the records we are looking for.
Returns:
" ( t1.field='2' or t1.field='7' or t1.field='10' ... ) "

addSqlFrom

public static java.lang.String addSqlFrom(StringPair[] tables)
returns "from" part of SQL

Parameters:
tables - table names (and aliases) in stringpair array. assumes key is alias, content tablename
Returns:
"from table1 as t1, table2 as t2 ..."

addSqlDeleted

public static java.lang.String addSqlDeleted(StringPair[] tables)
Returns "deleted" part in where clause. Assumes clause may start with and

Parameters:
tables - table names (and aliases) in stringpair array. key is used in clause
Returns:
"and t1.deleted=false and t2.deleted=false ..."

isEmpty

public static boolean isEmpty(RS2 rs)
Tests if rs2 is NULL or empty.

Parameters:
rs - RS2 we are testing
Returns:
true if rs2 is NULL or empty. (eg. returns false if rs2 has content)