Visitar URL original
Revert "Remove SQL Support Sample as it depends on indexing-sql" · apache/polygene-java@1cc2de9 · GitHub
Skip to content
This repository was archived by the owner on May 12, 2021. It is now read-only.

Commit 1cc2de9

Browse files
committed
Revert "Remove SQL Support Sample as it depends on indexing-sql"
This reverts commit c33ef71.
1 parent 42b3d33 commit 1cc2de9

13 files changed

Lines changed: 481 additions & 0 deletions

File tree

‎extensions/entitystore-sqlkv/src/docs/es-sqlkv.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ get <<library-circuitbreaker,Circuit Breaker>> and <<library-jmx, JMX>> integrat
3232

3333
The database schema is managed using <<library-sql-liquibase>>.
3434

35+
TIP: See the <<sample-sql-support>> that demonstrate combined use of <<library-sql>>, <<extension-es-sql>> and
36+
<<extension-indexing-sql>>.
37+
3538
The following SQL databases are supported:
3639

3740
- http://www.postgresql.org/[PostgreSQL]

‎extensions/indexing-sql/src/docs/index-sql.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ source=extensions/indexing-sql/dev-status.xml
2828
This extension fully leverage the <<library-sql>> meaning that you must use it to assemble your DataSource and that you
2929
get <<library-circuitbreaker,Circuit Breaker>> and <<library-jmx, JMX>> integration for free.
3030

31+
TIP: See the <<sample-sql-support>> that demonstrate combined use of <<library-sql>>, <<extension-es-sqlkv>> and
32+
<<extension-indexing-sql>>.
33+
3134
The following SQL databases are supported:
3235

3336
- http://www.postgresql.org/[PostgreSQL]

‎libraries/sql/src/docs/sql.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ The SQL Library provides facilities for working with SQL databases.
3131
The center piece is the DataSource support that comes with
3232
<<library-circuitbreaker>> and <<library-jmx>> support.
3333

34+
TIP: See the <<sample-sql-support>> that demonstrate combined use of <<library-sql>>, <<extension-es-sqlkv>> and
35+
<<extension-indexing-sql>>.
36+
3437
Moreover, supplementary libraries helps dealing with different connection pool implementations and schema migrations.
3538
None of theses libraries depends on an actual JDBC driver, you are free to use the one that suits your needs.
3639

‎manual/src/docs/website/samples.txt‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,37 @@ https://github.com/apache/polygene-java/tree/develop/samples/rental[Browse Sourc
6060
//
6161
// https://github.com/apache/polygene-java/tree/develop/samples/scala[Browse Source]
6262

63+
[[sample-sql-support,SQL Support Sample]]
64+
== SQL Support Sample ==
65+
66+
NOTE: This sample use PostgreSQL and drop all of its data once run in order to be runnable multiple times.
67+
68+
Sample of how to fully use Polygene™ SQL support : <<library-sql>>, <<extension-es-sqlkv>> and <<extension-indexing-sql>>.
69+
70+
https://github.com/apache/polygene-java/tree/develop/samples/sql-support[Browse Source]
71+
72+
Here are the steps needed to setup the database using the `psql` utility command:
73+
74+
[source,bash]
75+
----
76+
user@host $ psql postgres
77+
postgres=# CREATE USER jdbc_test_login WITH PASSWORD 'password';
78+
CREATE ROLE
79+
postgres=# CREATE DATABASE jdbc_test_db;
80+
CREATE DATABASE
81+
postgres=# GRANT ALL PRIVILEGES ON DATABASE jdbc_test_db TO jdbc_test_login;
82+
GRANT
83+
postgres=# \q
84+
user@host $ psql -d jdbc_test_db</strong>
85+
postgres=# CREATE EXTENSION ltree;
86+
CREATE EXTENSION
87+
postgres=# \q
88+
user@host $
89+
----
90+
91+
From the sources you can run the `runSqlSupportSample` Gradle task to run the example.
92+
See <<build-system>> if you need some guidance.
93+
6394
[[sample-swing,Swing Bindings Sample]]
6495
== Swing Bindings Sample ==
6596

