use of com.baidu.hugegraph.type.define.HugeKeys in project incubator-hugegraph by apache.
the class MysqlTable method buildInsertObjects.
protected List<Object> buildInsertObjects(MysqlBackendEntry.Row entry) {
List<Object> objects = new ArrayList<>();
for (Map.Entry<HugeKeys, Object> e : entry.columns().entrySet()) {
Object value = e.getValue();
String type = this.tableDefine().columns().get(e.getKey());
if (type.startsWith(DECIMAL)) {
value = new BigDecimal(value.toString());
}
objects.add(value);
}
return objects;
}
use of com.baidu.hugegraph.type.define.HugeKeys in project incubator-hugegraph by apache.
the class MysqlTable method delete.
@Override
public void delete(Session session, MysqlBackendEntry.Row entry) {
List<HugeKeys> idNames = this.idColumnName();
String template = this.buildDeleteTemplate(idNames);
PreparedStatement deleteStmt;
try {
deleteStmt = session.prepareStatement(template);
if (entry.columns().isEmpty()) {
// Delete just by id
List<Long> idValues = this.idColumnValue(entry);
assert idNames.size() == idValues.size();
for (int i = 0, n = idNames.size(); i < n; i++) {
deleteStmt.setObject(i + 1, idValues.get(i));
}
} else {
// Delete just by column keys(must be id columns)
for (int i = 0, n = idNames.size(); i < n; i++) {
HugeKeys key = idNames.get(i);
Object value = entry.column(key);
deleteStmt.setObject(i + 1, value);
}
}
} catch (SQLException e) {
throw new BackendException("Failed to prepare statement '%s'" + "with entry columns %s", template, entry.columns().values());
}
session.add(deleteStmt);
}
use of com.baidu.hugegraph.type.define.HugeKeys in project incubator-hugegraph by apache.
the class PaloTable method insert.
@Override
public void insert(MysqlSessions.Session session, MysqlBackendEntry.Row entry) {
assert session instanceof PaloSessions.Session;
PaloSessions.Session paloSession = (PaloSessions.Session) session;
Set<HugeKeys> columnNames = this.tableDefine().columnNames();
// Ensure column order match with table define
List<Object> columnValues = new ArrayList<>(columnNames.size());
for (HugeKeys key : columnNames) {
columnValues.add(entry.column(key));
}
String insert = StringUtils.join(columnValues, "\t");
paloSession.add(this.table(), insert);
}
use of com.baidu.hugegraph.type.define.HugeKeys in project incubator-hugegraph by apache.
the class PaloTable method createTable.
@Override
public void createTable(MysqlSessions.Session session, TableDefine tableDefine) {
StringBuilder sql = new StringBuilder();
sql.append("CREATE TABLE IF NOT EXISTS ");
sql.append(this.table()).append(" (");
// Add columns
int i = 0;
for (Map.Entry<HugeKeys, String> entry : tableDefine.columns().entrySet()) {
sql.append(formatKey(entry.getKey()));
sql.append(" ");
sql.append(entry.getValue());
if (++i != tableDefine.columns().size()) {
sql.append(", ");
}
}
sql.append(")");
// Unique keys
sql.append(" UNIQUE KEY(");
i = 0;
for (HugeKeys key : tableDefine.keys()) {
sql.append(formatKey(key));
if (++i != tableDefine.keys().size()) {
sql.append(", ");
}
}
sql.append(")");
// Hash keys
sql.append(" DISTRIBUTED BY HASH(");
i = 0;
for (HugeKeys key : tableDefine.keys()) {
sql.append(formatKey(key));
if (++i != tableDefine.keys().size()) {
sql.append(", ");
}
}
sql.append(");");
// TODO: 'replication_num(default=3)’ can be a configuration
LOG.debug("Create table: {}", sql);
try {
session.execute(sql.toString());
} catch (SQLException e) {
throw new BackendException("Failed to create table with '%s'", e, sql);
}
}
use of com.baidu.hugegraph.type.define.HugeKeys in project incubator-hugegraph by apache.
the class CassandraTable method buildEliminate.
protected Update buildEliminate(CassandraBackendEntry.Row entry) {
List<HugeKeys> idNames = this.idColumnName();
List<HugeKeys> colNames = this.modifiableColumnName();
Map<HugeKeys, Object> columns = entry.columns();
// Update by id
Update update = QueryBuilder.update(table());
for (HugeKeys key : colNames) {
/*
* NOTE: eliminate from map<text, text> should just pass key,
* if use the following statement:
* UPDATE vertices SET PROPERTIES=PROPERTIES-{'city':'"Wuhan"'}
* WHERE LABEL='person' AND PRIMARY_VALUES='josh';
* it will throw a cassandra exception:
* Invalid map literal for properties of typefrozen<set<text>>
*/
if (!columns.containsKey(key)) {
continue;
}
String name = formatKey(key);
Object value = columns.get(key);
if (value instanceof Map) {
@SuppressWarnings("rawtypes") Set<?> keySet = ((Map) value).keySet();
update.with(QueryBuilder.removeAll(name, keySet));
} else if (value instanceof Set) {
update.with(QueryBuilder.removeAll(name, (Set<?>) value));
} else if (value instanceof List) {
Set<?> keySet = new HashSet<>((List<?>) value);
update.with(QueryBuilder.removeAll(name, keySet));
} else {
update.with(QueryBuilder.remove(name, value));
}
}
for (HugeKeys idName : idNames) {
assert columns.containsKey(idName);
update.where(formatEQ(idName, columns.get(idName)));
}
return update;
}
Aggregations