Interface IDBConnectionHandler
- All Superinterfaces:
de.elo.ix.db.schema.IDBConnection
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interface
Wrapper class for the JDBC ResultSet. -
Method Summary
Modifier and TypeMethodDescriptionescapeString
(String value) Return an escaped String value to be used in a SQL statement.Returns the name of the Database product.boolean
isDB2()
Returns true, if connected to DB2.boolean
isMssql()
Returns true, if connected to Mssql.boolean
isOracle()
Returns true, if connected to Oracle.boolean
isPgsql()
Returns true, if connected to Mssql.open()
Open a database connection.Object[][]
Execute a SELECT command.Object[][]
Execute a SELECT command.Object[][]
Execute a SELECT command.Executes an update, delete or insert command.Executes an update, delete or insert command.Executes an update, delete oder insert Statement.Methods inherited from interface de.elo.ix.db.schema.IDBConnection
schema
-
Method Details
-
getDatabaseProductName
Returns the name of the Database product.- Returns:
- ...
- Throws:
Exception
-
isOracle
Returns true, if connected to Oracle.- Returns:
- ...
- Throws:
Exception
NamingException
-
isDB2
Returns true, if connected to DB2.- Returns:
- ...
- Throws:
NamingException
Exception
-
isMssql
Returns true, if connected to Mssql.- Returns:
- ...
- Throws:
NamingException
Exception
-
isPgsql
Returns true, if connected to Mssql.- Returns:
- ...
- Throws:
NamingException
Exception
-
open
Open a database connection. The returned database connections must be closed with a call to its close function.- Returns:
- Connection object
- Throws:
Exception
-
query
Execute a SELECT command.This function executes a SQL SELECT command and returns an array of result rows, whereby each row is an array of Java objects.
Three kinds of query strings in the parameter
sqlcmd
are supported:- Simple query without placeholders:
Example:
db.query("select objid from objekte where objshort like 'it''s my string'", undefined, 0, undefined);
This will execute the query "select objid from objekte where objshort like 'it''s my string'".
- Query with MessageFormat placeholders:
The placeholders are substituted by a java.text.MessageFormat object.
Example:
db.query("select {1} from {0} where {2} like {3}", ["objekte", "objid", "objshort", "'it''s my string'"], 0, undefined);
This will execute the query "select objid from objekte where objshort like 'it''s my string'".
Hints:Placeholders inside apostrophes - like in "... where objshort like '{0}' ..." - are NOT substituted. - Query with PreparedStatement placeholders:
The placeholders are substituted by the JDBC driver.
Example:
db.query("select objid from objekte where objshort like ?", ["it's my string"], 0, undefined);
Hint: Strings need not to be escaped.
Databases provide column data for some column types in a non-serializable Java class. E.g. the ORACLE database return both integers and doubles as Java BigDecimal objects. Thus it is not possible to distinguish between the types and automatically convert to Integer or Double without knowlege about the underlying column type definition. To handle this circumstance, a JavaScript object can be supplied in the parameter
binding
that invokes the getter functions of the ResultSet object. It has to define agetResult
function as shown in the example below. The first parameter of thegetResult
function is aIDBConnectionHandler.ResultSetWrapper
The following JavaScript class is an example for the
binding
parameter.function Binding() { this.getResult = function(row, column) { // column starts with 1 ! switch (column) { case 1: return row.getString(column); case 2: return row.getInteger(column); case 3: return row.getBytes(column); default: throw "Invalid column"; } }; } function dbread() { var db = new Packages.de.elo.ix.jscript.DBConnection( "jdbc/MyDB2", "TESTSCHEMA" ); var args = ["Stuttgart", 59]; var ret = db.query("select name, age, passportphoto from employees where branch like ? and age > ?", args, 0, new Binding()); for (var i = 0; i < ret.length; i++) { log.info("name=" + ret[i][0] + ", age=" + ret[i][1] + ", photo.length=" + ret[i][2].length); } return ret; }
- Parameters:
sqlcmd
- SELECT command string.params
- Optional. This parameters are bound if sqlcmd contains placeholders.max
- Optional. Maximum number of rows to be read. If 0, all selected rows are read.binding
- Optional. This parameter can hold a JavaScript object that extracts the data of a selected column from the ResultSet object.- Returns:
- Java Object[] of rows. If more than one column is selected, each row element is an array of columns.
- Throws:
Exception
- See Also:
- Simple query without placeholders:
-
query
Execute a SELECT command. This function callsquery(String, Object, int, Object)
.- Parameters:
sqlcmd
- SELECT command.params
- Optional. This parameters are bound if sqlcmd contains placeholders.- Returns:
- Matrix of rows and columns.
- Throws:
Exception
- See Also:
-
query
Execute a SELECT command. This function callsquery(String, Object, int, Object)
.- Parameters:
sqlcmd
- SELECT command.- Returns:
- Matrix of rows and columns.
- Throws:
Exception
- See Also:
-
update
Executes an update, delete oder insert Statement.- If the SQL command does not contain placeholders, the rows argument must be null.
- If the SQL command contains MessageFormat placeholders like {0}, {1} etc. or the PreparedStatement placeholder ?, the rows parameter must not be null.
- If rows is a single Object or an array of Objects, this objects replace the placeholders and one database row is updated.
- If rows is an 2-dimensional array of Objects with the dimensions
rows[rowcount][colcount]
, the SQL command containscolcount
placeholders androwcount
database rows are updated.
If the SQL command contains PreparedStatement placeholders, the function parameter
binding
can be supplied. A JavaScript object has to be passed that calls the setters of the internally used JDBC PreparedStatement object. This allows the programmer e.g. to store binary data. Thebinding
object has to be defined analogous to the binding object in thequery(String, Object, int, Object)
function. Here is an example:function Binding() { this.bindParam = function(row, col, obj) { switch (col) { case 1: return row.setString(col, obj); case 2: return row.setDouble(col, obj); case 3: return row.setInteger(col, obj); case 4: return row.setBytes(col, obj); default: throw "Invalid column"; } } } function dbinsert() { var db = new Packages.de.elo.ix.jscript.DBConnection( "jdbc/MyDB2", "TESTSCHEMA" ); var photos = [ ... bytes ... , ... bytes ... ]; var args = [["Fritz", 54321.22, 38, photos[0]],["Maria", 65432.11, 43, photos[1]]]; var ret = db.update("insert into employees (name, salary, age, passportphoto) values (?, ?, ?, ?)", args, new Binding()); log.info("number of inserted rows=" + ret); // == 2 return ret; }
- Parameters:
sqlcmd
- SQL command.rows
- Optional. Data to be written or parameters if the sqlcmd contains placeholders.binding
- Optional. This parameter can hold a JavaScript object that passes the data of a selected column into a PreparedStatement object.- Returns:
- Number of modified rows.
- Throws:
Exception
-
update
Executes an update, delete or insert command. This function callsupdate(String, Object, Object)
- Parameters:
sqlcmd
- SQL command.- Returns:
- Number of modified rows.
- Throws:
Exception
-
update
Executes an update, delete or insert command. This function callsupdate(String, Object, Object)
- Parameters:
sqlcmd
- SQL command.rows
- seeupdate(String, Object, Object)
- Returns:
- Number of modified rows.
- Throws:
Exception
-
escapeString
Return an escaped String value to be used in a SQL statement. Places an apostrophe before and after the given String and replaces every apostrophe in the string with two apostrophes.- Parameters:
value
- String value to be escaped.- Returns:
- Escaped String value.
-