add basic export to c# with todos
This commit is contained in:
parent
1a20bea369
commit
31e7bb7bda
@ -754,7 +754,7 @@ public class B3
|
|||||||
this.saveCSScriptBTN.setOnAction(arg0 -> {
|
this.saveCSScriptBTN.setOnAction(arg0 -> {
|
||||||
if (B3.this.generateRelationalModelCB.isSelected()) {
|
if (B3.this.generateRelationalModelCB.isSelected()) {
|
||||||
try {
|
try {
|
||||||
B3.this.saveSQLScript();
|
B3.this.saveCSScript();
|
||||||
} catch (ZZ20 ignored) {
|
} catch (ZZ20 ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1189,7 +1189,60 @@ public class B3
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void saveCSScript() throws ZZ20 {
|
||||||
|
if (this.currentTab == null || this.currentTab.canvas == null)
|
||||||
|
return;
|
||||||
|
FileChooser fc = new FileChooser();
|
||||||
|
fc.setInitialDirectory(new File(System.getProperty("user.home")));
|
||||||
|
|
||||||
|
fc.setInitialFileName(this.currentTab.canvas.dsfdsfd45645zr().getDatabaseName());
|
||||||
|
fc.getExtensionFilters().add(new FileChooser.ExtensionFilter("*.cs", "*.cs"));
|
||||||
|
fc.setTitle("Save cs script as ...");
|
||||||
|
final File file = fc.showSaveDialog(this);
|
||||||
|
|
||||||
|
if (file != null) {
|
||||||
|
|
||||||
|
this.progress = new V1(this);
|
||||||
|
|
||||||
|
Thread t = new Thread(() -> {
|
||||||
|
FileWriter fw = null;
|
||||||
|
try {
|
||||||
|
B3.this.borderPane.setDisable(true);
|
||||||
|
|
||||||
|
fw = new FileWriter(file);
|
||||||
|
|
||||||
|
StringBuffer sb = B3.this.currentTab.canvas.dsfdsfd45645zr().create_CS_Script();
|
||||||
|
|
||||||
|
assert sb != null;
|
||||||
|
fw.write(sb.toString());
|
||||||
|
fw.close();
|
||||||
|
|
||||||
|
} catch (IOException | ZZ20 ex) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (fw != null) fw.close();
|
||||||
|
} catch (IOException ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Platform.runLater(() -> {
|
||||||
|
B3.this.progress.close();
|
||||||
|
B3.this.progress = null;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
this.progress.setThread(t);
|
||||||
|
try {
|
||||||
|
t.join();
|
||||||
|
} catch (InterruptedException ignored) {
|
||||||
|
}
|
||||||
|
|
||||||
|
this.borderPane.setDisable(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
String databaseName = this.currentTab.getText().replace(".cs", "") + "db";
|
||||||
|
this.currentTab.canvas.dsfdsfd45645zr().setDatabaseName(databaseName);
|
||||||
|
}
|
||||||
public void saveSQLScript() throws ZZ20 {
|
public void saveSQLScript() throws ZZ20 {
|
||||||
if (this.currentTab == null || this.currentTab.canvas == null)
|
if (this.currentTab == null || this.currentTab.canvas == null)
|
||||||
return;
|
return;
|
||||||
@ -1233,9 +1286,7 @@ public class B3
|
|||||||
assert sb != null;
|
assert sb != null;
|
||||||
fw.write(sb.toString());
|
fw.write(sb.toString());
|
||||||
fw.close();
|
fw.close();
|
||||||
|
} catch (IOException | ZZ20 ex) {
|
||||||
Thread.sleep(2000L);
|
|
||||||
} catch (IOException | ZZ20 | InterruptedException ex) {
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (fw != null) fw.close();
|
if (fw != null) fw.close();
|
||||||
|
@ -335,6 +335,185 @@ public class C4 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String sqltoCSType(String typeName)
|
||||||
|
{
|
||||||
|
typeName = typeName.substring(0,typeName.indexOf('(')>-1?typeName.indexOf('('):typeName.length());
|
||||||
|
//System.out.println(typeName);
|
||||||
|
|
||||||
|
HashMap<String,String> SQL_TO_CS_TYPE_MAP = new HashMap<>();
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("bigint", "long");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("binary", "byte[]");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("bit", "bool");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("char", "char");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("date", "DateTime");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("datetime", "DateTime");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("datetime2", "DateTime");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("datetimeoffset", "DateTimeOffset");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("decimal", "decimal");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("filestream", "byte[]");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("float", "double");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("geography", "Microsoft.SqlServer.Types.SqlGeography");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("geometry", "Microsoft.SqlServer.Types.SqlGeometry");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("hierarchyid", "Microsoft.SqlServer.Types.SqlHierarchyId");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("image", "byte[]");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("int", "int");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("money", "decimal");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("nchar", "string");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("ntext", "string");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("numeric", "decimal");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("nvarchar", "string");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("real", "Single");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("rowversion", "byte[]");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("smalldatetime", "DateTime");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("smallint", "short");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("smallmoney", "decimal");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("sql_variant", "object");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("text", "string");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("time", "TimeSpan");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("timestamp", "byte[]");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("tinyint", "byte");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("uniqueidentifier", "Guid");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("varbinary", "byte[]");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("varchar", "string");
|
||||||
|
SQL_TO_CS_TYPE_MAP.put("xml", "string");
|
||||||
|
|
||||||
|
typeName = typeName.substring(0,typeName.indexOf('(')>-1?typeName.indexOf('('):typeName.length());
|
||||||
|
//System.out.println(typeName);
|
||||||
|
|
||||||
|
String csType = SQL_TO_CS_TYPE_MAP.get(typeName);
|
||||||
|
return csType != null ? csType : "object";
|
||||||
|
}
|
||||||
|
|
||||||
|
public StringBuffer create_CS_Script() throws ZZ20 {
|
||||||
|
|
||||||
|
this.databaseName = T3.replaceSpecialChars(this.databaseName);
|
||||||
|
|
||||||
|
this.databaseName = this.databaseName.toLowerCase();
|
||||||
|
|
||||||
|
this.sqlScript = new StringBuffer();
|
||||||
|
|
||||||
|
this.sqlScript.append("var opt = new DbContextOptionsBuilder()\n" + " .UseSqlite(@\"Data Source=").append(this.databaseName).append(".db\")\n").append(" .Options;\n");
|
||||||
|
|
||||||
|
this.sqlScript.append("// tables-#################################################\n");
|
||||||
|
for (T1 table : this.tables) {
|
||||||
|
if(table.getSkjdfhgdskjf674534().size() > 0) {
|
||||||
|
this.sqlScript = this.sqlScript.append("[Table(\"").append(table.getTablename()).append("\")]\n");
|
||||||
|
this.sqlScript = this.sqlScript.append("public class ").append(table.getTablename().substring(table.getTablenamePraefix().length())).append(" {").append("\n");
|
||||||
|
|
||||||
|
StringBuilder constructorbuilder = new StringBuilder("\n").append(addChars()).append("public ").append(table.getTablename().substring(table.getTablenamePraefix().length())).append("(");
|
||||||
|
StringBuilder innerconstructorbuilder = new StringBuilder();
|
||||||
|
|
||||||
|
int autoincrementCounter = 0;
|
||||||
|
long holdAutoincrementStartAt = 1L;
|
||||||
|
for (C3 column : table.getSkjdfhgdskjf674534()) {
|
||||||
|
System.out.println(column.getColumnName()+"\t"+sqltoCSType(column.getDatatype()));
|
||||||
|
String shortcolumnname = column.getColumnName().substring(column.getColumnName().lastIndexOf('_')+1);
|
||||||
|
this.sqlScript.append(addChars())
|
||||||
|
.append(column.isKey() ? ("[Key]\n"+addChars()) : "")
|
||||||
|
.append("[Column(TypeName=\"")
|
||||||
|
.append(column.getDatatype())
|
||||||
|
.append("\")]\n")
|
||||||
|
.append(addChars())
|
||||||
|
.append(!column.isNullable() ? "[Required]\n"+addChars() : "")
|
||||||
|
.append(column.isUnique()?"[Index(\""+column.getColumnName()+"Index\", IsUnique = true)]":"")
|
||||||
|
.append(column.isAutoincrement()?"[DatabaseGenerated(DatabaseGeneratedOption.Identity)]":"")
|
||||||
|
.append(sqltoCSType(column.getDatatype()))
|
||||||
|
.append(" ")
|
||||||
|
//a_id = id, a_b_id = id
|
||||||
|
.append(shortcolumnname)
|
||||||
|
.append(" { get; ")
|
||||||
|
.append(column.isKey()?"":"private ")
|
||||||
|
.append("set; }\n");
|
||||||
|
constructorbuilder.append(sqltoCSType(column.getDatatype()))
|
||||||
|
.append(" ").append(shortcolumnname).append(", ");
|
||||||
|
|
||||||
|
innerconstructorbuilder.append(addChars()).append(addChars()).append("this.").append(shortcolumnname).append("=").append(shortcolumnname).append(";\n");
|
||||||
|
|
||||||
|
if (column.isAutoincrement()) {
|
||||||
|
autoincrementCounter++;
|
||||||
|
holdAutoincrementStartAt = column.getAutoincrementStartAt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
constructorbuilder.delete(constructorbuilder.length()-2,constructorbuilder.length());
|
||||||
|
constructorbuilder.append(") {\n").append(innerconstructorbuilder).append(addChars()).append("}\n");
|
||||||
|
|
||||||
|
sqlScript.append(addChars()).append(constructorbuilder).append(addChars()).append("public ").append(table.getTablename().substring(table.getTablenamePraefix().length())).append("(){}\n");
|
||||||
|
|
||||||
|
this.sqlScript.append("}");
|
||||||
|
/*
|
||||||
|
if (autoincrementCounter == 1) {
|
||||||
|
this.sqlScript.append("auto_increment=").append(holdAutoincrementStartAt);
|
||||||
|
} else if (autoincrementCounter > 1) {
|
||||||
|
throw new ZZ20("There is only one autoincrement value allowed, and it must be the key. Table: " + table.getTablename());
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
this.sqlScript.append("\n\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
this.sqlScript.append("\n/* TODO: foreign keys-#################################################\n");
|
||||||
|
for (T1 table : this.tables) {
|
||||||
|
|
||||||
|
ArrayList<C3> fkColumns = new ArrayList<>();
|
||||||
|
for (C3 column : table.getSkjdfhgdskjf674534()) {
|
||||||
|
if (column.getReferencesTo() != null) fkColumns.add(column);
|
||||||
|
}
|
||||||
|
if (fkColumns.size() == 0)
|
||||||
|
continue;
|
||||||
|
this.sqlScript = this.sqlScript.append("alter table ").append(table.getTablename()).append("\n");
|
||||||
|
|
||||||
|
|
||||||
|
HashSet<String> tableNamesInfkColumns = new HashSet<>();
|
||||||
|
for (C3 fksamecol : fkColumns) {
|
||||||
|
tableNamesInfkColumns.add(fksamecol.getReferencesTo().tablename());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
ArrayList<C3> equalColumnsBecauseRecursive = new ArrayList<>();
|
||||||
|
for (int i = 0; i < fkColumns.size(); i++) {
|
||||||
|
for (int j = i + 1; j < fkColumns.size(); j++) {
|
||||||
|
if (fkColumns.get(i).getReferencesTo().tablename().equals(fkColumns.get(j).getReferencesTo().tablename()) && fkColumns.get(i).getReferencesTo().columnname().equals(fkColumns.get(j).getReferencesTo().columnname())) {
|
||||||
|
equalColumnsBecauseRecursive.add(fkColumns.get(i));
|
||||||
|
equalColumnsBecauseRecursive.add(fkColumns.get(j));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (C3 col : equalColumnsBecauseRecursive) {
|
||||||
|
this.sqlScript.append("add foreign key (");
|
||||||
|
this.sqlScript = this.sqlScript.append(col.getColumnName()).append(")").append(" references ").append(col.getReferencesTo().tablename()).append("(").append(col.getReferencesTo().columnname()).append(")").append(" on delete restrict on update restrict,\n");
|
||||||
|
|
||||||
|
tableNamesInfkColumns.remove(col.getReferencesTo().tablename());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
for (String tableName : tableNamesInfkColumns) {
|
||||||
|
|
||||||
|
this.sqlScript.append("add foreign key (");
|
||||||
|
StringBuilder columnsOrig = new StringBuilder();
|
||||||
|
StringBuilder refColumns = new StringBuilder(" references " + tableName + "(");
|
||||||
|
for (C3 fkcolumn : fkColumns) {
|
||||||
|
if (tableName.equals(fkcolumn.getReferencesTo().tablename())) {
|
||||||
|
columnsOrig.append(fkcolumn.getColumnName()).append(",");
|
||||||
|
refColumns.append(fkcolumn.getReferencesTo().columnname()).append(",");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
columnsOrig = new StringBuilder(columnsOrig.substring(0, columnsOrig.length() - 1));
|
||||||
|
columnsOrig.append(")");
|
||||||
|
refColumns = new StringBuilder(refColumns.substring(0, refColumns.length() - 1));
|
||||||
|
refColumns.append(")");
|
||||||
|
this.sqlScript.append(columnsOrig).append(refColumns).append(" on delete restrict on update restrict,\n");
|
||||||
|
}
|
||||||
|
this.sqlScript.replace(this.sqlScript.length() - 2, this.sqlScript.length(), ";\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
this.sqlScript.append("*/");
|
||||||
|
|
||||||
|
return this.sqlScript;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private void allColumnsSetForGeneration() throws ZZ20 {
|
private void allColumnsSetForGeneration() throws ZZ20 {
|
||||||
for (T1 table : this.tables) {
|
for (T1 table : this.tables) {
|
||||||
for (C3 column : table.getSkjdfhgdskjf674534()) {
|
for (C3 column : table.getSkjdfhgdskjf674534()) {
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
<excludePattern pattern=".gitignore" />
|
<excludePattern pattern=".gitignore" />
|
||||||
<excludePattern pattern="*.bat" />
|
<excludePattern pattern="*.bat" />
|
||||||
<excludePattern pattern="*.iml" />
|
<excludePattern pattern="*.iml" />
|
||||||
|
<excludePattern pattern="*.jar" />
|
||||||
</content>
|
</content>
|
||||||
<orderEntry type="inheritedJdk" />
|
<orderEntry type="inheritedJdk" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
Loading…
x
Reference in New Issue
Block a user