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
3 changes: 3 additions & 0 deletions nikitarykov/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.properties
test.mv.db
test.trace.db
32 changes: 31 additions & 1 deletion nikitarykov/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,38 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.190</version>
</dependency>
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<version>4.0.4</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>com.google.maps</groupId>
<artifactId>google-maps-services</artifactId>
<version>0.1.9</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20151123</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
package ru.fizteh.fivt.students.nikitarykov.miniorm;

import org.h2.jdbcx.JdbcConnectionPool;
import ru.fizteh.fivt.students.nikitarykov.miniorm.annotations.Column;
import ru.fizteh.fivt.students.nikitarykov.miniorm.annotations.PrimaryKey;
import ru.fizteh.fivt.students.nikitarykov.miniorm.annotations.Table;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static ru.fizteh.fivt.students.nikitarykov.miniorm.TypeMatcher.match;

/**
* Created by Nikita Rykov on 18.12.2015.
*/
public class DatabaseService<T> {
private Class<T> clazz;
private String tableName;
private List<Field> fields;
private int primaryKeyIndex = -1;
private JdbcConnectionPool pool;

DatabaseService(Class<T> clazz) throws SQLException, ClassNotFoundException {
this.clazz = clazz;
Class.forName("org.h2.Driver");
pool = JdbcConnectionPool.create(
"jdbc:h2:./test", "test", "test");
if (!clazz.isAnnotationPresent(Table.class)) {
throw new IllegalArgumentException("Class has no @Table annotation");
}
tableName = clazz.getAnnotation(Table.class).name();
fields = new ArrayList<>();
if (tableName.equals("")) {
tableName = clazz.getSimpleName()
.replaceAll("(.)(\\p{Upper})", "$1_$2").toLowerCase();
}
Set<String> columnNames = new HashSet<>();
for (Field field : clazz.getDeclaredFields()) {
if (field.isAnnotationPresent(Column.class)) {
String columnName = getName(field);
field.setAccessible(true);
fields.add(field);
if (columnNames.contains(columnName)) {
throw new IllegalArgumentException("Duplicate column names");
} else {
columnNames.add(columnName);
}
if (field.isAnnotationPresent(PrimaryKey.class)) {
if (primaryKeyIndex == -1) {
primaryKeyIndex = fields.size() - 1;
} else {
throw new IllegalArgumentException("Multiple primary keys");
}
}
}
}
}

public String getTableName() {
return tableName;
}

public Class<T> getClazz() {
return clazz;
}

public List<Field> getFields() {
return fields;
}

public String getName(Field field) {
String name = field.getAnnotation(Column.class).name();
if (name.equals("")) {
name = field.getName()
.replaceAll("(.)(\\p{Upper})", "$1_$2").toLowerCase();
}
return name;
}

public <K> T queryById(K key) throws SQLException {
String query = "SELECT * FROM " + tableName + " WHERE "
+ getName(fields.get(primaryKeyIndex)) + " = ?";
Connection connection = pool.getConnection();
try {
PreparedStatement statement = connection.prepareStatement(query);
statement.setString(1, key.toString());
ResultSet resultSet = statement.executeQuery();
resultSet.next();
T result = clazz.newInstance();
for (int i = 0; i < fields.size(); ++i) {
fields.get(i).set(result, resultSet.getObject(i + 1));
}
statement.close();
return result;
} catch (IllegalAccessException | InstantiationException exception) {
throw new IllegalArgumentException("Error in object creation");
} finally {
connection.close();
}
}

public List<T> queryForAll() throws SQLException {
List<T> result = new ArrayList<>();
String query = "SELECT * FROM " + tableName;
Connection connection = pool.getConnection();
try {
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query);

while (resultSet.next()) {
T element = clazz.newInstance();
for (int i = 0; i < fields.size(); ++i) {
fields.get(i).set(element, resultSet.getObject(i + 1));
}
result.add(element);
}
statement.close();
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalArgumentException("wrong class");
} finally {
connection.close();
}
return result;
}


void insert(T record) throws SQLException, IllegalAccessException {
String query = "INSERT INTO " + tableName + " (";
for (int i = 0; i < fields.size(); ++i) {
query += getName(fields.get(i));
if (i < fields.size() - 1) {
query += ", ";
} else {
query += ") ";
}
}
query += "VALUES (";
for (int i = 0; i < fields.size(); ++i) {
if (i < fields.size() - 1) {
query += "?, ";
} else {
query += "?)";
}

}
Connection connection = pool.getConnection();
try {
PreparedStatement statement = connection.prepareStatement(query);
for (int i = 0; i < fields.size(); ++i) {
statement.setObject(i + 1, fields.get(i).get(record));
}
statement.execute();
statement.close();
} finally {
connection.close();
}
}

