Package org.apache.ws.jaxme.sqls

Examples of org.apache.ws.jaxme.sqls.SQLGenerator


  /** <p>Basic test for creating a <code>CREATE TABLE</code>
   * statement.</p>
   */
  public void testBasicCreate() {
    Table table = getBasicTable();
    SQLGenerator generator = sqlFactory.newSQLGenerator();
    generator.setLineTerminator("\n");
    Collection statements = generator.getCreate(table.getSchema(), true);
    Iterator iter = statements.iterator();
    assertTrue(iter.hasNext());
    assertEquals("CREATE SCHEMA MySchema", iter.next());
    assertTrue(iter.hasNext());
    assertEquals("CREATE TABLE MySchema.MyTable (\n" +
View Full Code Here


  /** <p>Basic test for creating an <code>INSERT</code> statement.</p>
   */
  public void testBasicInsert() {
    Table table = getBasicTable();
    InsertStatement insertStatement = table.getInsertStatement();
    SQLGenerator generator = sqlFactory.newSQLGenerator();
    generator.setLineTerminator("\n");
    String s = generator.getQuery(insertStatement);
    assertEquals("INSERT INTO MySchema.MyTable (MyIndex, MyName, MyDate) VALUES (?, ?, ?)", s);
  }
View Full Code Here

  /** <p>Basic test for creating a <code>SELECT</code> statement.</p>
   */
  public void testBasicSelect() {
    Table table = getBasicTable();
    SelectStatement selectStatement = table.getSelectStatement();
    SQLGenerator generator = sqlFactory.newSQLGenerator();
    generator.setLineTerminator("\n");
    String s = generator.getQuery(selectStatement);
    assertEquals("SELECT MyIndex, MyName, MyDate FROM MySchema.MyTable", s);
  }
View Full Code Here

  /** <p>Basic test for creating an <code>UPDATE</code> statement.</p>
   */
  public void testBasicUpdate() {
    Table table = getPrimaryKeyTable();
    UpdateStatement updateStatement = table.getUpdateStatement();
    SQLGenerator generator = sqlFactory.newSQLGenerator();
    generator.setLineTerminator("\n");
    String s = generator.getQuery(updateStatement);
    assertEquals("UPDATE MySchema.MyTable SET MyName=?, MyDate=? WHERE MyIndex=?", s);
  }
View Full Code Here

  /** <p>Basic test for creating an <code>DELETE</code> statement.</p>
   */
  public void testBasicDelete() {
    Table table = getPrimaryKeyTable();
    DeleteStatement deleteStatement = table.getDeleteStatement();
    SQLGenerator generator = sqlFactory.newSQLGenerator();
    generator.setLineTerminator("\n");
    String s = generator.getQuery(deleteStatement);
    assertEquals("DELETE FROM MySchema.MyTable WHERE MyIndex=?", s);
  }
View Full Code Here

  /** <p>Test for a FOREIGN KEY definition.</p>
   */
  public void testCreateForeignKey() {
     Table table = getPrimaryKeyTable();
     Table otherTable = getForeignKeyTable(table);
     SQLGenerator generator = sqlFactory.newSQLGenerator();
     Collection c = generator.getCreate(otherTable);
     assertEquals(1, c.size());
     String expect = getCreateForeignKeyResult();
     String got = (String) c.iterator().next();
     assertEquals(expect, got);
  }
View Full Code Here

     CombinedConstraint cc = statement.getWhere();
     BooleanConstraint bc = cc.createEQ();
     bc.addPart(tableReference.newColumnReference("MyIndex"));
     bc.addPlaceholder();

     SQLGenerator generator = sqlFactory.newSQLGenerator();
     generator.setLineTerminator("\n");
     String got = generator.getQuery(statement);
     String expect = "SELECT MyIndex, RefIndex, Company FROM MySchema.OtherTable JOIN MySchema.MyTable ON RefIndex=MyIndex WHERE MyIndex=?";
     assertEquals(expect, got);
  }
View Full Code Here

  public void testComposedPrimaryKey() {
    Table table = getComposedKeyTable();

    SelectStatement statement = table.getSelectStatement();
    statement.getWhere().addColumnSetQuery(table.getPrimaryKey(), statement.getTableReference());
    SQLGenerator generator = sqlFactory.newSQLGenerator();
    generator.setLineTerminator("\n");
    String s = generator.getQuery(statement);
    assertEquals("SELECT MyIndex, MyName, MyDate, VerNum FROM MySchema.MyTable WHERE (MyIndex=? AND VerNum=?)", s);
  }
View Full Code Here

  }

  /** <p>Test for index names.</p>
   */
  public void testIndexNames() {
    SQLGenerator gen = sqlFactory.newSQLGenerator();
    Table table = getBasicTable();
    for (int i = 0;  i < 10;  i++) {
      Index index = table.newIndex();
      index.addColumn("MyName");
      String s = (String) gen.getCreate(index).iterator().next();
      assertEquals("CREATE INDEX MyTable_I" + i + " ON MySchema.MyTable (MyName)", s);
    }

    Collection coll = gen.getCreate(schema, true);
    String[] cmds = (String[]) coll.toArray(new String[coll.size()]);
    assertEquals(12, cmds.length);
    assertEquals("CREATE SCHEMA MySchema", cmds[0]);
    assertEquals("CREATE TABLE MySchema.MyTable (  MyIndex INT NOT NULL,  MyName VARCHAR(60) NOT NULL,  MyDate DATE)", cmds[1]);
    for (int i = 0;  i < 10;  i++) {
View Full Code Here

  }

  /** <p>Test for subselects.</p>
   */
  public void testSubSelect() {
    SQLGenerator gen = sqlFactory.newSQLGenerator();
    Table table = getComposedKeyTable();

    Table otherTable = table.getSchema().newTable("OtherTable");
    Column otherIndex = otherTable.newColumn("MyIndex", Column.Type.INTEGER);
    otherTable.newPrimaryKey().addColumn(otherIndex);
    ForeignKey foreignKey = otherTable.newForeignKey(table);
    SelectStatement selectStatement = sqlFactory.newSelectStatement();
    selectStatement.setTable(otherTable);
    DeleteStatement deleteStatement = sqlFactory.newDeleteStatement();
    deleteStatement.setTable(table);
    List columns = new ArrayList();
    for (Iterator iter = table.getColumns();  iter.hasNext()) {
      Column column = (Column) iter.next();
      Column refColumn = otherTable.newColumn("Ref" + column.getName(), column.getType());
      foreignKey.addColumnLink(refColumn, column);
      if (column.isPrimaryKeyPart()) {
        selectStatement.addResultColumn(selectStatement.getTableReference().newColumnReference(refColumn));
        columns.add(deleteStatement.getTableReference().newColumnReference(column));
      }
    }
    BooleanConstraint eq = selectStatement.getWhere().createEQ();
    eq.addPart(selectStatement.getTableReference().newColumnReference("RefMyName"));
    eq.addPlaceholder();

    BooleanConstraint bc = deleteStatement.getWhere().createIN();
    bc.addPart((ColumnReference[]) columns.toArray(new ColumnReference[columns.size()]));
    bc.addPart(selectStatement);
    String expect = "DELETE FROM MySchema.MyTable WHERE (MyIndex, VerNum) IN (SELECT RefMyIndex, RefVerNum FROM MySchema.OtherTable WHERE RefMyName=?)";
    String got = gen.getQuery(deleteStatement);
    assertEquals(expect, got);
  }
View Full Code Here

TOP

Related Classes of org.apache.ws.jaxme.sqls.SQLGenerator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.