Home Libraries Author Links

dbsql.hpp

Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the SysToMath DB C++ Libraries package (LibStmDb).
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Tom Michaelis, SysToMath.
00018  * Portions created by the Initial Developer are Copyright (C) 2003-2007
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 /******************************************************************************
00038  *                       First Release 2003-03-15
00039  ******************************************************************************/
00040 
00041 
00093 #ifndef STM_DBSQL_HPP
00094 #define STM_DBSQL_HPP
00095 
00096 
00121 // Include header files and macros for implementation.
00122 #include <stm/impl/dbxsql.hpp>
00123 
00124 
00125 // Include table base class implementation.
00126 #include <stm/impl/dbxsql.hpp>
00127 
00128 
00130 namespace stm
00131 {
00132 
00133 
00135 namespace db
00136 {
00137 
00138 
00140 namespace sql
00141 {
00142 
00143 
00148 // Forward declaration.
00149 class result;
00150 
00151 
00153 template <typename T>
00154 struct noconversion : public std::unary_function<T, T> {};
00155 
00156 
00160 const size_type delayedsort = local::DelayedSortFlag;
00161 
00162 
00167 const size_type recnosort = local::DelayedSortFlag | local::RecnoFlag;
00168 
00169 
00173 const size_type notsortable = local::NotSortableValue;
00174 
00175 
00177 class StmDllSpec table : private local::xtable
00178 {
00179 public:
00182     template <typename T>
00183     struct average;
00184 
00201     template
00202     <
00203         typename T,
00204         size_t Flags = 0,
00205         class Convert = noconversion<T>
00206     >
00207     struct defcol;
00208 
00222     template <class DefColSequence>
00223     struct types;
00224 
00226     class field;
00227 
00229     class column;
00230 
00234     class layout;
00235 
00236     class descr;
00237 
00240     class descrmap;
00241 
00242     class locker;
00243 
00245     virtual ~table ();
00246 
00248     virtual const std::string &name () const = 0;
00249 
00251     virtual const layout &columns () const = 0;
00252 
00255     virtual layout &columns () = 0;
00256 
00258     virtual size_t size () const = 0;
00259 
00260     virtual bool fixed () const = 0;
00261 
00262     virtual void setFixed (bool val = true) = 0;
00263 
00264     bool typeDefined (const std::string &name) const throw ();
00265 
00275     template <class TableTypes>
00276     static void defineType (const std::string &name) throw (std::runtime_error);
00277 
00278     template <class TableTypes>
00279     typename TableTypes::dbmap_type &getDbmap ();
00280 
00281     template <class TableTypes>
00282     typename TableTypes::indexes &getDbindexes ();
00283 
00284     template <class TableTypes>
00285     typename TableTypes::recnomap_type &getRecnomap ();
00286 
00287     template <class TableTypes>
00288     typename TableTypes::recnoindexes &getRecnoindexes ();
00289 
00290     template <class TableTypes>
00291     typename TableTypes::invrecnomap_type &getInvrecnomap ();
00292 
00293     template <class TableTypes>
00294     typename TableTypes::invrecnoindexes &getInvrecnoindexes ();
00295 
00296     result *createResult (const std::string &columnName,
00297                           bool descending = false);
00298     virtual result *createResult (size_t column,
00299                                   bool descending = false) = 0;
00300 
00301     virtual result *currentResult () = 0;
00302     virtual const result *currentResult () const = 0;
00303 
00304     void demandSorting (const std::string &columnName);
00305     virtual void demandSorting (size_t column) = 0;
00306 };
00307 
00308 
00309 class table::field
00310 {
00311 public:
00312     typedef boost::mpl::vector15
00313     <
00314         db::null_type,
00315         bool,
00316         char,
00317         int8,
00318         byte,
00319         int16,
00320         word,
00321         int32,
00322         dword,
00323         int64,
00324         qword,
00325         float,
00326         double,
00327         std::string,
00328         std::vector<byte>
00329         // Append additional types just before this line and do not forget to
00330         // augment the class template table::average accordingly. In order to
00331         // support the Qt QDbSqlMap database driver also consider to augment
00332         // the boost::variant::static_visitor<QVariant> derived type
00333         // QDbMapSqlResultPrivate::ToQtVariant in file qsql_dbmap.cpp
00334         // accordingly.
00335     > types;
00336 
00337     typedef boost::make_variant_over<types>::type type;
00338     typedef std::vector<type> row;
00339 
00340     struct format
00341     {
00342         format
00343         (
00344             std::string colTitle = std::string (),
00345             boost::function<type (const row &)> renderFct = 0,
00346             int length = -1,
00347             int precision = -1
00348         );
00349 
00350         std::string title;
00351         boost::function<type (const row &)> render;
00352         int len;
00353         int prec;
00354     };
00355 };
00356 
00357 
00358 template <typename T>
00359 struct table::average
00360 {
00361     typedef T value_type;
00362     value_type operator() (const value_type &val1, const value_type &val2)
00363     {
00364         return (val1 + val2) / 2;
00365     }
00366 };
00367 
00368 
00369 template <>
00370 struct table::average<db::null_type>
00371 {
00372     typedef db::null_type value_type;
00373     value_type operator() (const value_type &val1, const value_type &val2)
00374     {
00375         return db::null_type ();
00376     }
00377 };
00378 
00379 
00380 template <>
00381 struct table::average<bool>
00382 {
00383     typedef bool value_type;
00384     value_type operator() (const value_type &val1, const value_type &val2)
00385     {
00386         return false;
00387     }
00388 };
00389 
00390 
00391 template <>
00392 struct table::average<std::string>
00393 {
00394     typedef std::string value_type;
00395     typedef value_type::traits_type traits_type;
00396     value_type operator() (const value_type &val1, const value_type &val2)
00397     {
00398         value_type ret;
00399         bool less = val1.size () < val2.size ();
00400         const value_type &com = less ? val1 : val2;
00401         const value_type &big = less ? val2 : val1;
00402         value_type::size_type len = com.size ();
00403         value_type::size_type i;
00404         for (i = 0; i < len; ++ i)
00405         {
00406             ret += traits_type::to_char_type
00407                    (
00408                        (traits_type::to_int_type (com [i]) +
00409                         traits_type::to_int_type (big [i])) / 2
00410                    );
00411         }
00412         for (len = big.size (); i < len; ++ i) ret += big [i] / 2;
00413         return ret;
00414     }
00415 };
00416 
00417 
00418 template <>
00419 struct table::average<std::vector<byte> >
00420 {
00421     typedef std::vector<byte> value_type;
00422     value_type operator() (const value_type &val1, const value_type &val2)
00423     {
00424         value_type ret;
00425         bool less = val1.size () < val2.size ();
00426         const value_type &com = less ? val1 : val2;
00427         const value_type &big = less ? val2 : val2;
00428         value_type::size_type len = com.size ();
00429         value_type::size_type i;
00430         for (i = 0; i < len; ++ i) ret.push_back ((com [i] + big [i]) / 2);
00431         for (len = big.size (); i < len; ++ i) ret.push_back (big [i] / 2);
00432         return ret;
00433     }
00434 };
00435 
00436 
00437 template
00438 <
00439     typename T,
00440     size_t Flags,
00441     class Convert
00442 >
00443 struct table::defcol
00444 {
00445     typedef T value_type;
00446     typedef boost::mpl::size_t<Flags> flags_type;
00447     typedef Convert convert_type;
00448 };
00449 
00450 
00454 }}}  // namespace stm::db::sql
00455 
00456 
00457 // Include table::types base class implementation.
00458 #include <stm/impl/dbxsql.hpp>
00459 
00460 
00461 namespace stm {
00462 namespace db {
00463 namespace sql {
00464 
00465     
00470 template <class DefColSequence>
00471 struct table::types : private local::xtypes<DefColSequence>
00472 {
00473     typedef DefColSequence coldescr_type;
00474     typedef typename local::xtypes<DefColSequence>::pkey_type pkey_type;
00475     typedef typename local::xtypes<DefColSequence>::mapped_type mapped_type;
00476     typedef typename local::xtypes<DefColSequence>::dbmap_type dbmap_type;
00477     typedef typename local::xtypes<DefColSequence>::indexes indexes;
00478     typedef typename local::xtypes<DefColSequence>::recnomap_type recnomap_type;
00479     typedef typename local::xtypes<DefColSequence>::recnoindexes recnoindexes;
00480     typedef typename local::xtypes<DefColSequence>::invrecnomap_type invrecnomap_type;
00481     typedef typename local::xtypes<DefColSequence>::invrecnoindexes invrecnoindexes;
00482 
00483     static const size_t ColumnCount = boost::mpl::size<coldescr_type>::type::value;
00484     typedef boost::array<table::column, ColumnCount> column_array;
00485 };
00486 
00487 
00491 }}}  // namespace stm::db::sql
00492 
00493 
00494 // Include table::column, table::descr and table::locker base class
00495 // implementation.
00496 #include <stm/impl/dbxsql.hpp>
00497 
00498 
00499 namespace stm {
00500 namespace db {
00501 namespace sql {
00502 
00503     
00508 class table::column : private local::xcolumn
00509 {
00510     friend struct db::stream<table::column>;
00511 
00512 public:
00513     column (const std::string &name = std::string (),
00514             const table::field::type &val = db::null_type (),
00515             const table::field::format *pFormat = NULL,
00516             bool defval = false);
00517 
00518     const std::string &name () const;
00519 
00520     const std::string &title () const;
00521 
00522     const table::field::format *&pFormat ();
00523 
00524     const table::field::format *pFormat () const;
00525 
00526     table::field::type render
00527     (
00528         const table::field::row &rowData,
00529         size_t colNr
00530     ) const;
00531 
00532     const table::field::type &val () const;
00533     
00534     bool defval () const;
00535     
00536     int len () const;
00537     
00538     int prec () const;
00539     
00540     bool isSortable () const;
00541     
00542     void setSortable (bool val = true);
00543     
00544     bool isSorted () const;
00545     
00546     void setSorted (bool val = true);
00547     
00548     bool isSorting () const;
00549     
00550     void setSorting (bool val = true);
00551     
00552     bool hasRecno () const;
00553     
00554     void setHasRecno (bool val = true);
00555     
00556     bool isRecnoValid () const;
00557     
00558     void setRecnoValid (bool val = true);
00559 };
00560 
00561 
00562 class table::layout : public std::vector<table::column>
00563 {
00564 public:
00565     layout ();
00566 
00567     layout (const layout &columns);
00568 
00569     template <class InputIterator>
00570     layout (InputIterator first, InputIterator last);
00571 };
00572 
00573 
00574 class table::descr : public table::layout, private local::xdescr
00575 {
00576     friend struct db::stream<table::descr>;
00577     friend class connection;
00578 
00579 public:
00580     descr ();
00581 
00582     descr (const std::string &type, const table::layout &columns);
00583 
00584     ~descr ();
00585 
00586     bool setTypeLayout (const std::string &type, table::layout &columns);
00587     
00588     const std::string &type () const;
00589 
00593     table *active () const;
00594 };
00595 
00596 
00597 class table::descrmap : public db::map<std::string, table::descr>
00598 {
00599 public:
00600     descrmap ();
00601     descrmap (const std::string &name);
00602 };
00603 
00604 
00605 class table::locker : private local::xlocker
00606 {
00607 public:
00608     locker (const table *pTable);
00609     ~locker ();
00610 };
00611 
00612 
00614 class StmDllSpec result
00615 {
00616 public:
00618     typedef table::field::row row;
00619 
00621     virtual ~result ();
00622 
00624     virtual const table &rtable () const = 0;
00625 
00627     virtual const table::layout &columns () const = 0;
00628 
00630     virtual row &data () = 0;
00631     virtual const row &data () const = 0;
00632 
00636     virtual bool getData (size_t pos, row &data) = 0;
00637 
00639     virtual size_t size () const = 0;
00640 
00642     virtual bool valid () const = 0;
00643 
00645     virtual bool isRecnoValid () const = 0;
00646 
00650     virtual size_t getRecno () const throw (std::runtime_error) = 0;
00651 
00653     virtual bool syncFromOther (result *other) throw (std::runtime_error) = 0;
00654 
00656     virtual bool advance (ptrdiff_t dist) throw (std::runtime_error) = 0;
00657 
00659     virtual bool position (size_t pos) throw (std::runtime_error) = 0;
00660 
00662     virtual bool next () throw (std::runtime_error) = 0;
00663 
00665     virtual bool prev () throw (std::runtime_error) = 0;
00666 
00668     virtual bool first () throw (std::runtime_error) = 0;
00669 
00671     virtual bool last () throw (std::runtime_error) = 0;
00672 
00674     virtual size_t getSortColNr () const = 0;
00675 
00677     virtual bool isDescending () const = 0;
00678 };
00679 
00680 
00684 }}}  // namespace stm::db::sql
00685 
00686 
00687 // Include connection base class implementation.
00688 #include <stm/impl/dbxsql.hpp>
00689 
00690 
00691 namespace stm {
00692 namespace db {
00693 namespace sql {
00694 
00695     
00700 
00701 class StmDllSpec connection : private local::xconnection
00702 {
00703 public:
00705     explicit connection (const std::string &path) throw (std::runtime_error);
00706 
00707     ~connection ();
00708 
00710     const std::string &path () const;
00711 
00713     bool existsTable (const std::string &name) const throw ();
00714 
00719     void createTable
00720     (
00721         const std::string &name,
00722         const std::string &type,
00723         const table::layout &columns
00724     ) throw (std::runtime_error);
00725 
00731     template <class InputIterator>
00732     void createTable
00733     (
00734         const std::string &name,
00735         const std::string &type,
00736         InputIterator firstColumn,
00737         InputIterator lastColumn
00738     ) throw (std::runtime_error);
00739 
00746     bool deleteTable (const std::string &name, bool force = false);
00747 
00754     bool activateTable (const std::string &name,
00755                         size_type cacheSizeKb = 0) throw (std::runtime_error);
00756 
00761     bool deactivateTable (const std::string &name);
00762 
00763     table &getTable (const std::string &name) const throw (std::runtime_error);
00764 
00765     const std::string &getTableType (const std::string &name) const throw (std::runtime_error);
00766 
00769     const table::descrmap &tables () const;
00770 
00773     table::descr *getTableDescr (const std::string &name);
00774 
00777     result *execsql (const std::string &cmd) throw (std::runtime_error);
00778 
00780     int errorno () const;
00781 
00783     const std::string &errormsg () const;
00784 };
00785 
00786 
00787 }}}  // namespace stm::db::sql
00788 
00789 
00790 // Include inline implementation.
00791 #include <stm/impl/dbxsql.hpp>
00792 
00793 
00794 #endif  // STM_DBSQL_HPP

© Copyright Tom Michaelis 2002-2007

Distributed under the SysToMath Software License (See the accompanying file license.txt or a copy at www.SysToMath.com).