void update(T record) throws SQLException, IllegalAccessException {
if (primaryKeyIndex == -1) {
throw new IllegalArgumentException("No primary key found");
}
String query = "UPDATE " + tableName + " SET ";
for (int i = 0; i < fields.size(); ++i) {
query += getName(fields.get(i)) + " = ?";
if (i < fields.size() - 1) {
query += ", ";
}
}
query += " WHERE " + getName(fields.get(primaryKeyIndex)) + " = ?";
Connection connection = pool.getConnection();
try {
PreparedStatement statement = connection.prepareStatement(query);
for (int i = 0; i < fields.size(); ++i) {
statement.setObject(i + 1, fields.get(i).get(record));
}
statement.setObject(fields.size() + 1,
fields.get(primaryKeyIndex).get(record));
statement.execute();
statement.close();
} finally {
connection.close();
}
}

void delete(T record) throws SQLException, IllegalAccessException {
if (primaryKeyIndex == -1) {
throw new IllegalArgumentException("No primary key found");
}
String query = "DELETE " + tableName + " WHERE "
+ getName(fields.get(primaryKeyIndex)) + " = ?";
Connection connection = pool.getConnection();
try {
PreparedStatement statement = connection.prepareStatement(query);
statement.setObject(1, fields.get(primaryKeyIndex).get(record));
statement.execute();
statement.close();
} finally {
connection.close();
}
}

void createTable() throws SQLException {
String query = "CREATE TABLE IF NOT EXISTS "
+ tableName + "(";
for (int i = 0; i < fields.size(); ++i) {
query += getName(fields.get(i))
+ " " + match(fields.get(i).getType());
if (i == primaryKeyIndex) {
query += " PRIMARY KEY";
}
if (i < fields.size() - 1) {
query += ", ";
} else {
query += ")";
}
}
Connection connection = pool.getConnection();
try {
Statement statement = connection.createStatement();
statement.execute(query);
statement.close();
} finally {
connection.close();
}
}

void dropTable() throws SQLException {
String query = "DROP TABLE IF EXISTS " + tableName;
Connection connection = pool.getConnection();
try {
Statement statement = connection.createStatement();
statement.execute(query);
statement.close();
} finally {
connection.close();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package ru.fizteh.fivt.students.nikitarykov.miniorm;

import ru.fizteh.fivt.students.nikitarykov.miniorm.annotations.Column;
import ru.fizteh.fivt.students.nikitarykov.miniorm.annotations.PrimaryKey;
import ru.fizteh.fivt.students.nikitarykov.miniorm.annotations.Table;

import java.sql.Date;

/**
* Created by Nikita Rykov on 19.12.2015.
*/
@Table
public class RatingOfStudents {
@Column
@PrimaryKey
private String name;

@Column
private Integer rank;

@Column(name = "birthday")
private Date dateOfBirth;

RatingOfStudents() {
}

RatingOfStudents(String name, Integer rank, Date dateOfBirth) {
this.name = name;
this.rank = rank;
this.dateOfBirth = dateOfBirth;
}

public void setName(String name) {
this.name = name;
}

public void setRank(Integer rank) {
this.rank = rank;
}

public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}

public String getName() {
return name;
}

public Integer getRank() {
return rank;
}

public Date getDateOfBirth() {
return dateOfBirth;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package ru.fizteh.fivt.students.nikitarykov.miniorm;


import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.UUID;

/**
* Created by Nikita Rykov on 18.12.2015.
*/
public class TypeMatcher {
public static String match(Class clazz) {
if (clazz.isArray()) {
return "ARRAY";
} else if (clazz.equals(Integer.class)) {
return "INT";
} else if (clazz.equals(Boolean.class)) {
return "BOOL";
} else if (clazz.equals(Byte.class)) {
return "TINYINT";
} else if (clazz.equals(Short.class)) {
return "INT2";
} else if (clazz.equals(Double.class)) {
return "DOUBLE";
} else if (clazz.equals(Float.class)) {
return "FLOAT4";
} else if (clazz.equals(Float.class)) {
return "FLOAT4";
} else if (clazz.equals(Date.class)) {
return "DATE";
} else if (clazz.equals(Date.class)) {
return "DATE";
} else if (clazz.equals(Time.class)) {
return "TIME";
} else if (clazz.equals(Timestamp.class)) {
return "DATETIME";
} else if (clazz.equals(Character.class)) {
return "CHAR";
} else if (clazz.equals(String.class)) {
return "VARCHAR(256)";
} else if (clazz.equals(UUID.class)) {
return "UUID";
} else {
return "OTHER";
}
}
}
Loading