public final class Database extends Object implements DatabaseConstants
This class encapsulates the top level behaviour of a database. That is of creating itself, initializing itself, shutting itself down, deleting itself, creating/dropping a table, updating a table. It is not the responsibility of this class to handle table behaviour above this. Top level table behaviour is handled by DataTable through the DatabaseConnection interface.
The Database object is also responsible for various database management functions such a creating, editing and removing users, triggers, functions and services.
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_SCHEMA
The name of the default schema.
|
static String |
INTERNAL_SECURE_USERNAME
The username of the internal secure user.
|
static String |
JDBC_SCHEMA
The name of the schema that contains JDBC helper tables.
|
static String |
LOCK_GROUP
The name of the lock group.
|
static TableName |
NEW_TRIGGER_TABLE
The NEW table used inside a triggered procedure to represent a triggered
row after the operation occurs.
|
static TableName |
OLD_TRIGGER_TABLE
The OLD table used inside a triggered procedure to represent a triggered
row before the operation occurs.
|
static String |
SCHEMA_MANAGER_GROUP
The name of the schema manager group.
|
static String |
SECURE_GROUP
THe name of the secure access group.
|
static TableName |
SYS_DATA_TRIGGER
The system internally generated 'sUSRDataTrigger' table.
|
static TableName |
SYS_DB_STATISTICS
The system internally generated 'sUSRDatabaseStatistics' table.
|
static TableName |
SYS_FUNCTION
The function table.
|
static TableName |
SYS_FUNCTIONFACTORY
The function factory table.
|
static TableName |
SYS_GRANTS |
static TableName |
SYS_LABEL
The label table.
|
static TableName |
SYS_PASSWORD
The password privs and grants table.
|
static TableName |
SYS_SERVICE
The services table.
|
static TableName |
SYS_TABLE_COLUMNS
The system internally generated 'sUSRTableColumns' table.
|
static TableName |
SYS_TABLE_INFO
The system internally generated 'sUSRTableInfo' table.
|
static TableName |
SYS_USERCONNECT |
static TableName |
SYS_USERPRIV |
static TableName |
SYS_VIEW
The view table.
|
static String |
SYSTEM_SCHEMA
The name of the system schema that contains tables refering to system
information.
|
static String |
USER_MANAGER_GROUP
The name of the user manager group.
|
MAX_DATABASE_NAME_LENGTH, MAX_PASSWORD_LENGTH, MAX_PRIVGROUP_NAME_LENGTH, MAX_TABLE_NAME_LENGTH, MAX_USER_NAME_LENGTH
Constructor and Description |
---|
Database(DatabaseSystem system,
String name)
The Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
addUserToGroup(DatabaseQueryContext context,
String username,
String group)
Adds the user to the given group.
|
void |
alterUserPassword(DatabaseQueryContext context,
String username,
String password)
Alters the password of the user but otherwise does not change any
information about the user.
|
User |
authenticateUser(String username,
String password,
String connection_string)
Tries to authenticate a username/password against this database.
|
boolean |
canUserAlterTableObject(DatabaseQueryContext context,
User user,
TableName table)
Returns true if the user can alter a table or view with the given name,
otherwise returns false.
|
boolean |
canUserCompactTableObject(DatabaseQueryContext context,
User user,
TableName table)
Returns true if the user can compact a table with the given name,
otherwise returns false.
|
boolean |
canUserCreateAndDropSchema(DatabaseQueryContext context,
User user,
String schema)
Returns true if the user is permitted to create and drop schema's in the
database, otherwise returns false.
|
boolean |
canUserCreateAndDropUsers(DatabaseQueryContext context,
User user)
Returns true if the user is permitted to create, alter and drop user
information from the database, otherwise returns false.
|
boolean |
canUserCreateProcedureObject(DatabaseQueryContext context,
User user,
TableName table)
Returns true if the user can create a procedure with the given name,
otherwise returns false.
|
boolean |
canUserCreateSequenceObject(DatabaseQueryContext context,
User user,
TableName table)
Returns true if the user can create a sequence with the given name,
otherwise returns false.
|
boolean |
canUserCreateTableObject(DatabaseQueryContext context,
User user,
TableName table)
Returns true if the user can create a table or view with the given name,
otherwise returns false.
|
boolean |
canUserDeleteFromTableObject(DatabaseQueryContext context,
User user,
TableName table)
Returns true if the user can delete from a table or view with the given
name and given columns, otherwise returns false.
|
boolean |
canUserDropProcedureObject(DatabaseQueryContext context,
User user,
TableName table)
Returns true if the user can drop a procedure with the given name,
otherwise returns false.
|
boolean |
canUserDropSequenceObject(DatabaseQueryContext context,
User user,
TableName table)
Returns true if the user can drop a sequence with the given name,
otherwise returns false.
|
boolean |
canUserDropTableObject(DatabaseQueryContext context,
User user,
TableName table)
Returns true if the user can drop a table or view with the given name,
otherwise returns false.
|
boolean |
canUserExecuteStoredProcedure(DatabaseQueryContext context,
User user,
String procedure_name)
Returns true if the user is allowed to execute the given stored procedure.
|
boolean |
canUserInsertIntoTableObject(DatabaseQueryContext context,
User user,
TableName table,
Variable[] columns)
Returns true if the user can insert into a table or view with the given
name and given columns, otherwise returns false.
|
boolean |
canUserSelectFromTableObject(DatabaseQueryContext context,
User user,
TableName table,
Variable[] columns)
Returns true if the user can select from a table or view with the given
name and given columns, otherwise returns false.
|
boolean |
canUserShutDown(DatabaseQueryContext context,
User user)
Returns true if the user can shut down the database server.
|
boolean |
canUserUpdateTableObject(DatabaseQueryContext context,
User user,
TableName table,
Variable[] columns)
Returns true if the user can update a table or view with the given
name and given columns, otherwise returns false.
|
boolean |
convertToCurrent(PrintStream out,
String admin_username)
Functionality for converting and old database format to the existing
format.
|
void |
create(String username,
String password)
Creates and sets up a new database to an initial empty state.
|
Object |
createEvent(Runnable runner)
Creates an event for the database dispatcher.
|
DatabaseConnection |
createNewConnection(User user,
DatabaseConnection.CallBack call_back)
Returns a new DatabaseConnection instance that is used against this
database.
|
void |
createUser(DatabaseQueryContext context,
String username,
String password)
Creates and adds a new user to this database.
|
DebugLogger |
Debug()
Returns the DebugLogger implementation from the DatabaseSystem.
|
void |
deleteAllUserGroups(DatabaseQueryContext context,
String username)
Deletes all the groups the user belongs to.
|
void |
deleteUser(DatabaseQueryContext context,
String username)
Deletes the user from the system.
|
void |
execute(User user,
DatabaseConnection database,
Runnable runner)
Executes database functions from the 'run' method of the given runnable
instance on the first available worker thread.
|
boolean |
exists()
Returns true if the database exists.
|
void |
finalize() |
Log |
getCommandsLog()
Returns the log file where commands are recorded.
|
com.mckoi.database.DataCellCache |
getDataCellCache()
Returns the system DataCellCache.
|
DatabaseProcedure |
getDBProcedure(String procedure_name,
DatabaseConnection connection)
Resolves a procedure name into a DBProcedure object.
|
String |
getName()
Returns the name of this database.
|
Table |
getSingleRowTable()
Returns a static table that has a single row but no columns.
|
DatabaseSystem |
getSystem()
Returns the DatabaseSystem that this Database is from.
|
com.mckoi.database.TriggerManager |
getTriggerManager()
Returns the system trigger manager.
|
UserManager |
getUserManager()
Returns the system user manager.
|
void |
grantHostAccessToUser(DatabaseQueryContext context,
String user,
String protocol,
String host)
Grants the given user access to connect to the database from the
given host address.
|
String[] |
groupsUserBelongsTo(DatabaseQueryContext context,
String username)
Returns the list of all user groups the user belongs to.
|
boolean |
hasShutDown()
Returns true if the database has shut down.
|
void |
init()
Initializes the database.
|
boolean |
isInitialized()
Returns true if the database is initialised.
|
boolean |
isReadOnly()
Returns true if this database is in read only mode.
|
void |
liveCopyTo(File path)
Copies all the persistent data in this database (the conglomerate) to the
given destination path.
|
void |
postEvent(int time,
Object event)
Posts an event on the database dispatcher.
|
void |
registerShutDownDelegate(Runnable delegate)
Registers the delegate that is executed when the shutdown thread is
activated.
|
void |
setDeleteOnShutdown(boolean status)
If the 'deleteOnShutdown' flag is set, the database will delete the
database from the file system when it is shutdown.
|
void |
setIsExecutingCommands(boolean status)
Controls whether the database is allowed to execute commands or not.
|
void |
setupSystemFunctions(DatabaseConnection connection,
String admin_user)
Sets all the standard functions and procedures available to engine.
|
void |
setUserLock(DatabaseQueryContext context,
User user,
boolean lock_status)
Sets the lock status for the given user.
|
void |
shutdown()
Cleanly shuts down the database.
|
void |
startShutDownThread()
Starts the shutdown thread which should contain delegates that shut the
database and all its resources down.
|
Stats |
stats()
Convenience static for accessing the global Stats object.
|
com.mckoi.database.StoreSystem |
storeSystem()
Returns the StoreSystem for this Database.
|
boolean |
userBelongsToGroup(DatabaseQueryContext context,
String username,
String group)
Returns true if the given user belongs to the given group otherwise
returns false.
|
boolean |
userExists(DatabaseQueryContext context,
String username)
Returns true if a user exists in this database, otherwise returns
false.
|
void |
waitUntilShutdown()
Blocks until the database has shut down.
|
public static final String SYSTEM_SCHEMA
public static final String DEFAULT_SCHEMA
public static final String JDBC_SCHEMA
public static final TableName SYS_PASSWORD
public static final TableName SYS_USERCONNECT
public static final TableName SYS_USERPRIV
public static final TableName SYS_GRANTS
public static final TableName SYS_SERVICE
public static final TableName SYS_FUNCTIONFACTORY
public static final TableName SYS_FUNCTION
public static final TableName SYS_VIEW
public static final TableName SYS_LABEL
public static final TableName SYS_TABLE_COLUMNS
public static final TableName SYS_TABLE_INFO
public static final TableName SYS_DATA_TRIGGER
public static final TableName SYS_DB_STATISTICS
public static final TableName OLD_TRIGGER_TABLE
public static final TableName NEW_TRIGGER_TABLE
public static final String LOCK_GROUP
public static final String SECURE_GROUP
public static final String USER_MANAGER_GROUP
public static final String SCHEMA_MANAGER_GROUP
public static final String INTERNAL_SECURE_USERNAME
public Database(DatabaseSystem system, String name)
public String getName()
public boolean isReadOnly()
public Log getCommandsLog()
public DatabaseConnection createNewConnection(User user, DatabaseConnection.CallBack call_back)
When a new connection is made on this database, this method is called to create a new DatabaseConnection instance for the connection. This connection handles all transactional queries and modifications to the database.
public User authenticateUser(String username, String password, String connection_string)
This method also returns null if a user exists but was denied access from the given host string. The given 'host_name' object is formatted in the database host connection encoding. This method checks all the values from the sUSRUserConnectPriv table for this user for the given protocol. It first checks if the user is specifically DENIED access from the given host. It then checks if the user is ALLOWED access from the given host. If a host is neither allowed or denied then it is denied.
public boolean userExists(DatabaseQueryContext context, String username) throws DatabaseException
NOTE: Assumes exclusive lock on DatabaseConnection.
DatabaseException
public void createUser(DatabaseQueryContext context, String username, String password) throws DatabaseException
If the user is already defined by the database then an error is generated.
NOTE: Assumes exclusive lock on DatabaseConnection.
DatabaseException
public void deleteAllUserGroups(DatabaseQueryContext context, String username) throws DatabaseException
NOTE: Assumes exclusive lock on DatabaseConnection.
DatabaseException
public void deleteUser(DatabaseQueryContext context, String username) throws DatabaseException
NOTE: Assumes exclusive lock on DatabaseConnection.
DatabaseException
public void alterUserPassword(DatabaseQueryContext context, String username, String password) throws DatabaseException
NOTE: Assumes exclusive lock on DatabaseConnection.
DatabaseException
public String[] groupsUserBelongsTo(DatabaseQueryContext context, String username) throws DatabaseException
DatabaseException
public boolean userBelongsToGroup(DatabaseQueryContext context, String username, String group) throws DatabaseException
NOTE: Assumes exclusive lock on DatabaseConnection.
DatabaseException
public void addUserToGroup(DatabaseQueryContext context, String username, String group) throws DatabaseException
It is important that any security checks for ensuring the grantee is allowed to give the user these privs are preformed before this method is called.
NOTE: Assumes exclusive lock on DatabaseConnection.
DatabaseException
public void setUserLock(DatabaseQueryContext context, User user, boolean lock_status) throws DatabaseException
It is important that any security checks to determine if the process setting the user lock is allowed to do it is done before this method is called.
NOTE: Assumes exclusive lock on DatabaseConnection.
DatabaseException
public void grantHostAccessToUser(DatabaseQueryContext context, String user, String protocol, String host) throws DatabaseException
DatabaseException
public boolean canUserCreateAndDropUsers(DatabaseQueryContext context, User user) throws DatabaseException
DatabaseException
public boolean canUserCreateAndDropSchema(DatabaseQueryContext context, User user, String schema) throws DatabaseException
DatabaseException
public boolean canUserShutDown(DatabaseQueryContext context, User user) throws DatabaseException
DatabaseException
public boolean canUserExecuteStoredProcedure(DatabaseQueryContext context, User user, String procedure_name) throws DatabaseException
DatabaseException
public boolean canUserCreateTableObject(DatabaseQueryContext context, User user, TableName table) throws DatabaseException
DatabaseException
public boolean canUserAlterTableObject(DatabaseQueryContext context, User user, TableName table) throws DatabaseException
DatabaseException
public boolean canUserDropTableObject(DatabaseQueryContext context, User user, TableName table) throws DatabaseException
DatabaseException
public boolean canUserSelectFromTableObject(DatabaseQueryContext context, User user, TableName table, Variable[] columns) throws DatabaseException
DatabaseException
public boolean canUserInsertIntoTableObject(DatabaseQueryContext context, User user, TableName table, Variable[] columns) throws DatabaseException
DatabaseException
public boolean canUserUpdateTableObject(DatabaseQueryContext context, User user, TableName table, Variable[] columns) throws DatabaseException
DatabaseException
public boolean canUserDeleteFromTableObject(DatabaseQueryContext context, User user, TableName table) throws DatabaseException
DatabaseException
public boolean canUserCompactTableObject(DatabaseQueryContext context, User user, TableName table) throws DatabaseException
DatabaseException
public boolean canUserCreateProcedureObject(DatabaseQueryContext context, User user, TableName table) throws DatabaseException
DatabaseException
public boolean canUserDropProcedureObject(DatabaseQueryContext context, User user, TableName table) throws DatabaseException
DatabaseException
public boolean canUserCreateSequenceObject(DatabaseQueryContext context, User user, TableName table) throws DatabaseException
DatabaseException
public boolean canUserDropSequenceObject(DatabaseQueryContext context, User user, TableName table) throws DatabaseException
DatabaseException
public void setupSystemFunctions(DatabaseConnection connection, String admin_user) throws DatabaseException
DatabaseException
public void create(String username, String password)
The 'username' and 'password' parameter given are set for the administrator account.
public void init() throws DatabaseException
NOTE: Perhaps a better name for this method is 'open'.
DatabaseException
public void shutdown() throws DatabaseException
The main purpose of this method is to ensure any tables that are backed by files and in a 'safe' state and cleanly flushed to the file system.
If 'delete_on_shutdown' is true, the database will delete itself from the file system when it shuts down.
DatabaseException
public boolean exists()
public final void setDeleteOnShutdown(boolean status)
NOTE: Use with care - if this is set to true and the database is shutdown it will result in total loss of data.
public boolean isInitialized()
public void liveCopyTo(File path) throws IOException
IOException
public boolean convertToCurrent(PrintStream out, String admin_username) throws IOException
Returns true if the convert was successful or false if it wasn't (error message is output to the PrintWriter).
IOException
public DatabaseProcedure getDBProcedure(String procedure_name, DatabaseConnection connection) throws DatabaseException
ISSUE: Move this to DatabaseSystem?
DatabaseException
public final DatabaseSystem getSystem()
public final com.mckoi.database.StoreSystem storeSystem()
public final Stats stats()
public final DebugLogger Debug()
public final com.mckoi.database.TriggerManager getTriggerManager()
public final UserManager getUserManager()
public final Object createEvent(Runnable runner)
public final void postEvent(int time, Object event)
public final com.mckoi.database.DataCellCache getDataCellCache()
public final boolean hasShutDown()
public final void startShutDownThread()
public final void waitUntilShutdown()
public final void execute(User user, DatabaseConnection database, Runnable runner)
public final void registerShutDownDelegate(Runnable delegate)
public final void setIsExecutingCommands(boolean status)
public final Table getSingleRowTable()
Copyright © 2015. All rights reserved.