Package com.storedobject.core
Class Database
java.lang.Object
com.storedobject.core.Database
- Direct Known Subclasses:
Database
The abstract database class. In order to write a database driver that works with the stored objects system,
one of the tasks is to create a class that implements all the methods of this class. The class should be packaged in
a package like "com.storedobject.database.DDD" where DDD is the name of the database driver. For example, the Sybase
Adaptive Server Enterprise driver package is "com.storedobject.database.ase" and the Oracle driver package is
"com.storedobject.database.oracle".
- Author:
- Syam
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract boolean
alterPassword
(String user, String oldPassword, String newPassword) Change password of the user on the SQL connection.abstract String
Get the SQL statement string to call the stored procedure.protected final boolean
changePassword
(String user, String oldPassword, String newPassword) Change password of the user on the SQL connection.columnDetails
(String tableName) abstract String
columnType
(String columnType, int width, int precision) boolean
createDatabase
(String databaseName, String securityPassword) protected String
createDatabaseDDL
(String databaseName) abstract boolean
createLogin
(String user, String password) Creates a login with the given login name and password.boolean
createSchema
(String schemaName, String securityPassword) protected abstract String[]
createSchemaDDL
(String schemaName) boolean
createTable
(Class<? extends StoredObject> objectClass, String securityPassword) abstract String
The SQL column that can bring the current time from the database.The SQL statement that can fetch the current time from the database as first column in its result set.final boolean
databaseExists
(String databaseName) Check if a database exists or not.abstract String
databaseSQL
(String databaseName) Part of the SQL string (starting with FROM) that can query details of a database.dependentConstraints
(String tableName) boolean
dropDatabase
(String databaseName, String securityPassword) protected abstract String
dropDatabaseDDL
(String databaseName) abstract boolean
Drops a login with the given login name and password.static void
protected final boolean
executeSQL
(String[] sql, String user, String password) Executes SQL statements.protected final boolean
executeSQL
(String[] sql, String user, String password, boolean ignoreErrors) Executes SQL statements.final boolean
executeSQL
(String sql, String securityPassword) Executes a SQL statement as System Admin.protected final boolean
executeSQL
(String sql, String user, String password) Executes a SQL statements.protected final boolean
executeSQL
(String sql, String user, String password, boolean ignoreErrors) Executes a SQL statements.foreignKeyConstraintNames
(String tableName) foreignKeyConstraints
(String tableName) format
(D date) Format a date value that can be sent to the database as part of a SQL query.formatWithTime
(D date) Format a date value with time component that can be sent to the database as part of a SQL query.static Database
get()
Gets the current database.Current time in the databaseabstract String
Get the name of Object class of this database.abstract String
getSerial
(int tag) Get the SQL statement for generating next Id for the tag passed.protected RawSQL
getSQL()
A convenient method to create RawSQL object for executing adhoc SQL statements.The initial password to be used when a user is created for the first time.abstract boolean
Check if the username passed is a valid login of the sql connection passed.abstract boolean
Checks if the username and password are correct in the current database.static boolean
isMaster()
static void
Locks the database so that login() method can not be invoked again.static void
login
(String driver, String ip, int port, String database, String databaseMaster, String user, String password, String securityLogin) This method sets up the database.maxTime()
The maximum date-time value allowed in this database as a string.final Timestamp
Maximum date-time value converted to Timestamp.abstract String
The name of the model database.abstract String
name()
The name of the driver.int
Number of nano digits to be used in the format(Timestamp) method.parentTable
(String tableName) protected abstract boolean
resetPassword
(String user, String securityPassword, String newPassword) Reset the password of the user, given the security password.boolean
schemaExists
(String schemaName) final String
THe name of the security superuser of the database.boolean
tableExists
(String tableName) abstract String
userSQL()
The SQL string that can return the login name of the current user as a column value.boolean
validateSecurityPassword
(String password) boolean
viewExists
(String viewName)
-
Constructor Details
-
Database
protected Database()
-
-
Method Details
-
login
public static void login(String driver, String ip, int port, String database, String databaseMaster, String user, String password, String securityLogin) throws Exception This method sets up the database.- Parameters:
driver
- Name of the database driverip
- The IP address in dotted decimal form or the DNS entry of the server on which database is running.port
- The TCP port number on which the database is listening.database
- Name of the database to connect to.databaseMaster
- Name of the master database to connect to.user
- The login name of the user. If a database name to be specified, pass it along with the login name separated by a slash ("/") character. For example: "john/inventory".password
- The password of the user.securityLogin
- Security login (For example, for PostgreSQL, the default is "postgres").- Throws:
Exception
- Any
-
get
-
lockDatabase
public static void lockDatabase()Locks the database so that login() method can not be invoked again. -
name
The name of the driver. For example, Sybase Adaptive Server Enterprise driver name is "ASE".- Returns:
- The database driver name
-
securityLogin
THe name of the security superuser of the database. For example, in the case of Sybase Adaptive Server Enterprise, it will return "sa".- Returns:
- The login name of the security superuser.
-
modelDatabase
The name of the model database. Such a concept is there in some database systems.- Returns:
- The name of the model database. Null string may be returned if such a concept does not exist.
-
userSQL
The SQL string that can return the login name of the current user as a column value.- Returns:
- SQL that can be executed to get the name of the current user.
-
databaseSQL
-
isALogin
-
isALogin
-
initialPassword
The initial password to be used when a user is created for the first time. The default value is "welcome" without quotes.- Returns:
- The initial password.
-
createLogin
-
dropLogin
-
changePassword
Change password of the user on the SQL connection. This will reset the SQLConnector's password setting if the user is currently connected.- Parameters:
user
- The login name whose password needs to be changed.oldPassword
- Old password.newPassword
- New password.- Returns:
- True if the password is successfully changed.
-
alterPassword
Change password of the user on the SQL connection.- Parameters:
user
- The login name whose password needs to be changed.oldPassword
- Old password.newPassword
- New password.- Returns:
- True if the password is successfully changed.
-
resetPassword
Reset the password of the user, given the security password.- Parameters:
user
- The login name whose password needs to be changed.securityPassword
- The security superuser's passwordnewPassword
- The password for the user.- Returns:
- True if the password is successfully changed.
-
maxTime
The maximum date-time value allowed in this database as a string. For example, on the Sybase Adaptive Server Enterprise, the value returned will be "7910-12-31 23:59:59". Generally, it will be returned as 1 second less than the maximum allowed value in the database. The default implementation returns "7910-12-31 23:59:59" and it may be good enough for other database systems. But, it needs to be changed if the database system supports only values less than this.- Returns:
- The maximum date-time value allowed.
-
currentTime
The SQL column that can bring the current time from the database. For example, in the case of Sybase Adaptive Server Enterprise, it will return "getdate()".- Returns:
- The SQL column that can bring the current time from the database.
-
getCurrentTime
-
currentTimeSQL
The SQL statement that can fetch the current time from the database as first column in its result set. The default implementation returns "SELECT X" where X is the value returned by the currentTime() method.- Returns:
- The SQL statement.
-
callProc
Get the SQL statement string to call the stored procedure. For example, in the case of Sybase Adaptive Server Enterprise, if a procedure called "AuthorizeTran" needs to be invoked with parameters "25, 345, 23", this method will return "EXEC AuthorizeTran 25, 345, 23".- Parameters:
name
- The name of the stored procedure.parameter
- Parameter to the stored procedure.- Returns:
- The SQL statement to execute for invoking the stored procedure.
-
getSerial
Get the SQL statement for generating next Id for the tag passed.- Parameters:
tag
- Tag for which statement is needed.- Returns:
- Serial SQL for the tag
-
getObjectClassName
Get the name of Object class of this database.- Returns:
- Full name of the class
-
format
-
formatWithTime
Format a date value with time component that can be sent to the database as part of a SQL query. It should return typically in a format similar to "yyyy-MM-dd HH:mm:ss.ssssss". Please note the nanos. Some databases may not support so many digits in the nanos and in such cases, only up to the number of digits supported should be returned. The default implementation returns the number of digits returned by the method nanoDigits(). So, if you just want to adjust the number of nano digits returned, override the nanoDigits() method.- Type Parameters:
D
- Date type- Parameters:
date
- Date to be formatted- Returns:
- Formatted date.
-
nanoDigits
public int nanoDigits()Number of nano digits to be used in the format(Timestamp) method. The default implementation returns 6.- Returns:
- Number of nano digits.
-
executeSQL
-
executeSQL
-
executeSQL
-
executeSQL
-
executeSQL
-
maxTimeStamp
Maximum date-time value converted to Timestamp.- Returns:
- The maximum date-time value defined by maxTime() method converted to Timestamp.
-
getSQL
A convenient method to create RawSQL object for executing adhoc SQL statements.- Returns:
- A properly constructed RawSQL object.
-
schemaExists
-
databaseExists
Check if a database exists or not.- Parameters:
databaseName
- Database name.- Returns:
- True/false.
-
tableExists
-
viewExists
-
foreignKeyConstraintNames
-
foreignKeyConstraints
-
dependentConstraints
-
parentTable
-
columnType
-
columnDetails
-
dropDatabaseDDL
-
dropDatabase
-
createDatabaseDDL
-
createDatabase
-
createSchemaDDL
-
createSchema
-
createTable
public boolean createTable(Class<? extends StoredObject> objectClass, String securityPassword) throws Exception - Throws:
Exception
-
validateSecurityPassword
-
isMaster
public static boolean isMaster() -
ensureMaster
- Throws:
SOException
-