Skip to content

Commit

Permalink
committing in case I ever want to come back to this, but for now, i'm…
Browse files Browse the repository at this point in the history
… deleting the orm stuff, let's just do this the easy way
  • Loading branch information
PiggyPiglet committed Dec 27, 2020
1 parent 7501b6c commit 6cbe6cd
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 4 deletions.
Expand Up @@ -22,6 +22,7 @@ public TableManager(@NotNull final QueryRunner queryRunner, @NotNull final Map<C
}

public void loadTable(@NotNull final Class<?> table) {
queryRunner.applySchema(structures.get(table));
queryRunner.insert(structures.get(table));
// queryRunner.applySchema(structures.get(table));
}
}
Expand Up @@ -3,12 +3,17 @@
import co.aikar.idb.Database;
import com.google.inject.Inject;
import me.piggypiglet.docdex.db.orm.query.types.ExistsQuery;
import me.piggypiglet.docdex.db.orm.query.types.InsertQuery;
import me.piggypiglet.docdex.db.orm.query.types.SchemaQuery;
import me.piggypiglet.docdex.db.orm.structure.TableStructure;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.Map;
import java.util.Set;

// ------------------------------
// Copyright (c) PiggyPiglet 2020
// https://www.piggypiglet.me
Expand All @@ -21,13 +26,15 @@ public final class QueryRunner {

private final SchemaQuery schema;
private final ExistsQuery exists;
private final InsertQuery insert;

@Inject
public QueryRunner(@NotNull final Database database, @NotNull final SchemaQuery schema,
@NotNull final ExistsQuery exists) {
@NotNull final ExistsQuery exists, @NotNull final InsertQuery insert) {
this.database = database;
this.schema = schema;
this.exists = exists;
this.insert = insert;
}

public boolean tableExists(@NotNull final TableStructure table) {
Expand Down Expand Up @@ -62,4 +69,20 @@ public void applySchema(@NotNull final TableStructure table) {

LOGGER.info("Successfully applied schematic(s) for " + table.getName());
}

public void insert(@NotNull final TableStructure table) {
final Map<String, Object> map = Map.of(
"id", "312312",
"prefix", "d;",
"rules", Map.of(
"help", Map.of(
"allowed", Set.of("4312132", "42143812"),
"disallowed", Collections.emptySet(),
"recommendation", "blah blah"
)
)
);

insert.insert(map, table);
}
}
@@ -0,0 +1,80 @@
package me.piggypiglet.docdex.db.orm.query.types;

import me.piggypiglet.docdex.db.orm.structure.TableStructure;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

// ------------------------------
// Copyright (c) PiggyPiglet 2020
// https://www.piggypiglet.me
// ------------------------------
public final class InsertQuery {
@NotNull
public String insert(@NotNull final Map<String, Object> data, @NotNull final TableStructure table) {
final Map<String, Object> mappedData = new HashMap<>();

Map<TableStructure, Object> datum = Map.of(table, data);
do {
datum.forEach((structure, object) -> mappedData.put(structure.getName(), object));

final Map<TableStructure, Object> newDatum = new HashMap<>();

for (final TableStructure parent : datum.keySet()) {
final String parentName = parent.getName();
final Map<String, Object> parentMap = (Map<String, Object>) datum.get(parent);

for (final TableStructure sub : parent.getSubStructures()) {
System.out.println(sub.getName() + " - " + parentMap + " - " + sub.getClazz() + " - " + parent.getIdentifier().getName());

if (parent.getIdentifier().getName().equals("key")) {
final Set<Map<String, Object>> set = new HashSet<>();
parentMap.forEach((key, m) -> {
final Map<String, Object> dasd = new HashMap<>();
dasd.put("key", key);
((Map<String, Object>) m).forEach(dasd::put);
set.add(dasd);
});

newDatum.put(sub, set);
} else {
newDatum.put(sub, parentMap.get(sub.getName().replace(parentName + '_', "")));
}
}
}

datum = newDatum;
} while (!datum.isEmpty());

System.out.println(mappedData);

// final Set<TableStructure> all = Stream.concat(Stream.of(table), Query.getAll(table)).collect(Collectors.toSet());
// final Map<TableStructure, TableStructure> parents = new HashMap<>();
//
// all.forEach(structure -> structure.getSubStructures().forEach(sub -> parents.put(sub, structure)));
//
// final Map<Integer, Map<TableStructure, Map<String, Object>>> levels = new HashMap<>();
//
// final Map<String, Map<String, Object>> splitData = new HashMap<>();
//
// all.forEach(structure -> {
// final Map<String, Object> map = new HashMap<>();
//
// structure.getColumns().forEach(column -> {
// final String name = column.getLevel() + ":" + column.getName();
//
// Map<String, Object> level = data.get(column.getLevel());
// for (int j = column.getLevel(); j > 0; --j) {
//
// }
// });
//
// splitData.put(structure.getName(), map);
// });

return "";
}
}
Expand Up @@ -28,6 +28,8 @@ public Set<String> generate(@NotNull final TableStructure structure) {

for (final TableField column : struct.getColumns()) {
builder.append("`")
.append(column.getLevel())
.append(":")
.append(column.getName())
.append("` ")
.append(column.getType())
Expand All @@ -44,7 +46,7 @@ public Set<String> generate(@NotNull final TableStructure structure) {

builder.append("PRIMARY KEY (`")
.append(identifier.getName())
.append('`');
.append("`");

if (identifier.getType() == SqlDataTypes.TEXT) {
builder.append("(")
Expand Down
Expand Up @@ -25,7 +25,7 @@ private TableField(@Nullable final String string, @Nullable final Field field,
this.field = field;
this.level = level;

this.name = level + ":" + (isField() ? Objects.requireNonNull(field).getName().toLowerCase() : string);
this.name = isField() ? Objects.requireNonNull(field).getName().toLowerCase() : string;
this.type = isField() ? SqlDataTypes.from(Objects.requireNonNull(field).getType()).orElse(SqlDataTypes.TEXT) : SqlDataTypes.TEXT;
}

Expand Down

0 comments on commit 6cbe6cd

Please sign in to comment.