Class MySQLAdapter

java.lang.Object
org.datanucleus.store.rdbms.adapter.BaseDatastoreAdapter
org.datanucleus.store.rdbms.adapter.MySQLAdapter
All Implemented Interfaces:
DatastoreAdapter

public class MySQLAdapter extends BaseDatastoreAdapter
Provides methods for adapting SQL language elements to the MySQL database. Note that this also currently supports the MariaDB database. We could contemplate splitting this into separate MySQL and MariaDB support at some point, but one of the issues to overcome with that is that "datanucleus-geospatial" adds support for Geospatial types, including MySQL types and extends this class. We would need to have an equivalent extension for MariaDB.
  • Field Details

    • NONSQL92_RESERVED_WORDS

      public static final String NONSQL92_RESERVED_WORDS
      A string containing the list of MySQL keywords that are not also SQL/92 reserved words, separated by commas. This list is normally obtained dynamically from the driver using DatabaseMetaData.getSQLKeywords(), but MySQL drivers are known to return an incomplete list.

      This list was produced based on the reserved word list in the MySQL Manual (Version 4.0.10-gamma) at http://www.mysql.com/doc/en/Reserved_words.html.

      See Also:
    • isMariaDB

      boolean isMariaDB
    • needsQuoting

      private static final Pattern needsQuoting
  • Constructor Details

    • MySQLAdapter

      public MySQLAdapter(DatabaseMetaData metadata)
      Constructor. Overridden so we can add on our own list of NON SQL92 reserved words which is returned incorrectly with the JDBC driver.
      Parameters:
      metadata - MetaData for the DB
  • Method Details

    • initialiseTypes

      public void initialiseTypes(org.datanucleus.store.schema.StoreSchemaHandler handler, org.datanucleus.store.connection.ManagedConnection mconn)
      Initialise the types for this datastore.
      Specified by:
      initialiseTypes in interface DatastoreAdapter
      Overrides:
      initialiseTypes in class BaseDatastoreAdapter
      Parameters:
      handler - SchemaHandler that we initialise the types for
      mconn - Managed connection to use
    • getVendorID

      public String getVendorID()
      Description copied from interface: DatastoreAdapter
      Accessor for the Vendor ID for this datastore.
      Specified by:
      getVendorID in interface DatastoreAdapter
      Overrides:
      getVendorID in class BaseDatastoreAdapter
      Returns:
      Vendor id for this datastore
    • isReservedKeyword

      public boolean isReservedKeyword(String word)
      Description copied from class: BaseDatastoreAdapter
      Tests if a given string is an SQL keyword.

      The list of key words tested against is defined to contain all SQL/92 keywords, plus any additional key words reported by the JDBC driver for this adapter via DatabaseMetaData.getSQLKeywords().

      In general, use of a SQL key word as an identifier should be avoided. SQL/92 key words are divided into reserved and non-reserved words. If a reserved word is used as an identifier it must be quoted with double quotes. Strictly speaking, the same is not true of non-reserved words. However, as C.J. Date writes in A Guide To The SQL Standard :

      The rule by which it is determined within the standard that one key word needs to be reserved while another need not is not clear to this writer. In practice, it is probably wise to treat all key words as reserved.
      Specified by:
      isReservedKeyword in interface DatastoreAdapter
      Overrides:
      isReservedKeyword in class BaseDatastoreAdapter
      Parameters:
      word - The word to test.
      Returns:
      true if word is a SQL key word for this DBMS. The comparison is case-insensitive.
    • needsQuoting

      private static boolean needsQuoting(String columnName)
    • newRDBMSColumnInfo

      public RDBMSColumnInfo newRDBMSColumnInfo(ResultSet rs)
      Method to create a column info for the current row. Overrides the dataType for BLOB/CLOB as necessary
      Specified by:
      newRDBMSColumnInfo in interface DatastoreAdapter
      Overrides:
      newRDBMSColumnInfo in class BaseDatastoreAdapter
      Parameters:
      rs - ResultSet from DatabaseMetaData.getColumns()
      Returns:
      column info
    • newSQLTypeInfo

      public SQLTypeInfo newSQLTypeInfo(ResultSet rs)
      Description copied from interface: DatastoreAdapter
      Create a new SQL type info from the current row of the passed ResultSet. Allows an adapter to override particular types where the JDBC driver is known to be buggy.
      Specified by:
      newSQLTypeInfo in interface DatastoreAdapter
      Overrides:
      newSQLTypeInfo in class BaseDatastoreAdapter
      Parameters:
      rs - ResultSet
      Returns:
      The SQL type info
    • getCreateDatabaseStatement

      public String getCreateDatabaseStatement(String catalogName, String schemaName)
      Description copied from interface: DatastoreAdapter
      Method to return the statement necessary to create a database with this RDBMS. Note that some RDBMS don't support this.
      Specified by:
      getCreateDatabaseStatement in interface DatastoreAdapter
      Overrides:
      getCreateDatabaseStatement in class BaseDatastoreAdapter
      Parameters:
      catalogName - name of the catalog
      schemaName - Name of the schema
      Returns:
      The DDL statement
    • getDropDatabaseStatement

      public String getDropDatabaseStatement(String catalogName, String schemaName)
      Description copied from interface: DatastoreAdapter
      Method to return the statement necessary to drop a database with this RDBMS. Note that some RDBMS don't support this.
      Specified by:
      getDropDatabaseStatement in interface DatastoreAdapter
      Overrides:
      getDropDatabaseStatement in class BaseDatastoreAdapter
      Parameters:
      catalogName - Name of the catalog
      schemaName - Name of the schema
      Returns:
      The DDL statement
    • getAddPrimaryKeyStatement

      public String getAddPrimaryKeyStatement(PrimaryKey pk, IdentifierFactory factory)
      MySQL, when using AUTO_INCREMENT, requires the primary key specified in the CREATE TABLE, so we do nothing here.
      Specified by:
      getAddPrimaryKeyStatement in interface DatastoreAdapter
      Overrides:
      getAddPrimaryKeyStatement in class BaseDatastoreAdapter
      Parameters:
      pk - An object describing the primary key.
      factory - Identifier factory
      Returns:
      The PK statement
    • getCreateTableStatement

      public String getCreateTableStatement(TableImpl table, Column[] columns, Properties props, IdentifierFactory factory)
      Method to return the CREATE TABLE statement. Versions before 5 need INNODB table type selecting for them. It seems, MySQL ≥ 5 still needs innodb in order to support transactions.
      Specified by:
      getCreateTableStatement in interface DatastoreAdapter
      Overrides:
      getCreateTableStatement in class BaseDatastoreAdapter
      Parameters:
      table - The table
      columns - The columns in the table
      props - Properties for controlling the table creation
      factory - Identifier factory
      Returns:
      The creation statement
    • getDropTableStatement

      public String getDropTableStatement(Table table)
      Method to return the DROP TABLE statement.
      Specified by:
      getDropTableStatement in interface DatastoreAdapter
      Overrides:
      getDropTableStatement in class BaseDatastoreAdapter
      Parameters:
      table - The table
      Returns:
      The drop statement
    • getAddColumnStatement

      public String getAddColumnStatement(Table table, Column col)
      Accessor for the SQL statement to add a column to a table.
      Specified by:
      getAddColumnStatement in interface DatastoreAdapter
      Overrides:
      getAddColumnStatement in class BaseDatastoreAdapter
      Parameters:
      table - The table
      col - The column
      Returns:
      The SQL necessary to add the column
    • getDeleteTableStatement

      public String getDeleteTableStatement(SQLTable tbl)
      Method to return the basic SQL for a DELETE TABLE statement. Returns the String as DELETE t1 FROM tbl t1. Doesn't include any where clause.
      Specified by:
      getDeleteTableStatement in interface DatastoreAdapter
      Overrides:
      getDeleteTableStatement in class BaseDatastoreAdapter
      Parameters:
      tbl - The SQLTable to delete
      Returns:
      The delete table string
    • getCreateIndexStatement

      public String getCreateIndexStatement(Index idx, IdentifierFactory factory)
      Description copied from class: BaseDatastoreAdapter
      Returns the appropriate DDL to create an index. It should return something like:
       CREATE [UNIQUE ]INDEX FOO_U1 ON FOO (BAR,BAZ) [Extended Settings]
       
      Specified by:
      getCreateIndexStatement in interface DatastoreAdapter
      Overrides:
      getCreateIndexStatement in class BaseDatastoreAdapter
      Parameters:
      idx - An object describing the index.
      factory - Identifier factory
      Returns:
      The text of the SQL statement.
    • getIdentityLastValueStmt

      public String getIdentityLastValueStmt(Table table, String columnName)
      Accessor for the auto-increment sql statement for this datastore.
      Specified by:
      getIdentityLastValueStmt in interface DatastoreAdapter
      Overrides:
      getIdentityLastValueStmt in class BaseDatastoreAdapter
      Parameters:
      table - Name of the table that the autoincrement is for
      columnName - Name of the column that the autoincrement is for
      Returns:
      The statement for getting the latest auto-increment key
    • getIdentityKeyword

      public String getIdentityKeyword(org.datanucleus.store.StoreManager storeMgr)
      Accessor for the auto-increment keyword for generating DDLs (CREATE TABLEs...).
      Specified by:
      getIdentityKeyword in interface DatastoreAdapter
      Overrides:
      getIdentityKeyword in class BaseDatastoreAdapter
      Parameters:
      storeMgr - The Store Manager
      Returns:
      The keyword for a column using auto-increment
    • getSelectNewUUIDStmt

      public String getSelectNewUUIDStmt()
      The function to creates a unique value of type uniqueidentifier. MySQL generates 36-character hex uuids.
      Specified by:
      getSelectNewUUIDStmt in interface DatastoreAdapter
      Overrides:
      getSelectNewUUIDStmt in class BaseDatastoreAdapter
      Returns:
      The function. e.g. "SELECT uuid()"
    • getRangeByLimitEndOfStatementClause

      public String getRangeByLimitEndOfStatementClause(long offset, long count, boolean hasOrdering)
      Method to return the SQL to append to the WHERE clause of a SELECT statement to handle restriction of ranges using the LIMUT keyword.
      Specified by:
      getRangeByLimitEndOfStatementClause in interface DatastoreAdapter
      Overrides:
      getRangeByLimitEndOfStatementClause in class BaseDatastoreAdapter
      Parameters:
      offset - The offset to return from
      count - The number of items to return
      hasOrdering - Whether ordering is present
      Returns:
      The SQL to append to allow for ranges using LIMIT.
    • getEscapePatternExpression

      public String getEscapePatternExpression()
      The character for escaping patterns.
      Specified by:
      getEscapePatternExpression in interface DatastoreAdapter
      Overrides:
      getEscapePatternExpression in class BaseDatastoreAdapter
      Returns:
      Escape character(s)
    • validToIndexMapping

      public boolean validToIndexMapping(JavaTypeMapping mapping)
      Description copied from interface: DatastoreAdapter
      Method to return whether the specified mapping is indexable. Allows a datastore to not index particular column types.
      Specified by:
      validToIndexMapping in interface DatastoreAdapter
      Overrides:
      validToIndexMapping in class BaseDatastoreAdapter
      Parameters:
      mapping - The mapping
      Returns:
      Whether it is indexable
    • getSequenceCreateStmt

      public String getSequenceCreateStmt(String sequenceName, Integer min, Integer max, Integer start, Integer increment, Integer cacheSize)
      Accessor for the sequence statement to create the sequence.
      Specified by:
      getSequenceCreateStmt in interface DatastoreAdapter
      Overrides:
      getSequenceCreateStmt in class BaseDatastoreAdapter
      Parameters:
      sequenceName - Name of the sequence
      min - Minimum value for the sequence
      max - Maximum value for the sequence
      start - Start value for the sequence
      increment - Increment value for the sequence
      cacheSize - Cache size for the sequence
      Returns:
      The statement for getting the next id from the sequence
    • getSequenceNextStmt

      public String getSequenceNextStmt(String sequenceName)
      Accessor for the statement for getting the next id from the sequence for this datastore.
      Specified by:
      getSequenceNextStmt in interface DatastoreAdapter
      Overrides:
      getSequenceNextStmt in class BaseDatastoreAdapter
      Parameters:
      sequenceName - Name of the sequence
      Returns:
      The statement for getting the next id for the sequence
    • getSQLOperationClass

      public Class<? extends SQLOperation> getSQLOperationClass(String operationName)
      Description copied from interface: DatastoreAdapter
      Accessor for the SQLOperation class for the specified operation (if available for this datastore).
      Specified by:
      getSQLOperationClass in interface DatastoreAdapter
      Overrides:
      getSQLOperationClass in class BaseDatastoreAdapter
      Parameters:
      operationName - operation name
      Returns:
      SQLOperation class (or null if none available)
    • getSQLMethodClass

      public Class<? extends SQLMethod> getSQLMethodClass(String className, String methodName, org.datanucleus.ClassLoaderResolver clr)
      Description copied from interface: DatastoreAdapter
      Accessor for the SQLMethod class for the query invocation of specified class + method name (if available for this datastore).
      Specified by:
      getSQLMethodClass in interface DatastoreAdapter
      Overrides:
      getSQLMethodClass in class BaseDatastoreAdapter
      Parameters:
      className - Name of the class (or null if this is a STATIC method)
      methodName - Method name
      clr - ClassLoader resolver, in case className is a subclass of a supported type
      Returns:
      The SQLMethod class (or null if not defined for this datastore).
    • loadColumnMappings

      protected void loadColumnMappings(org.datanucleus.plugin.PluginManager mgr, org.datanucleus.ClassLoaderResolver clr)
      Load all datastore mappings defined in the associated plugins. We handle RDBMS datastore mappings so refer to rdbms-mapping-class, jdbc-type, sql-type in particular.
      Overrides:
      loadColumnMappings in class BaseDatastoreAdapter
      Parameters:
      mgr - the PluginManager
      clr - the ClassLoaderResolver