/
CreateTable.java
70 lines (58 loc) · 1.34 KB
/
CreateTable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.michaldabski.msqlite.queries;
import java.util.List;
import android.util.Log;
import com.michaldabski.msqlite.models.Column;
import com.michaldabski.msqlite.models.Table;
public class CreateTable extends QueryBuilder
{
private boolean
IF_NOT_EXISTS = false;
public CreateTable(Class<?> type)
{
super(type);
}
public CreateTable(Table table)
{
super(table);
}
public CreateTable setIF_NOT_EXIST(boolean iF_NOT_EXIST)
{
IF_NOT_EXISTS = iF_NOT_EXIST;
return this;
}
@Override
public String build()
{
Table table = getTable();
StringBuilder builder = new StringBuilder("CREATE TABLE ");
if (IF_NOT_EXISTS) builder.append(" IF NOT EXISTS");
builder
.append('`')
.append(table.getName())
.append('`')
.append(" (");
int n=0;
for (Column column : table.getColumns())
{
if (n++ > 0) builder.append(',');
builder
.append('\n')
.append(column.getBuilder());
}
n=0;
List<Column> primaryKeys = table.getPrimaryKeys();
if (primaryKeys.isEmpty() == false)
{
builder.append(",\nPRIMARY KEY (");
for (Column column : primaryKeys)
{
if (n++ > 0) builder.append(", ");
builder.append('`').append(column.getName()).append('`');
}
builder.append(")");
}
builder.append("\n);");
Log.d("QueryBuilder", builder.toString());
return builder.toString();
}
}