Skip to content

Commit

Permalink
oracle: named not null constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
llsand committed Oct 30, 2023
1 parent 406ca6a commit 4128d6e
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ enum LobDeduplicateType :
deduplicate="deduplicate"|keep_duplicates="keep_duplicates";

Column :
(name=DBNAME | name_string=STRING) data_type=DataType ("(" precision=INT ("," scale=INT )? (byteorchar=CharType)? ")" )? (unsigned?="unsigned")? (with_time_zone="with_time_zone")? ("type" object_type=STRING)? ("default" default_value=STRING | "as" "(" default_value=STRING ")" virtual="virtual")? ("defaultname" default_name=DBNAME)? (identity=ColumnIdentity)? (notnull?="not" "null")? (",")?;
(name=DBNAME | name_string=STRING) data_type=DataType ("(" precision=INT ("," scale=INT )? (byteorchar=CharType)? ")" )? (unsigned?="unsigned")? (with_time_zone="with_time_zone")? ("type" object_type=STRING)? ("default" default_value=STRING | "as" "(" default_value=STRING ")" virtual="virtual")? ("defaultname" default_name=DBNAME)? (identity=ColumnIdentity)? (("constraint" not_null_constraint_name=DBNAME)? notnull?="not" "null")? (",")?;

enum DataType :
number="number"|blob="blob"|clob="clob"|nclob="nclob"|varchar2="varchar2"|nvarchar2="nvarchar2"|char="char"|date="date"|xmltype="xmltype"|timestamp="timestamp"|rowid="rowid"|raw="raw"|long_raw="long_raw"|float="float"|long="long"|object="object"|tinyint="tinyint"|smallint="smallint"|mediumint="mediumint"|int="int"|bigint="bigint"|bit="bit"|urowid="urowid"|boolean="boolean";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public void alterSequenceIfNeeded( StatementBuilderAlter p1, SequenceDiff pSeque
.ifDifferent( SEQUENCE__CYCLE )//
.failIfAdditionsOnly()//
.handle( p -> p.addStmt( "alter sequence " + pSequenceDiff.sequence_nameNew + " " + pSequenceDiff.cycleNew.getLiteral() ) );

p1.handleAlterBuilder()//
.ifDifferent( SEQUENCE__CACHE )//
.ignoreIfAdditionsOnly()//
Expand Down Expand Up @@ -368,11 +368,11 @@ public void createSequnece( StatementBuilder p, SequenceDiff pSequenceDiff, Data
if( pSequenceDiff.cacheNew != null )
{
if ( pSequenceDiff.cacheNew.compareTo(BigInteger.ONE) > 0 ) {
p.stmtAppend( "cache " + pSequenceDiff.cacheNew );
p.stmtAppend( "cache " + pSequenceDiff.cacheNew );
} else {
p.stmtAppend( "nocache ");
}

}

if( pSequenceDiff.orderNew != null )
Expand Down Expand Up @@ -419,6 +419,10 @@ public void recreateColumn( StatementBuilder p, TableDiff pTableDiff, ColumnDiff
if( pColumnDiff.notnullNew )
{
p.stmtStart( "alter table " + pTableDiff.nameNew + " modify ( " + pColumnDiff.nameNew );
if(pColumnDiff.not_null_constraint_nameNew != null) {
p.stmtAppend( "constraint" );
p.stmtAppend( pColumnDiff.not_null_constraint_nameNew );
}
p.stmtAppend( "not null" );
p.stmtAppend( ")" );
p.stmtDone();
Expand Down Expand Up @@ -513,22 +517,32 @@ public void alterColumnIfNeeded( StatementBuilderAlter p1, TableDiff pTableDiff,

p1.handleAlterBuilder()//
.ifDifferent( COLUMN__NOTNULL )//
.ifDifferent( COLUMN__NOT_NULL_CONSTRAINT_NAME )//
.ignoreIfAdditionsOnly( pColumnDiff.notnullNew )//
.handle( p ->
{
p.stmtStartAlterTable( pTableDiff );
p.stmtAppend( "modify ( " + pColumnDiff.nameNew );
if( pColumnDiff.notnullNew == false )
{
p.stmtAppend( "null" );
}
else
{
p.stmtAppend( "not null" );
}
p.stmtAppend( ")" );
p.stmtDone();
} );
if (pColumnDiff.notnullIsEqual) {
p.stmtStartAlterTable(pTableDiff);
p.stmtAppend("modify ( " + pColumnDiff.nameNew);
p.stmtAppend("null");
p.stmtAppend(")");
p.stmtDone();
}

p.stmtStartAlterTable(pTableDiff);
p.stmtAppend("modify ( " + pColumnDiff.nameNew);
if (pColumnDiff.notnullNew == false) {
p.stmtAppend("null");
} else {
if (pColumnDiff.not_null_constraint_nameNew != null) {
p.stmtAppend("constraint");
p.stmtAppend(pColumnDiff.not_null_constraint_nameNew);
}
p.stmtAppend("not null");
}
p.stmtAppend(")");
p.stmtDone();
});
}

public void createPrimarykey( StatementBuilder p, TableDiff pTableDiff )
Expand Down Expand Up @@ -2268,9 +2282,12 @@ protected String createColumnCreatePart( ColumnDiff pColumnDiff, boolean pWithou

if( pColumnDiff.notnullNew )
{
if( !pWithoutNotNull )
{
lReturn = lReturn + " not null";
if( !pWithoutNotNull ) {
if (pColumnDiff.not_null_constraint_nameNew != null) {
lReturn = lReturn + " constraint " + pColumnDiff.not_null_constraint_nameNew + " not null";
} else {
lReturn = lReturn + " not null";
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,7 @@ public CharType byteorcharCleanValueIfNeeded(CharType pValue) {
} );
DiffRepository.getColumnMerge().byteorcharDefaultValue = lDefaultCharType;
DiffRepository.getColumnMerge().nameIsConvertToUpperCase = true;
DiffRepository.getColumnMerge().not_null_constraint_nameIsConvertToUpperCase = true;
DiffRepository.getColumnMerge().object_typeIsConvertToUpperCase = true;

DiffRepository.setTableMerge( new TableMerge()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1721,40 +1721,63 @@ protected void useResultSetRow( ResultSet pResultSet ) throws SQLException
" deferrable," + //
" deferred," + //
" constraints.status," + //
" constraints.generated," + //
" search_condition" + //
" from " + getDataDictionaryView( "constraints" ) + //
" where constraint_type = 'C'" + //
" and constraints.generated != 'GENERATED NAME'" + //
" order by table_name," + //
" constraints.generated," + //
" constraint_name" + //
"";

List<Column> columnsWithGeneratedNotNullConstraint = new ArrayList<>();

new WrapperIteratorResultSet( lSql, getCallableStatementProvider() )
{
@Override
protected void useResultSetRow( ResultSet pResultSet ) throws SQLException
{
if( !isIgnoredTable( pResultSet.getString( "table_name" ), pResultSet.getString( "owner" ) ) )
{
Table lTable = findTable( pModel, pResultSet.getString( "table_name" ), pResultSet.getString( "owner" ) );
if (!isIgnoredTable(pResultSet.getString("table_name"), pResultSet.getString("owner"))) {
Table lTable = findTable(pModel, pResultSet.getString("table_name"), pResultSet.getString("owner"));

EnableType lEnableType = getEnableType( pResultSet );
String searchCondition = pResultSet.getString("search_condition");
boolean isGenerated = "GENERATED NAME".equals(pResultSet.getString("generated"));

DeferrType lDeferrType = getDeferrType( pResultSet );
Stream<Column> columnStream = ((List<Column>) lTable.getColumns())
.stream()
.filter(Column::isNotnull)
.filter(it -> ("\"" + it.getName_string() + "\" IS NOT NULL").equals(searchCondition));
if (isGenerated) {
columnStream
.forEach(columnsWithGeneratedNotNullConstraint::add);
} else {
Optional<Column> owningNotNullColumn = columnStream
.filter(it -> !columnsWithGeneratedNotNullConstraint.contains(it))
.findFirst();

if (owningNotNullColumn.isPresent()) {
owningNotNullColumn.get().setNot_null_constraint_name(pResultSet.getString("constraint_name"));
columnsWithGeneratedNotNullConstraint.add(owningNotNullColumn.get());
} else {
EnableType lEnableType = getEnableType(pResultSet);

logLoading( "constraint-C", pResultSet.getString( "table_name" ), pResultSet.getString( "constraint_name" ) );
DeferrType lDeferrType = getDeferrType(pResultSet);

Constraint lConstraint = new ConstraintImpl();
logLoading("constraint-C", pResultSet.getString("table_name"), pResultSet.getString("constraint_name"));

lConstraint.setConsName( pResultSet.getString( "constraint_name" ) );
Constraint lConstraint = new ConstraintImpl();

lConstraint.setStatus( lEnableType );
lConstraint.setConsName(pResultSet.getString("constraint_name"));

lConstraint.setDeferrtype( lDeferrType );
lConstraint.setStatus(lEnableType);

lConstraint.setRule( pResultSet.getString( "search_condition" ) );
lConstraint.setDeferrtype(lDeferrType);

lTable.getConstraints().add( lConstraint );
lConstraint.setRule(searchCondition);

lTable.getConstraints().add(lConstraint);
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,10 @@

<template match="notnull">
<if test=". = 'true'">
<if test="../not_null_constraint_name != ''">
<text> constraint </text>
<value-of select="../not_null_constraint_name" />
</if>
<text> not null</text>
</if>
</template>
Expand Down Expand Up @@ -1017,4 +1021,4 @@
<text>"</text>
</template>

</stylesheet>
</stylesheet>
Empty file added orcas_integrationstest/OFF
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
create table tab_modify_constraint
(
id number(15) constraint wrong_name not null
);

create table tab_remove_name
(
id number(15) constraint remove_name not null
);

create table tab_remove_constraint
(
id number(15) constraint remove_name2 not null
);

create table tab_recreate_column
(
id number(20) constraint correct_name not null
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
create table tab_new_constraint
(
id number(15) constraint create_xy not null
);

create table tab_modify_constraint
(
id number(15) constraint update_xy not null
);

create table tab_remove_name
(
id number(15) not null
);

create table tab_remove_constraint
(
id number(15)
);

create table tab_recreate_column
(
id number(15) constraint correct_name not null
);

create table tab_with_two_not_null_con
(
id number(15) constraint one_name not null,
constraint other_name check ("ID" IS NOT NULL)
);

create table tab_noname_not_null_con
(
id number(15) not null
);

create table tab_with_separate_not_null_con
(
id number(15),
constraint other_name2 check ("ID" IS NOT NULL)
);

create table tab_with_unnamed_and_sec
(
id number(15) not null,
constraint other_name3 check ("ID" IS NOT NULL)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dropmode = false
test_extract = true

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
create table tab_new_constraint
(
id number(15) constraint create_xy not null
);

create table tab_modify_constraint
(
id number(15) constraint update_xy not null
);

create table tab_remove_name
(
id number(15) not null
);

create table tab_remove_constraint
(
id number(15)
);

create table tab_recreate_column
(
id number(15) constraint correct_name not null
);

create table tab_with_two_not_null_con
(
id number(15) constraint one_name not null,
constraint other_name check ("\"ID\" IS NOT NULL")
);

create table tab_noname_not_null_con
(
id number(15) not null
);

create table tab_with_separate_not_null_con
(
id number(15),
constraint other_name2 check ("\"ID\" IS NOT NULL")
);

create table tab_with_unnamed_and_sec
(
id number(15) not null,
constraint other_name3 check ("\"ID\" IS NOT NULL")
);

0 comments on commit 4128d6e

Please sign in to comment.