1 package jimm.datavision.testdata.mysql; 2 import jimm.datavision.testdata.SchemaGen; 3 4 public class MySQLSchemaGen extends SchemaGen { 5 6 7 protected static final int MAX_VARCHAR_LEN = 255; 8 9 protected String printableName(String name) { 10 if (name.indexOf(' ') >= 0 || !name.toLowerCase().equals(name)) 11 return "`" + name + "`"; 12 return name; 13 } 14 15 protected void makeTable(String tableName) { 16 String name = printableName(tableName); 17 System.out.println("drop table if exists " + name + ";"); 18 System.out.print("create table " + name + " ("); 19 } 20 21 protected void endTable() { 22 System.out.println(); 23 System.out.println(");"); 24 } 25 26 protected void printType(String type, int size) { 27 System.out.print(" "); 28 if ("integer".equals(type)) 29 System.out.print("int"); 30 else if ("date".equals(type)) 31 System.out.print("date"); 32 else if ("boolean".equals(type)) 33 System.out.print("enum('F','T') default 'F'"); 34 else if ("string".equals(type)) { 35 if (size > MAX_VARCHAR_LEN) 36 size = MAX_VARCHAR_LEN; 37 System.out.print("varchar(" + size + ")"); 38 } 39 } 40 41 protected void printNotNull() { 42 System.out.print(" not null"); 43 } 44 45 protected void printPrimaryKey() { 46 System.out.print(" primary key"); 47 } 48 49 public static void main(String [] args) { 50 new MySQLSchemaGen().run("../schema.xml"); 51 } 52 } 53 | Popular Tags |