Example #1
0
void TableSettings::FillColumns()
{
    wxVector<wxVariant> line;

    m_dvColumns->DeleteAllItems();

    for( SerializableList::iterator it = m_lstColumns.begin();
         it != m_lstColumns.end(); ++it ) {

        Column *c = wxDynamicCast( *it, Column );
        if( c ) {
            IDbType *type = c->GetType();

            line.clear();
            line.push_back( wxVariant( c->GetName() ) );
            line.push_back( wxVariant( type->GetTypeName() ) );
            line.push_back( wxVariant( wxString::Format( wxT("%ld,%ld"), type->GetSize(), type->GetSize2() ) ) );
            line.push_back( wxVariant( type->GetNotNull() ) );
            line.push_back( wxVariant( type->GetAutoIncrement() ) );
            line.push_back( wxVariant( IsPrimaryKey( c->GetName() ) ) );

            m_dvColumns->AppendItem( line, reinterpret_cast<wxUIntPtr>(c) );
        }
    }
}
Example #2
0
void PostgreSqlDbAdapter::ConvertTable(Table* pTab) {
	SerializableList::compatibility_iterator node = pTab->GetFirstChildNode();
	while( node ) {
		if( node->GetData()->IsKindOf( CLASSINFO(Column)) )  {
			Column* col = (Column*) node->GetData();
			col->SetType(ConvertType(col->GetType()));
		}
		node = node->GetNext();
	}
}
Example #3
0
wxString MySqlDbAdapter::GetCreateTableSql(Table* tab, bool dropTable)
{
	//TODO:SQL:
	wxString str = wxT("");
	if (dropTable) str = wxString::Format(wxT("SET FOREIGN_KEY_CHECKS = 0;\nDROP TABLE IF EXISTS `%s` ;\nSET FOREIGN_KEY_CHECKS = 1; \n"),tab->GetName().c_str());
	str.append(wxString::Format(wxT("CREATE TABLE `%s` (\n"),tab->GetName().c_str()));



	SerializableList::compatibility_iterator node = tab->GetFirstChildNode();
	while( node ) {
		Column* col = NULL;
		if( node->GetData()->IsKindOf( CLASSINFO(Column)) ) col = (Column*) node->GetData();
		if(col)	str.append(wxString::Format(wxT("\t`%s` %s"),col->GetName().c_str(), col->GetType()->ReturnSql().c_str()));

		Constraint* constr = wxDynamicCast(node->GetData(),Constraint);
		if (constr) {
			if (constr->GetType() == Constraint::primaryKey) str.append(wxString::Format(wxT("\tPRIMARY KEY (`%s`) \n"), constr->GetLocalColumn().c_str()));
		}

		node = node->GetNext();
		if (node) {
			if (wxDynamicCast(node->GetData(),Column)) str.append(wxT(",\n ")) ;
			else if ((constr = wxDynamicCast(node->GetData(),Constraint))) {
				if (constr->GetType() == Constraint::primaryKey) str.append(wxT(",\n ")) ;
			}

		}
		//else  str.append(wxT("\n ")) ;
	}

	/*	Column* col = tab->GetFristColumn();
		while (col) {
			str.append(wxString::Format(wxT("\t`%s` %s"),col->getName().c_str(), col->getPType()->ReturnSql().c_str()));
			col = wxDynamicCast(col->GetSibbling(),Column);
			if (col) str.append(wxT(",\n ")) ;
			else  str.append(wxT("\n ")) ;
		}*/

	str.append(wxT("\n) ENGINE=INNODB;\n"));
	str.append(wxT("-- -------------------------------------------------------------\n"));
	return str;
}
Example #4
0
void TableSettings::OnColumnChanged(wxDataViewEvent& event)
{
    Column *col = reinterpret_cast<Column*>(m_dvColumns->GetItemData( event.GetItem() ) );
    if( col ) {
        wxVariant val;
        event.GetModel()->GetValue( val, event.GetItem(), event.GetColumn() );
        if( ! val.IsNull() ) {
            switch( event.GetColumn() ) {
            case 0: {
                // rename local columns in keys
                SerializableList keys;
                GetConstraints( keys, col->GetName() );
                for(SerializableList::iterator it = keys.begin(); it != keys.end(); ++it ) {
                    Constraint *key = (Constraint*) *it;
                    if( key->GetType() == Constraint::primaryKey ) key->SetName( wxT("PK_") + val.GetString() );
                    key->SetLocalColumn( val.GetString() );
                }
                // rename table column
                col->SetName( val.GetString() );
                break;
            }
            case 1: {
                col->SetType( m_pDbAdapter->GetDbTypeByName( val.GetString() ) );
                break;
            }
            case 2: {
                long s1, s2;
                s1 = s2 = 0;
                wxSscanf( val.GetString(), wxT("%ld,%ld"), &s1, &s2 );
                IDbType *type = col->GetType();
                if( type->HaveSize() ) type->SetSize( s1 );
                else {
                    m_infobar->ShowMessage( wxT("This data type doesn't support size definition."), wxICON_WARNING );
                    Refresh();
                }
                if( type->HaveSize2() ) type->SetSize2( s1 );
                else { 
                    m_infobar->ShowMessage( wxT("This data type doesn't support size definition."), wxICON_WARNING );
                    Refresh();
                }
                break;
            }
            case 3: {
                IDbType *type = col->GetType();
                if( type->HaveNotNull() ) type->SetNotNull( val.GetBool() );
                else {
                    m_infobar->ShowMessage( wxT("This data type doesn't support NOT NULL feature."), wxICON_WARNING );
                    Refresh();
                }
                break;
            }
            case 4: {
                IDbType *type = col->GetType();
                if( type->HaveAutoIncrement() ) type->SetAutoIncrement( val.GetBool() );
                else { 
                    m_infobar->ShowMessage( wxT("This data type doesn't support AUTOINCREMENT feature."), wxICON_WARNING );
                    Refresh();
                }
                break;
            }
            case 5: {
                Constraint *key = GetConstraint( Constraint::primaryKey, col->GetName() );
                if( key ) {
                    // remove primary key if exists
                    m_lstKeys.DeleteObject( key );
                    delete key;
                } else {
                    // create new primary key
                    key = new Constraint( wxT("PK_") + col->GetName(),
                                          col->GetName(),
                                          Constraint::primaryKey,
                                          Constraint::noAction,
                                          Constraint::noAction );

                    m_lstKeys.Append( key );
                }
                break;
            }
            }
        }
    }

    event.Skip();

    UpdateView();
}
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;
}