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