You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by js...@apache.org on 2002/09/16 17:18:30 UTC

cvs commit: jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder OracleBuilder.java

jstrachan    2002/09/16 08:18:29

  Modified:    sql/src/java/org/apache/commons/sql/model Table.java
               sql/src/java/org/apache/commons/sql/builder
                        OracleBuilder.java
  Log:
  Added support for auto-increment columns to Oracle using a sequence and a pre-commit trigger
  
  Revision  Changes    Path
  1.4       +15 -0     jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/model/Table.java
  
  Index: Table.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/model/Table.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Table.java	16 Sep 2002 14:51:08 -0000	1.3
  +++ Table.java	16 Sep 2002 15:18:29 -0000	1.4
  @@ -156,4 +156,19 @@
           return answer;
       }
   
  +    /**
  +     * @return the auto increment column, if there is one, otherwise null is returned
  +     */
  +    public Column getAutoIncrementColumn() 
  +    {
  +        for (Iterator iter = getColumns().iterator(); iter.hasNext(); ) 
  +        {
  +            Column column = (Column) iter.next();
  +            if ( column.isAutoIncrement() )
  +            {
  +                return column;
  +            }
  +        }
  +        return null;
  +    }
   }
  
  
  
  1.4       +42 -0     jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/OracleBuilder.java
  
  Index: OracleBuilder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/OracleBuilder.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- OracleBuilder.java	12 Sep 2002 15:03:59 -0000	1.3
  +++ OracleBuilder.java	16 Sep 2002 15:18:29 -0000	1.4
  @@ -101,7 +101,49 @@
           println( text );
       }
       
  +    public void createTable(Table table) throws IOException {
  +        // lets create any sequences
  +        Column column = table.getAutoIncrementColumn();
  +        if (column != null) {
  +            createSequence(table, column);
  +        }
  +        super.createTable(table);
  +        if (column != null) {
  +            createSequenceTrigger(table, column);
  +        }
  +    }
  +    
  +    
       protected void printAutoIncrementColumn() throws IOException { 
           //print( "AUTO_INCREMENT" );
  +    }
  +    
  +    /**
  +     * Creates a sequence so that values can be auto incremented
  +     */
  +    protected void createSequence(Table table, Column column) throws IOException {
  +        print( "create sequence " );
  +        print( table.getName() );
  +        print( "_seq" );
  +        printEndOfStatement();
  +    }
  +    
  +    /**
  +     * Creates a trigger to auto-increment values
  +     */
  +    protected void createSequenceTrigger(Table table, Column column) throws IOException {
  +        print( "create or replace trigger " );
  +        print( table.getName() );
  +        print( "_trg before insert on " );
  +        println( table.getName() );
  +        println( "for each row" );
  +        println( "begin" );
  +        print( "select " );
  +        print( table.getName() );
  +        print( "_seq.nextval into :new." );
  +        print( column.getName() );
  +        println( " from dual;" );
  +        print( "end" );
  +        printEndOfStatement();
       }
   }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>