Commit 553f8934 by inrgihc

支持postgres数据库

parent 727a398b
......@@ -138,11 +138,10 @@ curl -k -sSL https://gitee.com/inrgihc/sqlrest/attach_files/2241027/download -o
(3) 物理机方式部署
- 步骤1:准备好一个MySQL5.7+的数据库,假设连接地址如下:
- 步骤1:准备好一个MySQL5.7+/PostgreSQL11+的数据库
| mysql的host地址 | mysql的端口号 | mysql的账号 | mysql的密码 |
| :------| :------ | :------ | :------ |
| 127.0.0.1 | 3306 | root | 123456 |
> 当使用MySQL数据库时,config.ini里的DB_TYPE配置mysql,并需要配置 MYSQLDB_ 前缀的参数
> 当使用PostgreSQL数据库时,config.ini里的DB_TYPE配置postgres,并需要配置 PGDB_ 前缀的参数
- 步骤2:修改sqlrest-relase-x.x.x/conf/config.ini配置文件
......@@ -162,20 +161,30 @@ EXECUTOR_PORT=8092
GATEWAY_PORT=8091
# mysql的host地址
MYSQLDB_HOST=127.0.0.1
# 数据库类型:mysql或postgres
DB_TYPE=mysql
# mysql的host地址
MYSQLDB_HOST=192.168.31.57
# mysql的端口号
MYSQLDB_PORT=3306
# mysql的库名
MYSQLDB_NAME=sqlrest
# mysql的账号
MYSQLDB_USERNAME=root
# mysql的密码
MYSQLDB_PASSWORD=123456
# pgsql的host地址
PGDB_HOST=192.168.31.57
# pgsql的端口号
PGDB_PORT=5432
# pgsql的库名
PGDB_NAME=sqlrest
# pgsql的账号
PGDB_USERNAME=postgres
# pgsql的密码
PGDB_PASSWORD=123456
```
>sqlrest的缓存支持使用分布式的hazelcast或者redis,在conf/{manager,gateway,executor}/目录下的application.yml配
......
......@@ -23,6 +23,7 @@ services:
image: registry.cn-hangzhou.aliyuncs.com/inrgihc/sqlrest-manager:latest
network_mode: host
environment:
DB_TYPE: mysql
MYSQLDB_HOST: localhost
MYSQLDB_PORT: 3306
MYSQLDB_USERNAME: tangyibo
......@@ -44,6 +45,7 @@ services:
image: registry.cn-hangzhou.aliyuncs.com/inrgihc/sqlrest-executor:latest
network_mode: host
environment:
DB_TYPE: mysql
MYSQLDB_HOST: localhost
MYSQLDB_PORT: 3306
MYSQLDB_USERNAME: tangyibo
......@@ -65,6 +67,7 @@ services:
image: registry.cn-hangzhou.aliyuncs.com/inrgihc/sqlrest-gateway:latest
network_mode: host
environment:
DB_TYPE: mysql
MYSQLDB_HOST: localhost
MYSQLDB_PORT: 3306
MYSQLDB_USERNAME: tangyibo
......
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${MYSQLDB_HOST}:${MYSQLDB_PORT}/${MYSQLDB_NAME}?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=UTF8&autoReconnect=true&useSSL=false&allowMultiQueries=true&failOverReadOnly=false&connectTimeout=30000
username: ${MYSQLDB_USERNAME}
password: ${MYSQLDB_PASSWORD}
validation-query: SELECT 1
test-on-borrow: true
spring:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://${PGDB_HOST}:${PGDB_PORT}/${PGDB_NAME}?currentSchema=public
username: ${PGDB_USERNAME}
password: ${PGDB_PASSWORD}
validation-query: SELECT 1
test-on-borrow: true
\ No newline at end of file
server:
port: ${EXECUTOR_PORT}
spring:
profiles:
include: ${DB_TYPE:mysql}
application:
name: sqlrest-executor
main:
allow-bean-definition-overriding: false
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${MYSQLDB_HOST}:${MYSQLDB_PORT}/${MYSQLDB_NAME}?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=UTF8&autoReconnect=true&useSSL=false&allowMultiQueries=true&failOverReadOnly=false&connectTimeout=30000
username: ${MYSQLDB_USERNAME}
password: ${MYSQLDB_PASSWORD}
validation-query: SELECT 1
test-on-borrow: true
sqlrest:
cache:
......
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${MYSQLDB_HOST}:${MYSQLDB_PORT}/${MYSQLDB_NAME}?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=UTF8&autoReconnect=true&useSSL=false&allowMultiQueries=true&failOverReadOnly=false&connectTimeout=30000
username: ${MYSQLDB_USERNAME}
password: ${MYSQLDB_PASSWORD}
validation-query: SELECT 1
test-on-borrow: true
spring:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://${PGDB_HOST}:${PGDB_PORT}/${PGDB_NAME}?currentSchema=public
username: ${PGDB_USERNAME}
password: ${PGDB_PASSWORD}
validation-query: SELECT 1
test-on-borrow: true
\ No newline at end of file
server:
port: ${GATEWAY_PORT}
spring:
profiles:
include: ${DB_TYPE:mysql}
application:
name: sqlrest-gateway
main:
web-application-type: reactive
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${MYSQLDB_HOST}:${MYSQLDB_PORT}/${MYSQLDB_NAME}?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=UTF8&autoReconnect=true&useSSL=false&allowMultiQueries=true&failOverReadOnly=false&connectTimeout=30000
username: ${MYSQLDB_USERNAME}
password: ${MYSQLDB_PASSWORD}
validation-query: SELECT 1
test-on-borrow: true
cloud:
gateway:
routes:
......
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${MYSQLDB_HOST}:${MYSQLDB_PORT}/${MYSQLDB_NAME}?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=UTF8&autoReconnect=true&useSSL=false&allowMultiQueries=true&failOverReadOnly=false&connectTimeout=30000
username: ${MYSQLDB_USERNAME}
password: ${MYSQLDB_PASSWORD}
validation-query: SELECT 1
test-on-borrow: true
liquibase:
enabled: true
change-log: classpath:db/changelog/db.changelog-master.yaml
database-change-log-table: SQLREST_DB_CHANGE_LOG_RECORD
database-change-log-lock-table: SQLREST_DB_CHANGE_LOG_LOCK
spring:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://${PGDB_HOST}:${PGDB_PORT}/${PGDB_NAME}?currentSchema=public
username: ${PGDB_USERNAME}
password: ${PGDB_PASSWORD}
validation-query: SELECT 1
test-on-borrow: true
liquibase:
enabled: true
change-log: classpath:pg/changelog/db.changelog-master.yaml
database-change-log-table: SQLREST_DB_CHANGE_LOG_RECORD
database-change-log-lock-table: SQLREST_DB_CHANGE_LOG_LOCK
\ No newline at end of file
server:
port: ${MANAGER_PORT}
spring:
profiles:
include: ${DB_TYPE:mysql}
application:
name: sqlrest-manager
mvc:
throw-exception-if-no-handler-found: false
static-path-pattern: /**
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${MYSQLDB_HOST}:${MYSQLDB_PORT}/${MYSQLDB_NAME}?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=UTF8&autoReconnect=true&useSSL=false&allowMultiQueries=true&failOverReadOnly=false&connectTimeout=30000
username: ${MYSQLDB_USERNAME}
password: ${MYSQLDB_PASSWORD}
validation-query: SELECT 1
test-on-borrow: true
liquibase:
enabled: true
change-log: classpath:db/changelog/db.changelog-master.yaml
database-change-log-table: SQLREST_DB_CHANGE_LOG_RECORD
database-change-log-lock-table: SQLREST_DB_CHANGE_LOG_LOCK
sqlrest:
cache:
......
......@@ -39,11 +39,17 @@ export MANAGER_HOST=$(grep -oP '(?<=^MANAGER_HOST=).*' ${APP_CONF_PATH}/config.i
export MANAGER_PORT=$(grep -oP '(?<=^MANAGER_PORT=).*' ${APP_CONF_PATH}/config.ini)
export EXECUTOR_PORT=$(grep -oP '(?<=^EXECUTOR_PORT=).*' ${APP_CONF_PATH}/config.ini)
export GATEWAY_PORT=$(grep -oP '(?<=^GATEWAY_PORT=).*' ${APP_CONF_PATH}/config.ini)
export DB_TYPE=$(grep -oP '(?<=^DB_TYPE=).*' ${APP_CONF_PATH}/config.ini)
export MYSQLDB_HOST=$(grep -oP '(?<=^MYSQLDB_HOST=).*' ${APP_CONF_PATH}/config.ini)
export MYSQLDB_PORT=$(grep -oP '(?<=^MYSQLDB_PORT=).*' ${APP_CONF_PATH}/config.ini)
export MYSQLDB_NAME=$(grep -oP '(?<=^MYSQLDB_NAME=).*' ${APP_CONF_PATH}/config.ini)
export MYSQLDB_USERNAME=$(grep -oP '(?<=^MYSQLDB_USERNAME=).*' ${APP_CONF_PATH}/config.ini)
export MYSQLDB_PASSWORD=$(grep -oP '(?<=^MYSQLDB_PASSWORD=).*' ${APP_CONF_PATH}/config.ini)
export PGDB_HOST=$(grep -oP '(?<=^PGDB_HOST=).*' ${APP_CONF_PATH}/config.ini)
export PGDB_PORT=$(grep -oP '(?<=^PGDB_PORT=).*' ${APP_CONF_PATH}/config.ini)
export PGDB_NAME=$(grep -oP '(?<=^PGDB_NAME=).*' ${APP_CONF_PATH}/config.ini)
export PGDB_USERNAME=$(grep -oP '(?<=^PGDB_USERNAME=).*' ${APP_CONF_PATH}/config.ini)
export PGDB_PASSWORD=$(grep -oP '(?<=^PGDB_PASSWORD=).*' ${APP_CONF_PATH}/config.ini)
# JVM参数可以在这里设置
JVMFLAGS="-server -Xms1024m -Xmx1024m -Xmn1024m -XX:+DisableExplicitGC -Djava.awt.headless=true -Dfile.encoding=UTF-8 "
......
......@@ -12,18 +12,27 @@ EXECUTOR_PORT=8092
GATEWAY_PORT=8091
# 数据库类型:mysql或postgres
DB_TYPE=mysql
# mysql的host地址
MYSQLDB_HOST=192.168.31.57
# mysql的端口号
MYSQLDB_PORT=3306
# mysql的库名
MYSQLDB_NAME=sqlrest
# mysql的账号
MYSQLDB_USERNAME=root
# mysql的密码
MYSQLDB_PASSWORD=123456
# pgsql的host地址
PGDB_HOST=192.168.31.57
# pgsql的端口号
PGDB_PORT=5432
# pgsql的库名
PGDB_NAME=sqlrest
# pgsql的账号
PGDB_USERNAME=postgres
# pgsql的密码
PGDB_PASSWORD=123456
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${MYSQLDB_HOST}:${MYSQLDB_PORT}/${MYSQLDB_NAME}?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=UTF8&autoReconnect=true&useSSL=false&allowMultiQueries=true&failOverReadOnly=false&connectTimeout=30000&serverTimezone=Asia/Shanghai
username: ${MYSQLDB_USERNAME}
password: ${MYSQLDB_PASSWORD}
validation-query: SELECT 1
test-on-borrow: true
\ No newline at end of file
spring:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://${PGDB_HOST}:${PGDB_PORT}/${PGDB_NAME}?currentSchema=public
username: ${PGDB_USERNAME}
password: ${PGDB_PASSWORD}
validation-query: SELECT 1
test-on-borrow: true
\ No newline at end of file
server:
port: ${EXECUTOR_PORT}
spring:
profiles:
include: ${DB_TYPE}
application:
name: sqlrest-executor
main:
allow-bean-definition-overriding: false
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${MYSQLDB_HOST}:${MYSQLDB_PORT}/${MYSQLDB_NAME}?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=UTF8&autoReconnect=true&useSSL=false&allowMultiQueries=true&failOverReadOnly=false&connectTimeout=30000&serverTimezone=Asia/Shanghai
username: ${MYSQLDB_USERNAME}
password: ${MYSQLDB_PASSWORD}
validation-query: SELECT 1
test-on-borrow: true
sqlrest:
cache:
......
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${MYSQLDB_HOST}:${MYSQLDB_PORT}/${MYSQLDB_NAME}?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=UTF8&autoReconnect=true&useSSL=false&allowMultiQueries=true&failOverReadOnly=false&connectTimeout=30000&serverTimezone=Asia/Shanghai
username: ${MYSQLDB_USERNAME}
password: ${MYSQLDB_PASSWORD}
validation-query: SELECT 1
test-on-borrow: true
\ No newline at end of file
spring:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://${PGDB_HOST}:${PGDB_PORT}/${PGDB_NAME}?currentSchema=public
username: ${PGDB_USERNAME}
password: ${PGDB_PASSWORD}
validation-query: SELECT 1
test-on-borrow: true
\ No newline at end of file
server:
port: ${GATEWAY_PORT}
spring:
profiles:
include: ${DB_TYPE}
application:
name: sqlrest-gateway
main:
web-application-type: reactive
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${MYSQLDB_HOST}:${MYSQLDB_PORT}/${MYSQLDB_NAME}?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=UTF8&autoReconnect=true&useSSL=false&allowMultiQueries=true&failOverReadOnly=false&connectTimeout=30000&serverTimezone=Asia/Shanghai
username: ${MYSQLDB_USERNAME}
password: ${MYSQLDB_PASSWORD}
validation-query: SELECT 1
test-on-borrow: true
cloud:
gateway:
routes:
......
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${MYSQLDB_HOST}:${MYSQLDB_PORT}/${MYSQLDB_NAME}?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=UTF8&autoReconnect=true&useSSL=false&allowMultiQueries=true&failOverReadOnly=false&connectTimeout=30000&serverTimezone=Asia/Shanghai
username: ${MYSQLDB_USERNAME}
password: ${MYSQLDB_PASSWORD}
validation-query: SELECT 1
test-on-borrow: true
liquibase:
enabled: true
change-log: classpath:db/changelog/db.changelog-master.yaml
database-change-log-table: SQLREST_DB_CHANGE_LOG_RECORD
database-change-log-lock-table: SQLREST_DB_CHANGE_LOG_LOCK
\ No newline at end of file
spring:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://${PGDB_HOST}:${PGDB_PORT}/${PGDB_NAME}?currentSchema=public
username: ${PGDB_USERNAME}
password: ${PGDB_PASSWORD}
validation-query: SELECT 1
test-on-borrow: true
liquibase:
enabled: true
change-log: classpath:pg/changelog/db.changelog-master.yaml
database-change-log-table: SQLREST_DB_CHANGE_LOG_RECORD
database-change-log-lock-table: SQLREST_DB_CHANGE_LOG_LOCK
\ No newline at end of file
server:
port: ${MANAGER_PORT}
spring:
profiles:
include: ${DB_TYPE}
application:
name: sqlrest-manager
mvc:
throw-exception-if-no-handler-found: false
static-path-pattern: /**
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${MYSQLDB_HOST}:${MYSQLDB_PORT}/${MYSQLDB_NAME}?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=UTF8&autoReconnect=true&useSSL=false&allowMultiQueries=true&failOverReadOnly=false&connectTimeout=30000&serverTimezone=Asia/Shanghai
username: ${MYSQLDB_USERNAME}
password: ${MYSQLDB_PASSWORD}
validation-query: SELECT 1
test-on-borrow: true
liquibase:
enabled: true
change-log: classpath:db/changelog/db.changelog-master.yaml
database-change-log-table: SQLREST_DB_CHANGE_LOG_RECORD
database-change-log-lock-table: SQLREST_DB_CHANGE_LOG_LOCK
sqlrest:
cache:
......
databaseChangeLog:
- include:
file: classpath:pg/changelog/log-v1.0.1.yaml
- include:
file: classpath:pg/changelog/log-v1.0.2.yaml
- include:
file: classpath:pg/changelog/log-v1.1.1.yaml
- include:
file: classpath:pg/changelog/log-v1.1.2.yaml
- include:
file: classpath:pg/changelog/log-v1.2.1.yaml
- include:
file: classpath:pg/changelog/log-v1.2.2.yaml
\ No newline at end of file
databaseChangeLog:
- changeSet:
id: 1.0.1
author: sqlrest
runOnChange: false
changes:
- sqlFile:
encoding: UTF-8
path: pg/migration/V1_0_1__system-ddl.sql
databaseChangeLog:
- changeSet:
id: 1.0.2
author: sqlrest
runOnChange: false
changes:
- sqlFile:
encoding: UTF-8
path: pg/migration/V1_0_2__system-dml.sql
databaseChangeLog:
- changeSet:
id: 1.1.1
author: sqlrest
runOnChange: false
changes:
- sqlFile:
encoding: UTF-8
path: pg/migration/V1_1_1__system-ddl.sql
databaseChangeLog:
- changeSet:
id: 1.1.2
author: sqlrest
runOnChange: false
changes:
- sqlFile:
encoding: UTF-8
path: pg/migration/V1_1_2__system-ddl.sql
databaseChangeLog:
- changeSet:
id: 1.2.1
author: sqlrest
runOnChange: false
changes:
- sqlFile:
encoding: UTF-8
path: pg/migration/V1_2_1__system-ddl.sql
databaseChangeLog:
- changeSet:
id: 1.2.2
author: sqlrest
runOnChange: false
changes:
- sqlFile:
encoding: UTF-8
path: pg/migration/V1_2_2__system-ddl.sql
CREATE TABLE SQLREST_SYSTEM_USER (
"id" bigserial not null,
"username" varchar(255) not null,
"password" varchar(128) not null,
"salt" varchar(128) not null,
"real_name" varchar(255) not null default '',
"email" varchar(255) not null default '',
"address" varchar(255) not null default '',
"locked" boolean not null default false,
"create_time" timestamp(6) not null default (CURRENT_TIMESTAMP(0))::timestamp(0) without time zone,
"update_time" timestamp(6) not null default (CURRENT_TIMESTAMP(0))::timestamp(0) without time zone,
PRIMARY KEY ("id")
);
CREATE UNIQUE INDEX "SQLREST_SYSTEM_USER_USERNAME_IDX" ON SQLREST_SYSTEM_USER("username");
CREATE TABLE SQLREST_DATASOURCE (
"id" bigserial not null,
"name" varchar(200) not null,
"type" varchar(200) not null,
"version" varchar(255) not null,
"driver" varchar(200) not null,
"url" text ,
"username" varchar(200) not null default '',
"password" varchar(200) not null default '',
"create_time" timestamp(6) not null default (CURRENT_TIMESTAMP(0))::timestamp(0) without time zone,
"update_time" timestamp(6) not null default (CURRENT_TIMESTAMP(0))::timestamp(0) without time zone,
primary key ("id")
);
CREATE UNIQUE INDEX "SQLREST_DATASOURCE_NAME_IDX" ON SQLREST_DATASOURCE("name");
CREATE TABLE SQLREST_API_GROUP
(
"id" bigserial not null,
"name" varchar(255) not null,
"create_time" timestamp(6) not null default (CURRENT_TIMESTAMP(0))::timestamp(0) without time zone,
"update_time" timestamp(6) not null default (CURRENT_TIMESTAMP(0))::timestamp(0) without time zone,
PRIMARY KEY ("id")
);
CREATE UNIQUE INDEX "SQLREST_API_GROUP_NAME_IDX" ON SQLREST_API_GROUP("name");
CREATE TABLE SQLREST_API_MODULE
(
"id" bigserial not null,
"name" varchar(255) not null,
"create_time" timestamp(6) not null default (CURRENT_TIMESTAMP(0))::timestamp(0) without time zone,
"update_time" timestamp(6) not null default (CURRENT_TIMESTAMP(0))::timestamp(0) without time zone,
PRIMARY KEY ("id")
);
CREATE UNIQUE INDEX "SQLREST_API_MODULE_NAME_IDX" ON SQLREST_API_MODULE("name");
CREATE TABLE SQLREST_API_ASSIGNMENT
(
"id" bigserial not null,
"group_id" int8 not null,
"module_id" int8 not null,
"datasource_id" int8 not null,
"name" varchar(255) not null,
"description" varchar(1024) default null,
"method" varchar(16) not null default 'GET',
"path" varchar(255) not null default '',
"params" text null,
"outputs" text null,
"status" boolean not null default false,
"open" boolean not null default false,
"engine" varchar(16) not null default 'SQL',
"response_format" text default null,
"naming_strategy" varchar(16) not null default 'NONE',
"flow_status" boolean not null default false,
"flow_grade" int8 not null,
"flow_count" int8 not null,
"content_type" varchar(50) not null default '',
"create_time" timestamp(6) not null default (CURRENT_TIMESTAMP(0))::timestamp(0) without time zone,
"update_time" timestamp(6) not null default (CURRENT_TIMESTAMP(0))::timestamp(0) without time zone,
PRIMARY KEY ("id"),
foreign key ("group_id") references SQLREST_API_GROUP ("id") on delete cascade on update cascade,
foreign key ("module_id") references SQLREST_API_MODULE ("id") on delete cascade on update cascade,
foreign key ("datasource_id") references SQLREST_DATASOURCE ("id") on delete cascade on update cascade
);
CREATE UNIQUE INDEX "SSQLREST_API_ASSIGNMENT_METHOD_PATH_IDX" ON SQLREST_API_ASSIGNMENT("method","path");
CREATE TABLE SQLREST_API_CONTEXT
(
"id" bigserial not null,
"api_id" int8 not null,
"sql_text" text not null,
primary key ("id"),
foreign key ("api_id") references SQLREST_API_ASSIGNMENT ("id") on delete cascade on update cascade
);
CREATE TABLE SQLREST_FIREWALL_RULES
(
"id" bigserial not null,
"status" varchar(4) not null default 'OFF',
"mode" varchar(16) not null default 'BLACK',
"addresses" text default null,
"create_time" timestamp(6) not null default (CURRENT_TIMESTAMP(0))::timestamp(0) without time zone,
"update_time" timestamp(6) not null default (CURRENT_TIMESTAMP(0))::timestamp(0) without time zone,
PRIMARY KEY ("id")
);
CREATE TABLE SQLREST_APP_CLIENT
(
"id" bigserial not null,
"name" varchar(255) not null,
"description" varchar(1024) default null,
"app_key" varchar(64) not null,
"app_secret" varchar(64) not null,
"expire_duration" varchar(16) not null default 'FOR_EVER',
"expire_at" int8 not null default '0',
"access_token" varchar(64) default null,
"create_time" timestamp(6) not null default (CURRENT_TIMESTAMP(0))::timestamp(0) without time zone,
"update_time" timestamp(6) not null default (CURRENT_TIMESTAMP(0))::timestamp(0) without time zone,
PRIMARY KEY ("id")
);
CREATE UNIQUE INDEX "SQLREST_APP_CLIENT_APP_KEY_IDX" ON SQLREST_APP_CLIENT("app_key");
CREATE TABLE SQLREST_CLIENT_GROUP
(
"id" bigserial not null,
"client_id" int8 not null,
"group_id" int8 not null,
PRIMARY KEY ("id")
);
CREATE UNIQUE INDEX "SQLREST_CLIENT_GROUP_IDX" ON SQLREST_CLIENT_GROUP("client_id","group_id");
CREATE TABLE SQLREST_ACCESS_RECORD
(
"id" bigserial not null,
"path" varchar(255) default null,
"status" int8 default null,
"duration" int8 default null,
"ip_addr" varchar(64) default null,
"user_agent" varchar(255) default null,
"client_key" varchar(64) default null,
"api_id" int8 not null,
"exception" varchar(1024) default null,
"create_time" timestamp(6) not null default (CURRENT_TIMESTAMP(0))::timestamp(0) without time zone,
PRIMARY KEY ("id")
);
CREATE TABLE SQLREST_SYSTEM_PARAM (
"id" bigserial NOT NULL,
"param_key" varchar(128) NOT NULL,
"param_type" varchar(64) NOT NULL,
"param_value" varchar(255) NOT NULL,
PRIMARY KEY ("id")
);
CREATE UNIQUE INDEX "SQLREST_SYSTEM_PARAM_PARAM_KEY_IDX" ON SQLREST_SYSTEM_PARAM("param_key");
INSERT INTO SQLREST_SYSTEM_USER("username","password","salt","real_name","locked","email")
VALUES ('admin', '$2a$10$eUanVjvzV27BBxAb4zuBCugwnngHkRZ7ZB4iI5tdx9ETJ2tnXJJDy', '$2a$10$eUanVjvzV27BBxAb4zuBCu', '管理员', false,'admin@126.com');
INSERT INTO SQLREST_FIREWALL_RULES ("id", "status", "mode")
VALUES ('1', 'OFF', 'BLACK');
INSERT INTO SQLREST_API_GROUP ("name")
VALUES ('默认分组');
INSERT INTO SQLREST_API_MODULE ("name")
VALUES ('默认模块');
INSERT INTO SQLREST_APP_CLIENT ("name", "description", "app_key", "app_secret", "expire_duration", "expire_at", "access_token")
VALUES ('测试', '测试使用', 'test', 'test', 'FOR_EVER', '-1', '9097ac1ab13198dfa4ddb2ecc1079693');
INSERT INTO SQLREST_CLIENT_GROUP ("client_id", "group_id")
VALUES ('1', '1');
INSERT INTO SQLREST_SYSTEM_PARAM ("param_key", "param_type", "param_value") VALUES ('apiDocOpen', 'BOOLEAN', 'true');
INSERT INTO SQLREST_SYSTEM_PARAM ("param_key", "param_type", "param_value") VALUES ('apiDocInfoTitle', 'STRING', '在线接口文档');
INSERT INTO SQLREST_SYSTEM_PARAM ("param_key", "param_type", "param_value") VALUES ('apiDocInfoVersion', 'STRING', '1.0');
INSERT INTO SQLREST_SYSTEM_PARAM ("param_key", "param_type", "param_value") VALUES ('apiDocInfoDescription', 'STRING', 'Swagger在线接口文档');
ALTER TABLE SQLREST_API_ASSIGNMENT ADD COLUMN "cache_key_type" varchar(16) not null default 'NONE';
ALTER TABLE SQLREST_API_ASSIGNMENT ADD COLUMN "cache_key_expr" varchar(255) null;
ALTER TABLE SQLREST_API_ASSIGNMENT ADD COLUMN "cache_expire_seconds" int8 not null default 0;
ALTER TABLE SQLREST_APP_CLIENT ADD COLUMN "token_alive" varchar(16) not null default 'PERIOD';
CREATE INDEX "SQLREST_APP_CLIENT_ACCESS_TOKEN_IDX" ON SQLREST_APP_CLIENT ("access_token");
CREATE TABLE SQLREST_UNIFY_ALARM (
"id" bigserial not null,
"status" varchar(4) not null default 'OFF',
"endpoint" varchar(256) not null,
"content_type" varchar(128) not null,
"input_template" varchar(4096) not null,
"create_time" timestamp(6) not null default (CURRENT_TIMESTAMP(0))::timestamp(0) without time zone,
"update_time" timestamp(6) not null default (CURRENT_TIMESTAMP(0))::timestamp(0) without time zone,
primary key ("id")
);
INSERT INTO SQLREST_UNIFY_ALARM ("id", "status", "endpoint","content_type", "input_template")
VALUES ('1', 'OFF', 'http://127.0.0.1:8000/api/v1/message/send', 'application/json', '{}');
ALTER TABLE SQLREST_API_ASSIGNMENT ADD COLUMN "alarm" boolean not null default false;
CREATE TABLE SQLREST_MCP_CLIENT (
"id" bigserial not null,
"name" varchar(256) not null,
"token" varchar(64) not null,
"create_time" timestamp(6) not null default (CURRENT_TIMESTAMP(0))::timestamp(0) without time zone,
"update_time" timestamp(6) not null default (CURRENT_TIMESTAMP(0))::timestamp(0) without time zone,
primary key ("id")
);
CREATE UNIQUE INDEX "SQLREST_MCP_CLIENT_NAME_IDX" ON SQLREST_MCP_CLIENT("name");
CREATE UNIQUE INDEX "SQLREST_MCP_CLIENT_TOKEN_IDX" ON SQLREST_MCP_CLIENT("token");
CREATE TABLE SQLREST_MCP_TOOL (
"id" bigserial not null,
"name" varchar(256) not null,
"description" varchar(1024) not null,
"api_id" int8 not null,
"create_time" timestamp(6) not null default (CURRENT_TIMESTAMP(0))::timestamp(0) without time zone,
"update_time" timestamp(6) not null default (CURRENT_TIMESTAMP(0))::timestamp(0) without time zone,
primary key ("id"),
FOREIGN KEY ("api_id") REFERENCES SQLREST_API_ASSIGNMENT ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE UNIQUE INDEX "SQLREST_MCP_TOOL_NAME_IDX" ON SQLREST_MCP_TOOL("name");
......@@ -30,6 +30,11 @@
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
......
package com.gitee.sqlrest.persistence.config;
import java.util.Properties;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisDatabaseConfiguration {
@Bean
public DatabaseIdProvider getDatabaseIdProvider() {
DatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
Properties props = new Properties();
props.setProperty("PostgreSQL", "postgresql");
props.setProperty("MySQL", "mysql");
databaseIdProvider.setProperties(props);
return databaseIdProvider;
}
}
......@@ -21,52 +21,102 @@ import org.apache.ibatis.annotations.Select;
public interface AccessRecordMapper extends BaseMapper<AccessRecordEntity> {
@Select("SELECT "
@Select("<script>"
+ "<if test='_databaseId == \"mysql\" '>"
+ "SELECT "
+ " (SELECT count(*) from SQLREST_API_ASSIGNMENT) as `totalCount`, "
+ " (SELECT count(*) from SQLREST_API_ASSIGNMENT where `open`=1) as `openCount`, "
+ " (SELECT count(*) from SQLREST_API_ASSIGNMENT where `status`=1) as `publishCount`, "
+ " (SELECT count(*) from SQLREST_DATASOURCE) as `datasourceCount`")
+ " (SELECT count(*) from SQLREST_DATASOURCE) as `datasourceCount` "
+ "</if>"
+ "<if test='_databaseId == \"postgresql\" '>"
+ "SELECT "
+ " (SELECT count(*) from SQLREST_API_ASSIGNMENT) as \"totalCount\", "
+ " (SELECT count(*) from SQLREST_API_ASSIGNMENT where open=true) as \"openCount\", "
+ " (SELECT count(*) from SQLREST_API_ASSIGNMENT where status=true) as \"publishCount\", "
+ " (SELECT count(*) from SQLREST_DATASOURCE) as \"datasourceCount\" "
+ "</if>"
+ "</script>")
Map<String, Integer> selectCount();
@Select("<script> "
@Select("<script>"
+ "<if test='_databaseId == \"mysql\" '>"
+ "SELECT path as name,count(1) as count from SQLREST_ACCESS_RECORD "
+ "WHERE DATE_SUB( CURDATE(), INTERVAL ${days} DAY ) &lt;= date(create_time) "
+ "WHERE DATE_SUB( CURDATE(), INTERVAL ${days} DAY ) <![CDATA[ <= ]]> date(create_time) "
+ "GROUP BY path "
+ "ORDER BY count DESC "
+ "LIMIT ${limit} "
+ "</if>"
+ "<if test='_databaseId == \"postgresql\" '>"
+ "SELECT path as name,count(1) as count from SQLREST_ACCESS_RECORD "
+ "WHERE CURRENT_DATE - INTERVAL'${days} day' <![CDATA[ <= ]]> create_time::date "
+ "GROUP BY path "
+ "ORDER BY count DESC "
+ "LIMIT ${limit} "
+ "</if>"
+ "</script>")
List<NameCount> getTopPathAccess(@Param("days") Integer days, @Param("limit") Integer limit);
@Select("<script> "
@Select("<script>"
+ "<if test='_databaseId == \"mysql\" '>"
+ "SELECT ip_addr as name,count(1) as count from SQLREST_ACCESS_RECORD "
+ "WHERE DATE_SUB( CURDATE(), INTERVAL ${days} DAY ) &lt;= date(create_time) "
+ "WHERE DATE_SUB( CURDATE(), INTERVAL ${days} DAY ) <![CDATA[ <= ]]> date(create_time) "
+ "GROUP BY ip_addr "
+ "ORDER BY count DESC "
+ "LIMIT ${limit} "
+ "</if>"
+ "<if test='_databaseId == \"postgresql\" '>"
+ "SELECT ip_addr as name,count(1) as count from SQLREST_ACCESS_RECORD "
+ "WHERE CURRENT_DATE - INTERVAL'${days} day' <![CDATA[ <= ]]> create_time::date "
+ "GROUP BY ip_addr "
+ "ORDER BY count DESC "
+ "LIMIT ${limit} "
+ "</if>"
+ "</script>")
List<NameCount> getTopIpAddrAccess(@Param("days") Integer days, @Param("limit") Integer limit);
@Select("<script> "
@Select("<script>"
+ "<if test='_databaseId == \"mysql\" '>"
+ "SELECT "
+ "IFNULL((select name from SQLREST_APP_CLIENT t where app_key = client_key),'空') as name, "
+ "count(1) as count "
+ " IFNULL((select name from SQLREST_APP_CLIENT t where app_key = client_key),'空') as name, "
+ " count(1) as count "
+ "from SQLREST_ACCESS_RECORD "
+ "WHERE DATE_SUB( CURDATE(), INTERVAL ${days} DAY ) &lt;= date(create_time) "
+ "GROUP BY client_key "
+ "ORDER BY count DESC "
+ "LIMIT ${limit} "
+ "</if>"
+ "<if test='_databaseId == \"postgresql\" '>"
+ "SELECT "
+ " COALESCE((select name from SQLREST_APP_CLIENT t where app_key = client_key),'空') as name, "
+ " count(1) as count "
+ "from SQLREST_ACCESS_RECORD "
+ "WHERE CURRENT_DATE - INTERVAL'${days} day' <![CDATA[ <= ]]> create_time::date "
+ "GROUP BY client_key "
+ "ORDER BY count DESC "
+ "LIMIT ${limit} "
+ "</if>"
+ "</script>")
List<NameCount> getTopAppClientAccess(@Param("days") Integer days, @Param("limit") Integer limit);
@Select("<script> "
+ "SELECT concat('HTTP(',status,')') as name,count(1) as count from SQLREST_ACCESS_RECORD \n"
+ "WHERE DATE_SUB( CURDATE(), INTERVAL ${days} DAY ) &lt;= date(create_time) \n"
+ "GROUP BY status \n"
@Select("<script>"
+ "<if test='_databaseId == \"mysql\" '>"
+ "SELECT concat('HTTP(',status,')') as name,count(1) as count from SQLREST_ACCESS_RECORD "
+ "WHERE DATE_SUB( CURDATE(), INTERVAL ${days} DAY ) <![CDATA[ <= ]]> date(create_time) "
+ "GROUP BY status "
+ "ORDER BY count DESC "
+ "</if>"
+ "<if test='_databaseId == \"postgresql\" '>"
+ "SELECT concat('HTTP(',status,')') as name,count(1) as count from SQLREST_ACCESS_RECORD "
+ "WHERE CURRENT_DATE - INTERVAL'${days} day' <![CDATA[ <= ]]> create_time::date "
+ "GROUP BY status "
+ "ORDER BY count DESC "
+ "</if>"
+ "</script>")
List<NameCount> getHttpStatusCount(@Param("days") Integer days);
@Select("<script> "
@Select("<script>"
+ "<if test='_databaseId == \"mysql\" '>"
+ "SELECT "
+ " DATE_FORMAT(create_time,'%Y-%m-%d') as of_date , "
+ " count(*) as total, "
......@@ -74,15 +124,34 @@ public interface AccessRecordMapper extends BaseMapper<AccessRecordEntity> {
+ "FROM ( "
+ " SELECT id,path,case when status=200 then 1 else 0 end as success, create_time "
+ " FROM SQLREST_ACCESS_RECORD "
+ " WHERE DATE_SUB( CURDATE(), INTERVAL ${days} DAY ) &lt;= date(create_time) "
+ " WHERE DATE_SUB( CURDATE(), INTERVAL ${days} DAY ) <![CDATA[ <= ]]> date(create_time) "
+ " ) t "
+ " GROUP BY of_date"
+ "</if>"
+ "<if test='_databaseId == \"postgresql\" '>"
+ "SELECT "
+ " to_char(create_time, 'YYYY-MM-DD') as of_date , "
+ " count(*) as total, "
+ " sum(success) as success "
+ "FROM ( "
+ " SELECT id,path,case when status=200 then 1 else 0 end as success, create_time "
+ " FROM SQLREST_ACCESS_RECORD "
+ " WHERE CURRENT_DATE - INTERVAL'${days} day' <![CDATA[ <= ]]> create_time::date "
+ " ) t "
+ " GROUP BY of_date"
+ "</if>"
+ "</script>")
List<DateCount> getDailyTrend(@Param("days") Integer days);
@Delete("<script>"
+ "<if test='_databaseId == \"mysql\" '>"
+ "DELETE FROM SQLREST_ACCESS_RECORD "
+ "WHERE date(create_time) &lt;= DATE_SUB( CURDATE(), INTERVAL ${days} DAY ) "
+ "</if>"
+ "<if test='_databaseId == \"postgresql\" '>"
+ "DELETE FROM SQLREST_ACCESS_RECORD "
+ "WHERE CURRENT_DATE - INTERVAL'${days} day' <![CDATA[ <= ]]> create_time::date "
+ "</if>"
+ "</script>")
void deleteHistoryBeforeDays(@Param("days") Integer days);
}
......@@ -18,6 +18,6 @@ import org.apache.ibatis.annotations.Select;
public interface ClientGroupMapper extends BaseMapper<ClientGroupEntity> {
@Select("SELECT g.id,g.name from `SQLREST_CLIENT_GROUP` a LEFT JOIN `SQLREST_API_GROUP` g on a.group_id=g.id where a.client_id= #{id}")
@Select("SELECT g.id,g.name from SQLREST_CLIENT_GROUP a LEFT JOIN SQLREST_API_GROUP g on a.group_id=g.id where a.client_id= #{id}")
List<IdWithName> getGroupAuth(@Param("id") Long id);
}
......@@ -17,7 +17,7 @@ import org.apache.ibatis.annotations.Update;
public interface FirewallRulesMapper extends BaseMapper<FirewallRulesEntity> {
@Update("update `SQLREST_FIREWALL_RULES` set status = #{status} where id = #{id}")
@Update("update SQLREST_FIREWALL_RULES set status = #{status} where id = #{id}")
void updateStatus(@Param("id") Long id, @Param("status") OnOffEnum status);
}
......@@ -17,6 +17,6 @@ import org.apache.ibatis.annotations.Update;
public interface UnifyAlarmMapper extends BaseMapper<UnifyAlarmEntity> {
@Update("update `SQLREST_UNIFY_ALARM` set status = #{status} where id = #{id}")
@Update("update SQLREST_UNIFY_ALARM set status = #{status} where id = #{id}")
void updateStatus(@Param("id") Long id, @Param("status") OnOffEnum status);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment