Class PostgresDB
- java.lang.Object
-
- nz.co.gregs.dbvolution.databases.DBDatabase
-
- nz.co.gregs.dbvolution.databases.PostgresDB
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Cloneable,SupportsPolygonDatatype
- Direct Known Subclasses:
PostgresDBOverSSL
public class PostgresDB extends DBDatabase implements SupportsPolygonDatatype
A DBDatabase tweaked for PostgreSQL.Support DBvolution at Patreon
- Author:
- Gregory Graham
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class nz.co.gregs.dbvolution.databases.DBDatabase
DBDatabase.ResponseToException, DBDatabase.RunRegularProcessors
-
-
Field Summary
Fields Modifier and Type Field Description static intPOSTGRES_DEFAULT_PORTThe default port number used by PostgreSQL.static java.lang.StringPOSTGRES_DEFAULT_USERNAMEThe default username used by PostgreSQL.static longserialVersionUID-
Fields inherited from class nz.co.gregs.dbvolution.databases.DBDatabase
storedConnection
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedPostgresDB()Provides a convenient constructor for DBDatabases that have configuration details hardwired or are able to automatically retrieve the details.PostgresDB(java.lang.String hostname, int port, java.lang.String databaseName, java.lang.String username, java.lang.String password)Creates a PostgreSQL connection to the server on the port supplied, using the username and password supplied.PostgresDB(java.lang.String hostname, int port, java.lang.String databaseName, java.lang.String username, java.lang.String password, java.lang.String urlExtras)Creates a PostgreSQL connection to the server on the port supplied, using the username and password supplied.PostgresDB(java.lang.String jdbcURL, java.lang.String username, java.lang.String password)Creates a PostgreSQL connection for the JDBC URL, username, and password.PostgresDB(java.lang.String databaseName, java.lang.String username, java.lang.String password, java.lang.String urlExtras)Creates a PostgreSQL connection to local computer("localhost") on the default port(5432) using the username and password supplied.PostgresDB(javax.sql.DataSource ds)Creates a PostgreSQL connection for the DataSource.PostgresDB(DatabaseConnectionSettings dcs)Creates a PostgreSQL connection for the DatabaseConnectionSettings.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidaddDatabaseSpecificFeatures(java.sql.Statement stmnt)Used By Subclasses To Inject Datatypes, Functions, Etc Into the Database.DBDatabase.ResponseToExceptionaddFeatureToFixException(java.lang.Exception exp)Used to add features in a just-in-time manner.DBDatabaseclone()Clones the DBDatabase.voidcreateDatabase(java.lang.String databaseName)Create a new database/schema on this database server.voidcreateUser(java.lang.String username, java.lang.String password)Create a new database/schema on this database server.<TR extends DBRow>
voiddropTableNoExceptions(TR tableRow)Drops a table from the database.protected java.lang.Class<? extends DBDatabase>getBaseDBDatabaseClass()java.lang.IntegergetDefaultPort()protected DatabaseConnectionSettingsgetSettingsFromJDBCURL(java.lang.String jdbcURL)protected java.lang.StringgetUrlFromSettings(DatabaseConnectionSettings settings)intloadFromCSVFile(DBRow table, java.io.File file, java.lang.String delimiter, java.lang.String nullValue, java.lang.String escapeCharacter, java.lang.String quoteCharacter)Assumes that the database and application are on the the same machine.-
Methods inherited from class nz.co.gregs.dbvolution.databases.DBDatabase
addRegularProcess, backupToDBDatabase, batchSQLStatementsWhenPossible, createForeignKeyConstraints, createIndexesOnAllFields, createOrUpdateTable, createTable, createTableNoExceptions, createTableNoExceptions, createTablesNoExceptions, createTablesNoExceptions, createTablesWithForeignKeysNoExceptions, createTableWithForeignKeys, delete, delete, deleteAll, discardConnection, doReadOnlyTransaction, doTransaction, doTransaction, dropAnyAssociatedDatabaseObjects, dropDatabase, dropDatabase, dropTable, dropTableIfExists, equals, executeDBAction, executeDBQuery, get, get, get, get, get, getAllRows, getBatchSQLStatementsWhenPossible, getByExample, getByExample, getByExamples, getConnection, getConnectionFromDriverManager, getCount, getCount, getDatabaseInstance, getDatabaseName, getDataSource, getDBMigration, getDBQuery, getDBQuery, getDBQuery, getDBQuery, getDBQueryInsert, getDBRecursiveQuery, getDBStatement, getDBTable, getDefinition, getDriverName, getExtras, getHost, getJdbcURL, getLabel, getLastException, getLowLevelStatement, getPassword, getPort, getPreventAccidentalDroppingOfDatabases, getPreventAccidentalDroppingOfTables, getPrintSQLBeforeExecuting, getRegularProcessors, getRegularThreadPool, getRows, getSchema, getSettings, getSQLForCreateTable, getSQLForDBQuery, getUsername, hashCode, implement, insert, insert, insert, insertOrUpdate, isDBDatabaseCluster, isMemoryDatabase, isPrintSQLBeforeExecuting, preventDDLDuringTransaction, preventDroppingOfDatabases, preventDroppingOfTables, print, printSQLIfRequested, removeForeignKeyConstraints, removeRegularProcess, save, save, save, setBatchSQLStatementsWhenPossible, setDatabaseName, setDefinition, setDefinitionBasedOnConnectionMetaData, setDriverName, setJdbcURL, setLabel, setLastException, setPassword, setPrintSQLBeforeExecuting, setSettings, setUsername, startServerIfRequired, stop, supportsPooledConnections, tableExists, test, toString, unusedConnection, update, update, updateAnyway, updateTableToMatchDBRow, willCreateBlankQuery
-
-
-
-
Field Detail
-
serialVersionUID
public static final long serialVersionUID
- See Also:
- Constant Field Values
-
POSTGRES_DEFAULT_PORT
public static final int POSTGRES_DEFAULT_PORT
The default port number used by PostgreSQL.- See Also:
- Constant Field Values
-
POSTGRES_DEFAULT_USERNAME
public static final java.lang.String POSTGRES_DEFAULT_USERNAME
The default username used by PostgreSQL.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
PostgresDB
protected PostgresDB()
Provides a convenient constructor for DBDatabases that have configuration details hardwired or are able to automatically retrieve the details.This constructor creates an empty DBDatabase with only the default settings, in particular with no driver, URL, username, password, or
DBDefinitionMost programmers should not call this constructor directly. Instead you should define a no-parameter constructor that supplies the details for creating an instance using a more complete constructor.
DBDatabase encapsulates the knowledge of the database, in particular the syntax of the database in the DBDefinition and the connection details from a DataSource.
- See Also:
DBDefinition
-
PostgresDB
public PostgresDB(javax.sql.DataSource ds) throws java.sql.SQLExceptionCreates a PostgreSQL connection for the DataSource.- Parameters:
ds- ds- Throws:
java.sql.SQLException- database errors
-
PostgresDB
public PostgresDB(DatabaseConnectionSettings dcs) throws java.sql.SQLException
Creates a PostgreSQL connection for the DatabaseConnectionSettings.- Parameters:
dcs- dcs- Throws:
java.sql.SQLException- database errors
-
PostgresDB
public PostgresDB(java.lang.String jdbcURL, java.lang.String username, java.lang.String password) throws java.sql.SQLExceptionCreates a PostgreSQL connection for the JDBC URL, username, and password.- Parameters:
jdbcURL- jdbcURLusername- usernamepassword- password- Throws:
java.sql.SQLException- database errors
-
PostgresDB
public PostgresDB(java.lang.String hostname, int port, java.lang.String databaseName, java.lang.String username, java.lang.String password) throws java.sql.SQLExceptionCreates a PostgreSQL connection to the server on the port supplied, using the username and password supplied.- Parameters:
hostname- hostnameport- portdatabaseName- databaseNameusername- usernamepassword- password- Throws:
java.sql.SQLException- database errors
-
PostgresDB
public PostgresDB(java.lang.String hostname, int port, java.lang.String databaseName, java.lang.String username, java.lang.String password, java.lang.String urlExtras) throws java.sql.SQLExceptionCreates a PostgreSQL connection to the server on the port supplied, using the username and password supplied.Extra parameters to be added to the JDBC URL can be included in the urlExtras parameter.
- Parameters:
hostname- hostnamepassword- passworddatabaseName- databaseNameport- portusername- usernameurlExtras- urlExtras- Throws:
java.sql.SQLException- database errors
-
PostgresDB
public PostgresDB(java.lang.String databaseName, java.lang.String username, java.lang.String password, java.lang.String urlExtras) throws java.sql.SQLExceptionCreates a PostgreSQL connection to local computer("localhost") on the default port(5432) using the username and password supplied.Extra parameters to be added to the JDBC URL can be included in the urlExtras parameter.
- Parameters:
databaseName- databaseNameusername- usernamepassword- passwordurlExtras- urlExtras- Throws:
java.sql.SQLException- database errors
-
-
Method Detail
-
getUrlFromSettings
protected java.lang.String getUrlFromSettings(DatabaseConnectionSettings settings)
- Specified by:
getUrlFromSettingsin classDBDatabase
-
clone
public DBDatabase clone() throws java.lang.CloneNotSupportedException
Description copied from class:DBDatabaseClones the DBDatabase.Support DBvolution at Patreon
- Overrides:
clonein classDBDatabase- Returns:
- a clone of the DBDatabase.
- Throws:
java.lang.CloneNotSupportedException- not likely
-
loadFromCSVFile
public int loadFromCSVFile(DBRow table, java.io.File file, java.lang.String delimiter, java.lang.String nullValue, java.lang.String escapeCharacter, java.lang.String quoteCharacter) throws java.sql.SQLException
Assumes that the database and application are on the the same machine.- Parameters:
table- the table to be loadedfile- the file to load data fromdelimiter- the separator between the values of each rownullValue- the string that represents NULL in this file.escapeCharacter- the character that escapes special valuesquoteCharacter- the character the surrounds strings.Support DBvolution at Patreon
- Returns:
- either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing 1 Database exceptions may be thrown
- Throws:
java.sql.SQLException- database exceptions may be thrown.
-
createDatabase
public void createDatabase(java.lang.String databaseName) throws java.sql.SQLExceptionCreate a new database/schema on this database server.Generally requires all sorts of privileges and is best performed by database administrator (DBA).
- Parameters:
databaseName- the name of the new database- Throws:
java.sql.SQLException- database exceptions may be thrown
-
createUser
public void createUser(java.lang.String username, java.lang.String password) throws java.sql.SQLExceptionCreate a new database/schema on this database server.Generally requires all sorts of privileges and is best performed by database administrator (DBA).
- Parameters:
username- The user to be createdpassword- the password the user will use.- Throws:
java.sql.SQLException- database exceptions may be throwns.
-
dropTableNoExceptions
public <TR extends DBRow> void dropTableNoExceptions(TR tableRow) throws AccidentalDroppingOfTableException, AutoCommitActionDuringTransactionException
Description copied from class:DBDatabaseDrops a table from the database.The easy way to drop a table that might not exist. Will still throw a AutoCommitActionDuringTransactionException if you use it during a transaction or AccidentalDroppingOfTableException if dropping tables is being prevented by DBvolution.
An even worse idea than
DBDatabase.dropTable(nz.co.gregs.dbvolution.DBRow)In General NEVER USE THIS METHOD.
Seriously NEVER USE THIS METHOD.
Your DBA will murder you.
- Overrides:
dropTableNoExceptionsin classDBDatabase- Type Parameters:
TR- DBRow type- Parameters:
tableRow- tableRow- Throws:
AccidentalDroppingOfTableExceptionAutoCommitActionDuringTransactionException
-
addDatabaseSpecificFeatures
protected void addDatabaseSpecificFeatures(java.sql.Statement stmnt) throws java.sql.SQLExceptionDescription copied from class:DBDatabaseUsed By Subclasses To Inject Datatypes, Functions, Etc Into the Database.- Specified by:
addDatabaseSpecificFeaturesin classDBDatabase- Parameters:
stmnt- the statement to use when adding features, DO NOT CLOSE THIS STATEMENT.- Throws:
java.sql.SQLException- database exceptions may occur- See Also:
PostgresDB,H2DB,SQLiteDB,OracleDB,MSSQLServerDB,MySQLDB
-
addFeatureToFixException
public DBDatabase.ResponseToException addFeatureToFixException(java.lang.Exception exp) throws java.lang.Exception
Used to add features in a just-in-time manner.During a statement the database may throw an exception because a feature has not yet been added. Use this method to parse the exception and install the required feature.
The statement will be automatically run after this method exits.
- Overrides:
addFeatureToFixExceptionin classDBDatabase- Parameters:
exp- the exception throw by the database that may need fixing- Returns:
- the suggested response to this exception
- Throws:
java.sql.SQLException- accessing the database may cause exceptionsSQLException- accessing the database may cause exceptionsjava.lang.Exception
-
getSettingsFromJDBCURL
protected DatabaseConnectionSettings getSettingsFromJDBCURL(java.lang.String jdbcURL)
- Specified by:
getSettingsFromJDBCURLin classDBDatabase
-
getDefaultPort
public java.lang.Integer getDefaultPort()
- Specified by:
getDefaultPortin classDBDatabase
-
getBaseDBDatabaseClass
protected java.lang.Class<? extends DBDatabase> getBaseDBDatabaseClass()
- Specified by:
getBaseDBDatabaseClassin classDBDatabase
-
-