You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.8 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2022 Jon Evans <jon@craftyjon.com>
  5. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software: you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation, either version 3 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef KICAD_DATABASE_CONNECTION_H
  21. #define KICAD_DATABASE_CONNECTION_H
  22. #include <any>
  23. #include <map>
  24. #include <memory>
  25. #include <set>
  26. #include <vector>
  27. #include <database/database_cache.h>
  28. extern const char* const traceDatabase;
  29. namespace nanodbc
  30. {
  31. class connection;
  32. }
  33. class DATABASE_CONNECTION
  34. {
  35. public:
  36. static const long DEFAULT_TIMEOUT = 10;
  37. typedef std::map<std::string, std::any> ROW;
  38. DATABASE_CONNECTION( const std::string& aDataSourceName, const std::string& aUsername,
  39. const std::string& aPassword, int aTimeoutSeconds = DEFAULT_TIMEOUT,
  40. bool aConnectNow = true );
  41. DATABASE_CONNECTION( const std::string& aConnectionString,
  42. int aTimeoutSeconds = DEFAULT_TIMEOUT,
  43. bool aConnectNow = true );
  44. ~DATABASE_CONNECTION();
  45. void SetCacheParams( int aMaxSize, int aMaxAge );
  46. bool Connect();
  47. bool Disconnect();
  48. bool IsConnected() const;
  49. bool CacheTableInfo( const std::string& aTable, const std::set<std::string>& aColumns );
  50. /**
  51. * Retrieves a single row from a database table. Table and column names are cached when the
  52. * connection is created, so schema changes to the database won't be recognized unless the
  53. * connection is recreated.
  54. * @param aTable the name of a table in the database
  55. * @param aWhere column to search, and the value to search for
  56. * @param aResult will be filled with a row in the database if one was found
  57. * @return true if aResult was filled; false otherwise
  58. */
  59. bool SelectOne( const std::string& aTable, const std::pair<std::string, std::string>& aWhere,
  60. ROW& aResult );
  61. /**
  62. * Retrieves all rows from a database table.
  63. * @param aTable the name of a table in the database
  64. * @param aKey holds the column name of the primary key used for caching results
  65. * @param aResults will be filled with all rows in the table
  66. * @return true if the query succeeded and at least one ROW was found, false otherwise
  67. */
  68. bool SelectAll( const std::string& aTable, const std::string& aKey,
  69. std::vector<ROW>& aResults );
  70. std::string GetLastError() const { return m_lastError; }
  71. private:
  72. void init();
  73. bool getQuoteChar();
  74. std::string columnsFor( const std::string& aTable );
  75. bool selectAllAndCache( const std::string& aTable, const std::string& aKey );
  76. std::unique_ptr<nanodbc::connection> m_conn;
  77. std::string m_dsn;
  78. std::string m_user;
  79. std::string m_pass;
  80. std::string m_connectionString;
  81. std::string m_lastError;
  82. std::map<std::string, std::string> m_tables;
  83. /// Map of table -> map of column name -> data type
  84. std::map<std::string, std::map<std::string, int>> m_columnCache;
  85. long m_timeout;
  86. char m_quoteChar;
  87. typedef DATABASE_CACHE<std::map<std::string, ROW>> DB_CACHE_TYPE;
  88. std::unique_ptr<DB_CACHE_TYPE> m_cache;
  89. };
  90. #endif //KICAD_DATABASE_CONNECTION_H