package me.mrten.mysqlapi.queries;

import java.util.ArrayList;
import java.util.List;

public class CreateTableQuery {

    private String table;
    private boolean ifNotExists = false;
    private List<String> columns = new ArrayList<String>();
    private String primaryKey;

    public CreateTableQuery(String table) {
        this.table = table;
    }

    public CreateTableQuery ifNotExists() {
        this.ifNotExists = true;
        return this;
    }

    public CreateTableQuery column(String column, String settings) {
        columns.add(column + " " + settings);
        return this;
    }

    public CreateTableQuery primaryKey(String column) {
        this.primaryKey = column;
        return this;
    }

    public String build() {
        StringBuilder builder = new StringBuilder();
        builder.append("CREATE TABLE ");

        if (ifNotExists) {
            builder.append("IF NOT EXISTS ");
        }

        builder.append(table);
        builder.append(" (");

        for (String column : columns) {
            builder.append(column + ", ");
        }

        if (primaryKey != null) {
            builder.append("PRIMARY KEY(");
            builder.append(primaryKey);
            builder.append(")");
        }

        builder.append(")");

        return builder.toString();
    }

}

@@ -27,17 +27,19 @@
 queries;
-%0A
+%0D%0A%0D
 %0Aimport 
@@ -58,16 +58,17 @@
 rayList;
+%0D
 %0Aimport 
@@ -82,17 +82,19 @@
 il.List;
-%0A
+%0D%0A%0D
 %0Apublic 
@@ -117,17 +117,19 @@
 eQuery %7B
-%0A
+%0D%0A%0D
 %0A    pri
@@ -142,24 +142,25 @@
 tring table;
+%0D
 %0A    private
@@ -188,16 +188,17 @@
 = false;
+%0D
 %0A    pri
@@ -249,16 +249,17 @@
 ring%3E();
+%0D
 %0A    pri
@@ -281,17 +281,19 @@
 maryKey;
-%0A
+%0D%0A%0D
 %0A    pub
@@ -328,16 +328,17 @@
 table) %7B
+%0D
 %0A       
@@ -357,23 +357,26 @@
 = table;
+%0D
 %0A    %7D
-%0A
+%0D%0A%0D
 %0A    pub
@@ -407,24 +407,25 @@
 otExists() %7B
+%0D
 %0A        thi
@@ -445,16 +445,17 @@
  = true;
+%0D
 %0A       
@@ -459,39 +459,42 @@
     return this;
+%0D
 %0A    %7D
-%0A
+%0D%0A%0D
 %0A    public Crea
@@ -542,24 +542,25 @@
  settings) %7B
+%0D
 %0A        col
@@ -593,16 +593,17 @@
 ttings);
+%0D
 %0A       
@@ -607,39 +607,42 @@
     return this;
+%0D
 %0A    %7D
-%0A
+%0D%0A%0D
 %0A    public Crea
@@ -681,16 +681,17 @@
 olumn) %7B
+%0D
 %0A       
@@ -716,16 +716,17 @@
  column;
+%0D
 %0A       
@@ -738,23 +738,26 @@
 rn this;
+%0D
 %0A    %7D
-%0A
+%0D%0A%0D
 %0A    pub
@@ -776,16 +776,17 @@
 uild() %7B
+%0D
 %0A       
@@ -830,16 +830,17 @@
 ilder();
+%0D
 %0A       
@@ -872,17 +872,19 @@
 ABLE %22);
-%0A
+%0D%0A%0D
 %0A       
@@ -898,24 +898,25 @@
 NotExists) %7B
+%0D
 %0A           
@@ -945,24 +945,25 @@
 T EXISTS %22);
+%0D
 %0A        %7D%0A%0A
@@ -952,33 +952,35 @@
 S %22);%0D%0A        %7D
-%0A
+%0D%0A%0D
 %0A        builder
@@ -994,16 +994,17 @@
 (table);
+%0D
 %0A       
@@ -1025,17 +1025,19 @@
 d(%22 (%22);
-%0A
+%0D%0A%0D
 %0A       
@@ -1068,16 +1068,17 @@
 lumns) %7B
+%0D
 %0A       
@@ -1108,20 +1108,125 @@
 umn + %22,
- 
 %22
+);%0D%0A        %7D%0D%0A%0D%0A        if (columns.size() %3E 0) %7B%0D%0A            builder.deleteCharAt(builder.length() - 1
 );
+%0D
 %0A       
@@ -1219,33 +1219,35 @@
 - 1);%0D%0A        %7D
-%0A
+%0D%0A%0D
 %0A        if (pri
@@ -1264,16 +1264,17 @@
  null) %7B
+%0D
 %0A       
@@ -1294,16 +1294,17 @@
 append(%22
+,
 PRIMARY 
@@ -1310,16 +1310,17 @@
  KEY(%22);
+%0D
 %0A       
@@ -1351,16 +1351,17 @@
 aryKey);
+%0D
 %0A       
@@ -1385,16 +1385,17 @@
 nd(%22)%22);
+%0D
 %0A       
@@ -1396,17 +1396,19 @@
        %7D
-%0A
+%0D%0A%0D
 %0A       
@@ -1428,17 +1428,19 @@
 nd(%22)%22);
-%0A
+%0D%0A%0D
 %0A       
@@ -1470,14 +1470,18 @@
 g();
+%0D
 %0A    %7D
-%0A
+%0D%0A%0D
 %0A%7D
+%0D
 %0A

Remove last comma if no primary key is set