.
This commit is contained in:
@@ -129,7 +129,7 @@
|
||||
<th>数据平台</th>
|
||||
<td>
|
||||
<select v-model="row.dbid" class="form-control">
|
||||
<option v-for="item in dbPlats" :value="item.key" v-text="item.name"></option>
|
||||
<option v-for="item in dbPlats" :value="item.dbid" v-text="item.dbname"></option>
|
||||
</select>
|
||||
</td>
|
||||
<th>CataLog</th>
|
||||
@@ -418,7 +418,7 @@
|
||||
catalogs: function () {
|
||||
let dbPlats = this.dbPlats;
|
||||
for (i in dbPlats) {
|
||||
if (dbPlats[i].key === this.row.dbplatid) {
|
||||
if (dbPlats[i].dbid === this.row.dbid) {
|
||||
return dbPlats[i]["catalogs"]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,216 +0,0 @@
|
||||
package net.tccn.base.arango;
|
||||
|
||||
import com.arangodb.ArangoCollection;
|
||||
import com.arangodb.ArangoDB;
|
||||
import com.arangodb.ArangoDatabase;
|
||||
import com.arangodb.entity.DocumentCreateEntity;
|
||||
import com.arangodb.entity.DocumentDeleteEntity;
|
||||
import com.arangodb.entity.MultiDocumentEntity;
|
||||
import net.tccn.base.MetaListenter;
|
||||
import net.tccn.base.Utils;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.persistence.Table;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
/**
|
||||
* 管理 数据源连接对象
|
||||
*
|
||||
* @author: liangxianyou at 2018/12/15 11:35.
|
||||
*/
|
||||
public class ArangoSource {
|
||||
|
||||
@Resource(name = "property.arango.host")
|
||||
private String host = "127.0.0.1";
|
||||
@Resource(name = "property.arango.user")
|
||||
private String user = "root";
|
||||
@Resource(name = "property.arango.password")
|
||||
private String password = "123456";
|
||||
@Resource(name = "property.arango.port")
|
||||
private int port = 8529;
|
||||
|
||||
public Logger logger = Logger.getLogger(this.getClass().getSimpleName());
|
||||
private ArangoDB arangoDb;
|
||||
|
||||
private static Map<String, ArangoSource> sourceMap = new HashMap();
|
||||
|
||||
private ArangoSource() {
|
||||
MetaListenter.resourceFactory.inject(this);
|
||||
|
||||
arangoDb = new ArangoDB.Builder().host(host, port).user(user).password(password).timeout(1000*15).build();
|
||||
}
|
||||
|
||||
private ArangoSource(ArangoDB arangoDb) {
|
||||
MetaListenter.resourceFactory.inject(this);
|
||||
|
||||
this.arangoDb = arangoDb;
|
||||
}
|
||||
|
||||
public static ArangoSource use(String unit) {
|
||||
ArangoSource arangoSource = new ArangoSource();
|
||||
return arangoSource;
|
||||
}
|
||||
|
||||
public ArangoDatabase db(String db) {
|
||||
return arangoDb.db(db);
|
||||
}
|
||||
|
||||
public <T extends Doc> ArangoCollection collection(Doc<T> doc) {
|
||||
return collection(doc.getClass());
|
||||
}
|
||||
|
||||
public <T extends Doc> ArangoCollection collection(Class<T> type) {
|
||||
Table table = type.getAnnotation(Table.class);
|
||||
//createDb(table.catalog());
|
||||
return arangoDb.db(table.catalog()).collection(table.name());
|
||||
}
|
||||
|
||||
public boolean createDb(String db) {
|
||||
ArangoDatabase database = arangoDb.db(db);
|
||||
if (!database.exists()) {
|
||||
return database.create();
|
||||
}
|
||||
logger.log(Level.INFO, "arango database exists");
|
||||
return true;
|
||||
}
|
||||
|
||||
public <T extends Doc> ArangoCollection createDocument(Doc<T> doc) {
|
||||
Class<? extends Doc> type = doc.getClass();
|
||||
Table table = type.getAnnotation(Table.class);
|
||||
|
||||
ArangoDatabase database = arangoDb.db(table.catalog());
|
||||
if (!database.exists()) {
|
||||
database.create();
|
||||
}
|
||||
|
||||
ArangoCollection collection = database.collection(table.name());
|
||||
if (!collection.exists()) {
|
||||
collection.create();
|
||||
}
|
||||
|
||||
return collection;
|
||||
}
|
||||
|
||||
// 首字母大写
|
||||
private static Function<String, String> upFirst = (s) -> {
|
||||
return s.substring(0, 1).toUpperCase() + s.substring(1);
|
||||
};
|
||||
|
||||
/**
|
||||
* Doc 转为查询对象
|
||||
*
|
||||
* @param t
|
||||
* @param <T>
|
||||
* @return
|
||||
*/
|
||||
private Function<Doc, StringBuilder> filterBuilder = (t) -> {
|
||||
Table table = t.getClass().getAnnotation(Table.class);
|
||||
StringBuilder buf = new StringBuilder();
|
||||
|
||||
buf.append("for d in ").append(table.name());
|
||||
buf.append(" filter 1==1");
|
||||
t.toDoc().forEach((k, v) -> {
|
||||
if (v != null && (v.getClass() == String.class || v instanceof Number)) {
|
||||
buf.append(" and d.").append(k).append("==");
|
||||
}
|
||||
|
||||
if (v.getClass() == String.class) {
|
||||
buf.append("'").append(v).append("'");
|
||||
} else if (v instanceof Number) {
|
||||
buf.append(v);
|
||||
}
|
||||
});
|
||||
return buf;
|
||||
};
|
||||
|
||||
private Function<Doc, StringBuilder> orderBuilder = (t) -> {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
Map<String, Integer> order = t.getOrder();
|
||||
if (Utils.isEmpty(order)) {
|
||||
return buf.append(" sort d._key desc");
|
||||
}
|
||||
buf.append(" sort ");
|
||||
order.forEach((k, v) -> {
|
||||
buf.append("d.").append(k).append(v == 1 ? " desc," : " asc,");
|
||||
});
|
||||
buf.deleteCharAt(buf.length() - 1);
|
||||
return buf;
|
||||
};
|
||||
|
||||
private Function<Doc, StringBuilder> returnBuilder = (t) -> {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
|
||||
if (Utils.isEmpty(t.get_Shows())) {
|
||||
return buf.append(" return d");
|
||||
}
|
||||
|
||||
buf.append(" return {");
|
||||
t.get_Shows().forEach(x -> {
|
||||
buf.append(x).append(":d.").append(x).append(",");
|
||||
});
|
||||
buf.deleteCharAt(buf.length() - 1).append("}");
|
||||
return buf;
|
||||
};
|
||||
|
||||
public <T extends Doc> String parseAqlCount(T t) {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append(filterBuilder.apply(t));
|
||||
buf.append(" COLLECT WITH COUNT INTO total");
|
||||
buf.append(" return total");
|
||||
//logger.log(Level.INFO, buf.toString());
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
public <T extends Doc> String parseAql(T t, int offset, int ps) {
|
||||
if (offset < 0) offset = 0;
|
||||
if (ps <= 0) ps = 1000;
|
||||
StringBuilder buf = new StringBuilder();
|
||||
|
||||
buf.append(filterBuilder.apply(t));
|
||||
buf.append(orderBuilder.apply(t));
|
||||
buf.append(" limit ").append(offset).append(",").append(ps);
|
||||
buf.append(returnBuilder.apply(t));
|
||||
//logger.log(Level.INFO, buf.toString());
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
//----------------------------------------
|
||||
//ok
|
||||
public <T extends Doc> T save(T doc) {
|
||||
DocumentCreateEntity<Map> tmap = collection(doc).insertDocument(doc.toDoc());
|
||||
doc.setKey(tmap.getKey());
|
||||
doc.setId(tmap.getId());
|
||||
return doc;
|
||||
}
|
||||
|
||||
//ok
|
||||
public <T extends Doc> void update(T doc) {
|
||||
collection(doc).updateDocument(doc.getKey(), doc.toDoc());
|
||||
}
|
||||
|
||||
//ok
|
||||
public <T extends Doc> T getDoc(Object key, Class<T> type) {
|
||||
return collection(type).getDocument(String.valueOf(key), type);
|
||||
}
|
||||
|
||||
//ok
|
||||
public <T extends Doc> DocumentDeleteEntity<Void> delete(String key, Class<T> type) {
|
||||
return collection(type).deleteDocument(key);
|
||||
}
|
||||
|
||||
//ok
|
||||
public <T extends Doc> MultiDocumentEntity<DocumentDeleteEntity<Void>> deleteAll(Doc<T>... docs) {
|
||||
return collection(docs[0]).deleteDocuments(asList(docs));
|
||||
}
|
||||
|
||||
public <T extends Doc> MultiDocumentEntity find(Collection keys, Class<T> type) {
|
||||
return collection(type).getDocuments(keys, type);
|
||||
}
|
||||
}
|
||||
@@ -1,271 +0,0 @@
|
||||
package net.tccn.base.arango;
|
||||
|
||||
import com.arangodb.ArangoCollection;
|
||||
import com.arangodb.ArangoDBException;
|
||||
import com.arangodb.ArangoDatabase;
|
||||
import com.arangodb.entity.DocumentCreateEntity;
|
||||
import net.tccn.base.PageBean;
|
||||
import org.redkale.source.Flipper;
|
||||
|
||||
import javax.persistence.Table;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* @author: liangxianyou at 2018/12/1 22:59.
|
||||
*/
|
||||
public abstract class Doc<T extends Doc> {
|
||||
private HashMap attr = new HashMap();
|
||||
|
||||
private String _id;
|
||||
private String _key;
|
||||
|
||||
private Set<String> _shows;
|
||||
private Map _order;
|
||||
private List<Map> _filters;//[{col, value, expr}]
|
||||
|
||||
public String getId() {
|
||||
return _id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this._id = id;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return _key;
|
||||
}
|
||||
|
||||
public void setKey(String key) {
|
||||
this._key = key;
|
||||
}
|
||||
|
||||
protected Doc<T> set(String k, Object v) {
|
||||
attr.put(k, v);
|
||||
return this;
|
||||
}
|
||||
|
||||
protected <V> V get(String k) {
|
||||
return (V)attr.get(k);
|
||||
}
|
||||
|
||||
/*public T setShows(String... show) {
|
||||
if (_shows == null) {
|
||||
_shows = new HashSet<>();
|
||||
}
|
||||
for (String s : show) {
|
||||
_shows.add(s);
|
||||
}
|
||||
return (T) this;
|
||||
}*/
|
||||
|
||||
public Set<String> get_Shows() {
|
||||
return _shows;
|
||||
}
|
||||
|
||||
/*public void set_Shows(Set<String> shows) {
|
||||
this._shows = shows;
|
||||
}*/
|
||||
|
||||
public T setOrder(String col, int desc) {
|
||||
if (_order == null) {
|
||||
_order = new LinkedHashMap();
|
||||
}
|
||||
|
||||
_order.put(col, desc);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
public Map<String, Integer> getOrder() {
|
||||
return _order;
|
||||
}
|
||||
|
||||
/*public void setOrder(Map order) {
|
||||
this._order = order;
|
||||
}*/
|
||||
/*@Override
|
||||
public String toString() {
|
||||
//convert.
|
||||
|
||||
String doc = convert.convertTo(this);
|
||||
if (attr.size() == 0) {
|
||||
return doc;
|
||||
}
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
if (attr.size() != 0) {
|
||||
String attrStr = convert.convertTo(attr);
|
||||
builder.append("{");
|
||||
builder.append(attrStr, 1, attrStr.length() - 1);
|
||||
builder.append(",");
|
||||
builder.append(doc, 1, doc.length()); //builder.append(doc.substring(1));
|
||||
}
|
||||
|
||||
return builder.toString();
|
||||
}*/
|
||||
|
||||
private Function<String, String> fieldName = (s) -> {
|
||||
return s.replace("get", "").substring(0, 1).toLowerCase() + s.substring(4);
|
||||
};
|
||||
|
||||
/**
|
||||
* 提取Doc 属性到 Map,用于存贮到Arango中
|
||||
* 提取规则:
|
||||
* 1、将doc中自带非空( !=null )属性提取都map中,
|
||||
* 2、将attr中属性覆盖到map中(如果attr中存在同名属性,attr为主)
|
||||
* @return
|
||||
*/
|
||||
public Map toDoc() {
|
||||
HashMap clone = (HashMap) attr.clone();
|
||||
|
||||
Class<? extends Doc> type = this.getClass();
|
||||
Method[] methods = type.getDeclaredMethods();
|
||||
|
||||
for (Method method : methods) {
|
||||
String name = method.getName();
|
||||
if (name.startsWith("get") && method.getParameterCount() == 0) {
|
||||
//Type mt = method.getAnnotatedReturnType().getType();
|
||||
try {
|
||||
//System.out.println(method.getName());
|
||||
Object v = method.invoke(this);
|
||||
if (v != null) {
|
||||
clone.put(fieldName.apply(name), v);
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
return clone;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------------------
|
||||
|
||||
private ArangoSource arangoSource;
|
||||
private ArangoDatabase db;
|
||||
private ArangoCollection collection;
|
||||
|
||||
private static ConcurrentHashMap<Class, Doc> daos = new ConcurrentHashMap();
|
||||
public Doc() {
|
||||
Table table = this.getClass().getAnnotation(Table.class);
|
||||
String sourceName = null;
|
||||
Source source = this.getClass().getAnnotation(Source.class);
|
||||
if (source != null) {
|
||||
sourceName = source.name();
|
||||
}
|
||||
|
||||
try {
|
||||
arangoSource = ArangoSource.use(sourceName);
|
||||
this.db = arangoSource.db(table.catalog());
|
||||
this.collection = arangoSource.collection(this);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
protected final static <T extends Doc> T dao(Class<T> type) {
|
||||
|
||||
if (daos.get(type) == null) {
|
||||
try {
|
||||
daos.put(type, type.newInstance());
|
||||
} catch (InstantiationException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return (T) daos.get(type);
|
||||
}
|
||||
|
||||
//ok
|
||||
public T save() {
|
||||
DocumentCreateEntity<Map> tmap = collection.insertDocument(this.toDoc());
|
||||
this.setId(tmap.getId());
|
||||
this.setKey(tmap.getKey());
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
//ok
|
||||
public void update() {
|
||||
collection.updateDocument(this.getKey(), this.toDoc());
|
||||
}
|
||||
|
||||
public <T extends Doc> PageBean<T> findPage(T t, Flipper flipper) {
|
||||
if (flipper == null) {
|
||||
flipper = new Flipper();
|
||||
}
|
||||
if (t == null) {
|
||||
t = (T) this;
|
||||
}
|
||||
|
||||
List<T> list = find(t, flipper.getOffset(), flipper.getLimit());
|
||||
long count = count(t);
|
||||
return PageBean.by(list, count);
|
||||
}
|
||||
|
||||
public T findFirst(T t) {
|
||||
return findFirst(arangoSource.parseAql(t, 0, 1), (Class<T>) t.getClass());
|
||||
}
|
||||
public List<T> find() {
|
||||
int count = count(this);
|
||||
return find((T) this, 0, count);
|
||||
}
|
||||
public List<T> find(T t) {
|
||||
return find(t, 0, 1000);
|
||||
}
|
||||
public <T extends Doc> List<T> find(T t, int offset, int ps) {
|
||||
if (t == null) {
|
||||
t = (T) this;
|
||||
}
|
||||
return find(arangoSource.parseAql(t, offset, ps), (Class<T>)this.getClass());
|
||||
}
|
||||
|
||||
public <T> List<T> find(String aql, Class<T> clazz) {
|
||||
try {
|
||||
return db.query(aql, clazz).asListRemaining();
|
||||
} catch (ArangoDBException e) {
|
||||
System.out.println(aql);
|
||||
e.printStackTrace();
|
||||
}
|
||||
return db.query(aql, clazz).asListRemaining();
|
||||
}
|
||||
public <T> T findFirst(String aql, Class<T> clazz) {
|
||||
try {
|
||||
return db.query(aql, clazz).first();
|
||||
} catch (ArangoDBException e) {
|
||||
System.out.println(aql);
|
||||
e.printStackTrace();
|
||||
}
|
||||
return db.query(aql, clazz).first();
|
||||
}
|
||||
|
||||
public int count() {
|
||||
return count(this);
|
||||
}
|
||||
public <T extends Doc> int count(T t) {
|
||||
if (t == null) {
|
||||
t = (T) this;
|
||||
}
|
||||
// arangoSource.createDocument(this);
|
||||
return db.query(arangoSource.parseAqlCount(t), int.class).first();
|
||||
}
|
||||
//ok
|
||||
public <T extends Doc> T findByKey(Object key) {
|
||||
return (T) collection.getDocument(String.valueOf(key), this.getClass());
|
||||
}
|
||||
|
||||
// ok todo: 将数据放入回收库
|
||||
public void delete() {
|
||||
collection.deleteDocument(getKey());
|
||||
}
|
||||
|
||||
public void delete(Collection<String> key) {
|
||||
collection.deleteDocuments(key);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package net.tccn.base.arango;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* @author: liangxianyou at 2018/12/22 0:32.
|
||||
*/
|
||||
@Target({ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Source {
|
||||
String name() default "";
|
||||
}
|
||||
Reference in New Issue
Block a user