Search in sources :

Example 1 with JavaSchemaModel

use of com.google.gwtorm.schema.java.JavaSchemaModel in project gerrit by GerritCodeReview.

the class ProtoGen method run.

@Override
public int run() throws Exception {
    LockFile lock = new LockFile(file.getAbsoluteFile());
    if (!lock.lock()) {
        throw die("Cannot lock " + file);
    }
    try {
        JavaSchemaModel jsm = new JavaSchemaModel(ReviewDb.class);
        try (OutputStream o = lock.getOutputStream();
            PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(o, UTF_8)))) {
            String header;
            try (InputStream in = getClass().getResourceAsStream("ProtoGenHeader.txt")) {
                ByteBuffer buf = IO.readWholeStream(in, 1024);
                int ptr = buf.arrayOffset() + buf.position();
                int len = buf.remaining();
                header = new String(buf.array(), ptr, len, UTF_8);
            }
            String version = com.google.gerrit.common.Version.getVersion();
            out.write(header.replace("@@VERSION@@", version));
            jsm.generateProto(out);
            out.flush();
        }
        if (!lock.commit()) {
            throw die("Could not write to " + file);
        }
    } finally {
        lock.unlock();
    }
    System.out.println("Created " + file.getPath());
    return 0;
}
Also used : LockFile(org.eclipse.jgit.internal.storage.file.LockFile) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) ByteBuffer(java.nio.ByteBuffer) JavaSchemaModel(com.google.gwtorm.schema.java.JavaSchemaModel) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Example 2 with JavaSchemaModel

use of com.google.gwtorm.schema.java.JavaSchemaModel in project gerrit by GerritCodeReview.

the class ProtobufImport method run.

@Override
public int run() throws Exception {
    mustHaveValidSite();
    Injector dbInjector = createDbInjector(SINGLE_USER);
    manager.add(dbInjector);
    manager.start();
    RuntimeShutdown.add(manager::stop);
    dbInjector.injectMembers(this);
    ProgressMonitor progress = new TextProgressMonitor();
    progress.beginTask("Importing entities", ProgressMonitor.UNKNOWN);
    try (ReviewDb db = schemaFactory.open()) {
        for (RelationModel model : new JavaSchemaModel(ReviewDb.class).getRelations()) {
            relations.put(model.getRelationID(), Relation.create(model, db));
        }
        Parser<UnknownFieldSet> parser = UnknownFieldSet.getDefaultInstance().getParserForType();
        try (InputStream in = new BufferedInputStream(Files.newInputStream(file.toPath()))) {
            UnknownFieldSet msg;
            while ((msg = parser.parseDelimitedFrom(in)) != null) {
                Map.Entry<Integer, UnknownFieldSet.Field> e = Iterables.getOnlyElement(msg.asMap().entrySet());
                Relation rel = checkNotNull(relations.get(e.getKey()), "unknown relation ID %s in message: %s", e.getKey(), msg);
                List<ByteString> values = e.getValue().getLengthDelimitedList();
                checkState(values.size() == 1, "expected one string field in message: %s", msg);
                upsert(rel, values.get(0));
                progress.update(1);
            }
        }
        progress.endTask();
    }
    return 0;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ByteString(com.google.protobuf.ByteString) TextProgressMonitor(org.eclipse.jgit.lib.TextProgressMonitor) ProgressMonitor(org.eclipse.jgit.lib.ProgressMonitor) TextProgressMonitor(org.eclipse.jgit.lib.TextProgressMonitor) BufferedInputStream(java.io.BufferedInputStream) Injector(com.google.inject.Injector) RelationModel(com.google.gwtorm.schema.RelationModel) UnknownFieldSet(com.google.protobuf.UnknownFieldSet) HashMap(java.util.HashMap) Map(java.util.Map) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) JavaSchemaModel(com.google.gwtorm.schema.java.JavaSchemaModel)

Example 3 with JavaSchemaModel

use of com.google.gwtorm.schema.java.JavaSchemaModel in project gerrit by GerritCodeReview.

the class Schema_101 method findPKUpdates.

private Map<String, PrimaryKey> findPKUpdates() throws OrmException, SQLException {
    Map<String, PrimaryKey> corrections = new TreeMap<>();
    DatabaseMetaData meta = conn.getMetaData();
    JavaSchemaModel jsm = new JavaSchemaModel(ReviewDb.class);
    for (RelationModel rm : jsm.getRelations()) {
        String tableName = rm.getRelationName();
        List<String> expectedPKCols = relationPK(rm);
        PrimaryKey actualPK = dbTablePK(meta, tableName);
        if (!expectedPKCols.equals(actualPK.cols)) {
            actualPK.cols = expectedPKCols;
            corrections.put(tableName, actualPK);
        }
    }
    return corrections;
}
Also used : RelationModel(com.google.gwtorm.schema.RelationModel) TreeMap(java.util.TreeMap) DatabaseMetaData(java.sql.DatabaseMetaData) JavaSchemaModel(com.google.gwtorm.schema.java.JavaSchemaModel)

Aggregations

JavaSchemaModel (com.google.gwtorm.schema.java.JavaSchemaModel)3 RelationModel (com.google.gwtorm.schema.RelationModel)2 InputStream (java.io.InputStream)2 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)1 Injector (com.google.inject.Injector)1 ByteString (com.google.protobuf.ByteString)1 UnknownFieldSet (com.google.protobuf.UnknownFieldSet)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedWriter (java.io.BufferedWriter)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 ByteBuffer (java.nio.ByteBuffer)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 LockFile (org.eclipse.jgit.internal.storage.file.LockFile)1 ProgressMonitor (org.eclipse.jgit.lib.ProgressMonitor)1 TextProgressMonitor (org.eclipse.jgit.lib.TextProgressMonitor)1