preparing filling of intermediate table
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -116,6 +116,7 @@ public class Constants {
|
||||
public static final String LANGUAGE = "language";
|
||||
public static final String LAST_CUSTOMER_NUMBER = "last_customer_number";
|
||||
public static final String LIMIT = "limit";
|
||||
public static final String LOCATION_ID = "location_id";
|
||||
public static final String LOGIN = "login";
|
||||
|
||||
public static final String MEMBERS = "members";
|
||||
|
||||
@@ -1,10 +1,60 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.core.model;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.databaseException;
|
||||
|
||||
public class Location {
|
||||
long owner;
|
||||
boolean ownerIsCompany = false;
|
||||
private long id;
|
||||
private long parentLocationId;
|
||||
private Long parentLocationId;
|
||||
private String name;
|
||||
private String description;
|
||||
private String relation; // when added to an item, this field describes the type of the relation
|
||||
|
||||
private Location(long owner, boolean ownerIsCompany, long id, Long parentLocationId, String name, String description){
|
||||
this.owner = owner;
|
||||
this.ownerIsCompany = ownerIsCompany;
|
||||
this.id = id;
|
||||
this.parentLocationId = parentLocationId;
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public static Location of(ResultSet rs){
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Location ofLegacy(ResultSet rs) throws SQLException {
|
||||
var id = rs.getString(ID);
|
||||
var parent = rs.getString(LOCATION_ID);
|
||||
var name = rs.getString(NAME);
|
||||
var description = rs.getString(DESCRIPTION);
|
||||
var ownerIsCompany = false;
|
||||
var parts = id.split(":");
|
||||
if (parts.length != 3) throw databaseException("Legacy id expected to be of format ss:dd:ss, encountered {0}",id);
|
||||
switch (parts[0]){
|
||||
case "company":
|
||||
ownerIsCompany = true; break;
|
||||
case "user":
|
||||
break;
|
||||
case null, default:
|
||||
throw databaseException("Legacy id expected to start with 'company' or 'user', encountered {0}",id);
|
||||
}
|
||||
var owner = 0L;
|
||||
try {
|
||||
owner = Long.parseLong(parts[1]);
|
||||
} catch (NumberFormatException nfe){
|
||||
throw databaseException("Legacy id expected to be of format ss:dd:ss, encountered {0}",id);
|
||||
}
|
||||
Long parentLocationId = null;
|
||||
if (parent != null){
|
||||
// TODO
|
||||
}
|
||||
return new Location(owner, ownerIsCompany, id, parentLocationId, name, description);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,12 +2,12 @@
|
||||
package de.srsoftware.umbrella.stock;
|
||||
|
||||
public class Constants {
|
||||
public static final String PARENT_LOCATION_ID = "parent_location_id";
|
||||
|
||||
private Constants(){}
|
||||
|
||||
|
||||
|
||||
public static final String CONFIG_DATABASE = "umbrella.modules.stock.database";
|
||||
public static final String ITEM_ID = "item_id"; public static final String LOCATION_ID = "location_id";
|
||||
public static final String ITEM_ID = "item_id";
|
||||
public static final String OWNER = "owner";
|
||||
public static final String PROPERTY_ID = "prop_id";
|
||||
public static final String TABLE_ITEMS = "items";
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
package de.srsoftware.umbrella.stock;
|
||||
|
||||
import static de.srsoftware.tools.Optionals.nullIfEmpty;
|
||||
import static de.srsoftware.tools.jdbc.Query.select;
|
||||
import static de.srsoftware.tools.jdbc.Query.update;
|
||||
import static de.srsoftware.tools.jdbc.Query.*;
|
||||
import static de.srsoftware.tools.jdbc.Query.SelectQuery.ALL;
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.databaseException;
|
||||
import static de.srsoftware.umbrella.stock.Constants.*;
|
||||
@@ -11,6 +11,7 @@ import static java.lang.System.Logger.Level.ERROR;
|
||||
import static java.text.MessageFormat.format;
|
||||
|
||||
import de.srsoftware.tools.Tuple;
|
||||
import de.srsoftware.tools.jdbc.Query;
|
||||
import de.srsoftware.umbrella.core.BaseDb;
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.model.Hash;
|
||||
@@ -96,53 +97,24 @@ public class SqliteDb extends BaseDb implements StockDb {
|
||||
|
||||
private void transformLocationsTable(){
|
||||
try {
|
||||
var tempTable = "locations_temp";
|
||||
db.setAutoCommit(false);
|
||||
var sql = "ALTER TABLE {0} ADD COLUMN {1} LONG NOT NULL DEFAULT ``";
|
||||
sql = format(sql,TABLE_LOCATIONS,OWNER);
|
||||
db.prepareStatement(sql).execute();
|
||||
var rs = select(ID,LOCATION_ID).from(TABLE_LOCATIONS).exec(db);
|
||||
var queue = new ArrayList<Tuple<String,String>>();
|
||||
var map = new HashMap<String, Long>(); // map from old ids to new numeric ids
|
||||
var ids = new HashMap<Long, Long>(); // map from owner to last inserted id
|
||||
while (rs.next()) queue.add(Tuple.of(rs.getString(ID),nullIfEmpty(rs.getString(LOCATION_ID))));
|
||||
rs.close();
|
||||
var query = update(TABLE_LOCATIONS).set(ID,LOCATION_ID,OWNER).prepare(db);
|
||||
while (!queue.isEmpty()) {
|
||||
var entry = queue.removeFirst();
|
||||
var oldId = entry.a;
|
||||
var oldLoc = nullIfEmpty(entry.b);
|
||||
if (oldLoc != null && !map.containsKey(oldLoc)) {
|
||||
queue.add(entry);
|
||||
continue;
|
||||
{ // create intermediate table
|
||||
var sql = "CREATE TABLE IF NOT EXISTS locations_temp ({0} LONG NOT NULL, {1} LONG NOT NULL, {2} LONG DEFAULT NULL, {3} VARCHAR(255) NOT NULL, {4} TEXT)";
|
||||
sql = format(sql, OWNER, ID, PARENT_LOCATION_ID, NAME, DESCRIPTION);
|
||||
db.prepareStatement(sql).execute();
|
||||
}
|
||||
var locations = new ArrayList<Location>();
|
||||
{ // fill intermediate table
|
||||
var rs = select(ALL).from(tempTable).exec(db);
|
||||
while (rs.next()) locations.add(Location.ofLegacy(rs));
|
||||
rs.close();
|
||||
|
||||
var query = insertInto(tempTable, OWNER, ID, PARENT_LOCATION_ID, NAME, DESCRIPTION);
|
||||
for (var location : locations){
|
||||
|
||||
}
|
||||
var parts = oldId.split(":");
|
||||
if (parts.length != 3) throw new IllegalArgumentException(format("Expected 'xxx:dd:yy', got {0}",oldId));
|
||||
long ownerId;
|
||||
try {
|
||||
ownerId = Long.parseLong(parts[1]);
|
||||
} catch (NumberFormatException nfe){
|
||||
throw new IllegalArgumentException(format("Expected 'xxx:dd:yy', got {0}",oldId));
|
||||
}
|
||||
switch (parts[0]){
|
||||
case "company":
|
||||
ownerId = -ownerId;
|
||||
break;
|
||||
case "user":
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException(format("Expected 'company:dd:dd' or 'user:dd:dd', got {0}",oldId));
|
||||
}
|
||||
Long location = null;
|
||||
if (oldLoc != null){
|
||||
var expected = String.join(":",parts[0],parts[1],"");
|
||||
if (!oldLoc.startsWith(expected)) throw new IllegalArgumentException(format("Expected location_id to start with {1}, got {0}",oldId,expected));
|
||||
location = Long.parseLong(oldLoc.substring(expected.length()));
|
||||
}
|
||||
var itemId = ids.get(ownerId);
|
||||
if (itemId == null) itemId = 1L;
|
||||
ids.put(ownerId,itemId+1);
|
||||
query.apply(itemId,location,ownerId);
|
||||
map.put(oldId,itemId);
|
||||
|
||||
}
|
||||
db.setAutoCommit(true);
|
||||
} catch (Exception e) {
|
||||
|
||||
Reference in New Issue
Block a user