|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectutil.PropertyContainer
util.PropertyResources
util.PropertyBundles
mysql.StaticSql
public class StaticSql
A class that provides data base access to a postgres or any other sql
database. It provides only generic functionality, like loading the JDBC
bridge, opening the connection and executing basic statements. Differently to
#GenericSql, this class is static in the sense that it allows
different user to maintain their connections during the entire life-time of
the virtual machine. Each user has an open connection in a static lookup
table. There is a single living instance of this group, whose properties are
searched in the following way, search stop at first match:
GenericSql| Nested Class Summary | |
|---|---|
private static class |
StaticSql.ReconnectTask
A reconnect task first deletes any pending statements and then tries to reopen it in its run method. |
static class |
StaticSql.ResultMetaData
Tries to collect meta-data on the result set of a given table. |
| Nested classes/interfaces inherited from class util.PropertyResources |
|---|
PropertyResources.URLResource |
| Field Summary | |
|---|---|
private static BufferedWriter |
batch
The single living instance. |
private static Map<String,Connection> |
db
The connection to the database, per user one connection. |
private static String |
DEFBRIDGE
The default JDBC-postgres bridge class. |
private static String |
DEFCONNECT
The default driver-specific connection string. |
static DateFormat |
DEFDATEFORMAT
The default date format for sql date and time objects witrhout ms. |
static DateFormat |
DEFMSFORMAT
The default date format for sql date and time objects. |
private static long |
DEFRECONNECT
If zero, reconnecting is not allowed. |
private static boolean |
DEFTRUEREAD
Default writing to db. |
private static boolean |
DEFTRUEWRITE
Default writing to db. |
static String |
FLOAT4
If you try to create a real[], use this in the connection. |
static String |
KEY_BATCHFILE
If this file is given, all commands are also writtn to batch file. |
static String |
KEY_BRIDGE
The class name of the JDBC-postgres bridge. |
static String |
KEY_CONNECT
The driver-specific connection string. |
static String |
KEY_DATEFORMAT
The date formatter to use. |
static String |
KEY_FAILINSERT
On true write, if writing fails, we write to this file. |
static String |
KEY_PASSWORD
The password used for identifying the user. |
static String |
KEY_RECONNECT
If the connection dies, this is the scheduling retry time. |
static String |
KEY_TRUEREAD
For debugging purposes this may be false. |
static String |
KEY_TRUEWRITE
For debugging purposes this may be false. |
static String |
KEY_USERNAME
The user name to identify the class on the database. |
private static boolean |
LOADED
If true, bridge is loaded. |
private static Map<String,String> |
pwd
All valid passwords. |
private static Timer |
reopen
A timer used to schedule re-open tries. |
private static Map<String,Date> |
retry
The lookup for for the reconnections. |
private static StaticSql |
run
The single living instance. |
static String |
SCAN
The resource name psql.static.conf. |
private static String |
testcatalog
|
static Calendar |
UTC
The default calendar to use with time zone UTC. |
| Fields inherited from class util.PropertyBundles |
|---|
KEY_LOCALECOUNTRY, KEY_LOCALELANGUAGE, KEY_RESOURCEBUNDLES |
| Fields inherited from class util.PropertyResources |
|---|
KEY_NOINITONCREATE, localurl, locate, POSTFIX_DIR, POSTFIX_EXT, POSTFIX_FILE, POSTFIX_LIST, POSTFIX_URL, urlset |
| Fields inherited from class util.PropertyContainer |
|---|
KEY_LISTSEPARATOR, KEY_MAPKEYVALUECHAR, KEY_MAPSEPARATOR |
| Fields inherited from interface util.ResourceSupplying |
|---|
KEY_URLRESOURCES, KEY_URLUSECONFIG, KEY_URLUSECURRENT, KEY_URLUSEHOME |
| Fields inherited from interface util.PropertySupplying |
|---|
CONFIG, KEY_CLASS |
| Constructor Summary | |
|---|---|
protected |
StaticSql(Map<String,String> prop)
Constructs a new sql-db access object. |
| Method Summary | |
|---|---|
private static double |
calc(String what,
String whom,
String expression,
String from,
String where)
Returns a result set. |
static boolean |
canRead()
If true, we can read to the database. |
static boolean |
canReconnect()
Returns true if reconnections can be tried. |
static boolean |
canWrite()
If true, we can write to the database. |
private static boolean |
checkRun()
|
private static void |
closeConnections()
Closes all connections and deletes the connected users. |
private static String |
columnNameWrap(String hasup)
Could you believe that upper case column names are allowed in SQL, but if put on the query and not protected by double quotes, they are converted to lower case letters and than the lookup fails with 'no such column'? What sounds like a bad joke is realized in SQL. |
static double |
count(String whom,
String expression,
String from,
String where)
Returns a result set. |
protected static void |
createDefault()
|
protected static void |
createInstance(Map<String,String> nondefault)
|
protected static Statement |
createStatement()
Creates the executable statement for the default user |
protected static Statement |
createStatement(String isnull)
Creates a statement for a certain user. |
protected static String |
defaultUser(String isnull)
If argument is null or empty, return the default user name, otherwise return the argument. |
static int |
delete(String whom,
String table,
String where)
Delets row from a database. |
static int |
executeUpdate(String whom,
String table,
String set,
String where)
Dangerous method to directly update entries in a table. |
void |
exit()
Closes the database without throwing an exception. |
protected static Connection |
getConnection(String user)
Returns the connection for a specified user. |
private static String |
getDatabaseEntry(String key,
Object val)
Formats the argument to allow it to be entered into a normal database. |
static String |
getMetaInfo(ResultSet rs)
Retrieves meta info on a non-null result set. |
static Map<String,String> |
getRuntimeProperties()
Returns the properties of the single run-time instance. |
void |
init()
Initializes the sensor-data to SQL-DB access. |
static boolean |
insert(Map<String,Object> data,
String table)
Inserts new data into the database for the default user. |
static boolean |
insert(String whom,
Map<String,Object> data,
String table)
Inserts new data into the database. |
static boolean |
isDebug()
Returns true if we are in debug mode either for read or write. |
static boolean |
isFullDebug()
Returns true if we are in debug mode in both for read or write. |
static boolean |
isReadDebug()
Returns true if we are in debug mode for read. |
static boolean |
isReady()
Test if data can be writtn to the database using the insert(java.util.Map,
replace(java.lang.String, java.lang.String, java.util.Map or retrieve(java.util.Collection method. |
static boolean |
isReady(String isnull)
Test if data can be writtn to the database using the insert(java.util.Map,
replace(java.lang.String, java.lang.String, java.util.Map or retrieve(java.util.Collection method. |
static boolean |
isWriteDebug()
Returns true if we are in debug mode for write. |
static ResultSet |
join(String user,
Collection<String> fields,
String[] tables,
String[] keys,
String where)
Returns the result of a database join, meaning that at least two tables are joined together. |
static Connection |
openUser(String username,
String password)
Opens the sensor database. |
static PreparedStatement |
prepareInsert(String whom,
String table,
List<String> fields)
Returns a prepared statement for inserting data. |
static PreparedStatement |
prepareUpdate(String whom,
String table,
List<String> cols,
String where)
Returns a prepared statement for updating data. |
static PreparedStatement |
prepareUpdate(String whom,
String table,
String set,
String where)
Returns a prepared statement for updating data. |
static void |
reconnect(String user,
String passwd)
Tries to reconnect using a timer task. |
private static void |
reconnect(String user,
String password,
long redo)
Tries to reconnect by permanently trying to open the stated user connection. |
static boolean |
replace(String whom,
String table,
Map<String,Object> data,
String where)
Replaces data in the database. |
static ResultSet |
retrieve(Collection<String> fields,
String from,
String where)
Returns the result of a database query. |
static ResultSet |
retrieve(Collection<String> fields,
String from,
String where,
String order)
Returns the result of a database query. |
static ResultSet |
retrieve(String whom,
Collection<String> fields,
String from,
String where)
Returns a result set. |
static ResultSet |
retrieve(String whom,
Collection<String> fields,
String from,
String where,
String order)
Returns a result set. |
static ResultSet |
retrieve(String whom,
Collection<String> fields,
String from,
String where,
String order,
int limit)
Returns a result set. |
static ResultSet |
retrieve(String whom,
Map<String,String> asfield,
String from,
String where,
String order,
int limit)
Returns a result set. |
static Map<String,Object> |
retrievePrimary(String whom,
Collection<String> fields,
String from,
String where)
Tries to retrieve all entries in a single result set. |
static double |
sum(String whom,
String expression,
String from,
String where)
Returns a result set. |
static Map<String,Object> |
toMap(ResultSet rs)
Maps a result set into a java map by querying all column names and puting the result set get object as the values into it. |
| Methods inherited from class util.PropertyBundles |
|---|
clone, getLocalized, getLocalized, getLocalizedString, getLocalizedString, loadResource |
| Methods inherited from class util.PropertyResources |
|---|
createFrom, createFrom, createFrom, getApplet, getAsResources, getLocalClassLoader, getPropertiesToKey, getPropertiesToKey, getResource, getResourceAsStream, getResourceFromKey, getResources, keyCreate, keyCreate, reload, setApplet |
| Methods inherited from class util.PropertyContainer |
|---|
augment, augment, augment, defaultBoolean, defaultChar, defaultDouble, defaultFloat, defaultInt, defaultLong, defaultObject, defaultObject, defaultProperties, defaultProperty, getAsBoolean, getAsChar, getAsDouble, getAsEnums, getAsFloat, getAsInt, getAsList, getAsLong, getAsMap, getAsMap, getAsObject, getAsObject, getProperties, getProperty, has, isNew, parseObject, reload, removeProperty, rescanned, setObject, setProperties, setProperty, stringProperties, toString |
| Methods inherited from class java.lang.Object |
|---|
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Methods inherited from interface util.ResourceSupplying |
|---|
getResource, getResourceAsStream, getResources |
| Methods inherited from interface util.PropertySupplying |
|---|
defaultBoolean, defaultChar, defaultDouble, defaultFloat, defaultInt, defaultLong, defaultObject, defaultObject, defaultProperties, defaultProperty, getAsBoolean, getAsChar, getAsDouble, getAsFloat, getAsInt, getAsList, getAsLong, getAsMap, getAsObject, getAsObject, getProperties, getProperty, has, parseObject, removeProperty, setObject, setProperty, stringProperties |
| Field Detail |
|---|
public static final String SCAN
public static final String KEY_BRIDGE
public static final String KEY_CONNECT
public static final String KEY_USERNAME
public static final String KEY_PASSWORD
public static final String KEY_DATEFORMAT
public static final String KEY_TRUEREAD
public static final String KEY_TRUEWRITE
public static final String KEY_FAILINSERT
public static final String KEY_BATCHFILE
public static final String KEY_RECONNECT
public static final String FLOAT4
public static final DateFormat DEFMSFORMAT
public static final DateFormat DEFDATEFORMAT
public static final Calendar UTC
private static final String DEFBRIDGE
private static final String DEFCONNECT
private static final boolean DEFTRUEWRITE
private static final boolean DEFTRUEREAD
private static final long DEFRECONNECT
private static Map<String,Connection> db
private static Map<String,String> pwd
private static Map<String,Date> retry
private static StaticSql run
private static BufferedWriter batch
private static boolean LOADED
private static transient Timer reopen
private static String testcatalog
| Constructor Detail |
|---|
protected StaticSql(Map<String,String> prop)
init() method occurs.
| Method Detail |
|---|
protected static final void createDefault()
protected static final void createInstance(Map<String,String> nondefault)
private static boolean checkRun()
public void init()
KEY_BRIDGE, is loaded.
init in interface Initializableinit in class PropertyResourcespublic void exit()
exit in interface ExitCleaningpublic static boolean canReconnect()
public static void reconnect(String user,
String passwd)
private static void closeConnections()
throws SQLException
SQLException
public static Connection openUser(String username,
String password)
throws SQLException
SQLException
protected static Statement createStatement()
throws SQLException,
ConnectException
SQLException
ConnectException
protected static Statement createStatement(String isnull)
throws SQLException,
ConnectException
ConnectException is thrown.
SQLException
ConnectExceptionpublic static boolean isDebug()
public static boolean isFullDebug()
public static boolean isReadDebug()
public static boolean isWriteDebug()
public static Map<String,String> getRuntimeProperties()
public static boolean isReady()
insert(java.util.Map, java.lang.String) ,
replace(java.lang.String, java.lang.String, java.util.Map, java.lang.String) or retrieve(java.util.Collection, java.lang.String, java.lang.String) method. This method checks if we
have a non-null connection db and a non-null statement
#execute.
public static boolean isReady(String isnull)
insert(java.util.Map, java.lang.String) ,
replace(java.lang.String, java.lang.String, java.util.Map, java.lang.String) or retrieve(java.util.Collection, java.lang.String, java.lang.String) method. This method checks if we
have a non-null connection db and a non-null statement
#execute.
public static boolean insert(Map<String,Object> data,
String table)
throws SQLException,
ConnectException
INSERT INTO {table} ({field1}, {field2},...)
VALUES({num1}, '{string2}',...)
The user supplied map are the fields as keys and their values. The values
should currently only consist of object of types String, Numbers, or
Dates. String are converted to be embraced with single quotes ('), Date
objects are converted into strings using the KEY_DATEFORMAT
format. Numbers are simply converted into strings.
SQLException
ConnectException
public static boolean insert(String whom,
Map<String,Object> data,
String table)
throws SQLException,
ConnectException
INSERT INTO {table} ({field1}, {field2},...)
VALUES({num1}, '{string2}',...)
The user supplied map are the fields as keys and their values. The values
should currently only consist of object convertable to Strings, Numbers,
or Dates. String are converted to be embraced with single quotes ('),
Date objects are converted into strings using the KEY_DATEFORMAT
format. Numbers are simply converted into strings.
Unthinkably so, upper-case column names are only supported if put in
double quotes, so instead of writing
INSERT INTO table (A,B,C..., you have to write
INSERT INTO table ("A","B","C".... Lol.
SQLException
ConnectException
public static boolean replace(String whom,
String table,
Map<String,Object> data,
String where)
throws SQLException,
ConnectException
UPDATE {table} SET {field1}={num1}, {field2}='{string2}',...
The user supplied lists are the field list and the values list. The
values should consist of object of types String, Numbers, or Dates.
String are converted to be embraced with single quotes ('), Date objects
are converted into strings using the KEY_DATEFORMAT format.
Numbers are simply converted into strings. Note that it is the users
responsibility to ensure proper ordering in the list, as no further type
checks are done, only the run-time types of the values are used.
fields - A list of strings giving the field names in the databasevalues - A list of objects that are the correspondend values to the
fields in the parameter above.where - The identification which sets should be affected, without the
leading WHERE.
SQLException
ConnectException
public static PreparedStatement prepareInsert(String whom,
String table,
List<String> fields)
throws SQLException,
ConnectException
SQLException
ConnectException
public static PreparedStatement prepareUpdate(String whom,
String table,
List<String> cols,
String where)
throws SQLException,
ConnectException
SQLException
ConnectException
public static PreparedStatement prepareUpdate(String whom,
String table,
String set,
String where)
throws SQLException,
ConnectException
SQLException
ConnectException
public static int executeUpdate(String whom,
String table,
String set,
String where)
throws SQLException,
ConnectException
This method execute an update reading as (arg replaced by their value)
UPDATE table SET set WHERE where, thus set must be in the
form of column1=value1,column2=value2....
whom - The user name. Only allowed if this connection is open.table - The name of the tableset - in the form of column1=value1,column2=value2,etc.where - The essential where clause.
SQLException
ConnectExceptionprotected static Connection getConnection(String user)
public static int delete(String whom,
String table,
String where)
throws SQLException,
ConnectException
SQLException
ConnectException
public static Map<String,Object> retrievePrimary(String whom,
Collection<String> fields,
String from,
String where)
throws SQLException,
ConnectException
IllegalArguementException - If more than a single entry was detected.
SQLException
ConnectException
public static ResultSet retrieve(Collection<String> fields,
String from,
String where)
throws SQLException,
ConnectException
fields - A list of fields to retrieve or null, if all fields are
needed.where - The WHERE clause without the leading WHERE or null
SQLException
ConnectException
public static ResultSet retrieve(Collection<String> fields,
String from,
String where,
String order)
throws SQLException,
ConnectException
fields - A list of fields to retrieve or null, if all fields are
needed.where - The WHERE clause without the leading WHERE or null
SQLException
ConnectException
public static ResultSet retrieve(String whom,
Collection<String> fields,
String from,
String where)
throws SQLException,
ConnectException
SQLException
ConnectException
public static ResultSet retrieve(String whom,
Collection<String> fields,
String from,
String where,
String order)
throws SQLException,
ConnectException
SQLException
ConnectException
public static ResultSet retrieve(String whom,
Collection<String> fields,
String from,
String where,
String order,
int limit)
throws SQLException,
ConnectException
SQLException
ConnectException
public static ResultSet retrieve(String whom,
Map<String,String> asfield,
String from,
String where,
String order,
int limit)
throws SQLException,
ConnectException
asfield - A list of fields to retrieve or null for all columns.where - The WHERE clause without the leading WHERE or null.order - order result by this column, '-' infront for descending.limit - Limit to this number, zero or negativ for unlimitted.
SQLException
ConnectException
public static Map<String,Object> toMap(ResultSet rs)
throws SQLException
SQLExceptionprivate static final String columnNameWrap(String hasup)
public static double sum(String whom,
String expression,
String from,
String where)
throws SQLException,
ConnectException
SQLException
ConnectException
public static double count(String whom,
String expression,
String from,
String where)
throws SQLException,
ConnectException
SQLException
ConnectException
private static double calc(String what,
String whom,
String expression,
String from,
String where)
throws SQLException,
ConnectException
SQLException
ConnectException
public static ResultSet join(String user,
Collection<String> fields,
String[] tables,
String[] keys,
String where)
throws SQLException,
ConnectException
retrieve(java.util.Collection, java.lang.String, java.lang.String) .
fields - A list of fields to retrieve or null, if all fields are
needed.tables - The names of the tables to join.keys - The column names used on joins.where - The WHERE clause without the leading WHERE, but correctly
column-added
SQLException
ConnectExceptionpublic static String getMetaInfo(ResultSet rs)
public static boolean canWrite()
public static boolean canRead()
protected static String defaultUser(String isnull)
private static String getDatabaseEntry(String key,
Object val)
KEY_DATEFORMAT to
convert the date into a string, returns this within single quotes.
private static void reconnect(String user,
String password,
long redo)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||