Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ workbench.xmi
.settings
.checkstyle
twitter4j.properties
riskingh/twitter4j.properties
riskingh/src/main/resources/googleMaps.properties
7 changes: 4 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>ru.fizteh.fivt.students</groupId>
Expand All @@ -19,7 +19,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<modules>
<modules>
<module>egiby</module>
<module>akormushin</module>
<!--Add you module here-->
Expand All @@ -46,6 +46,7 @@
<module>ladyae</module>
<module>nikitarykov</module>
<module>duha666</module>
<module>riskingh</module>
</modules>

<dependencies>
Expand Down
56 changes: 56 additions & 0 deletions riskingh/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ru.fizteh.fivt.students</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>ru.fizteh.fivt.students</groupId>
<artifactId>riskingh</artifactId>
<version>1.0-SNAPSHOT</version>
<name>riskingh</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<version>[4.0,)</version>
</dependency>
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-stream</artifactId>
<version>4.0.4</version>
</dependency>
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.48</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20151123</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>14.0</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.190</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ru.fizteh.fivt.students.riskingh.MiniORM;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface Column {
String name() default "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
package ru.fizteh.fivt.students.riskingh.MiniORM;

import org.h2.jdbcx.JdbcConnectionPool;

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.*;

import static ru.fizteh.fivt.students.riskingh.MiniORM.NameResolver.convertCamelToUnderscore;
import static ru.fizteh.fivt.students.riskingh.MiniORM.NameResolver.isGood;

public class DatabaseService<T> implements Closeable {
private final String connectionName;
private final String username;
private final String password;

private Class<T> clazz;
private JdbcConnectionPool pool;
private String tableName;
private Field[] fields;
private int pkIndex = -1;

String getColumnName(Field f) {
String name = f.getAnnotation(Column.class).name();
if (name.equals("")) {
return convertCamelToUnderscore(f.getName());
}
return name;
}

void init() throws IllegalArgumentException, IOException {
if (!clazz.isAnnotationPresent(Table.class)) {
throw new IllegalArgumentException("no @Table annotation");
}

tableName = clazz.getAnnotation(Table.class).name();
if (tableName.equals("")) {
tableName = convertCamelToUnderscore(clazz.getSimpleName());
}

if (!isGood(tableName)) {
throw new IllegalArgumentException("Bad table name");
}

Set<String> names = new HashSet<>();
List<Field> fieldsList = new ArrayList<>();
for (Field f: clazz.getDeclaredFields()) {
if (f.isAnnotationPresent(Column.class)) {
String name = getColumnName(f);
names.add(name);
if (!isGood(name)) {
throw new IllegalArgumentException("Bad column name");
}

f.setAccessible(true);
fieldsList.add(f);
if (f.isAnnotationPresent(PrimaryKey.class)) {
if (pkIndex == -1) {
pkIndex = fieldsList.size() - 1;
} else {
throw new
IllegalArgumentException("Several @PrimaryKey");
}
}
} else if (f.isAnnotationPresent(PrimaryKey.class)) {
throw new
IllegalArgumentException("@PrimaryKey without @Column");
}
}

if (names.size() != fieldsList.size()) {
throw new IllegalArgumentException("Duplicate columns");
}

fields = new Field[fieldsList.size()];
fields = fieldsList.toArray(fields);

try {
Class.forName("org.h2.Driver");
} catch (ClassNotFoundException e) {
throw new IllegalStateException("No H2 driver found");
}

pool = JdbcConnectionPool.create(connectionName, username, password);
}

DatabaseService(Class<T> newClazz, String properties) throws IOException {
Properties credits = new Properties();
try (InputStream inputStream = this.getClass().getResourceAsStream(properties)) {
credits.load(inputStream);
}
connectionName = credits.getProperty("connection_name");
username = credits.getProperty("username");
password = credits.getProperty("password");

clazz = newClazz;
init();
}

DatabaseService(Class<T> newClazz) throws IOException {
this(newClazz, "/h2test.properties");
}

void createTable() throws SQLException {
StringBuilder queryBuilder = new StringBuilder();
queryBuilder.append("CREATE TABLE IF NOT EXISTS ").append(tableName).append("(");
for (int i = 0; i < fields.length; ++i) {
if (i != 0) {
queryBuilder.append(", ");
}
queryBuilder.append(getColumnName(fields[i])).append(" ")
.append(H2StringsResolver.resolve(fields[i].getType()));
if (i == pkIndex) {
queryBuilder.append(" PRIMARY KEY");
}
}
queryBuilder.append(")");
try (Connection conn = pool.getConnection()) {
conn.createStatement().execute(queryBuilder.toString());
}

}

void dropTable() throws SQLException {
StringBuilder queryBuilder = new StringBuilder();
queryBuilder.append("DROP TABLE IF EXISTS ").append(tableName);
try (Connection conn = pool.getConnection()) {
conn.createStatement().execute(queryBuilder.toString());
}
}

public void insert(T record) throws SQLException, IllegalAccessException {
StringBuilder queryBuilder = new StringBuilder();
queryBuilder.append("INSERT INTO ").append(tableName).append(" (");
for (int i = 0; i < fields.length; ++i) {
if (i != 0) {
queryBuilder.append(", ");
}
queryBuilder.append(getColumnName(fields[i])).append(" ");
}
queryBuilder.append(") VALUES (");
for (int i = 0; i < fields.length; ++i) {
if (i != 0) {
queryBuilder.append(", ");
}
queryBuilder.append("?");
}
queryBuilder.append(")");

try (Connection conn = pool.getConnection()) {
PreparedStatement statement = conn.prepareStatement(queryBuilder.toString());
for (int i = 0; i < fields.length; ++i) {
statement.setObject(i + 1, fields[i].get(record));
}
statement.execute();
}
}

public void delete(T record) throws IllegalArgumentException, IllegalAccessException, SQLException {
if (pkIndex == -1) {
throw new IllegalArgumentException("NO @PrimaryKey");
}
StringBuilder queryBuilder = new StringBuilder();
queryBuilder.append("DELETE FROM ").append(tableName).append(" WHERE ")
.append(fields[pkIndex].getName()).append(" = ?");
try (Connection conn = pool.getConnection()) {
PreparedStatement statement = conn.prepareStatement(queryBuilder.toString());
statement.setObject(1, fields[pkIndex].get(record));
statement.execute();
}
}

public void update(T record) throws IllegalArgumentException, SQLException, IllegalAccessException {
if (pkIndex == -1) {
throw new IllegalArgumentException("NO @PrimaryKey");
}
StringBuilder queryBuilder = new StringBuilder();
queryBuilder.append("UPDATE ").append(tableName).append(" SET ");
for (int i = 0; i < fields.length; ++i) {
if (i != 0) {
queryBuilder.append(", ");
}
queryBuilder.append(getColumnName(fields[i])).append(" = ?");
}
queryBuilder.append(" WHERE ").append(getColumnName(fields[pkIndex]))
.append(" = ?");

try (Connection conn = pool.getConnection()) {
PreparedStatement statement = conn.prepareStatement(queryBuilder.toString());
for (int i = 0; i < fields.length; ++i) {
statement.setObject(i + 1, fields[i].get(record));
}
statement.setObject(fields.length + 1, fields[pkIndex].get(record));
statement.execute();
}
}

public List<T> queryForAll() throws SQLException {
List<T> result = new ArrayList<>();

StringBuilder queryBuilder = new StringBuilder();
queryBuilder.append("SELECT * FROM ").append(tableName);

try (Connection conn = pool.getConnection()) {
try (ResultSet rs = conn.createStatement().executeQuery(queryBuilder.toString())) {
while (rs.next()) {
T record = clazz.newInstance();
for (int i = 0; i < fields.length; ++i) {
if (fields[i].getClass().isAssignableFrom(Number.class)) {
Long val = rs.getLong(i + 1);
fields[i].set(record, val);
} else if (fields[i].getType() != String.class) {
fields[i].set(record, rs.getObject(i + 1));
} else {
Clob data = rs.getClob(i + 1);
fields[i].set(record, data.getSubString(1, (int) data.length()));
}
}
result.add(record);
}
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalArgumentException("wrong class");
}
}
return result;
}

public <K> T queryById(K key) throws IllegalArgumentException, SQLException {
StringBuilder queryBuilder = new StringBuilder();
queryBuilder.append("SELECT * FROM ").append(tableName).append(" WHERE ")
.append(fields[pkIndex].getName()).append(" = ?");
try (Connection conn = pool.getConnection()) {
PreparedStatement statement = conn.prepareStatement(queryBuilder.toString());
statement.setString(1, key.toString());
try (ResultSet rs = statement.executeQuery()) {
rs.next();
T record = clazz.newInstance();
for (int i = 0; i < fields.length; ++i) {
if (fields[i].getClass().isAssignableFrom(Number.class)) {
Long val = rs.getLong(i + 1);
fields[i].set(record, val);
} else if (fields[i].getType() != String.class) {
fields[i].set(record, rs.getObject(i + 1));
} else {
Clob data = rs.getClob(i + 1);
fields[i].set(record, data.getSubString(1, (int) data.length()));
}
}
return record;
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalArgumentException("wrong class");
}
}

}

@Override
public void close() throws IOException {
if (pool != null) {
pool.dispose();
}
}
}
Loading