Skip to content

Commit d2cf83a

Browse files
authored
Merge pull request #4 from griddb/2.2.0-rc
Add SQL Interface
2 parents d9c4750 + be72e59 commit d2cf83a

12 files changed

Lines changed: 695 additions & 3 deletions

File tree

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ GridDB WebAPI is developed using GridDB Java Client and [Spring Boot](https://sp
88

99
Building of the library and execution of the sample programs have been checked in the following environment.
1010
- OS: CentOS 7.5(x64)
11-
- [GridDB Server](https://github.com/griddb/griddb_nosql): 4.2
11+
- [GridDB Server](https://github.com/griddb/griddb): 4.5
1212

1313
## QuickStart
1414

@@ -69,7 +69,7 @@ GridDB Server need to be started in advance.
6969
--> {"columns":[{"name":"col1","type":"STRING"},{"name":"col2","type":"INTEGER"},
7070
{"name":"col3","type":"BOOL"}],"rows":[["value",1,true]],"offset":0,"limit":1000,"total":1}
7171

72-
4. Query
72+
4. Query with TQL
7373

7474
#Request http://[host]:[port]/griddb/v2/[clusterName]/dbs/public/tql
7575
curl -X POST --basic -u admin:admin -H "Content-type:application/json" -d
@@ -78,6 +78,15 @@ GridDB Server need to be started in advance.
7878
--> [{"columns":[{"name":"col1","type":"STRING"},{"name":"col2","type":"INTEGER"},
7979
{"name":"col3","type":"BOOL"}],"results":[["value",1,true]],"offset":0,"limit":1000000,"total":1}]
8080

81+
5. Query with SQL
82+
83+
#Request http://[host]:[port]/griddb/v2/[clusterName]/dbs/public/sql
84+
curl -X POST -u admin:admin -H "Content-type:application/json; charset=UTF-8"
85+
-d '[{"type":"sql-select","stmt":"select * from test"}]'
86+
http://127.0.0.1:8010/griddb/v2/mycluster/dbs/public/sql
87+
--> [{"columns":[{"name":"col1","type":"STRING"},{"name":"col2","type":"INTEGER"},
88+
{"name":"col3","type":"BOOL"}],"results":[["value",1,true]]}]
89+
8190
Please refer to [WebAPI Specification](WebAPISpecification.md).
8291

8392
## Community

WebAPISpecification.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Port number is a parameter set at griddb_webapi.properties.
1616
|F) delete rows|DELETE|/:cluster/dbs/public/containers/:container/rows|
1717
|G) create container|POST|/:cluster/dbs/public/containers|
1818
|H) delete containers|DELETE|/:cluster/dbs/public/containers|
19+
|I) execute multiple SQLs|POST|/:cluster/dbs/public/sql|
1920

2021
The :cluster (the :container) means cluster name (container name).
2122

@@ -147,3 +148,21 @@ Example:
147148
"container2",
148149
"container3"
149150
]
151+
152+
### I) Body for executing multiple SQLs
153+
154+
Body:
155+
request of SQLs
156+
157+
Example:
158+
159+
[
160+
{"type" : "sql-select", "stmt" : "select * from container1"},
161+
{"type" : "sql-select", "stmt" : "select column1 from container 2 where column1>=0"},
162+
{"type" : "sql-select", "stmt" : "select column2, column3 from container3 order by column1 desc"}
163+
]
164+
165+
|Name|Type|Description|Value|
166+
|---|---|---|---|
167+
|/type|string|Type of query statement|"sql-select"|
168+
|stmt|string|SQL SELECT statement||

webapi-ce/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ group = 'com.toshiba.mwcloud.gs.tools'
1919
version = '2.1.0'
2020
sourceCompatibility = 1.8
2121

22-
def gridstoreVersion = '4.0.0'
22+
def gridstoreVersion = '4.5.0'
23+
def gridstoreJdbcVersion = '4.5.0.1'
2324

