001package io.ebeaninternal.dbmigration.model; 002 003import io.ebean.annotation.IdentityGenerated; 004import io.ebean.config.dbplatform.IdType; 005import io.ebeaninternal.dbmigration.migration.CreateTable; 006import io.ebeaninternal.dbmigration.migration.IdentityType; 007import io.ebeaninternal.server.deploy.IdentityMode; 008 009import java.math.BigInteger; 010 011/** 012 * Helper to convert between IdentityMode and CreateTable 013 */ 014public class MTableIdentity { 015 016 /** 017 * Return the IdentityMode from CreateTable. 018 */ 019 public static IdentityMode fromCreateTable(CreateTable createTable) { 020 IdType type = fromType(createTable.getIdentityType()); 021 IdentityGenerated generated = fromGenerated(createTable.getIdentityGenerated()); 022 int start = toInt(createTable.getIdentityStart(), createTable.getSequenceInitial()); 023 int increment = toInt(createTable.getIdentityIncrement(), createTable.getSequenceAllocate()); 024 int cache = toInt(createTable.getIdentityCache(), null); 025 String seqName = createTable.getSequenceName(); 026 027 return new IdentityMode(type, generated, start, increment, cache, seqName); 028 } 029 030 private static IdentityGenerated fromGenerated(String identityGenerated) { 031 if (identityGenerated == null) { 032 return IdentityGenerated.AUTO; 033 } 034 return IdentityGenerated.valueOf(identityGenerated.toUpperCase()); 035 } 036 037 /** 038 * Set the IdentityMode to the CreateTable model. 039 */ 040 public static void toCreateTable(IdentityMode identityMode, CreateTable createTable) { 041 if (!identityMode.isPlatformDefault()) { 042 createTable.setIdentityType(toType(identityMode.getIdType())); 043 } 044 final String seqName = identityMode.getSequenceName(); 045 if (seqName != null && !seqName.isEmpty()) { 046 createTable.setSequenceName(seqName); 047 } 048 049 createTable.setIdentityStart(toBigInteger(identityMode.getStart())); 050 createTable.setIdentityIncrement(toBigInteger(identityMode.getIncrement())); 051 createTable.setIdentityCache(toBigInteger(identityMode.getCache())); 052 final IdentityGenerated generated = identityMode.getGenerated(); 053 if (generated != null && generated != IdentityGenerated.AUTO) { 054 createTable.setIdentityGenerated(generated.name().toLowerCase()); 055 } 056 } 057 058 059 private static IdType fromType(IdentityType type) { 060 if (type == null) { 061 return IdType.AUTO; 062 } 063 switch (type) { 064 case DEFAULT: 065 return IdType.AUTO; 066 case SEQUENCE: 067 return IdType.SEQUENCE; 068 case IDENTITY: 069 return IdType.IDENTITY; 070 case GENERATOR: 071 return IdType.GENERATOR; 072 case EXTERNAL: 073 return IdType.EXTERNAL; 074 } 075 return IdType.AUTO; 076 } 077 078 private static IdentityType toType(IdType type) { 079 if (type == null) { 080 // intersection or element collection table 081 return null; 082 } 083 switch (type) { 084 case SEQUENCE: 085 return IdentityType.SEQUENCE; 086 case IDENTITY: 087 return IdentityType.IDENTITY; 088 case EXTERNAL: 089 return IdentityType.EXTERNAL; 090 case GENERATOR: 091 return IdentityType.GENERATOR; 092 default: 093 return null; 094 } 095 } 096 097 private static int toInt(BigInteger firstVal, BigInteger secVal) { 098 if (firstVal != null) { 099 return firstVal.intValue(); 100 } 101 return (secVal == null) ? 0 : secVal.intValue(); 102 } 103 104 private static BigInteger toBigInteger(int value) { 105 return (value == 0) ? null : BigInteger.valueOf(value); 106 } 107}