Package lazyj
Class DBFunctions
java.lang.Object
lazyj.DBFunctions
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
public class DBFunctions
extends java.lang.Object
implements java.io.Closeable
Wrapper for JDBC connections and collection of useful functions related to database connections. It is also a connection pool, recycling previously established sessions and closing the idle ones.
- Since:
- Oct 15, 2006
- Author:
- costing
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
DBFunctions.DBConnection
Wrapper around a raw database connection. -
Field Summary
Fields Modifier and Type Field Description static java.util.concurrent.ConcurrentHashMap<java.lang.String,java.util.concurrent.atomic.AtomicInteger>
chmQueryCount
For statistics: how many queries were executed on each connection.static java.util.concurrent.ConcurrentHashMap<java.lang.String,java.util.concurrent.atomic.AtomicLong>
chmQueryTime
For statistics: total time to execute the queries on each of the connections. -
Constructor Summary
Constructors Constructor Description DBFunctions(java.lang.String driverClass, java.lang.String jdbcURL)
If you already have the full JDBC connection URL, connect like this.DBFunctions(java.lang.String driverClass, java.lang.String jdbcURL, java.util.Properties configProperties)
If you already have the full JDBC connection URL, connect like thisDBFunctions(java.lang.String driverClass, java.lang.String jdbcURL, java.util.Properties configProperties, java.lang.String sQuery)
If you already have the full JDBC connection URL, connect like this.DBFunctions(java.util.Properties configProperties)
DBFunctions(java.util.Properties configProperties, java.lang.String sQuery)
Create a connection to the database using the parameters in this properties file, then execute the given query.DBFunctions(ExtProperties configProperties)
Create a connection to the database using the parameters in this properties file.DBFunctions(ExtProperties configProperties, java.lang.String sQuery)
Create a connection to the database using the parameters in this properties file, then execute the given query. -
Method Summary
Modifier and Type Method Description boolean
absolute(int position)
Jump to an absolute position in the result setvoid
close()
Explicitly close the allocated resourcesstatic java.lang.String
composeInsert(java.lang.String tableName, java.util.Map<java.lang.String,?> values)
Create an INSERT statement for these valuesstatic java.lang.String
composeUpdate(java.lang.String tableName, java.util.Map<java.lang.String,?> values, java.util.Collection<java.lang.String> primaryKeys)
Compose an UPDATE SQL statementstatic java.lang.String
composeUpsert(java.lang.String tableName, java.util.Map<java.lang.String,?> values, java.util.Collection<java.lang.String> primaryKeys)
Build a INSERT-on-conflict-UPDATE queryint
count()
Get the number of rows that were selected by the previous query.static java.util.List<java.lang.String>
decode(java.lang.String sValue)
Given an array in PostgreSQL format, convert it to a Java array of Strings.static java.util.List<java.lang.Integer>
decodeToInt(java.lang.String sValue)
Convert each entry from an array to Integer.static java.util.List<java.lang.Long>
decodeToLong(java.lang.String sValue)
Convert each entry from an array to Long.static java.lang.String
encodeArray(java.util.Collection<?> array)
Generate a PostgreSQL array representation of the given one-dimensional collection.protected void
finalize()
Override the default destructor to properly close any resources in use.static java.util.HashMap<java.lang.String,java.lang.Integer>
getActiveConnections()
Statistics : get the number of connections per each unique keystatic long
getActiveConnectionsCount()
Statistics : get the number of connections currently establishedboolean
getb(int iColumn, boolean bDefault)
Get the boolean value of a columnboolean
getb(java.lang.String sColumn, boolean bDefault)
Get the boolean value of a columnbyte[]
getBytes(int iColumn)
Get the raw bytes of this columnbyte[]
getBytes(java.lang.String columnName)
Get the raw bytes of this columnstatic long
getClosedConnectionsCount()
Statistics : the total number of closed connection to the databases until now.static long
getClosedOnFinalizeConnectionsCount()
Statistics : the total number of closed connection to the databases executed when the object is deallocated.java.lang.String[]
getColumnNames()
A shortcut to find out the column names for this queryjava.lang.String
getConnectFailReason()
Get the reason why the last connect() attempt has failed.DBFunctions.DBConnection
getConnection()
Get a raw database connection wrapper.int
getCursorType()
double
getd(int iColumn)
Get the double value of a column.double
getd(int iColumn, double dDefault)
Get the double value of a column.double
getd(java.lang.String sColumnName)
Get the double value of a column.double
getd(java.lang.String sColumnName, double dDefault)
Get the double value of a column.java.util.Date
getDate(int iColumn)
Get the column contents converted to Date.java.util.Date
getDate(int iColumn, java.util.Date dDefault)
Get the value of a column as a Date object.java.util.Date
getDate(java.lang.String sColumnName)
Get the column contents converted to Date.java.util.Date
getDate(java.lang.String sColumnName, java.util.Date dDefault)
Get the column contents converted to Date.java.lang.String
getEquivalentInsert(java.lang.String sTable)
Get the SQL INSERT statement that would generate the current row with all the columns (their aliases more precisely).java.lang.String
getEquivalentInsert(java.lang.String sTable, java.lang.String[] columns)
Get the SQL INSERT statement that would generate the current row, for the given list of columnsjava.lang.String
getEquivalentInsert(java.lang.String sTable, java.lang.String[] columns, java.util.Map<java.lang.String,?> overrides)
Get the SQL INSERT statement that would generate the current row, for the given list of columnsfloat
getf(int iColumn)
Get the float value of a column.float
getf(int iColumn, float fDefault)
Get the float value of a column.float
getf(java.lang.String sColumnName)
Get the float value of a column.float
getf(java.lang.String sColumnName, float fDefault)
Get the float value of a column.static java.lang.String
getFormattedValue(java.lang.Object o)
Get the value formatted for SQL statementsint
geti(int iColumn)
Get the value of this column as int, returning the default value of 0 if the conversion is not possible.int
geti(int iColumn, int iDefault)
Get the integer value of a column.int
geti(java.lang.String sColumnName)
Get the value of this column as int.int
geti(java.lang.String sColumnName, int iDefault)
Get the value of this column as int, returning the default value if the conversion is not possible.java.util.List<java.lang.Integer>
getIntArray(int iColumn)
Extract a PostgreSQL array into a Collection of Integer objectsjava.util.List<java.lang.Integer>
getIntArray(java.lang.String sColumn)
Extract a PostgreSQL array into a Collection of Integer objectslong
getl(int iColCount)
Get the long value of a column.long
getl(int iColCount, long lDefault)
Get the long value of a column.long
getl(java.lang.String sColumnName)
Get the long value of a column.long
getl(java.lang.String sColumnName, long lDefault)
Get the long value of a column.java.lang.Exception
getLastError()
java.lang.Integer
getLastGeneratedKey()
Get the last generated key, aftersetLastGeneratedKey(boolean)
was called withtrue
java.lang.Long
getLastGeneratedKeyLong()
Get the last generated key, aftersetLastGeneratedKey(boolean)
was called withtrue
java.sql.ResultSetMetaData
getMetaData()
Get the meta information for the current query.java.lang.Object
getObject(int columnId)
Get the content of a column, as native object prepared by the JDBC driverjava.lang.Object
getObject(java.lang.String columnName)
Get the content of a column, as native object prepared by the JDBC driverstatic long
getOpenedConnectionsCount()
Statistics : the total number of opened connection to the databases until now.int
getPosition()
Get the current position in the result setstatic long
getQueryCount()
Statistics : get the total number of executed queries.java.lang.String
gets(int iColumn)
Get the contents of a column from the current row based on its positionjava.lang.String
gets(int iColumn, java.lang.String sDefault)
Get the contents of a column from the current row based on its position.java.lang.String
gets(java.lang.String sColumnName)
Get the contents of a column from the current row based on its name.java.lang.String
gets(java.lang.String sColumnName, java.lang.String sDefault)
Get the contents of a column from the current row based on its name.java.util.List<java.lang.String>
getStringArray(int iColumn)
Extract a PostgreSQL array into a Collection of String objectsjava.util.List<java.lang.String>
getStringArray(java.lang.String sColumn)
Extract a PostgreSQL array into a Collection of StriG96Lng objectsint
getTransactionIsolationLevel()
int
getUpdateCount()
Get the number of rows affected by the last SQL update query.java.util.Map<java.lang.String,java.lang.Object>
getValuesMap()
Convert the result set in a column name -> value mappingboolean
isMySQL()
Check if this connection is done to a MySQL database (if we are using the MySQL JDBC driver)boolean
isPostgreSQL()
Check if this connection is done to a PostgreSQL database (if we are using the PG JDBC driver)boolean
isReadOnly()
Get the current value of the read-only flagboolean
moveNext()
Jump to the next row in the resultstatic java.lang.String
propToJDBC(java.util.Properties prop)
Build a JDBC URL connection string for a bunch of parametersboolean
query(java.lang.String sQuery)
Execute a query.boolean
query(java.lang.String sQuery, boolean bIgnoreErrors, java.lang.Object... values)
Execute an error and as an option you can force to ignore any errors, no to log them if you expect a query to fail.boolean
relative(int count)
Jump an arbitrary number of rows.int
setCursorType(int type)
Set the cursor type to one of the ResultSet.TYPE_FORWARD_ONLY (default), ResultSet.TYPE_SCROLL_INSENSITIVE (when you needcount()
or such) and so on.boolean
setLastGeneratedKey(boolean enabled)
Enable the fetching of the last generated IDint
setQueryTimeout(int newTimeout)
Set the query timeout.boolean
setReadOnly(boolean readOnly)
Signal that the following query is read-only and, if available, a database slave could be used to execute it.void
setTransactionIsolation(int isolationLevel)
Override the transaction isolation level for the following queries.long
setValidateInterval(long newValidateInterval)
Set how often to validate the connection.static void
startThread()
Start the cleanup thread.static void
stopThread()
Signal the thread that it's time to stop.
-
Field Details
-
chmQueryCount
public static final java.util.concurrent.ConcurrentHashMap<java.lang.String,java.util.concurrent.atomic.AtomicInteger> chmQueryCountFor statistics: how many queries were executed on each connection. -
chmQueryTime
public static final java.util.concurrent.ConcurrentHashMap<java.lang.String,java.util.concurrent.atomic.AtomicLong> chmQueryTimeFor statistics: total time to execute the queries on each of the connections.
-
-
Constructor Details
-
DBFunctions
Create a connection to the database using the parameters in this properties file. The following keys are extracted:
- driver : (required) one of org.postgresql.Driver, com.mysql.jdbc.Driver or com.microsoft.jdbc.sqlserver.SQLServerDriver
- url : (required) full JDBC URL. If found it would be preferred instead of the following keys (database, host and port)
- database : (required; alternative) name of the database to connect to
- host : (optional) server's ip address, defaults to 127.0.0.1
- port : (optional) tcp port to connect to on the host, if it is missing the default port for each database type is used
- user : (recommended) supply this account name when connecting
- password : (recommended) password for the account
- transactionIsolation : (optional) numerical value of the transaction isolation constant. Default 2 (read committed).
Connection.TRANSACTION_READ_COMMITTED
- MySQL:
- connectTimeout : timeout in milliseconds for a new connection, default is 0 infinite)
- useCompression : true/false, default false
- PostgreSQL:
- ssl : present=true for now
- charSet : string
- Parameters:
configProperties
- connection options
-
DBFunctions
public DBFunctions(java.util.Properties configProperties)- Parameters:
configProperties
-- See Also:
DBFunctions(ExtProperties)
-
DBFunctions
public DBFunctions(java.util.Properties configProperties, java.lang.String sQuery)Create a connection to the database using the parameters in this properties file, then execute the given query.- Parameters:
configProperties
- connection parameterssQuery
- query to execute after connecting- See Also:
DBFunctions(ExtProperties)
-
DBFunctions
Create a connection to the database using the parameters in this properties file, then execute the given query.- Parameters:
configProperties
- connection parameterssQuery
- query to execute after connecting- See Also:
DBFunctions(ExtProperties)
-
DBFunctions
public DBFunctions(java.lang.String driverClass, java.lang.String jdbcURL)If you already have the full JDBC connection URL, connect like this.- Parameters:
driverClass
- JDBC driver class namejdbcURL
- JDBC connection URL- See Also:
DBFunctions(String, String, Properties)
-
DBFunctions
public DBFunctions(java.lang.String driverClass, java.lang.String jdbcURL, java.util.Properties configProperties)If you already have the full JDBC connection URL, connect like this- Parameters:
driverClass
- JDBC driver class namejdbcURL
- full JDBC connection URLconfigProperties
- extra configuration options. Can benull
if the URL has everything in it- See Also:
DBFunctions(ExtProperties)
-
DBFunctions
public DBFunctions(java.lang.String driverClass, java.lang.String jdbcURL, java.util.Properties configProperties, java.lang.String sQuery)If you already have the full JDBC connection URL, connect like this.- Parameters:
driverClass
- JDBC driver class namejdbcURL
- full JDBC connection URLconfigProperties
- extra configuration optionssQuery
- query to execute- See Also:
DBFunctions(ExtProperties)
-
-
Method Details
-
setReadOnly
public boolean setReadOnly(boolean readOnly)Signal that the following query is read-only and, if available, a database slave could be used to execute it.- Parameters:
readOnly
- iftrue
then the query can potentially go to a slave, iffalse
then only the master can execute it- Returns:
- previous value of the read-only flag
-
isReadOnly
public boolean isReadOnly()Get the current value of the read-only flag- Returns:
- read-only flag
-
setCursorType
public int setCursorType(int type)Set the cursor type to one of the ResultSet.TYPE_FORWARD_ONLY (default), ResultSet.TYPE_SCROLL_INSENSITIVE (when you needcount()
or such) and so on.- Parameters:
type
- new cursor type- Returns:
- previous cursor type
-
getCursorType
public int getCursorType()- Returns:
- cursor type
-
setTransactionIsolation
public void setTransactionIsolation(int isolationLevel)Override the transaction isolation level for the following queries. Set to-1
to disable any override.- Parameters:
isolationLevel
-
-
getTransactionIsolationLevel
public int getTransactionIsolationLevel()- Returns:
- the override value
-
isPostgreSQL
public boolean isPostgreSQL()Check if this connection is done to a PostgreSQL database (if we are using the PG JDBC driver)- Returns:
- true if the connection is done to a PostgreSQL database
-
isMySQL
public boolean isMySQL()Check if this connection is done to a MySQL database (if we are using the MySQL JDBC driver)- Returns:
- true if the connection is done to a MySQL database
-
getConnectFailReason
public java.lang.String getConnectFailReason()Get the reason why the last connect() attempt has failed.- Returns:
- reason, if there is any, or
null
if the connection actually worked
-
getConnection
Get a raw database connection wrapper. Remember to alwaysDBFunctions.DBConnection.free()
orDBFunctions.DBConnection.close()
at the end of the section where you use it!- Returns:
- database connection wrapper or
null
if a connection cannot be established - See Also:
DBFunctions.DBConnection
-
propToJDBC
public static final java.lang.String propToJDBC(java.util.Properties prop)Build a JDBC URL connection string for a bunch of parameters- Parameters:
prop
-- Returns:
- JDBC URL connection string, or
null
if for any reason it cannot be built (unknown driver?)
-
startThread
public static final void startThread()Start the cleanup thread. Should not be called externally since it is called automatically at the first use of this class. -
stopThread
public static final void stopThread()Signal the thread that it's time to stop. You should only call this when the JVM is about to shut down, and not even then it's necessary to do so. -
getUpdateCount
public final int getUpdateCount()Get the number of rows affected by the last SQL update query.- Returns:
- number of rows
-
close
public void close()Explicitly close the allocated resources- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
-
finalize
protected void finalize()Override the default destructor to properly close any resources in use.- Overrides:
finalize
in classjava.lang.Object
-
query
public boolean query(java.lang.String sQuery)Execute a query.- Parameters:
sQuery
- SQL query to execute- Returns:
true
if the query succeeded,false
if there was an error (connection or syntax).- See Also:
query(String, boolean,Object...)
-
setLastGeneratedKey
public boolean setLastGeneratedKey(boolean enabled)Enable the fetching of the last generated ID- Parameters:
enabled
- set totrue
to be able to do getLastGeneratedKey()- Returns:
- the previous setting
- See Also:
getLastGeneratedKey()
-
getLastGeneratedKey
public java.lang.Integer getLastGeneratedKey()Get the last generated key, aftersetLastGeneratedKey(boolean)
was called withtrue
- Returns:
- the last generated key, as Integer, or
null
if there is any problem (orsetLastGeneratedKey(boolean)
was not generated) - See Also:
setLastGeneratedKey(boolean)
-
getLastGeneratedKeyLong
public java.lang.Long getLastGeneratedKeyLong()Get the last generated key, aftersetLastGeneratedKey(boolean)
was called withtrue
- Returns:
- the last generated key, as Long, or
null
if there is any problem (orsetLastGeneratedKey(boolean)
was not generated) - See Also:
setLastGeneratedKey(boolean)
-
setQueryTimeout
public final int setQueryTimeout(int newTimeout)Set the query timeout. This is implementation specific, make sure that the JDBC supports it and the functionality is enabled. For MySQL this is done via theenableQueryTimeouts=(default) true
flag.- Parameters:
newTimeout
- query timeout, in seconds- Returns:
- old execution timeout
-
setValidateInterval
public final long setValidateInterval(long newValidateInterval)Set how often to validate the connection. The default value is0
meaning always check but it can also be overwritten from the properties passed to the constructor with the "validateInterval" key.- Parameters:
newValidateInterval
-- Returns:
- old interval to execute isValid()
-
getLastError
public java.lang.Exception getLastError()- Returns:
- the last known exception
-
query
public final boolean query(java.lang.String sQuery, boolean bIgnoreErrors, java.lang.Object... values)Execute an error and as an option you can force to ignore any errors, no to log them if you expect a query to fail.- Parameters:
sQuery
- query to execute, can be a full query or a prepared statement in which case the values to the columns should be passed as wellbIgnoreErrors
-true
if you want to hide any errorsvalues
- values to set to the prepared statement- Returns:
- true if the query succeeded, false if there was an error
-
count
public final int count()Get the number of rows that were selected by the previous query. Will only work if you have previously calledsetCursorType(int)
with one of the ResultSet.TYPE_SCROLL_* constants.- Returns:
- number of rows, or -1 if the query was not a select one or there was an error
-
getPosition
public final int getPosition()Get the current position in the result set- Returns:
- current position, -1 if there was an error
- See Also:
ResultSet.getRow()
-
relative
public final boolean relative(int count)Jump an arbitrary number of rows.- Parameters:
count
- can be positive or negative- Returns:
- true if the jump was possible, false if not
- See Also:
ResultSet.relative(int)
-
absolute
public final boolean absolute(int position)Jump to an absolute position in the result set- Parameters:
position
- new position- Returns:
- true if the positioning was possible, false otherwise
- See Also:
ResultSet.absolute(int)
-
moveNext
public final boolean moveNext()Jump to the next row in the result- Returns:
- true if there is a next entry to jump to, false if not
-
gets
public final java.lang.String gets(java.lang.String sColumnName)Get the contents of a column from the current row based on its name. By default will return "" if there is any problem (column missing, value is null ...)- Parameters:
sColumnName
- column name- Returns:
- value, defaulting to ""
- See Also:
gets(String, String)
,gets(int)
,gets(int, String)
-
gets
public final java.lang.String gets(java.lang.String sColumnName, java.lang.String sDefault)Get the contents of a column from the current row based on its name. It will return the given default if there is any problem (column missing, value is null ...)- Parameters:
sColumnName
- column namesDefault
- default value to return if the column doesn't exist or isnull
- Returns:
- value for the column with the same name from the current row
- See Also:
gets(String)
,gets(int)
,gets(int, String)
-
gets
public final java.lang.String gets(int iColumn)Get the contents of a column from the current row based on its position- Parameters:
iColumn
- column count- Returns:
- value
- See Also:
gets(String)
,gets(String, String)
,gets(int, String)
-
gets
public final java.lang.String gets(int iColumn, java.lang.String sDefault)Get the contents of a column from the current row based on its position. It will return the given default if there is any problem (column missing, value is null ...)- Parameters:
iColumn
- position (1 = first column of the result set)sDefault
- default value to return if the column doesn't exist or isnull
- Returns:
- value in the DB or the default value
- See Also:
gets(String)
,gets(String, String)
,gets(int)
-
getDate
public final java.util.Date getDate(java.lang.String sColumnName)Get the column contents converted to Date. Will return the given default Date if there is a problem parsing the column.- Parameters:
sColumnName
- column name- Returns:
- date, never
null
but maybe the current time if the contents cannot be converted to Date - See Also:
Format.parseDate(String)
,getDate(String, Date)
,getDate(int)
,getDate(int, Date)
-
getDate
public final java.util.Date getDate(java.lang.String sColumnName, java.util.Date dDefault)Get the column contents converted to Date. Will return the given default Date if there is a problem parsing the column.- Parameters:
sColumnName
- column namedDefault
- default value to return if the contents in db cannot be parsed to Date- Returns:
- date from db, or the default value
- See Also:
Format.parseDate(String)
,getDate(String)
,getDate(int)
,getDate(int, Date)
-
getDate
public final java.util.Date getDate(int iColumn)Get the column contents converted to Date. Will return the current date/time as default if there is a problem parsing the column.- Parameters:
iColumn
- column number ( 1 = first column of the result set )- Returns:
- date from db, or the default value
- See Also:
Format.parseDate(String)
,getDate(String)
,getDate(String, Date)
,getDate(int, Date)
-
getDate
public final java.util.Date getDate(int iColumn, java.util.Date dDefault)Get the value of a column as a Date object. Will return the given default Date if there is a problem parsing the column.- Parameters:
iColumn
- column number ( 1 = first column of the result set )dDefault
- default value to return in case of an error at parsing- Returns:
- a Date representation of this column
- See Also:
Format.parseDate(String)
,getDate(String)
,getDate(String, Date)
,getDate(int)
-
geti
public final int geti(java.lang.String sColumnName)Get the value of this column as int. Will return the current date/time as default if there is a problem parsing the column.- Parameters:
sColumnName
- column- Returns:
- value as int, or 0 if there is a problem parsing
- See Also:
geti(String, int)
,geti(int)
,geti(int, int)
-
geti
public final int geti(java.lang.String sColumnName, int iDefault)Get the value of this column as int, returning the default value if the conversion is not possible.- Parameters:
sColumnName
- column nameiDefault
- default value to return- Returns:
- the value in the db or the given default if there is a problem parsing
- See Also:
geti(String)
,geti(int)
,geti(int, int)
-
geti
public final int geti(int iColumn)Get the value of this column as int, returning the default value of 0 if the conversion is not possible.- Parameters:
iColumn
- column position- Returns:
- the value in the db or 0 if there is a problem parsing
- See Also:
geti(String, int)
,geti(String)
,geti(int, int)
-
geti
public final int geti(int iColumn, int iDefault)Get the integer value of a column. Will return the given default value if the column value cannot be parsed into an integer.- Parameters:
iColumn
- column numberiDefault
- default value to return in case of a parsing error- Returns:
- the integer value of this column
- See Also:
geti(String, int)
,geti(int)
,geti(String)
-
getl
public final long getl(java.lang.String sColumnName)Get the long value of a column. Will return 0 by default if the column value cannot be parsed into a long.- Parameters:
sColumnName
- column name- Returns:
- the long value of this column
- See Also:
getl(String, long)
,getl(int)
,getl(int, long)
-
getl
public final long getl(java.lang.String sColumnName, long lDefault)Get the long value of a column. Will return the given default value if the column value cannot be parsed into a long.- Parameters:
sColumnName
- column namelDefault
- default value to return in case of a parsing error- Returns:
- the long value of this column
- See Also:
getl(String)
,getl(int)
,getl(int, long)
-
getl
public final long getl(int iColCount)Get the long value of a column. Will return 0 by default if the column value cannot be parsed into a long.- Parameters:
iColCount
- column count- Returns:
- the long value of this column
- See Also:
getl(String, long)
,getl(String)
,getl(int, long)
-
getl
public final long getl(int iColCount, long lDefault)Get the long value of a column. Will return the given default value if the column value cannot be parsed into a long.- Parameters:
iColCount
- column countlDefault
- default value to return in case of a parsing error- Returns:
- the long value of this column
- See Also:
getl(String, long)
,getl(int)
,getl(String)
-
getf
public final float getf(java.lang.String sColumnName)Get the float value of a column. Will return 0 by default if the column value cannot be parsed into a float.- Parameters:
sColumnName
- column name- Returns:
- the float value of this column
-
getf
public final float getf(java.lang.String sColumnName, float fDefault)Get the float value of a column. Will return the given default value if the column value cannot be parsed into a float.- Parameters:
sColumnName
- column namefDefault
- default value to return in case of a parsing error- Returns:
- the float value of this column
-
getf
public final float getf(int iColumn)Get the float value of a column. Will return 0 by default if the column value cannot be parsed into a float.- Parameters:
iColumn
- column position- Returns:
- the float value of this column
-
getf
public final float getf(int iColumn, float fDefault)Get the float value of a column. Will return the given default value if the column value cannot be parsed into a float.- Parameters:
iColumn
- column positionfDefault
- default value to return in case of a parsing error- Returns:
- the float value of this column
-
getd
public final double getd(java.lang.String sColumnName)Get the double value of a column. Will return 0 by default if the column value cannot be parsed into a double.- Parameters:
sColumnName
- column name- Returns:
- the double value of this column
-
getd
public final double getd(java.lang.String sColumnName, double dDefault)Get the double value of a column. Will return 0 by default if the column value cannot be parsed into a double.- Parameters:
sColumnName
- column namedDefault
- default value to return in case of a parsing error- Returns:
- the double value of this column
-
getd
public final double getd(int iColumn)Get the double value of a column. Will return 0 by default if the column value cannot be parsed into a double.- Parameters:
iColumn
- column position- Returns:
- the double value of this column
-
getd
public final double getd(int iColumn, double dDefault)Get the double value of a column. Will return 0 by default if the column value cannot be parsed into a double.- Parameters:
iColumn
- column positiondDefault
- default value to return in case of a parsing error- Returns:
- the double value of this column
-
getb
public final boolean getb(java.lang.String sColumn, boolean bDefault)Get the boolean value of a column- Parameters:
sColumn
- column namebDefault
- default value- Returns:
- true/false, obviously :)
- See Also:
Utils.stringToBool(String, boolean)
,getb(int, boolean)
-
getb
public final boolean getb(int iColumn, boolean bDefault)Get the boolean value of a column- Parameters:
iColumn
- column indexbDefault
- default value- Returns:
- true/false, obviously :)
- See Also:
Utils.stringToBool(String, boolean)
,getb(String, boolean)
-
getBytes
public final byte[] getBytes(int iColumn)Get the raw bytes of this column- Parameters:
iColumn
-- Returns:
- the bytes of this column
-
getBytes
public final byte[] getBytes(java.lang.String columnName)Get the raw bytes of this column- Parameters:
columnName
-- Returns:
- the bytes of this column
-
getStringArray
public final java.util.List<java.lang.String> getStringArray(java.lang.String sColumn)Extract a PostgreSQL array into a Collection of StriG96Lng objects- Parameters:
sColumn
- column name- Returns:
- the values in the array, as Strings
- Since:
- 1.0.3
-
getStringArray
public final java.util.List<java.lang.String> getStringArray(int iColumn)Extract a PostgreSQL array into a Collection of String objects- Parameters:
iColumn
- column index- Returns:
- the values in the array, as Strings
- Since:
- 1.0.3
-
getIntArray
public final java.util.List<java.lang.Integer> getIntArray(java.lang.String sColumn)Extract a PostgreSQL array into a Collection of Integer objects- Parameters:
sColumn
- column name- Returns:
- the values in the array, as Integers
- Since:
- 1.0.3
-
getIntArray
public final java.util.List<java.lang.Integer> getIntArray(int iColumn)Extract a PostgreSQL array into a Collection of Integer objects- Parameters:
iColumn
- column index- Returns:
- the values in the array, as Integers
- Since:
- 1.0.3
-
decodeToInt
public static java.util.List<java.lang.Integer> decodeToInt(java.lang.String sValue)Convert each entry from an array to Integer.- Parameters:
sValue
-- Returns:
- the members of the database array, as list of Integer objects
- Since:
- 1.0.3
-
decodeToLong
public static java.util.List<java.lang.Long> decodeToLong(java.lang.String sValue)Convert each entry from an array to Long.- Parameters:
sValue
-- Returns:
- the members of the database array, as list of Long objects
-
decode
public static java.util.List<java.lang.String> decode(java.lang.String sValue)Given an array in PostgreSQL format, convert it to a Java array of Strings.- Parameters:
sValue
-- Returns:
- the members of the database array, as list of String objects
- Since:
- 1.0.3
-
encodeArray
public static java.lang.String encodeArray(java.util.Collection<?> array)Generate a PostgreSQL array representation of the given one-dimensional collection. For details consult the documentation.- Parameters:
array
-- Returns:
- a string encoding of the values
- Since:
- 1.0.3
-
getMetaData
public final java.sql.ResultSetMetaData getMetaData()Get the meta information for the current query. You can look at this structure to extract column names, types and so on.- Returns:
- the meta information for the current query.
-
getColumnNames
public final java.lang.String[] getColumnNames()A shortcut to find out the column names for this query- Returns:
- an array of column names
-
getOpenedConnectionsCount
public static final long getOpenedConnectionsCount()Statistics : the total number of opened connection to the databases until now.- Returns:
- total number of opened connections.
-
getClosedConnectionsCount
public static final long getClosedConnectionsCount()Statistics : the total number of closed connection to the databases until now.- Returns:
- total number of closed connections.
-
getClosedOnFinalizeConnectionsCount
public static final long getClosedOnFinalizeConnectionsCount()Statistics : the total number of closed connection to the databases executed when the object is deallocated.- Returns:
- total number of closed connections on object deallocation.
-
getQueryCount
public static final long getQueryCount()Statistics : get the total number of executed queries.- Returns:
- number of executed queries.
-
getActiveConnectionsCount
public static final long getActiveConnectionsCount()Statistics : get the number of connections currently established- Returns:
- the number of active connections
-
getActiveConnections
public static final java.util.HashMap<java.lang.String,java.lang.Integer> getActiveConnections()Statistics : get the number of connections per each unique key- Returns:
- a map of key - number of active connections
-
getEquivalentInsert
public final java.lang.String getEquivalentInsert(java.lang.String sTable)Get the SQL INSERT statement that would generate the current row with all the columns (their aliases more precisely).- Parameters:
sTable
- table name- Returns:
- the INSERT statement, or
null
if any problem
-
getEquivalentInsert
public final java.lang.String getEquivalentInsert(java.lang.String sTable, java.lang.String[] columns)Get the SQL INSERT statement that would generate the current row, for the given list of columns- Parameters:
sTable
- table namecolumns
- what column names are to be taken into account- Returns:
- the INSERT statement, or
null
if there was any problem
-
getValuesMap
public final java.util.Map<java.lang.String,java.lang.Object> getValuesMap()Convert the result set in a column name -> value mapping- Returns:
- the column name -> value mapping
-
getEquivalentInsert
public final java.lang.String getEquivalentInsert(java.lang.String sTable, java.lang.String[] columns, java.util.Map<java.lang.String,?> overrides)Get the SQL INSERT statement that would generate the current row, for the given list of columns- Parameters:
sTable
- table namecolumns
- what column names are to be taken into account. Non-existing column names are ignored.overrides
- value overrides. Column names that don't exist in the columns selection are appended to the output.- Returns:
- the INSERT statement, or
null
if there was any problem
-
getFormattedValue
public static java.lang.String getFormattedValue(java.lang.Object o)Get the value formatted for SQL statements- Parameters:
o
- value to format- Returns:
- formatted string, depending on the object type
-
composeInsert
public static java.lang.String composeInsert(java.lang.String tableName, java.util.Map<java.lang.String,?> values)Create an INSERT statement for these values- Parameters:
tableName
- table namevalues
- column - value mapping- Returns:
- the SQL statement, or
null
if there was any problem
-
composeUpdate
public static java.lang.String composeUpdate(java.lang.String tableName, java.util.Map<java.lang.String,?> values, java.util.Collection<java.lang.String> primaryKeys)Compose an UPDATE SQL statement- Parameters:
tableName
- table namevalues
- column - value mappingprimaryKeys
- the set of primary keys from the values map- Returns:
- the UPDATE statement
-
composeUpsert
public static java.lang.String composeUpsert(java.lang.String tableName, java.util.Map<java.lang.String,?> values, java.util.Collection<java.lang.String> primaryKeys)Build a INSERT-on-conflict-UPDATE query- Parameters:
tableName
-values
-primaryKeys
-- Returns:
- PostgreSQL-compatible UPSERT command
-
getObject
public java.lang.Object getObject(int columnId)Get the content of a column, as native object prepared by the JDBC driver- Parameters:
columnId
-- Returns:
- the content of the respective column, as an object
-
getObject
public java.lang.Object getObject(java.lang.String columnName)Get the content of a column, as native object prepared by the JDBC driver- Parameters:
columnName
-- Returns:
- the content of the respective column, as an object
-