Debian Codegraph @

DatabaseExplorer/dumpclass.cpp

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2014 Eran Ifrah
5 // file name : dumpclass.cpp
6 //
7 // -------------------------------------------------------------------------
8 // A
9 // _____ _ _ _ _
10 // / __ \ | | | | (_) |
11 // | / \/ ___ __| | ___| | _| |_ ___
12 // | | / _ \ / _ |/ _ \ | | | __/ _ )
13 // | \__/\ (_) | (_| | __/ |___| | || __/
14 // \____/\___/ \__,_|\___\_____/_|\__\___|
15 //
16 // F i l e
17 //
18 // This program is free software; you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation; either version 2 of the License, or
21 // (at your option) any later version.
22 //
23 //////////////////////////////////////////////////////////////////////////////
24 //////////////////////////////////////////////////////////////////////////////
25
26 #include "dumpclass.h"
27
28 DumpClass::DumpClass(IDbAdapter* pDbAdapter, xsSerializable* pItems, const wxString& fileName) {
29 m_pDbAdapter = pDbAdapter;
30 m_pItems = pItems;
31 m_fileName = fileName;
32
33 }
34
35 DumpClass::~DumpClass() {
36 }
37
38 wxString DumpClass::DumpData() {
39 int totalRowCount = 0;
40 int tableCount = 0;
41 wxTextFile* pOutFile = new wxTextFile(m_fileName);
42 if (pOutFile->Exists()) {
43 pOutFile->Open();
44 pOutFile->Clear();
45 } else {
46 pOutFile->Create();
47 pOutFile->Open();
48 }
49
50 if (pOutFile->IsOpened()) {
51
52 SerializableList::compatibility_iterator node = m_pItems->GetFirstChildNode();
53 while( node ) {
54 Table* pTab = wxDynamicCast(node->GetData(), Table);
55 if (pTab) {
56 totalRowCount += DumpTable(pOutFile, pTab);
57 tableCount++;
58 }
59 node = node->GetNext();
60 }
61
62 pOutFile->Write(wxTextFileType_None, wxConvUTF8);
63 pOutFile->Close();
64
65 }
66 if (pOutFile) delete pOutFile;
67 return wxString::Format(wxT("Dumped %i rows in %i tables"),totalRowCount, tableCount );
68 }
69
70 int DumpClass::DumpTable(wxTextFile* pFile, Table* pTab) {
71 int rowCount = 0;
72 if ((pFile->IsOpened())&&(pTab != NULL)) {
73
74 wxString cols = wxT("");
75 SerializableList::compatibility_iterator node = pTab->GetFirstChildNode();
76 while (node){
77 Column* pCol = wxDynamicCast(node->GetData(), Column);
78 if (pCol){
79 if (!cols.IsEmpty()) cols += wxT(", ");
80 cols += pCol->GetName();
81 }
82 node = node->GetNext();
83 }
84
85 wxString startLine = wxString::Format(wxT("INSERT INTO %s (%s) VALUES"), pTab->GetName().c_str(), cols.c_str());
86 int n = 0;
87 bool pocatek = false;
88
89
90 DatabaseLayerPtr pDbLayer = m_pDbAdapter->GetDatabaseLayer(pTab->GetParentName());
91 if (pDbLayer){
92 //DatabaseResultSet* pResult = pDbLayer->RunQueryWithResults(wxString::Format(wxT("SELECT %s FROM %s.%s"), cols.c_str(),pTab->GetParentName().c_str(), pTab->GetName().c_str()));
93 DatabaseResultSet* pResult = pDbLayer->RunQueryWithResults(m_pDbAdapter->GetDefaultSelect(cols ,pTab->GetParentName(), pTab->GetName()));
94
95 while (pResult->Next()){
96 if (n == 0 ) pFile->AddLine(startLine);
97 rowCount++;
98
99 int colIndex = 1;
100 wxString dataLine = wxT("");
101 SerializableList::compatibility_iterator node = pTab->GetFirstChildNode();
102 while (node){
103 Column* pCol = wxDynamicCast(node->GetData(), Column);
104 if (pCol){
105
106 if (!dataLine.IsEmpty()) dataLine += wxT(", ");
107
108 if (pCol->GetType()->GetUniversalType() == IDbType::dbtTYPE_TEXT){
109 dataLine += wxString::Format(wxT("'%s'"), pResult->GetResultString(colIndex).c_str());
110 } else if (pCol->GetType()->GetUniversalType() == IDbType::dbtTYPE_DATE_TIME){
111 dataLine += wxString::Format(wxT("'%s'"), pResult->GetResultDate(colIndex).FormatDate().c_str());
112 } else if (pCol->GetType()->GetUniversalType() == IDbType::dbtTYPE_DECIMAL){
113 wxString cislo = wxString::Format(wxT("%f"), pResult->GetResultDouble(colIndex));
114 cislo.Replace(wxT(","),wxT("."));
115 dataLine += cislo;
116 } else if (pCol->GetType()->GetUniversalType() == IDbType::dbtTYPE_FLOAT){
117 wxString cislo = wxString::Format(wxT("%f"), pResult->GetResultDouble(colIndex));
118 cislo.Replace(wxT(","),wxT("."));
119 dataLine += cislo;
120 } else if (pCol->GetType()->GetUniversalType() == IDbType::dbtTYPE_INT){
121 dataLine += wxString::Format(wxT("%i"), pResult->GetResultInt(colIndex));
122 } else if (pCol->GetType()->GetUniversalType() == IDbType::dbtTYPE_BOOLEAN){
123 if (pResult->GetResultBool(colIndex)) dataLine += wxT("true");
124 else dataLine += wxT("false");
125 } else if (pCol->GetType()->GetUniversalType() != IDbType::dbtTYPE_OTHER){
126 dataLine += wxString::Format(wxT("%s"), pResult->GetResultString(colIndex).c_str());
127 }
128 colIndex++;
129 }
130 node = node->GetNext();
131 }
132
133 if (n++ > 20 ){
134 pFile->AddLine(wxString::Format(wxT(",(%s);"), dataLine.c_str()));
135 pocatek = false;
136 n = 0;
137 }else{
138 if (pocatek == false){
139 pocatek = true;
140 pFile->AddLine(wxString::Format(wxT("(%s)"), dataLine.c_str()));
141 }else{
142 pFile->AddLine(wxString::Format(wxT(",(%s)"), dataLine.c_str()));
143 }
144
145 }
146
147 }
148 if (rowCount > 0) pFile->AddLine(wxT(";"));
149 pDbLayer->CloseResultSet(pResult);
150 pDbLayer->Close();
151 }
152 }
153 return rowCount;
154 }