Skip to content

fix(datastore): Fix aliasing of column names #2312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 2, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"associatedType": "Call",
"name": "HasMany"
},
"ownedBy": {
"ownerOfPhone": {
"associatedType": "Person",
"name": "BelongsTo",
"targetName": "ownedById"
"targetName": "ownerOfPhoneId"
}
},
"authRules": [],
Expand All @@ -33,24 +33,24 @@
"name": "number",
"targetType": "String"
},
"ownedById": {
"ownerOfPhoneId": {
"authRules": [],
"isArray": false,
"isEnum": false,
"isModel": false,
"isRequired": true,
"javaClassForValue": "java.lang.String",
"name": "ownedById",
"name": "ownerOfPhoneId",
"targetType": "ID"
},
"ownedBy": {
"ownerOfPhone": {
"authRules": [],
"isArray": false,
"isEnum": false,
"isModel": true,
"isRequired": true,
"javaClassForValue": "com.amplifyframework.core.model.SerializedModel",
"name": "ownedBy",
"name": "ownerOfPhone",
"targetType": "Person"
},
"calls": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,11 @@ public void querySavedDataWithMultipleForeignKeysOfSameType() throws DataStoreEx

final Phone phoneCalling = Phone.builder()
.number("123-456-7890")
.ownedBy(personCalling)
.ownerOfPhone(personCalling)
.build();
final Phone phoneCalled = Phone.builder()
.number("567-890-1234")
.ownedBy(personCalled)
.ownerOfPhone(personCalled)
.build();

final Call phoneCall = Call.builder()
Expand Down Expand Up @@ -424,11 +424,11 @@ public void querySavedDataWithTimePredicates() throws DataStoreException {

final Phone phoneCalling = Phone.builder()
.number("123-456-7890")
.ownedBy(personCalling)
.ownerOfPhone(personCalling)
.build();
final Phone phoneCalled = Phone.builder()
.number("567-890-1234")
.ownedBy(personCalled)
.ownerOfPhone(personCalled)
.build();

adapter.save(personCalling);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public SqlCommand queryFor(@NonNull ModelSchema modelSchema,
Iterator<SQLiteColumn> columnsIterator = Objects.requireNonNull(columns.get(tableAlias)).iterator();
while (columnsIterator.hasNext()) {
final SQLiteColumn column = columnsIterator.next();
String columnName = column.getQuotedColumnName().replace(column.getTableName(), tableAlias);
String columnName = column.getQuotedColumnName().replaceFirst(column.getTableName(), tableAlias);
selectColumns.append(columnName);
// Alias columns with a unique alias to avoid duplicate column names or alias names
String columnAlias = column.getAliasedName()
Expand Down Expand Up @@ -542,8 +542,8 @@ private void recursivelyBuildJoins(SQLiteTable table, Map<String, List<SQLiteCol
}

// Reference the foreign key and primary key using the corresponding table's alias.
String foreignKeyName = foreignKey.getQuotedColumnName().replace(table.getName(), tableAlias);
String ownedTablePrimaryKeyName = ownedTable.getPrimaryKeyColumnName().replace(ownedTableName,
String foreignKeyName = foreignKey.getQuotedColumnName().replaceFirst(table.getName(), tableAlias);
String ownedTablePrimaryKeyName = ownedTable.getPrimaryKeyColumnName().replaceFirst(ownedTableName,
ownedTableAlias);
joinStatement.append(SqlKeyword.ON)
.append(SqlKeyword.DELIMITER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
public final class Phone implements Model {
public static final QueryField ID = field("Phone", "id");
public static final QueryField NUMBER = field("Phone", "number");
public static final QueryField OWNED_BY = field("Phone", "ownedById");
public static final QueryField OWNER_OF_PHONE = field("Phone", "ownerOfPhoneId");
private final @ModelField(targetType="ID", isRequired = true) String id;
private final @ModelField(targetType="String", isRequired = true) String number;
private final @ModelField(targetType="Person", isRequired = true) @BelongsTo(targetName = "ownedById", type = Person.class) Person ownedBy;
private final @ModelField(targetType="Person", isRequired = true) @BelongsTo(targetName = "ownerOfPhoneId", type = Person.class) Person ownerOfPhone;
private final @ModelField(targetType="Call") @HasMany(associatedWith = "id", type = Call.class) List<Call> calls = null;
private @ModelField(targetType="AWSDateTime", isReadOnly = true) Temporal.DateTime createdAt;
private @ModelField(targetType="AWSDateTime", isReadOnly = true) Temporal.DateTime updatedAt;
Expand All @@ -58,8 +58,8 @@ public String getNumber() {
return number;
}

public Person getOwnedBy() {
return ownedBy;
public Person getOwnerOfPhone() {
return ownerOfPhone;
}

public List<Call> getCalls() {
Expand All @@ -74,10 +74,10 @@ public Temporal.DateTime getUpdatedAt() {
return updatedAt;
}

private Phone(String id, String number, Person ownedBy) {
private Phone(String id, String number, Person ownerOfPhone) {
this.id = id;
this.number = number;
this.ownedBy = ownedBy;
this.ownerOfPhone = ownerOfPhone;
}

@Override
Expand All @@ -90,7 +90,7 @@ public boolean equals(Object obj) {
Phone phone = (Phone) obj;
return ObjectsCompat.equals(getId(), phone.getId()) &&
ObjectsCompat.equals(getNumber(), phone.getNumber()) &&
ObjectsCompat.equals(getOwnedBy(), phone.getOwnedBy()) &&
ObjectsCompat.equals(getOwnerOfPhone(), phone.getOwnerOfPhone()) &&
ObjectsCompat.equals(getCreatedAt(), phone.getCreatedAt()) &&
ObjectsCompat.equals(getUpdatedAt(), phone.getUpdatedAt());
}
Expand All @@ -101,7 +101,7 @@ public int hashCode() {
return new StringBuilder()
.append(getId())
.append(getNumber())
.append(getOwnedBy())
.append(getOwnerOfPhone())
.append(getCreatedAt())
.append(getUpdatedAt())
.toString()
Expand All @@ -114,7 +114,7 @@ public String toString() {
.append("Phone {")
.append("id=" + String.valueOf(getId()) + ", ")
.append("number=" + String.valueOf(getNumber()) + ", ")
.append("ownedBy=" + String.valueOf(getOwnedBy()) + ", ")
.append("ownerOfPhone=" + String.valueOf(getOwnerOfPhone()) + ", ")
.append("createdAt=" + String.valueOf(getCreatedAt()) + ", ")
.append("updatedAt=" + String.valueOf(getUpdatedAt()))
.append("}")
Expand Down Expand Up @@ -154,14 +154,14 @@ public static Phone justId(String id) {
public CopyOfBuilder copyOfBuilder() {
return new CopyOfBuilder(id,
number,
ownedBy);
ownerOfPhone);
}
public interface NumberStep {
OwnedByStep number(String number);
OwnerOfPhoneStep number(String number);
}

public interface OwnedByStep {
BuildStep ownedBy(Person ownedBy);
public interface OwnerOfPhoneStep {
BuildStep ownerOfPhone(Person ownerOfPhone);
}


Expand All @@ -171,30 +171,30 @@ public interface BuildStep {
}


public static class Builder implements NumberStep, OwnedByStep, BuildStep {
public static class Builder implements NumberStep, OwnerOfPhoneStep, BuildStep {
private String id;
private String number;
private Person ownedBy;
private Person ownerOfPhone;
@Override
public Phone build() {
String id = this.id != null ? this.id : UUID.randomUUID().toString();

return new Phone(
id,
number,
ownedBy);
ownerOfPhone);
}

@Override
public OwnedByStep number(String number) {
public OwnerOfPhoneStep number(String number) {
Objects.requireNonNull(number);
this.number = number;
return this;
}

@Override
public BuildStep ownedBy(Person ownedBy) {
this.ownedBy = ownedBy;
public BuildStep ownerOfPhone(Person ownerOfPhone) {
this.ownerOfPhone = ownerOfPhone;
return this;
}

Expand All @@ -221,10 +221,10 @@ public BuildStep id(String id) throws IllegalArgumentException {


public final class CopyOfBuilder extends Builder {
private CopyOfBuilder(String id, String number, Person ownedBy) {
private CopyOfBuilder(String id, String number, Person ownerOfPhone) {
super.id(id);
super.number(number)
.ownedBy(ownedBy);
.ownerOfPhone(ownerOfPhone);
}

@Override
Expand All @@ -233,8 +233,8 @@ public CopyOfBuilder number(String number) {
}

@Override
public CopyOfBuilder ownedBy(Person ownedBy) {
return (CopyOfBuilder) super.ownedBy(ownedBy);
public CopyOfBuilder ownerOfPhone(Person ownerOfPhone) {
return (CopyOfBuilder) super.ownerOfPhone(ownerOfPhone);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ type Person @model {
type Phone @model {
id: ID!
number: String!
ownedById: ID!
ownedBy: Person! @connection(fields: ["ownedById"])
ownerOfPhoneId: ID!
ownerOfPhone: Person! @connection(fields: ["ownerOfPhoneId"])
calls: [Call] @connection(fields: ["id"])
}

Expand Down