Skip to content

Commit

Permalink
dbmodel: Column: added GeneratedInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
tbrugz committed Oct 17, 2024
1 parent b6ed085 commit 277dd76
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/tbrugz/sqldump/SchemaModelScriptDumper.java
Original file line number Diff line number Diff line change
Expand Up @@ -622,16 +622,16 @@ static void addGrantToMap(Map<String, List<PrivilegeWithColumns>> grantMap, Gran
boolean added = false;
for(int i=0;i<privs.size();i++) {
if(privs.get(i).priv.equals(g.getPrivilege())) {
if(g.getColumn()!=null) {
privs.get(i).columns.add(g.getColumn());
if(g.getColumns()!=null) {
privs.get(i).columns.addAll(g.getColumns());
}
added = true;
}
}
if(!added) {
PrivilegeWithColumns pwc = new PrivilegeWithColumns(g.getPrivilege());
if(g.getColumn()!=null) {
pwc.columns.add(g.getColumn());
if(g.getColumns()!=null) {
pwc.columns.addAll(g.getColumns());
}
privs.add(pwc);
}
Expand Down
57 changes: 47 additions & 10 deletions src/tbrugz/sqldump/dbmodel/Column.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@ public static boolean isBoolean(String type) {
}

}

/* XXX generated/virtual columns
https://www.postgresql.org/docs/current/ddl-generated-columns.html
https://blog.jooq.org/2012/02/19/subtle-sql-differences-identity-columns/
*/
public static class GeneratedInfo {
boolean generated;
boolean generatedAlways = true;
boolean identity;
boolean stored;
boolean virtual;
boolean autoIncrement; // GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY ; start-with, increment
}

private static final long serialVersionUID = 1L;
static final Log log = LogFactory.getLog(Column.class);
Expand All @@ -147,7 +160,7 @@ public static boolean isBoolean(String type) {
boolean nullable = true;
String defaultValue;
String remarks;
Boolean autoIncrement; // GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY ; start-with, increment - see: https://blog.jooq.org/2012/02/19/subtle-sql-differences-identity-columns/
GeneratedInfo generated;
//XXX add transient String tableName; //??
//XXX Boolean updateable; //?? - http://english.stackexchange.com/questions/56431/correct-spelling-updatable-or-updateable
Integer ordinalPosition; //XXXdone add column position in table? nice for column compare...
Expand Down Expand Up @@ -240,18 +253,30 @@ public String getColumnConstraints() {
/*if((autoIncrement!=null && autoIncrement)) {
log.info("auto-increment["+name+"]: useAutoIncrement? "+ColTypeUtil.useAutoIncrement()+" [dbid="+ColTypeUtil.dbid+"]");
}*/
return getDefaultSnippet()
return (isGenerated()?"":getDefaultSnippet())
+getNullableSnippet()
+(ColTypeUtil.useAutoIncrement()?
((autoIncrement!=null && autoIncrement)?" auto_increment":"")
:"");
+getGeneratedSnippet()
;
}

public String getFullColumnConstraints() {
return getDefaultSnippet()
return (isGenerated()?"":getDefaultSnippet())
+getFullNullableSnippet()
+(ColTypeUtil.useAutoIncrement()?
((autoIncrement!=null && autoIncrement)?" auto_increment":"")
+getGeneratedSnippet()
;
}

public String getGeneratedSnippet() {
if(generated==null) { return ""; }
return
( generated.generated ? " generated "+(generated.generatedAlways?"always":"by default")+
" as " + (generated.identity?"identity":"("+defaultValue+")") +
( generated.stored ? " stored" : "" ) +
( generated.virtual ? " virtual" : "" )
: ""
) +
(ColTypeUtil.useAutoIncrement()?
((generated.autoIncrement)?" auto_increment":"")
:"");
}

Expand Down Expand Up @@ -323,11 +348,14 @@ public void setNullable(boolean nullable) {
}

public Boolean getAutoIncrement() {
return autoIncrement;
return generated!=null?generated.autoIncrement:null;
}

public void setAutoIncrement(Boolean autoIncrement) {
this.autoIncrement = autoIncrement;
if(generated==null) {
initGeneratedInfo();
}
this.generated.autoIncrement = autoIncrement;
}

public Integer getOrdinalPosition() {
Expand All @@ -338,4 +366,13 @@ public void setOrdinalPosition(Integer ordinalPosition) {
this.ordinalPosition = ordinalPosition;
}

void initGeneratedInfo() {
generated = new GeneratedInfo();
}

public boolean isGenerated() {
if(generated==null) return false;
return generated.generated;
}

}
39 changes: 38 additions & 1 deletion src_test/tbrugz/sqldump/dbmodel/ColumnTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,42 @@ public void testColumnAutoIncMysql() {
ColTypeUtilTest.updateDbId("mysql");
Assert.assertEquals("`one` numeric(3) auto_increment", c.getDefinition());
}


@Test
public void testColumnGenerated1() {
Column c = getColumn();
Column.GeneratedInfo gi = new Column.GeneratedInfo();
gi.generated = true;
gi.identity = true;
c.generated = gi;
SQLIdentifierDecorator.dumpQuoteAll = true;
ColTypeUtilTest.updateDbId("pgsql");
Assert.assertEquals("\"one\" numeric(3) generated always as identity", c.getDefinition());
}

@Test
public void testColumnGenerated2() {
Column c = getColumn();
Column.GeneratedInfo gi = new Column.GeneratedInfo();
gi.generated = true;
gi.identity = true;
gi.generatedAlways = false;
c.generated = gi;
SQLIdentifierDecorator.dumpQuoteAll = true;
ColTypeUtilTest.updateDbId("pgsql");
Assert.assertEquals("\"one\" numeric(3) generated by default as identity", c.getDefinition());
}

@Test
public void testColumnGeneratedWithDefault() {
Column c = getColumn();
c.setDefaultValue("two / 2");
Column.GeneratedInfo gi = new Column.GeneratedInfo();
gi.generated = true;
c.generated = gi;
SQLIdentifierDecorator.dumpQuoteAll = true;
ColTypeUtilTest.updateDbId("pgsql");
Assert.assertEquals("\"one\" numeric(3) generated always as (two / 2)", c.getDefinition());
}

}

0 comments on commit 277dd76

Please sign in to comment.