utils.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2010-2011 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
  4. //
  5. // Distributed under:
  6. //
  7. // the Boost Software License, Version 1.0.
  8. // (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // or (at your opinion) under:
  12. //
  13. // The MIT License
  14. // (See accompanying file MIT.txt or a copy at
  15. // http://www.opensource.org/licenses/mit-license.php)
  16. //
  17. ///////////////////////////////////////////////////////////////////////////////
  18. #ifndef CPPDB_UTIL_H
  19. #define CPPDB_UTIL_H
  20. #include <cppdb/defs.h>
  21. #include <string>
  22. #include <ctime>
  23. #include <map>
  24. namespace cppdb {
  25. ///
  26. /// \brief parse a string as time value.
  27. ///
  28. /// Used by backend implementations;
  29. ///
  30. CPPDB_API std::tm parse_time(char const *value);
  31. ///
  32. /// \brief format a string as time value.
  33. ///
  34. /// Used by backend implementations;
  35. ///
  36. CPPDB_API std::string format_time(std::tm const &v);
  37. ///
  38. /// \brief parse a string as time value.
  39. ///
  40. /// Used by backend implementations;
  41. ///
  42. CPPDB_API std::tm parse_time(std::string const &v);
  43. ///
  44. /// \brief Parse a connection string \a cs into driver name \a driver_name and list of properties \a props
  45. ///
  46. /// The connection string format is following:
  47. ///
  48. /// \verbatim driver:[key=value;]* \endverbatim
  49. ///
  50. /// Where value can be either a sequence of characters (white space is trimmed) or it may be a general
  51. /// sequence encloded in a single quitation marks were double quote is used for insering a single quote value.
  52. ///
  53. /// Key values starting with \@ are reserved to be used as special cppdb keys
  54. /// For example:
  55. ///
  56. /// \verbatim mysql:username= root;password = 'asdf''5764dg';database=test;@use_prepared=off' \endverbatim
  57. ///
  58. /// Where driver is "mysql", username is "root", password is "asdf'5764dg", database is "test" and
  59. /// special value "@use_prepared" is off - internal cppdb option.
  60. CPPDB_API void parse_connection_string( std::string const &cs,
  61. std::string &driver_name,
  62. std::map<std::string,std::string> &props);
  63. ///
  64. /// \brief Class that represents parsed connection string
  65. ///
  66. class CPPDB_API connection_info {
  67. public:
  68. ///
  69. /// The original connection string
  70. ///
  71. std::string connection_string;
  72. ///
  73. /// The driver name
  74. ///
  75. std::string driver;
  76. ///
  77. /// Type that represent key, values set
  78. ///
  79. typedef std::map<std::string,std::string> properties_type;
  80. ///
  81. /// The std::map of key value properties.
  82. ///
  83. properties_type properties;
  84. ///
  85. /// Cheks if property \a prop, has been given in connection string.
  86. ///
  87. bool has(std::string const &prop) const;
  88. ///
  89. /// Get property \a prop, returning \a default_value if not defined.
  90. ///
  91. std::string get(std::string const &prop,std::string const &default_value=std::string()) const;
  92. ///
  93. /// Get numeric value for property \a prop, returning \a default_value if not defined.
  94. /// If the value is not a number, throws cppdb_error.
  95. ///
  96. int get(std::string const &prop,int default_value) const;
  97. ///
  98. /// Default constructor - empty info
  99. ///
  100. connection_info()
  101. {
  102. }
  103. ///
  104. /// Create connection_info from the connection string parsing it.
  105. ///
  106. explicit connection_info(std::string const &cs) :
  107. connection_string(cs)
  108. {
  109. parse_connection_string(cs,driver,properties);
  110. }
  111. };
  112. }
  113. #endif