use of com.baidu.hugegraph.type.define.HugeKeys in project incubator-hugegraph by apache.
the class PostgresqlTable method orderByKeys.
@Override
protected String orderByKeys() {
// Set order-by to keep results order consistence for PostgreSQL result
if (this.orderByKeysTemplate != null) {
return this.orderByKeysTemplate;
}
int i = 0;
int size = this.tableDefine().keys().size();
StringBuilder select = new StringBuilder(" ORDER BY ");
for (HugeKeys hugeKey : this.tableDefine().keys()) {
String key = formatKey(hugeKey);
select.append(key).append(" ");
select.append("ASC ");
if (++i != size) {
select.append(", ");
}
}
this.orderByKeysTemplate = select.toString();
return this.orderByKeysTemplate;
}
use of com.baidu.hugegraph.type.define.HugeKeys in project incubator-hugegraph by apache.
the class PostgresqlTable method buildInsertTemplateForce.
@Override
protected String buildInsertTemplateForce(MysqlBackendEntry.Row entry) {
StringBuilder insert = new StringBuilder();
insert.append("INSERT INTO ").append(this.table()).append(" (");
int i = 0;
int size = entry.columns().size();
for (HugeKeys key : entry.columns().keySet()) {
insert.append(formatKey(key));
if (++i != size) {
insert.append(", ");
}
}
insert.append(") VALUES (");
for (i = 0; i < size; i++) {
insert.append("?");
if (i != size - 1) {
insert.append(", ");
}
}
insert.append(")");
i = 0;
size = this.tableDefine().keys().size();
insert.append(" ON CONFLICT (");
for (HugeKeys key : this.tableDefine().keys()) {
insert.append(formatKey(key));
if (++i != size) {
insert.append(", ");
}
}
insert.append(")");
i = 0;
size = entry.columns().keySet().size();
insert.append(" DO UPDATE SET ");
for (HugeKeys key : entry.columns().keySet()) {
insert.append(formatKey(key)).append(" = ?");
if (++i != size) {
insert.append(", ");
}
}
return insert.toString();
}
use of com.baidu.hugegraph.type.define.HugeKeys in project incubator-hugegraph by apache.
the class MysqlTable method createTable.
protected void createTable(Session session, TableDefine tableDefine) {
StringBuilder sql = new StringBuilder();
sql.append("CREATE TABLE IF NOT EXISTS ");
sql.append(this.table()).append(" (");
// Add columns
for (Map.Entry<HugeKeys, String> entry : tableDefine.columns().entrySet()) {
sql.append(formatKey(entry.getKey()));
sql.append(" ");
sql.append(entry.getValue());
sql.append(", ");
}
// Specified primary keys
sql.append(" PRIMARY KEY (");
int i = 0;
int size = tableDefine.keys().size();
for (HugeKeys key : tableDefine.keys()) {
sql.append(formatKey(key));
if (++i != size) {
sql.append(", ");
}
}
sql.append("))");
sql.append(this.engine(session));
sql.append(";");
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 MysqlTable method wrapPage.
protected void wrapPage(StringBuilder select, Query query, boolean scan) {
String page = query.page();
// It's the first time if page is empty
if (!page.isEmpty()) {
byte[] position = PageState.fromString(page).position();
Map<HugeKeys, Object> columns = PagePosition.fromBytes(position).columns();
List<HugeKeys> idColumnNames = this.idColumnName();
List<Object> values = new ArrayList<>(idColumnNames.size());
for (HugeKeys key : idColumnNames) {
values.add(columns.get(key));
}
// Need add `where` to `select` when query is IdQuery
boolean expectWhere = scan || query.conditionsSize() == 0;
WhereBuilder where = this.newWhereBuilder(expectWhere);
if (!expectWhere) {
where.and();
}
where.gte(formatKeys(idColumnNames), values);
select.append(where.build());
}
}
use of com.baidu.hugegraph.type.define.HugeKeys in project incubator-hugegraph by apache.
the class MysqlTable method queryByRange.
protected StringBuilder queryByRange(ConditionQuery query, StringBuilder select) {
E.checkArgument(query.relations().size() == 1, "Invalid scan with multi conditions: %s", query);
Condition.Relation scan = query.relations().iterator().next();
Shard shard = (Shard) scan.value();
String page = query.page();
if (MysqlShardSplitter.START.equals(shard.start()) && MysqlShardSplitter.END.equals(shard.end()) && (page == null || page.isEmpty())) {
this.wrapLimit(select, query);
return select;
}
HugeKeys partitionKey = this.idColumnName().get(0);
if (page != null && !page.isEmpty()) {
// >= page
this.wrapPage(select, query, true);
// < end
WhereBuilder where = this.newWhereBuilder(false);
if (!MysqlShardSplitter.END.equals(shard.end())) {
where.and();
where.lt(formatKey(partitionKey), shard.end());
}
select.append(where.build());
} else {
// >= start
WhereBuilder where = this.newWhereBuilder();
boolean hasStart = false;
if (!MysqlShardSplitter.START.equals(shard.start())) {
where.gte(formatKey(partitionKey), shard.start());
hasStart = true;
}
// < end
if (!MysqlShardSplitter.END.equals(shard.end())) {
if (hasStart) {
where.and();
}
where.lt(formatKey(partitionKey), shard.end());
}
select.append(where.build());
}
this.wrapLimit(select, query);
return select;
}
Aggregations