Skip to content

Commit

Permalink
Implemented PostgreSQL support
Browse files Browse the repository at this point in the history
  • Loading branch information
Francismb committed May 8, 2016
1 parent 0f7b2f2 commit 5247c07
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 21 deletions.
6 changes: 5 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@
<artifactId>h2</artifactId>
<version>1.4.191</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1208</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.17</version>
</dependency>

</dependencies>
</project>
3 changes: 3 additions & 0 deletions postgre-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
connector: postgre
name: testing
username: development
24 changes: 20 additions & 4 deletions src/org/activeorm/database/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.activeorm.database.configuration.DatabaseConfiguration;
import org.activeorm.database.configuration.H2DatabaseConfiguration;
import org.activeorm.database.configuration.PostgreSQLDatabaseConfiguration;
import org.activeorm.database.configuration.SQLiteDatabaseConfiguration;
import org.activeorm.database.datahandler.BooleanHandler;
import org.activeorm.database.datahandler.ByteHandler;
Expand Down Expand Up @@ -193,20 +194,35 @@ public static Database fromYaml(final File file) {
}

// Get the data
final String address = map.get("address");
final String sqlliteAddress = map.get("address");

// Create the configuration and set the database instance
final SQLiteDatabaseConfiguration sqliteConfiguration = new SQLiteDatabaseConfiguration(address);
final SQLiteDatabaseConfiguration sqliteConfiguration = new SQLiteDatabaseConfiguration(sqlliteAddress);
Database.instance = new SQLiteDatabase(sqliteConfiguration);
break;
case "h2":
case "h2database":
// Create the configuration and set the database instance
Database.instance = new H2Database();
break;
case "mysql":
break;
case "postgre":
case "postgresql":
// If the map does not contain a location of the database then throw an exception
if (!map.containsKey("name")) {
throw new RuntimeException("Configuration file did not contain a name for the database");
}

// Get the data
final String postgreName = map.get("name");
final String postgreUsername = map.containsKey("username") ? map.get("username") : "";
final String postgrePassword = map.containsKey("password") ? map.get("password") : "";
final String postgreAddress = map.containsKey("address") ? map.get("address") : "localhost";

// Create the configuration and set the database instance
final PostgreSQLDatabaseConfiguration postgresConfiguration = new PostgreSQLDatabaseConfiguration(postgreName, postgreAddress, postgreUsername, postgrePassword);
Database.instance = new PostgreSQLDatabase(postgresConfiguration);
break;
case "mysql":
break;
}

Expand Down
34 changes: 34 additions & 0 deletions src/org/activeorm/database/PostgreSQLDatabase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.activeorm.database;

