Skip to content

Commit

Permalink
Commiting local changes before pull
Browse files Browse the repository at this point in the history
  • Loading branch information
Francismb committed May 7, 2016
1 parent e029e8c commit 0ff236c
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 47 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
![alt tag](https://www.dropbox.com/s/fip95qrdcg0yhm5/small-logo.png?dl=1)
[logo]: https://www.dropbox.com/s/fip95qrdcg0yhm5/small-logo.png?dl=1 "Logo"

[![Build Status](https://travis-ci.org/frazboyz/Active-ORM.svg?branch=master)](https://travis-ci.org/frazboyz/Active-ORM)

ActiveORM is a java active record implementation which support SQLite, Postgresql and MySQL.
ActiveORM, it is a object relational mapping which follows the active record pattern.

It supports the major databases(SQLite, PostgreSQL, MySQL and H2).
It also provides a query module so you can easily query for objects in an object orientated manner.
1 change: 1 addition & 0 deletions h2-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
connector: h2
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<artifactId>Jactive-Record</artifactId>
<packaging>jar</packaging>
<version>0.01</version>
<name>Jactive Record</name>
<build>
<plugins>
<plugin>
Expand All @@ -19,7 +20,6 @@
</plugin>
</plugins>
</build>
<name>Jactive Record</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
Expand All @@ -32,10 +32,16 @@
<artifactId>sqlite-jdbc</artifactId>
<version>3.7.2</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.191</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.17</version>
</dependency>

</dependencies>
</project>
File renamed without changes.
29 changes: 26 additions & 3 deletions src/org/activeorm/database/Database.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.activeorm.database;

import org.activeorm.database.configuration.DatabaseConfiguration;
import org.activeorm.database.configuration.H2DatabaseConfiguration;
import org.activeorm.database.configuration.SQLiteDatabaseConfiguration;
import org.activeorm.database.datahandler.*;
import org.activeorm.database.sql.SQLProducer;
Expand Down Expand Up @@ -94,6 +95,17 @@ protected Database(final DatabaseConfiguration configuration, final SQLProducer
connection = connect();
}

/**
* Sets the static database instance to the one provided.
*
* @param database the {@link Database} to set the instance to.
* @return The database passed in.
*/
public static Database fromInstance(final Database database) {
Database.instance = database;
return database;
}

/**
* Constructs a {@link Database} from YAML.
*
Expand Down Expand Up @@ -135,9 +147,17 @@ public static Database fromYaml(final File file) {
throw new RuntimeException("Configuration file did not contain a address for the database");
}

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

// Create the configuration and set the database instance
final SQLiteDatabaseConfiguration sqliteConfiguration = new SQLiteDatabaseConfiguration(address);
Database.instance = new SQLiteDatabase(sqliteConfiguration);
break;
case "h2":
case "h2database":
// Create the configuration and set the database instance
final SQLiteDatabaseConfiguration configuration = new SQLiteDatabaseConfiguration(map.get("address"));
Database.instance = new SQLiteDatabase(configuration);
Database.instance = new H2Database();
break;
case "mysql":
break;
Expand Down Expand Up @@ -229,7 +249,9 @@ public synchronized int execute(final String sql, final Object[] parameters) {
final PreparedStatement statement = prepare(sql, parameters, false);
if (statement != null) {
try {
return statement.executeUpdate();
final int result = statement.executeUpdate();
statement.close();
return result;
} catch (SQLException e) {
e.printStackTrace();
try {
Expand Down Expand Up @@ -261,6 +283,7 @@ public synchronized int execute(final String sql, final Object[] parameters, fin
if (keys.next()) {
primaryKey.setValue(keys.getInt(1));
}
statement.close();
return result;
} catch (SQLException e) {
e.printStackTrace();
Expand Down
35 changes: 35 additions & 0 deletions src/org/activeorm/database/H2Database.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.activeorm.database;

import org.activeorm.database.configuration.H2DatabaseConfiguration;
import org.activeorm.database.sql.DefaultSQLProducer;

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

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

/**
* Constructs a new {@link H2Database}.
*/
public H2Database() {
super(new H2DatabaseConfiguration(), new DefaultSQLProducer());
}

public Connection connect() {
try {
// Load the HSQLDB driver class
Class.forName("org.h2.Driver");
connection = DriverManager.getConnection("jdbc:h2:mem:database", "sa", "");
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
return connection;
}
}
4 changes: 2 additions & 2 deletions src/org/activeorm/database/SQLiteDatabase.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.activeorm.database;

import org.activeorm.database.configuration.SQLiteDatabaseConfiguration;
import org.activeorm.database.sql.SQLLiteSQLProducer;
import org.activeorm.database.sql.DefaultSQLProducer;

import java.sql.Connection;
import java.sql.DriverManager;
Expand All @@ -21,7 +21,7 @@ public class SQLiteDatabase extends Database {
* @param configuration the database configuration object.
*/
public SQLiteDatabase(final SQLiteDatabaseConfiguration configuration) {
super(configuration, new SQLLiteSQLProducer());
super(configuration, new DefaultSQLProducer());
}

public Connection connect() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.activeorm.database.configuration;

/**
* Created by Francis on 7/05/16.
* Project Active-ORM.
*
* A H2Database {@link DatabaseConfiguration} implementation.
*/
public class H2DatabaseConfiguration extends DatabaseConfiguration {

/**
* Constructs a new {@link H2DatabaseConfiguration}.
*/
public H2DatabaseConfiguration() {
super(null, null, null, null);
}

}

This file was deleted.

1 change: 1 addition & 0 deletions src/org/activeorm/database/datahandler/TimeHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ public void set(final int index, final Time value, final PreparedStatement state
throw new DataHandleException(index, "Time");
}
}

}

10 changes: 0 additions & 10 deletions src/org/activeorm/database/sql/SQLLiteSQLProducer.java

This file was deleted.

24 changes: 20 additions & 4 deletions test/org/activeorm/database/DatabaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,34 @@
public class DatabaseTest {

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

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

@Test
public void testSQLiteRawQuery() {
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);
assertNotNull(database.query(database.sql.select("users", null, new String[]{"username"}, new String[]{"="}, null, null, false), new Object[]{"Jackson"}));
database.execute("DROP TABLE users", null);
database.disconnect();
}

@Test
public void testH2RawQuery() {
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);
assertNotNull(database.query(database.sql.select("users", null, new String[]{"username"}, new String[]{"="}, null, null, false), new Object[]{"Jackson"}));
database.execute("DROP TABLE users", null);
database.disconnect();
}
}
22 changes: 20 additions & 2 deletions test/org/activeorm/mapping/ActiveRecordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.activeorm.User;
import org.activeorm.database.Database;
import org.activeorm.query.Query;
import org.junit.Test;

import static junit.framework.Assert.assertEquals;
import static junit.framework.TestCase.assertTrue;

/**
Expand All @@ -13,8 +15,8 @@
public class ActiveRecordTest {

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

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

Expand All @@ -27,4 +29,20 @@ public void testSave() {

database.disconnect();
}

@Test
public void testH2Save() {
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);

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

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

database.disconnect();
}
}
2 changes: 1 addition & 1 deletion test/org/activeorm/query/QueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class QueryTest {

@Test
public void testQuery() {
final Database database = Database.fromYaml("config.yml");
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 tom = new User();
Expand Down

0 comments on commit 0ff236c

Please sign in to comment.