1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
3  *
4  *  Effective License of whole file:
5  *
6  *    This library is free software; you can redistribute it and/or
7  *    modify it under the terms of the GNU Lesser General Public
8  *    License version 2.1, as published by the Free Software Foundation.
9  *
10  *    This library is distributed in the hope that it will be useful,
11  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *    Lesser General Public License for more details.
14  *
15  *    You should have received a copy of the GNU Lesser General Public
16  *    License along with this library; if not, write to the Free Software
17  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18  *    MA  02111-1307  USA
19  *
20  *  Parts "Copyright by Sun Microsystems, Inc" prior to August 2011:
21  *
22  *    The Contents of this file are made available subject to the terms of
23  *    the GNU Lesser General Public License Version 2.1
24  *
25  *    Copyright: 2000 by Sun Microsystems, Inc.
26  *
27  *    Contributor(s): Joerg Budischewski
28  *
29  *  All parts contributed on or after August 2011:
30  *
31  *    This Source Code Form is subject to the terms of the Mozilla Public
32  *    License, v. 2.0. If a copy of the MPL was not distributed with this
33  *    file, You can obtain one at http://mozilla.org/MPL/2.0/.
34  *
35  ************************************************************************/
36 
37 #pragma once
38 
39 #include <com/sun/star/beans/XPropertySet.hpp>
40 #include <com/sun/star/sdbc/XParameters.hpp>
41 #include <com/sun/star/util/Date.hpp>
42 #include <com/sun/star/util/Time.hpp>
43 #include <com/sun/star/util/DateTime.hpp>
44 
45 #include <rtl/ustrbuf.hxx>
46 #include <rtl/string.hxx>
47 
48 #include "pq_connection.hxx"
49 
50 #include <string_view>
51 #include <vector>
52 
53 namespace
54 {
55 // helper to create one-time deleters
56 template <auto fn>
57 using deleter_from_fn = std::integral_constant<decltype(fn), fn>;
58 
59 }
60 
61 namespace pq_sdbc_driver
62 {
63 bool isWhitespace( sal_Unicode c );
64 
65 OUString concatQualified( std::u16string_view a, std::u16string_view b);
66 
67 OString OUStringToOString( std::u16string_view str, ConnectionSettings const *settings);
68 
69 void bufferQuoteConstant( OUStringBuffer & buf, std::u16string_view str, ConnectionSettings *settings );
70 void bufferQuoteAnyConstant( OUStringBuffer & buf, const css::uno::Any &val, ConnectionSettings *settings );
71 
72 void bufferEscapeConstant( OUStringBuffer & buf, std::u16string_view str, ConnectionSettings *settings );
73 
74 OUString sqltype2string(
75     const css::uno::Reference< css::beans::XPropertySet > & column );
76 
77 
78 void bufferQuoteQualifiedIdentifier(
79     OUStringBuffer & buf, std::u16string_view schema, std::u16string_view name, ConnectionSettings *settings );
80 
81 void bufferQuoteQualifiedIdentifier(
82     OUStringBuffer & buf,
83     std::u16string_view schema,
84     std::u16string_view name,
85     std::u16string_view col,
86     ConnectionSettings *settings );
87 
88 void bufferQuoteIdentifier( OUStringBuffer & buf, std::u16string_view toQuote, ConnectionSettings *settings );
89 void bufferKey2TableConstraint(
90     OUStringBuffer &buf,
91     const css::uno::Reference< css::beans::XPropertySet > &key,
92     ConnectionSettings *settings  );
93 
94 OUString extractStringProperty(
95     const css::uno::Reference< css::beans::XPropertySet > & descriptor,
96     const OUString &name );
97 
98 sal_Int32 extractIntProperty(
99     const css::uno::Reference< css::beans::XPropertySet > & descriptor,
100     const OUString &name );
101 
102 bool extractBoolProperty(
103     const css::uno::Reference< css::beans::XPropertySet > & descriptor,
104     const OUString &name );
105 
106 void disposeNoThrow( const css::uno::Reference< css::uno::XInterface > & r );
107 void disposeObject( const css::uno::Reference< css::uno::XInterface > & r );
108 
109 OUString extractTableFromInsert( const OUString & sql );
110 OString extractSingleTableFromSelect( const std::vector< OString > &vec );
111 
112 OUString getColExprForDefaultSettingVal(ConnectionSettings const *settings);
113 
114 void tokenizeSQL( const OString & sql, std::vector< OString > &vec  );
115 void splitSQL( const OString & sql, std::vector< OString > &vec  );
116 std::vector< sal_Int32 > parseIntArray( const OUString & str );
117 /// @throws css::sdbc::SQLException
118 std::vector< css::uno::Any > parseArray( const OUString & str );
119 
120 OUString array2String( const css::uno::Sequence< css::uno::Any > &seq );
121 
122 css::uno::Reference< css::sdbc::XConnection > extractConnectionFromStatement(
123     const css::uno::Reference< css::uno::XInterface > & stmt );
124 
125 void splitConcatenatedIdentifier( std::u16string_view source, OUString *first, OUString *second);
126 
127 
128 void fillAttnum2attnameMap(
129     Int2StringMap &map,
130     const css::uno::Reference< css::sdbc::XConnection > &conn,
131     const OUString &schema,
132     const OUString &table );
133 
134 css::uno::Sequence< sal_Int32 > string2intarray( const OUString & str );
135 
136 css::uno::Sequence< OUString > convertMappedIntArray2StringArray(
137     const Int2StringMap &map, const css::uno::Sequence< sal_Int32> &source );
138 
139 typedef std::unordered_map< OString, OString > String2StringMap;
140 
141 OUString querySingleValue(
142     const css::uno::Reference< css::sdbc::XConnection > &connection,
143     const OUString &query );
144 
145 void extractNameValuePairsFromInsert( String2StringMap & map, const OString & lastQuery );
146 sal_Int32 typeNameToDataType( const OUString &typeName, std::u16string_view typtype );
147 
148 // copied from connectivity/source/dbtools, can't use the function directly
149 bool implSetObject( const css::uno::Reference< css::sdbc::XParameters >& _rxParameters,
150                     const sal_Int32 _nColumnIndex, const css::uno::Any& _rValue);
151 
152 class DisposeGuard
153 {
154     css::uno::Reference< css::uno::XInterface > d;
155 public:
156     explicit DisposeGuard(const css::uno::Reference< css::uno::XInterface > & r );
157     ~DisposeGuard();
158 
159 };
160 
161 class TransactionGuard
162 {
163     css::uno::Reference< css::sdbc::XStatement > m_stmt;
164     bool m_commited;
165 public:
166     /// takes over ownership of given statement
167     explicit TransactionGuard( const css::uno::Reference< css::sdbc::XStatement > &stmt );
168     ~TransactionGuard( );
169 
170     void commit();
171     void executeUpdate( const OUString & sql );
172 };
173 
174 }
175 
176 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
177