001package io.ebeaninternal.dbmigration.ddlgeneration.platform.util; 002 003import io.ebean.config.dbplatform.DbPlatformType; 004import io.ebean.config.dbplatform.DbPlatformTypeMapping; 005 006/** 007 * Converts a logical column definition into platform specific one. 008 * <p> 009 * This translates standard sql types into platform specific ones. 010 */ 011public class PlatformTypeConverter { 012 013 protected final DbPlatformTypeMapping platformTypes; 014 015 /** 016 * Construct with the platform specific types. 017 */ 018 public PlatformTypeConverter(DbPlatformTypeMapping platformTypes) { 019 this.platformTypes = platformTypes; 020 } 021 022 /** 023 * Convert the standard type to the platform specific type. 024 */ 025 public String convert(String columnDefinition) { 026 027 int open = columnDefinition.indexOf('('); 028 if (open > -1) { 029 // no scale or precision 030 return convertWithScale(columnDefinition, open); 031 } else { 032 return convertNoScale(columnDefinition); 033 } 034 } 035 036 /** 037 * Convert a type that has scale and possibly precision. 038 */ 039 protected String convertWithScale(String columnDefinition, int open) { 040 041 int close = columnDefinition.lastIndexOf(')'); 042 if (close == -1) { 043 // assume already platform specific, leave as is 044 return columnDefinition; 045 } 046 047 String suffix = close + 1 < columnDefinition.length() ? columnDefinition.substring(close + 1) : ""; 048 String type = columnDefinition.substring(0, open); 049 try { 050 DbPlatformType dbType = platformTypes.lookup(type, true); 051 int comma = columnDefinition.indexOf(',', open); 052 if (comma > -1) { 053 // scale and precision - decimal(10,4) 054 int scale = Integer.parseInt(columnDefinition.substring(open + 1, comma)); 055 int precision = Integer.parseInt(columnDefinition.substring(comma + 1, close)); 056 return dbType.renderType(scale, precision) + suffix; 057 058 } else { 059 // scale - varchar(10) 060 int scale = Integer.parseInt(columnDefinition.substring(open + 1, close)); 061 return dbType.renderType(scale, -1) + suffix; 062 } 063 064 } catch (IllegalArgumentException e) { 065 // assume already platform specific, leave as is 066 return columnDefinition; 067 } 068 } 069 070 /** 071 * Convert a simple type with not scale or precision. 072 */ 073 protected String convertNoScale(String columnDefinition) { 074 075 try { 076 DbPlatformType dbType = platformTypes.lookup(columnDefinition, false); 077 return dbType.renderType(0, 0); 078 079 } catch (IllegalArgumentException e) { 080 // assume already platform specific, leave as is 081 return columnDefinition; 082 } 083 } 084 085}