Skip to content

Commit

Permalink
new repo
Browse files Browse the repository at this point in the history
  • Loading branch information
Pawel Szymczyk committed Jan 18, 2015
1 parent de88803 commit 757cddd
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 25 deletions.
Expand Up @@ -34,20 +34,6 @@ public SqlRepositoryBuilder withConnectionProvider(ConnectionProvider connection
return this;
}

/*
public SqlRepositoryBuilder withDatabaseUrl(String databaseUrl) {
jdbcConfiguration.withDatabaseUrl(databaseUrl);
return this;
}
public SqlRepositoryBuilder withUsername(String username) {
jdbcConfiguration.withUsername(username);
return this;
}
public SqlRepositoryBuilder withPassword(String password) {
jdbcConfiguration.withPassword(password);
return this;
}*/

public JaversSqlRepository build() {
logger.info("starting up SQL repository module ...");
bootContainer();
Expand Down
@@ -0,0 +1,29 @@
package org.javers.repository.sql.domain;

import org.javers.core.commit.CommitMetadata;
import org.javers.core.json.JsonConverter;
import org.javers.repository.sql.poly.JaversPolyJDBC;
import org.javers.repository.sql.schema.FixedSchemaFactory;
import org.polyjdbc.core.query.InsertQuery;

public class CommitRepository {

private JaversPolyJDBC javersPolyjdbc;
private JsonConverter jsonConverter;

public CommitRepository(JaversPolyJDBC javersPolyjdbc, JsonConverter jsonConverter) {
this.javersPolyjdbc = javersPolyjdbc;
this.jsonConverter = jsonConverter;
}

public Long save(CommitMetadata commitMetadata) {
InsertQuery query = javersPolyjdbc.query().insert().into(FixedSchemaFactory.COMMIT_TABLE_NAME)
.value(FixedSchemaFactory.COMMIT_TABLE_AUTHOR, commitMetadata.getAuthor())
.value(FixedSchemaFactory.COMMIT_TABLE_COMMIT_DATE, jsonConverter.toJson(commitMetadata.getCommitDate()))
.value(FixedSchemaFactory.COMMIT_TABLE_COMMIT_ID, commitMetadata.getId().toString());

javersPolyjdbc.queryRunner().insert(query);

return 1L;
}
}
Expand Up @@ -17,7 +17,6 @@ public class GlobalIdRepository {
private JsonConverter jsonConverter;

public GlobalIdRepository(JaversPolyJDBC javersPolyjdbc, JsonConverter jsonConverter) {

this.javersPolyjdbc = javersPolyjdbc;
this.jsonConverter = jsonConverter;
}
Expand Down
Expand Up @@ -2,6 +2,7 @@

import org.javers.core.pico.JaversModule;
import org.javers.repository.sql.JaversSqlRepository;
import org.javers.repository.sql.domain.CommitRepository;
import org.javers.repository.sql.domain.GlobalIdRepository;
import org.javers.repository.sql.poly.JaversPolyJDBC;
import org.javers.repository.sql.schema.FixedSchemaFactory;
Expand All @@ -17,14 +18,15 @@
*
* @author bartosz walacik
*/
public class JaversSqlModule implements JaversModule{
private static Class[] moduleComponents = new Class[] {JaversPolyJDBC.class,
JaversSqlRepository.class,
FixedSchemaFactory.class,
JaversSchemaManager.class,
ProvidedConnectionTransactionManager.class,
QueryRunnerFactory.class,
GlobalIdRepository.class};
public class JaversSqlModule implements JaversModule {
private static Class[] moduleComponents = new Class[]{JaversPolyJDBC.class,
JaversSqlRepository.class,
FixedSchemaFactory.class,
JaversSchemaManager.class,
ProvidedConnectionTransactionManager.class,
QueryRunnerFactory.class,
GlobalIdRepository.class,
CommitRepository.class};

@Override
public Collection<Class> getComponents() {
Expand Down
Expand Up @@ -79,7 +79,11 @@ private Schema commitTableSchema(Dialect dialect, String tableName) {
.withAttribute().timestamp(COMMIT_TABLE_COMMIT_DATE).and()
.withAttribute().string(COMMIT_TABLE_COMMIT_ID).withMaxLength(10).and()
.build();
return schema;
// return schema;

return schema != null ? schema : new Schema(null);


}

private Schema cdoClassTableSchema(Dialect dialect, String tableName) {
Expand Down
@@ -0,0 +1,60 @@
package org.javers.repository.sql.domain

import org.javers.core.Javers
import org.javers.core.JaversBuilder
import org.javers.core.commit.Commit
import org.javers.core.commit.CommitId
import org.javers.core.commit.CommitMetadata
import org.javers.core.json.JsonConverterBuilder
import org.javers.core.model.DummyUser
import org.javers.repository.sql.ConnectionProvider
import org.javers.repository.sql.DialectName
import org.javers.repository.sql.SqlRepositoryTestBuilder
import org.joda.time.LocalDateTime
import spock.lang.Specification

import java.sql.Connection
import java.sql.DriverManager


class CommitRepositoryTest extends Specification {

def "should save commit"() {

given:
def jsonConverter = JsonConverterBuilder.jsonConverter().build()

def connection = DriverManager.getConnection(
"jdbc:postgresql://localhost:5432/pawel.szymczyk", "pawel.szymczyk", "");
connection.setAutoCommit(false);

def builder = SqlRepositoryTestBuilder.sqlRepository().withConnectionProvider(new ConnectionProvider() {
@Override
Connection getConnection() {
connection
}
}).withDialect(DialectName.POSTGRES).withJSONConverter(jsonConverter)

builder.build()

def commitRepository = builder.getComponent(CommitRepository)

when:
def id = commitRepository.save(new CommitMetadata("author", LocalDateTime.now(), new CommitId(1L,0)))
connection.commit()

then:
true

// then:
// globalIdRepository.get(id).id == "kazik"
// globalIdRepository.get(id).class == DummyUser
//
// when:
// def nextId = globalIdRepository.save(globalId)
//
// then:
// nextId == id

}
}
Expand Up @@ -21,7 +21,7 @@ class FixedSchemaFactoryIntegrationTest extends Specification {
when:
Class.forName("org.postgresql.Driver");
def connection = DriverManager.getConnection(
"jdbc:postgresql://localhost:5432/bartosz.galek", "bartosz.galek", "");
"jdbc:postgresql://localhost:5432/pawel.szymczyk", "pawel.szymczyk", "");

def repo = SqlRepositoryBuilder
.sqlRepository()
Expand Down

0 comments on commit 757cddd

Please sign in to comment.