commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject cvs commit: jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder OracleBuilder.java
Date Mon, 16 Sep 2002 15:18:30 GMT
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:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message