2425
repositories {
2526
mavenCentral()
@@ -33,6 +34,7 @@ dependencies {
3334

3435
compile group: 'com.mashape.unirest', name: 'unirest-java', version: '1.4.9'
3536
compile group: 'com.github.griddb', name: 'gridstore', version: "${gridstoreVersion}"
37+
compile group: 'com.github.griddb', name: 'gridstore-jdbc', version: "${gridstoreJdbcVersion}"
3638

3739
compile project(":griddb-tools-common")
3840
}

webapi-ce/src/main/java/com/toshiba/mwcloud/gs/tools/webapi/controller/WebAPIController.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
import com.toshiba.mwcloud.gs.tools.webapi.dto.GWQueryParams;
5050
import com.toshiba.mwcloud.gs.tools.webapi.dto.GWSortCondition;
5151
import com.toshiba.mwcloud.gs.tools.webapi.dto.GWTQLInput;
52+
import com.toshiba.mwcloud.gs.tools.webapi.dto.GWSQLInput;
53+
import com.toshiba.mwcloud.gs.tools.webapi.dto.GWSQLOutput;
5254
import com.toshiba.mwcloud.gs.tools.webapi.exception.GWBadRequestException;
5355
import com.toshiba.mwcloud.gs.tools.webapi.exception.GWException;
5456
import com.toshiba.mwcloud.gs.tools.webapi.service.WebAPIService;
@@ -434,4 +436,40 @@ public ResponseEntity<?> deleteContainers(
434436
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
435437
}
436438

439+
/**
440+
* [CE] Execute multiple select-SQLs.
441+
*
442+
* <br>
443+
* <br>
444+
* <b>Processing flow:</b>
445+
* <ol>
446+
* <li>Execute SQLs by calling WebAPIService.executeSQLs(String, String,
447+
* String, List) function.</li>
448+
* </ol>
449+
*
450+
* @param authorization
451+
* basic authentication
452+
* @param cluster
453+
* name of cluster
454+
* @param database
455+
* name of database
456+
* @param listSQLInput
457+
* a {@link List} of {@link GWSQLInput}
458+
* @return a {@link ResponseEntity} object with body is a {@link List} of
459+
* {@link GWSQLOutput} and status {@link HttpStatus#OK}
460+
* @throws GSException
461+
* internal server exception
462+
* {@link HttpStatus#INTERNAL_SERVER_ERROR}
463+
* @throws SQLException a {@link SQLException}
464+
* @throws UnsupportedEncodingException a {@link UnsupportedEncodingException}
465+
*/
466+
@RequestMapping(value = "{cluster}/dbs/{database}/sql", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
467+
public ResponseEntity<?> executeSQLs(@RequestHeader(name = "Authorization", required = false) String authorization,
468+
@PathVariable("cluster") String cluster, @PathVariable("database") String database,
469+
@RequestBody List<GWSQLInput> listSQLInput) throws GSException, SQLException, UnsupportedEncodingException {
470+
471+
List<GWSQLOutput> gwOutput = webAPIServiceImpl.executeSQLs(authorization, cluster, database, listSQLInput);
472+
return new ResponseEntity<>(gwOutput, HttpStatus.OK);
473+
}
474+
437475
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
Copyright (c) 2019 TOSHIBA Digital Solutions Corporation.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package com.toshiba.mwcloud.gs.tools.webapi.dto;
18+
19+
public class GWSQLColumnInfo {
20+
21+
/**
22+
* Name of SQL column
23+
*/
24+
private String name;
25+
26+
/**
27+
* Type of SQL column
28+
*/
29+
private String type;
30+
31+
/**
32+
* Constructor for {@link GWColumnInfo}
33+
*/
34+
public GWSQLColumnInfo(){}
35+
36+
/**
37+
* Constructor for {@link GWColumnInfo}
38+
*
39+
* @param name Name of SQL column
40+
* @param type Type of SQL column
41+
*/
42+
public GWSQLColumnInfo(String name, String type){
43+
this.name = name;
44+
this.type = type;
45+
}
46+
47+
/**
48+
* Get name of SQL column
49+
*
50+
* @return name of SQL column
51+
*/
52+
public String getName() {
53+
return name;
54+
}
55+
56+
/**
57+
* Set name for SQL column
58+
*
59+
* @param name name of SQL column
60+
*/
61+
public void setName(String name) {
62+
this.name = name;
63+
}
64+
65+
/**
66+
* Get type of SQL column
67+
*
68+
* @return type of SQL column
69+
*/
70+
public String getType() {
71+
return type;
72+
}
73+
74+
/**
75+
* Set type for SQL column
76+
*
77+
* @param type type of SQL column
78+
*/
79+
public void setType(String type) {
80+
this.type = type;
81+
}
82+
83+
84+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Copyright (c) 2019 TOSHIBA Digital Solutions Corporation.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package com.toshiba.mwcloud.gs.tools.webapi.dto;
18+
19+
public class GWSQLInput {
20+
21+
/**
22+
* Type of SQL
23+
*/
24+
private String type;
25+
26+
/**
27+
* Statement of SQL
28+
*/
29+
private String stmt;
30+
31+
/**
32+
* Get type of SQL
33+
*
34+
* @return type of SQL
35+
*/
36+
public String getType() {
37+
return type;
38+
}
39+
40+
/**
41+
* Set type for SQL
42+
*
43+
* @param type type of SQL
44+
*/
45+
public void setType(String type) {
46+
this.type = type;
47+
}
48+
49+
/**
50+
* Get the statement of SQL
51+
*
52+
* @return statement of SQL
53+
*/
54+
public String getStmt() {
55+
return stmt;
56+
}
57+
58+
/**
59+
* Set the statement for SQL
60+
*
61+
* @param stmt statement of SQL
62+
*/
63+
public void setStmt(String stmt) {
64+
this.stmt = stmt;
65+
}
66+
67+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
Copyright (c) 2019 TOSHIBA Digital Solutions Corporation.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package com.toshiba.mwcloud.gs.tools.webapi.dto;
18+
19+
import java.util.List;
20+
21+
public class GWSQLOutput {
22+
23+
/**
24+
* List of column information
25+
*/
26+
private List<GWSQLColumnInfo> columns;
27+
28+
/**
29+
* List of rows
30+
*/
31+
private List<List<Object>> results;
32+
33+
/**
34+
* Get list of column information
35+
*
36+
* @return list of column information
37+
*/
38+
public List<GWSQLColumnInfo> getColumns() {
39+
return columns;
40+
}
41+
42+
/**
43+
* Set list of column information
44+
*
45+
* @param columns
46+
* list of column information
47+
*/
48+
public void setColumns(List<GWSQLColumnInfo> columns) {
49+
this.columns = columns;
50+
}
51+
52+
/**
53+
* Get list of rows
54+
*
55+
* @return list of rows
56+
*/
57+
public List<List<Object>> getResults() {
58+
return results;
59+
}
60+
61+
/**
62+
* Set result
63+
*
64+
* @param results the result of SQL
65+
*/
66+
public void setResults(List<List<Object>> results) {
67+
this.results = results;
68+
}
69+
70+
}

webapi-ce/src/main/java/com/toshiba/mwcloud/gs/tools/webapi/service/WebAPIService.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import com.toshiba.mwcloud.gs.tools.webapi.dto.GWPutRowOutput;
3333
import com.toshiba.mwcloud.gs.tools.webapi.dto.GWQueryOutput;
3434
import com.toshiba.mwcloud.gs.tools.webapi.dto.GWQueryParams;
35+
import com.toshiba.mwcloud.gs.tools.webapi.dto.GWSQLInput;
36+
import com.toshiba.mwcloud.gs.tools.webapi.dto.GWSQLOutput;
3537
import com.toshiba.mwcloud.gs.tools.webapi.dto.GWSortCondition;
3638
import com.toshiba.mwcloud.gs.tools.webapi.dto.GWTQLInput;
3739
import com.toshiba.mwcloud.gs.tools.webapi.dto.GWTQLOutput;
@@ -265,4 +267,28 @@ public void deleteRows(String authorization, String cluster, String database, St
265267
*/
266268
public void createContainer(String authorization, String cluster, String database, GWContainerInfo gwContainerInfo) throws GSException;
267269

270+
/**
271+
* Execute multiple SQLs.
272+
*
273+
* <br><br>
274+
* <b>Processing flow:</b>
275+
* <ol>
276+
* <li>Check authorization</li>
277+
* <li>Call function {@link GWUser#getUserfromAuthorization(String)} to get {@link GWUser} from authorization</li>
278+
* <li>Call function {@link GridStoreUtils#getGridStore(String, String, String, String)} to get the information of the target cluster</li>
279+
* <li>Call function checkAuthentication(GridStore) to check the authentication</li>
280+
* <li>If the size of {@code listSQLInput} is larger than the maximum of number of SQLs that can be executed, throw a {@link GWBadRequestException}</li>
281+
* <li>For each {@link GWSQLInput} in the {@code listSQLInput}, call function {@code executeSQL(String, String, String, String, GWSQLOutput)} to execute each SQL</li>
282+
* <li>Return a list of {@link GWSQLOutput}</li>
283+
* </ol>
284+
* @param authorization basic authentication
285+
* @param cluster name of cluster
286+
* @param database name of database
287+
* @param listSQLInput a {@link List} of {@link GWSQLInput}
288+
* @return a {@link List} of {@link GWSQLOutput}
289+
* @throws GSException internal server exception
290+
* @throws SQLException a {@link SQLException}
291+
* @throws UnsupportedEncodingException a {@link UnsupportedEncodingException}
292+
*/
293+
public List<GWSQLOutput> executeSQLs(String authorization, String cluster, String database, List<GWSQLInput> listSQLInput) throws GSException, SQLException, UnsupportedEncodingException;
268294
}

0 commit comments

Comments
 (0)