Skip to content

Commit

Permalink
Attempt to query an entity even if no Annotation present on the class. (
Browse files Browse the repository at this point in the history
#16)

* Attempt to query an entity even if no Annotation present on the class.

* Update

* update version before release
  • Loading branch information
martin-jamszolik authored Dec 26, 2024
1 parent 24f4689 commit a8970b3
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ bin/
!**/src/main/**/target/
!**/src/test/**/target/
.gradle
*.orig

### IntelliJ IDEA ###
.idea
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,4 @@ Explore real-world usage scenarios through the provided test cases:

## Contributing

Contributions are welcome! Feel free to submit issues or pull requests to improve the library.
Contributions are welcome! Feel free to submit issues or pull requests to improve the library.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ plugins {
id("maven-publish")
}

var libReleaseVersion = "1.7.1"
var libReleaseVersion = "1.7.2"

group = "org.viablespark"
version = libReleaseVersion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,11 @@ public Optional<E> get(Key key, Class<E> cls) {
}

public List<E> queryEntity(SqlQuery query, Class<E> cls) {
var primaryKeyName = cls.isAnnotationPresent(PrimaryKey.class)
? cls.getAnnotation(PrimaryKey.class).value()
: query.getPrimaryKeyName();
String sql = String.format("SELECT %s FROM %s %s",
WithSql.getSelectClause(cls, query.getPrimaryKeyName()),
WithSql.getSelectClause(cls, primaryKeyName),
deriveEntityName(cls),
query.sql());
return jdbc.query(sql, PersistableRowMapper.of(cls), query.values());
Expand Down
62 changes: 62 additions & 0 deletions src/test/java/org/viablespark/persistence/NotePersistable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2023 the original author or authors.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/

package org.viablespark.persistence;

import org.viablespark.persistence.dsl.Named;

import java.time.LocalDate;

@Named("note")
public class NotePersistable implements Persistable {
private Key key = Key.None;

private String note;
private String additional;

private LocalDate note_date;

public String getNote() {
return note;
}

public void setNote(String note) {
this.note = note;
}

public String getAdditional() {
return additional;
}

public void setAdditional(String additional) {
this.additional = additional;
}

public LocalDate getNote_date() {
return note_date;
}

public void setNote_date(LocalDate note_date) {
this.note_date = note_date;
}

public Key getRefs() {
return key;
}

@Override
public void setRefs(Key refs) {
this.key = refs;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,23 @@ public void testQueryNote() throws Exception {
var found = repository.queryEntity(SqlQuery
.withClause("WHERE progress_id = ?", 1)
.primaryKey("n_key"),Note.class);

assertTrue( found.size() > 1);

}

@Test
public void testQueryOnNotePersistable() throws Exception {
var repo = new BaseRepository<NotePersistable>(new JdbcTemplate(db)){};

var plainFound = repo.queryEntity(SqlQuery
.withClause("WHERE progress_id = ?", 1)
.primaryKey("n_key"), NotePersistable.class);

assertTrue( plainFound.size() > 1);
}



@Test
public void testUsingPersistableInAMap() throws Exception {
Map<Persistable,Key> map = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ public void testGet() {
public void testQuery() {

List<Proposal> results = repository.queryEntity(SqlQuery
.withClause("WHERE dist >= ?", 10)
.primaryKey("pr_key"),
Proposal.class);
.withClause("WHERE dist >= ?", 10),Proposal.class);

assertEquals(3, results.size());

Expand Down

0 comments on commit a8970b3

Please sign in to comment.