-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
213 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright (C) 2023 Nameless Production Committee | ||
* | ||
* Licensed under the MIT License (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://opensource.org/licenses/mit-license.php | ||
*/ | ||
package typewriter.query; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import typewriter.api.Identifiable; | ||
import typewriter.api.Specifier; | ||
|
||
public abstract class Query<M extends Identifiable> { | ||
|
||
private final List<String> select = new ArrayList(); | ||
|
||
/** | ||
* Declare SELECT statement. | ||
* | ||
* @param names | ||
*/ | ||
protected final void SELECT(Specifier<M, ?>... names) { | ||
SELECT(Stream.of(names).map(Specifier::propertyName).toList()); | ||
} | ||
|
||
/** | ||
* Declare SELECT statement. | ||
* | ||
* @param names | ||
*/ | ||
protected final void SELECT(String... names) { | ||
SELECT(List.of(names)); | ||
} | ||
|
||
/** | ||
* Declare SELECT statement. | ||
* | ||
* @param names | ||
*/ | ||
protected final void SELECT(List<String> names) { | ||
select.addAll(names); | ||
} | ||
|
||
/** | ||
* Test query. | ||
* | ||
* @param query | ||
* @return | ||
*/ | ||
public boolean is(String query) { | ||
return query.equals(toString()); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public String toString() { | ||
StringBuilder builder = new StringBuilder(); | ||
|
||
// SELECT | ||
builder.append("SELECT ").append(select.stream().collect(Collectors.joining(", "))); | ||
|
||
return builder.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright (C) 2023 Nameless Production Committee | ||
* | ||
* Licensed under the MIT License (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://opensource.org/licenses/mit-license.php | ||
*/ | ||
package typewriter.query; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import typewriter.api.model.IdentifiableModel; | ||
|
||
public class QueryTest { | ||
|
||
@Test | ||
void selectName() { | ||
Query query = new Query() { | ||
{ | ||
SELECT("test"); | ||
} | ||
}; | ||
|
||
assert query.is("SELECT test"); | ||
} | ||
|
||
@Test | ||
void selectNames() { | ||
Query query = new Query() { | ||
{ | ||
SELECT("first", "second"); | ||
} | ||
}; | ||
|
||
assert query.is("SELECT first, second"); | ||
} | ||
|
||
@Test | ||
void selectSpecifier() { | ||
Query query = new Query<Person>() { | ||
{ | ||
SELECT(Person::getName); | ||
} | ||
}; | ||
|
||
assert query.is("SELECT name"); | ||
} | ||
|
||
@Test | ||
void selectSpecifiers() { | ||
Query query = new Query() { | ||
{ | ||
SELECT("first", "second"); | ||
} | ||
}; | ||
|
||
assert query.is("SELECT first, second"); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
static class Person extends IdentifiableModel { | ||
|
||
public String name; | ||
|
||
public int age; | ||
|
||
/** | ||
* Create empty model. | ||
*/ | ||
public Person() { | ||
} | ||
|
||
/** | ||
* @param name | ||
* @param age | ||
*/ | ||
public Person(String name, int age) { | ||
this.name = name; | ||
this.age = age; | ||
} | ||
|
||
/** | ||
* Get the name property of this {@link Person}. | ||
* | ||
* @return The name property. | ||
*/ | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
/** | ||
* Get the age property of this {@link Person}. | ||
* | ||
* @return The age property. | ||
*/ | ||
public int getAge() { | ||
return age; | ||
} | ||
} | ||
} |