wip commit with working vasc demo tech
This commit is contained in:
parent
a25e98f5d5
commit
c23d14522f
100 changed files with 4356 additions and 1220 deletions
|
|
@ -0,0 +1,90 @@
|
|||
package net.forwardfire.vasc.backend.metamodel;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
|
||||
import org.eobjects.metamodel.DataContext;
|
||||
import org.eobjects.metamodel.jdbc.JdbcDataContext;
|
||||
|
||||
|
||||
public class MetaModelDataContextJdbc implements MetaModelDataContextProvider {
|
||||
|
||||
private String driverClass = null;
|
||||
private String connectUrl = null;
|
||||
private String username = null;
|
||||
private String password = null;
|
||||
|
||||
public DataContext getDataContext() {
|
||||
JdbcDataContext dataContext = new JdbcDataContext(getConnection());
|
||||
return dataContext;
|
||||
}
|
||||
|
||||
public Connection getConnection() {
|
||||
synchronized (this) {
|
||||
try {
|
||||
Class.forName(getDriverClass());
|
||||
Connection connection = DriverManager.getConnection(getConnectUrl(),getUsername(),getPassword());
|
||||
return connection;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the username
|
||||
*/
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param username the username to set
|
||||
*/
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the password
|
||||
*/
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param password the password to set
|
||||
*/
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the connectUrl
|
||||
*/
|
||||
public String getConnectUrl() {
|
||||
return connectUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param connectUrl the connectUrl to set
|
||||
*/
|
||||
public void setConnectUrl(String connectUrl) {
|
||||
this.connectUrl = connectUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the driverClass
|
||||
*/
|
||||
public String getDriverClass() {
|
||||
return driverClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param driverClass the driverClass to set
|
||||
*/
|
||||
public void setDriverClass(String driverClass) {
|
||||
this.driverClass = driverClass;
|
||||
}
|
||||
}
|
||||
|
|
@ -19,8 +19,8 @@ public class MetaModelDataContextMongodb implements MetaModelDataContextProvider
|
|||
private String password = null;
|
||||
private boolean readonly = false;
|
||||
|
||||
protected static Mongo mongo = null;
|
||||
protected static DB db = null;
|
||||
protected Mongo mongo = null;
|
||||
//protected DB db = null;
|
||||
|
||||
public DataContext getDataContext() {
|
||||
MongoDbDataContext dataContext = new MongoDbDataContext(getMongodbConnection());
|
||||
|
|
@ -28,9 +28,6 @@ public class MetaModelDataContextMongodb implements MetaModelDataContextProvider
|
|||
}
|
||||
|
||||
public DB getMongodbConnection() {
|
||||
if (db!=null) {
|
||||
return db;
|
||||
}
|
||||
synchronized (this) {
|
||||
ServerAddress server;
|
||||
try {
|
||||
|
|
@ -40,7 +37,7 @@ public class MetaModelDataContextMongodb implements MetaModelDataContextProvider
|
|||
}
|
||||
MongoOptions options = new MongoOptions();
|
||||
mongo = new Mongo(server,options);
|
||||
db = mongo.getDB(database);
|
||||
DB db = mongo.getDB(database);
|
||||
if (username!=null && password!=null) {
|
||||
boolean auth = db.authenticate(username, password.toCharArray());
|
||||
if (auth==false) {
|
||||
|
|
@ -50,9 +47,8 @@ public class MetaModelDataContextMongodb implements MetaModelDataContextProvider
|
|||
if (readonly) {
|
||||
db.setReadOnly(true);
|
||||
}
|
||||
//logger.info("Connection to: "+db.getName());
|
||||
return db;
|
||||
}
|
||||
return db;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -23,8 +23,9 @@
|
|||
package net.forwardfire.vasc.backend.metamodel;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
|
@ -33,8 +34,11 @@ import org.eobjects.metamodel.UpdateScript;
|
|||
import org.eobjects.metamodel.UpdateableDataContext;
|
||||
import org.eobjects.metamodel.data.DataSet;
|
||||
import org.eobjects.metamodel.data.Row;
|
||||
import org.eobjects.metamodel.insert.RowInsertionBuilder;
|
||||
import org.eobjects.metamodel.query.Query;
|
||||
import org.eobjects.metamodel.query.SelectItem;
|
||||
import org.eobjects.metamodel.query.builder.SatisfiedQueryBuilder;
|
||||
import org.eobjects.metamodel.query.builder.SatisfiedWhereBuilder;
|
||||
import org.eobjects.metamodel.schema.Schema;
|
||||
import org.eobjects.metamodel.schema.Table;
|
||||
import org.eobjects.metamodel.update.RowUpdationBuilder;
|
||||
|
|
@ -74,7 +78,51 @@ public class MetaModelVascBackend extends AbstractVascBackend {
|
|||
UpdateableDataContext dataContext = getUpdateableDataContext();
|
||||
Schema schema = dataContext.getDefaultSchema();
|
||||
Table t = schema.getTableByName(table);
|
||||
Query q = dataContext.query().from(t).select(t.getColumns()).toQuery();
|
||||
|
||||
Object qWhere = dataContext.query().from(t).select(t.getColumns());
|
||||
Iterator<String> keys = state.getDataParameterKeys().iterator();
|
||||
boolean first = true;
|
||||
while (keys.hasNext()) {
|
||||
String key = keys.next();
|
||||
Object value = state.getDataParameter(key); /// TODO: redo this nasty code
|
||||
if (value instanceof Number) {
|
||||
if (first) {
|
||||
qWhere = ((SatisfiedQueryBuilder<?>)qWhere).where(key).equals((Number)value);
|
||||
} else {
|
||||
qWhere = ((SatisfiedWhereBuilder<?>)qWhere).and(key).equals((Number)value);
|
||||
}
|
||||
} else if (value instanceof Date) {
|
||||
if (first) {
|
||||
qWhere = ((SatisfiedQueryBuilder<?>)qWhere).where(key).equals((Date)value);
|
||||
} else {
|
||||
qWhere = ((SatisfiedWhereBuilder<?>)qWhere).and(key).equals((Date)value);
|
||||
}
|
||||
} else if (value instanceof Boolean) {
|
||||
if (first) {
|
||||
qWhere = ((SatisfiedQueryBuilder<?>)qWhere).where(key).equals((Boolean)value);
|
||||
} else {
|
||||
qWhere = ((SatisfiedWhereBuilder<?>)qWhere).and(key).equals((Boolean)value);
|
||||
}
|
||||
} else {
|
||||
if (value==null) {
|
||||
if (first) {
|
||||
qWhere = ((SatisfiedQueryBuilder<?>)qWhere).where(key).isNull();
|
||||
} else {
|
||||
qWhere = ((SatisfiedWhereBuilder<?>)qWhere).and(key).isNull();
|
||||
}
|
||||
} else {
|
||||
if (first) {
|
||||
qWhere = ((SatisfiedQueryBuilder<?>)qWhere).where(key).equals(value.toString());
|
||||
} else {
|
||||
qWhere = ((SatisfiedWhereBuilder<?>)qWhere).and(key).equals(value.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
first = false;
|
||||
}
|
||||
|
||||
|
||||
Query q = ((SatisfiedQueryBuilder<?>)qWhere).toQuery();
|
||||
DataSet ds = dataContext.executeQuery(q);
|
||||
List<Object> result = new ArrayList<Object>(50);
|
||||
while (ds.next()) {
|
||||
|
|
@ -87,29 +135,25 @@ public class MetaModelVascBackend extends AbstractVascBackend {
|
|||
}
|
||||
result.add(map);
|
||||
}
|
||||
ds.close();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void persist(Object object) throws VascException {
|
||||
final Map<String,Object> map = (Map<String,Object>)object;
|
||||
UpdateableDataContext dataContext = getUpdateableDataContext();
|
||||
dataContext.executeUpdate(new UpdateScript() {
|
||||
public void run(UpdateCallback backendImpl) {
|
||||
Table table = backendImpl.createTable(null, "some_entries").withColumn("foo").withColumn("bar")
|
||||
.withColumn("baz").withColumn("list").execute();
|
||||
|
||||
backendImpl.insertInto(table).value("foo", 1).value("bar", "hello").execute();
|
||||
backendImpl.insertInto(table).value("foo", 2).value("bar", "world").execute();
|
||||
backendImpl.insertInto(table).value("foo", 3).value("bar", "hi").execute();
|
||||
|
||||
Map<String, Object> nestedObj = new HashMap<String, Object>();
|
||||
nestedObj.put("foo", "bar");
|
||||
nestedObj.put("123", 456);
|
||||
|
||||
backendImpl.insertInto(table).value("foo", 4).value("bar", "there").value("baz", nestedObj)
|
||||
.value("list", Arrays.asList(1, 2, 3)).execute();
|
||||
RowInsertionBuilder query = backendImpl.insertInto(table);
|
||||
for (String key:map.keySet()) {
|
||||
Object value = map.get(key);
|
||||
query.value(key, value);
|
||||
}
|
||||
query.execute();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
|
@ -118,8 +162,17 @@ public class MetaModelVascBackend extends AbstractVascBackend {
|
|||
UpdateableDataContext dataContext = getUpdateableDataContext();
|
||||
dataContext.executeUpdate(new UpdateScript() {
|
||||
public void run(UpdateCallback backendImpl) {
|
||||
RowUpdationBuilder query = backendImpl.update(table).where(tableId).equals(""+map.get(tableId));
|
||||
|
||||
RowUpdationBuilder query = null;
|
||||
if (map.get(tableId) instanceof Number) {
|
||||
query = backendImpl.update(table).where(tableId).equals((Number)map.get(tableId));
|
||||
} else {
|
||||
query = backendImpl.update(table).where(tableId).equals(map.get(tableId).toString());
|
||||
}
|
||||
for (String key:map.keySet()) {
|
||||
if (key.equals(tableId)) {
|
||||
continue; // skip id;
|
||||
}
|
||||
Object value = map.get(key);
|
||||
query.value(key, value);
|
||||
}
|
||||
|
|
@ -135,7 +188,11 @@ public class MetaModelVascBackend extends AbstractVascBackend {
|
|||
UpdateableDataContext dataContext = getUpdateableDataContext();
|
||||
dataContext.executeUpdate(new UpdateScript() {
|
||||
public void run(UpdateCallback backendImpl) {
|
||||
backendImpl.deleteFrom(table).where(tableId).in(""+map.get(tableId)).execute();
|
||||
if (map.get(tableId) instanceof Number) {
|
||||
backendImpl.deleteFrom(table).where(tableId).equals((Number)map.get(tableId)).execute();
|
||||
} else {
|
||||
backendImpl.deleteFrom(table).where(tableId).equals(map.get(tableId).toString()).execute();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue