Skip to content

Commit

Permalink
✨ feat(GoORMHelperSettingForm): Add tag mode combo box for setting form
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmai-dev committed Jan 27, 2025
1 parent 10e2596 commit 55d69d5
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 40 deletions.
27 changes: 22 additions & 5 deletions src/main/java/com/github/maiqingqiang/goormhelper/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.github.maiqingqiang.goormhelper.sql2struct.impl.SQL2GormStruct;
import com.github.maiqingqiang.goormhelper.sql2struct.impl.SQL2Struct;
import com.github.maiqingqiang.goormhelper.sql2struct.impl.SQL2XormStruct;
import com.intellij.openapi.project.Project;

import java.util.List;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -61,12 +62,12 @@ public String toString() {
return name;
}

public ISQL2Struct sql2Struct(String sql, DbType dbType) {
public ISQL2Struct sql2Struct(Project project, String sql, DbType dbType) {
return switch (this) {
case General -> new SQL2Struct(sql, dbType);
case Gorm -> new SQL2GormStruct(sql, dbType);
case Xorm -> new SQL2XormStruct(sql, dbType);
case GoFrame -> new SQL2GoFrameStruct(sql, dbType);
case General -> new SQL2Struct(project, sql, dbType);
case Gorm -> new SQL2GormStruct(project, sql, dbType);
case Xorm -> new SQL2XormStruct(project, sql, dbType);
case GoFrame -> new SQL2GoFrameStruct(project, sql, dbType);
default -> null;
};
}
Expand Down Expand Up @@ -96,4 +97,20 @@ public String toString() {
return name;
}
}

