Skip to content

Commit f304200

Browse files
committed
Added example project, for now, only with posix scripts.
1 parent 3236851 commit f304200

File tree

10 files changed

+558
-0
lines changed

10 files changed

+558
-0
lines changed

examples/fbjava-example/README

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
=================================
2+
Example project for FB/Java
3+
=================================
4+
5+
Prerequisites
6+
=============
7+
8+
Java SE Development Kit 8 (http://java.sun.com/javase/downloads/index.jsp) [may work with JRE]
9+
Apache Maven (http://maven.apache.org)
10+
11+
12+
Preparing
13+
=========
14+
15+
Make sure JAVA_HOME is correctly defined and points to a Java of the same architecture (32 or 64
16+
bit) of Firebird.
17+
18+
Set FIREBIRD environment variable.
19+
20+
Set FBJAVA_ROOT environment variable.
21+
22+
Set ISC_USER and ISC_PASSWORD.
23+
24+
Go to this example's root directory.
25+
26+
Run mvn package
27+
(If you're new to Maven, it's normal that it will start downloading a lot of files on the first run)
28+
29+
Run util/createdb.{sh,bat} script.
30+
31+
Optionally create a database named employee in PostgreSQL with user name postgres and password
32+
postgres.
33+
34+
Open the postgres employee database, create this table and insert some records.
35+
create table employee (id integer, name varchar(60));
36+
37+
38+
Testing
39+
=======
40+
41+
Connect to db.fdb. Some things to play with:
42+
43+
-- Lets see what Java says about our OS name, directly calling a standard method:
44+
select get_system_property('os.name') from rdb$database;
45+
46+
-- Lets call a method that we created:
47+
select regex_replace('foo', '123 foo 456', 'bar') from rdb$database;
48+
49+
-- INSERT/UPDATE/DELETE on the employee table. All actions will be logged in db.log file.
50+
insert into employee values (1, 'Juliet Burke');
51+
insert into employee values (2, 'Kate Austen');
52+
insert into employee values (3, 'John Locke');
53+
54+
-- This is dumb example, but you may use same concept to make cross database queries.
55+
select * from employee_local;
56+
57+
-- This is same concept and implementation, but read data from the postgres database!
58+
select * from employee_pgsql;
59+
60+
-- You can now open util/code.sql and see how the postgres query is created. Create another SP based
61+
-- on a Firebird database just replacing jdbc:postgresql:employee|postgres|postgres by
62+
-- jdbc:firebirdsql:database|user|password
63+
64+
-- After change .java files, run mvn package and util/replace-jar.{sh,bat} script.
65+
66+
-- Now you can do queries with multiple external datasources, of different DBMSs!
67+
68+
select * from employee_local
69+
union all
70+
select * from employee_pgsql;
71+
72+
select *
73+
from employee_local el
74+
join employee_pgsql ep
75+
on ep.id = el.id;
76+
77+
insert into employee
78+
select * from employee_pgsql;

examples/fbjava-example/pom.xml

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
3+
>
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>org.firebirdsql.fbjava</groupId>
6+
<artifactId>fbjava-example</artifactId>
7+
<packaging>jar</packaging>
8+
<version>1.0.0-alpha-1</version>
9+
<name>fbjava-example</name>
10+
<url></url>
11+
12+
<properties>
13+
<fbjava.version>1.0.0-alpha-1</fbjava.version>
14+
<slf4j.version>1.7.13</slf4j.version>
15+
<logback.version>1.1.3</logback.version>
16+
<maven-assembly-plugin.version>2.2-beta-3</maven-assembly-plugin.version>
17+
<postgresql.version>8.3-603.jdbc4</postgresql.version>
18+
</properties>
19+
20+
<repositories>
21+
<repository>
22+
<id>fbjava</id>
23+
<url>https://raw.githubusercontent.com/asfernandes/fbjava-maven/master/maven2</url>
24+
</repository>
25+
</repositories>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.firebirdsql.fbjava</groupId>
30+
<artifactId>fbjava</artifactId>
31+
<version>${fbjava.version}</version>
32+
<scope>provided</scope>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>org.slf4j</groupId>
37+
<artifactId>slf4j-api</artifactId>
38+
<version>${slf4j.version}</version>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>ch.qos.logback</groupId>
43+
<artifactId>logback-classic</artifactId>
44+
<version>${logback.version}</version>
45+
</dependency>
46+
47+
<dependency>
48+
<groupId>postgresql</groupId>
49+
<artifactId>postgresql</artifactId>
50+
<version>${postgresql.version}</version>
51+
</dependency>
52+
</dependencies>
53+
54+
<build>
55+
<resources>
56+
<resource>
57+
<directory>src/main/resources</directory>
58+
<filtering>true</filtering>
59+
</resource>
60+
</resources>
61+
62+
<plugins>
63+
<plugin>
64+
<groupId>org.apache.maven.plugins</groupId>
65+
<artifactId>maven-compiler-plugin</artifactId>
66+
<configuration>
67+
<source>1.8</source>
68+
<target>1.8</target>
69+
</configuration>
70+
</plugin>
71+
72+
<plugin>
73+
<groupId>org.apache.maven.plugins</groupId>
74+
<artifactId>maven-dependency-plugin</artifactId>
75+
<executions>
76+
<execution>
77+
<id>copy-dependencies</id>
78+
<phase>package</phase>
79+
<goals>
80+
<goal>copy-dependencies</goal>
81+
</goals>
82+
<configuration>
83+
<excludeScope>provided</excludeScope>
84+
</configuration>
85+
</execution>
86+
</executions>
87+
</plugin>
88+
</plugins>
89+
</build>
90+
</project>
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* FB/Java plugin
3+
*
4+
* Distributable under LGPL license.
5+
* You may obtain a copy of the License at http://www.gnu.org/copyleft/lgpl.html
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* LGPL License for more details.
11+
*
12+
* This file was created by members of the Firebird development team.
13+
* All individual contributions remain the Copyright (C) of those
14+
* individuals. Contributors to this file are either listed here or
15+
* can be obtained from a git log command.
16+
*
17+
* All rights reserved.
18+
*/
19+
package org.firebirdsql.fbjava.examples.fbjava_example;
20+
21+
import java.sql.Connection;
22+
import java.sql.DriverManager;
23+
import java.sql.PreparedStatement;
24+
import java.sql.ResultSet;
25+
import java.sql.SQLException;
26+
import java.util.StringTokenizer;
27+
28+
import org.firebirdsql.fbjava.ExternalResultSet;
29+
import org.firebirdsql.fbjava.ProcedureContext;
30+
import org.firebirdsql.fbjava.Values;
31+
import org.firebirdsql.fbjava.ValuesMetadata;
32+
import org.slf4j.Logger;
33+
import org.slf4j.LoggerFactory;
34+
35+
36+
/**
37+
* @author <a href="mailto:adrianosf@gmail.com">Adriano dos Santos Fernandes</a>
38+
*/
39+
public class FbJdbc
40+
{
41+
private static final Logger log = LoggerFactory.getLogger(FbJdbc.class);
42+
43+
static
44+
{
45+
try
46+
{
47+
Class.forName("org.postgresql.Driver");
48+
}
49+
catch (ClassNotFoundException e)
50+
{
51+
log.warn("Cannot load org.postgresql.Driver", e);
52+
}
53+
}
54+
55+
public static ExternalResultSet executeQuery() throws Exception
56+
{
57+
final ProcedureContext context = ProcedureContext.get();
58+
return new ExternalResultSet() {
59+
ValuesMetadata outMetadata = context.getOutputMetadata();
60+
Values outValues = context.getOutputValues();
61+
int count = outMetadata.getParameterCount();
62+
Connection conn;
63+
PreparedStatement stmt;
64+
ResultSet rs;
65+
66+
{
67+
StringTokenizer tokenizer = new StringTokenizer(context.getNameInfo(), "|");
68+
69+
String uri = tokenizer.nextToken();
70+
String user = tokenizer.hasMoreTokens() ? tokenizer.nextToken() : null;
71+
String password = tokenizer.hasMoreTokens() ? tokenizer.nextToken() : null;
72+
73+
conn = DriverManager.getConnection(uri, user, password);
74+
try
75+
{
76+
StringBuilder sb = new StringBuilder("select ");
77+
78+
for (int i = 0; i < count; ++i)
79+
{
80+
if (i != 0)
81+
sb.append(", ");
82+
sb.append("x." + outMetadata.getName(i + 1));
83+
}
84+
85+
sb.append(" from (" + context.getBody() + ") x");
86+
87+
stmt = conn.prepareStatement(sb.toString());
88+
rs = stmt.executeQuery();
89+
}
90+
catch (Exception e)
91+
{
92+
close();
93+
throw e;
94+
}
95+
}
96+
97+
@Override
98+
public void close()
99+
{
100+
try
101+
{
102+
if (rs != null)
103+
rs.close();
104+
105+
if (stmt != null)
106+
stmt.close();
107+
108+
if (conn != null)
109+
conn.close();
110+
}
111+
catch (SQLException e)
112+
{
113+
log.error("Error closing the ExternalResultSet", e);
114+
}
115+
}
116+
117+
@Override
118+
public boolean fetch() throws Exception
119+
{
120+
if (!rs.next())
121+
return false;
122+
123+
for (int i = 0; i < count; ++i)
124+
{
125+
Object obj = rs.getObject(i + 1);
126+
127+
if (obj instanceof Number)
128+
obj = rs.getBigDecimal(i + 1);
129+
130+
outValues.setObject(i + 1, obj);
131+
}
132+
133+
return true;
134+
}
135+
};
136+
}
137+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* FB/Java plugin
3+
*
4+
* Distributable under LGPL license.
5+
* You may obtain a copy of the License at http://www.gnu.org/copyleft/lgpl.html
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* LGPL License for more details.
11+
*
12+
* This file was created by members of the Firebird development team.
13+
* All individual contributions remain the Copyright (C) of those
14+
* individuals. Contributors to this file are either listed here or
15+
* can be obtained from a git log command.
16+
*
17+
* All rights reserved.
18+
*/
19+
package org.firebirdsql.fbjava.examples.fbjava_example;
20+
21+
import java.sql.SQLException;
22+
23+
import org.firebirdsql.fbjava.TriggerContext;
24+
import org.firebirdsql.fbjava.Values;
25+
import org.firebirdsql.fbjava.ValuesMetadata;
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
29+
30+
/**
31+
* @author <a href="mailto:adrianosf@gmail.com">Adriano dos Santos Fernandes</a>
32+
*/
33+
public class FbLogger
34+
{
35+
private static final Logger log = LoggerFactory.getLogger(FbLogger.class);
36+
private static final String NEWLINE = System.getProperty("line.separator");
37+
38+
public static void info() throws SQLException
39+
{
40+
TriggerContext context = TriggerContext.get();
41+
42+
String msg = "Table: " + context.getTableName() +
43+
"; Type: " + context.getType() +
44+
"; Action: " + context.getAction() +
45+
valuesToStr(context.getFieldsMetadata(), context.getOldValues(), NEWLINE + "OLD:" + NEWLINE) +
46+
valuesToStr(context.getFieldsMetadata(), context.getNewValues(), NEWLINE + "NEW:" + NEWLINE);
47+
48+
log.info(msg);
49+
}
50+
51+
private static String valuesToStr(ValuesMetadata metadata, Values values, String label) throws SQLException
52+
{
53+
if (values == null)
54+
return "";
55+
56+
StringBuilder sb = new StringBuilder(label);
57+
58+
for (int i = 1, count = metadata.getParameterCount(); i <= count; ++i)
59+
sb.append(metadata.getName(i) + ": " + values.getObject(i) + NEWLINE);
60+
61+
return sb.toString();
62+
}
63+
}

0 commit comments

Comments
 (0)