DataSource dataSource = dbcpDataSource( host, port );
// DataSource dataSource = rawDataSource(host,port);
TransactionProvider transactionProvider = new PolygeneTransactionProvider( new ThreadLocalTransactionProvider( connectionProvider, false ) );
// TransactionProvider transactionProvider = new ThreadLocalTransactionProvider( connectionProvider, false );
package org.hedhman.niclas;import javax.sql.DataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.jooq.Configuration;
import org.jooq.ConnectionProvider;
import org.jooq.DSLContext;
import org.jooq.Field;
import org.jooq.Name;
import org.jooq.SQLDialect;
import org.jooq.Schema;
import org.jooq.TransactionContext;
import org.jooq.TransactionProvider;
import org.jooq.conf.RenderNameStyle;
import org.jooq.conf.Settings;
import org.jooq.exception.DataAccessException;
import org.jooq.impl.DSL;
import org.jooq.impl.DataSourceConnectionProvider;
import org.jooq.impl.DefaultConfiguration;
import org.jooq.impl.ThreadLocalTransactionProvider;
import org.junit.Test;
import org.postgresql.ds.PGSimpleDataSource;
import org.postgresql.jdbc.AutoSave;import static org.apache.polygene.entitystore.sql.TableFields.tableNameColumn;
import static org.apache.polygene.entitystore.sql.TypesTable.makeField;
public class Experiment
{
@Test
public void test1()
throws Exception
{
String host = "127.0.0.1";
int port = 5432;
DataSource dataSource = dbcpDataSource( host, port );
// DataSource dataSource = rawDataSource(host,port);
Settings settings = new Settings().withRenderNameStyle( RenderNameStyle.QUOTED );
SQLDialect dialect = SQLDialect.POSTGRES;
Schema schema = DSL.schema( DSL.name( "POLYGENE" ) );
ConnectionProvider connectionProvider = new DataSourceConnectionProvider( dataSource );
TransactionProvider transactionProvider = new PolygeneTransactionProvider( new ThreadLocalTransactionProvider( connectionProvider, false ) );
// TransactionProvider transactionProvider = new ThreadLocalTransactionProvider( connectionProvider, false );
Configuration configuration = new DefaultConfiguration()
.set( dialect )
.set( connectionProvider )
.set( transactionProvider )
.set( settings );
DSLContext dsl = DSL.using( configuration );
Field<String> identityColumn = makeField( "_identity", String.class );
Name tableName = DSL.name( schema.getName(), "TESTTABLE" );
dsl.transaction( t -> {
dsl.createSchema( "POLYGENE" ).execute();
} );
dsl.transaction( t -> {
dsl.createTableIfNotExists( tableName )
.column( identityColumn )
.execute();
});
}
private DataSource dbcpDataSource( String host, int port )
throws Exception
{
BasicDataSource pool = new BasicDataSource();
String driverClass = "org.postgresql.Driver";
Class.forName( driverClass );
pool.setDriverClassName( driverClass );
pool.setUrl( "jdbc:postgresql://" + host + ":" + port + "/jdbc_test_db" );
pool.setUsername( System.getProperty( "user.name" ) );
pool.setPassword( "ThisIsGreat!" );
pool.setDefaultAutoCommit( false );
return pool;
}
private DataSource rawDataSource( String host, int port )
throws Exception
{
PGSimpleDataSource datasource = new PGSimpleDataSource();
datasource.setUser( System.getProperty( "user.name" ) );
datasource.setPassword( "ThisIsGreat!" );
datasource.setAutosave( AutoSave.NEVER );
datasource.setUrl( "jdbc:postgresql://" + host + ":" + port + "/jdbc_test_db" );
return datasource;
}
static class PolygeneTransactionProvider
implements TransactionProvider
{
private TransactionProvider delegate;
private PolygeneTransactionProvider( TransactionProvider delegate )
{
this.delegate = delegate;
}
@Override
public void begin( TransactionContext ctx )
throws DataAccessException
{
System.out.println( "\"------------------------------> begin( " + ctx + " )" );
delegate.begin( ctx );
}
@Override
public void commit( TransactionContext ctx )
throws DataAccessException
{
System.out.println( "------------------------------> commit( " + ctx + " )" );
delegate.commit( ctx );
}
@Override
public void rollback( TransactionContext ctx )
throws DataAccessException
{
System.out.println( "\"------------------------------> rollback( " + ctx + " )" );
delegate.rollback( ctx );
}
}
}
--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
dsl.transaction( t -> {
if( dsl.isSchemaCapable()
&& dsl.meta().getSchemas().stream().noneMatch( s -> schema.getName().equalsIgnoreCase( s.getName() ) ) )
{
dsl.createSchema( schemaName ).execute();
}
} );
@Mixins( JooqDslContext.Mixin.class )
public interface JooqDslContext extends DSLContext
{
boolean isSchemaCapable();
Name tableNameOf( String tableName );
Table<Record> tableOf( String tableName );
class Mixin
implements InvocationHandler
{
private final Schema schema;
private final DSLContext dsl;
public Mixin( @Service DataSource dataSource, @Uses Settings settings, @Uses SQLDialect dialect, @Uses Schema schema )
{
this.schema = schema;
ConnectionProvider connectionProvider = new DataSourceConnectionProvider( dataSource );
TransactionProvider transactionProvider = new ThreadLocalTransactionProvider( connectionProvider, false );
Configuration configuration = new DefaultConfiguration()
.set( dialect )
.set( connectionProvider )
.set( transactionProvider )
.set( settings );
dsl = DSL.using( configuration );
}
@Override
public Object invoke( Object o, Method method, Object[] args )
throws Throwable
{
if( method.getName().equals( "tableOf" ) )
{
return DSL.table( tableNameOf( (String) args[ 0 ] ) );
}
if( method.getName().equals( "tableNameOf" ) )
{
return tableNameOf( (String) args[ 0 ] );
}
if( method.getName().equals( "isSchemaCapable" ) )
{
return isSchemaCapable();
}
return method.invoke( dsl, args ); // delegate all
}
private Name tableNameOf( String name )
{
return this.isSchemaCapable() ? DSL.name( schema.getName(), name ) : DSL.name( name );
}
private boolean isSchemaCapable()
{
return !dsl.dialect().equals( SQLDialect.SQLITE ) && !dsl.dialect().equals( SQLDialect.MYSQL );
}
}
}
// instantiation of the above;
SqlEntityStoreConfiguration config = this.configuration.get(); // our configuration
SQLDialect dialect = getSqlDialect( config ); // just convert String and deals with null value.
Settings settings = serviceDescriptor
.metaInfo( Settings.class ); // metaInfo is set 'onto' services during bootstrap (see below). // set to new Settings().withRenderNameStyle( RenderNameStyle.QUOTED )
String schemaName = config.schemaName().get();
String typesTableName = config.typesTableName().get();
String entitiesTableName = config.entitiesTableName().get();
Schema schema = DSL.schema( DSL.name( schemaName ) );// tbf is a factory for instantiation of transient (mutable/non-persistable) composites (objects) // the additional args are mapped to '@Uses" parameter in constructor dsl = tbf.newTransient( JooqDslContext.class, settings, dialect, schema );
--
You received this message because you are subscribed to a topic in the Google Groups "jOOQ User Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jooq-user/Ek-Pu3i2vKE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jooq-user+unsubscribe@googlegroups.com.
class Mixin
implements InvocationHandler
{
private final DSLContext dsl;
public Mixin( @Service javax.sql.DataSource dataSource, @Uses Settings settings, @Uses SQLDialect dialect )
{
ConnectionProvider connectionProvider = new DataSourceConnectionProvider( dataSource );
TransactionProvider transactionProvider = new ThreadLocalTransactionProvider( connectionProvider, false );
Configuration configuration = new DefaultConfiguration()
.set( dialect )
.set( connectionProvider )
.set( transactionProvider )
.set( settings );
dsl = DSL.using( configuration );
}
To unsubscribe from this group and stop receiving emails from it, send an email to jooq...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to a topic in the Google Groups "jOOQ User Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jooq-user/Ek-Pu3i2vKE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jooq...@googlegroups.com.
--Niclas Hedhman, Software Developer
http://polygene.apache.org - New Energy for Java
--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jooq-user/64aa66a9-fd71-4cbb-be25-90a0012a5572%40googlegroups.com.