+ database stuff for address.
Signed-off-by: Johannes Theiner <j.theiner@live.de>
This commit is contained in:
parent
5e21307980
commit
b2ed3d6a03
|
@ -0,0 +1,115 @@
|
|||
package de.hsel.itech.db;
|
||||
|
||||
import de.hsel.itech.db.pojo.Address;
|
||||
import de.hsel.itech.db.pojo.User;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Johannes Theiner
|
||||
* @version 0.1
|
||||
* @since 0.5
|
||||
**/
|
||||
public class AddressDB {
|
||||
|
||||
private Database database;
|
||||
|
||||
AddressDB(Database database) {
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public List<Address> getAll(@NotNull User user) {
|
||||
Map.Entry<ResultSet, Connection> entry = database.getResultSetsByValue(database.tableUserAddress, "user", String.valueOf(user.getId()));
|
||||
assert entry != null;
|
||||
|
||||
List<Address> list = new ArrayList<>();
|
||||
|
||||
try {
|
||||
ResultSet rs = entry.getKey();
|
||||
while(rs.next()) {
|
||||
list.add(new Address(user, rs.getLong("id"), rs.getString("name"), rs.getString("street"), rs.getString("number"), rs.getInt("zipCode"), rs.getString("city")));
|
||||
}
|
||||
|
||||
entry.getValue().close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Address get(long id) {
|
||||
Map.Entry<ResultSet, Connection> entry = database.getResultSetById(database.tableAddress, id);
|
||||
assert entry != null;
|
||||
|
||||
try {
|
||||
ResultSet rs = entry.getKey();
|
||||
Address address = new Address(getUserFromId(id), id, rs.getString("name"), rs.getString("street"), rs.getString("number"), rs.getInt("zipCode"), rs.getString("city"));
|
||||
entry.getValue().close();
|
||||
|
||||
return address;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private User getUserFromId(long id) {
|
||||
Map.Entry<ResultSet, Connection> entry = database.getResultSetByValue(database.tableUserAddress, "address", id);
|
||||
assert entry != null;
|
||||
|
||||
try {
|
||||
User user = database.user().get(entry.getKey().getLong("user"));
|
||||
entry.getValue().close();
|
||||
return user;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public int insert(@NotNull Address address) {
|
||||
|
||||
Connection connection = database.getConnection();
|
||||
int updateCount = 0;
|
||||
|
||||
try {
|
||||
assert connection != null;
|
||||
PreparedStatement statement = connection.prepareStatement("INSERT INTO address (name, street, number, zipCode, city) VALUES (?, ?, ?, ?, ?)");
|
||||
statement.setString(1, address.getName());
|
||||
statement.setString(2, address.getStreet());
|
||||
statement.setString(3, address.getNumber());
|
||||
statement.setInt(4, address.getZipCode());
|
||||
statement.setString(5, address.getCity());
|
||||
|
||||
updateCount += statement.executeUpdate();
|
||||
|
||||
PreparedStatement relationStatement = connection.prepareStatement("INSERT INTO user_address (user, address) VALUES (?, ?)");
|
||||
relationStatement.setLong(1, address.getUser().getId());
|
||||
relationStatement.setLong(2, address.getId());
|
||||
|
||||
updateCount += statement.executeUpdate();
|
||||
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return updateCount;
|
||||
}
|
||||
|
||||
public int delete(@NotNull Address address) {
|
||||
return database.delete(address.getId(), database.tableUserAddress);
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package de.hsel.itech.db;
|
||||
|
||||
import de.hsel.itech.config.Configuration;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.json.JSONObject;
|
||||
|
@ -41,6 +42,7 @@ public class Database {
|
|||
private CategoryDB categoryDB;
|
||||
private PublisherDB publisherDB;
|
||||
private UserDB userDB;
|
||||
private AddressDB addressDB;
|
||||
|
||||
private static Database instance;
|
||||
|
||||
|
@ -104,36 +106,49 @@ public class Database {
|
|||
return null;
|
||||
}
|
||||
|
||||
@Contract
|
||||
public BookDB book() {
|
||||
if(bookDB == null)
|
||||
bookDB = new BookDB(this);
|
||||
return bookDB;
|
||||
}
|
||||
|
||||
@Contract
|
||||
public AuthorDB author() {
|
||||
if(authorDB == null)
|
||||
authorDB = new AuthorDB(this);
|
||||
return authorDB;
|
||||
}
|
||||
|
||||
@Contract
|
||||
public CategoryDB category() {
|
||||
if (categoryDB == null)
|
||||
categoryDB = new CategoryDB(this);
|
||||
return categoryDB;
|
||||
}
|
||||
|
||||
|
||||
@Contract
|
||||
public PublisherDB publisher() {
|
||||
if(publisherDB == null)
|
||||
publisherDB = new PublisherDB(this);
|
||||
return publisherDB;
|
||||
}
|
||||
|
||||
@Contract
|
||||
public UserDB user() {
|
||||
if(userDB == null)
|
||||
userDB = new UserDB(this);
|
||||
return userDB;
|
||||
}
|
||||
|
||||
@Contract
|
||||
public AddressDB address() {
|
||||
if(addressDB == null)
|
||||
addressDB = new AddressDB(this);
|
||||
return addressDB;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
|
|
@ -3,11 +3,11 @@ INSERT IGNORE INTO test(hello) values ('Welt');
|
|||
CREATE TABLE IF NOT EXISTS book (id bigint primary key auto_increment, isbn bigint unique, title varchar(50), description varchar(500), price int, year year, publisher bigint, category bigint, image varchar(11));
|
||||
CREATE TABLE IF NOT EXISTS publisher(id bigint primary key auto_increment, name varchar(50) unique);
|
||||
CREATE TABLE IF NOT EXISTS author(id bigint primary key auto_increment, name varchar(50) unique);
|
||||
CREATE TABLE IF NOT EXISTS author_book(author bigint, book bigint, primary key(author, book), foreign key (author) REFERENCES author(id), foreign key (book) references book(id));
|
||||
CREATE TABLE IF NOT EXISTS author_book(author bigint, book bigint, primary key(author, book), foreign key (author) REFERENCES author(id), foreign key (book) references book(id) on delete cascade);
|
||||
CREATE TABLE IF NOT EXISTS category(id bigint primary key auto_increment, name varchar(25), image varchar(11));
|
||||
CREATE TABLE IF NOT EXISTS user(id bigint primary key auto_increment, name varchar(50), email varchar(255), password binary(40), type tinyint);
|
||||
CREATE TABLE IF NOT EXISTS address(id bigint primary key auto_increment, street varchar(100), number smallint, zipCode smallint, city varchar(50));
|
||||
CREATE TABLE IF NOT EXISTS user_address(id bigint primary key auto_increment, user bigint, address bigint, foreign key (user) REFERENCES user(id), foreign key (address) references address(id));
|
||||
CREATE TABLE IF NOT EXISTS address(id bigint primary key auto_increment, name varchar(100), street varchar(100), number smallint, zipCode smallint, city varchar(50));
|
||||
CREATE TABLE IF NOT EXISTS user_address(id bigint primary key auto_increment, user bigint, address bigint, foreign key (user) REFERENCES user(id), foreign key (address) references address(id) on delete cascade);
|
||||
CREATE TABLE IF NOT EXISTS payment_type(id bigint primary key auto_increment, name varchar(25) unique);
|
||||
INSERT IGNORE INTO payment_type (name) values ('CreditCard');
|
||||
INSERT IGNORE INTO payment_type (name) values ('DebitCard');
|
||||
|
|
Loading…
Reference in New Issue