import org.activeorm.database.configuration.PostgreSQLDatabaseConfiguration;
import org.activeorm.database.sql.DefaultSQLProducer;
import org.activeorm.database.sql.PostgreSQLProducer;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
* Created by Francis on 8/05/16.
* Project Active-ORM.
*
* A PostgreSQL {@link Database} implementation.
*/
public class PostgreSQLDatabase extends Database {

/**
* Constructs a new {@link PostgreSQLDatabase}.
*/
public PostgreSQLDatabase(final PostgreSQLDatabaseConfiguration configuration) {
super(configuration, new PostgreSQLProducer());
}

public Connection connect() {
try {
connection = DriverManager.getConnection("jdbc:postgresql://" + configuration.address + "/" + configuration.name, configuration.username, configuration.password);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.activeorm.database.configuration;

/**
* Created by Francis on 8/05/16.
* Project Active-ORM.
*/
public class PostgreSQLDatabaseConfiguration extends DatabaseConfiguration {

/**
* Constructs a new {@link DatabaseConfiguration}
*
* @param name the name of the database
* @param address the address of the database
* @param username the username to connect to the database
* @param password the password to connect to the database
*/
public PostgreSQLDatabaseConfiguration(final String name, final String address, final String username, final String password) {
super(name, address, username, password);
}
}
20 changes: 11 additions & 9 deletions src/org/activeorm/database/sql/DefaultSQLProducer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*/
public class DefaultSQLProducer implements SQLProducer {

protected String delimiter = "`";

public String select(final String table, final String[] columns, final String[] whereColumns, final String[] whereOperators, final String[] orderColumns, final String[] orderOperators, final boolean limit) {
// Throw an exception if there is not table specified
if (table == null) {
Expand Down Expand Up @@ -37,7 +39,7 @@ public String select(final String table, final String[] columns, final String[]
final String column = columns[i];

// Add the condition to the sql
sql.append('`').append(column).append('`');
sql.append(delimiter).append(column).append(delimiter);

// If there is another condition after this
// one add AND sql
Expand All @@ -52,7 +54,7 @@ public String select(final String table, final String[] columns, final String[]
}

// Add the from table sql
sql.append("FROM ").append('`').append(table).append('`').append(" ");
sql.append("FROM ").append(delimiter).append(table).append(delimiter).append(" ");

// Add the where sql to the statement
sql.append(where(whereColumns, whereOperators));
Expand Down Expand Up @@ -84,15 +86,15 @@ public String insert(final String table, final String[] columns) {
final StringBuilder sql = new StringBuilder();

// Append initial INSERT INTO table sql
sql.append("INSERT INTO ").append("`").append(table).append("` ");
sql.append("INSERT INTO ").append(delimiter).append(table).append(delimiter).append(" ");

// add the columns to the sql
sql.append('(');
for (int i = 0; i < columns.length; i++) {
final String column = columns[i];

// Add the condition to the sql
sql.append('`').append(column).append('`');
sql.append(delimiter).append(column).append(delimiter);

// If there is another column after this
// one then add ', ' to the sql
Expand Down Expand Up @@ -138,15 +140,15 @@ public String update(final String table, final String[] columns, final String[]
sql.append("UPDATE ");

// Append table name
sql.append('`').append(table).append("` ");
sql.append(delimiter).append(table).append(delimiter).append(" ");

// Add the columns to the sql
sql.append("SET ");
for (int i = 0; i < columns.length; i++) {
final String column = columns[i];

// Add the condition to the sql
sql.append('`').append(column).append('`').append(" = ?");
sql.append(delimiter).append(column).append(delimiter).append(" = ?");

// If there is another column after this
// one then add ', ' to the sql
Expand Down Expand Up @@ -177,7 +179,7 @@ public String delete(final String table, final String[] whereColumns, final Stri
sql.append("DELETE FROM ");

// Append table name
sql.append('`').append(table).append('`').append(" ");
sql.append(delimiter).append(table).append(delimiter).append(" ");

// Append where sql
sql.append(where(whereColumns, whereOperators));
Expand Down Expand Up @@ -207,7 +209,7 @@ public String where(final String[] columns, final String[] operators) {

// Add the condition to the sql

sql.append('`').append(column).append('`').append(" ").append(operator).append(" ? ");
sql.append(delimiter).append(column).append(delimiter).append(" ").append(operator).append(" ? ");

// If there is another condition after this
// one add AND sql
Expand Down Expand Up @@ -240,7 +242,7 @@ public String order(final String[] columns, final String[] operators) {
final String operator = operators[i];

// Add the condition to the sql
sql.append('`').append(column).append('`').append(" ").append(operator);
sql.append(delimiter).append(column).append(delimiter).append(" ").append(operator);

// If there is another order after this
// one add a ', ' to the sql
Expand Down
12 changes: 12 additions & 0 deletions src/org/activeorm/database/sql/PostgreSQLProducer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.activeorm.database.sql;

/**
* Created by Francis on 8/05/16.
* Project Active-ORM.
*/
public class PostgreSQLProducer extends DefaultSQLProducer {

public PostgreSQLProducer() {
delimiter = "\"";
}
}
2 changes: 1 addition & 1 deletion src/org/activeorm/query/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public T first() {

try {
// Iterate through the result set
if (resultSet.next()) {
if (resultSet != null && resultSet.next()) {
// Create a new object instance
final T result = activeRecord.newInstance();

Expand Down
22 changes: 20 additions & 2 deletions test/org/activeorm/database/DatabaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ public void loadHSQLDatabase() {
assertNotNull(database);
}

@Test
public void loadPostgreSQLDatabase() {
final Database database = Database.fromYaml("./postgre-config.yml");
assertNotNull(database);
}

@Test
public void testSQLiteRawQuery() throws SQLException {
final Database database = Database.fromYaml("./sqllite-config.yml");
database.execute("CREATE TABLE users(user_id INTEGER PRIMARY KEY, username TEXT, password TEXT)", null);
assertEquals(1, database.execute(database.sql.insert("users", new String[]{"username", "password"}), new Object[]{"Jackson", "super secret password"}), 1);
assertEquals(1, database.execute(database.sql.insert("users", new String[]{"username", "password"}), new Object[]{"Jackson", "super secret password"}));
final ResultSet rs = database.query(database.sql.select("users", null, new String[]{"username"}, new String[]{"="}, null, null, false), new Object[]{"Jackson"});
assertNotNull(rs);
rs.close();
Expand All @@ -42,7 +48,19 @@ public void testSQLiteRawQuery() throws SQLException {
public void testH2RawQuery() throws SQLException {
final Database database = Database.fromYaml("./h2-config.yml");
database.execute("CREATE MEMORY TABLE users(user_id INT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(50), password VARCHAR(50))", null);
assertEquals(1, database.execute(database.sql.insert("users", new String[]{"username", "password"}), new Object[]{"Jackson", "super secret password"}), 1);
assertEquals(1, database.execute(database.sql.insert("users", new String[]{"username", "password"}), new Object[]{"Jackson", "super secret password"}));
final ResultSet rs = database.query(database.sql.select("users", null, new String[]{"username"}, new String[]{"="}, null, null, false), new Object[]{"Jackson"});
assertNotNull(rs);
rs.close();
database.execute("DROP TABLE users", null);
database.disconnect();
}

@Test
public void testPostgreSQLRawQuery() throws SQLException {
final Database database = Database.fromYaml("./postgre-config.yml");
database.execute("CREATE TABLE users(id serial PRIMARY KEY, username VARCHAR(50), password VARCHAR(50))", null);
assertEquals(1, database.execute(database.sql.insert("users", new String[]{"username", "password"}), new Object[]{"Jackson", "super secret password"}));
final ResultSet rs = database.query(database.sql.select("users", null, new String[]{"username"}, new String[]{"="}, null, null, false), new Object[]{"Jackson"});
assertNotNull(rs);
rs.close();
Expand Down
61 changes: 58 additions & 3 deletions test/org/activeorm/mapping/ActiveRecordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import org.activeorm.User;
import org.activeorm.database.Database;
import org.activeorm.query.Query;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

import static junit.framework.Assert.assertEquals;
import static junit.framework.TestCase.assertTrue;
Expand All @@ -12,17 +14,27 @@
* Created by Francis on 3/05/16.
* Project Jactive-Record.
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ActiveRecordTest {

@Test
public void testSQLiteSave() {
public void testASQLiteSave() {
final Database database = Database.fromYaml("./sqllite-config.yml");

database.execute("CREATE TABLE users(user_id INTEGER PRIMARY KEY, username TEXT, password TEXT)", null);

final User user = new User();
user.name = "tom";
assertTrue(user.save());

database.disconnect();
}

@Test
public void testBSQLiteDestroy() {
final Database database = Database.fromYaml("./sqllite-config.yml");

final User user = Query.build(User.class).where("username").equalTo("tom").first();
assertTrue(user.destroy());

database.execute("DROP TABLE users", null);
Expand All @@ -31,14 +43,57 @@ public void testSQLiteSave() {
}

@Test
public void testH2Save() {
public void testCH2Save() {
final Database database = Database.fromYaml("./h2-config.yml");

database.execute("CREATE MEMORY TABLE users(user_id INT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(50), password VARCHAR(50))", null);
database.execute("CREATE TABLE users(user_id INT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(50), password VARCHAR(50))", null);

User user = new User();
user.name = "tom";

assertTrue(user.save());

database.disconnect();
}

@Test
public void testDH2Destroy() {
final Database database = Database.fromYaml("./h2-config.yml");

database.execute("CREATE TABLE users(user_id INT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(50), password VARCHAR(50))", null);

User user = new User();
user.name = "tom";
user.save();
user = null;

user = Query.build(User.class).where("username").equalTo("tom").first();
assertTrue(user.destroy());

database.execute("DROP TABLE users", null);

database.disconnect();
}

@Test
public void testEPostgresSave() {
final Database database = Database.fromYaml("./postgre-config.yml");

database.execute("CREATE TABLE users(user_id serial PRIMARY KEY, username VARCHAR(50), password VARCHAR(50))", null);

User user = new User();
user.name = "tom";

assertTrue(user.save());

database.disconnect();
}

@Test
public void testFPostgresDestroy() {
final Database database = Database.fromYaml("./postgre-config.yml");

final User user = Query.build(User.class).where("username").equalTo("tom").first();
assertTrue(user.destroy());

database.execute("DROP TABLE users", null);
Expand Down
Loading

0 comments on commit 5247c07

Please sign in to comment.