/usr/include/tntdb/bits/statement.h is in libtntdb-dev 1.3-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 | /*
* Copyright (C) 2005,2010 Tommi Maekitalo
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* As a special exception, you may use this file as part of a free
* software library without restriction. Specifically, if other files
* instantiate templates or use macros or inline functions from this
* file, or you compile this file and link it with other files to
* produce an executable, this file does not by itself cause the
* resulting executable to be covered by the GNU General Public
* License. This exception does not however invalidate any other
* reasons why the executable file might be covered by the GNU Library
* General Public License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef TNTDB_BITS_STATEMENT_H
#define TNTDB_BITS_STATEMENT_H
#include <string>
#include <cxxtools/smartptr.h>
#include <tntdb/iface/istatement.h>
#include <tntdb/date.h>
#include <tntdb/time.h>
#include <tntdb/datetime.h>
#include <cxxtools/convert.h>
#include <tntdb/config.h>
#include <vector>
#include <list>
#include <deque>
#include <set>
namespace tntdb
{
class Connection;
class Result;
class Row;
class Value;
class Date;
class Time;
class Datetime;
/**
* This class represents a sql-statement. A statement can have parameters,
* which are referenced by name, called hostvariables. They are prefixed with
* a colon followed by a name. A name starts with a letter followed by
* alphanumeric characters or underscore. Hostvariables are not searched in
* strings (between apostrophes, quotation marks or backticks). The backslash
* prevents the interpretation of a special meaning of the following
* character.
*/
class Statement
{
public:
class const_iterator;
typedef IStatement::size_type size_type;
private:
cxxtools::SmartPtr<IStatement> stmt;
public:
Statement(IStatement* stmt_ = 0)
: stmt(stmt_)
{ }
/**
* Sets all hostvariables to NULL.
*/
Statement& clear()
{ stmt->clear(); return *this; }
/**
* Sets the hostvariable with the given name to NULL.
*/
Statement& setNull(const std::string& col)
{ stmt->setNull(col); return *this; }
/**
* Sets the hostvariable with the given name to a boolean value.
*/
Statement& setBool(const std::string& col, bool data)
{ stmt->setBool(col, data); return *this; }
/**
* Sets the hostvariable with the given name to a short value.
*/
Statement& setShort(const std::string& col, short data)
{ stmt->setShort(col, data); return *this; }
/**
* Sets the hostvariable with the given name to a int value.
*/
Statement& setInt(const std::string& col, int data)
{ stmt->setInt(col, data); return *this; }
/**
* Sets the hostvariable with the given name to a int value.
*/
Statement& setLong(const std::string& col, long data)
{ stmt->setLong(col, data); return *this; }
/**
* Sets the hostvariable with the given name to a unsigned short value.
*/
Statement& setUnsignedShort(const std::string& col, unsigned short data)
{ stmt->setUnsignedShort(col, data); return *this; }
/**
* Sets the hostvariable with the given name to a unsigned value.
*/
Statement& setUnsigned(const std::string& col, unsigned data)
{ stmt->setUnsigned(col, data); return *this; }
/**
* Sets the hostvariable with the given name to a unsigned long value.
*/
Statement& setUnsignedLong(const std::string& col, unsigned long data)
{ stmt->setUnsignedLong(col, data); return *this; }
/**
* Sets the hostvariable with the given name to a int32_t value.
*/
Statement& setInt32(const std::string& col, int32_t data)
{ stmt->setInt32(col, data); return *this; }
/**
* Sets the hostvariable with the given name to a uint32_t value.
*/
Statement& setUnsigned32(const std::string& col, uint32_t data)
{ stmt->setUnsigned32(col, data); return *this; }
/**
* Sets the hostvariable with the given name to a int64_t value.
*/
Statement& setInt64(const std::string& col, int64_t data)
{ stmt->setInt64(col, data); return *this; }
/**
* Sets the hostvariable with the given name to a uint64_t value.
*/
Statement& setUnsigned64(const std::string& col, uint64_t data)
{ stmt->setUnsigned64(col, data); return *this; }
/**
* Sets the hostvariable with the given name to a Decimal.
*/
Statement& setDecimal(const std::string& col, const Decimal& data)
{ stmt->setDecimal(col, data); return *this; }
/**
* Sets the hostvariable with the given name to a float value.
*/
Statement& setFloat(const std::string& col, float data)
{ stmt->setFloat(col, data); return *this; }
/**
* Sets the hostvariable with the given name to a double value.
*/
Statement& setDouble(const std::string& col, double data)
{ stmt->setDouble(col, data); return *this; }
/**
* Sets the hostvariable with the given name to a char value.
*/
Statement& setChar(const std::string& col, char data)
{ stmt->setChar(col, data); return *this; }
/**
* Sets the hostvariable with the given name to a string value.
*/
Statement& setString(const std::string& col, const std::string& data)
{ stmt->setString(col, data); return *this; }
/**
* Sets the hostvariable with the given name to a string value or null,
* if passing null-pointer.
*/
Statement& setString(const std::string& col, const char* data)
{ data == 0 ? stmt->setNull(col)
: stmt->setString(col, data); return *this; }
/**
* Sets the hostvariable with the given name to a unicode string value.
*/
Statement& setUString(const std::string& col, const cxxtools::String& data)
{ stmt->setUString(col, data); return *this; }
/**
* Sets the hostvariable with the given name to a blob value.
*/
Statement& setBlob(const std::string& col, const Blob& data)
{ stmt->setBlob(col, data); return *this; }
/**
* Sets the hostvariable with the given name to a date value.
*/
Statement& setDate(const std::string& col, const Date& data)
{ data.isNull() ? stmt->setNull(col)
: stmt->setDate(col, data); return *this; }
/**
* Sets the hostvariable with the given name to a time value.
*/
Statement& setTime(const std::string& col, const Time& data)
{ data.isNull() ? stmt->setNull(col)
: stmt->setTime(col, data); return *this; }
/**
* Sets the hostvariable with the given name to a datetime value.
*/
Statement& setDatetime(const std::string& col, const Datetime& data)
{ data.isNull() ? stmt->setNull(col)
: stmt->setDatetime(col, data); return *this; }
//@{
/**
* Set the hostvariable with the given name to the passed value.
*
* The method uses the operator<< with a l-value of tntdb::Hostvar&
* and r-value of a const reference to the actual type to read the
* value. The operator is defined for standard types and may be defined
* for user defined types.
*/
template <typename T>
Statement& set(const std::string& col, const T& data);
//@}
/*
* Sets multiple numbered parameters to the values specified by the iterator range.
*
* The method expects, that the statement has columns with the specified column name
* appended by a number range starting from 0. This list can be generated for example
* with tntdb::SqlBuilder.
*
* Example:
*
* \code
* std::vector<int> values;
* values.push_back(5);
* values.push_back(12);
* tntdb::Statement stmt = conn.prepare("select a, b, c from tab1 where v in (" + tntdb::Statement::paramlist("v", values.size()) + ")");
* stmt.set("v", values.begin(), values.end());
* // or short version:
* stmt.set("v", values);
*
* // now stmt is ready for iteration
* \endcode
*/
template <typename Iterator>
Statement& set(const std::string& col, Iterator it1, Iterator it2);
/**
* Set the hostvariable with the given name to the passed value or null.
*
* The method sets the hostvariable to the given value if the 2nd
* argument is true. Otherwise the hostvariable is set to null.
*/
template <typename T>
Statement& setIf(const std::string& col, bool notNull, const T& data)
{
if (notNull)
set(col, data);
else
setNull(col);
return *this;
}
/// statement-execution-methods
/**
* Executes a query with the current parameters. The query should not
* return results. This method is normally used with INSERT-, UPDATE- or
* DELETE-statements.
*/
size_type execute();
/**
* Executes a query, which returns a resultset, with the current
* parameters. The query is normally a SELECT-statement.
*/
Result select();
/**
* Executes a query, which returns a row, with the current
* parameters. If the query return no rows, a exception of type
* tntdb::NotFound is thrown. When the query returns more than one row,
* additional rows are discarded.
*/
Row selectRow();
/**
* Executes a query, which returns a single value, with the current
* parameters. If the query return no rows, a exception of type
* tntdb::NotFound is thrown. Only the first value of the first row is
* returned.
*/
Value selectValue();
/**
* This methods creates a cursor and fetches the first row.
*/
const_iterator begin(unsigned fetchsize = 100) const;
/**
* A empty iterator is returned. Every const_iterator, which reaches the
* end is equal with this.
*/
const_iterator end() const;
/**
* Returns true, if this class is not connected to a actual statement.
*/
bool operator!() const { return !stmt; }
/**
* Returns the actual implementation-class.
*/
const IStatement* getImpl() const { return &*stmt; }
IStatement* getImpl() { return &*stmt; }
};
/**
* Helper class to hold a tntdb::Statement and a column name.
*/
class Hostvar
{
Statement& stmt;
const std::string& name;
public:
Hostvar(Statement& stmt_, const std::string& name_)
: stmt(stmt_),
name(name_)
{ }
Statement& getStatement() { return stmt; }
const std::string& getName() { return name; }
void setNull()
{ stmt.setNull(name); }
void setBool(bool data)
{ stmt.setBool(name, data); }
void setInt(int data)
{ stmt.setInt(name, data); }
void setLong(long data)
{ stmt.setLong(name, data); }
void setUnsigned(unsigned data)
{ stmt.setUnsigned(name, data); }
void setUnsignedLong(unsigned long data)
{ stmt.setUnsignedLong(name, data); }
void setInt32(int32_t data)
{ stmt.setInt32(name, data); }
void setUnsigned32(uint32_t data)
{ stmt.setUnsigned32(name, data); }
void setInt64(int64_t data)
{ stmt.setInt64(name, data); }
void setUnsigned64(uint64_t data)
{ stmt.setUnsigned64(name, data); }
void setDecimal(const Decimal& data)
{ stmt.setDecimal(name, data); }
void setFloat(float data)
{ stmt.setFloat(name, data); }
void setDouble(double data)
{ stmt.setDouble(name, data); }
void setChar(char data)
{ stmt.setChar(name, data); }
void setString(const std::string& data)
{ stmt.setString(name, data); }
void setString(const char* data)
{ data == 0 ? stmt.setNull(name)
: stmt.setString(name, data); }
void setUString(const cxxtools::String& data)
{ stmt.setUString(name, data); }
void setBlob(const Blob& data)
{ stmt.setBlob(name, data); }
void setDate(const Date& data)
{ data.isNull() ? stmt.setNull(name)
: stmt.setDate(name, data); }
void setTime(const Time& data)
{ data.isNull() ? stmt.setNull(name)
: stmt.setTime(name, data); }
void setDatetime(const Datetime& data)
{ data.isNull() ? stmt.setNull(name)
: stmt.setDatetime(name, data); }
template <typename T>
void set(const T& data);
};
//@{
/**
* Set operators for host variables.
*/
inline void operator<< (Hostvar& hostvar, bool data)
{
hostvar.setBool(data);
}
inline void operator<< (Hostvar& hostvar, int data)
{
hostvar.setInt(data);
}
inline void operator<< (Hostvar& hostvar, long data)
{
hostvar.setLong(data);
}
inline void operator<< (Hostvar& hostvar, unsigned data)
{
hostvar.setUnsigned(data);
}
inline void operator<< (Hostvar& hostvar, unsigned long data)
{
hostvar.setUnsignedLong(data);
}
#if INT_INT32_T_CONFLICT != 1
inline void operator<< (Hostvar& hostvar, int32_t data)
{
hostvar.setInt32(data);
}
#endif
#if UNSIGNED_UINT32_T_CONFLICT != 1
inline void operator<< (Hostvar& hostvar, uint32_t data)
{
hostvar.setUnsigned32(data);
}
#endif
#if INT_INT64_T_CONFLICT != 1
inline void operator<< (Hostvar& hostvar, int64_t data)
{
hostvar.setInt64(data);
}
#endif
#if UNSIGNED_UINT64_T_CONFLICT != 1
inline void operator<< (Hostvar& hostvar, uint64_t data)
{
hostvar.setUnsigned64(data);
}
#endif
inline void operator<< (Hostvar& hostvar, const Decimal& data)
{
hostvar.setDecimal(data);
}
inline void operator<< (Hostvar& hostvar, float data)
{
hostvar.setFloat(data);
}
inline void operator<< (Hostvar& hostvar, double data)
{
hostvar.setDouble(data);
}
inline void operator<< (Hostvar& hostvar, char data)
{
hostvar.setChar(data);
}
inline void operator<< (Hostvar& hostvar, const std::string& data)
{
hostvar.setString(data);
}
inline void operator<< (Hostvar& hostvar, const char* data)
{
hostvar.setString(data);
}
inline void operator<< (Hostvar& hostvar, const cxxtools::String& data)
{
hostvar.setUString(data);
}
inline void operator<< (Hostvar& hostvar, const Blob& data)
{
hostvar.setBlob(data);
}
inline void operator<< (Hostvar& hostvar, const Date& data)
{
hostvar.setDate(data);
}
inline void operator<< (Hostvar& hostvar, const Time& data)
{
hostvar.setTime(data);
}
inline void operator<< (Hostvar& hostvar, const Datetime& data)
{
hostvar.setDatetime(data);
}
template <typename T>
void operator<< (Hostvar& hostvar, const std::vector<T>& data)
{ hostvar.getStatement().set(hostvar.getName(), data.begin(), data.end()); }
template <typename T>
void operator<< (Hostvar& hostvar, const std::list<T>& data)
{ hostvar.getStatement().set(hostvar.getName(), data.begin(), data.end()); }
template <typename T>
void operator<< (Hostvar& hostvar, const std::deque<T>& data)
{ hostvar.getStatement().set(hostvar.getName(), data.begin(), data.end()); }
template <typename T>
void operator<< (Hostvar& hostvar, const std::set<T>& data)
{ hostvar.getStatement().set(hostvar.getName(), data.begin(), data.end()); }
//@}
template <typename T>
Statement& Statement::set(const std::string& name, const T& data)
{
Hostvar h(*this, name);
h << data;
return *this;
}
template <typename Iterator>
Statement& Statement::set(const std::string& col, Iterator it1, Iterator it2)
{
for (unsigned n = 0; it1 != it2; ++n, ++it1)
{
set(col + cxxtools::convert<std::string>(n), *it1);
}
return *this;
}
template <typename T>
void Hostvar::set(const T& data)
{
*this << data;
}
}
#endif // TNTDB_BITS_STATEMENT_H
|