‎samples/sql-support/build.gradle‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
*
19+
*/
20+
import org.apache.polygene.gradle.TaskGroups
21+
22+
apply plugin: 'polygene-sample'
23+
24+
description = "SQL Support Sample."
25+
26+
jar { manifest { name = "Apache Polygene™ Sample - SQL Support" } }
27+
28+
dependencies {
29+
30+
implementation polygene.core.bootstrap
31+
implementation polygene.library( 'sql-dbcp' )
32+
implementation polygene.extension( 'entitystore-preferences' )
33+
implementation polygene.extension( 'entitystore-sqlkv' )
34+
implementation polygene.extension( 'indexing-sql' )
35+
36+
runtimeOnly polygene.core.runtime
37+
runtimeOnly libraries.postgres
38+
runtimeOnly libraries.logback
39+
}
40+
41+
task( runSqlSupportSample, dependsOn: 'classes', type: JavaExec ) {
42+
group = TaskGroups.SAMPLES
43+
description = "Runs $project.name sample."
44+
main = 'org.apache.polygene.sample.sqlsupport.Main'
45+
classpath = sourceSets.main.runtimeClasspath
46+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
*
19+
*/
20+
package org.apache.polygene.sample.sqlsupport;
21+
22+
import org.apache.polygene.api.common.Visibility;
23+
import org.apache.polygene.bootstrap.ApplicationAssembler;
24+
import org.apache.polygene.bootstrap.ApplicationAssembly;
25+
import org.apache.polygene.bootstrap.ApplicationAssemblyFactory;
26+
import org.apache.polygene.bootstrap.LayerAssembly;
27+
import org.apache.polygene.bootstrap.ModuleAssembly;
28+
import org.apache.polygene.entitystore.memory.MemoryEntityStoreService;
29+
import org.apache.polygene.entitystore.sql.assembly.PostgreSQLEntityStoreAssembler;
30+
import org.apache.polygene.index.sql.assembly.PostgreSQLIndexQueryAssembler;
31+
import org.apache.polygene.library.sql.assembly.DataSourceAssembler;
32+
import org.apache.polygene.library.sql.datasource.DataSources;
33+
import org.apache.polygene.library.sql.dbcp.DBCPDataSourceServiceAssembler;
34+
35+
/**
36+
* Assemble the Application.
37+
*
38+
* Use SQL EntityStore and SQL Index/Query for Persistence using PostgreSQL.
39+
* EntityStore and Index/Query use different DataSource in order to allow splitting the two in two servers/databases.
40+
*/
41+
public class AppAssembler
42+
implements ApplicationAssembler
43+
{
44+
45+
@Override
46+
public ApplicationAssembly assemble( ApplicationAssemblyFactory applicationFactory )
47+
{
48+
ApplicationAssembly appAss = applicationFactory.newApplicationAssembly();
49+
appAss.setName( "SQL Support Sample" );
50+
51+
// Config
52+
LayerAssembly configLayer = appAss.layer( "config" );
53+
ModuleAssembly configModule = configLayer.module( "config" );
54+
{
55+
configModule.services( MemoryEntityStoreService.class ).
56+
visibleIn( Visibility.module );
57+
// Use a PreferenceEntityStore instead if you want the configuration to be persistent
58+
// new PreferenceEntityStoreAssembler( Visibility.module ).assemble( configModule );
59+
}
60+
61+
// Infra
62+
LayerAssembly infraLayer = appAss.layer( "infra" );
63+
ModuleAssembly persistenceModule = infraLayer.module( "persistence" );
64+
{
65+
// SQL DataSource Service
66+
String dataSourceServiceIdentity = "postgresql-datasource-service";
67+
new DBCPDataSourceServiceAssembler().
68+
identifiedBy( dataSourceServiceIdentity ).
69+
visibleIn( Visibility.module ).
70+
withConfig( configModule, Visibility.application ).
71+
assemble( persistenceModule );
72+
73+
// SQL EntityStore DataSource and Service
74+
new DataSourceAssembler().
75+
withDataSourceServiceIdentity( dataSourceServiceIdentity ).
76+
identifiedBy( "postgresql-es-datasource" ).
77+
visibleIn( Visibility.module ).
78+
withCircuitBreaker( DataSources.newDataSourceCircuitBreaker() ).assemble( persistenceModule );
79+
new PostgreSQLEntityStoreAssembler().
80+
visibleIn( Visibility.application ).
81+
withConfig( configModule, Visibility.application ).
82+
assemble( persistenceModule );
83+
84+
// SQL Index/Query DataSource and Service
85+
new DataSourceAssembler().
86+
withDataSourceServiceIdentity( dataSourceServiceIdentity ).
87+
identifiedBy( "postgresql-index-datasource" ).
88+
visibleIn( Visibility.module ).
89+
withCircuitBreaker().
90+
assemble( persistenceModule );
91+
new PostgreSQLIndexQueryAssembler().
92+
visibleIn( Visibility.application ).
93+
withConfig( configModule, Visibility.application ).
94+
assemble( persistenceModule );
95+
}
96+
97+
// App
98+
LayerAssembly appLayer = appAss.layer( "app" );
99+
ModuleAssembly domainModule = appLayer.module( "domain" );
100+
{
101+
domainModule.entities( PretextEntity.class );
102+
}
103+
104+
// Uses
105+
infraLayer.uses( configLayer );
106+
appLayer.uses( infraLayer );
107+
108+
return appAss;
109+
}
110+
111+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
*
19+
*/
20+
package org.apache.polygene.sample.sqlsupport;
21+
22+
import java.sql.Connection;
23+
import java.sql.SQLException;
24+
import java.sql.Statement;
25+
import javax.sql.DataSource;
26+
import org.apache.polygene.api.entity.EntityBuilder;
27+
import org.apache.polygene.api.query.Query;
28+
import org.apache.polygene.api.query.QueryBuilder;
29+
import org.apache.polygene.api.structure.Application;
30+
import org.apache.polygene.api.structure.Module;
31+
import org.apache.polygene.api.unitofwork.UnitOfWork;
32+
import org.apache.polygene.bootstrap.Energy4Java;
33+
import org.apache.polygene.entitystore.sql.assembly.PostgreSQLEntityStoreAssembler;
34+
import org.apache.polygene.index.sql.assembly.PostgreSQLIndexQueryAssembler;
35+
import org.apache.polygene.library.sql.common.SQLConfiguration;
36+
import org.apache.polygene.library.sql.common.SQLUtil;
37+
38+
import static org.apache.polygene.api.query.QueryExpressions.eq;
39+
import static org.apache.polygene.api.query.QueryExpressions.templateFor;
40+
41+
/**
42+
* SQL Support Sample Main Class.
43+
* <p><strong>psql postgres</strong></p>
44+
* <p>CREATE USER jdbc_test_login WITH PASSWORD 'password';</p>
45+
* <p>CREATE DATABASE jdbc_test_db;</p>
46+
* <p>GRANT ALL PRIVILEGES ON DATABASE jdbc_test_db TO jdbc_test_login;</p>
47+
* <p><strong>psql -d jdbc_test_db</strong></p>
48+
* <p>CREATE EXTENSION ltree;</p>
49+
*/
50+
public class Main
51+
{
52+
53+
public static void main( String[] args )
54+
throws Exception
55+
{
56+
final Application application = new Energy4Java().newApplication( new AppAssembler() );
57+
application.activate();
58+
Runtime.getRuntime().addShutdownHook( new Thread( new Runnable()
59+
{
60+
@Override
61+
@SuppressWarnings( "CallToThreadDumpStack" )
62+
public void run()
63+
{
64+
try {
65+
application.passivate();
66+
} catch ( Exception ex ) {
67+
System.err.println( "Unable to passivate Polygene application!" );
68+
ex.printStackTrace();
69+
}
70+
}
71+
72+
} ) );
73+
Module domainModule = application.findModule( "app", "domain" );
74+
75+
int exitStatus = 0;
76+
77+
try {
78+
79+
UnitOfWork uow = domainModule.unitOfWorkFactory().newUnitOfWork();
80+
EntityBuilder<PretextEntity> builder = uow.newEntityBuilder( PretextEntity.class );
81+
PretextEntity pretext = builder.instance();
82+
pretext.reason().set( "Testing purpose" );
83+
builder.newInstance();
84+
uow.complete();
85+
86+
uow = domainModule.unitOfWorkFactory().newUnitOfWork();
87+
QueryBuilder<PretextEntity> queryBuilder = domainModule.newQueryBuilder( PretextEntity.class );
88+
queryBuilder = queryBuilder.where( eq( templateFor( PretextEntity.class ).reason(), "Testing purpose" ) );
89+
Query<PretextEntity> query = uow.newQuery( queryBuilder );
90+
pretext = query.find();
91+
if ( pretext == null ) {
92+
System.err.println( "ERROR: Unable to find pretext!" );
93+
exitStatus = -1;
94+
} else {
95+
System.out.println( "SUCCESS: Found Pretext with reason: " + pretext.reason().get() );
96+
}
97+
uow.discard();
98+
99+
} finally {
100+
deleteData( application.findModule( "infra", "persistence" ) );
101+
}
102+
103+
System.exit( exitStatus );
104+
}
105+
106+
/**
107+
* Completely delete data so the sample can be run multiple times.
108+
*/
109+
private static void deleteData( Module persistenceModule )
110+
throws SQLException
111+
{
112+
// EntityStore Data
113+
{
114+
UnitOfWork uow = persistenceModule.unitOfWorkFactory().newUnitOfWork();
115+
try {
116+
SQLConfiguration config = uow.get( SQLConfiguration.class, PostgreSQLEntityStoreAssembler.DEFAULT_ENTITYSTORE_IDENTITY );
117+
Connection connection = persistenceModule.findService( DataSource.class ).get().getConnection();
118+
connection.setAutoCommit( false );
119+
connection.setReadOnly( false );
120+
String schemaName = config.schemaName().get();
121+
122+
Statement stmt = null;
123+
try {
124+
stmt = connection.createStatement();
125+
stmt.execute( "DROP SCHEMA " + schemaName + " CASCADE" );
126+
connection.commit();
127+
} finally {
128+
SQLUtil.closeQuietly( stmt );
129+
}
130+
} finally {
131+
uow.discard();
132+
}
133+
}
134+
135+
// Indexing Data
136+
{
137+
UnitOfWork uow = persistenceModule.unitOfWorkFactory().newUnitOfWork();
138+
try {
139+
SQLConfiguration config = uow.get( SQLConfiguration.class, PostgreSQLIndexQueryAssembler.DEFAULT_IDENTITY );
140+
Connection connection = persistenceModule.findService( DataSource.class ).get().getConnection();
141+
connection.setAutoCommit( false );
142+
connection.setReadOnly( false );
143+
String schemaName = config.schemaName().get();
144+
145+
Statement stmt = null;
146+
try {
147+
stmt = connection.createStatement();
148+
stmt.execute( "DROP SCHEMA " + schemaName + " CASCADE" );
149+
connection.commit();
150+
} finally {
151+
SQLUtil.closeQuietly( stmt );
152+
SQLUtil.closeQuietly( connection );
153+
}
154+
} finally {
155+
uow.discard();
156+
}
157+
}
158+
}
159+
160+
}

0 commit comments

Comments
 (0)