Skip to content

Commit 958e0f1

Browse files
committed
支持oracle
1 parent 03bd9ec commit 958e0f1

25 files changed

Lines changed: 241 additions & 109 deletions

front/src/utils/global.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Object.assign(Vue.prototype, {
2727
if (code === '0') { // 成功
2828
callback && callback.call(that, resp)
2929
} else {
30+
errorCallback && errorCallback.call(that, resp)
3031
that.$message.error(resp.msg)
3132
}
3233
}).catch(function(error) {

front/src/views/generate/GenerateConfig/index.vue

Lines changed: 115 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,35 @@
133133
<el-form-item label="Port" prop="port">
134134
<el-input v-model="datasourceFormData.port" placeholder="端口" show-word-limit maxlength="10" />
135135
</el-form-item>
136-
<el-form-item label="Database" prop="dbName">
137-
<el-input v-model="datasourceFormData.dbName" placeholder="数据库" show-word-limit maxlength="64" />
136+
<el-form-item :label="dbNamePlaceholder" prop="dbName">
137+
<el-input v-model="datasourceFormData.dbName" :placeholder="dbNamePlaceholder" show-word-limit maxlength="64" />
138138
</el-form-item>
139-
<el-form-item v-show="showPgSqlSchema" label="Schema" prop="schemaName">
140-
<el-input v-model="datasourceFormData.schemaName" placeholder="schema" show-word-limit maxlength="64" />
139+
<el-form-item label="连接类型" v-show="showOracleFields">
140+
<el-select v-model="datasourceFormData.oracleConnType">
141+
<el-option
142+
v-for="item in oracleConnTypeList"
143+
:key="item.val"
144+
:label="item.lab"
145+
:value="item.val"
146+
/>
147+
</el-select>
148+
</el-form-item>
149+
<el-form-item v-show="showPgSqlSchema" :label="schemaPlaceholder" :prop="schemaPlaceholder">
150+
<el-input v-model="datasourceFormData.schemaName" :placeholder="schemaPlaceholder+',如SCOTT'" show-word-limit maxlength="64" />
141151
</el-form-item>
142152
<el-form-item label="Username" prop="username">
143153
<el-input v-model="datasourceFormData.username" placeholder="用户名" show-word-limit maxlength="100" />
144154
</el-form-item>
155+
<el-form-item label="角色" v-show="showOracleFields">
156+
<el-select v-model="datasourceFormData.oracleRole">
157+
<el-option
158+
v-for="item in oracleRoleList"
159+
:key="item.val"
160+
:label="item.lab"
161+
:value="item.val"
162+
/>
163+
</el-select>
164+
</el-form-item>
145165
<el-form-item label="Password" prop="password">
146166
<el-input v-model="datasourceFormData.password" type="password" placeholder="密码" show-word-limit maxlength="100" />
147167
</el-form-item>
@@ -234,12 +254,28 @@ export default {
234254
username: '',
235255
password: '',
236256
dbName: '',
257+
oracleConnType: '',
258+
oracleRole: '',
237259
schemaName: '',
238260
packageName: '',
239261
delPrefix: '',
240262
groupId: ''
241263
},
242264
dbTypeConfig: [],
265+
oracleConnTypeList: [{
266+
lab:"服务名",
267+
val:1
268+
},{
269+
lab:"SID",
270+
val:2
271+
}],
272+
oracleRoleList: [{
273+
lab: "SYSDBA",
274+
val: 1
275+
},{
276+
lab: "SYSOPER",
277+
val: 2
278+
}],
243279
datasourceRule: {
244280
host: [
245281
{ required: true, message: '不能为空', trigger: 'blur' }
@@ -277,7 +313,24 @@ export default {
277313
},
278314
computed: {
279315
showPgSqlSchema() {
280-
return this.datasourceFormData.dbType === DB_TYPE.PostgreSQL;
316+
return this.datasourceFormData.dbType === DB_TYPE.PostgreSQL || this.datasourceFormData.dbType === DB_TYPE.Oracle;
317+
},
318+
dbNamePlaceholder() {
319+
if(this.datasourceFormData.dbType === DB_TYPE.Oracle) {
320+
return "服务名";
321+
} else {
322+
return "数据库";
323+
}
324+
},
325+
schemaPlaceholder() {
326+
if(this.datasourceFormData.dbType === DB_TYPE.Oracle) {
327+
return "数据库";
328+
} else {
329+
return "schema";
330+
}
331+
},
332+
showOracleFields() {
333+
return this.datasourceFormData.dbType === DB_TYPE.Oracle;
281334
}
282335
},
283336
created() {
@@ -360,6 +413,44 @@ export default {
360413
const id = this.getAttr(current_datasource_id_key)
361414
return parseInt(id) || ''
362415
},
416+
packageOracleFields() {
417+
// 处理oracle连接数据
418+
if(this.datasourceFormData.dbType === DB_TYPE.Oracle) {
419+
// 处理连接类型
420+
if (this.datasourceFormData.oracleConnType == 1) {
421+
this.datasourceFormData.dbName = "/" + this.datasourceFormData.dbName;
422+
} else if (this.datasourceFormData.oracleConnType == 2) {
423+
this.datasourceFormData.dbName = ":" + this.datasourceFormData.dbName;
424+
}
425+
// 处理账号角色
426+
if (this.datasourceFormData.oracleRole == 1) {
427+
this.datasourceFormData.username += " AS SYSDBA";
428+
} else {
429+
this.datasourceFormData.username += " AS SYSOPER";
430+
}
431+
}
432+
},
433+
unPackOracleFields(item) {
434+
// 处理oracle属性 拆包
435+
if (item.dbType = DB_TYPE.Oracle) {
436+
// 处理连接类型
437+
if (item.dbName.startsWith("/")) {
438+
item.oracleConnType = 1;
439+
item.dbName = item.dbName.replace("/","");
440+
} else if (item.dbName.startsWith(":")) {
441+
item.oracleConnType = 2;
442+
item.dbName = item.dbName.replace(":","");
443+
}
444+
// 处理账号角色
445+
if (item.username.includes("AS SYSDBA")) {
446+
item.oracleRole = 1;
447+
item.username = item.username.replace(" AS SYSDBA", "");
448+
} else if (item.username.includes("AS SYSOPER")) {
449+
item.oracleRole = 2;
450+
item.username = item.username.replace(" AS SYSOPER", "");
451+
}
452+
}
453+
},
363454
onDataSourceAdd() {
364455
this.datasourceTitle = '新建连接'
365456
Object.keys(this.datasourceFormData).forEach(key => {
@@ -394,6 +485,7 @@ export default {
394485
},
395486
onDataSourceUpdate(item) {
396487
this.datasourceTitle = '修改连接'
488+
this.unPackOracleFields(item);
397489
Object.assign(this.datasourceFormData, item)
398490
this.datasourceDlgShow = true
399491
},
@@ -431,15 +523,33 @@ export default {
431523
})
432524
},
433525
onDatasourceTest() {
526+
this.packageOracleFields();
434527
this.$refs.datasourceForm.validate((valid) => {
435528
if (valid) {
436529
this.post('/datasource/test', this.datasourceFormData, resp => {
530+
if (this.datasourceFormData.dbType === DB_TYPE.Oracle) {
531+
// 处理连接类型
532+
if (this.datasourceFormData.oracleConnType == 1) {
533+
this.datasourceFormData.dbName = this.datasourceFormData.dbName.replace("/","");
534+
} else if (this.datasourceFormData.oracleConnType == 2) {
535+
this.datasourceFormData.dbName = this.datasourceFormData.dbName.replace(":");
536+
}
537+
// 处理账号角色
538+
if (this.datasourceFormData.oracleRole == 1) {
539+
this.datasourceFormData.username = this.datasourceFormData.username.replace(" AS SYSDBA","");
540+
} else {
541+
this.datasourceFormData.username = this.datasourceFormData.username.replace(" AS SYSOPER","");
542+
}
543+
}
437544
this.tip('连接成功')
545+
}, err => {
546+
this.unPackOracleFields(this.datasourceFormData);
438547
})
439548
}
440549
})
441550
},
442551
onDatasourceSave() {
552+
this.packageOracleFields();
443553
this.$refs.datasourceForm.validate((valid) => {
444554
if (valid) {
445555
this.post('/datasource/test', this.datasourceFormData, resp => {

gen/src/main/java/com/gitee/gen/gen/DbType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public enum DbType {
1313
ORACLE(2,
1414
"Oracle",
1515
"oracle.jdbc.driver.OracleDriver",
16-
"jdbc:oracle:thin:@%s:%s:%s"),
16+
"jdbc:oracle:thin:@%s:%s%s"),
1717

1818
SQL_SERVER(3,
1919
"SQL Server",

gen/src/main/java/com/gitee/gen/gen/oracle/OracleColumnSelector.java

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,54 @@ public class OracleColumnSelector extends ColumnSelector {
1717

1818
private static final TypeFormatter TYPE_FORMATTER = new OracleTypeFormatter();
1919

20-
private static final String COLUMN_SQL = "select "
21-
+ " utc.column_name as FIELD,utc.data_type TYPE, utc.data_scale SCALE, utc.data_length 最大长度, "
22-
+ " CASE utc.nullable WHEN 'N' THEN '否' ELSE '是' END 可空, "
23-
+ " utc.data_default 默认值,ucc.comments COMMENTS,UTC.table_name 表名, "
24-
+ " CASE UTC.COLUMN_NAME "
25-
+ " WHEN (select "
26-
+ " col.column_name "
27-
+ " from "
28-
+ " user_constraints con,user_cons_columns col "
29-
+ " where "
30-
+ " con.constraint_name=col.constraint_name and con.constraint_type='P' "
31-
+ " and col.table_name='%s') THEN 'true' ELSE 'false' END AS KEY "
32-
+ " from "
33-
+ " user_tab_columns utc,user_col_comments ucc "
34-
+ " where "
35-
+ " utc.table_name = ucc.table_name "
36-
+ " and utc.column_name = ucc.column_name "
37-
+ " and utc.table_name = '%s' "
38-
+ " order by "
39-
+ " column_id ";
20+
// private static final String COLUMN_SQL = "select "
21+
// + " utc.column_name as FIELD,utc.data_type TYPE, utc.data_scale SCALE, utc.data_length 最大长度, "
22+
// + " CASE utc.nullable WHEN 'N' THEN '否' ELSE '是' END 可空, "
23+
// + " utc.data_default 默认值,ucc.comments COMMENTS,UTC.table_name 表名, "
24+
// + " CASE UTC.COLUMN_NAME "
25+
// + " WHEN (select "
26+
// + " col.column_name "
27+
// + " from "
28+
// + " user_constraints con,user_cons_columns col "
29+
// + " where "
30+
// + " con.constraint_name=col.constraint_name and con.constraint_type='P' "
31+
// + " and col.table_name='%s') THEN 'true' ELSE 'false' END AS KEY "
32+
// + " from "
33+
// + " user_tab_columns utc,user_col_comments ucc "
34+
// + " where "
35+
// + " utc.table_name = ucc.table_name "
36+
// + " and utc.column_name = ucc.column_name "
37+
// + " and utc.table_name = '%s' "
38+
// + " order by "
39+
// + " column_id ";
40+
41+
private static final String COLUMN_SQL = " SELECT " +
42+
" atc.COLUMN_NAME FIELD, atc.DATA_TYPE TYPE, atc.DATA_SCALE SCALE, atc.DATA_LENGTH 最大长度, " +
43+
" CASE atc.NULLABLE WHEN 'N' THEN '否' ELSE '是' END 可空, " +
44+
" atc.DATA_DEFAULT 默认值, acc.COMMENTS COMMENTS, atc.TABLE_NAME 表名, " +
45+
" CASE atc.COLUMN_NAME " +
46+
" WHEN " +
47+
" ( SELECT col.column_name FROM all_constraints con " +
48+
" LEFT JOIN all_cons_columns col ON con.table_name = col.table_name " +
49+
" AND con.OWNER = col.OWNER AND con.CONSTRAINT_NAME = col.CONSTRAINT_NAME " +
50+
" WHERE con.constraint_type = 'P' " +
51+
" AND col.table_name = '%s' AND con.OWNER = '%s' ) " +
52+
" THEN 'true' ELSE 'false' END AS KEY " +
53+
" FROM ALL_TAB_COLUMNS atc " +
54+
" LEFT JOIN ALL_COL_COMMENTS acc " +
55+
" ON acc.TABLE_NAME = atc.TABLE_NAME AND acc.COLUMN_NAME = atc.COLUMN_NAME " +
56+
" AND acc.OWNER = atc.OWNER " +
57+
" WHERE atc.TABLE_NAME = '%s' AND atc.OWNER = '%s' " +
58+
" ORDER BY atc.COLUMN_ID ";
4059

4160
public OracleColumnSelector(GeneratorConfig generatorConfig) {
4261
super(generatorConfig);
4362
}
4463

4564
@Override
4665
protected String getColumnInfoSQL(String tableName) {
47-
return String.format(COLUMN_SQL, tableName, tableName);
66+
String owner = this.getGeneratorConfig().getSchemaName();
67+
return String.format(COLUMN_SQL, tableName, owner, tableName, owner);
4868
}
4969

5070
@Override

gen/src/main/java/com/gitee/gen/gen/oracle/OracleTableSelector.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,18 @@ public OracleTableSelector(ColumnSelector columnSelector,
2929
*/
3030
@Override
3131
protected String getShowTablesSQL(GeneratorConfig generatorConfig) {
32-
String dbName = generatorConfig.getUsername().toUpperCase();
32+
String owner = generatorConfig.getSchemaName().toUpperCase();
3333
StringBuilder sb = new StringBuilder("");
34-
sb.append(" SELECT a.TABLE_NAME as NAME,b.COMMENTS as COMMENTS ");
35-
sb.append(" FROM ALL_TABLES a,USER_TAB_COMMENTS b ");
36-
sb.append(" WHERE a.TABLE_NAME=b.TABLE_NAME ");
34+
sb.append(" SELECT TABLE_NAME AS NAME, COMMENTS FROM all_tab_comments ");
35+
sb.append(" WHERE 1=1 ");
3736
if(this.getSchTableNames() != null && this.getSchTableNames().size() > 0) {
3837
StringBuilder tables = new StringBuilder();
3938
for (String table : this.getSchTableNames()) {
4039
tables.append(",'").append(table).append("'");
4140
}
42-
sb.append(" AND a.TABLE_NAME IN (" + tables.substring(1) + ")");
41+
sb.append(" AND TABLE_NAME IN (" + tables.substring(1) + ")");
4342
}
44-
sb.append(" AND a.OWNER='"+dbName+"'");
43+
sb.append(" AND OWNER='"+owner+"'");
4544
return sb.toString();
4645
}
4746

0 commit comments

Comments
 (0)