KC presentation on CodeGen…

0

On February 21st I did a presentation on CodeGen at AMIS. Just a short presentation on what is possible using this great tool. I have created a couple of questions for the hands-on part of the session.

In case you are wondering what CodeGen is exactly, CodeGen is a freeware tool provided by Quest software and developed by Steven Feuerstein (mainly). CodeGen can generate any kind of textfile (source code, documentation etc) based on any Oracle database object (table/view or program). It can generate any kind of language (SQL, PL/SQL, Java etc) as long as it’s a textfile. The scripts are written in CGML (Code Generation Markup Language) which is a proprietary language to CodeGen. It takes some time to learn, but it is quite powerful. And anything that can’t be done with the standard installation of CodeGen can probably be done by extending CodeGen with your own PL/SQL Code.

For the assignments that were made for the presentation, and their answers (at least, how I would solve them). (I wanted to hide the resolutions, but that doesn’t appear to work on this blog. It does on my own blog (crosspost http://bar-solutions.com/wordpress/?p=221))

Assignment 1: Build a script that shows the contents (describe) of a table.

Resolution 1: This is the resolution using all the different fields of the COL array.

<div>Name Type Nullable Default Comments
-----------------------------------------------
[FOREACH]COL
[COLNAME] [DATA_DECLARATION] [COLNULLABLE] [DATA_DEFAULT] [COMMENTS]
[ENDFOREACH]
</div>

Assignment 2: Build a script to create the table.

Resolution 2:

<div>-- Create table
create table [OBJNAME]
(
[FOREACH]col[BETWEEN],
  [COLNAME] [DATA_DECLARATION]
[ENDFOREACH]
);</div>

Assignment 3: Retrieve DATA from the CD-Collection table, make it into an HTML file.

Resolution 3: Use DEFARRAY to create your own array

<div>[DEFARRAY]cdcollection[AS]
select COL_TITLE TITLE, COL_ARTIST ARTIST
  from cd_collection
[ENDDEFARRAY]
&lt;html&gt;
  &lt;body&gt;
    &lt;h2&gt;My CD Collection&lt;/h2&gt;
    &lt;table border=&quot;1&quot;&gt;
      &lt;tr bgcolor=&quot;#9acd32&quot;&gt;
        &lt;th&gt;Title&lt;/th&gt;
        &lt;th&gt;Artist&lt;/th&gt;
      &lt;/tr&gt;
      [FOREACH]cdcollection
      &lt;tr&gt;
        &lt;td&gt;[TITLE]&lt;/td&gt;
        &lt;td&gt;[ARTIST]&lt;/td&gt;
      &lt;/tr&gt;
      [ENDFOREACH]
    &lt;/table&gt;
  &lt;/body&gt;
&lt;/html&gt;
 </div>

Assignment 4: Create JAVA classes and PL/SQL Code for a table Example taken from: Oracle and Java Stored Procedures http://www.developer.com/db/article.php/3337411

Resolution 4:

Script 1:

<div>[RTO]RTOJavaType[AS]
  [IF]coldatatype[IN]VARCHAR2,CHAR,VARCHAR
    [SETALIAS]_JavaDataType[TO]String
  [ELSIF]coldatatype[IN]NUMBER,FLOAT,DECIMAL,INTEGER,INT
     [SETALIAS]_JavaDataType[TO]float
    [IF]data_declaration[LIKE]%,%
      [SETALIAS]_JavaDataType[TO]float
    [ELSE]
      [SETALIAS]_JavaDataType[TO]int
    [ENDIF]
  [ELSIF]coldatatype[IN]DATE,TIMESTAMP,DATETIME
     [SETALIAS]_JavaDataType[TO]Date
  [ENDIF]
[ENDRTO]
[RTO]RTOJavaSet[AS]
  [IF]coldatatype[IN]VARCHAR2,CHAR,VARCHAR
    [SETALIAS]_JavaSet[TO]setString
  [ELSIF]coldatatype[IN]NUMBER,FLOAT,DECIMAL,INTEGER,INT
     [SETALIAS]_JavaSet[TO]setfloat
    [IF]data_declaration[LIKE]%,%
      [SETALIAS]_JavaSet[TO]setfloat
    [ELSE]
      [SETALIAS]_JavaSet[TO]setint
    [ENDIF]
  [ELSIF]coldatatype[IN]DATE,TIMESTAMP,DATETIME
     [SETALIAS]_JavaSet[TO]setDate
  [ENDIF]
[ENDRTO]

import java.sql.*;
import oracle.jdbc.*;

public class [OBJNAME]Manager \{

   //Add a record to the database.
   public static void add[OBJNAME](
   [FOREACH]col[BETWEEN],
   [INCLUDE]RTOJavaType
     {_JavaDataType} [COLNAME]
   [ENDFOREACH]
      ) \{

      System.out.println(&quot;Creating new record...&quot;);

      try \{
         Connection conn =
            DriverManager.getConnection(&quot;jdbc:default:connection:&quot;);

         [SETALIAS]_Columns[TO]
         [SETALIAS]_PlaceHolder[TO]
         [SETALIAS]_first[TO]Y
         [FOREACH]COL
         [IF]{_first}[EQ]Y
           [SETALIAS]_Columns[TO]{_Columns}[COLNAME]
           [SETALIAS]_PlaceHolder[TO]{_PlaceHolder}?
           [SETALIAS]_first[TO]N
         [ELSE]
           [SETALIAS]_Columns[TO]{_Columns},[COLNAME]
           [SETALIAS]_PlaceHolder[TO]{_PlaceHolder},?
         [ENDIF]
         [ENDFOREACH]
         String sql =
            &quot;INSERT INTO [OBJNAME] &quot; +
            &quot;({_Columns}) &quot; +
            &quot;VALUES ({_PlaceHolder})&quot;;
         PreparedStatement pstmt = conn.prepareStatement(sql);
         [SET]QE_TOOLS.COUNTER[TO]0
         [FOREACH]col
           [SET]QE_TOOLS.COUNTER[TO]{QE_TOOLS.COUNTER+1}
           [INCLUDE]RTOJavaSet
         pstmt.{_JavaSet}({QE_TOOLS.COUNTER},[COLNAME]);
         [ENDFOREACH]
         pstmt.executeUpdate();
         pstmt.close();
         \}
      catch(SQLException e) \{
         System.err.println(&quot;ERROR! Adding Employee: &quot;
           + e.getMessage());
         \}
   \}
\}
 </div>

Script 2:

<div>loadjava -u scott/tiger -v -resolve [OBJNAME]Manager.java
--check if it is valid
SELECT object_name, object_type, status
  FROM user_objects
 WHERE object_type LIKE 'JAVA%'
   AND object_name LIKE '%[OBJNAME]%';
 </div>

Script 3:

<div>[RTO]RTOJavaType[AS]
  [IF]coldatatype[IN]VARCHAR2,CHAR,VARCHAR
    [SETALIAS]_JavaDataType[TO]java.lang.String
  [ELSIF]coldatatype[IN]NUMBER,FLOAT,DECIMAL,INTEGER,INT
     [SETALIAS]_JavaDataType[TO]float
    [IF]data_declaration[LIKE]%,%
      [SETALIAS]_JavaDataType[TO]float
    [ELSE]
      [SETALIAS]_JavaDataType[TO]int
    [ENDIF]
  [ELSIF]coldatatype[IN]DATE,TIMESTAMP,DATETIME
     [SETALIAS]_JavaDataType[TO]Date
  [ENDIF]
[ENDRTO]
CREATE OR REPLACE PROCEDURE add_[OBJNAME] (
  [FOREACH]col[BETWEEN],
  [COLNAME] [COLDATATYPE]
  [ENDFOREACH]
  )
  AS LANGUAGE JAVA
  NAME '[OBJNAME]Manager.add[OBJNAME](
  [FOREACH]col[BETWEEN],
  [INCLUDE]RTOJavaType
  {_JavaDataType}
  [ENDFOREACH]
  )';
/</div>

Share.

About Author

Patrick Barel is a PL/SQL Developer for AMIS Services in the Netherlands. Besides working with SQL and PL/SQL did he co-develop CodeGen and wrote different plug-ins for PL/SQL Developer. He publishes articles on the AMIS Technology blog and on his own blog http://blog.bar-solutions.com/.

Comments are closed.