This is an automated email from the ASF dual-hosted git repository.
doebele pushed a commit to branch version3
in repository https://gitbox.apache.org/repos/asf/empire-db.git
The following commit(s) were added to refs/heads/version3 by this push:
new f8a21f0 EMPIREDB-362 cleanup
f8a21f0 is described below
commit f8a21f02f79d5a204d2d2a5f7a5fd9ddb7f80375
Author: Rainer Döbele <[email protected]>
AuthorDate: Fri Jan 21 20:35:37 2022 +0100
EMPIREDB-362
cleanup
---
.../org/apache/empire/samples/db/SampleApp.java | 106 +++++++++++----------
.../main/java/org/apache/empire/db/DBColumn.java | 2 +-
.../main/java/org/apache/empire/db/DBCommand.java | 2 +-
.../main/java/org/apache/empire/db/DBDatabase.java | 32 ++-----
.../org/apache/empire/db/DBDatabaseDriver.java | 6 +-
.../main/java/org/apache/empire/db/DBQuery.java | 5 +-
.../main/java/org/apache/empire/db/DBRecord.java | 11 +--
.../main/java/org/apache/empire/db/DBRowSet.java | 9 +-
8 files changed, 81 insertions(+), 92 deletions(-)
diff --git
a/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/SampleApp.java
b/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/SampleApp.java
index b637730..8d7d90a 100644
---
a/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/SampleApp.java
+++
b/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/SampleApp.java
@@ -306,14 +306,15 @@ public class SampleApp
*/
private static int insertDepartment(String departmentName, String
businessUnit)
{
+ SampleDB.Departments DEP = db.DEPARTMENTS;
// Insert a Department
- DBRecord rec = new DBRecord(context, db.DEPARTMENTS);
+ DBRecord rec = new DBRecord(context, DEP);
rec.create();
- rec.setValue(db.DEPARTMENTS.NAME, departmentName);
- rec.setValue(db.DEPARTMENTS.BUSINESS_UNIT, businessUnit);
+ rec.setValue(DEP.NAME, departmentName);
+ rec.setValue(DEP.BUSINESS_UNIT, businessUnit);
rec.update();
// Return Department ID
- return rec.getInt(db.DEPARTMENTS.DEPARTMENT_ID);
+ return rec.getInt(DEP.DEPARTMENT_ID);
}
/**
@@ -323,16 +324,17 @@ public class SampleApp
*/
private static int insertEmployee(String firstName, String lastName,
Gender gender, int departmentId)
{
+ SampleDB.Employees EMP = db.EMPLOYEES;
// Insert an Employee
- DBRecord rec = new DBRecord(context, db.EMPLOYEES);
+ DBRecord rec = new DBRecord(context, EMP);
rec.create();
- rec.setValue(db.EMPLOYEES.FIRSTNAME, firstName);
- rec.setValue(db.EMPLOYEES.LASTNAME, lastName);
- rec.setValue(db.EMPLOYEES.GENDER, gender);
- rec.setValue(db.EMPLOYEES.DEPARTMENT_ID, departmentId);
+ rec.setValue(EMP.FIRSTNAME, firstName);
+ rec.setValue(EMP.LASTNAME, lastName);
+ rec.setValue(EMP.GENDER, gender);
+ rec.setValue(EMP.DEPARTMENT_ID, departmentId);
rec.update();
// Return Employee ID
- return rec.getInt(db.EMPLOYEES.EMPLOYEE_ID);
+ return rec.getInt(EMP.EMPLOYEE_ID);
}
/**
@@ -357,11 +359,12 @@ public class SampleApp
*/
private static void updatePartialRecord(int idEmp, String phoneNumber)
{
- SampleDB.Employees E = db.EMPLOYEES;
+ // Shortcut for convenience
+ SampleDB.Employees EMP = db.EMPLOYEES;
// Update an Employee with partial record
// this will only load the EMPLOYEE_ID and the PHONE_NUMBER
- DBRecord rec = new DBRecord(context, E);
- E.readRecord(rec, DBRecord.key(idEmp), PartialMode.INCLUDE,
E.PHONE_NUMBER);
+ DBRecord rec = new DBRecord(context, EMP);
+ EMP.readRecord(rec, DBRecord.key(idEmp), PartialMode.INCLUDE,
EMP.PHONE_NUMBER);
// Set
rec.setValue(db.EMPLOYEES.PHONE_NUMBER, phoneNumber);
rec.update();
@@ -374,21 +377,22 @@ public class SampleApp
*/
private static void updateJoinedRecords(int idEmp, int salary)
{
- SampleDB.Employees E = db.EMPLOYEES;
- SampleDB.Departments D = db.DEPARTMENTS;
+ // Shortcuts for convenience
+ SampleDB.Employees EMP = db.EMPLOYEES;
+ SampleDB.Departments DEP = db.DEPARTMENTS;
// Create DBQuery from command
DBCommand cmd = db.createCommand();
- cmd.select(E.getColumns());
- cmd.select(D.getColumns());
- cmd.join(E.DEPARTMENT_ID, D.DEPARTMENT_ID);
- DBQuery query = new DBQuery(cmd, E.EMPLOYEE_ID);
+ cmd.select(EMP.getColumns());
+ cmd.select(DEP.getColumns());
+ cmd.join(EMP.DEPARTMENT_ID, DEP.DEPARTMENT_ID);
+ DBQuery query = new DBQuery(cmd, EMP.EMPLOYEE_ID);
// Make employee Head of Department and update salary
DBRecord rec = new DBRecord(context, query);
rec.read(idEmp);
- rec.setValue(E.SALARY, salary);
- rec.setValue(D.HEAD, rec.getString(E.LASTNAME));
+ rec.setValue(EMP.SALARY, salary);
+ rec.setValue(DEP.HEAD, rec.getString(EMP.LASTNAME));
rec.update();
}
@@ -398,28 +402,29 @@ public class SampleApp
*/
private static int testTransactionCreate(int idDep)
{
- SampleDB.Employees T = db.EMPLOYEES;
- DBRecord rec = new DBRecord(context, T);
-
+ // Shortcut for convenience
+ SampleDB.Employees EMP = db.EMPLOYEES;
+
+ DBRecord rec = new DBRecord(context, EMP);
rec.create();
- rec.setValue(T.FIRSTNAME, "Foo");
- rec.setValue(T.LASTNAME, "Manchoo");
- rec.setValue(T.GENDER, Gender.M);
- rec.setValue(T.DEPARTMENT_ID, idDep);
+ rec.setValue(EMP.FIRSTNAME, "Foo");
+ rec.setValue(EMP.LASTNAME, "Manchoo");
+ rec.setValue(EMP.GENDER, Gender.M);
+ rec.setValue(EMP.DEPARTMENT_ID, idDep);
rec.update();
- log.info("Timestamp {}", rec.getString(T.UPDATE_TIMESTAMP));
+ log.info("Timestamp {}", rec.getString(EMP.UPDATE_TIMESTAMP));
- rec.setValue(T.FIRSTNAME, "Foo 2");
- rec.setValue(T.LASTNAME, "Manchu");
- rec.setValue(T.PHONE_NUMBER, "0815/4711");
+ rec.setValue(EMP.FIRSTNAME, "Foo 2");
+ rec.setValue(EMP.LASTNAME, "Manchu");
+ rec.setValue(EMP.PHONE_NUMBER, "0815/4711");
rec.update();
- log.info("Timestamp {}", rec.getString(T.UPDATE_TIMESTAMP));
+ log.info("Timestamp {}", rec.getString(EMP.UPDATE_TIMESTAMP));
context.rollback();
- rec.setValue(T.FIRSTNAME, "Dr. Foo");
+ rec.setValue(EMP.FIRSTNAME, "Dr. Foo");
rec.update();
- log.info("Timestamp {}", rec.getString(T.UPDATE_TIMESTAMP));
+ log.info("Timestamp {}", rec.getString(EMP.UPDATE_TIMESTAMP));
rec.delete();
@@ -427,12 +432,12 @@ public class SampleApp
// insert final
rec.update();
- log.info("Timestamp {}", rec.getString(T.UPDATE_TIMESTAMP));
+ log.info("Timestamp {}", rec.getString(EMP.UPDATE_TIMESTAMP));
log.info("testTransactionCreate performed OK");
context.commit();
- return rec.getInt(T.EMPLOYEE_ID);
+ return rec.getInt(EMP.EMPLOYEE_ID);
}
/**
* @param context
@@ -440,28 +445,29 @@ public class SampleApp
*/
private static void testTransactionUpdate(int idEmp)
{
- SampleDB.Employees T = db.EMPLOYEES;
- DBRecord rec = new DBRecord(context, T);
+ // Shortcut for convenience
+ SampleDB.Employees EMP = db.EMPLOYEES;
+ DBRecord rec = new DBRecord(context, EMP);
rec.read(idEmp);
- rec.setValue(T.PHONE_NUMBER, null);
- rec.setValue(T.SALARY, "100.000");
+ rec.setValue(EMP.PHONE_NUMBER, null);
+ rec.setValue(EMP.SALARY, "100.000");
rec.update();
- log.info("Timestamp {}", rec.getString(T.UPDATE_TIMESTAMP));
+ log.info("Timestamp {}", rec.getString(EMP.UPDATE_TIMESTAMP));
context.rollback();
- rec.setValue(T.PHONE_NUMBER, "07531-45716-0");
+ rec.setValue(EMP.PHONE_NUMBER, "07531-45716-0");
rec.update();
- log.info("Timestamp {}", rec.getString(T.UPDATE_TIMESTAMP));
+ log.info("Timestamp {}", rec.getString(EMP.UPDATE_TIMESTAMP));
context.rollback();
rec.update();
- log.info("Timestamp {}", rec.getString(T.UPDATE_TIMESTAMP));
+ log.info("Timestamp {}", rec.getString(EMP.UPDATE_TIMESTAMP));
log.info("testTransactionUpdate performed OK");
context.commit();
@@ -472,9 +478,10 @@ public class SampleApp
*/
private static void testTransactionDelete(int idEmp)
{
+ // Shortcut for convenience
SampleDB.Employees T = db.EMPLOYEES;
+
DBRecord rec = new DBRecord(context, T);
-
rec.read(idEmp);
/*
log.info("Timestamp {}", rec.getString(T.UPDATE_TIMESTAMP));
@@ -618,15 +625,16 @@ public class SampleApp
private static void queryBeans(Connection conn)
{
+ SampleDB.Employees EMP = db.EMPLOYEES;
// Query all males
- BeanResult<SampleBean> result = new
BeanResult<SampleBean>(SampleBean.class, db.EMPLOYEES);
- result.getCommand().where(db.EMPLOYEES.GENDER.is(Gender.M));
+ BeanResult<SampleBean> result = new
BeanResult<SampleBean>(SampleBean.class, EMP);
+ result.getCommand().where(EMP.GENDER.is(Gender.M));
result.fetch(context);
System.out.println("Number of male employees is: "+result.size());
// And now, the females
- result.getCommand().where(db.EMPLOYEES.GENDER.is(Gender.F));
+ result.getCommand().where(EMP.GENDER.is(Gender.F));
result.fetch(context);
System.out.println("Number of female employees is: "+result.size());
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBColumn.java
b/empire-db/src/main/java/org/apache/empire/db/DBColumn.java
index 2b4ca20..d9c3eac 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBColumn.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBColumn.java
@@ -129,7 +129,7 @@ public abstract class DBColumn extends DBColumnExpr
return;
}
// write dbid and rowset-name
- String dbid = rowset.getDatabase().getId();
+ String dbid = rowset.getDatabase().getIdentifier();
String rsname = rowset.getName();
strm.writeObject(dbid);
strm.writeObject(rsname);
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBCommand.java
b/empire-db/src/main/java/org/apache/empire/db/DBCommand.java
index c5a002b..42125a2 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBCommand.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBCommand.java
@@ -97,7 +97,7 @@ public abstract class DBCommand extends DBCommandExpr
strm.defaultWriteObject();
return;
}
- String dbid = db.getId();
+ String dbid = db.getIdentifier();
strm.writeObject(dbid);
if (log.isDebugEnabled())
log.debug("Serialization: writing DBCommand "+dbid);
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java
b/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java
index 6aab38b..baba3ba 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java
@@ -20,7 +20,6 @@ package org.apache.empire.db;
import java.io.Serializable;
import java.lang.ref.WeakReference;
-import java.sql.Connection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
@@ -94,7 +93,7 @@ public abstract class DBDatabase extends DBObject
/**
* find a database by id
*/
- public static synchronized DBDatabase findById(String dbIdent)
+ public static DBDatabase findById(String dbIdent)
{
WeakReference<DBDatabase> ref = databaseMap.get(dbIdent);
if (ref==null)
@@ -111,7 +110,7 @@ public abstract class DBDatabase extends DBObject
/**
* find a database by id
*/
- public static synchronized DBDatabase findByClass(Class<? extends
DBDatabase> cls)
+ public static DBDatabase findByClass(Class<? extends DBDatabase> cls)
{
for (WeakReference<DBDatabase> ref : databaseMap.values())
{ // find database by class
@@ -124,9 +123,9 @@ public abstract class DBDatabase extends DBObject
}
// properties
- protected String schema; // database schema name
- protected String linkName; // database link name
- protected String instanceId; // internal instance id
+ private String schema; // database schema name
+ private String linkName; // database link name
+ private String instanceId; // internal instance id
// Collections
protected final List<DBTable> tables = new ArrayList<DBTable>();
@@ -250,20 +249,20 @@ public abstract class DBDatabase extends DBObject
}
/**
- * returns the default database id
+ * returns the default database identifier
* Override this to customize
* @return the defaultId
*/
- protected String getDefaultId()
+ protected String getDefaultIdentifier()
{
return getClass().getSimpleName();
}
/**
- * Returns the database instance id
+ * Returns the database instance identifier
* @return the identifier of the database
*/
- public String getId()
+ public String getIdentifier()
{
return instanceId;
}
@@ -831,19 +830,6 @@ public abstract class DBDatabase extends DBObject
checkOpen();
return driver.createCommand(this);
}
-
- /**
- * Returns a timestamp that is used for record updates.
- *
- * @param conn the connection
- * @return the current date and time.
- */
- public java.sql.Timestamp getUpdateTimestamp(Connection conn)
- {
- // Ask driver
- checkOpen();
- return driver.getUpdateTimestamp(conn);
- }
/**
* Detects the DataType of a given value.
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java
b/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java
index 0b0887a..60764fb 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java
@@ -396,8 +396,10 @@ public abstract class DBDatabaseDriver implements
Serializable
return UUID.randomUUID();
}
else if (type==DataType.DATE || type==DataType.DATETIME ||
type==DataType.TIMESTAMP)
- { // Get database system's date and time
- Date ts = db.getUpdateTimestamp(conn);
+ { if (conn==null)
+ return null; // No connection
+ // Get database system's date and time
+ Date ts = getUpdateTimestamp(conn);
return (type==DataType.DATE ? DateUtils.getDateOnly(ts) : ts);
}
// Other types
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBQuery.java
b/empire-db/src/main/java/org/apache/empire/db/DBQuery.java
index 5b91c56..e459c2b 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBQuery.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBQuery.java
@@ -467,7 +467,8 @@ public class DBQuery extends DBRowSet
}
}
// the connection
- Connection conn = rec.getContext().getConnection();
+ DBContext context = rec.getContext();
+ Connection conn = context.getConnection();
// the commands
DBCommand cmd = getCommandFromExpression();
Object[] key = getRecordKey(rec);
@@ -544,7 +545,7 @@ public class DBQuery extends DBRowSet
timestampIndex = this.getColumnIndex(tsColumn);
if (timestampIndex>=0)
{ // The timestamp is availabe in the record
- timestampValue = db.getUpdateTimestamp(conn);
+ timestampValue =
context.getDriver().getUpdateTimestamp(conn);
Object lastTS = fields[timestampIndex];
if (ObjectUtils.isEmpty(lastTS)==false)
{ // set timestamp constraint
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBRecord.java
b/empire-db/src/main/java/org/apache/empire/db/DBRecord.java
index acc324b..76af745 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBRecord.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBRecord.java
@@ -508,22 +508,13 @@ public class DBRecord extends DBRecordData implements
DBContextAware, Record, Cl
* Returns the array of primary key columns.
* @return the array of primary key columns
*/
+ @Override
public Object[] getKey()
{
return ((rowset != null) ? rowset.getRecordKey(this) : null);
}
/**
- * Returns the array of primary key columns.
- * @Deprecated use getKey() instead
- */
- @Deprecated
- public Object[] getKeyValues()
- {
- return ((rowset != null) ? rowset.getRecordKey(this) : null);
- }
-
- /**
* Returns the value for the given column or null if either the index is
out of range or the value is not valid (see {@link DBRecord#isValueValid(int)})
* @return the index value
*/
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java
b/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java
index d337f4a..3dde471 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java
@@ -130,7 +130,7 @@ public abstract class DBRowSet extends DBExpr
*/
public String getId()
{
- return db.getId()+"."+getName();
+ return db.getIdentifier()+"."+getName();
}
/**
@@ -167,7 +167,7 @@ public abstract class DBRowSet extends DBExpr
strm.defaultWriteObject();
return;
}
- String dbid = db.getId();
+ String dbid = db.getIdentifier();
strm.writeObject(dbid);
if (log.isDebugEnabled())
log.debug("Serialization: writing DBRowSet "+dbid);
@@ -826,10 +826,11 @@ public abstract class DBRowSet extends DBExpr
if (rec.isValid()==false)
throw new ObjectNotValidException(rec);
// the connection
- Connection conn = rec.getContext().getConnection();
+ DBContext context = rec.getContext();
+ Connection conn = context.getConnection();
// Get the new Timestamp
String name = getName();
- Timestamp timestamp = (timestampColumn!=null) ?
db.getUpdateTimestamp(conn) : null;
+ Timestamp timestamp = (timestampColumn!=null) ?
context.getDriver().getUpdateTimestamp(conn) : null;
DBDatabaseDriver.DBSetGenKeys setGenKey = null;
// Get the fields and the flags
Object[] fields = rec.getFields();