LCOV - code coverage report
Current view: top level - core/db/sql - SPSqlDriver.cc (source / functions) Hit Total Coverage
Test: coverage.info Lines: 51 66 77.3 %
Date: 2024-05-12 00:16:13 Functions: 12 15 80.0 %

          Line data    Source code
       1             : /**
       2             : Copyright (c) 2021-2022 Roman Katuntsev <sbkarr@stappler.org>
       3             : Copyright (c) 2023-2024 Stappler LLC <admin@stappler.dev>
       4             : 
       5             : Permission is hereby granted, free of charge, to any person obtaining a copy
       6             : of this software and associated documentation files (the "Software"), to deal
       7             : in the Software without restriction, including without limitation the rights
       8             : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
       9             : copies of the Software, and to permit persons to whom the Software is
      10             : furnished to do so, subject to the following conditions:
      11             : 
      12             : The above copyright notice and this permission notice shall be included in
      13             : all copies or substantial portions of the Software.
      14             : 
      15             : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      16             : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      17             : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      18             : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      19             : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      20             : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
      21             : THE SOFTWARE.
      22             : **/
      23             : 
      24             : #include "SPSqlDriver.h"
      25             : #include "SPPqDriver.h"
      26             : #include "SPSqliteDriver.h"
      27             : #include "SPDbAdapter.h"
      28             : 
      29             : namespace STAPPLER_VERSIONIZED stappler::db::sql {
      30             : 
      31             : thread_local std::map<StringView, Map<StringView, const void *>> tl_DriverQueryStorage;
      32             : 
      33       16175 : QueryStorageHandle::QueryStorageHandle(const Driver *d, StringView n, Map<StringView, const void *> *dt)
      34       16175 : : driver(d), name(n), data(dt) { }
      35             : 
      36       16175 : QueryStorageHandle::~QueryStorageHandle() {
      37       16175 :         if (driver) {
      38       15750 :                 driver->unregisterQueryStorage(name);
      39             :         }
      40       16175 : }
      41             : 
      42           0 : QueryStorageHandle::QueryStorageHandle(QueryStorageHandle &&other)
      43           0 : : driver(other.driver), name(other.name), data(other.data) {
      44           0 :         other.driver = nullptr;
      45           0 : }
      46             : 
      47           0 : QueryStorageHandle& QueryStorageHandle::operator=(QueryStorageHandle &&other) {
      48           0 :         driver = other.driver;
      49           0 :         name = other.name;
      50           0 :         data = other.data;
      51           0 :         other.driver = nullptr;
      52           0 :         return *this;
      53             : }
      54             : 
      55         150 : Driver *Driver::open(pool_t *pool, ApplicationInterface *app, StringView path, const void *external) {
      56         150 :         Driver *ret = nullptr;
      57         150 :         pool::push(pool);
      58         150 :         if (path == "pgsql") {
      59          50 :                 ret = pq::Driver::open(pool, app, StringView(), external);
      60         100 :         } else if (path.starts_with("pgsql:")) {
      61           0 :                 path += "pgsql:"_len;
      62           0 :                 ret = pq::Driver::open(pool, app, path, external);
      63         100 :         } else if (path == "sqlite" || path == "sqlite3") {
      64         100 :                 ret = sqlite::Driver::open(pool, app, path);
      65             :         }
      66             : 
      67         150 :         registerCleanupDestructor(ret, pool);
      68             : 
      69         150 :         pool::pop();
      70         150 :         return ret;
      71             : }
      72             : 
      73         125 : Driver::~Driver() { }
      74             : 
      75          25 : void Driver::setDbCtrl(Function<void(bool)> &&fn) {
      76          25 :         _dbCtrl = std::move(fn);
      77          25 : }
      78             : 
      79       18550 : const CustomFieldInfo *Driver::getCustomFieldInfo(StringView key) const {
      80       18550 :         auto it = _customFields.find(key);
      81       18550 :         if (it != _customFields.end()) {
      82       18550 :                 return &it->second;
      83             :         }
      84           0 :         return nullptr;
      85             : }
      86             : 
      87       16175 : QueryStorageHandle Driver::makeQueryStorage(StringView name) const {
      88       16175 :         auto d = registerQueryStorage(name);
      89       16175 :         if (d) {
      90       15750 :                 return QueryStorageHandle(this, name, d);
      91             :         }
      92         425 :         return QueryStorageHandle(nullptr, name, nullptr);
      93             : }
      94             : 
      95        1800 : Map<StringView, const void *> *Driver::getQueryStorage(StringView name) const {
      96        1800 :         auto it = tl_DriverQueryStorage.find(name);
      97        1800 :         if (it != tl_DriverQueryStorage.end()) {
      98        1800 :                 return &it->second;
      99             :         }
     100           0 :         return nullptr;
     101             : }
     102             : 
     103         500 : Map<StringView, const void *> *Driver::getCurrentQueryStorage() const {
     104         500 :         if (tl_DriverQueryStorage.size() > 0) {
     105         500 :                 return &tl_DriverQueryStorage.begin()->second;
     106             :         }
     107           0 :         return nullptr;
     108             : }
     109             : 
     110       16175 : Map<StringView, const void *> *Driver::registerQueryStorage(StringView name) const {
     111       16175 :         if (tl_DriverQueryStorage.find(name) != tl_DriverQueryStorage.end()) {
     112         425 :                 return nullptr;
     113             :         }
     114             : 
     115       15750 :         auto ret = &tl_DriverQueryStorage.emplace(name, Map<StringView, const void *>()).first->second;
     116             : 
     117       15750 :         return ret;
     118             : }
     119             : 
     120       15750 : void Driver::unregisterQueryStorage(StringView name) const {
     121       15750 :         tl_DriverQueryStorage.erase(name);
     122       15750 : }
     123             : 
     124         150 : Driver::Driver(pool_t *p, ApplicationInterface *app)
     125         150 : : _pool(p), _application(app) {
     126         150 :         if (!app) {
     127          50 :                 auto mem = pool::palloc(_pool, sizeof(ApplicationInterface));
     128             : 
     129          50 :                 _application = new (mem) ApplicationInterface;
     130             :         }
     131         150 : }
     132             : 
     133             : }

Generated by: LCOV version 1.14