enum TagMode {
Compact(GoORMHelperBundle.message("tagMode.Compact")),
Full(GoORMHelperBundle.message("tagMode.Full"));

private final String name;

@Override
public String toString() {
return name;
}

TagMode(String name) {
this.name = name;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected void doExecute(@NotNull Editor editor, @Nullable Caret caret, DataCont
return;
}

ISQL2Struct sql2Struct = finalSelectedORM.sql2Struct(text, finalSelectedDatabase.toDbType());
ISQL2Struct sql2Struct = finalSelectedORM.sql2Struct(project, text, finalSelectedDatabase.toDbType());
if (sql2Struct == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;
Expand All @@ -19,13 +20,15 @@ public class SQL2StructAction extends AnAction {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {

GoORMHelperProjectSettings.State state = Objects.requireNonNull(GoORMHelperProjectSettings.getInstance(Objects.requireNonNull(e.getProject())).getState());
Project project = e.getProject();

GoORMHelperProjectSettings.State state = Objects.requireNonNull(GoORMHelperProjectSettings.getInstance(Objects.requireNonNull(project)).getState());

Types.ORM selectedORM = state.defaultORM;
Types.Database selectedDatabase = state.defaultDatabase;

if (selectedORM == Types.ORM.AskEveryTime || selectedDatabase == Types.Database.AskEveryTime) {
ConvertSettingDialogWrapper wrapper = new ConvertSettingDialogWrapper(e.getProject());
ConvertSettingDialogWrapper wrapper = new ConvertSettingDialogWrapper(project);
if (!wrapper.showAndGet()) return;

selectedORM = (Types.ORM) wrapper.getOrmComponent().getComponent().getSelectedItem();
Expand All @@ -44,10 +47,10 @@ public void actionPerformed(@NotNull AnActionEvent e) {
final Types.ORM finalSelectedORM = selectedORM;
final Types.Database finalSelectedDatabase = selectedDatabase;

WriteCommandAction.runWriteCommandAction(e.getProject(), () -> {
WriteCommandAction.runWriteCommandAction(project, () -> {
if (text == null || text.isEmpty() || finalSelectedORM == null || finalSelectedDatabase == null) return;

ISQL2Struct sql2Struct = finalSelectedORM.sql2Struct(text, finalSelectedDatabase.toDbType());
ISQL2Struct sql2Struct = finalSelectedORM.sql2Struct(project, text, finalSelectedDatabase.toDbType());

editor.getDocument().replaceString(start, end, sql2Struct.convert());
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public void setDefaultDatabase(Types.Database database) {
state.defaultDatabase = database;
}

public void setDefaultTagMode(Types.TagMode tagMode) {
state.defaultTagMode = tagMode;
}

public void setEnableGlobalScan(boolean enable) {
state.enableGlobalScan = enable;
}
Expand All @@ -59,6 +63,7 @@ public void setScanPathList(List<String> scanPathList) {
public static class State extends SimpleModificationTracker {
public Types.ORM defaultORM = Types.ORM.AskEveryTime;
public Types.Database defaultDatabase = Types.Database.AskEveryTime;
public Types.TagMode defaultTagMode = Types.TagMode.Full;
public boolean enableGlobalScan = true;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;

public class SQL2GoFrameStruct extends SQL2Struct {
public SQL2GoFrameStruct(String sql, DbType dbType) {
super(sql, dbType);
public SQL2GoFrameStruct(Project project, String sql, DbType dbType) {
super(project, sql, dbType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,37 @@

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
import com.github.maiqingqiang.goormhelper.Types;
import com.github.maiqingqiang.goormhelper.services.GoORMHelperProjectSettings;
import com.github.maiqingqiang.goormhelper.utils.Strings;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

public class SQL2GormStruct extends SQL2Struct {
public SQL2GormStruct(String sql, DbType dbType) {
super(sql, dbType);
public SQL2GormStruct(Project project, String sql, DbType dbType) {
super(project, sql, dbType);
}

@Override
protected void generateORMTag(@NotNull StringBuilder stringBuilder, @NotNull SQLColumnDefinition definition) {
stringBuilder.append("gorm:\"")
.append("column:").append(getColumn(definition)).append(";")
.append("type:").append(getDBType(definition)).append(";");
GoORMHelperProjectSettings.State state = Objects.requireNonNull(GoORMHelperProjectSettings.getInstance(project).getState());

if (state.defaultTagMode == Types.TagMode.Compact || state.defaultTagMode == Types.TagMode.Full) {
stringBuilder.append("gorm:\"")
.append("column:").append(getColumn(definition)).append(";");
}

if (state.defaultTagMode == Types.TagMode.Full) {
appendOriginalTagAttributes(stringBuilder, definition);
}

stringBuilder.append("\" ");
}

private void appendOriginalTagAttributes(@NotNull StringBuilder stringBuilder, @NotNull SQLColumnDefinition definition) {
stringBuilder.append("type:").append(getDBType(definition)).append(";");

String comment = getComment(definition);
if (!comment.isEmpty()) {
Expand All @@ -31,12 +49,9 @@ protected void generateORMTag(@NotNull StringBuilder stringBuilder, @NotNull SQL

if (definition.getDefaultExpr() != null) {
String def = Strings.clearSingleQuotn(definition.getDefaultExpr().toString());

if (!def.isEmpty()) {
stringBuilder.append("default:").append(def).append(";");
}
}

stringBuilder.append("\" ");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.github.maiqingqiang.goormhelper.sql2struct.ISQL2Struct;
import com.github.maiqingqiang.goormhelper.utils.Strings;
import com.google.common.base.CaseFormat;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;

import java.util.List;
Expand Down Expand Up @@ -63,11 +64,13 @@ public class SQL2Struct implements ISQL2Struct {
private static final String defaultUnsignedDataType = "uint32";
private final String sql;
private final DbType dbType;
protected final Project project;


public SQL2Struct(String sql, DbType dbType) {
public SQL2Struct(Project project, String sql, DbType dbType) {
this.sql = sql;
this.dbType = dbType;
this.project = project;
}

public String convert() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,37 @@

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
import com.github.maiqingqiang.goormhelper.Types;
import com.github.maiqingqiang.goormhelper.services.GoORMHelperProjectSettings;
import com.github.maiqingqiang.goormhelper.utils.Strings;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

public class SQL2XormStruct extends SQL2Struct {
public SQL2XormStruct(String sql, DbType dbType) {
super(sql, dbType);
public SQL2XormStruct(Project project, String sql, DbType dbType) {
super(project, sql, dbType);
}

@Override
protected void generateORMTag(@NotNull StringBuilder stringBuilder, @NotNull SQLColumnDefinition definition) {
stringBuilder.append("xorm:\"");
GoORMHelperProjectSettings.State state = Objects.requireNonNull(GoORMHelperProjectSettings.getInstance(project).getState());

if (state.defaultTagMode == Types.TagMode.Compact || state.defaultTagMode == Types.TagMode.Full) {
stringBuilder.append("xorm:\"")
.append(getDBType(definition)).append(" ")
.append("'").append(getColumn(definition)).append("' ");
}

if (state.defaultTagMode == Types.TagMode.Full) {
appendOriginalTagAttributes(stringBuilder, definition);
}

stringBuilder.append(getDBType(definition)).append(" ");
stringBuilder.append("'").append(getColumn(definition)).append("' ");
stringBuilder.append("\" ");
}

private void appendOriginalTagAttributes(@NotNull StringBuilder stringBuilder, @NotNull SQLColumnDefinition definition) {
String comment = getComment(definition);
if (!comment.isEmpty()) {
stringBuilder.append("comment('").append(comment).append("') ");
Expand All @@ -30,18 +46,15 @@ protected void generateORMTag(@NotNull StringBuilder stringBuilder, @NotNull SQL
stringBuilder.append("autoincr ");
}

if (definition.containsNotNullConstaint()) {
if (definition.containsNotNullConstraint()) {
stringBuilder.append("notnull ");
}

if (definition.getDefaultExpr() != null) {
String def = Strings.clearSingleQuotn(definition.getDefaultExpr().toString());

if (!def.isEmpty()) {
stringBuilder.append("default ").append(def).append(" ");
}
}

stringBuilder.append("\" ");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class GoORMHelperSettingForm implements ConfigurableUi<GoORMHelperProject
private JPanel panel;
private ComboBox<Types.ORM> ormComboBox;
private ComboBox<Types.Database> databaseComboBox;
private ComboBox<Types.TagMode> tagModeComboBox;

private JCheckBox enableGlobalScanCheckBox;
private ListTableModel<String> scanPathListTableModel;
// private TextFieldWithBrowseButton sqlPathTextField;
Expand Down Expand Up @@ -63,13 +65,16 @@ private static FileChooserDescriptor getFileChooserDescriptor(String title) {
private void initComponent() {
ormComboBox = new ComboBox<>(Types.ORM.values());
databaseComboBox = new ComboBox<>(Types.Database.values());
tagModeComboBox = new ComboBox<>(Types.TagMode.values());

enableGlobalScanCheckBox = new JCheckBox(GoORMHelperBundle.message("setting.enableGlobalScanCheckBox.title"));
findStructTableNameFuncCheckBox = new JCheckBox(GoORMHelperBundle.message("setting.findStructTableNameFuncCheckBox.title"));

panel = FormBuilder
.createFormBuilder()
.addLabeledComponent(GoORMHelperBundle.message("setting.ormComboBox.title"), ormComboBox)
.addLabeledComponent(GoORMHelperBundle.message("setting.databaseComboBox.title"), databaseComboBox)
.addLabeledComponent(GoORMHelperBundle.message("setting.tagModeComboBox.title"), tagModeComboBox)
.addComponent(enableGlobalScanCheckBox)
.addComponentFillVertically(initScanPathComponent(), 0)
.addComponent(new JSeparator())
Expand Down Expand Up @@ -134,6 +139,8 @@ private void loadSettings(@NotNull GoORMHelperProjectSettings settings) {

ormComboBox.setSelectedItem(state.defaultORM);
databaseComboBox.setSelectedItem(state.defaultDatabase);
tagModeComboBox.setSelectedItem(state.defaultTagMode);

// sqlPathTextField.setText(state.sqlPath);
enableGlobalScanCheckBox.setSelected(state.enableGlobalScan);
findStructTableNameFuncCheckBox.setSelected(state.findStructTableNameFunc);
Expand All @@ -147,6 +154,7 @@ public boolean isModified(@NotNull GoORMHelperProjectSettings settings) {

return !(ormComboBox.getSelectedItem() == state.defaultORM
&& databaseComboBox.getSelectedItem() == state.defaultDatabase
&& tagModeComboBox.getSelectedItem() == state.defaultTagMode
// && sqlPathTextField.getText().equals(state.sqlPath)
&& enableGlobalScanCheckBox.isSelected() == state.enableGlobalScan
&& findStructTableNameFuncCheckBox.isSelected() == state.findStructTableNameFunc
Expand All @@ -160,6 +168,7 @@ public void apply(@NotNull GoORMHelperProjectSettings settings) {
List<String> oldscanPathList = scanPathListTableModel.getItems();

settings.setDefaultDatabase((Types.Database) databaseComboBox.getSelectedItem());
settings.setDefaultTagMode((Types.TagMode) tagModeComboBox.getSelectedItem());
settings.setDefaultORM((Types.ORM) ormComboBox.getSelectedItem());
settings.setEnableGlobalScan(enableGlobalScanCheckBox.isSelected());
settings.setFindStructTableNameFunc(findStructTableNameFuncCheckBox.isSelected());
Expand Down
9 changes: 7 additions & 2 deletions src/main/resources/messages/GoORMHelperBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@ sql.convert.struct.check=Please check if the SQL is correct.
action.GoORMHelper.SQL2StructAction.text=SQL Convert Struct
action.GoORMHelper.SQL2StructAction.description=SQL Convert Struct
# setting
setting.basic.title=Basic
setting.tableview.column.location=Location
setting.decorator.title=Scan Path
setting.ormComboBox.title=ORM
setting.databaseComboBox.title=Database
setting.sqlPathTextField.title=SQL File Path
setting.tagModeComboBox.title=Tag Mode
setting.scanPath.title=SQL File Path
setting.enableGlobalScanCheckBox.title=Global Scan
setting.findStructTableNameFuncCheckBox.title=Scan Struct Table Name
setting.experimental.title=Experimental:
# database
database.AskEveryTime=Ask every time
# orm
orm.AskEveryTime=Ask every time
orm.General=General
orm.General=General
# tag mode
tagMode.Compact=Compact
tagMode.Full=Full
8 changes: 6 additions & 2 deletions src/main/resources/messages/GoORMHelperBundle_ja.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ setting.tableview.column.location=\u30D1\u30B9
setting.decorator.title=\u30B9\u30AD\u30E3\u30F3\u30D1\u30B9
setting.ormComboBox.title=ORM
setting.databaseComboBox.title=\u30C7\u30FC\u30BF\u30D9\u30FC\u30B9
setting.sqlPathTextField.title=SQL \u30D5\u30A1\u30A4\u30EB\u30D1\u30B9
setting.tagModeComboBox.title=Tag Mode
setting.scanPath.title=SQL \u30D5\u30A1\u30A4\u30EB\u30D1\u30B9
setting.enableGlobalScanCheckBox.title=\u30B0\u30ED\u30FC\u30D0\u30EB\u30B9\u30AD\u30E3\u30F3
setting.findStructTableNameFuncCheckBox.title=Struct \u30c6\u30fc\u30d6\u30eb\u540d\u3092\u30b9\u30ad\u30e3\u30f3\u3059\u308b
setting.experimental.title=\u5b9f\u9a13\u7684:
# database
database.AskEveryTime=\u6BCE\u56DE\u306E\u554F\u3044\u5408\u308F\u305B
# orm
orm.AskEveryTime=\u6BCE\u56DE\u306E\u554F\u3044\u5408\u308F\u305B
orm.General=\u5171\u901A
orm.General=\u5171\u901A
# tag mode
tagMode.Compact=Compact
tagMode.Full=Full
8 changes: 6 additions & 2 deletions src/main/resources/messages/GoORMHelperBundle_ko.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ setting.tableview.column.location=\uACBD\uB85C
setting.decorator.title=\uC2A4\uCE94 \uACBD\uB85C
setting.ormComboBox.title=ORM
setting.databaseComboBox.title=\uB370\uC774\uD130\uBCA0\uC774\uC2A4
setting.sqlPathTextField.title=SQL \uD30C\uC77C \uACBD\uB85C
setting.tagModeComboBox.title=Tag Mode
setting.scanPath.title=SQL \uD30C\uC77C \uACBD\uB85C
setting.enableGlobalScanCheckBox.title=\uC804\uC5ED \uAC80\uC0C9
setting.findStructTableNameFuncCheckBox.title=Struct \ud14c\uc774\ube14 \uc774\ub984 \uc2a4\uce94
setting.experimental.title=\uc2e4\ud5d8\uc801:
# database
database.AskEveryTime=\uC9C8\uBB38\uB9C8\uB2E4
# orm
orm.AskEveryTime=\uC9C8\uBB38\uB9C8\uB2E4
orm.General=\uACF5\uD1B5
orm.General=\uACF5\uD1B5
# tag mode
tagMode.Compact=Compact
tagMode.Full=Full
Loading

0 comments on commit 55d69d5

Please sign in to comment.