コード例 #1
0
void DatabaseCanvas::CreateFKConstraint(const DatabaseTable *fkTable, const std::vector<FKField *> &foreignKeyField)
{
    Constraint* pConstr = NULL;
    bool found = false;
    for( std::vector<MyErdTable *>::iterator it = m_displayedTables.begin(); it < m_displayedTables.end() && !found; it ++ )
    {
        if( const_cast<DatabaseTable &>( (*it)->GetTable() ).GetTableName() == foreignKeyField.at( 0 )->GetReferencedTableName() )
        {
            found = true;
            for( std::vector<FKField *>::const_iterator it4 = foreignKeyField.begin(); it4 < foreignKeyField.end(); it4++ )
            {
                if( ((DrawingView *) m_view)->GetViewType() == DatabaseView )
                {
                    pConstr = new DatabaseConstraint( (*it4)->GetFKName() );
                    pConstr->SetLocalColumn( (*it4)->GetOriginalFieldName() );
                    pConstr->SetRefColumn( (*it4)->GetReferencedFieldName() );
                    pConstr->SetRefTable( (*it4)->GetReferencedTableName() );
                    pConstr->SetType( Constraint::foreignKey );
                    pConstr->SetFKDatabaseTable( fkTable );
                    pConstr->SetPGMatch( (*it4)->GetMatchOPtion() );
                    switch( foreignKeyField.at( 0 )->GetOnUpdateConstraint() )
                    {
                        case RESTRICT_UPDATE:
                            pConstr->SetOnUpdate( Constraint::restrict );
                            break;
                        case SET_NULL_UPDATE:
                            pConstr->SetOnUpdate( Constraint::setNull );
                            break;
                        case SET_DEFAULT_UPDATE:
                        case CASCADE_UPDATE:
                            pConstr->SetOnUpdate( Constraint::cascade );
                            break;
                        case NO_ACTION_UPDATE:
                            pConstr->SetOnUpdate( Constraint::noAction );
                            break;
                    }
                    switch( foreignKeyField.at( 0 )->GetOnDeleteConstraint() )
                    {
                        case RESTRICT_DELETE:
                            pConstr->SetOnUpdate( Constraint::restrict );
                            break;
                        case SET_NULL_DELETE:
                            pConstr->SetOnUpdate( Constraint::setNull );
                            break;
                        case SET_DEFAULT_DELETE:
                        case CASCADE_DELETE:
                            pConstr->SetOnUpdate( Constraint::cascade );
                            break;
                        case NO_ACTION_DELETE:
                            pConstr->SetOnUpdate( Constraint::noAction );
                            break;
                    }
                    (*it)->GetShapeManager()->CreateConnection( (*it)->GetId(), dynamic_cast<DrawingDocument *>( m_view->GetDocument() )->GetReferencedTable( foreignKeyField.at( 0 )->GetReferencedTableName() )->GetId(), new ErdForeignKey( pConstr, ((DrawingView *) m_view)->GetViewType(), GetDiagramManager() ), sfDONT_SAVE_STATE );
                }
            }
        }
    }
}
コード例 #2
0
void DatabaseCanvas::DisplayTables(std::vector<wxString> &selections, wxString &query)
{
    std::vector<MyErdTable *> tables = ((DrawingDocument *)m_view->GetDocument())->GetTables();
    for( std::vector<MyErdTable *>::iterator it = tables.begin(); it < tables.end(); it++ ) 
    {
        if( !IsTableDisplayed( (*it)->GetTableName() ) )
        {
            std::vector<Field *> fields = const_cast<DatabaseTable &>( (*it)->GetTable() ).GetFields();
            m_pManager.AddShape( (*it), NULL, startPoint, sfINITIALIZE, sfDONT_SAVE_STATE );
            if( (*it) == tables.back() && dynamic_cast<DrawingView *>( m_view )->GetViewType() == DatabaseView )
                (*it)->Select( true );
            (*it)->UpdateTable();
            if( dynamic_cast<DrawingView *>( m_view )->GetViewType() == QueryView )
            {
                for( std::vector<Field *>::iterator it1 = fields.begin(); it1 < fields.end(); it1++ )
                {
                    dynamic_cast<DrawingView *>( m_view )->GetWherePage()->AppendField( (*it)->GetTableName() + L"." + (*it1)->GetFieldName() );
                    dynamic_cast<DrawingView *>( m_view )->GetHavingPage()->AppendField( (*it)->GetTableName() + L"." + (*it1)->GetFieldName() );
                }
            }
            wxRect rect = (*it)->GetBoundingBox();
            startPoint.x += 200 + rect.GetWidth();
            m_displayedTables.push_back( (*it) );
        }
    }
    for( std::vector<MyErdTable *>::iterator it1 = m_displayedTables.begin(); it1 < m_displayedTables.end(); it1++ )
    {
        wxString name = const_cast<DatabaseTable &>( (*it1)->GetTable() ).GetTableName();
        if( std::find( selections.begin(), selections.end(), name ) == selections.end() )
            selections.push_back( name );
        if( dynamic_cast<DrawingView *>( m_view )->GetViewType() == QueryView )
        {
            query += (*it1)->GetTableName();
            if( it1 != m_displayedTables.end() - 1 )
                query += ", ";
        }
    }
    if( dynamic_cast<DrawingView *>( m_view )->GetViewType() == QueryView )
        query += "\n";
//    Refresh();
    Constraint *pConstr = NULL;
    bool found = false, secondIteration = false;
    for( std::vector<MyErdTable *>::iterator it2 = tables.begin(); it2 < tables.end(); it2++ )
    {
        std::map<int, std::vector<FKField *> > foreignKeys = const_cast<DatabaseTable &>( (*it2)->GetTable() ).GetForeignKeyVector();
        for( std::map<int, std::vector<FKField *> >::iterator it3 = foreignKeys.begin(); it3 != foreignKeys.end(); it3++ )
        {
//            if( ((DrawingView *) m_view)->GetViewType() == DatabaseView )
//                pConstr = new DatabaseConstraint( (*it3).second );
            for( std::vector<FKField *>::iterator it4 = (*it3).second.begin(); it4 < (*it3).second.end(); it4++ )
            {
                wxString referencedTableName = (*it4)->GetReferencedTableName();
                if( std::find( selections.begin(), selections.end(), referencedTableName ) != selections.end() )
                {
                    if( found )
                        secondIteration = true;
                    if( !found )
                    {
                        query += "WHERE ";
                        found = true;
                    }
                    if( ((DrawingView *) m_view)->GetViewType() == QueryView )
                    {
                        pConstr = new QueryConstraint( ((DrawingView *) m_view)->GetViewType() );
                        pConstr->SetLocalColumn( (*it4)->GetOriginalFieldName() );
                        pConstr->SetRefColumn( (*it4)->GetReferencedFieldName() );
                        pConstr->SetRefTable( referencedTableName );
                        pConstr->SetType( QueryConstraint::foreignKey );
                        pConstr->SetFKDatabaseTable( &(*it2)->GetTable() );
                        dynamic_cast<QueryConstraint *>( pConstr )->SetSign( 0 );
                    }
                    if( ((DrawingView *) m_view)->GetViewType() == DatabaseView )
                    {
                        pConstr = new DatabaseConstraint( (*it4)->GetFKName() );
                        pConstr->SetLocalColumns( (*it4)->GetOriginalFields() );
                        pConstr->SetRefColumns( (*it4)->GetReferencedFields() );
                        pConstr->SetLocalColumn( (*it4)->GetOriginalFieldName() );
                        pConstr->SetRefColumn( (*it4)->GetReferencedFieldName() );
                        pConstr->SetRefTable( referencedTableName );
                        pConstr->SetType( QueryConstraint::foreignKey );
                        pConstr->SetFKDatabaseTable( &(*it2)->GetTable() );
                        pConstr->SetPGMatch( (*it4)->GetMatchOPtion() );
                    }
                    if( dynamic_cast<DrawingView *>( m_view )->GetViewType() == QueryView )
                    {
                        if( secondIteration )
                            query += " AND ";
                        query += wxString::Format( "%s.%s = %s.%s", (*it2)->GetTableName(), (*it4)->GetOriginalFieldName(), referencedTableName, (*it4)->GetReferencedFieldName() );
                    }
                    switch( (*it4)->GetOnUpdateConstraint() )
                    {
                        case RESTRICT_UPDATE:
                            pConstr->SetOnUpdate( Constraint::restrict );
                            break;
                        case SET_NULL_UPDATE:
                            pConstr->SetOnUpdate( Constraint::setNull );
                            break;
                        case SET_DEFAULT_UPDATE:
                        case CASCADE_UPDATE:
                            pConstr->SetOnUpdate( Constraint::cascade );
                            break;
                        case NO_ACTION_UPDATE:
                            pConstr->SetOnUpdate( Constraint::noAction );
                            break;
                    }
                    switch( (*it4)->GetOnDeleteConstraint() )
                    {
                        case RESTRICT_DELETE:
                            pConstr->SetOnUpdate( Constraint::restrict );
                            break;
                        case SET_NULL_DELETE:
                            pConstr->SetOnUpdate( Constraint::setNull );
                            break;
                        case SET_DEFAULT_DELETE:
                        case CASCADE_DELETE:
                            pConstr->SetOnUpdate( Constraint::cascade );
                            break;
                        case NO_ACTION_DELETE:
                            pConstr->SetOnUpdate( Constraint::noAction );
                            break;
                    }
                    (*it2)->GetShapeManager()->CreateConnection( (*it2)->GetId(), dynamic_cast<DrawingDocument *>( m_view->GetDocument() )->GetReferencedTable( referencedTableName )->GetId(), new ErdForeignKey( pConstr, ((DrawingView *) m_view)->GetViewType(), m_pManager ), sfDONT_SAVE_STATE );
                }
            }
        }
    }
    query += ";";
    Refresh();
}