Exemplo n.º 1
0
bool ClassGenerateDialog::GenerateFile(Table* pTab, wxTextFile& htmpFile, wxString& hFile, const wxString& classItemName, const wxString& classItemDef, const wxString& classColName, const wxString& classTableName, const wxString& classUtilName)
{
    Constraint* pPK = NULL;
    int colCount = 0;
    int lastEditParam = 0;

    SerializableList::compatibility_iterator node = pTab->GetFirstChildNode();
    while( node ) {
        Constraint* pConstr = wxDynamicCast(node->GetData(),Constraint);
        if (pConstr) {
            if (pConstr->GetType() == Constraint::primaryKey) pPK = pConstr;
        } else colCount++;
        node = node->GetNext();
    }
    Column* pPKCol = NULL;

    if (pPK) {
        SerializableList::compatibility_iterator node = pTab->GetFirstChildNode();
        while( node ) {
            Column* pCol = wxDynamicCast(node->GetData(),Column);
            if (pCol) {
                if (pCol->GetName() == pPK->GetLocalColumn()) pPKCol = pCol;
            }
            node = node->GetNext();
        }
    }

    if( (pPKCol == NULL) && (pTab->IsView() == false) ) {
        m_textLog->AppendText( wxString::Format( _("Table %s has no primary key defined!\n"), pTab->GetName().c_str() ) );
        return false;
    }

    for ( wxString str = htmpFile.GetFirstLine(); !htmpFile.Eof(); str = htmpFile.GetNextLine() ) {
        if (str.Contains(wxT("%%classItemGetters%%"))) {
            SerializableList::compatibility_iterator node = pTab->GetFirstChildNode();
            while( node ) {
                Column* pCol = wxDynamicCast(node->GetData(),Column);
                if (pCol) {
                    hFile << wxString::Format(wxT("\tconst %s Get%s() const {"), GetResTypeName(pCol->GetType()->GetUniversalType()).c_str(), pCol->GetName().c_str()) << "\n";
                    hFile << wxString::Format(wxT("\t\treturn m_%s;"), pCol->GetName().c_str()) << "\n";
                    hFile << wxString::Format(wxT("\t\t}")) << "\n";
                }
                node = node->GetNext();
            }

        } else if (str.Contains(wxT("%%classItemVariables%%"))) {
            SerializableList::compatibility_iterator node = pTab->GetFirstChildNode();
            while( node ) {
                Column* pCol = wxDynamicCast(node->GetData(),Column);
                if (pCol) {
                    hFile << wxString::Format(wxT("\t%s m_%s;"), GetTypeName(pCol->GetType()->GetUniversalType()).c_str(), pCol->GetName().c_str())<< "\n";
                }
                node = node->GetNext();
            }

        } else if (str.Contains(wxT("%%classItemLoading%%"))) {
            SerializableList::compatibility_iterator node = pTab->GetFirstChildNode();
            while( node ) {
                Column* pCol = wxDynamicCast(node->GetData(),Column);
                if (pCol) {
                    hFile << wxString::Format(wxT("\t\tm_%s = pResult->%s(wxT(\"%s\"));"),pCol->GetName().c_str(), GetResultFunction(pCol->GetType()->GetUniversalType()).c_str(), pCol->GetName().c_str()) << "\n";
                }
                node = node->GetNext();
            }

        } else if (str.Contains(wxT("%%classItemBindings%%"))) {
            SerializableList::compatibility_iterator node = pTab->GetFirstChildNode();
            while( node ) {
                Column* pCol = wxDynamicCast(node->GetData(),Column);
                if (pCol) {
                    hFile <<  GetDebeaBinding(pCol) << "\n";
                }
                node = node->GetNext();
            }

        } else if (str.Contains(wxT("%%classItemAddParameters%%"))) {
            bool first = true;
            SerializableList::compatibility_iterator node = pTab->GetFirstChildNode();
            while( node ) {
                Column* pCol = wxDynamicCast(node->GetData(),Column);
                if (pCol) {
                    if( first ) {
                        hFile << wxString::Format(wxT("\t\t\t%s %s"), GetParamTypeName(pCol->GetType()->GetUniversalType()).c_str(), pCol->GetName().c_str()) << "\n";
                        first = false;
                    } else {
                        hFile << wxString::Format(wxT("\t\t\t,%s %s"), GetParamTypeName(pCol->GetType()->GetUniversalType()).c_str(), pCol->GetName().c_str()) << "\n";
                    }
                }
                node = node->GetNext();
            }

        } else if (str.Contains(wxT("%%classItemSetParams%%"))) {
            SerializableList::compatibility_iterator node = pTab->GetFirstChildNode();
            while( node ) {
                Column* pCol = wxDynamicCast(node->GetData(),Column);
                if (pCol ) {
                    hFile <<  wxT("\tm_") + pCol->GetName() + wxT(" = ") + pCol->GetName() + wxT(";") << "\n";
                }
                node = node->GetNext();
            }

        } else if (str.Contains(wxT("%%classColLabelFillGrid%%"))) {
            int i = 0;
            SerializableList::compatibility_iterator node = pTab->GetFirstChildNode();
            while( node ) {
                Column* pCol = wxDynamicCast(node->GetData(),Column);
                if (pCol) {
                    hFile << wxT("\t\tpGrid->AppendCols(1);")<< "\n";
                    hFile << wxString::Format(wxT("\t\tpGrid->SetColLabelValue(%i,wxT(\"%s\"));"),i++,pCol->GetName().c_str())<< "\n";
                }
                node = node->GetNext();
            }

        } else if (str.Contains(wxT("%%classColDataFillGrid%%"))) {
            int i = 0;
            SerializableList::compatibility_iterator node = pTab->GetFirstChildNode();
            while( node ) {
                Column* pCol = wxDynamicCast(node->GetData(),Column);
                if (pCol) {
                    hFile << GetFillData(pCol, i++) << "\n";
                }
                node = node->GetNext();
            }

        } else if (str.Contains(wxT("%%primaryKeyHeader%%"))) {
            if (pPKCol) {
                hFile << wxString::Format(wxT("\t/*! \\brief Return %s from db on the %s base */"),pPKCol->GetParentName().c_str(),pPKCol->GetName().c_str()) << "\n";
                hFile << wxString::Format(wxT("\tstatic %s* GetBy%s(%s %s, DatabaseLayer* pDbLayer);"),classItemName.c_str(),pPKCol->GetName().c_str(), GetTypeName(pPKCol->GetType()->GetUniversalType()).c_str(),pPKCol->GetName().c_str()) << "\n";
            }

        } else if (str.Contains(wxT("%%primaryKeyBody%%"))) {
            if (pPKCol) {
                hFile << wxString::Format(wxT("%s* %s::GetBy%s(%s %s, DatabaseLayer* pDbLayer)"),classItemName.c_str(),classItemName.c_str(),pPKCol->GetName().c_str(), GetTypeName(pPKCol->GetType()->GetUniversalType()).c_str(),pPKCol->GetName().c_str()) << "\n";
                hFile << wxT("{") << "\n";
                hFile << wxT("\tDatabaseResultSet* resSet = NULL;") << "\n";
                hFile << wxT("\tPreparedStatement* pStatement = NULL;") << "\n";
                hFile << wxT("\tif (pDbLayer){") << "\n";
                hFile << wxT("\t\tif (pDbLayer->IsOpen()){") << "\n";

                hFile << wxString::Format(wxT("\t\t\tpStatement = pDbLayer->PrepareStatement(wxT(\"SELECT * FROM %s WHERE %s = ?\"));"),classTableName.c_str(), pPKCol->GetName().c_str()) << "\n";
                hFile << wxString::Format(wxT("\t\t\tpStatement->%s(1, %s);"), GetAddParamFunction(pPKCol->GetType()->GetUniversalType()).c_str(), pPKCol->GetName().c_str()) << "\n";
                hFile << wxT("\t\t\tresSet = pStatement->RunQueryWithResults();") << "\n";
                hFile << wxT("\t\t\t}") << "\n";
                hFile << wxT("\t\t}") << "\n";

                hFile << wxT("\tif (resSet){") << "\n";
                hFile << wxString::Format(wxT("\t\tif (resSet->Next()) return new %s(resSet);"),classItemName.c_str()) << "\n";
                hFile << wxT("\t\tpStatement->Close();") << "\n";
                hFile << wxT("\t\t}") << "\n";
                hFile << wxT("\treturn NULL;") << "\n";


                hFile << wxT("}") << "\n";
            }

        } else if (str.Contains(wxT("%%classUtilsAddParameters%%"))) {
            SerializableList::compatibility_iterator node = pTab->GetFirstChildNode();
            while( node ) {
                Column* pCol = wxDynamicCast(node->GetData(),Column);
                if (pCol) {
                    hFile << wxString::Format(wxT("\t\t\t,%s %s"), GetParamTypeName(pCol->GetType()->GetUniversalType()).c_str(), pCol->GetName().c_str()) << "\n";
                }
                node = node->GetNext();
            }

        } else if (str.Contains(wxT("%%classUtilsAddParametersWithoutPK%%"))) {
            SerializableList::compatibility_iterator node = pTab->GetFirstChildNode();
            while( node ) {
                Column* pCol = wxDynamicCast(node->GetData(),Column);
                if (pCol && ( !pPKCol || (pCol->GetName() != pPKCol->GetName()) ) ) {
                    hFile << wxString::Format(wxT("\t\t\t,%s %s"), GetParamTypeName(pCol->GetType()->GetUniversalType()).c_str(), pCol->GetName().c_str()) << "\n";
                }
                node = node->GetNext();
            }

        } else if (str.Contains(wxT("%%classUtilsDeleteParameters%%"))) {
            if (pPKCol) hFile << wxString::Format(wxT("\t\t\t,%s %s"), GetParamTypeName(pPKCol->GetType()->GetUniversalType()).c_str(), pPKCol->GetName().c_str()) << "\n";

        } else if (str.Contains(wxT("%%classUtilsAddSetParams%%"))) {
            int i = 1;
            SerializableList::compatibility_iterator node = pTab->GetFirstChildNode();
            while( node ) {
                Column* pCol = wxDynamicCast(node->GetData(),Column);
                if (pCol) {
                    hFile << wxString::Format(wxT("\t\t\tpStatement->%s(%i, %s);"), GetAddParamFunction(pCol->GetType()->GetUniversalType()).c_str(),i++,pCol->GetName().c_str()) << "\n";
                }
                node = node->GetNext();
            }
            lastEditParam = i;

        } else if (str.Contains(wxT("%%classUtilsAddSetDebeaParams%%"))) {
            SerializableList::compatibility_iterator node = pTab->GetFirstChildNode();
            while( node ) {
                Column* pCol = wxDynamicCast(node->GetData(),Column);
                if (pCol && ( !pPKCol || (pCol->GetName() != pPKCol->GetName()) ) ) {
                    hFile <<  wxT("\t\tc.m_") + pCol->GetName() + wxT(" = ") + pCol->GetName() + wxT(";") << "\n";
                }
                node = node->GetNext();
            }

        } else if (str.Contains(wxT("%%classUtilsAddDelParams%%"))) {
            if (pPKCol) hFile << wxString::Format(wxT("\t\t\tpStatement->%s(%i, %s);"), GetAddParamFunction(pPKCol->GetType()->GetUniversalType()).c_str(),1,pPKCol->GetName().c_str()) << "\n";

        } else if (str.Contains(wxT("%%classUtilsAddStatement%%"))) {
            wxString cols = wxT("");
            wxString params = wxT("");
            SerializableList::compatibility_iterator node = pTab->GetFirstChildNode();
            while( node ) {
                Column* pCol = wxDynamicCast(node->GetData(),Column);
                if (pCol) {
                    if (!cols.IsEmpty()) cols = cols + wxT(",");
                    cols += pCol->GetName();

                    if (!params.IsEmpty()) params += wxT(",");
                    params += wxT("?");
                }
                node = node->GetNext();
            }
            hFile << wxString::Format(wxT("\t\tPreparedStatement* pStatement = pDbLayer->PrepareStatement(wxT(\"INSERT INTO %s (%s) VALUES (%s)\"));"),pTab->GetName().c_str(), cols.c_str(), params.c_str()) << "\n";

        } else if (str.Contains(wxT("%%classUtilsEditStatement%%"))) {
            wxString cols = wxT("");
            wxString params = wxT("");
            SerializableList::compatibility_iterator node = pTab->GetFirstChildNode();
            while( node ) {
                Column* pCol = wxDynamicCast(node->GetData(),Column);
                if (pCol) {
                    if (!cols.IsEmpty()) cols = cols + wxT(",");
                    cols += pCol->GetName() + wxT(" = ?");
                }
                node = node->GetNext();
            }
            if (pPKCol)	hFile << wxString::Format(wxT("\t\tPreparedStatement* pStatement = pDbLayer->PrepareStatement(wxT(\"UPDATE %s SET %s WHERE %s = ?\"));"),pTab->GetName().c_str(), cols.c_str(), pPKCol->GetName().c_str()) << "\n";
            else hFile << wxT("\t\tPreparedStatement* pStatement = NULL;") << "\n";

        } else if (str.Contains(wxT("%%classUtilsEditDebeaStatement%%"))) {
            SerializableList::compatibility_iterator node = pTab->GetFirstChildNode();
            while( node ) {
                Column* pCol = wxDynamicCast(node->GetData(),Column);
                if (pCol && ( !pPKCol || (pCol->GetName() != pPKCol->GetName()) ) ) {
                    hFile <<  wxT("\t\t\tc->setMember(c->m_") + pCol->GetName() + wxT(", ") + pCol->GetName() + wxT(");") << "\n";
                }
                node = node->GetNext();
            }

        } else if (str.Contains(wxT("%%classUtilsDeleteStatement%%"))) {
            if (pPKCol)	hFile << wxString::Format(wxT("\t\tPreparedStatement* pStatement = pDbLayer->PrepareStatement(wxT(\"DELETE FROM %s WHERE %s = ?\"));"),pTab->GetName().c_str(), pPKCol->GetName().c_str()) << "\n";
            else hFile << wxT("\t\tPreparedStatement* pStatement = NULL;") << "\n";

        } else if (str.Contains(wxT("%%classUtilsPKSetParams%%"))) {
            if (pPKCol) hFile << wxString::Format(wxT("\t\t\tpStatement->%s(%i, %s);"), GetAddParamFunction(pPKCol->GetType()->GetUniversalType()).c_str(),lastEditParam,pPKCol->GetName().c_str()) << "\n";

        } else if (str.Contains(wxT("%%classUtilsCreateStatement%%"))) {
            wxStringTokenizer tknz( m_pDbAdapter->GetCreateTableSql( pTab, true ), wxT("\n"), wxTOKEN_STRTOK );
            while( true ) {
                wxString line = tknz.GetNextToken();
                if( !tknz.HasMoreTokens() ) break; // omit last line
                hFile <<  wxT("\t\t\t\"") + line + wxT("\" \\")  << "\n";
            }

        } else if (str.Contains(wxT("%%classUtilsDropStatement%%"))) {
            wxStringTokenizer tknz( m_pDbAdapter->GetDropTableSql( pTab ), wxT("\n"), wxTOKEN_STRTOK );
            while( tknz.HasMoreTokens() ) {
                hFile <<  wxT("\t\t\t\"") + tknz.GetNextToken() + wxT("\" \\")  << "\n";
            }

        } else {
            str.Replace(wxT("%%classItemName%%"),classItemName);
            str.Replace(wxT("%%classItemDef%%"),classItemDef);
            str.Replace(wxT("%%classColName%%"),classColName);
            str.Replace(wxT("%%classTableName%%"),classTableName);
            str.Replace(wxT("%%classUtilName%%"), classUtilName);
            if( pPKCol ) {
                str.Replace(wxT("%%pkType%%"), GetParamTypeName(pPKCol->GetType()->GetUniversalType()));
                str.Replace(wxT("%%pkName%%"), pPKCol->GetName());
            }
            hFile << str << "\n";
        }
    }

    return true;
}