Skip to content

Commit ed1e939

Browse files
Merge pull request #14 from vertical-blank/add-demo-source
add demo sources
2 parents 701906b + 59e0fe0 commit ed1e939

File tree

7 files changed

+150
-2
lines changed

7 files changed

+150
-2
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
![travis](https://api.travis-ci.org/vertical-blank/sql-formatter.svg?branch=master)
55
[![codecov](https://codecov.io/gh/vertical-blank/sql-formatter/branch/master/graph/badge.svg)](https://codecov.io/gh/vertical-blank/sql-formatter)
66

7-
Java port of great SQL formatter https://github.com/zeroturnaround/sql-formatter.
7+
Java port of great SQL formatter <https://github.com/zeroturnaround/sql-formatter.>
88

99
Written with only Java Standard Library, without dependencies.
1010

1111
[Demo](http://www.vertical-blank.com/sql-formatter/)
1212

13+
Demo is running on Google Cloud Function, with native-compiled shared library by GraalVM.
14+
1315
## Usage
1416

1517
### Maven
@@ -32,11 +34,12 @@ implementation 'com.github.vertical-blank:sql-formatter:1.0'
3234

3335
You can easily use `com.github.vertical_blank.sqlformatter.SqlFormatter` :
3436

35-
```
37+
```java
3638
SqlFormatter.format("SELECT * FROM table1")
3739
```
3840

3941
This will output:
42+
4043
```sql
4144
SELECT
4245
*
@@ -71,6 +74,7 @@ SqlFormatter.format("SELECT * FROM table1", " ");
7174
```
7275

7376
This will output:
77+
7478
```sql
7579
SELECT
7680
*

demo/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
*.h
3+
*.so
4+
*.jar
5+
*.class

demo/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
# Demo
3+
4+
## How to build
5+
6+
### Download jar
7+
8+
```bash
9+
wget http://central.maven.org/maven2/com/github/vertical-blank/sql-formatter/1.0/sql-formatter-1.0.jar
10+
```
11+
12+
### compile java
13+
14+
```bash
15+
path/to/graalvm/bin/javac src/main/java/com/github/vertical_blank/sqlformatter/SqlFormatterDemo.java -cp ./sql-formatter-1.0.jar
16+
```
17+
18+
### generate shared object
19+
20+
```bash
21+
path/to/graalvm/bin/native-image --shared -H:Name=sqlformatterdemo -cp src/main/java/:sql-formatter-1.0.jar
22+
```
23+
24+
### setup javascript
25+
26+
Note: Python 2.7 is requied by node-gyp.
27+
28+
```bash
29+
npm i
30+
cp sqlformatterdemo.so js/
31+
```
32+
33+
## deploy to google cloud function
34+
35+
```bash
36+
gcloud functions deploy format_sql --runtime nodejs8 --entry-point handler --trigger-http
37+
```

demo/js/.gcloudignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# This file specifies files that are *not* uploaded to Google Cloud Platform
2+
# using gcloud. It follows the same syntax as .gitignore, with the addition of
3+
# "#!include" directives (which insert the entries of the given .gitignore-style
4+
# file at that point).
5+
#
6+
# For more information, run:
7+
# $ gcloud topic gcloudignore
8+
#
9+
.gcloudignore
10+
# If you would like to upload your .git directory, .gitignore file or files
11+
# from your .gitignore file, remove the corresponding line
12+
# below:
13+
.git
14+
.gitignore

demo/js/index.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const ref = require('ref');
2+
const ffi = require('ffi');
3+
4+
const libJava = ffi.Library(__dirname + '/sqlformatterdemo', {
5+
graal_create_isolate: [
6+
ref.types.int, [
7+
ref.refType(ref.types.void),
8+
ref.refType(ref.types.void),
9+
ref.refType(ref.refType(ref.types.void))
10+
]],
11+
graal_tear_down_isolate: [
12+
ref.types.int, [
13+
ref.refType(ref.types.void)]
14+
],
15+
hello: [
16+
ref.types.CString,
17+
[ref.refType(ref.types.void)]],
18+
format_sql: [
19+
ref.types.CString, [
20+
ref.refType(ref.types.void),
21+
ref.refType(ref.types.CString)
22+
]],
23+
})
24+
25+
/**
26+
* HTTP Cloud Function.
27+
*
28+
* @param {Object} req Cloud Function request context.
29+
* @param {Object} res Cloud Function response context.
30+
*/
31+
exports.handler = (req, res) => {
32+
const p_graal_isolatethread_t = ref.alloc(ref.refType(ref.types.void))
33+
34+
const rc = libJava.graal_create_isolate(ref.NULL, ref.NULL, p_graal_isolatethread_t)
35+
if (rc !== 0) {
36+
res.header('Access-Control-Allow-Origin', "*");
37+
res.header('Access-Control-Allow-Headers', "Origin, X-Requested-With, Content-Type, Accept");
38+
res.send('error on isolate creation or attach')
39+
return;
40+
}
41+
if (req.method !== 'POST') {
42+
const hello = libJava.hello(ref.deref(p_graal_isolatethread_t))
43+
res.header('Access-Control-Allow-Origin', "*");
44+
res.header('Access-Control-Allow-Headers', "Origin, X-Requested-With, Content-Type, Accept");
45+
res.send(hello);
46+
} else {
47+
const formatted = libJava.format_sql(ref.deref(p_graal_isolatethread_t), ref.allocCString(req.rawBody.toString()))
48+
res.header('Access-Control-Allow-Origin', "*");
49+
res.header('Access-Control-Allow-Headers', "Origin, X-Requested-With, Content-Type, Accept");
50+
res.send(formatted);
51+
}
52+
53+
libJava.graal_tear_down_isolate(ref.deref(p_graal_isolatethread_t));
54+
};

demo/js/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "google-cloud-function-node",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"dependencies": {
12+
"ffi": "^2.3.0"
13+
}
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.github.vertical_blank.sqlformatter;
2+
3+
import org.graalvm.nativeimage.IsolateThread;
4+
import org.graalvm.nativeimage.c.function.CEntryPoint;
5+
import org.graalvm.nativeimage.c.type.CTypeConversion;
6+
import org.graalvm.nativeimage.c.type.CCharPointer;
7+
8+
public final class SqlFormatterDemo {
9+
10+
@CEntryPoint(name = "hello")
11+
static CCharPointer hello(IsolateThread thread) {
12+
return CTypeConversion.toCString("hello from native lib").get();
13+
}
14+
15+
@CEntryPoint(name = "format_sql")
16+
static CCharPointer formatSql(IsolateThread thread, CCharPointer sql) {
17+
return CTypeConversion.toCString(SqlFormatter.format(CTypeConversion.toJavaString(sql))).get();
18+
}
19+
20+
}

0 commit comments

Comments
 (0)