21#include <core_resource.hxx>
23#include <com/sun/star/sdbcx/XTablesSupplier.hpp>
24#include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
25#include <com/sun/star/sdbc/DataType.hpp>
26#include <com/sun/star/sdbc/ColumnValue.hpp>
27#include <com/sun/star/sdb/CommandType.hpp>
28#include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
29#include <com/sun/star/sdbc/XRow.hpp>
30#include <com/sun/star/uno/XComponentContext.hpp>
31#include <com/sun/star/util/NumberFormat.hpp>
32#include <com/sun/star/util/XNumberFormatTypes.hpp>
44#include <com/sun/star/awt/FontDescriptor.hpp>
50#include <com/sun/star/sdb/application/CopyTableOperation.hpp>
67namespace CopyTableOperation = ::com::sun::star::sdb::application::CopyTableOperation;
70ODatabaseExport::ODatabaseExport(sal_Int32 nRows,
71 TPositions&&_rColumnPositions,
74 const TColumnVector* pList,
76 bool _bAutoIncrementEnabled,
78 :m_vColumnPositions(
std::move(_rColumnPositions))
80 ,m_xFormatter(_rxNumberF)
82 ,m_pFormatter(nullptr)
83 ,m_rInputStream( _rInputStream )
85 ,m_pInfoMap(_pInfoMap)
92 ,m_bDontAskAgain(false)
93 ,m_bIsAutoIncrement(_bAutoIncrementEnabled)
96 ,m_bAppendFirstLine(false)
100 for(
const std::pair<sal_Int32,sal_Int32> & rPair : m_vColumnPositions)
104 m_vColumnSize.resize(nCount);
105 m_vNumberFormat.resize(nCount);
108 m_vColumnSize[
i] = 0;
109 m_vNumberFormat[
i] = 0;
121 SetColumnTypes(pList,_pInfoMap);
128 :m_aDestColumns(_rxConnection->getMetaData().is() && _rxConnection->getMetaData()->supportsMixedCaseQuotedIdentifiers())
130 ,m_xFormatter(_rxNumberF)
132 ,m_pFormatter(nullptr)
133 ,m_rInputStream( _rInputStream )
134 ,m_pColumnList(nullptr)
142 ,m_bDontAskAgain(false)
143 ,m_bIsAutoIncrement(false)
144 ,m_bFoundTable(false)
146 ,m_bAppendFirstLine(false)
159 m_xTables = xTablesSup->getTables();
166 std::vector<sal_Int32>
aTypes;
167 std::vector<bool> aNullable;
174 sal_Int32
nCount = xResultSetMetaData->getColumnCount();
178 aNullable.reserve(nCount+1);
180 aNullable.push_back(
false);
181 for (sal_Int32 j = 1; j <=
nCount ; ++j)
183 aNullable.push_back(xResultSetMetaData->isNullable(j) != ColumnValue::NO_NULLS );
184 aTypes.push_back(xResultSetMetaData->getColumnType(j));
189 OSL_ENSURE((nPos) <
static_cast<sal_Int32
>(
aTypes.size()),
"aTypes: Illegal index for vector");
190 aValue.
fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
193 OSL_ENSURE((nPos) <
static_cast<sal_Int32
>(
aTypes.size()),
"aTypes: Illegal index for vector");
194 aValue.
fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
198 if( nType == DataType::VARCHAR )
200 m_pTypeInfo = std::make_shared<OTypeInfo>();
202 m_pTypeInfo->aTypeName = sTypeName;
203 m_pTypeInfo->nType =
nType;
205 OSL_ENSURE((nPos) <
static_cast<sal_Int32
>(
aTypes.size()),
"aTypes: Illegal index for vector");
206 aValue.
fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
207 m_pTypeInfo->nPrecision = aValue.
getInt32();
209 aValue.
fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
211 aValue.
fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
213 aValue.
fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
214 m_pTypeInfo->aCreateParams = aValue.
getString();
216 aValue.
fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
217 m_pTypeInfo->bNullable = aValue.
getInt32() == ColumnValue::NULLABLE;
219 aValue.
fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
222 aValue.
fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
223 m_pTypeInfo->nSearchType = aValue.
getInt16();
225 aValue.
fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
228 aValue.
fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
229 m_pTypeInfo->bCurrency = aValue.
getBool();
231 aValue.
fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
232 m_pTypeInfo->bAutoIncrement = aValue.
getBool();
234 aValue.
fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
235 m_pTypeInfo->aLocalTypeName = aValue.
getString();
237 aValue.
fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
238 m_pTypeInfo->nMinimumScale = aValue.
getInt16();
240 aValue.
fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
241 m_pTypeInfo->nMaximumScale = aValue.
getInt16();
243 aValue.
fill(nPos,aTypes[nPos],xRow);
244 m_pTypeInfo->nNumPrecRadix = aValue.
getInt32();
247 if( m_pTypeInfo->nPrecision < 0)
248 m_pTypeInfo->nPrecision = 0;
249 if( m_pTypeInfo->nMinimumScale < 0)
250 m_pTypeInfo->nMinimumScale = 0;
251 if( m_pTypeInfo->nMaximumScale < 0)
252 m_pTypeInfo->nMaximumScale = 0;
253 if( m_pTypeInfo->nNumPrecRadix <= 1)
254 m_pTypeInfo->nNumPrecRadix = 10;
260 m_pTypeInfo = std::make_shared<OTypeInfo>();
263ODatabaseExport::~ODatabaseExport()
267 delete destColumn.second;
282 OSL_ENSURE(nNewPos <
static_cast<sal_Int32
>(
m_vColumnPositions.size()),
"m_vColumnPositions: Illegal index for vector");
293 OSL_ENSURE((nNewPos) <
static_cast<sal_Int32
>(
m_vColumnTypes.size()),
"Illegal index for vector");
296 SAL_INFO(
"dbaccess.ui",
"ODatabaseExport::insertValueIntoColumn != DataType::VARCHAR" );
298 sal_Int32 nNumberFormat = 0;
299 double fOutNumber = 0.0;
300 bool bNumberFormatError =
false;
304 sal_uInt32 nNumberFormat2( nNumberFormat );
311 nNumberFormat =
static_cast<sal_Int32
>(nNumberFormat2);
317 const sal_Int16 nFormats[] = {
318 NumberFormat::DATETIME
321 ,NumberFormat::CURRENCY
322 ,NumberFormat::NUMBER
323 ,NumberFormat::LOGICAL
325 for (
short nFormat : nFormats)
342 bNumberFormatError =
true;
346 if ( !bNumberFormatError )
357 case NumberFormat::DATE:
360 case NumberFormat::DATETIME:
363 case NumberFormat::TIME:
387 sal_Int16 nNumberFormat = 0;
398 sal_uInt32 nFormatKey(0);
404 return NumberFormat::TEXT;
412 sal_Int32 nFormatKey =
m_xFormatter->detectNumberFormat(xNumType->getStandardFormat(NumberFormat::ALL,
m_aLocale),aCheckToken);
413 m_xFormatter->convertStringToNumber(nFormatKey,aCheckToken);
421 case NumberFormat::ALL:
422 nNumberFormat = NumberFormat::ALL;
424 case NumberFormat::DEFINED:
425 nNumberFormat = NumberFormat::TEXT;
427 case NumberFormat::DATE:
428 switch(_nOldNumberFormat)
430 case NumberFormat::DATETIME:
431 case NumberFormat::TEXT:
432 case NumberFormat::DATE:
433 nNumberFormat = _nOldNumberFormat;
435 case NumberFormat::ALL:
436 nNumberFormat = NumberFormat::DATE;
439 nNumberFormat = NumberFormat::TEXT;
443 case NumberFormat::TIME:
444 switch(_nOldNumberFormat)
446 case NumberFormat::DATETIME:
447 case NumberFormat::TEXT:
448 case NumberFormat::TIME:
449 nNumberFormat = _nOldNumberFormat;
451 case NumberFormat::ALL:
452 nNumberFormat = NumberFormat::TIME;
455 nNumberFormat = NumberFormat::TEXT;
459 case NumberFormat::CURRENCY:
460 switch(_nOldNumberFormat)
462 case NumberFormat::NUMBER:
463 nNumberFormat = NumberFormat::CURRENCY;
465 case NumberFormat::CURRENCY:
466 nNumberFormat = _nOldNumberFormat;
468 case NumberFormat::ALL:
469 nNumberFormat = NumberFormat::CURRENCY;
472 nNumberFormat = NumberFormat::TEXT;
476 case NumberFormat::NUMBER:
477 case NumberFormat::SCIENTIFIC:
478 case NumberFormat::FRACTION:
479 case NumberFormat::PERCENT:
480 switch(_nOldNumberFormat)
482 case NumberFormat::NUMBER:
483 nNumberFormat = _nOldNumberFormat;
485 case NumberFormat::CURRENCY:
486 nNumberFormat = NumberFormat::CURRENCY;
488 case NumberFormat::ALL:
489 nNumberFormat =
nType;
492 nNumberFormat = NumberFormat::TEXT;
496 case NumberFormat::TEXT:
497 case NumberFormat::UNDEFINED:
498 case NumberFormat::LOGICAL:
499 nNumberFormat = NumberFormat::TEXT;
501 case NumberFormat::DATETIME:
502 switch(_nOldNumberFormat)
504 case NumberFormat::DATETIME:
505 case NumberFormat::TEXT:
506 case NumberFormat::TIME:
507 nNumberFormat = _nOldNumberFormat;
509 case NumberFormat::ALL:
510 nNumberFormat = NumberFormat::DATETIME;
513 nNumberFormat = NumberFormat::TEXT;
518 SAL_WARN(
"dbaccess.ui",
"ODatabaseExport: Unknown NumberFormat");
524 nNumberFormat = NumberFormat::TEXT;
527 return nNumberFormat;
532 if(!(_pList && _pInfoMap))
540 for (
auto const& elem : *_pList)
542 if (
i >= minBothSize)
546 sal_Int32
nLength(0),nScale(0);
551 case NumberFormat::ALL:
554 case NumberFormat::DEFINED:
558 case NumberFormat::DATE:
561 case NumberFormat::TIME:
564 case NumberFormat::DATETIME:
567 case NumberFormat::CURRENCY:
572 case NumberFormat::NUMBER:
573 case NumberFormat::SCIENTIFIC:
574 case NumberFormat::FRACTION:
575 case NumberFormat::PERCENT:
578 case NumberFormat::TEXT:
579 case NumberFormat::UNDEFINED:
580 case NumberFormat::LOGICAL:
586 OTypeInfoMap::const_iterator aFind = _pInfoMap->find(
nDataType);
587 if(aFind != _pInfoMap->end())
589 elem->second->SetType(aFind->second);
590 elem->second->SetPrecision(std::min<sal_Int32>(aFind->second->nPrecision,
nLength));
591 elem->second->SetScale(std::min<sal_Int32>(aFind->second->nMaximumScale,nScale));
593 sal_Int32 nFormatKey = ::dbtools::getDefaultNumberFormat(
nDataType,
594 elem->second->GetScale(),
595 elem->second->IsCurrency(),
599 elem->second->SetFormatKey(nFormatKey);
608 sal_Int32 nMaxNameLen(xDestMetaData->getMaxColumnNameLength());
609 OUString aAlias = _rColumnName;
611 aAlias = ::dbtools::convertName2SQLName(_rColumnName,xDestMetaData->getExtraNameCharacters());
613 if(nMaxNameLen && aAlias.getLength() > nMaxNameLen)
614 aAlias = aAlias.copy(0, std::min<sal_Int32>( nMaxNameLen-1, aAlias.getLength() ) );
616 OUString
sName(aAlias);
624 + OUString::number(++
nPos);
625 if(nMaxNameLen &&
sName.getLength() > nMaxNameLen)
627 aAlias = aAlias.copy(0,std::min<sal_Int32>( nMaxNameLen-
nCount, aAlias.getLength() ));
629 + OUString::number(
nPos);
649 delete aFind->second;
668 bHaveDefaultTable ? CopyTableOperation::AppendData : CopyTableOperation::CopyDefinitionAndData,
685 case CopyTableOperation::CopyDefinitionAndData:
686 case CopyTableOperation::AppendData:
693 if(_aTextColor.hasValue())
712 catch(
const SQLException&)
729 OUString aMsg = e.Message
747 OSL_ENSURE(nNewPos <
static_cast<sal_Int32
>(
m_vColumnPositions.size()),
"Illegal index for vector");
754 OSL_ENSURE((nColPos) <
static_cast<sal_Int32
>(
m_vNumberFormat.size()),
"m_vFormatKey: Illegal index for vector");
755 OSL_ENSURE((nColPos) <
static_cast<sal_Int32
>(
m_vColumnSize.size()),
"m_vColumnSize: Illegal index for vector");
774 auto pSupplierImpl = comphelper::getFromUnoTunnel<SvNumberFormatsSupplierObj>(xSupplier);
775 m_pFormatter = pSupplierImpl ? pSupplierImpl->GetNumberFormatter() :
nullptr;
777 xNumberFormatSettings->getPropertyValue(
"NullDate") >>=
m_aNullDate;
785 OUString sComposedTableName = ::dbtools::composeTableName( _xMetaData, _xDestTable, ::dbtools::EComposeRule::InDataManipulation,
true );
787 OUStringBuffer aSql =
"INSERT INTO "
792 OUStringBuffer aValues(
" VALUES ( ");
795 if ( _xMetaData.is() )
796 aQuote = _xMetaData->getIdentifierQuoteString();
802 if ( !aDestColumnNames.hasElements() )
806 const OUString* pIter = aDestColumnNames.getConstArray();
807 std::vector< OUString> aInsertList;
808 aInsertList.resize(aDestColumnNames.getLength()+1);
809 for(
size_t j=0; j < aInsertList.size(); ++j)
811 ODatabaseExport::TPositions::const_iterator aFind = std::find_if(_rvColumns.begin(),_rvColumns.end(),
812 [j] (
const ODatabaseExport::TPositions::value_type& tPos)
813 { return tPos.second == static_cast<sal_Int32>(j+1); });
816 OSL_ENSURE((aFind->first) <
static_cast<sal_Int32
>(aInsertList.size()),
"aInsertList: Illegal index for vector");
817 aInsertList[aFind->first] = ::dbtools::quoteName( aQuote,*(pIter+j));
822 for (
auto const& elem : aInsertList)
824 if ( !elem.isEmpty() )
826 aSql.append(elem +
",");
827 aValues.append(
"?,");
831 aSql[aSql.getLength()-1] =
')';
832 aValues[aValues.getLength()-1] =
')';
834 aSql.append(aValues);
836 return _xMetaData->getConnection()->prepareStatement(aSql.makeStringAndClear());
#define COLUMN_POSITION_NOT_FOUND
const css::lang::Locale & getLocale(bool bResolveSystem=true) const
static double GetTableDataOptionsValNum(sal_uInt32 &nNumForm, LanguageType &eNumLang, const OUString &aValStr, std::u16string_view aNumStr, SvNumberFormatter &rFormatter)
const LanguageTag & GetLanguageTag() const
sal_Int32 getInt32() const
OUString getString() const
sal_Int16 getInt16() const
void fill(sal_Int32 _nPos, sal_Int32 _nType, const css::uno::Reference< css::sdbc::XRow > &_xRow)
sal_Int16 getOperation() const
const ODatabaseExport::TPositions & GetColumnPositions() const
css::uno::Reference< css::beans::XPropertySet > returnTable()
const std::vector< sal_Int32 > & GetColumnTypes() const
bool shouldCreatePrimaryKey() const
returns whether a primary key should be created in the target database
bool UseHeaderLine() const
SvNumberFormatter * m_pFormatter
sal_Int16 CheckString(const OUString &aToken, sal_Int16 _nOldNumberFormat)
TColumns m_aDestColumns
container for new created columns
OUString m_sDefaultTableName
for saving the selected tablename
std::map< OUString, OFieldDescription *, ::comphelper::UStringMixLess > TColumns
static css::uno::Reference< css::sdbc::XPreparedStatement > createPreparedStatement(const css::uno::Reference< css::sdbc::XDatabaseMetaData > &_xMetaData, const css::uno::Reference< css::beans::XPropertySet > &_xDestTable, const TPositions &_rvColumnPositions)
OUString m_sTextToken
cell content
void CreateDefaultColumn(const OUString &_rColumnName)
TColumnVector m_vDestVector
bool m_bDontAskAgain
if there is an error when pasting, don't show it again
css::uno::Reference< css::util::XNumberFormatter > m_xFormatter
a number formatter working with the connection's NumberFormatsSupplier
bool m_bError
error and termination code
css::uno::Reference< css::uno::XComponentContext > m_xContext
std::vector< sal_Int16 > m_vNumberFormat
std::vector< std::pair< sal_Int32, sal_Int32 > > TPositions
std::shared_ptr< IUpdateHelper > m_pUpdateHelper
std::vector< sal_Int32 > m_vColumnSize
sal_Int32 m_nColumnPos
current column position
css::util::Date m_aNullDate
OUString m_sNumToken
SDNUM value.
css::uno::Reference< css::beans::XPropertySet > m_xTable
dest table
bool executeWizard(const OUString &_sTableName, const css::uno::Any &_aTextColor, const css::awt::FontDescriptor &_rFont)
executeWizard calls a wizard to create/append data
bool m_bIsAutoIncrement
if PKey is set by user
void showErrorDialog(const css::sdbc::SQLException &e)
css::lang::Locale m_aLocale
virtual TypeSelectionPageFactory getTypeSelectionPageFactory()=0
void SetColumnTypes(const TColumnVector *rList, const OTypeInfoMap *_pInfoMap)
std::vector< sal_Int32 > m_vColumnTypes
ColumnTypes for faster access.
TOTypeInfoSP m_pTypeInfo
contains the default type
void insertValueIntoColumn()
std::vector< TColumns::const_iterator > TColumnVector
SvStream & m_rInputStream
TPositions m_vColumnPositions
columns to be used
SharedConnection m_xConnection
dest conn
void SetPrecision(sal_Int32 _rPrecision)
void SetIsNullable(sal_Int32 _rIsNullable)
void SetPrimaryKey(bool _bPKey)
void SetType(const TOTypeInfoSP &_pType)
void SetAutoIncrement(bool _bAuto)
void SetName(const OUString &_rName)
sal_Int32 GetType() const
void SetCurrency(bool _bIsCurrency)
void SetScale(sal_Int32 _rScale)
virtual Dialog * getDialog() override
virtual css::uno::Reference< css::awt::XWindow > GetXWindow()=0
Reference< XComponentContext > m_xContext
#define DBG_UNHANDLED_EXCEPTION(...)
#define SAL_WARN(area, stream)
#define SAL_INFO(area, stream)
std::multimap< DataTypeEnum, OExtendedTypeInfo * > OTypeInfoMap
std::multimap< sal_Int32, TOTypeInfoSP > OTypeInfoMap
bool isSQL92CheckEnabled(const css::uno::Reference< css::sdbc::XConnection > &_xConnection)
check if SQL92 name checking is enabled
Reference< XConnection > m_xConnection
constexpr OUStringLiteral PROPERTY_TEXTCOLOR(u"TextColor")
constexpr OUStringLiteral PROPERTY_FONT(u"FontDescriptor")
constexpr OUStringLiteral PROPERTY_TYPE(u"Type")