Skip to content
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

feat: adjust panic to Java Exception #1971

Merged
merged 26 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6bdeec6
build: update to version 0.5.2
linux-china Feb 27, 2023
b7c6982
feat: add Exception for method signature
linux-china Feb 27, 2023
0aa7287
chore: add Exception for test method
linux-china Feb 27, 2023
2923052
feat: adjust panic to Java Exception and add format method
linux-china Feb 27, 2023
08933f0
chore: use implicit return
linux-china Feb 27, 2023
7f3bc62
test: add compileWithError() to test compile with error
linux-china Feb 27, 2023
12c9e2a
Merge branch 'main' of github.com:PRQL/prql
linux-china Feb 28, 2023
ede4814
Merge branch 'main' into main
max-sixty Feb 28, 2023
0f4cc4a
Merge branch 'main' of github.com:PRQL/prql
linux-china Feb 28, 2023
21d2475
feat: add dialect, format and signature parameters for toSql method
linux-china Feb 28, 2023
ab9b999
chore: update toSql method signature
linux-china Feb 28, 2023
e84b16e
Merge branch 'PRQL:main' into main
linux-china Feb 28, 2023
f35b571
Merge branch 'main' of github.com:PRQL/prql
linux-china Feb 28, 2023
9c1792e
Merge branch 'main' of github.com:linux-china/prql
linux-china Feb 28, 2023
e05cf47
Merge branch 'PRQL:main' into main
linux-china Mar 1, 2023
a2a5293
lint: code polishing reported by clippy
linux-china Mar 1, 2023
cc1b4e4
chore: code format
linux-china Mar 1, 2023
855c5fc
docs: adjust signature for toSQL()
linux-china Mar 1, 2023
7bc823f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 1, 2023
733f771
docs: format
linux-china Mar 1, 2023
48bcb88
chore: introduce target dialect from https://github.com/PRQL/prql/blo…
linux-china Mar 1, 2023
5b71f5a
chore: rename dialect to target
linux-china Mar 1, 2023
baf1e41
Merge branch 'main' of github.com:linux-china/prql
linux-china Mar 1, 2023
1b3f46f
Use target rather than dialect
max-sixty Mar 1, 2023
7c301a7
lint
max-sixty Mar 1, 2023
1c2f8ab
docs: add javadoc for toSql()
linux-china Mar 1, 2023
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
2 changes: 1 addition & 1 deletion prql-java/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.prqllang</groupId>
<artifactId>prql-java</artifactId>
<version>0.2.1</version>
<version>0.5.2</version>

<name>${project.groupId}:${project.artifactId}</name>
<description>prql-compiler api for java</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import java.io.IOException;

public class PrqlCompiler {
public static native String toSql(String query);
public static native String toJson(String query);
public static native String toSql(String query) throws Exception;
public static native String toJson(String query) throws Exception;
public static native String format(String query) throws Exception;

static {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class PrqlCompilerTest {
@Test
public void compile() {
public void compile() throws Exception {
String found = PrqlCompiler.toSql("from table");

// remove signature
Expand All @@ -16,4 +16,9 @@ public void compile() {
" table";
assert expected.equalsIgnoreCase(found);
}

@Test(expected = Exception.class)
public void compileWithError() throws Exception {
PrqlCompiler.toSql("from table | filter id >> 1");
}
max-sixty marked this conversation as resolved.
Show resolved Hide resolved
}
45 changes: 34 additions & 11 deletions prql-java/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use jni::objects::{JClass, JString};
use jni::sys::jstring;
use jni::JNIEnv;
use prql_compiler::{json, prql_to_pl, Options};
use prql_compiler::{json, prql_to_pl, pl_to_prql, Options, ErrorMessages};

#[no_mangle]
#[allow(non_snake_case)]
Expand All @@ -14,11 +14,23 @@ pub extern "system" fn Java_org_prql_prql4j_PrqlCompiler_toSql(
.get_string(query)
.expect("Couldn't get java string!")
.into();
let rs_sql_str: String = prql_compiler::compile(&prql_query, &Options::default())
.expect("Couldn't compile query to prql!");
env.new_string(rs_sql_str)
.expect("Couldn't create java string!")
.into_raw()
let result = prql_compiler::compile(&prql_query, &Options::default());
java_string_with_exception(result, &env)
}

#[no_mangle]
#[allow(non_snake_case)]
pub extern "system" fn Java_org_prql_prql4j_PrqlCompiler_format(
env: JNIEnv,
_class: JClass,
query: JString,
) -> jstring {
let prql_query: String = env
.get_string(query)
.expect("Couldn't get java string!")
.into();
let result = prql_to_pl(&prql_query).and_then(pl_to_prql);
java_string_with_exception(result,&env)
}

#[no_mangle]
Expand All @@ -32,9 +44,20 @@ pub extern "system" fn Java_org_prql_prql4j_PrqlCompiler_toJson(
.get_string(query)
.expect("Couldn't get java string!")
.into();
let rs_json_str: String = { prql_to_pl(&prql_query).and_then(json::from_pl) }
.expect("Couldn't get json from prql query!");
env.new_string(rs_json_str)
.expect("Couldn't create java string!")
.into_raw()
let result = prql_to_pl(&prql_query).and_then(json::from_pl);
return java_string_with_exception(result, &env);
}

fn java_string_with_exception(result: Result<String, ErrorMessages>, env: &JNIEnv) -> jstring {
if result.is_ok() {
env.new_string(result.unwrap())
.expect("Couldn't create java string!")
.into_raw()
} else {
let exception = env.find_class("java/lang/Exception").unwrap();
if let Err(e) = env.throw_new(exception, result.err().unwrap().to_string()) {
println!("Error throwing exception: {:?}", e);
}
std::ptr::null_mut() as jstring
}
}