parent
b898a8d190
commit
7049ca0e6b
@ -0,0 +1,33 @@ |
|||||||
|
package de.kreth.dbmanager; |
||||||
|
|
||||||
|
public class ColumnDefinition { |
||||||
|
|
||||||
|
private DataType type; |
||||||
|
private String columnName; |
||||||
|
private String columnParameters; |
||||||
|
|
||||||
|
public ColumnDefinition(DataType type, String columnName) { |
||||||
|
super(); |
||||||
|
this.type = type; |
||||||
|
this.columnName = columnName; |
||||||
|
} |
||||||
|
|
||||||
|
public ColumnDefinition(DataType type, String columnName, String columnParameters) { |
||||||
|
super(); |
||||||
|
this.type = type; |
||||||
|
this.columnName = columnName; |
||||||
|
this.columnParameters = columnParameters; |
||||||
|
} |
||||||
|
|
||||||
|
public DataType getType() { |
||||||
|
return type; |
||||||
|
} |
||||||
|
|
||||||
|
public String getColumnName() { |
||||||
|
return columnName; |
||||||
|
} |
||||||
|
|
||||||
|
public String getColumnParameters() { |
||||||
|
return columnParameters; |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,13 @@ |
|||||||
|
package de.kreth.dbmanager; |
||||||
|
|
||||||
|
public enum DataType { |
||||||
|
INTEGER, |
||||||
|
REAL, |
||||||
|
TEXT, |
||||||
|
BLOB, |
||||||
|
BOOLEAN, |
||||||
|
DATETIME, |
||||||
|
VARCHAR25, |
||||||
|
VARCHAR100, |
||||||
|
VARCHAR255, |
||||||
|
} |
||||||
@ -0,0 +1,32 @@ |
|||||||
|
package de.kreth.dbmanager; |
||||||
|
|
||||||
|
import java.sql.SQLException; |
||||||
|
import java.util.Collection; |
||||||
|
import java.util.Iterator; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
public interface Database { |
||||||
|
|
||||||
|
public void beginTransaction() throws SQLException; |
||||||
|
|
||||||
|
public void setTransactionSuccessful() throws SQLException; |
||||||
|
|
||||||
|
public void endTransaction() throws SQLException; |
||||||
|
|
||||||
|
public void execSQL(String sql) throws SQLException; |
||||||
|
|
||||||
|
public void execSQL(String sql, Object bindArgs[]) throws SQLException; |
||||||
|
|
||||||
|
public int getVersion(); |
||||||
|
|
||||||
|
public long insert(String table, List<DbValue> values) throws SQLException; |
||||||
|
|
||||||
|
public boolean needUpgrade(int newVersion); |
||||||
|
|
||||||
|
public Iterator<Collection<DbValue>> query(String table, String columns[]) throws SQLException; |
||||||
|
|
||||||
|
public void setVersion(int version); |
||||||
|
|
||||||
|
int delete(String table, String whereClause, String[] whereArgs) throws SQLException; |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,149 @@ |
|||||||
|
package de.kreth.dbmanager; |
||||||
|
|
||||||
|
import java.sql.SQLException; |
||||||
|
import java.util.Collection; |
||||||
|
import java.util.Collections; |
||||||
|
import java.util.Comparator; |
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.List; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
public class DbManager { |
||||||
|
private Map<String, TableDefinition> tableDefinitions; |
||||||
|
private Database db; |
||||||
|
private int newVersion; |
||||||
|
private List<Version> versions; |
||||||
|
|
||||||
|
public DbManager(Collection<TableDefinition> tables, List<Version> versions, Database db, int newVersion) { |
||||||
|
tableDefinitions = new HashMap<String, TableDefinition>(); |
||||||
|
this.db = db; |
||||||
|
this.newVersion = newVersion; |
||||||
|
this.versions = versions; |
||||||
|
|
||||||
|
for(TableDefinition def: tables){ |
||||||
|
tableDefinitions.put(def.getTableName(), def); |
||||||
|
} |
||||||
|
|
||||||
|
checkParamters(); |
||||||
|
sortVersionsAndMakeUnmodifiable(); |
||||||
|
} |
||||||
|
|
||||||
|
private void checkParamters() { |
||||||
|
StringBuilder errorMsg = new StringBuilder(); |
||||||
|
if(tableDefinitions.isEmpty()) |
||||||
|
append(errorMsg, "Parameter tables darf nicht leer sein!"); |
||||||
|
if(db == null) |
||||||
|
append(errorMsg, "Parameter db darf nicht null sein!"); |
||||||
|
if(newVersion<1) |
||||||
|
append(errorMsg, "Parameter newVersion muss >= 1 sein!"); |
||||||
|
if(versions == null) |
||||||
|
append(errorMsg, "Liste mit Versionen darf nicht null sein"); |
||||||
|
if(versions != null && versions.get(0).getVersionNr() != 1) |
||||||
|
append(errorMsg, "Der erste Eintrag muss die Versionsnummer 1 haben! Die Create Table anweisungen für erste Version müssen enthalten sein."); |
||||||
|
|
||||||
|
if(errorMsg.length()>0) |
||||||
|
throw new IllegalArgumentException(errorMsg.toString()); |
||||||
|
} |
||||||
|
|
||||||
|
private void sortVersionsAndMakeUnmodifiable() { |
||||||
|
|
||||||
|
Collections.sort(versions, new Comparator<Version>() { |
||||||
|
|
||||||
|
@Override |
||||||
|
public int compare(Version o1, Version o2) { |
||||||
|
if(o1.getVersionNr() == o2.getVersionNr()) |
||||||
|
throw new IllegalStateException("Es gibt zwei Einträge mit der selben Version! Es muss eine eindeutige Reihenfolge gegeben sein!"); |
||||||
|
|
||||||
|
return o1.getVersionNr() - o2.getVersionNr(); |
||||||
|
} |
||||||
|
}); |
||||||
|
|
||||||
|
versions = Collections.unmodifiableList(versions); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
private void append(StringBuilder bld, String msg){ |
||||||
|
if(bld.length()>0) |
||||||
|
bld.append("\n"); |
||||||
|
bld.append(msg); |
||||||
|
} |
||||||
|
|
||||||
|
public Map<String, TableDefinition> getTableDefinitions() { |
||||||
|
return Collections.unmodifiableMap(tableDefinitions); |
||||||
|
} |
||||||
|
|
||||||
|
public boolean needUpdate() { |
||||||
|
return db.getVersion()<newVersion; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Führt die nötigen Befehle aus um die aktuelle Version zu erreichen. <br /> |
||||||
|
* @throws SQLException |
||||||
|
*/ |
||||||
|
public void execute() throws SQLException { |
||||||
|
int currentVersion = db.getVersion(); |
||||||
|
int from = 0; |
||||||
|
|
||||||
|
for (int i = from; i < versions.size(); i++) { |
||||||
|
Version v = versions.get(i); |
||||||
|
|
||||||
|
if(v.getVersionNr()>currentVersion) |
||||||
|
executeStatements(v.getSqlStms()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private void executeStatements(List<String> sqlStms) throws SQLException { |
||||||
|
for(String sql : sqlStms){ |
||||||
|
db.execSQL(sql); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public static String createSqlStatement(TableDefinition def) { |
||||||
|
StringBuilder sql = new StringBuilder(); |
||||||
|
sql.append("CREATE TABLE ").append(def.getTableName()).append(" (\n"); |
||||||
|
|
||||||
|
boolean first = true; |
||||||
|
for(ColumnDefinition col : def.getColumns()) { |
||||||
|
if(!first) { |
||||||
|
sql.append(",\n"); |
||||||
|
} |
||||||
|
|
||||||
|
first = false; |
||||||
|
|
||||||
|
sql.append("\t").append(col.getColumnName()).append(" "); |
||||||
|
switch (col.getType()) { |
||||||
|
case BLOB: |
||||||
|
throw new IllegalArgumentException("Column Type " + col.getType() + " not supported"); |
||||||
|
case BOOLEAN: |
||||||
|
sql.append(col.getType().name()); |
||||||
|
break; |
||||||
|
case DATETIME: |
||||||
|
sql.append("DATETIME"); |
||||||
|
break; |
||||||
|
case INTEGER: |
||||||
|
sql.append(col.getType().name()); |
||||||
|
break; |
||||||
|
case REAL: |
||||||
|
sql.append("DOUBLE"); |
||||||
|
break; |
||||||
|
case VARCHAR100: |
||||||
|
sql.append("VARCHAR(100)"); |
||||||
|
break; |
||||||
|
case VARCHAR25: |
||||||
|
sql.append("VARCHAR(25)"); |
||||||
|
break; |
||||||
|
case TEXT: |
||||||
|
case VARCHAR255: |
||||||
|
sql.append("VARCHAR(255)"); |
||||||
|
break; |
||||||
|
} |
||||||
|
if(col.getColumnParameters() != null && !col.getColumnParameters().isEmpty()){ |
||||||
|
sql.append(" ").append(col.getColumnParameters()); |
||||||
|
} |
||||||
|
sql.append("\n"); |
||||||
|
} |
||||||
|
sql.append(")"); |
||||||
|
return sql.toString(); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,26 @@ |
|||||||
|
package de.kreth.dbmanager; |
||||||
|
|
||||||
|
public class DbValue { |
||||||
|
private DataType type; |
||||||
|
private String columnName; |
||||||
|
private Object value; |
||||||
|
|
||||||
|
public DbValue(DataType type, String columnName) { |
||||||
|
super(); |
||||||
|
this.type = type; |
||||||
|
this.columnName = columnName; |
||||||
|
} |
||||||
|
public DataType getType() { |
||||||
|
return type; |
||||||
|
} |
||||||
|
public Object getValue() { |
||||||
|
return value; |
||||||
|
} |
||||||
|
public void setValue(Object value) { |
||||||
|
this.value = value; |
||||||
|
} |
||||||
|
public String getColumnName() { |
||||||
|
return columnName; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,20 @@ |
|||||||
|
package de.kreth.dbmanager; |
||||||
|
public interface Statement { |
||||||
|
|
||||||
|
public void close(); |
||||||
|
|
||||||
|
public void execute(); |
||||||
|
|
||||||
|
public long executeInsert(); |
||||||
|
|
||||||
|
public void bindBlob(int index, byte value[]); |
||||||
|
|
||||||
|
public void bindDouble(int index, double value); |
||||||
|
|
||||||
|
public void bindLong(int index, long value); |
||||||
|
|
||||||
|
public void bindNull(int index); |
||||||
|
|
||||||
|
public void bindString(int index, String value); |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,37 @@ |
|||||||
|
package de.kreth.dbmanager; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.Collection; |
||||||
|
import java.util.Collections; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
/** |
||||||
|
* Immutable |
||||||
|
* <br /> Der Liste von Spalten wird <i>immer</i> eine Spalte mit Namen {@link #COLUMN_ID_NAME} hinzugefügt, die als Primärschlüssel dient. |
||||||
|
* @author markus |
||||||
|
* |
||||||
|
*/ |
||||||
|
public class TableDefinition { |
||||||
|
public static final String COLUMN_ID_NAME = "_id"; |
||||||
|
private String tableName; |
||||||
|
private Collection<ColumnDefinition> columns; |
||||||
|
|
||||||
|
public TableDefinition(String tableName,Collection<ColumnDefinition> columns) { |
||||||
|
super(); |
||||||
|
this.tableName = tableName; |
||||||
|
ColumnDefinition id = new ColumnDefinition(DataType.INTEGER, COLUMN_ID_NAME, "primary key AUTO_INCREMENT"); |
||||||
|
List<ColumnDefinition> def = new ArrayList<ColumnDefinition>(); |
||||||
|
def.add(id); |
||||||
|
def.addAll(columns); |
||||||
|
this.columns = Collections.unmodifiableCollection(def); |
||||||
|
} |
||||||
|
|
||||||
|
public String getTableName() { |
||||||
|
return tableName; |
||||||
|
} |
||||||
|
|
||||||
|
public Collection<ColumnDefinition> getColumns() { |
||||||
|
return columns; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,29 @@ |
|||||||
|
package de.kreth.dbmanager; |
||||||
|
|
||||||
|
import java.util.Collections; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
public class Version { |
||||||
|
|
||||||
|
private int versionNr; |
||||||
|
private List<String> sqlStms; |
||||||
|
|
||||||
|
public Version(int versionNr, List<String> sqlStms) { |
||||||
|
super(); |
||||||
|
this.versionNr = versionNr; |
||||||
|
this.sqlStms = Collections.unmodifiableList(sqlStms); |
||||||
|
} |
||||||
|
|
||||||
|
public int getVersionNr() { |
||||||
|
return versionNr; |
||||||
|
} |
||||||
|
|
||||||
|
public List<String> getSqlStms() { |
||||||
|
return sqlStms; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public String toString() { |
||||||
|
return getClass().getSimpleName() + ": nr=" + versionNr + ", Anz.Sql-Statements=" + sqlStms.size(); |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue