001package io.ebean.config.dbplatform.mysql; 002 003import io.ebean.Query; 004import io.ebean.annotation.Platform; 005import io.ebean.config.dbplatform.DatabasePlatform; 006import io.ebean.config.dbplatform.DbPlatformType; 007import io.ebean.config.dbplatform.DbType; 008import io.ebean.config.dbplatform.IdType; 009import io.ebean.config.dbplatform.SqlErrorCodes; 010 011import java.sql.Types; 012 013/** 014 * MySQL specific platform. 015 * <p> 016 * <ul> 017 * <li>supportsGetGeneratedKeys = true</li> 018 * <li>Uses LIMIT OFFSET clause</li> 019 * <li>Uses ` for quoted identifiers</li> 020 * </ul> 021 * </p> 022 */ 023public class MySqlPlatform extends DatabasePlatform { 024 025 public MySqlPlatform() { 026 super(); 027 this.platform = Platform.MYSQL; 028 this.useExtraTransactionOnIterateSecondaryQueries = true; 029 this.selectCountWithAlias = true; 030 this.dbEncrypt = new MySqlDbEncrypt(); 031 this.historySupport = new MySqlHistorySupport(); 032 this.columnAliasPrefix = null; 033 034 this.dbIdentity.setIdType(IdType.IDENTITY); 035 this.dbIdentity.setSupportsGetGeneratedKeys(true); 036 this.dbIdentity.setSupportsIdentity(true); 037 this.dbIdentity.setSupportsSequence(false); 038 039 this.dbDefaultValue.setNow("now(6)"); // must have same precision as TIMESTAMP 040 this.dbDefaultValue.setFalse("0"); 041 this.dbDefaultValue.setTrue("1"); 042 043 044 this.exceptionTranslator = 045 new SqlErrorCodes() 046 .addAcquireLock("1205") 047 .addDuplicateKey("1062", "1169") 048 .addDataIntegrity("630", "839", "840", "893", "1215", "1216", "1217", "1364", "1451", "1452", "1557") 049 .build(); 050 051 this.openQuote = "`"; 052 this.closeQuote = "`"; 053 // use pipe for escaping as it depends if mysql runs in no_backslash_escapes or not. 054 this.likeClauseRaw = "like binary ? escape ''"; 055 this.likeClauseEscaped = "like binary ? escape '|'"; 056 057 this.forwardOnlyHintOnFindIterate = true; 058 this.booleanDbType = Types.BIT; 059 060 dbTypeMap.put(DbType.BIT, new DbPlatformType("tinyint(1)")); 061 dbTypeMap.put(DbType.BOOLEAN, new DbPlatformType("tinyint(1)")); 062 dbTypeMap.put(DbType.TIMESTAMP, new DbPlatformType("datetime(6)")); 063 dbTypeMap.put(DbType.CLOB, new MySqlClob()); 064 dbTypeMap.put(DbType.BLOB, new MySqlBlob()); 065 dbTypeMap.put(DbType.BINARY, new DbPlatformType("binary", 255)); 066 dbTypeMap.put(DbType.VARBINARY, new DbPlatformType("varbinary", 255)); 067 } 068 069 @Override 070 protected String withForUpdate(String sql, Query.ForUpdate forUpdateMode) { 071 // NOWAIT and SKIP LOCKED currently not supported with MySQL 072 return sql + " for update"; 073 } 074 075}