--- /dev/null
+# This is the official list of ViST authors for copyright purposes.
+# This file is distinct from the CONTRIBUTORS files.
+# See the latter for an explanation.
+#
+# Names should be added to this file as:
+# Name or Organization <email address>
+# The email address is not required for organizations.
+#
+# Please keep the list sorted.
+
+Jaemin Ryu <jm77.ryu@samsung.com>
+Samsung Electronics Co.
+Sangwan Kwon <sangwan.kwon@samsung.com>
#include <bluetooth-api.h>
#include <bluetooth_internal.h>
-#include <vist/policy/sdk/policy-model.h>
-#include <vist/policy/sdk/policy-provider.h>
+#include <vist/policy/sdk/policy-model.hpp>
+#include <vist/policy/sdk/policy-provider.hpp>
#include <memory>
#include <arpa/inet.h>
#include <wifi-manager.h>
-#include <vist/policy/sdk/policy-model.h>
-#include <vist/policy/sdk/policy-provider.h>
+#include <vist/policy/sdk/policy-model.hpp>
+#include <vist/policy/sdk/policy-provider.hpp>
#include <klay/dbus/connection.h>
#include <osquery/sql.h>
#include <osquery/tables.h>
-#include <vist/policy/api.h>
-#include <vist/common/audit/logger.h>
+#include <vist/policy/api.hpp>
+#include <vist/logger.hpp>
namespace osquery {
namespace tables {
#include <osquery/sql.h>
#include <osquery/tables.h>
-#include <vist/policy/api.h>
-#include <vist/common/audit/logger.h>
+#include <vist/policy/api.hpp>
+#include <vist/logger.hpp>
namespace {
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#include <gtest/gtest.h>
+
+#include <vist/policy/api.hpp>
+
+using namespace vist;
+
+class PolicyTests : public testing::Test {};
+
+TEST_F(PolicyTests, get_all) {
+ auto policies = policy::API::GetAll();
+
+ EXPECT_TRUE(policies.size() > 0);
+}
+
+TEST_F(PolicyTests, get_admin_all) {
+ auto admins = policy::API::Admin::GetAll();
+ EXPECT_EQ(admins.size(), 0);
+
+ policy::API::Admin::Enroll("testAdmin");
+ admins = policy::API::Admin::GetAll();
+ EXPECT_EQ(admins.size(), 1);
+
+ policy::API::Admin::Enroll("testAdmin1");
+ admins = policy::API::Admin::GetAll();
+ EXPECT_EQ(admins.size(), 2);
+
+ policy::API::Admin::Disenroll("testAdmin");
+ admins = policy::API::Admin::GetAll();
+ EXPECT_EQ(admins.size(), 1);
+
+ policy::API::Admin::Disenroll("testAdmin1");
+ admins = policy::API::Admin::GetAll();
+ EXPECT_EQ(admins.size(), 0);
+}
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-#include <gtest/gtest.h>
-
-#include <vist/policy/api.h>
-
-using namespace vist;
-
-class PolicyTests : public testing::Test {};
-
-TEST_F(PolicyTests, get_all) {
- auto policies = policy::API::GetAll();
-
- EXPECT_TRUE(policies.size() > 0);
-}
-
-TEST_F(PolicyTests, get_admin_all) {
- auto admins = policy::API::Admin::GetAll();
- EXPECT_EQ(admins.size(), 0);
-
- policy::API::Admin::Enroll("testAdmin");
- admins = policy::API::Admin::GetAll();
- EXPECT_EQ(admins.size(), 1);
-
- policy::API::Admin::Enroll("testAdmin1");
- admins = policy::API::Admin::GetAll();
- EXPECT_EQ(admins.size(), 2);
-
- policy::API::Admin::Disenroll("testAdmin");
- admins = policy::API::Admin::GetAll();
- EXPECT_EQ(admins.size(), 1);
-
- policy::API::Admin::Disenroll("testAdmin1");
- admins = policy::API::Admin::GetAll();
- EXPECT_EQ(admins.size(), 0);
-}
-DSCRIPT_INSTALL_DIR="${SCRIPT_INSTALL_DIR}")
ADD_SUBDIRECTORY(client)
-ADD_SUBDIRECTORY(common)
ADD_SUBDIRECTORY(notification)
ADD_SUBDIRECTORY(policy)
ADD_SUBDIRECTORY(service)
WORLD_READ
WORLD_EXECUTE)
+FILE(GLOB COMMON_TESTS "tests/*.cpp")
+ADD_VIST_TEST(${COMMON_TESTS})
+
ADD_EXECUTABLE(${TARGET_VIST_TEST} main/tests.cpp
${${TARGET_VIST_LIB}_TESTS})
TARGET_LINK_LIBRARIES(${TARGET_VIST_TEST} ${TARGET_VIST_LIB}
* limitations under the License
*/
-#include "query.h"
+#include "query.hpp"
-#include <vist/common/ipc/client.h>
-#include <vist/common/audit/logger.h>
+#include <vist/logger.hpp>
+#include <vist/ipc/client.hpp>
namespace {
const std::string SOCK_ADDR = "/tmp/.vist";
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-#pragma once
-
-#include <map>
-#include <string>
-#include <vector>
-
-namespace vist {
-
-using Row = std::map<std::string, std::string>;
-using Rows = std::vector<Row>;
-
-struct Query final {
- static Rows Execute(const std::string& statement);
-};
-
-} // namespace vist
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#pragma once
+
+#include <map>
+#include <string>
+#include <vector>
+
+namespace vist {
+
+using Row = std::map<std::string, std::string>;
+using Rows = std::vector<Row>;
+
+struct Query final {
+ static Rows Execute(const std::string& statement);
+};
+
+} // namespace vist
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-
-/**
- * @file processes.h
- * @brief The scheme of policy
- */
-
-
-#pragma once
-
-#include <string>
-
-namespace vist {
-namespace schema {
-
-struct Policy {
- std::string name;
- std::string value;
-};
-
-} // namesapce schema
-} // namesapce vist
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#pragma once
+
+#include <string>
+
+namespace vist {
+namespace schema {
+
+struct Policy {
+ std::string name;
+ std::string value;
+};
+
+} // namesapce schema
+} // namesapce vist
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-
-/**
- * @file processes.h
- * @brief The scheme of processes (sync with osquery/tables/spec/x/processes.table)
- */
-
-
-#pragma once
-
-#include <string>
-
-namespace vist {
-namespace schema {
-
-struct Processes {
- long long int pid;
- std::string name;
- std::string path;
- std::string cmdline;
- long long int uid;
- long long int gid;
- long long int euid;
- long long int egid;
- int on_disk;
- long long int resident_size;
- long long int parent;
-};
-
-} // namesapce schema
-} // namesapce vist
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#pragma once
+
+#include <string>
+
+namespace vist {
+namespace schema {
+
+struct Processes {
+ long long int pid;
+ std::string name;
+ std::string path;
+ std::string cmdline;
+ long long int uid;
+ long long int gid;
+ long long int euid;
+ long long int egid;
+ int on_disk;
+ long long int resident_size;
+ long long int parent;
+};
+
+} // namesapce schema
+} // namesapce vist
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-
-/**
- * @file time.h
- * @brief The scheme of time (sync with osquery/tables/spec/time.table)
- */
-
-
-#pragma once
-
-namespace vist {
-namespace schema {
-
-struct Time {
- int hour;
- int minutes;
- int seconds;
-};
-
-} // namesapce schema
-} // namesapce vist
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#pragma once
+
+namespace vist {
+namespace schema {
+
+struct Time {
+ int hour;
+ int minutes;
+ int seconds;
+};
+
+} // namesapce schema
+} // namesapce vist
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-#include <gtest/gtest.h>
-
-#include "../query.h"
-
-#include <chrono>
-#include <thread>
-
-using namespace vist;
-
-class ClientTests : public testing::Test {};
-
-TEST_F(ClientTests, query) {
- auto rows = Query::Execute("SELECT * FROM policy");
-
- EXPECT_TRUE(rows.size() > 0);
-}
-
-TEST_F(ClientTests, admin_enrollment) {
- auto rows = Query::Execute("INSERT INTO policy_admin (name) VALUES ('testAdmin')");
- EXPECT_EQ(rows.size(), 0);
-
- rows = Query::Execute("SELECT * FROM policy_admin");
- EXPECT_EQ(rows.size(), 1);
-
- Query::Execute("INSERT INTO policy_admin (name) VALUES ('testAdmin2')");
- rows = Query::Execute("SELECT * FROM policy_admin");
- EXPECT_EQ(rows.size(), 2);
-
- rows = Query::Execute("DELETE FROM policy_admin WHERE name = 'testAdmin'");
- EXPECT_EQ(rows.size(), 0);
-
- rows = Query::Execute("SELECT * FROM policy_admin");
- EXPECT_EQ(rows.size(), 1);
-
- Query::Execute("DELETE FROM policy_admin WHERE name = 'testAdmin2'");
- rows = Query::Execute("SELECT * FROM policy_admin");
- EXPECT_EQ(rows.size(), 0);
-}
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#include <gtest/gtest.h>
+
+#include <vist/client/query.hpp>
+
+#include <chrono>
+#include <thread>
+
+using namespace vist;
+
+class ClientTests : public testing::Test {};
+
+TEST(ClientTests, query) {
+ auto rows = Query::Execute("SELECT * FROM policy");
+
+ EXPECT_TRUE(rows.size() > 0);
+}
+
+TEST(ClientTests, admin_enrollment) {
+ auto rows = Query::Execute("INSERT INTO policy_admin (name) VALUES ('testAdmin')");
+ EXPECT_EQ(rows.size(), 0);
+
+ rows = Query::Execute("SELECT * FROM policy_admin");
+ EXPECT_EQ(rows.size(), 1);
+
+ Query::Execute("INSERT INTO policy_admin (name) VALUES ('testAdmin2')");
+ rows = Query::Execute("SELECT * FROM policy_admin");
+ EXPECT_EQ(rows.size(), 2);
+
+ rows = Query::Execute("DELETE FROM policy_admin WHERE name = 'testAdmin'");
+ EXPECT_EQ(rows.size(), 0);
+
+ rows = Query::Execute("SELECT * FROM policy_admin");
+ EXPECT_EQ(rows.size(), 1);
+
+ Query::Execute("DELETE FROM policy_admin WHERE name = 'testAdmin2'");
+ rows = Query::Execute("SELECT * FROM policy_admin");
+ EXPECT_EQ(rows.size(), 0);
+}
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-#include <gtest/gtest.h>
-
-#include "../virtual-table.h"
-
-#include "../schema/time.h"
-#include "../schema/policy.h"
-#include "../schema/processes.h"
-
-#include <vist/common/audit/logger.h>
-
-using namespace vist;
-using namespace vist::schema;
-
-class VirtualTableTests : public testing::Test {};
-
-TEST_F(VirtualTableTests, time_row_at) {
- Time result = { -1, -1, -1 };
-
- VirtualRow<Time> time;
- result.hour = time.at(&Time::hour);
- result.minutes = time.at(&Time::minutes);
- result.seconds = time.at(&Time::seconds);
-
- /// Once query execution
- INFO(VIST_CLIENT, "[Test] time table:");
- INFO(VIST_CLIENT, "\t hour: " << result.hour);
- INFO(VIST_CLIENT, "\t minutes: " << result.minutes);
- INFO(VIST_CLIENT, "\t seconds: " << result.seconds);
-
- /// Each query execution
- INFO(VIST_CLIENT, "[Test] time table:");
- INFO(VIST_CLIENT, "\t hour: " << VirtualRow<Time>().at(&Time::hour));
- INFO(VIST_CLIENT, "\t minutes: " << VirtualRow<Time>().at(&Time::minutes));
- INFO(VIST_CLIENT, "\t seconds: " << VirtualRow<Time>().at(&Time::seconds));
-
- EXPECT_NE(result.hour, -1);
- EXPECT_NE(result.minutes, -1);
- EXPECT_NE(result.seconds, -1);
-}
-
-TEST_F(VirtualTableTests, time_row_arry_op) {
- Time result = { -1, -1, -1 };
-
- VirtualRow<Time> time;
- result.hour = time[&Time::hour];
- result.minutes = time[&Time::minutes];
- result.seconds = time[&Time::seconds];
-
- /// Once query execution
- INFO(VIST_CLIENT, "[Test] time table:");
- INFO(VIST_CLIENT, "\t hour: " << result.hour);
- INFO(VIST_CLIENT, "\t minutes: " << result.minutes);
- INFO(VIST_CLIENT, "\t seconds: " << result.seconds);
-
- EXPECT_NE(result.hour, -1);
- EXPECT_NE(result.minutes, -1);
- EXPECT_NE(result.seconds, -1);
-}
-
-TEST_F(VirtualTableTests, processes_table) {
- Processes result;
- VirtualTable<Processes> processes;
- EXPECT_TRUE(processes.size() > 0);
-
- for(auto& p : processes) {
- EXPECT_TRUE(p.size() > 0);
- result.pid = p.at(&Processes::pid);
- result.name = p.at(&Processes::name);
- result.path = p.at(&Processes::path);
- result.cmdline = p.at(&Processes::cmdline);
- result.uid = p.at(&Processes::uid);
- result.gid = p.at(&Processes::gid);
- result.euid = p.at(&Processes::euid);
- result.egid = p.at(&Processes::egid);
- result.on_disk = p.at(&Processes::on_disk);
- result.parent = p.at(&Processes::parent);
-
- INFO(VIST_CLIENT, "[Test] Processes table:");
- INFO(VIST_CLIENT, "\t pid: " << result.pid);
- INFO(VIST_CLIENT, "\t name: " << result.name);
- INFO(VIST_CLIENT, "\t path: " << result.path);
- INFO(VIST_CLIENT, "\t cmdline: " << result.cmdline);
- INFO(VIST_CLIENT, "\t uid: " << result.uid);
- INFO(VIST_CLIENT, "\t gid: " << result.gid);
- INFO(VIST_CLIENT, "\t euid: " << result.euid);
- INFO(VIST_CLIENT, "\t egid: " << result.egid);
- INFO(VIST_CLIENT, "\t on_disk: " << result.on_disk);
- INFO(VIST_CLIENT, "\t parent: " << result.parent);
- }
-}
-
-TEST_F(VirtualTableTests, policy_table) {
- VirtualTable<Policy> table;
- EXPECT_TRUE(table.size() > 0);
-
- for(const auto& row : table) {
- Policy policy = { row[&Policy::name], row[&Policy::value] };
-
- INFO(VIST_CLIENT, "[Test] Policy table:");
- INFO(VIST_CLIENT, "\t name: " << policy.name);
- INFO(VIST_CLIENT, "\t value: " << policy.value);
- }
-}
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#include <gtest/gtest.h>
+
+#include <vist/client/virtual-table.hpp>
+
+#include <vist/client/schema/policy.hpp>
+#include <vist/client/schema/processes.hpp>
+#include <vist/client/schema/time.hpp>
+
+#include <vist/logger.hpp>
+
+using namespace vist;
+using namespace vist::schema;
+
+class VirtualTableTests : public testing::Test {};
+
+TEST(VirtualTableTests, time_row_at) {
+ Time result = { -1, -1, -1 };
+
+ VirtualRow<Time> time;
+ result.hour = time.at(&Time::hour);
+ result.minutes = time.at(&Time::minutes);
+ result.seconds = time.at(&Time::seconds);
+
+ /// Once query execution
+ INFO(VIST_CLIENT, "[Test] time table:");
+ INFO(VIST_CLIENT, "\t hour: " << result.hour);
+ INFO(VIST_CLIENT, "\t minutes: " << result.minutes);
+ INFO(VIST_CLIENT, "\t seconds: " << result.seconds);
+
+ /// Each query execution
+ INFO(VIST_CLIENT, "[Test] time table:");
+ INFO(VIST_CLIENT, "\t hour: " << VirtualRow<Time>().at(&Time::hour));
+ INFO(VIST_CLIENT, "\t minutes: " << VirtualRow<Time>().at(&Time::minutes));
+ INFO(VIST_CLIENT, "\t seconds: " << VirtualRow<Time>().at(&Time::seconds));
+
+ EXPECT_NE(result.hour, -1);
+ EXPECT_NE(result.minutes, -1);
+ EXPECT_NE(result.seconds, -1);
+}
+
+TEST(VirtualTableTests, time_row_arry_op) {
+ Time result = { -1, -1, -1 };
+
+ VirtualRow<Time> time;
+ result.hour = time[&Time::hour];
+ result.minutes = time[&Time::minutes];
+ result.seconds = time[&Time::seconds];
+
+ /// Once query execution
+ INFO(VIST_CLIENT, "[Test] time table:");
+ INFO(VIST_CLIENT, "\t hour: " << result.hour);
+ INFO(VIST_CLIENT, "\t minutes: " << result.minutes);
+ INFO(VIST_CLIENT, "\t seconds: " << result.seconds);
+
+ EXPECT_NE(result.hour, -1);
+ EXPECT_NE(result.minutes, -1);
+ EXPECT_NE(result.seconds, -1);
+}
+
+TEST(VirtualTableTests, processes_table) {
+ Processes result;
+ VirtualTable<Processes> processes;
+ EXPECT_TRUE(processes.size() > 0);
+
+ for(auto& p : processes) {
+ EXPECT_TRUE(p.size() > 0);
+ result.pid = p.at(&Processes::pid);
+ result.name = p.at(&Processes::name);
+ result.path = p.at(&Processes::path);
+ result.cmdline = p.at(&Processes::cmdline);
+ result.uid = p.at(&Processes::uid);
+ result.gid = p.at(&Processes::gid);
+ result.euid = p.at(&Processes::euid);
+ result.egid = p.at(&Processes::egid);
+ result.on_disk = p.at(&Processes::on_disk);
+ result.parent = p.at(&Processes::parent);
+
+ INFO(VIST_CLIENT, "[Test] Processes table:");
+ INFO(VIST_CLIENT, "\t pid: " << result.pid);
+ INFO(VIST_CLIENT, "\t name: " << result.name);
+ INFO(VIST_CLIENT, "\t path: " << result.path);
+ INFO(VIST_CLIENT, "\t cmdline: " << result.cmdline);
+ INFO(VIST_CLIENT, "\t uid: " << result.uid);
+ INFO(VIST_CLIENT, "\t gid: " << result.gid);
+ INFO(VIST_CLIENT, "\t euid: " << result.euid);
+ INFO(VIST_CLIENT, "\t egid: " << result.egid);
+ INFO(VIST_CLIENT, "\t on_disk: " << result.on_disk);
+ INFO(VIST_CLIENT, "\t parent: " << result.parent);
+ }
+}
+
+TEST(VirtualTableTests, policy_table) {
+ VirtualTable<Policy> table;
+ EXPECT_TRUE(table.size() > 0);
+
+ for(const auto& row : table) {
+ Policy policy = { row[&Policy::name], row[&Policy::value] };
+
+ INFO(VIST_CLIENT, "[Test] Policy table:");
+ INFO(VIST_CLIENT, "\t name: " << policy.name);
+ INFO(VIST_CLIENT, "\t value: " << policy.value);
+ }
+}
* See the License for the specific language governing permissions and
* limitations under the License
*/
-/*
- * @file virtual-table.cpp
- * @author Sangwan Kwon (sangwan.kwon@samsung.com)
- * @brief Implementation of virtual object
- */
-
-#include "virtual-table.h"
-#include "query.h"
-#include "schema/time.h"
-#include "schema/policy.h"
-#include "schema/processes.h"
+#include "virtual-table.hpp"
-#include <vist/common/audit/logger.h>
+#include <vist/client/query.hpp>
+#include <vist/client/schema/policy.hpp>
+#include <vist/client/schema/processes.hpp>
+#include <vist/client/schema/time.hpp>
-#include <tsqb.hxx>
+#include <vist/logger.hpp>
+#include <vist/query-builder.hpp>
#include <boost/lexical_cast.hpp>
namespace {
-using namespace tsqb;
+using namespace vist::tsqb;
using namespace vist::schema;
auto time = make_table("time",
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-#pragma once
-
-#include <map>
-#include <stdexcept>
-#include <string>
-#include <vector>
-
-namespace vist {
-
-template <typename T>
-class VirtualRow final {
-public:
- using KeyValuePair = std::map<std::string, std::string>;
-
- explicit VirtualRow();
- explicit VirtualRow(KeyValuePair&&);
-
- template<typename Struct, typename Member>
- Member at(Member Struct::*) const;
-
- template<typename Struct, typename Member>
- Member operator[](Member Struct::*) const;
-
- inline std::size_t size() const { return data.size(); }
-
-private:
- KeyValuePair data;
-};
-
-
-template <typename T>
-class VirtualTable final {
-public:
- explicit VirtualTable();
-
- /// Make iteratable
- using Iter = typename std::vector<VirtualRow<T>>::iterator;
- using CIter = typename std::vector<VirtualRow<T>>::const_iterator;
-
- inline Iter begin() { return dataset.begin(); }
- inline CIter begin() const { return dataset.cbegin(); }
- inline Iter end() { return dataset.end(); }
- inline CIter end() const { return dataset.end(); }
-
- inline std::size_t size() const { return dataset.size(); }
-
-private:
- std::vector<VirtualRow<T>> dataset;
-};
-
-} // namespace vist
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#pragma once
+
+#include <map>
+#include <stdexcept>
+#include <string>
+#include <vector>
+
+namespace vist {
+
+template <typename T>
+class VirtualRow final {
+public:
+ using KeyValuePair = std::map<std::string, std::string>;
+
+ explicit VirtualRow();
+ explicit VirtualRow(KeyValuePair&&);
+
+ template<typename Struct, typename Member>
+ Member at(Member Struct::*) const;
+
+ template<typename Struct, typename Member>
+ Member operator[](Member Struct::*) const;
+
+ inline std::size_t size() const { return data.size(); }
+
+private:
+ KeyValuePair data;
+};
+
+
+template <typename T>
+class VirtualTable final {
+public:
+ explicit VirtualTable();
+
+ /// Make iteratable
+ using Iter = typename std::vector<VirtualRow<T>>::iterator;
+ using CIter = typename std::vector<VirtualRow<T>>::const_iterator;
+
+ inline Iter begin() { return dataset.begin(); }
+ inline CIter begin() const { return dataset.cbegin(); }
+ inline Iter end() { return dataset.end(); }
+ inline CIter end() const { return dataset.end(); }
+
+ inline std::size_t size() const { return dataset.size(); }
+
+private:
+ std::vector<VirtualRow<T>> dataset;
+};
+
+} // namespace vist
+++ /dev/null
-# Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License
-
-FILE(GLOB COMMON_TESTS "tests/*.cpp")
-ADD_VIST_TEST(${COMMON_TESTS})
+++ /dev/null
-/*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-#pragma once
-
-#include <klay/audit/logger.h>
-#include <klay/audit/dlog-sink.h>
-
-#include <memory>
-
-#define VIST vist::Logger::Instance()
-#define VIST_CLIENT vist::Logger::ClientInstance()
-
-namespace vist {
-
-class Logger final {
-public:
- Logger(const Logger&) = delete;
- Logger& operator=(const Logger&) = delete;
-
- Logger(Logger&&) = default;
- Logger& operator=(Logger&&) = default;
-
- static klay::LogSink* Instance()
- {
- static Logger instance("VIST");
- return &instance.logSink;
- }
-
- static klay::LogSink* ClientInstance()
- {
- static Logger instance("VIST_CLIENT");
- return &instance.logSink;
- }
-
-private:
- explicit Logger(const std::string& tag) : logSink(tag) {}
- ~Logger() = default;
-
- klay::DlogLogSink logSink;
-};
-
-} // namespace vist
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-/*
- * @file exception.h
- * @author Sangwan Kwon (sangwan.kwon@samsung.com)
- * @brief Enum based exception handling
- * @usage
- * enum class ErrCode {
- * LogicError = 1,
- * RuntimeError
- * };
- *
- * try {
- * THROW(ErrCode::LogicError) << "Additional information about exception";
- * } catch (const vist::Exception<ErrCode>& e) {
- * ErrCode ec = e.get();
- * std::string message = e.what();
- * }
- */
-
-#pragma once
-
-#include <cstring>
-#include <exception>
-#include <sstream>
-#include <string>
-#include <type_traits>
-#include <typeinfo>
-
-#include <boost/core/demangle.hpp>
-
-#ifndef __FILENAME__
-#define __FILENAME__ \
- ::strrchr(__FILE__, '/') ? ::strrchr(__FILE__, '/') + 1 : __FILE__
-#endif
-
-#define THROW(ec) \
- throw vist::Exception<decltype(ec)>(ec,__FILENAME__,__FUNCTION__,__LINE__)
-
-namespace vist {
-
-template <typename ErrCode>
-class Exception final : public std::exception {
-public:
- static_assert(std::is_enum<ErrCode>::value, "Error code must be enum type.");
-
- using Self = Exception<ErrCode>;
-
- Exception(ErrCode ec, const char *file,
- const char *function, unsigned int line) noexcept : ec(ec)
- {
- std::stringstream ss;
- ss << "[" << file << ":" << line << " " << function << "()]"
- << "[" << boost::core::demangle(typeid(ec).name())
- << "(" << static_cast<typename std::underlying_type_t<ErrCode>>(ec) << ")]";
- message = ss.str();
- }
-
- virtual const char* what() const noexcept override
- {
- return message.c_str();
- }
-
- inline ErrCode get() const noexcept
- {
- return ec;
- }
-
- template<typename T>
- Self& operator<<(const T& arg) noexcept
- {
- message += (static_cast<std::stringstream&>(std::stringstream() << arg)).str();
- return *this;
- }
-
-private:
- ErrCode ec;
- std::string message;
-};
-
-} // namespace vist
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-#pragma once
-
-#include <string>
-#include <memory>
-
-#include <klay/rmi/client.h>
-
-namespace vist {
-namespace ipc {
-
-class Client final {
-public:
- Client(const Client&) = delete;
- Client& operator=(const Client&) = delete;
-
- Client(Client&&) = delete;
- Client& operator=(Client&&) = delete;
-
- static std::unique_ptr<klay::rmi::Client>& Instance(const std::string& sock)
- {
- static Client client(sock);
- return client.instance;
- }
-
-private:
- explicit Client(const std::string& sock) :
- instance(std::make_unique<klay::rmi::Client>(sock))
- {
- instance->connect();
- }
- ~Client() = default;
-
- std::unique_ptr<klay::rmi::Client> instance;
-};
-
-} // namespace ipc
-} // namespace vist
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-#pragma once
-
-#include <string>
-#include <memory>
-
-#include <klay/rmi/service.h>
-
-namespace vist {
-namespace ipc {
-
-class Server final {
-public:
- Server(const Server&) = delete;
- Server& operator=(const Server&) = delete;
-
- Server(Server&&) = delete;
- Server& operator=(Server&&) = delete;
-
- static std::unique_ptr<klay::rmi::Service>& Instance(const std::string& sock)
- {
- static Server server(sock);
- return server.instance;
- }
-
-private:
- explicit Server(const std::string& sock) :
- instance(std::make_unique<klay::rmi::Service>(sock)) {}
- ~Server() = default;
-
- std::unique_ptr<klay::rmi::Service> instance;
-};
-
-} // namespace ipc
-} // namespace vist
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-#include <gtest/gtest.h>
-
-#include "ipc/server.h"
-#include "ipc/client.h"
-
-#include <chrono>
-#include <thread>
-
-#include <sys/types.h>
-#include <unistd.h>
-
-using namespace vist;
-
-namespace {
- std::string g_socket = "/tmp/vist-test";
-} // anonymous namespace
-
-class TestServer {
-public:
- void init()
- {
- auto& server = ipc::Server::Instance(g_socket);
- server->expose(this, "", (std::string)(TestServer::testMethod)());
- server->start();
- }
-
- std::string testMethod()
- {
- return "test";
- }
-};
-
-class IpcTests : public testing::Test {
-public:
- void SetUp() override
- {
- ::unlink(g_socket.c_str());
-
- this->pid = fork();
- if (pid < 0) {
- return;
- } else if (pid == 0) {
- TestServer server;
- server.init();
- }
-
- std::this_thread::sleep_for(std::chrono::seconds(1));
- }
-
- void TearDown() override
- {
- if (::kill(pid, SIGTERM) == -1)
- return;
-
- std::this_thread::sleep_for(std::chrono::seconds(1));
- }
-
-private:
- pid_t pid = -1;
-};
-
-TEST_F(IpcTests, method_call) {
- auto& client = ipc::Client::Instance(g_socket);
-
- std::string ret = client->methodCall<std::string>("TestServer::testMethod");
- EXPECT_EQ(ret, "test");
-}
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-#include <gtest/gtest.h>
-
-#include "../exception.h"
-
-enum class ErrCode {
- InvalidArgument = 1,
- DominError,
- LogicError,
- RuntimeError,
- None
-};
-
-class ExceptionTests : public testing::Test {};
-
-TEST_F(ExceptionTests, exception)
-{
- bool raised = false;
- ErrCode ec = ErrCode::None;
- std::string msg;
-
- try {
- THROW(ErrCode::InvalidArgument);
- } catch (const vist::Exception<ErrCode>& e) {
- raised = true;
- ec = e.get();
- msg = e.what();
- }
-
- EXPECT_TRUE(raised);
- EXPECT_EQ(ec, ErrCode::InvalidArgument);
- EXPECT_NE(std::string::npos, msg.find("ErrCode"));
-}
-
-TEST_F(ExceptionTests, exception_msg)
-{
- bool raised = false;
- ErrCode ec = ErrCode::None;
- std::string msg;
-
- try {
- THROW(ErrCode::RuntimeError) << "Additional error message";
- } catch (const vist::Exception<ErrCode>& e) {
- raised = true;
- ec = e.get();
- msg = e.what();
- }
-
- EXPECT_TRUE(!raised) << msg;
- EXPECT_EQ(ec, ErrCode::RuntimeError);
- EXPECT_NE(std::string::npos, msg.find("Additional"));
-}
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+/*
+ * @usage
+ * enum class ErrCode {
+ * LogicError = 1,
+ * RuntimeError
+ * };
+ *
+ * try {
+ * THROW(ErrCode::LogicError) << "Additional information about exception";
+ * } catch (const vist::Exception<ErrCode>& e) {
+ * ErrCode ec = e.get();
+ * std::string message = e.what();
+ * }
+ */
+
+#pragma once
+
+#include <cstring>
+#include <exception>
+#include <sstream>
+#include <string>
+#include <type_traits>
+#include <typeinfo>
+
+#include <boost/core/demangle.hpp>
+
+#ifndef __FILENAME__
+#define __FILENAME__ \
+ ::strrchr(__FILE__, '/') ? ::strrchr(__FILE__, '/') + 1 : __FILE__
+#endif
+
+#define THROW(ec) \
+ throw vist::Exception<decltype(ec)>(ec,__FILENAME__,__FUNCTION__,__LINE__)
+
+namespace vist {
+
+template <typename ErrCode>
+class Exception final : public std::exception {
+public:
+ static_assert(std::is_enum<ErrCode>::value, "Error code must be enum type.");
+
+ using Self = Exception<ErrCode>;
+
+ Exception(ErrCode ec, const char *file,
+ const char *function, unsigned int line) noexcept : ec(ec)
+ {
+ std::stringstream ss;
+ ss << "[" << file << ":" << line << " " << function << "()]"
+ << "[" << boost::core::demangle(typeid(ec).name())
+ << "(" << static_cast<typename std::underlying_type_t<ErrCode>>(ec) << ")]";
+ message = ss.str();
+ }
+
+ virtual const char* what() const noexcept override
+ {
+ return message.c_str();
+ }
+
+ inline ErrCode get() const noexcept
+ {
+ return ec;
+ }
+
+ template<typename T>
+ Self& operator<<(const T& arg) noexcept
+ {
+ message += (static_cast<std::stringstream&>(std::stringstream() << arg)).str();
+ return *this;
+ }
+
+private:
+ ErrCode ec;
+ std::string message;
+};
+
+} // namespace vist
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#pragma once
+
+#include <string>
+#include <memory>
+
+#include <klay/rmi/client.h>
+
+namespace vist {
+namespace ipc {
+
+class Client final {
+public:
+ Client(const Client&) = delete;
+ Client& operator=(const Client&) = delete;
+
+ Client(Client&&) = delete;
+ Client& operator=(Client&&) = delete;
+
+ static std::unique_ptr<klay::rmi::Client>& Instance(const std::string& sock)
+ {
+ static Client client(sock);
+ return client.instance;
+ }
+
+private:
+ explicit Client(const std::string& sock) :
+ instance(std::make_unique<klay::rmi::Client>(sock))
+ {
+ instance->connect();
+ }
+ ~Client() = default;
+
+ std::unique_ptr<klay::rmi::Client> instance;
+};
+
+} // namespace ipc
+} // namespace vist
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#pragma once
+
+#include <string>
+#include <memory>
+
+#include <klay/rmi/service.h>
+
+namespace vist {
+namespace ipc {
+
+class Server final {
+public:
+ Server(const Server&) = delete;
+ Server& operator=(const Server&) = delete;
+
+ Server(Server&&) = delete;
+ Server& operator=(Server&&) = delete;
+
+ static std::unique_ptr<klay::rmi::Service>& Instance(const std::string& sock)
+ {
+ static Server server(sock);
+ return server.instance;
+ }
+
+private:
+ explicit Server(const std::string& sock) :
+ instance(std::make_unique<klay::rmi::Service>(sock)) {}
+ ~Server() = default;
+
+ std::unique_ptr<klay::rmi::Service> instance;
+};
+
+} // namespace ipc
+} // namespace vist
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#include <gtest/gtest.h>
+
+#include <vist/ipc/client.hpp>
+#include <vist/ipc/server.hpp>
+
+#include <chrono>
+#include <thread>
+
+#include <sys/types.h>
+#include <unistd.h>
+
+using namespace vist;
+
+namespace {
+ std::string g_socket = "/tmp/vist-test";
+} // anonymous namespace
+
+class TestServer {
+public:
+ void init()
+ {
+ auto& server = ipc::Server::Instance(g_socket);
+ server->expose(this, "", (std::string)(TestServer::testMethod)());
+ server->start();
+ }
+
+ std::string testMethod()
+ {
+ return "test";
+ }
+};
+
+class IpcTests : public testing::Test {
+public:
+ void SetUp() override
+ {
+ ::unlink(g_socket.c_str());
+
+ this->pid = fork();
+ if (pid < 0) {
+ return;
+ } else if (pid == 0) {
+ TestServer server;
+ server.init();
+ }
+
+ std::this_thread::sleep_for(std::chrono::seconds(1));
+ }
+
+ void TearDown() override
+ {
+ if (::kill(pid, SIGTERM) == -1)
+ return;
+
+ std::this_thread::sleep_for(std::chrono::seconds(1));
+ }
+
+private:
+ pid_t pid = -1;
+};
+
+TEST_F(IpcTests, method_call) {
+ auto& client = ipc::Client::Instance(g_socket);
+
+ std::string ret = client->methodCall<std::string>("TestServer::testMethod");
+ EXPECT_EQ(ret, "test");
+}
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#pragma once
+
+#include <klay/audit/dlog-sink.h>
+#include <klay/audit/logger.h>
+
+#include <memory>
+
+#define VIST vist::Logger::Instance()
+#define VIST_CLIENT vist::Logger::ClientInstance()
+
+namespace vist {
+
+class Logger final {
+public:
+ Logger(const Logger&) = delete;
+ Logger& operator=(const Logger&) = delete;
+
+ Logger(Logger&&) = default;
+ Logger& operator=(Logger&&) = default;
+
+ static klay::LogSink* Instance()
+ {
+ static Logger instance("VIST");
+ return &instance.logSink;
+ }
+
+ static klay::LogSink* ClientInstance()
+ {
+ static Logger instance("VIST_CLIENT");
+ return &instance.logSink;
+ }
+
+private:
+ explicit Logger(const std::string& tag) : logSink(tag) {}
+ ~Logger() = default;
+
+ klay::DlogLogSink logSink;
+};
+
+} // namespace vist
* limitations under the License
*/
-#include "service/vist.h"
+#include <vist/service/vist.hpp>
#include <cstdlib>
#include <stdexcept>
using namespace vist;
-int main() try
-{
+int main() try {
Vist::Instance().start();
return EXIT_SUCCESS;
-} catch(const std::exception&)
-{
+} catch(const std::exception&) {
return EXIT_FAILURE;
}
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
#include <gtest/gtest.h>
int main(int argc, char* argv[]) {
- testing::InitGoogleTest(&argc, argv);
- return RUN_ALL_TESTS();
+ testing::InitGoogleTest(&argc, argv);
+ return RUN_ALL_TESTS();
}
* See the License for the specific language governing permissions and
* limitations under the License
*/
-/*
- * @file notification.cpp
- * @author Sangwan Kwon (sangwan.kwon@samsung.com)
- * @brief Implementation of notification
- */
-#include "notification.h"
+#include "notification.hpp"
#include <mutex>
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-
-/**
- * @file notification.h
- * @brief Notify to registered stuffs when event-callback called
- */
-
-
-#pragma once
-
-#include <map>
-#include <vector>
-
-#include <osquery/status.h>
-
-namespace vist {
-
-using Row = std::map<std::string, std::string>;
-using Rows = std::vector<Row>;
-
-using Callback = std::function<void(const Row&)>;
-
-using NotifyCallback = Callback;
-
-class Notification final {
-public:
- static Notification& instance();
-
- Notification(const Notification&) = delete;
- Notification& operator=(const Notification&) = delete;
-
- osquery::Status add(const std::string& table, const NotifyCallback& callback);
- osquery::Status emit(const std::string& table, const Row& result) const;
-
-private:
- Notification() = default;
- ~Notification() = default;
-
- std::multimap<std::string, NotifyCallback> callbacks;
-};
-
-} // namespace vist
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#pragma once
+
+#include <map>
+#include <vector>
+
+#include <osquery/status.h>
+
+namespace vist {
+
+using Row = std::map<std::string, std::string>;
+using Rows = std::vector<Row>;
+
+using Callback = std::function<void(const Row&)>;
+
+using NotifyCallback = Callback;
+
+class Notification final {
+public:
+ static Notification& instance();
+
+ Notification(const Notification&) = delete;
+ Notification& operator=(const Notification&) = delete;
+
+ osquery::Status add(const std::string& table, const NotifyCallback& callback);
+ osquery::Status emit(const std::string& table, const Row& result) const;
+
+private:
+ Notification() = default;
+ ~Notification() = default;
+
+ std::multimap<std::string, NotifyCallback> callbacks;
+};
+
+} // namespace vist
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-#include <gtest/gtest.h>
-
-#include "../notification.h"
-#include <osquery/logger.h>
-
-using namespace vist;
-
-class NotificationTests : public testing::Test {};
-
-TEST_F(NotificationTests, test_add_positive) {
- auto& notifier = Notification::instance();
-
- auto callback = [](const Row& row) {
- LOG(INFO) << "NotifyCallback called:";
- for (const auto& r : row)
- LOG(INFO) << "\t" << r.first << " : " << r.second;
- };
-
- auto s = notifier.add("test", std::move(callback));
- EXPECT_TRUE(s.ok());
-}
-
-TEST_F(NotificationTests, test_add_negative) {
- auto& notifier = Notification::instance();
-
- auto callback = [](const Row& row) {
- LOG(INFO) << "NotifyCallback called:";
- for (const auto& r : row)
- LOG(INFO) << "\t" << r.first << " : " << r.second;
- };
-
- auto s = notifier.add("", std::move(callback));
- EXPECT_FALSE(s.ok());
-}
-
-TEST_F(NotificationTests, test_emit_positive) {
- auto& notifier = Notification::instance();
-
- int called = 0;
- auto callback = [&](const Row& row) {
- LOG(INFO) << "NotifyCallback called:";
- for (const auto& r : row)
- LOG(INFO) << "\t" << r.first << " : " << r.second;
- called++;
- };
-
- auto s = notifier.add("test2", std::move(callback));
- EXPECT_TRUE(s.ok());
-
- Row row;
- row["foo"] = "bar";
- s = notifier.emit("test2", row);
-
- EXPECT_TRUE(s.ok());
- EXPECT_EQ(called, 1);
-}
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#include <gtest/gtest.h>
+
+#include <vist/notification/notification.hpp>
+
+#include <osquery/logger.h>
+
+using namespace vist;
+
+class NotificationTests : public testing::Test {};
+
+TEST_F(NotificationTests, test_add_positive)
+{
+ auto& notifier = Notification::instance();
+
+ auto callback = [](const Row& row) {
+ LOG(INFO) << "NotifyCallback called:";
+ for (const auto& r : row)
+ LOG(INFO) << "\t" << r.first << " : " << r.second;
+ };
+
+ auto s = notifier.add("test", std::move(callback));
+ EXPECT_TRUE(s.ok());
+}
+
+TEST_F(NotificationTests, test_add_negative)
+{
+ auto& notifier = Notification::instance();
+
+ auto callback = [](const Row& row) {
+ LOG(INFO) << "NotifyCallback called:";
+ for (const auto& r : row)
+ LOG(INFO) << "\t" << r.first << " : " << r.second;
+ };
+
+ auto s = notifier.add("", std::move(callback));
+ EXPECT_FALSE(s.ok());
+}
+
+TEST_F(NotificationTests, test_emit_positive)
+{
+ auto& notifier = Notification::instance();
+
+ int called = 0;
+ auto callback = [&](const Row& row) {
+ LOG(INFO) << "NotifyCallback called:";
+ for (const auto& r : row)
+ LOG(INFO) << "\t" << r.first << " : " << r.second;
+ called++;
+ };
+
+ auto s = notifier.add("test2", std::move(callback));
+ EXPECT_TRUE(s.ok());
+
+ Row row;
+ row["foo"] = "bar";
+ s = notifier.emit("test2", row);
+
+ EXPECT_TRUE(s.ok());
+ EXPECT_EQ(called, 1);
+}
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-#pragma once
-
-#include <vist/policy/sdk/policy-value.h>
-
-#include <string>
-#include <unordered_map>
-#include <vector>
-
-namespace vist {
-namespace policy {
-
-struct API {
- static PolicyValue Get(const std::string& policy);
- static std::unordered_map<std::string, PolicyValue> GetAll();
-
- struct Admin {
- static void Set(const std::string& policy, const PolicyValue& value);
-
- static void Enroll(const std::string& admin);
- static void Disenroll(const std::string& admin);
-
- static std::vector<std::string> GetAll();
- };
-};
-
-} // namespace policy
-} // namespace vist
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#pragma once
+
+#include <vist/policy/sdk/policy-value.hpp>
+
+#include <string>
+#include <unordered_map>
+#include <vector>
+
+namespace vist {
+namespace policy {
+
+struct API {
+ static PolicyValue Get(const std::string& policy);
+ static std::unordered_map<std::string, PolicyValue> GetAll();
+
+ struct Admin {
+ static void Set(const std::string& policy, const PolicyValue& value);
+
+ static void Enroll(const std::string& admin);
+ static void Disenroll(const std::string& admin);
+
+ static std::vector<std::string> GetAll();
+ };
+};
+
+} // namespace policy
+} // namespace vist
* limitations under the License
*/
-#include <vist/policy/api.h>
+#include <vist/policy/api.hpp>
-#include "policy-manager.h"
+#include "policy-manager.hpp"
namespace vist {
namespace policy {
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-#pragma once
-
-#include <string>
-
-namespace vist {
-namespace policy {
-namespace schema {
-
-struct Admin {
- std::string name;
-};
-
-struct PolicyActivated {
- std::string admin;
- std::string policy;
- int value = -1;
-};
-
-struct PolicyDefinition {
- std::string name;
- int ivalue = -1;
-};
-
-} // namespace schema
-} // namespace policy
-} // namespace vist
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#pragma once
+
+#include <string>
+
+namespace vist {
+namespace policy {
+namespace schema {
+
+struct Admin {
+ std::string name;
+};
+
+struct PolicyActivated {
+ std::string admin;
+ std::string policy;
+ int value = -1;
+};
+
+struct PolicyDefinition {
+ std::string name;
+ int ivalue = -1;
+};
+
+} // namespace schema
+} // namespace policy
+} // namespace vist
* limitations under the License
*/
-#include "policy-loader.h"
+#include "policy-loader.hpp"
namespace vist {
namespace policy {
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-#pragma once
-
-#include <vist/policy/sdk/policy-provider.h>
-
-#include <stdexcept>
-#include <string>
-#include <memory>
-
-#include <dlfcn.h>
-
-namespace vist {
-namespace policy {
-
-struct PolicyLoader final {
- static PolicyProvider* load(const std::string& path);
-};
-
-class PluginLoader final {
-public:
- explicit PluginLoader(const std::string& path, int flag = RTLD_LAZY);
-
- template<typename T>
- void load(const std::string& name, T& symbol);
-
-private:
- using Handle = std::unique_ptr<void, int(*)(void*)>;
- Handle handle;
-};
-
-template<typename T>
-void PluginLoader::load(const std::string& name, T& symbol)
-{
- symbol = reinterpret_cast<T>(::dlsym(handle.get(), name.c_str()));
- if (symbol == nullptr)
- throw std::runtime_error("Failed to load: " + name);
-}
-
-} // namespace policy
-} // namespace vist
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#pragma once
+
+#include <vist/policy/sdk/policy-provider.hpp>
+
+#include <memory>
+#include <stdexcept>
+#include <string>
+
+#include <dlfcn.h>
+
+namespace vist {
+namespace policy {
+
+struct PolicyLoader final {
+ static PolicyProvider* load(const std::string& path);
+};
+
+class PluginLoader final {
+public:
+ explicit PluginLoader(const std::string& path, int flag = RTLD_LAZY);
+
+ template<typename T>
+ void load(const std::string& name, T& symbol);
+
+private:
+ using Handle = std::unique_ptr<void, int(*)(void*)>;
+ Handle handle;
+};
+
+template<typename T>
+void PluginLoader::load(const std::string& name, T& symbol)
+{
+ symbol = reinterpret_cast<T>(::dlsym(handle.get(), name.c_str()));
+ if (symbol == nullptr)
+ throw std::runtime_error("Failed to load: " + name);
+}
+
+} // namespace policy
+} // namespace vist
* limitations under the License
*/
-#include "policy-manager.h"
-#include "policy-loader.h"
+#include "policy-manager.hpp"
+#include "policy-loader.hpp"
-#include <vist/common/audit/logger.h>
+#include <vist/logger.hpp>
#include <klay/filesystem.h>
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-#pragma once
-
-#include <vist/policy/sdk/policy-provider.h>
-#include <vist/policy/sdk/policy-value.h>
-
-#include "policy-storage.h"
-
-#include <exception>
-#include <memory>
-#include <string>
-#include <unordered_map>
-#include <vector>
-
-#include <gtest/gtest_prod.h>
-
-namespace vist {
-namespace policy {
-
-class PolicyManager final {
-public:
- PolicyManager(const PolicyManager&) = delete;
- PolicyManager& operator=(const PolicyManager&) = delete;
-
- PolicyManager(PolicyManager&&) = delete;
- PolicyManager& operator=(PolicyManager&&) = delete;
-
- static PolicyManager& Instance()
- {
- static PolicyManager manager;
- return manager;
- }
-
- void enroll(const std::string& admin);
- void disenroll(const std::string& admin);
-
- void set(const std::string& policy,
- const PolicyValue& value,
- const std::string& admin);
- PolicyValue get(const std::string& policy);
- std::unordered_map<std::string, PolicyValue> getAll();
-
- std::vector<std::string> getAdmins();
-
-private:
- explicit PolicyManager();
- ~PolicyManager() = default;
-
- std::pair<int, int> loadProviders(const std::string& path);
- int loadPolicies();
-
- PolicyStorage storage;
- std::vector<std::unique_ptr<PolicyProvider>> providers;
-
- /// Policy-Provider
- std::unordered_map<std::string, std::string> policies;
-
- FRIEND_TEST(PolicyCoreTests, policy_loader);
-};
-
-} // namespace policy
-} // namespace vist
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#pragma once
+
+#include <vist/policy/sdk/policy-provider.hpp>
+#include <vist/policy/sdk/policy-value.hpp>
+
+#include "policy-storage.hpp"
+
+#include <exception>
+#include <memory>
+#include <string>
+#include <unordered_map>
+#include <vector>
+
+#include <gtest/gtest_prod.h>
+
+namespace vist {
+namespace policy {
+
+class PolicyManager final {
+public:
+ PolicyManager(const PolicyManager&) = delete;
+ PolicyManager& operator=(const PolicyManager&) = delete;
+
+ PolicyManager(PolicyManager&&) = delete;
+ PolicyManager& operator=(PolicyManager&&) = delete;
+
+ static PolicyManager& Instance()
+ {
+ static PolicyManager manager;
+ return manager;
+ }
+
+ void enroll(const std::string& admin);
+ void disenroll(const std::string& admin);
+
+ void set(const std::string& policy,
+ const PolicyValue& value,
+ const std::string& admin);
+ PolicyValue get(const std::string& policy);
+ std::unordered_map<std::string, PolicyValue> getAll();
+
+ std::vector<std::string> getAdmins();
+
+private:
+ explicit PolicyManager();
+ ~PolicyManager() = default;
+
+ std::pair<int, int> loadProviders(const std::string& path);
+ int loadPolicies();
+
+ PolicyStorage storage;
+ std::vector<std::unique_ptr<PolicyProvider>> providers;
+
+ /// Policy-Provider
+ std::unordered_map<std::string, std::string> policies;
+
+ FRIEND_TEST(PolicyCoreTests, policy_loader);
+};
+
+} // namespace policy
+} // namespace vist
* limitations under the License
*/
-#include "policy-storage.h"
+#include "policy-storage.hpp"
-#include <vist/common/audit/logger.h>
+#include <vist/logger.hpp>
+#include <vist/query-builder.hpp>
#include <klay/db/column.h>
-#include <klay/db/query-builder.h>
#include <klay/db/statement.h>
#include <klay/exception.h>
-#include <fstream>
#include <algorithm>
+#include <fstream>
-using namespace query_builder;
+using namespace vist::tsqb;
using namespace vist::policy::schema;
namespace {
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * ttp://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-#pragma once
-
-#include <vist/policy/sdk/policy-value.h>
-
-#include "db-schema.h"
-
-#include <memory>
-#include <unordered_map>
-#include <vector>
-
-#include <klay/db/connection.h>
-
-namespace vist {
-namespace policy {
-
-using namespace schema;
-
-class PolicyStorage final {
-public:
- explicit PolicyStorage(const std::string& path);
-
- /// TODO(Sangwan): Consider to support lazy sync
- void sync();
-
- void syncAdmin();
- void syncPolicyActivated();
- void syncPolicyDefinition();
-
- inline bool exists(const std::string& policy) const noexcept
- {
- return definitions.find(policy) != definitions.end();
- }
-
- inline bool isActivated() const noexcept
- {
- return admins.size() > 0 && activatedPolicies.size() > 0;
- }
-
- void enroll(const std::string& admin);
- void disenroll(const std::string& admin);
-
- void define(const std::string& policy, int ivalue);
- void update(const std::string& admin,
- const std::string& policy,
- const PolicyValue& value);
-
- PolicyValue strictest(const std::string& policy);
- /// Return all strictest policy values
- std::unordered_map<std::string, PolicyValue> strictest();
-
- const std::vector<std::string>& getAdmins() const noexcept;
-
-private:
- std::string getScript(const std::string& name);
-
- std::shared_ptr<klay::database::Connection> database;
-
- /// DB Cache objects
- /// TODO(Sangwan): add locking mechanism
- std::vector<std::string> admins;
- std::unordered_map<std::string, PolicyActivated> activatedPolicies;
- std::unordered_map<std::string, PolicyDefinition> definitions;
-};
-
-} // namespace policy
-} // namespace vist
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * ttp://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#pragma once
+
+#include <vist/policy/sdk/policy-value.hpp>
+
+#include "db-schema.hpp"
+
+#include <memory>
+#include <unordered_map>
+#include <vector>
+
+#include <klay/db/connection.h>
+
+namespace vist {
+namespace policy {
+
+using namespace schema;
+
+class PolicyStorage final {
+public:
+ explicit PolicyStorage(const std::string& path);
+
+ /// TODO(Sangwan): Consider to support lazy sync
+ void sync();
+
+ void syncAdmin();
+ void syncPolicyActivated();
+ void syncPolicyDefinition();
+
+ inline bool exists(const std::string& policy) const noexcept
+ {
+ return definitions.find(policy) != definitions.end();
+ }
+
+ inline bool isActivated() const noexcept
+ {
+ return admins.size() > 0 && activatedPolicies.size() > 0;
+ }
+
+ void enroll(const std::string& admin);
+ void disenroll(const std::string& admin);
+
+ void define(const std::string& policy, int ivalue);
+ void update(const std::string& admin,
+ const std::string& policy,
+ const PolicyValue& value);
+
+ PolicyValue strictest(const std::string& policy);
+ /// Return all strictest policy values
+ std::unordered_map<std::string, PolicyValue> strictest();
+
+ const std::vector<std::string>& getAdmins() const noexcept;
+
+private:
+ std::string getScript(const std::string& name);
+
+ std::shared_ptr<klay::database::Connection> database;
+
+ /// DB Cache objects
+ /// TODO(Sangwan): add locking mechanism
+ std::vector<std::string> admins;
+ std::unordered_map<std::string, PolicyActivated> activatedPolicies;
+ std::unordered_map<std::string, PolicyDefinition> definitions;
+};
+
+} // namespace policy
+} // namespace vist
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-#include <gtest/gtest.h>
-
-#include "../policy-manager.h"
-
-namespace vist {
-namespace policy {
-
-class PolicyCoreTests : public testing::Test {};
-
-TEST_F(PolicyCoreTests, policy_loader) {
- auto& manager = PolicyManager::Instance();
-
- EXPECT_TRUE(manager.providers.size() > 0);
- EXPECT_TRUE(manager.policies.size() > 0);
-}
-
-TEST_F(PolicyCoreTests, policy_set_get) {
- auto& manager = PolicyManager::Instance();
- manager.enroll("testAdmin");
- manager.set("bluetooth", PolicyValue(5), "testAdmin");
-
- auto policy = manager.get("bluetooth");
- EXPECT_EQ(policy.value, 5);
-
- manager.enroll("testAdmin1");
- manager.set("bluetooth", PolicyValue(10), "testAdmin1");
-
- /// Manager should return the strongest policy.
- policy = manager.get("bluetooth");
- EXPECT_EQ(policy.value, 5);
-
- manager.disenroll("testAdmin");
- manager.disenroll("testAdmin1");
-}
-
-} // namespace policy
-} // namespace vist
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#include <gtest/gtest.h>
+
+#include <vist/policy/core/policy-manager.hpp>
+
+namespace vist {
+namespace policy {
+
+class PolicyCoreTests : public testing::Test {};
+
+TEST_F(PolicyCoreTests, policy_loader) {
+ auto& manager = PolicyManager::Instance();
+
+ EXPECT_TRUE(manager.providers.size() > 0);
+ EXPECT_TRUE(manager.policies.size() > 0);
+}
+
+TEST_F(PolicyCoreTests, policy_set_get) {
+ auto& manager = PolicyManager::Instance();
+ manager.enroll("testAdmin");
+ manager.set("bluetooth", PolicyValue(5), "testAdmin");
+
+ auto policy = manager.get("bluetooth");
+ EXPECT_EQ(policy.value, 5);
+
+ manager.enroll("testAdmin1");
+ manager.set("bluetooth", PolicyValue(10), "testAdmin1");
+
+ /// Manager should return the strongest policy.
+ policy = manager.get("bluetooth");
+ EXPECT_EQ(policy.value, 5);
+
+ manager.disenroll("testAdmin");
+ manager.disenroll("testAdmin1");
+}
+
+} // namespace policy
+} // namespace vist
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-#include <gtest/gtest.h>
-
-#include <memory>
-
-#include "../policy-storage.h"
-
-using namespace vist::policy;
-
-class PolicyStorageTests : public testing::Test {
-public:
- void SetUp() override
- {
- this->storage = std::make_shared<PolicyStorage>(DB_PATH);
- }
-
- std::shared_ptr<PolicyStorage> getStorage()
- {
- return this->storage;
- }
-
-private:
- std::shared_ptr<PolicyStorage> storage = nullptr;
-};
-
-TEST_F(PolicyStorageTests, initialize)
-{
- bool isRaised = false;
-
- try {
- // DB is maden at run-time
- PolicyStorage storage("/tmp/dummy");
- } catch (const std::exception&) {
- isRaised = true;
- }
-
- EXPECT_FALSE(isRaised);
-}
-
-TEST_F(PolicyStorageTests, enrollment)
-{
- auto storage = getStorage();
- EXPECT_FALSE(storage->isActivated());
-
- storage->enroll("testAdmin0");
- storage->enroll("testAdmin1");
- EXPECT_TRUE(storage->isActivated());
-
- storage->disenroll("testAdmin0");
- EXPECT_TRUE(storage->isActivated());
-
- storage->disenroll("testAdmin1");
- EXPECT_FALSE(storage->isActivated());
-}
-
-TEST_F(PolicyStorageTests, update)
-{
- auto storage = getStorage();
- storage->enroll("testAdmin");
-
- bool isRaised = false;
- try {
- storage->update("fakeAdmin", "bluetooth", PolicyValue(0));
- } catch (const std::exception&) {
- isRaised = true;
- }
-
- isRaised = false;
- try {
- storage->update("testAdmin", "bluetooth", PolicyValue(0));
- } catch (const std::exception&) {
- isRaised = true;
- }
- EXPECT_FALSE(isRaised);
-
- isRaised = false;
- try {
- storage->update("testAdmin", "FakePolicy", PolicyValue(0));
- } catch (const std::exception&) {
- isRaised = true;
- }
- EXPECT_TRUE(isRaised);
-
- storage->disenroll("testAdmin");
-}
-
-TEST_F(PolicyStorageTests, strictest)
-{
- auto storage = getStorage();
- storage->enroll("testAdmin0");
- storage->enroll("testAdmin1");
-
- storage->update("testAdmin0", "bluetooth", PolicyValue(3));
- storage->update("testAdmin1", "bluetooth", PolicyValue(6));
-
- bool isRaised = false;
- try {
- auto value = storage->strictest("FakePolicy");
- } catch (const std::exception&) {
- isRaised = true;
- }
- EXPECT_TRUE(isRaised);
-
- auto policy = storage->strictest("bluetooth");
- EXPECT_EQ(policy.value, 3);
-
- storage->disenroll("testAdmin0");
- storage->disenroll("testAdmin1");
-}
-
-TEST_F(PolicyStorageTests, strictest_all)
-{
- auto storage = getStorage();
- storage->enroll("testAdmin");
-
- auto policies = storage->strictest();
- EXPECT_TRUE(policies.size() > 0);
-
- storage->disenroll("testAdmin");
-}
-
-TEST_F(PolicyStorageTests, admin_list)
-{
- auto storage = getStorage();
-
- auto admins = storage->getAdmins();
- EXPECT_EQ(admins.size(), 0);
-
- storage->enroll("testAdmin1");
- admins = storage->getAdmins();
- EXPECT_EQ(admins.size(), 1);
-
- storage->enroll("testAdmin2");
- admins = storage->getAdmins();
- EXPECT_EQ(admins.size(), 2);
-
- storage->disenroll("testAdmin2");
- admins = storage->getAdmins();
- EXPECT_EQ(admins.size(), 1);
-
- storage->disenroll("testAdmin1");
- admins = storage->getAdmins();
- EXPECT_EQ(admins.size(), 0);
-}
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#include <gtest/gtest.h>
+
+#include <memory>
+
+#include <vist/policy/core/policy-storage.hpp>
+
+using namespace vist::policy;
+
+class PolicyStorageTests : public testing::Test {
+public:
+ void SetUp() override
+ {
+ this->storage = std::make_shared<PolicyStorage>(DB_PATH);
+ }
+
+ std::shared_ptr<PolicyStorage> getStorage()
+ {
+ return this->storage;
+ }
+
+private:
+ std::shared_ptr<PolicyStorage> storage = nullptr;
+};
+
+TEST_F(PolicyStorageTests, initialize)
+{
+ bool isRaised = false;
+
+ try {
+ // DB is maden at run-time
+ PolicyStorage storage("/tmp/dummy");
+ } catch (const std::exception&) {
+ isRaised = true;
+ }
+
+ EXPECT_FALSE(isRaised);
+}
+
+TEST_F(PolicyStorageTests, enrollment)
+{
+ auto storage = getStorage();
+ EXPECT_FALSE(storage->isActivated());
+
+ storage->enroll("testAdmin0");
+ storage->enroll("testAdmin1");
+ EXPECT_TRUE(storage->isActivated());
+
+ storage->disenroll("testAdmin0");
+ EXPECT_TRUE(storage->isActivated());
+
+ storage->disenroll("testAdmin1");
+ EXPECT_FALSE(storage->isActivated());
+}
+
+TEST_F(PolicyStorageTests, update)
+{
+ auto storage = getStorage();
+ storage->enroll("testAdmin");
+
+ bool isRaised = false;
+ try {
+ storage->update("fakeAdmin", "bluetooth", PolicyValue(0));
+ } catch (const std::exception&) {
+ isRaised = true;
+ }
+
+ isRaised = false;
+ try {
+ storage->update("testAdmin", "bluetooth", PolicyValue(0));
+ } catch (const std::exception&) {
+ isRaised = true;
+ }
+ EXPECT_FALSE(isRaised);
+
+ isRaised = false;
+ try {
+ storage->update("testAdmin", "FakePolicy", PolicyValue(0));
+ } catch (const std::exception&) {
+ isRaised = true;
+ }
+ EXPECT_TRUE(isRaised);
+
+ storage->disenroll("testAdmin");
+}
+
+TEST_F(PolicyStorageTests, strictest)
+{
+ auto storage = getStorage();
+ storage->enroll("testAdmin0");
+ storage->enroll("testAdmin1");
+
+ storage->update("testAdmin0", "bluetooth", PolicyValue(3));
+ storage->update("testAdmin1", "bluetooth", PolicyValue(6));
+
+ bool isRaised = false;
+ try {
+ auto value = storage->strictest("FakePolicy");
+ } catch (const std::exception&) {
+ isRaised = true;
+ }
+ EXPECT_TRUE(isRaised);
+
+ auto policy = storage->strictest("bluetooth");
+ EXPECT_EQ(policy.value, 3);
+
+ storage->disenroll("testAdmin0");
+ storage->disenroll("testAdmin1");
+}
+
+TEST_F(PolicyStorageTests, strictest_all)
+{
+ auto storage = getStorage();
+ storage->enroll("testAdmin");
+
+ auto policies = storage->strictest();
+ EXPECT_TRUE(policies.size() > 0);
+
+ storage->disenroll("testAdmin");
+}
+
+TEST_F(PolicyStorageTests, admin_list)
+{
+ auto storage = getStorage();
+
+ auto admins = storage->getAdmins();
+ EXPECT_EQ(admins.size(), 0);
+
+ storage->enroll("testAdmin1");
+ admins = storage->getAdmins();
+ EXPECT_EQ(admins.size(), 1);
+
+ storage->enroll("testAdmin2");
+ admins = storage->getAdmins();
+ EXPECT_EQ(admins.size(), 2);
+
+ storage->disenroll("testAdmin2");
+ admins = storage->getAdmins();
+ EXPECT_EQ(admins.size(), 1);
+
+ storage->disenroll("testAdmin1");
+ admins = storage->getAdmins();
+ EXPECT_EQ(admins.size(), 0);
+}
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-#pragma once
-
-#include <vist/policy/sdk/policy-value.h>
-
-#include <string>
-#include <stdexcept>
-
-namespace vist {
-namespace policy {
-
-class PolicyModel {
-public:
- explicit PolicyModel(std::string name, PolicyValue initial) noexcept :
- name(std::move(name)), initial(std::move(initial)) {}
- virtual ~PolicyModel() = default;
-
- PolicyModel(const PolicyModel&) = delete;
- PolicyModel& operator=(const PolicyModel&) = delete;
-
- PolicyModel(PolicyModel&&) = default;
- PolicyModel& operator=(PolicyModel&&) = default;
-
- inline void set(const PolicyValue& value)
- {
- auto rollback = current;
- current = value;
- ready = true;
-
- try {
- this->onChanged(value);
- } catch (const std::exception& e) {
- current = rollback;
- ready = false;
- std::rethrow_exception(std::current_exception());
- }
- }
-
- inline const PolicyValue& get() const
- {
- if (!ready)
- throw std::runtime_error("Policy value should be set once before use.");
-
- return current;
- }
-
- virtual void onChanged(const PolicyValue& value) = 0;
-
- const std::string& getName() const noexcept { return name; }
- const PolicyValue& getInitial() const noexcept { return initial; }
-
-protected:
- std::string name;
- PolicyValue initial;
- PolicyValue current;
- bool ready = false;
-};
-
-} // namespace policy
-} // namespace vist
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#pragma once
+
+#include <vist/policy/sdk/policy-value.hpp>
+
+#include <string>
+#include <stdexcept>
+
+namespace vist {
+namespace policy {
+
+class PolicyModel {
+public:
+ explicit PolicyModel(std::string name, PolicyValue initial) noexcept :
+ name(std::move(name)), initial(std::move(initial)) {}
+ virtual ~PolicyModel() = default;
+
+ PolicyModel(const PolicyModel&) = delete;
+ PolicyModel& operator=(const PolicyModel&) = delete;
+
+ PolicyModel(PolicyModel&&) = default;
+ PolicyModel& operator=(PolicyModel&&) = default;
+
+ inline void set(const PolicyValue& value)
+ {
+ auto rollback = current;
+ current = value;
+ ready = true;
+
+ try {
+ this->onChanged(value);
+ } catch (const std::exception& e) {
+ current = rollback;
+ ready = false;
+ std::rethrow_exception(std::current_exception());
+ }
+ }
+
+ inline const PolicyValue& get() const
+ {
+ if (!ready)
+ throw std::runtime_error("Policy value should be set once before use.");
+
+ return current;
+ }
+
+ virtual void onChanged(const PolicyValue& value) = 0;
+
+ const std::string& getName() const noexcept { return name; }
+ const PolicyValue& getInitial() const noexcept { return initial; }
+
+protected:
+ std::string name;
+ PolicyValue initial;
+ PolicyValue current;
+ bool ready = false;
+};
+
+} // namespace policy
+} // namespace vist
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-#pragma once
-
-#include <vist/policy/sdk/policy-model.h>
-
-#include <cstddef>
-#include <memory>
-#include <unordered_map>
-
-namespace vist {
-namespace policy {
-
-class PolicyProvider {
-public:
- using FactoryType = PolicyProvider* (*)();
-
- explicit PolicyProvider(std::string name) noexcept : name(std::move(name)) {}
- virtual ~PolicyProvider() = default;
-
- inline void add(const std::shared_ptr<PolicyModel>& policy)
- {
- policies[policy->getName()] = policy;
- }
-
- inline const std::string& getName() const noexcept { return name; }
- static const std::string& getFactoryName() noexcept
- {
- static std::string name = "PolicyFactory";
- return name;
- }
-
- inline std::size_t size() const noexcept { return policies.size(); }
-
-private:
- std::string name;
- std::unordered_map<std::string, std::shared_ptr<PolicyModel>> policies;
-
- friend class PolicyManager;
-};
-
-} // namespace policy
-} // namespace vist
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#pragma once
+
+#include <vist/policy/sdk/policy-model.hpp>
+
+#include <cstddef>
+#include <memory>
+#include <unordered_map>
+
+namespace vist {
+namespace policy {
+
+class PolicyProvider {
+public:
+ using FactoryType = PolicyProvider* (*)();
+
+ explicit PolicyProvider(std::string name) noexcept : name(std::move(name)) {}
+ virtual ~PolicyProvider() = default;
+
+ inline void add(const std::shared_ptr<PolicyModel>& policy)
+ {
+ policies[policy->getName()] = policy;
+ }
+
+ inline const std::string& getName() const noexcept { return name; }
+ static const std::string& getFactoryName() noexcept
+ {
+ static std::string name = "PolicyFactory";
+ return name;
+ }
+
+ inline std::size_t size() const noexcept { return policies.size(); }
+
+private:
+ std::string name;
+ std::unordered_map<std::string, std::shared_ptr<PolicyModel>> policies;
+
+ friend class PolicyManager;
+};
+
+} // namespace policy
+} // namespace vist
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-#pragma once
-
-namespace vist {
-namespace policy {
-
-// TODO: Support various value type
-struct PolicyValue final {
- explicit PolicyValue(int value) noexcept : value(value) {}
- explicit PolicyValue() noexcept = default;
- ~PolicyValue() = default;
-
- PolicyValue(const PolicyValue&) noexcept = default;
- PolicyValue& operator=(const PolicyValue&) noexcept = default;
-
- PolicyValue(PolicyValue&&) noexcept = default;
- PolicyValue& operator=(PolicyValue&&) noexcept = default;
-
- PolicyValue& operator=(int val) {
- value = val;
- return *this;
- }
-
- operator int() const { return value; }
- bool operator==(const PolicyValue& rhs) const { return value == rhs.value; }
- bool operator!=(const PolicyValue& rhs) const { return value != rhs.value; }
- bool operator<(const PolicyValue& rhs) const { return value < rhs.value; }
-
- int value = -1;
-};
-
-} // namespace policy
-} // namespace vist
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#pragma once
+
+namespace vist {
+namespace policy {
+
+// TODO: Support various value type
+struct PolicyValue final {
+ explicit PolicyValue(int value) noexcept : value(value) {}
+ explicit PolicyValue() noexcept = default;
+ ~PolicyValue() = default;
+
+ PolicyValue(const PolicyValue&) noexcept = default;
+ PolicyValue& operator=(const PolicyValue&) noexcept = default;
+
+ PolicyValue(PolicyValue&&) noexcept = default;
+ PolicyValue& operator=(PolicyValue&&) noexcept = default;
+
+ PolicyValue& operator=(int val) {
+ value = val;
+ return *this;
+ }
+
+ operator int() const { return value; }
+ bool operator==(const PolicyValue& rhs) const { return value == rhs.value; }
+ bool operator!=(const PolicyValue& rhs) const { return value != rhs.value; }
+ bool operator<(const PolicyValue& rhs) const { return value < rhs.value; }
+
+ int value = -1;
+};
+
+} // namespace policy
+} // namespace vist
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-#include <gtest/gtest.h>
-
-#include "../policy-model.h"
-#include "../policy-provider.h"
-
-#include <exception>
-
-namespace {
- int g_value = -1;
-} // anonymous namespace
-
-using namespace vist::policy;
-
-class PolicySDKTests : public testing::Test {};
-
-class TestPolicyModel : public PolicyModel {
-public:
- TestPolicyModel() : PolicyModel("test_policy", PolicyValue(1)) {}
-
- virtual void onChanged(const PolicyValue& value)
- {
- g_value = value;
- }
-};
-
-class TestPolicyModelFailed : public PolicyModel {
-public:
- TestPolicyModelFailed() : PolicyModel("test_policy_failed", PolicyValue(1)) {}
-
- virtual void onChanged(const PolicyValue& value)
- {
- throw std::runtime_error("Intended exception for test.");
- }
-};
-
-TEST_F(PolicySDKTests, policy_model)
-{
- TestPolicyModel policy;
-
- EXPECT_EQ(policy.getName(), "test_policy");
- EXPECT_EQ(policy.getInitial(), 1);
-
- // Policy value should be set once before use
- bool isRaised = false;
- try {
- auto value = policy.get();
- } catch (const std::exception&) {
- isRaised = true;
- }
-
- EXPECT_TRUE(isRaised);
-
- policy.set(PolicyValue(3));
- EXPECT_EQ(3, g_value);
- EXPECT_EQ(3, policy.get());
-}
-
-TEST_F(PolicySDKTests, policy_model_failed)
-{
- TestPolicyModelFailed policy;
-
- EXPECT_EQ(policy.getName(), "test_policy_failed");
- EXPECT_EQ(policy.getInitial(), 1);
-
- bool isRaised = true;
- try {
- policy.set(PolicyValue(3));
- } catch (const std::exception&) {
- isRaised = true;
- }
-
- EXPECT_TRUE(isRaised);
-}
-
-TEST_F(PolicySDKTests, policy_provider)
-{
- PolicyProvider provider("testProvider");
- provider.add(std::make_shared<TestPolicyModel>());
- provider.add(std::make_shared<TestPolicyModelFailed>());
-
- EXPECT_EQ(2, provider.size());
-}
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#include <gtest/gtest.h>
+
+#include <vist/policy/sdk/policy-model.hpp>
+#include <vist/policy/sdk/policy-provider.hpp>
+
+#include <exception>
+
+namespace {
+ int g_value = -1;
+} // anonymous namespace
+
+using namespace vist::policy;
+
+class PolicySDKTests : public testing::Test {};
+
+class TestPolicyModel : public PolicyModel {
+public:
+ TestPolicyModel() : PolicyModel("test_policy", PolicyValue(1)) {}
+
+ virtual void onChanged(const PolicyValue& value)
+ {
+ g_value = value;
+ }
+};
+
+class TestPolicyModelFailed : public PolicyModel {
+public:
+ TestPolicyModelFailed() : PolicyModel("test_policy_failed", PolicyValue(1)) {}
+
+ virtual void onChanged(const PolicyValue& value)
+ {
+ throw std::runtime_error("Intended exception for test.");
+ }
+};
+
+TEST_F(PolicySDKTests, policy_model)
+{
+ TestPolicyModel policy;
+
+ EXPECT_EQ(policy.getName(), "test_policy");
+ EXPECT_EQ(policy.getInitial(), 1);
+
+ // Policy value should be set once before use
+ bool isRaised = false;
+ try {
+ auto value = policy.get();
+ } catch (const std::exception&) {
+ isRaised = true;
+ }
+
+ EXPECT_TRUE(isRaised);
+
+ policy.set(PolicyValue(3));
+ EXPECT_EQ(3, g_value);
+ EXPECT_EQ(3, policy.get());
+}
+
+TEST_F(PolicySDKTests, policy_model_failed)
+{
+ TestPolicyModelFailed policy;
+
+ EXPECT_EQ(policy.getName(), "test_policy_failed");
+ EXPECT_EQ(policy.getInitial(), 1);
+
+ bool isRaised = true;
+ try {
+ policy.set(PolicyValue(3));
+ } catch (const std::exception&) {
+ isRaised = true;
+ }
+
+ EXPECT_TRUE(isRaised);
+}
+
+TEST_F(PolicySDKTests, policy_provider)
+{
+ PolicyProvider provider("testProvider");
+ provider.add(std::make_shared<TestPolicyModel>());
+ provider.add(std::make_shared<TestPolicyModelFailed>());
+
+ EXPECT_EQ(2, provider.size());
+}
--- /dev/null
+/*
+ * Copyright (c) 2017-present Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+/*
+ * @brief namespace TSQB means type-safe query builder
+ */
+
+#pragma once
+
+#include "query-builder/column.hpp"
+#include "query-builder/condition.hpp"
+#include "query-builder/database.hpp"
+#include "query-builder/expression.hpp"
+#include "query-builder/table.hpp"
+#include "query-builder/util.hpp"
--- /dev/null
+# Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License
+
+/// TSQB: Type Safe Query Builder
+FILE(GLOB TSQB_TESTS "tests/*.cpp")
+ADD_VIST_TEST(${TSQB_TESTS})
--- /dev/null
+/*
+ * Copyright (c) 2017-present Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#pragma once
+
+#include "type.hpp"
+
+#include <string>
+#include <vector>
+
+namespace vist {
+namespace tsqb {
+namespace internal {
+
+template<typename... Base>
+class ColumnPack {
+public:
+ virtual ~ColumnPack() = default;
+
+ template<typename ColumnType>
+ std::string getName(ColumnType&&) const noexcept { return std::string(); }
+ std::vector<std::string> getNames(void) const noexcept { return {}; }
+
+ int size() const noexcept { return 0; }
+};
+
+template<typename Front, typename... Rest>
+class ColumnPack<Front, Rest...> : public ColumnPack<Rest...> {
+public:
+ using Column = Front;
+ using TableType = typename Column::TableType;
+
+ explicit ColumnPack(Front&& front, Rest&& ...rest);
+ virtual ~ColumnPack() = default;
+
+ ColumnPack(const ColumnPack&) = delete;
+ ColumnPack& operator=(const ColumnPack&) = delete;
+
+ ColumnPack(ColumnPack&&) = default;
+ ColumnPack& operator=(ColumnPack&&) = default;
+
+ template<typename ColumnType>
+ std::string getName(ColumnType&& type) const noexcept;
+ std::vector<std::string> getNames(void) const noexcept;
+
+ int size() const noexcept { return Base::size() + 1; }
+
+private:
+ using Base = ColumnPack<Rest...>;
+
+ Column column;
+};
+
+template<typename Front, typename... Rest>
+ColumnPack<Front, Rest...>::ColumnPack(Front&& front, Rest&& ...rest) :
+ Base(std::forward<Rest>(rest)...), column(front)
+{
+}
+
+template<typename Front, typename... Rest>
+std::vector<std::string> ColumnPack<Front, Rest...>::getNames(void) const noexcept
+{
+ auto names = Base::getNames();
+ names.push_back(this->column.name);
+
+ return std::move(names);
+}
+
+template<typename Front, typename... Rest>
+template<typename ColumnType>
+std::string ColumnPack<Front, Rest...>::getName(ColumnType&& type) const noexcept
+{
+ if (type::cast_compare(column.type, std::forward<ColumnType>(type)))
+ return column.name;
+
+ return Base::template getName<ColumnType>(std::forward<ColumnType>(type));
+}
+
+} // namespace internal
+} // namespace tsqb
+} // namespace vist
--- /dev/null
+/*
+ * Copyright (c) 2017-present Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#pragma once
+
+#include <string>
+#include <tuple>
+
+namespace vist {
+namespace tsqb {
+
+template<typename Object, typename Field>
+struct Column {
+ using Type = Field Object::*;
+ using FieldType = Field;
+ using TableType = Object;
+
+ std::string name;
+ Type type;
+};
+
+template<typename O, typename F>
+Column<O, F> make_column(const std::string& name, F O::*field)
+{
+ return {name, field};
+}
+
+template<typename Type>
+struct Distinct {
+ Type value;
+};
+
+template<typename... Args>
+auto distinct(Args&&... args) -> decltype(Distinct<std::tuple<Args...>>())
+{
+ return {std::make_tuple(std::forward<Args>(args)...)};
+}
+
+} // namespace tsqb
+} // namespace vist
--- /dev/null
+/*
+ * Copyright (c) 2017-present Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#pragma once
+
+#include "type.hpp"
+
+namespace vist {
+namespace tsqb {
+namespace condition {
+
+struct Base {};
+
+template<typename L, typename R>
+struct And : public Base {
+ L l;
+ R r;
+
+ And(L l, R r) : l(l), r(r) {}
+ operator std::string() const
+ {
+ return "AND";
+ }
+};
+
+template<typename L, typename R>
+struct Or : public Base {
+ L l;
+ R r;
+
+ Or(L l, R r) : l(l), r(r) {}
+ operator std::string() const
+ {
+ return "OR";
+ }
+};
+
+template<typename L, typename R>
+struct Binary : public Base {
+ L l;
+ R r;
+
+ Binary(L l, R r) : l(l), r(r)
+ {
+ using FieldType = typename L::FieldType;
+ type::assert_compare(FieldType(), r);
+ }
+};
+
+template<typename L>
+struct Binary<L, const char*> : public Base {
+ L l;
+ std::string r;
+
+ Binary(L l, const char* r) : l(l), r(r)
+ {
+ using FieldType = typename L::FieldType;
+ type::assert_compare(FieldType(), std::string());
+ }
+};
+
+enum class Join : int {
+ INNER,
+ CROSS,
+ LEFT_OUTER,
+ RIGHT_OUTER,
+ FULL_OUTER
+};
+
+inline std::string to_string(Join type)
+{
+ switch (type) {
+ case Join::CROSS: return "CROSS";
+ case Join::LEFT_OUTER: return "LEFT OUTER";
+ case Join::RIGHT_OUTER: return "RIGHT OUTER";
+ case Join::FULL_OUTER: return "FULL OUTER";
+ case Join::INNER:
+ default:
+ return "INNER";
+ }
+}
+
+} // namespace condition
+} // namespace tsqb
+} // namespace vist
--- /dev/null
+/*
+ * Copyright (c) 2017-present Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#pragma once
+
+#include "column.hpp"
+#include "expression.hpp"
+
+#include <string>
+#include <sstream>
+
+namespace vist {
+namespace tsqb {
+
+template<typename T>
+class Crud {
+public:
+ template<typename... ColumnTypes>
+ T& select(ColumnTypes&&... cts);
+
+ template<typename Type>
+ T& select(Distinct<Type> distinct);
+
+ template<typename TableType>
+ T& selectAll(void);
+
+ T& selectAll(void);
+
+ template<typename... ColumnTypes>
+ T& update(ColumnTypes&&... cts);
+
+ template<typename... ColumnTypes>
+ T& insert(ColumnTypes&&... cts);
+
+ template<typename... ColumnTypes>
+ T& remove(ColumnTypes&&... cts);
+
+ template<typename Expr>
+ T& where(Expr expr);
+
+private:
+ template<typename ColumnTuple>
+ T& selectInternal(ColumnTuple&& ct, bool distinct = false);
+
+ template<typename L, typename R>
+ std::string processWhere(condition::And<L,R>& expr);
+
+ template<typename L, typename R>
+ std::string processWhere(condition::Or<L,R>& expr);
+
+ template<typename Expr>
+ std::string processWhere(Expr expr);
+};
+
+template<typename T>
+template<typename... ColumnTypes>
+T& Crud<T>::select(ColumnTypes&&... cts)
+{
+ auto columnTuple = std::make_tuple(std::forward<ColumnTypes>(cts)...);
+
+ return this->selectInternal(std::move(columnTuple));
+}
+
+template<typename T>
+template<typename Type>
+T& Crud<T>::select(Distinct<Type> distinct)
+{
+ return this->selectInternal(std::move(distinct.value), true);
+}
+
+template<typename T>
+T& Crud<T>::selectAll(void)
+{
+ static_cast<T*>(this)->cache.clear();
+
+ std::stringstream ss;
+ ss << "SELECT * FROM " << static_cast<T*>(this)->name;
+
+ static_cast<T*>(this)->cache.emplace_back(ss.str());
+
+ return *(static_cast<T*>(this));
+}
+
+template<typename T>
+template<typename TableType>
+T& Crud<T>::selectAll(void)
+{
+ static_cast<T*>(this)->cache.clear();
+
+ std::stringstream ss;
+ auto tableName = static_cast<T*>(this)->getTableName(TableType());
+ ss << "SELECT * FROM " << tableName;
+
+ static_cast<T*>(this)->cache.emplace_back(ss.str());
+
+ return *(static_cast<T*>(this));
+}
+
+template<typename T>
+template<typename ColumnTuple>
+T& Crud<T>::selectInternal(ColumnTuple&& ct, bool distinct)
+{
+ static_cast<T*>(this)->cache.clear();
+
+ auto columnNames = static_cast<T*>(this)->getColumnNames(std::move(ct));
+ auto tableNames = static_cast<T*>(this)->getTableNames(std::move(ct));
+
+ std::stringstream ss;
+ ss << "SELECT ";
+
+ if (distinct)
+ ss << "DISTINCT ";
+
+ int i = 0;
+ for (const auto& c : columnNames) {
+ ss << c;
+
+ if (i++ < columnNames.size() - 1)
+ ss << ", ";
+ }
+
+ ss << " FROM ";
+
+ i = 0;
+ for (const auto& t : tableNames) {
+ ss << t;
+
+ if (i++ < tableNames.size() - 1)
+ ss << ", ";
+ }
+
+ static_cast<T*>(this)->cache.emplace_back(ss.str());
+
+ return *(static_cast<T*>(this));
+}
+
+template<typename T>
+template<typename... ColumnTypes>
+T& Crud<T>::update(ColumnTypes&&... cts)
+{
+ static_cast<T*>(this)->cache.clear();
+
+ auto columnTuple = std::make_tuple(std::forward<ColumnTypes>(cts)...);
+ auto columnNames = static_cast<T*>(this)->getColumnNames(std::move(columnTuple));
+
+ std::stringstream ss;
+ ss << "UPDATE " << static_cast<T*>(this)->name << " ";
+ ss << "SET ";
+
+ int i = 0;
+ for (const auto& c : columnNames) {
+ ss << c << " = ?";
+
+ if (i++ < columnNames.size() - 1)
+ ss << ", ";
+ }
+
+ static_cast<T*>(this)->cache.emplace_back(ss.str());
+
+ return *(static_cast<T*>(this));
+}
+
+template<typename T>
+template<typename... ColumnTypes>
+T& Crud<T>::insert(ColumnTypes&&... cts)
+{
+ static_cast<T*>(this)->cache.clear();
+
+ auto columnTuple = std::make_tuple(std::forward<ColumnTypes>(cts)...);
+ auto columnNames = static_cast<T*>(this)->getColumnNames(std::move(columnTuple));
+
+ std::stringstream ss;
+ ss << "INSERT INTO " << static_cast<T*>(this)->name << " (";
+
+ const int columnCount = columnNames.size();
+ for (int i = 0; i < columnCount; i++) {
+ ss << columnNames[i];
+ if (i < columnCount - 1)
+ ss << ", ";
+ }
+
+ ss << ") VALUES (";
+
+ for (int i = 0; i < columnCount; i++) {
+ ss << "?";
+ if (i < columnCount - 1)
+ ss << ", ";
+ }
+
+ ss << ")";
+
+ static_cast<T*>(this)->cache.emplace_back(ss.str());
+
+ return *(static_cast<T*>(this));
+}
+
+template<typename T>
+template<typename... ColumnTypes>
+T& Crud<T>::remove(ColumnTypes&&... cts)
+{
+ static_cast<T*>(this)->cache.clear();
+
+ auto columnTuple = std::make_tuple(std::forward<ColumnTypes>(cts)...);
+ auto columnNames = static_cast<T*>(this)->getColumnNames(std::move(columnTuple));
+
+ std::stringstream ss;
+ ss << "DELETE FROM " << static_cast<T*>(this)->name;
+
+ static_cast<T*>(this)->cache.emplace_back(ss.str());
+
+ return *(static_cast<T*>(this));
+}
+
+template<typename T>
+template<typename Expr>
+T& Crud<T>::where(Expr expr)
+{
+ std::stringstream ss;
+ ss << "WHERE " << this->processWhere(expr);
+
+ static_cast<T*>(this)->cache.emplace_back(ss.str());
+
+ return *(static_cast<T*>(this));
+}
+
+template<typename T>
+template<typename L, typename R>
+std::string Crud<T>::processWhere(condition::And<L,R>& expr)
+{
+ std::stringstream ss;
+ ss << this->processWhere(expr.l) << " ";
+ ss << static_cast<std::string>(expr) << " ";
+ ss << this->processWhere(expr.r);
+
+ return ss.str();
+}
+
+template<typename T>
+template<typename L, typename R>
+std::string Crud<T>::processWhere(condition::Or<L,R>& expr)
+{
+ std::stringstream ss;
+ ss << this->processWhere(expr.l) << " ";
+ ss << static_cast<std::string>(expr) << " ";
+ ss << this->processWhere(expr.r);
+
+ return ss.str();
+}
+
+template<typename T>
+template<typename Expr>
+std::string Crud<T>::processWhere(Expr expr)
+{
+ std::stringstream ss;
+ ss << static_cast<T*>(this)->getColumnName(expr.l.type);
+ ss << " " << std::string(expr) << " ?";
+
+ return ss.str();
+}
+
+} // namespace tsqb
+} // namespace vist
--- /dev/null
+/*
+ * Copyright (c) 2017-present Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#pragma once
+
+#include "crud.hpp"
+#include "condition.hpp"
+#include "expression.hpp"
+#include "table-pack.hpp"
+#include "tuple-helper.hpp"
+#include "util.hpp"
+
+#include <vector>
+#include <set>
+#include <string>
+#include <sstream>
+#include <algorithm>
+
+namespace vist {
+namespace tsqb {
+
+template<typename... Tables>
+class Database : public Crud<Database<Tables...>> {
+public:
+ using Self = Database<Tables...>;
+
+ virtual ~Database() = default;
+
+ Database(const Database&) = delete;
+ Database& operator=(const Database&) = delete;
+
+ Database(Database&&) = default;
+ Database& operator=(Database&&) = default;
+
+ // Functions for Crud
+ template<typename Cs>
+ std::set<std::string> getTableNames(Cs&& tuple) const noexcept;
+ template<typename Cs>
+ std::vector<std::string> getColumnNames(Cs&& tuple) const noexcept;
+ template<typename TableType>
+ std::string getTableName(TableType&& type) const noexcept;
+ template<typename ColumnType>
+ std::string getColumnName(ColumnType&& type) const noexcept;
+
+ template<typename Table>
+ Self& join(condition::Join type = condition::Join::INNER);
+
+ template<typename Expr>
+ Self& on(Expr expr);
+
+ operator std::string();
+
+ std::string name;
+ std::vector<std::string> cache;
+
+private:
+ using TablePackType = internal::TablePack<Tables...>;
+ using ColumnNames = std::vector<std::string>;
+ using TableNames = std::set<std::string>;
+
+ explicit Database(const std::string& name, TablePackType&& tablePack);
+
+ template<typename ...Ts>
+ friend Database<Ts...> make_database(const std::string& name, Ts&& ...tables);
+
+ struct GetTableNames {
+ const TablePackType& tablePack;
+ std::set<std::string> names;
+ GetTableNames(const TablePackType& tablePack) : tablePack(tablePack) {}
+
+ template <typename T>
+ void operator()(T&& type)
+ {
+ auto column = make_column("anonymous", type);
+ using TableType = typename decltype(column)::TableType;
+ auto name = this->tablePack.getName(TableType());
+ if (!name.empty())
+ names.emplace(name);
+ }
+ };
+
+ struct GetColumnNames {
+ const TablePackType& tablePack;
+ std::vector<std::string> names;
+
+ GetColumnNames(const TablePackType& tablePack) : tablePack(tablePack) {}
+
+ template <typename T>
+ void operator()(T&& type)
+ {
+ auto column = make_column("anonymous", type);
+ auto name = this->tablePack.getColumnName(std::move(column));
+ if (!name.empty())
+ names.emplace_back(name);
+ }
+ };
+
+ TablePackType tablePack;
+};
+
+template<typename ...Tables>
+Database<Tables...> make_database(const std::string& name, Tables&& ...tables)
+{
+ auto tablePack = internal::TablePack<Tables...>(std::forward<Tables>(tables)...);
+ return Database<Tables...>(name, std::move(tablePack));
+}
+
+template<typename ...Tables>
+Database<Tables...>::Database(const std::string& name, TablePackType&& tablePack)
+ : name(name), tablePack(std::move(tablePack)) {}
+
+template<typename... Tables>
+template<typename Table>
+Database<Tables...>& Database<Tables...>::join(condition::Join type)
+{
+ std::stringstream ss;
+ ss << condition::to_string(type) << " ";
+ ss << "JOIN ";
+ ss << this->tablePack.getName(Table());
+
+ this->cache.emplace_back(ss.str());
+ return *this;
+}
+
+template<typename... Tables>
+template<typename Expr>
+Database<Tables...>& Database<Tables...>::on(Expr expr)
+{
+ std::stringstream ss;
+ ss << "ON ";
+
+ auto lname = this->tablePack.getColumnName(std::move(expr.l));
+ ss << lname << " ";
+
+ ss << std::string(expr) << " ";
+
+ auto rname = this->tablePack.getColumnName(std::move(expr.r));
+ ss << rname;
+
+ this->cache.emplace_back(ss.str());
+ return *this;
+}
+
+template<typename... Tables>
+Database<Tables...>::operator std::string()
+{
+ std::stringstream ss;
+ for (const auto& c : cache)
+ ss << c << " ";
+
+ this->cache.clear();
+ return util::rtrim(ss.str());
+}
+
+template<typename... Tables>
+template<typename Cs>
+std::set<std::string> Database<Tables...>::getTableNames(Cs&& tuple) const noexcept
+{
+ GetTableNames closure(this->tablePack);
+ tuple_helper::for_each(std::forward<Cs>(tuple), closure);
+
+ return closure.names;
+}
+
+template<typename... Tables>
+template<typename Cs>
+std::vector<std::string> Database<Tables...>::getColumnNames(Cs&& tuple) const noexcept
+{
+ GetColumnNames closure(this->tablePack);
+ tuple_helper::for_each(std::forward<Cs>(tuple), closure);
+
+ return closure.names;
+}
+
+template<typename... Tables>
+template<typename TableType>
+std::string Database<Tables...>::getTableName(TableType&& type) const noexcept
+{
+ return this->tablePack.getName(std::forward<TableType>(type));
+}
+
+template<typename... Tables>
+template<typename ColumnType>
+std::string Database<Tables...>::getColumnName(ColumnType&& type) const noexcept
+{
+ auto column = make_column("anonymous", type);
+ return this->tablePack.getColumnName(std::move(column));
+}
+
+} // namespace tsqb
+} // namespace vist
--- /dev/null
+/*
+ * Copyright (c) 2017-present Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#pragma once
+
+#include "column.hpp"
+#include "type.hpp"
+#include "condition.hpp"
+
+#include <type_traits>
+
+namespace vist {
+namespace tsqb {
+
+template<typename Type>
+struct Expression {
+ Type value;
+};
+
+template<typename O, typename F>
+Expression<Column<O, F>> expr(F O::*field)
+{
+ Column<O, F> anonymous = {"anonymous", field};
+ return {anonymous};
+}
+
+template<typename L, typename R>
+struct Lesser : public condition::Binary<L, R> {
+ using condition::Binary<L, R>::Binary;
+
+ operator std::string() const
+ {
+ return "<";
+ }
+};
+
+template<typename L, typename R>
+Lesser<L, R> operator<(Expression<L> expr, R r)
+{
+ return {expr.value, r};
+}
+
+template<typename L, typename R>
+struct Equal : public condition::Binary<L, R>
+{
+ using condition::Binary<L, R>::Binary;
+
+ operator std::string() const
+ {
+ return "=";
+ }
+};
+
+template<typename L, typename R>
+Equal<L, R> operator==(Expression<L> expr, R r)
+{
+ return {expr.value, r};
+}
+
+namespace join {
+
+template<typename L, typename R>
+struct Equal
+{
+ L l;
+ R r;
+
+ operator std::string() const
+ {
+ return "=";
+ }
+};
+
+} // namespace join
+
+template<typename L, typename R>
+join::Equal<L, R> operator==(Expression<L> l, Expression<R> r)
+{
+ return {l.value, r.value};
+}
+
+template<typename L, typename R>
+struct Greater : public condition::Binary<L, R>
+{
+ using condition::Binary<L, R>::Binary;
+
+ operator std::string() const
+ {
+ return ">";
+ }
+};
+
+template<typename L, typename R>
+Greater<L, R> operator>(Expression<L> expr, R r)
+{
+ return {expr.value, r};
+}
+
+template<bool B, typename T = void>
+using enable_if_t = typename std::enable_if<B, T>::type;
+
+template<typename T>
+using is_condition = typename std::is_base_of<condition::Base, T>;
+
+template<typename L,
+ typename R,
+ typename = typename tsqb::enable_if_t<is_condition<L>::value
+ && tsqb::is_condition<R>::value>>
+condition::And<L, R> operator&&(const L& l, const R& r)
+{
+ return {l, r};
+}
+
+template<typename L,
+ typename R,
+ typename = typename tsqb::enable_if_t<is_condition<L>::value
+ && tsqb::is_condition<R>::value>>
+condition::Or<L, R> operator||(const L& l, const R& r)
+{
+ return {l, r};
+}
+
+} // namespace tsqb
+} // namespace vist
--- /dev/null
+/*
+ * Copyright (c) 2017-present Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#pragma once
+
+#include <vector>
+#include <set>
+#include <string>
+
+namespace vist {
+namespace tsqb {
+namespace internal {
+
+template<typename... Base>
+class TablePack {
+public:
+ virtual ~TablePack() = default;
+
+ template<typename TableType>
+ std::string getName(TableType&&) const noexcept { return std::string(); }
+
+ template<typename ColumnType>
+ std::string getColumnName(ColumnType&&) const noexcept { return std::string(); }
+};
+
+template<typename Front, typename... Rest>
+class TablePack<Front, Rest...> : public TablePack<Rest...> {
+public:
+ using Table = Front;
+
+ explicit TablePack(Front&& front, Rest&& ...rest);
+ virtual ~TablePack() = default;
+
+ TablePack(const TablePack&) = delete;
+ TablePack& operator=(const TablePack&) = delete;
+
+ TablePack(TablePack&&) = default;
+ TablePack& operator=(TablePack&&) = default;
+
+ template<typename TableType>
+ std::string getName(TableType&& table) const noexcept;
+
+ template<typename ColumnType>
+ std::string getColumnName(ColumnType&& column) const noexcept;
+
+private:
+ using Base = TablePack<Rest...>;
+
+ Table table;
+};
+
+template<typename Front, typename... Rest>
+TablePack<Front, Rest...>::TablePack(Front&& front, Rest&& ...rest) :
+ Base(std::forward<Rest>(rest)...), table(front)
+{
+}
+
+template<typename Front, typename... Rest>
+template<typename TableType>
+std::string TablePack<Front, Rest...>::getName(TableType&& table) const noexcept
+{
+ if (this->table.compare(table))
+ return this->table.name;
+
+ return Base::template getName<TableType>(std::forward<TableType>(table));
+}
+
+template<typename Front, typename... Rest>
+template<typename ColumnType>
+std::string TablePack<Front, Rest...>::getColumnName(ColumnType&& column) const noexcept
+{
+ using DecayColumnType = typename std::decay<ColumnType>::type;
+ using DecayTableType = typename DecayColumnType::TableType;
+ if (this->table.compare(DecayTableType())) {
+ auto cname = this->table.getColumnName(column.type);
+ return this->table.name + "." + cname;
+ }
+
+ return Base::template getColumnName<ColumnType>(std::forward<ColumnType>(column));
+}
+
+} // namespace internal
+} // namespace tsqb
+} // namespace vist
--- /dev/null
+/*
+ * Copyright (c) 2017-present Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#pragma once
+
+#include "crud.hpp"
+#include "column.hpp"
+#include "column-pack.hpp"
+#include "tuple-helper.hpp"
+#include "util.hpp"
+
+#include <vector>
+#include <string>
+#include <sstream>
+
+namespace vist {
+namespace tsqb {
+
+template<typename... Columns>
+class Table : public Crud<Table<Columns...>> {
+public:
+ virtual ~Table() = default;
+
+ Table(const Table&) = delete;
+ Table& operator=(const Table&) = delete;
+
+ Table(Table&&) = default;
+ Table& operator=(Table&&) = default;
+
+ // Functions for Crud
+ template<typename Cs>
+ std::set<std::string> getTableNames(Cs&& tuple) const noexcept;
+ template<typename Cs>
+ std::vector<std::string> getColumnNames(Cs&& tuple) const noexcept;
+ template<typename That>
+ std::string getTableName(That&& type) const noexcept;
+ template<typename ColumnType>
+ std::string getColumnName(ColumnType&& type) const noexcept;
+
+ std::vector<std::string> getColumnNames(void) const noexcept;
+
+ template<typename That>
+ bool compare(const That& that) const noexcept;
+
+ int size() const noexcept;
+
+ operator std::string();
+
+ std::string name;
+ std::vector<std::string> cache;
+
+private:
+ using ColumnPackType = internal::ColumnPack<Columns...>;
+ using TableType = typename ColumnPackType::TableType;
+
+ explicit Table(const std::string& name, ColumnPackType&& columnPack);
+
+ template<typename ...Cs>
+ friend Table<Cs...> make_table(const std::string& name, Cs&& ...columns);
+
+ struct GetColumnNames {
+ const ColumnPackType& columnPack;
+ std::vector<std::string> names;
+
+ GetColumnNames(const ColumnPackType& columnPack) : columnPack(columnPack) {}
+
+ template <typename T>
+ void operator()(T&& type)
+ {
+ auto name = this->columnPack.getName(std::forward<T>(type));
+ if (!name.empty())
+ names.emplace_back(name);
+ }
+ };
+
+ ColumnPackType columnPack;
+};
+
+template<typename ...Columns>
+Table<Columns...> make_table(const std::string& name, Columns&& ...cs)
+{
+ auto columnPack = internal::ColumnPack<Columns...>(std::forward<Columns>(cs)...);
+ return Table<Columns...>(name, std::move(columnPack));
+}
+
+template<typename... Columns>
+Table<Columns...>::Table(const std::string& name, ColumnPackType&& columnPack)
+ : name(name), columnPack(std::move(columnPack)) {}
+
+template<typename... Columns>
+template<typename Cs>
+std::set<std::string> Table<Columns...>::getTableNames(Cs&& tuple) const noexcept
+{
+ return {this->name};
+}
+
+template<typename... Columns>
+template<typename Cs>
+std::vector<std::string> Table<Columns...>::getColumnNames(Cs&& tuple) const noexcept
+{
+ GetColumnNames closure(this->columnPack);
+ tuple_helper::for_each(std::forward<Cs>(tuple), closure);
+
+ return closure.names;
+}
+
+template<typename... Columns>
+template<typename That>
+std::string Table<Columns...>::getTableName(That&& type) const noexcept
+{
+ return this->name;
+}
+
+template<typename... Columns>
+template<typename ColumnType>
+std::string Table<Columns...>::getColumnName(ColumnType&& type) const noexcept
+{
+ return this->columnPack.getName(std::forward<ColumnType>(type));
+}
+
+template<typename... Columns>
+std::vector<std::string> Table<Columns...>::getColumnNames(void) const noexcept
+{
+ return this->columnPack.getNames();
+}
+
+template<typename... Columns>
+template<typename That>
+bool Table<Columns...>::compare(const That& that) const noexcept
+{
+ using This = TableType;
+ return type::compare(This(), that);
+}
+
+template<typename... Columns>
+Table<Columns...>::operator std::string()
+{
+ std::stringstream ss;
+ for (const auto& c : cache)
+ ss << c << " ";
+
+ this->cache.clear();
+ return util::rtrim(ss.str());
+}
+
+template<typename... Columns>
+int Table<Columns...>::size() const noexcept
+{
+ return this->columnPack.size();
+}
+
+} // namespace tsqb
+} // namespace vist
--- /dev/null
+/*
+ * Copyright (c) 2017-present Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#include <vist/query-builder.hpp>
+
+#include <gtest/gtest.h>
+
+using namespace vist::tsqb;
+
+struct Admin {
+ int id;
+ std::string pkg;
+ int uid;
+ std::string key;
+ int removable;
+};
+
+struct ManagedPolicy {
+ int id;
+ int aid;
+ int pid;
+ int value;
+};
+
+struct PolicyDefinition {
+ int id;
+ int scope;
+ std::string name;
+ int ivalue;
+};
+
+auto admin = make_table("admin", make_column("id", &Admin::id),
+ make_column("pkg", &Admin::pkg),
+ make_column("uid", &Admin::uid),
+ make_column("key", &Admin::key),
+ make_column("removable", &Admin::removable));
+
+auto managedPolicy = make_table("managed_policy",
+ make_column("id", &ManagedPolicy::id),
+ make_column("aid", &ManagedPolicy::aid),
+ make_column("pid", &ManagedPolicy::pid),
+ make_column("value", &ManagedPolicy::value));
+
+auto policyDefinition = make_table("policy_definition",
+ make_column("id", &PolicyDefinition::id),
+ make_column("scope", &PolicyDefinition::scope),
+ make_column("name", &PolicyDefinition::name),
+ make_column("ivalue", &PolicyDefinition::ivalue));
+
+auto db = make_database("dpm", admin, managedPolicy, policyDefinition);
+
+class TsqbTests : public testing::Test {};
+
+TEST_F(TsqbTests, SELECT)
+{
+ std::string select1 = admin.select(&Admin::id, &Admin::pkg, &Admin::uid, &Admin::key);
+ std::string select2 = admin.select(&Admin::id, &Admin::uid, &Admin::key);
+
+ EXPECT_EQ(select1, "SELECT id, pkg, uid, key FROM admin");
+ EXPECT_EQ(select2, "SELECT id, uid, key FROM admin");
+}
+
+TEST_F(TsqbTests, SELECT_ALL)
+{
+ std::string select = admin.selectAll();
+
+ EXPECT_EQ(select, "SELECT * FROM admin");
+}
+
+TEST_F(TsqbTests, SELECT_WHERE)
+{
+ std::string select1 = admin.select(&Admin::uid, &Admin::key)
+ .where(expr(&Admin::id) > 3);
+ std::string select2 = admin.selectAll().where(expr(&Admin::uid) > 3);
+ std::string select3 = admin.selectAll().where(expr(&Admin::uid) > 3 &&
+ expr(&Admin::pkg) == "dpm");
+ std::string select4 = admin.selectAll().where(expr(&Admin::uid) > 3 ||
+ expr(&Admin::pkg) == "dpm");
+
+ EXPECT_EQ(select1, "SELECT uid, key FROM admin WHERE id > ?");
+ EXPECT_EQ(select2, "SELECT * FROM admin WHERE uid > ?");
+ EXPECT_EQ(select3, "SELECT * FROM admin WHERE uid > ? AND pkg = ?");
+ EXPECT_EQ(select4, "SELECT * FROM admin WHERE uid > ? OR pkg = ?");
+}
+
+TEST_F(TsqbTests, SELECT_DISTINCT)
+{
+ std::string select = admin.select(distinct(&Admin::uid, &Admin::key))
+ .where(expr(&Admin::id) > 3);
+
+ EXPECT_EQ(select, "SELECT DISTINCT uid, key FROM admin WHERE id > ?");
+}
+
+TEST_F(TsqbTests, UPDATE)
+{
+ int uid = 0, id = 1;
+ std::string update1 = admin.update(&Admin::id, &Admin::pkg, &Admin::uid, &Admin::key);
+ std::string update2 = admin.update(&Admin::key).where(expr(&Admin::uid) == uid &&
+ expr(&Admin::id) == id);
+ std::string update3 = admin.update(&Admin::key, &Admin::pkg)
+ .where(expr(&Admin::uid) == 0 && expr(&Admin::id) == 1);
+
+ EXPECT_EQ(update1, "UPDATE admin SET id = ?, pkg = ?, uid = ?, key = ?");
+ EXPECT_EQ(update2, "UPDATE admin SET key = ? WHERE uid = ? AND id = ?");
+ EXPECT_EQ(update3, "UPDATE admin SET key = ?, pkg = ? WHERE uid = ? AND id = ?");
+}
+
+TEST_F(TsqbTests, DELETE)
+{
+ std::string delete1 = admin.remove();
+ std::string delete2 = admin.remove().where(expr(&Admin::pkg) == "dpm" &&
+ expr(&Admin::uid) == 3);
+
+ EXPECT_EQ(delete1, "DELETE FROM admin");
+ EXPECT_EQ(delete2, "DELETE FROM admin WHERE pkg = ? AND uid = ?");
+}
+
+TEST_F(TsqbTests, INSERT)
+{
+ std::string insert1 = admin.insert(&Admin::id, &Admin::pkg, &Admin::uid, &Admin::key);
+ std::string insert2 = admin.insert(&Admin::id, &Admin::pkg, &Admin::key);
+
+ EXPECT_EQ(insert1, "INSERT INTO admin (id, pkg, uid, key) VALUES (?, ?, ?, ?)");
+ EXPECT_EQ(insert2, "INSERT INTO admin (id, pkg, key) VALUES (?, ?, ?)");
+}
+
+TEST_F(TsqbTests, TYPE_SAFE)
+{
+/*
+ * Below cause complie error since expression types are dismatch.
+
+ std::string type_unsafe1 = admin.selectAll().where(expr(&Admin::uid) > "dpm");
+ std::string type_unsafe2 = admin.selectAll().where(expr(&Admin::uid) == "dpm");
+ std::string type_unsafe3 = admin.selectAll().where(expr(&Admin::pkg) == 3);
+ int pkg = 3;
+ std::string type_unsafe4 = admin.selectAll().where(expr(&Admin::pkg) < pkg);
+ std::string type_unsafe5 = admin.remove().where(expr(&Admin::pkg) == "dpm" &&
+ expr(&Admin::uid) == "dpm");
+*/
+}
+
+TEST_F(TsqbTests, MULTI_SELECT)
+{
+ std::string multiSelect1 = db.select(&Admin::uid, &Admin::key,
+ &ManagedPolicy::id, &ManagedPolicy::value);
+ std::string multiSelect2 = db.select(&Admin::uid, &Admin::key,
+ &ManagedPolicy::id, &ManagedPolicy::value)
+ .where(expr(&Admin::uid) > 0 && expr(&ManagedPolicy::id) == 3);
+
+ EXPECT_EQ(multiSelect1, "SELECT admin.uid, admin.key, managed_policy.id, "
+ "managed_policy.value FROM admin, managed_policy");
+ EXPECT_EQ(multiSelect2, "SELECT admin.uid, admin.key, managed_policy.id, "
+ "managed_policy.value FROM admin, managed_policy "
+ "WHERE admin.uid > ? AND managed_policy.id = ?");
+}
+
+TEST_F(TsqbTests, JOIN)
+{
+ std::string join1 = db.select(&Admin::uid, &Admin::key)
+ .join<PolicyDefinition>(condition::Join::LEFT_OUTER);
+ std::string join2 = db.select(&Admin::uid, &Admin::key)
+ .join<ManagedPolicy>(condition::Join::CROSS);
+ std::string join3 = db.select(&ManagedPolicy::value)
+ .join<PolicyDefinition>()
+ .on(expr(&ManagedPolicy::pid) == expr(&PolicyDefinition::id))
+ .join<Admin>()
+ .on(expr(&ManagedPolicy::aid) == expr(&Admin::id))
+ .where(expr(&ManagedPolicy::pid) == 99);
+
+ EXPECT_EQ(join1, "SELECT admin.uid, admin.key FROM admin "
+ "LEFT OUTER JOIN policy_definition");
+ EXPECT_EQ(join2, "SELECT admin.uid, admin.key FROM admin "
+ "CROSS JOIN managed_policy");
+ EXPECT_EQ(join3, "SELECT managed_policy.value FROM managed_policy "
+ "INNER JOIN policy_definition "
+ "ON managed_policy.pid = policy_definition.id "
+ "INNER JOIN admin ON managed_policy.aid = admin.id "
+ "WHERE managed_policy.pid = ?");
+}
--- /dev/null
+/*
+ * Copyright (c) 2017-present Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#pragma once
+
+#include <tuple>
+
+namespace vist {
+namespace tsqb {
+namespace tuple_helper {
+namespace internal {
+
+template<int n, typename T, typename C>
+class Iterator {
+public:
+ Iterator(const T& tuple, C&& closure) {
+ Iterator<n - 1, T, C> iter(tuple, std::forward<C>(closure));
+ closure(std::get<n-1>(tuple));
+ }
+};
+
+template<typename T, typename C>
+class Iterator<0, T, C> {
+public:
+ Iterator(const T&, C&&) {}
+};
+
+} // namespace internal
+
+template<typename T, typename C>
+void for_each(const T& tuple, C&& closure)
+{
+ using Iter = internal::Iterator<std::tuple_size<T>::value, T, C>;
+ Iter iter(tuple, std::forward<C>(closure));
+}
+
+} // namspace tuple-hepler
+} // namspace tsqb
+} // namspace vist
--- /dev/null
+/*
+ * Copyright (c) 2017-present Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#pragma once
+
+#include <type_traits>
+
+namespace vist {
+namespace tsqb {
+namespace type {
+
+template<typename L, typename R>
+bool cast_compare(L l, R r)
+{
+ return l == reinterpret_cast<L>(r);
+}
+
+template<typename L, typename R>
+bool compare(L l, R r)
+{
+ return std::is_same<L, R>::value;
+}
+
+template<typename L, typename R>
+void assert_compare(L l, R r)
+{
+ static_assert(std::is_same<L, R>::value, "Type is unsafe.");
+}
+
+} // namespace type
+} // namespace tsqb
+} // namespace vist
--- /dev/null
+/*
+ * Copyright (c) 2017-present Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#pragma once
+
+#include <string>
+#include <algorithm>
+#include <cctype>
+
+namespace vist {
+namespace tsqb {
+namespace util {
+
+inline std::string rtrim(std::string&& s)
+{
+ auto predicate = [](unsigned char c){ return !std::isspace(c); };
+ auto base = std::find_if(s.rbegin(), s.rend(), predicate).base();
+ s.erase(base, s.end());
+ return s;
+}
+
+} // namespace util
+} // namespace tsqb
+} // namespace vist
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-#include <gtest/gtest.h>
-
-#include "../vist.h"
-
-#include <iostream>
-#include <chrono>
-#include <thread>
-
-#include <vist/policy/core/policy-manager.h>
-
-using namespace vist;
-
-class CoreTests : public testing::Test {};
-
-TEST_F(CoreTests, query_select) {
- auto rows = Vist::Query("SELECT * FROM policy");
-
- EXPECT_TRUE(rows.size() > 0);
-
- std::string statement = "SELECT * FROM policy WHERE name = 'bluetooth'";
- rows = Vist::Query(statement);
-
- EXPECT_EQ(rows.size(), 1);
- EXPECT_EQ(rows[0]["name"], "bluetooth");
-}
-
-TEST_F(CoreTests, query_update) {
- auto& manager = policy::PolicyManager::Instance();
- manager.enroll("admin");
-
- std::string statement = "SELECT * FROM policy WHERE name = 'bluetooth'";
- auto rows = Vist::Query(statement);
- /// Initial policy value
- EXPECT_EQ(rows[0]["value"], std::to_string(1));
-
- statement = "UPDATE policy SET value = '3' WHERE name = 'bluetooth'";
- rows = Vist::Query(statement);
- EXPECT_EQ(rows.size(), 0);
-
- statement = "SELECT * FROM policy WHERE name = 'bluetooth'";
- rows = Vist::Query(statement);
- EXPECT_EQ(rows[0]["value"], std::to_string(3));
-
- manager.disenroll("admin");
-}
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#include <gtest/gtest.h>
+
+#include <vist/service/vist.hpp>
+#include <vist/policy/core/policy-manager.hpp>
+
+#include <iostream>
+#include <chrono>
+#include <thread>
+
+using namespace vist;
+
+class CoreTests : public testing::Test {};
+
+TEST_F(CoreTests, query_select)
+{
+ auto rows = Vist::Query("SELECT * FROM policy");
+
+ EXPECT_TRUE(rows.size() > 0);
+
+ std::string statement = "SELECT * FROM policy WHERE name = 'bluetooth'";
+ rows = Vist::Query(statement);
+
+ EXPECT_EQ(rows.size(), 1);
+ EXPECT_EQ(rows[0]["name"], "bluetooth");
+}
+
+TEST_F(CoreTests, query_update)
+{
+ auto& manager = policy::PolicyManager::Instance();
+ manager.enroll("admin");
+
+ std::string statement = "SELECT * FROM policy WHERE name = 'bluetooth'";
+ auto rows = Vist::Query(statement);
+ /// Initial policy value
+ EXPECT_EQ(rows[0]["value"], std::to_string(1));
+
+ statement = "UPDATE policy SET value = '3' WHERE name = 'bluetooth'";
+ rows = Vist::Query(statement);
+ EXPECT_EQ(rows.size(), 0);
+
+ statement = "SELECT * FROM policy WHERE name = 'bluetooth'";
+ rows = Vist::Query(statement);
+ EXPECT_EQ(rows[0]["value"], std::to_string(3));
+
+ manager.disenroll("admin");
+}
* limitations under the License
*/
-#include "vist.h"
+#include "vist.hpp"
-#include <vist/common/ipc/server.h>
-#include <vist/common/audit/logger.h>
+#include <vist/ipc/server.hpp>
+#include <vist/logger.hpp>
#include <stdexcept>
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-#pragma once
-
-#include <map>
-#include <string>
-#include <vector>
-
-namespace vist {
-
-using Row = std::map<std::string, std::string>;
-using Rows = std::vector<Row>;
-
-class Vist final {
-public:
- Vist(const Vist&) = delete;
- Vist& operator=(const Vist&) = delete;
-
- Vist(Vist&&) = default;
- Vist& operator=(Vist&&) = default;
-
- /// Exposed method (API)
- Rows query(const std::string& statement);
-
- static Vist& Instance()
- {
- static Vist instance;
- return instance;
- }
-
- static Rows Query(const std::string& statement)
- {
- return Vist::Instance().query(statement);
- }
-
- void start();
-
-private:
- explicit Vist();
- ~Vist() = default;
-};
-
-} // namespace vist
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#pragma once
+
+#include <map>
+#include <string>
+#include <vector>
+
+namespace vist {
+
+using Row = std::map<std::string, std::string>;
+using Rows = std::vector<Row>;
+
+class Vist final {
+public:
+ Vist(const Vist&) = delete;
+ Vist& operator=(const Vist&) = delete;
+
+ Vist(Vist&&) = default;
+ Vist& operator=(Vist&&) = default;
+
+ /// Exposed method (API)
+ Rows query(const std::string& statement);
+
+ static Vist& Instance()
+ {
+ static Vist instance;
+ return instance;
+ }
+
+ static Rows Query(const std::string& statement)
+ {
+ return Vist::Instance().query(statement);
+ }
+
+ void start();
+
+private:
+ explicit Vist();
+ ~Vist() = default;
+};
+
+} // namespace vist
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#include <gtest/gtest.h>
+
+#include <vist/exception.hpp>
+
+enum class ErrCode {
+ InvalidArgument = 1,
+ DominError,
+ LogicError,
+ RuntimeError,
+ None
+};
+
+class ExceptionTests : public testing::Test {};
+
+TEST_F(ExceptionTests, exception)
+{
+ bool raised = false;
+ ErrCode ec = ErrCode::None;
+ std::string msg;
+
+ try {
+ THROW(ErrCode::InvalidArgument);
+ } catch (const vist::Exception<ErrCode>& e) {
+ raised = true;
+ ec = e.get();
+ msg = e.what();
+ }
+
+ EXPECT_TRUE(raised);
+ EXPECT_EQ(ec, ErrCode::InvalidArgument);
+ EXPECT_NE(std::string::npos, msg.find("ErrCode"));
+}
+
+TEST_F(ExceptionTests, exception_msg)
+{
+ bool raised = false;
+ ErrCode ec = ErrCode::None;
+ std::string msg;
+
+ try {
+ THROW(ErrCode::RuntimeError) << "Additional error message";
+ } catch (const vist::Exception<ErrCode>& e) {
+ raised = true;
+ ec = e.get();
+ msg = e.what();
+ }
+
+ EXPECT_TRUE(raised);
+ EXPECT_EQ(ec, ErrCode::RuntimeError);
+ EXPECT_NE(std::string::npos, msg.find("Additional"));
+}
+++ /dev/null
-# Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License
-
-FILE(GLOB TSQB_TESTS "tests/*.cpp")
-ADD_VIST_TEST(${TSQB_TESTS})
+++ /dev/null
-/*
- * Copyright (c) 2017-present Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-/*
- * @file column-pack.hxx
- * @author Sangwan Kwon (sangwan.kwon@samsung.com)
- * @brief Tie different types of columns
- */
-
-#pragma once
-
-#include "type.hxx"
-
-#include <string>
-#include <vector>
-
-namespace tsqb {
-namespace internal {
-
-template<typename... Base>
-class ColumnPack {
-public:
- virtual ~ColumnPack() = default;
-
- template<typename ColumnType>
- std::string getName(ColumnType&&) const noexcept { return std::string(); }
- std::vector<std::string> getNames(void) const noexcept { return {}; }
-
- int size() const noexcept { return 0; }
-};
-
-template<typename Front, typename... Rest>
-class ColumnPack<Front, Rest...> : public ColumnPack<Rest...> {
-public:
- using Column = Front;
- using TableType = typename Column::TableType;
-
- explicit ColumnPack(Front&& front, Rest&& ...rest);
- virtual ~ColumnPack() = default;
-
- ColumnPack(const ColumnPack&) = delete;
- ColumnPack& operator=(const ColumnPack&) = delete;
-
- ColumnPack(ColumnPack&&) = default;
- ColumnPack& operator=(ColumnPack&&) = default;
-
- template<typename ColumnType>
- std::string getName(ColumnType&& type) const noexcept;
- std::vector<std::string> getNames(void) const noexcept;
-
- int size() const noexcept { return Base::size() + 1; }
-
-private:
- using Base = ColumnPack<Rest...>;
-
- Column column;
-};
-
-template<typename Front, typename... Rest>
-ColumnPack<Front, Rest...>::ColumnPack(Front&& front, Rest&& ...rest) :
- Base(std::forward<Rest>(rest)...), column(front)
-{
-}
-
-template<typename Front, typename... Rest>
-std::vector<std::string> ColumnPack<Front, Rest...>::getNames(void) const noexcept
-{
- auto names = Base::getNames();
- names.push_back(this->column.name);
-
- return std::move(names);
-}
-
-template<typename Front, typename... Rest>
-template<typename ColumnType>
-std::string ColumnPack<Front, Rest...>::getName(ColumnType&& type) const noexcept
-{
- if (type::cast_compare(column.type, std::forward<ColumnType>(type)))
- return column.name;
-
- return Base::template getName<ColumnType>(std::forward<ColumnType>(type));
-}
-
-} // namespace internal
-} // namespace tsqb
+++ /dev/null
-/*
- * Copyright (c) 2017-present Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-/*
- * @file culumn.hxx
- * @author Sangwan Kwon (sangwan.kwon@samsung.com)
- * @brief Capture member pointer of struct and use it for matching with column
- */
-
-#pragma once
-
-#include <string>
-#include <tuple>
-
-namespace tsqb {
-
-template<typename Object, typename Field>
-struct Column {
- using Type = Field Object::*;
- using FieldType = Field;
- using TableType = Object;
-
- std::string name;
- Type type;
-};
-
-template<typename O, typename F>
-Column<O, F> make_column(const std::string& name, F O::*field)
-{
- return {name, field};
-}
-
-template<typename Type>
-struct Distinct {
- Type value;
-};
-
-template<typename... Args>
-auto distinct(Args&&... args) -> decltype(Distinct<std::tuple<Args...>>())
-{
- return {std::make_tuple(std::forward<Args>(args)...)};
-}
-
-} // namespace tsqb
+++ /dev/null
-/*
- * Copyright (c) 2017-present Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-/*
- * @file condition.hxx
- * @author Sangwan Kwon (sangwan.kwon@samsung.com)
- * @brief Represent the condition statement of SQL
- */
-
-#pragma once
-
-#include "type.hxx"
-
-namespace tsqb {
-namespace condition {
-
-struct Base {};
-
-template<typename L, typename R>
-struct And : public Base {
- L l;
- R r;
-
- And(L l, R r) : l(l), r(r) {}
- operator std::string() const
- {
- return "AND";
- }
-};
-
-template<typename L, typename R>
-struct Or : public Base {
- L l;
- R r;
-
- Or(L l, R r) : l(l), r(r) {}
- operator std::string() const
- {
- return "OR";
- }
-};
-
-template<typename L, typename R>
-struct Binary : public Base {
- L l;
- R r;
-
- Binary(L l, R r) : l(l), r(r)
- {
- using FieldType = typename L::FieldType;
- type::assert_compare(FieldType(), r);
- }
-};
-
-template<typename L>
-struct Binary<L, const char*> : public Base {
- L l;
- std::string r;
-
- Binary(L l, const char* r) : l(l), r(r)
- {
- using FieldType = typename L::FieldType;
- type::assert_compare(FieldType(), std::string());
- }
-};
-
-enum class Join : int {
- INNER,
- CROSS,
- LEFT_OUTER,
- RIGHT_OUTER,
- FULL_OUTER
-};
-
-inline std::string to_string(Join type)
-{
- switch (type) {
- case Join::CROSS: return "CROSS";
- case Join::LEFT_OUTER: return "LEFT OUTER";
- case Join::RIGHT_OUTER: return "RIGHT OUTER";
- case Join::FULL_OUTER: return "FULL OUTER";
- case Join::INNER:
- default:
- return "INNER";
- }
-}
-
-} // namespace condition
-} // namespace tsqb
+++ /dev/null
-/*
- * Copyright (c) 2017-present Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-/*
- * @file crud.hxx
- * @author Sangwan Kwon (sangwan.kwon@samsung.com)
- * @brief Represent four basic functions of CRUD
- */
-
-#pragma once
-
-#include "column.hxx"
-#include "expression.hxx"
-
-#include <string>
-#include <sstream>
-
-namespace tsqb {
-
-template<typename T>
-class Crud {
-public:
- template<typename... ColumnTypes>
- T& select(ColumnTypes&&... cts);
-
- template<typename Type>
- T& select(Distinct<Type> distinct);
-
- template<typename TableType>
- T& selectAll(void);
-
- T& selectAll(void);
-
- template<typename... ColumnTypes>
- T& update(ColumnTypes&&... cts);
-
- template<typename... ColumnTypes>
- T& insert(ColumnTypes&&... cts);
-
- template<typename... ColumnTypes>
- T& remove(ColumnTypes&&... cts);
-
- template<typename Expr>
- T& where(Expr expr);
-
-private:
- template<typename ColumnTuple>
- T& selectInternal(ColumnTuple&& ct, bool distinct = false);
-
- template<typename L, typename R>
- std::string processWhere(condition::And<L,R>& expr);
-
- template<typename L, typename R>
- std::string processWhere(condition::Or<L,R>& expr);
-
- template<typename Expr>
- std::string processWhere(Expr expr);
-};
-
-template<typename T>
-template<typename... ColumnTypes>
-T& Crud<T>::select(ColumnTypes&&... cts)
-{
- auto columnTuple = std::make_tuple(std::forward<ColumnTypes>(cts)...);
-
- return this->selectInternal(std::move(columnTuple));
-}
-
-template<typename T>
-template<typename Type>
-T& Crud<T>::select(Distinct<Type> distinct)
-{
- return this->selectInternal(std::move(distinct.value), true);
-}
-
-template<typename T>
-T& Crud<T>::selectAll(void)
-{
- static_cast<T*>(this)->cache.clear();
-
- std::stringstream ss;
- ss << "SELECT * FROM " << static_cast<T*>(this)->name;
-
- static_cast<T*>(this)->cache.emplace_back(ss.str());
-
- return *(static_cast<T*>(this));
-}
-
-template<typename T>
-template<typename TableType>
-T& Crud<T>::selectAll(void)
-{
- static_cast<T*>(this)->cache.clear();
-
- std::stringstream ss;
- auto tableName = static_cast<T*>(this)->getTableName(TableType());
- ss << "SELECT * FROM " << tableName;
-
- static_cast<T*>(this)->cache.emplace_back(ss.str());
-
- return *(static_cast<T*>(this));
-}
-
-template<typename T>
-template<typename ColumnTuple>
-T& Crud<T>::selectInternal(ColumnTuple&& ct, bool distinct)
-{
- static_cast<T*>(this)->cache.clear();
-
- auto columnNames = static_cast<T*>(this)->getColumnNames(std::move(ct));
- auto tableNames = static_cast<T*>(this)->getTableNames(std::move(ct));
-
- std::stringstream ss;
- ss << "SELECT ";
-
- if (distinct)
- ss << "DISTINCT ";
-
- int i = 0;
- for (const auto& c : columnNames) {
- ss << c;
-
- if (i++ < columnNames.size() - 1)
- ss << ", ";
- }
-
- ss << " FROM ";
-
- i = 0;
- for (const auto& t : tableNames) {
- ss << t;
-
- if (i++ < tableNames.size() - 1)
- ss << ", ";
- }
-
- static_cast<T*>(this)->cache.emplace_back(ss.str());
-
- return *(static_cast<T*>(this));
-}
-
-template<typename T>
-template<typename... ColumnTypes>
-T& Crud<T>::update(ColumnTypes&&... cts)
-{
- static_cast<T*>(this)->cache.clear();
-
- auto columnTuple = std::make_tuple(std::forward<ColumnTypes>(cts)...);
- auto columnNames = static_cast<T*>(this)->getColumnNames(std::move(columnTuple));
-
- std::stringstream ss;
- ss << "UPDATE " << static_cast<T*>(this)->name << " ";
- ss << "SET ";
-
- int i = 0;
- for (const auto& c : columnNames) {
- ss << c << " = ?";
-
- if (i++ < columnNames.size() - 1)
- ss << ", ";
- }
-
- static_cast<T*>(this)->cache.emplace_back(ss.str());
-
- return *(static_cast<T*>(this));
-}
-
-template<typename T>
-template<typename... ColumnTypes>
-T& Crud<T>::insert(ColumnTypes&&... cts)
-{
- static_cast<T*>(this)->cache.clear();
-
- auto columnTuple = std::make_tuple(std::forward<ColumnTypes>(cts)...);
- auto columnNames = static_cast<T*>(this)->getColumnNames(std::move(columnTuple));
-
- std::stringstream ss;
- ss << "INSERT INTO " << static_cast<T*>(this)->name << " (";
-
- const int columnCount = columnNames.size();
- for (int i = 0; i < columnCount; i++) {
- ss << columnNames[i];
- if (i < columnCount - 1)
- ss << ", ";
- }
-
- ss << ") VALUES (";
-
- for (int i = 0; i < columnCount; i++) {
- ss << "?";
- if (i < columnCount - 1)
- ss << ", ";
- }
-
- ss << ")";
-
- static_cast<T*>(this)->cache.emplace_back(ss.str());
-
- return *(static_cast<T*>(this));
-}
-
-template<typename T>
-template<typename... ColumnTypes>
-T& Crud<T>::remove(ColumnTypes&&... cts)
-{
- static_cast<T*>(this)->cache.clear();
-
- auto columnTuple = std::make_tuple(std::forward<ColumnTypes>(cts)...);
- auto columnNames = static_cast<T*>(this)->getColumnNames(std::move(columnTuple));
-
- std::stringstream ss;
- ss << "DELETE FROM " << static_cast<T*>(this)->name;
-
- static_cast<T*>(this)->cache.emplace_back(ss.str());
-
- return *(static_cast<T*>(this));
-}
-
-template<typename T>
-template<typename Expr>
-T& Crud<T>::where(Expr expr)
-{
- std::stringstream ss;
- ss << "WHERE " << this->processWhere(expr);
-
- static_cast<T*>(this)->cache.emplace_back(ss.str());
-
- return *(static_cast<T*>(this));
-}
-
-template<typename T>
-template<typename L, typename R>
-std::string Crud<T>::processWhere(condition::And<L,R>& expr)
-{
- std::stringstream ss;
- ss << this->processWhere(expr.l) << " ";
- ss << static_cast<std::string>(expr) << " ";
- ss << this->processWhere(expr.r);
-
- return ss.str();
-}
-
-template<typename T>
-template<typename L, typename R>
-std::string Crud<T>::processWhere(condition::Or<L,R>& expr)
-{
- std::stringstream ss;
- ss << this->processWhere(expr.l) << " ";
- ss << static_cast<std::string>(expr) << " ";
- ss << this->processWhere(expr.r);
-
- return ss.str();
-}
-
-template<typename T>
-template<typename Expr>
-std::string Crud<T>::processWhere(Expr expr)
-{
- std::stringstream ss;
- ss << static_cast<T*>(this)->getColumnName(expr.l.type);
- ss << " " << std::string(expr) << " ?";
-
- return ss.str();
-}
-
-} // namespace tsqb
+++ /dev/null
-/*
- * Copyright (c) 2017-present Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-/*
- * @file database.hxx
- * @author Sangwan Kwon (sangwan.kwon@samsung.com)
- * @brief Represent the database scheme of SQL
- */
-
-#pragma once
-
-#include "crud.hxx"
-#include "condition.hxx"
-#include "expression.hxx"
-#include "table-pack.hxx"
-#include "tuple-helper.hxx"
-#include "util.hxx"
-
-#include <vector>
-#include <set>
-#include <string>
-#include <sstream>
-#include <algorithm>
-
-namespace tsqb {
-
-template<typename... Tables>
-class Database : public Crud<Database<Tables...>> {
-public:
- using Self = Database<Tables...>;
-
- virtual ~Database() = default;
-
- Database(const Database&) = delete;
- Database& operator=(const Database&) = delete;
-
- Database(Database&&) = default;
- Database& operator=(Database&&) = default;
-
- // Functions for Crud
- template<typename Cs>
- std::set<std::string> getTableNames(Cs&& tuple) const noexcept;
- template<typename Cs>
- std::vector<std::string> getColumnNames(Cs&& tuple) const noexcept;
- template<typename TableType>
- std::string getTableName(TableType&& type) const noexcept;
- template<typename ColumnType>
- std::string getColumnName(ColumnType&& type) const noexcept;
-
- template<typename Table>
- Self& join(condition::Join type = condition::Join::INNER);
-
- template<typename Expr>
- Self& on(Expr expr);
-
- operator std::string();
-
- std::string name;
- std::vector<std::string> cache;
-
-private:
- using TablePackType = internal::TablePack<Tables...>;
- using ColumnNames = std::vector<std::string>;
- using TableNames = std::set<std::string>;
-
- explicit Database(const std::string& name, TablePackType&& tablePack);
-
- template<typename ...Ts>
- friend Database<Ts...> make_database(const std::string& name, Ts&& ...tables);
-
- struct GetTableNames {
- const TablePackType& tablePack;
- std::set<std::string> names;
- GetTableNames(const TablePackType& tablePack) : tablePack(tablePack) {}
-
- template <typename T>
- void operator()(T&& type)
- {
- auto column = make_column("anonymous", type);
- using TableType = typename decltype(column)::TableType;
- auto name = this->tablePack.getName(TableType());
- if (!name.empty())
- names.emplace(name);
- }
- };
-
- struct GetColumnNames {
- const TablePackType& tablePack;
- std::vector<std::string> names;
-
- GetColumnNames(const TablePackType& tablePack) : tablePack(tablePack) {}
-
- template <typename T>
- void operator()(T&& type)
- {
- auto column = make_column("anonymous", type);
- auto name = this->tablePack.getColumnName(std::move(column));
- if (!name.empty())
- names.emplace_back(name);
- }
- };
-
- TablePackType tablePack;
-};
-
-template<typename ...Tables>
-Database<Tables...> make_database(const std::string& name, Tables&& ...tables)
-{
- auto tablePack = internal::TablePack<Tables...>(std::forward<Tables>(tables)...);
- return Database<Tables...>(name, std::move(tablePack));
-}
-
-template<typename ...Tables>
-Database<Tables...>::Database(const std::string& name, TablePackType&& tablePack)
- : name(name), tablePack(std::move(tablePack)) {}
-
-template<typename... Tables>
-template<typename Table>
-Database<Tables...>& Database<Tables...>::join(condition::Join type)
-{
- std::stringstream ss;
- ss << condition::to_string(type) << " ";
- ss << "JOIN ";
- ss << this->tablePack.getName(Table());
-
- this->cache.emplace_back(ss.str());
- return *this;
-}
-
-template<typename... Tables>
-template<typename Expr>
-Database<Tables...>& Database<Tables...>::on(Expr expr)
-{
- std::stringstream ss;
- ss << "ON ";
-
- auto lname = this->tablePack.getColumnName(std::move(expr.l));
- ss << lname << " ";
-
- ss << std::string(expr) << " ";
-
- auto rname = this->tablePack.getColumnName(std::move(expr.r));
- ss << rname;
-
- this->cache.emplace_back(ss.str());
- return *this;
-}
-
-template<typename... Tables>
-Database<Tables...>::operator std::string()
-{
- std::stringstream ss;
- for (const auto& c : cache)
- ss << c << " ";
-
- this->cache.clear();
- return util::rtrim(ss.str());
-}
-
-template<typename... Tables>
-template<typename Cs>
-std::set<std::string> Database<Tables...>::getTableNames(Cs&& tuple) const noexcept
-{
- GetTableNames closure(this->tablePack);
- tuple_helper::for_each(std::forward<Cs>(tuple), closure);
-
- return closure.names;
-}
-
-template<typename... Tables>
-template<typename Cs>
-std::vector<std::string> Database<Tables...>::getColumnNames(Cs&& tuple) const noexcept
-{
- GetColumnNames closure(this->tablePack);
- tuple_helper::for_each(std::forward<Cs>(tuple), closure);
-
- return closure.names;
-}
-
-template<typename... Tables>
-template<typename TableType>
-std::string Database<Tables...>::getTableName(TableType&& type) const noexcept
-{
- return this->tablePack.getName(std::forward<TableType>(type));
-}
-
-template<typename... Tables>
-template<typename ColumnType>
-std::string Database<Tables...>::getColumnName(ColumnType&& type) const noexcept
-{
- auto column = make_column("anonymous", type);
- return this->tablePack.getColumnName(std::move(column));
-}
-
-} // namespace tsqb
+++ /dev/null
-/*
- * Copyright (c) 2017-present Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-/*
- * @file expression.hxx
- * @author Sangwan Kwon (sangwan.kwon@samsung.com)
- * @brief Represent the expression of SQL
- */
-
-#pragma once
-
-#include "column.hxx"
-#include "type.hxx"
-#include "condition.hxx"
-
-#include <type_traits>
-
-namespace tsqb {
-
-template<typename Type>
-struct Expression {
- Type value;
-};
-
-template<typename O, typename F>
-Expression<Column<O, F>> expr(F O::*field)
-{
- Column<O, F> anonymous = {"anonymous", field};
- return {anonymous};
-}
-
-template<typename L, typename R>
-struct Lesser : public condition::Binary<L, R> {
- using condition::Binary<L, R>::Binary;
-
- operator std::string() const
- {
- return "<";
- }
-};
-
-template<typename L, typename R>
-Lesser<L, R> operator<(Expression<L> expr, R r)
-{
- return {expr.value, r};
-}
-
-template<typename L, typename R>
-struct Equal : public condition::Binary<L, R>
-{
- using condition::Binary<L, R>::Binary;
-
- operator std::string() const
- {
- return "=";
- }
-};
-
-template<typename L, typename R>
-Equal<L, R> operator==(Expression<L> expr, R r)
-{
- return {expr.value, r};
-}
-
-namespace join {
-
-template<typename L, typename R>
-struct Equal
-{
- L l;
- R r;
-
- operator std::string() const
- {
- return "=";
- }
-};
-
-} // namespace join
-
-template<typename L, typename R>
-join::Equal<L, R> operator==(Expression<L> l, Expression<R> r)
-{
- return {l.value, r.value};
-}
-
-template<typename L, typename R>
-struct Greater : public condition::Binary<L, R>
-{
- using condition::Binary<L, R>::Binary;
-
- operator std::string() const
- {
- return ">";
- }
-};
-
-template<typename L, typename R>
-Greater<L, R> operator>(Expression<L> expr, R r)
-{
- return {expr.value, r};
-}
-
-template<bool B, typename T = void>
-using enable_if_t = typename std::enable_if<B, T>::type;
-
-template<typename T>
-using is_condition = typename std::is_base_of<condition::Base, T>;
-
-template<typename L,
- typename R,
- typename = typename tsqb::enable_if_t<is_condition<L>::value
- && tsqb::is_condition<R>::value>>
-condition::And<L, R> operator&&(const L& l, const R& r)
-{
- return {l, r};
-}
-
-template<typename L,
- typename R,
- typename = typename tsqb::enable_if_t<is_condition<L>::value
- && tsqb::is_condition<R>::value>>
-condition::Or<L, R> operator||(const L& l, const R& r)
-{
- return {l, r};
-}
-
-} // namespace tsqb
+++ /dev/null
-/*
- * Copyright (c) 2017-present Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-/*
- * @file table-pack.hxx
- * @author Sangwan Kwon (sangwan.kwon@samsung.com)
- * @brief Tie diffirent types of tables
- */
-
-#pragma once
-
-#include <vector>
-#include <set>
-#include <string>
-
-namespace tsqb {
-namespace internal {
-
-template<typename... Base>
-class TablePack {
-public:
- virtual ~TablePack() = default;
-
- template<typename TableType>
- std::string getName(TableType&&) const noexcept { return std::string(); }
-
- template<typename ColumnType>
- std::string getColumnName(ColumnType&&) const noexcept { return std::string(); }
-};
-
-template<typename Front, typename... Rest>
-class TablePack<Front, Rest...> : public TablePack<Rest...> {
-public:
- using Table = Front;
-
- explicit TablePack(Front&& front, Rest&& ...rest);
- virtual ~TablePack() = default;
-
- TablePack(const TablePack&) = delete;
- TablePack& operator=(const TablePack&) = delete;
-
- TablePack(TablePack&&) = default;
- TablePack& operator=(TablePack&&) = default;
-
- template<typename TableType>
- std::string getName(TableType&& table) const noexcept;
-
- template<typename ColumnType>
- std::string getColumnName(ColumnType&& column) const noexcept;
-
-private:
- using Base = TablePack<Rest...>;
-
- Table table;
-};
-
-template<typename Front, typename... Rest>
-TablePack<Front, Rest...>::TablePack(Front&& front, Rest&& ...rest) :
- Base(std::forward<Rest>(rest)...), table(front)
-{
-}
-
-template<typename Front, typename... Rest>
-template<typename TableType>
-std::string TablePack<Front, Rest...>::getName(TableType&& table) const noexcept
-{
- if (this->table.compare(table))
- return this->table.name;
-
- return Base::template getName<TableType>(std::forward<TableType>(table));
-}
-
-template<typename Front, typename... Rest>
-template<typename ColumnType>
-std::string TablePack<Front, Rest...>::getColumnName(ColumnType&& column) const noexcept
-{
- using DecayColumnType = typename std::decay<ColumnType>::type;
- using DecayTableType = typename DecayColumnType::TableType;
- if (this->table.compare(DecayTableType())) {
- auto cname = this->table.getColumnName(column.type);
- return this->table.name + "." + cname;
- }
-
- return Base::template getColumnName<ColumnType>(std::forward<ColumnType>(column));
-}
-
-} // namespace internal
-} // namespace tsqb
+++ /dev/null
-/*
- * Copyright (c) 2017-present Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-/*
- * @file table.hxx
- * @author Sangwan Kwon (sangwan.kwon@samsung.com)
- * @brief Represent the table of SQL
- */
-
-#pragma once
-
-#include "crud.hxx"
-#include "column.hxx"
-#include "column-pack.hxx"
-#include "tuple-helper.hxx"
-#include "util.hxx"
-
-#include <vector>
-#include <string>
-#include <sstream>
-
-namespace tsqb {
-
-template<typename... Columns>
-class Table : public Crud<Table<Columns...>> {
-public:
- virtual ~Table() = default;
-
- Table(const Table&) = delete;
- Table& operator=(const Table&) = delete;
-
- Table(Table&&) = default;
- Table& operator=(Table&&) = default;
-
- // Functions for Crud
- template<typename Cs>
- std::set<std::string> getTableNames(Cs&& tuple) const noexcept;
- template<typename Cs>
- std::vector<std::string> getColumnNames(Cs&& tuple) const noexcept;
- template<typename That>
- std::string getTableName(That&& type) const noexcept;
- template<typename ColumnType>
- std::string getColumnName(ColumnType&& type) const noexcept;
-
- std::vector<std::string> getColumnNames(void) const noexcept;
-
- template<typename That>
- bool compare(const That& that) const noexcept;
-
- int size() const noexcept;
-
- operator std::string();
-
- std::string name;
- std::vector<std::string> cache;
-
-private:
- using ColumnPackType = internal::ColumnPack<Columns...>;
- using TableType = typename ColumnPackType::TableType;
-
- explicit Table(const std::string& name, ColumnPackType&& columnPack);
-
- template<typename ...Cs>
- friend Table<Cs...> make_table(const std::string& name, Cs&& ...columns);
-
- struct GetColumnNames {
- const ColumnPackType& columnPack;
- std::vector<std::string> names;
-
- GetColumnNames(const ColumnPackType& columnPack) : columnPack(columnPack) {}
-
- template <typename T>
- void operator()(T&& type)
- {
- auto name = this->columnPack.getName(std::forward<T>(type));
- if (!name.empty())
- names.emplace_back(name);
- }
- };
-
- ColumnPackType columnPack;
-};
-
-template<typename ...Columns>
-Table<Columns...> make_table(const std::string& name, Columns&& ...cs)
-{
- auto columnPack = internal::ColumnPack<Columns...>(std::forward<Columns>(cs)...);
- return Table<Columns...>(name, std::move(columnPack));
-}
-
-template<typename... Columns>
-Table<Columns...>::Table(const std::string& name, ColumnPackType&& columnPack)
- : name(name), columnPack(std::move(columnPack)) {}
-
-template<typename... Columns>
-template<typename Cs>
-std::set<std::string> Table<Columns...>::getTableNames(Cs&& tuple) const noexcept
-{
- return {this->name};
-}
-
-template<typename... Columns>
-template<typename Cs>
-std::vector<std::string> Table<Columns...>::getColumnNames(Cs&& tuple) const noexcept
-{
- GetColumnNames closure(this->columnPack);
- tuple_helper::for_each(std::forward<Cs>(tuple), closure);
-
- return closure.names;
-}
-
-template<typename... Columns>
-template<typename That>
-std::string Table<Columns...>::getTableName(That&& type) const noexcept
-{
- return this->name;
-}
-
-template<typename... Columns>
-template<typename ColumnType>
-std::string Table<Columns...>::getColumnName(ColumnType&& type) const noexcept
-{
- return this->columnPack.getName(std::forward<ColumnType>(type));
-}
-
-template<typename... Columns>
-std::vector<std::string> Table<Columns...>::getColumnNames(void) const noexcept
-{
- return this->columnPack.getNames();
-}
-
-template<typename... Columns>
-template<typename That>
-bool Table<Columns...>::compare(const That& that) const noexcept
-{
- using This = TableType;
- return type::compare(This(), that);
-}
-
-template<typename... Columns>
-Table<Columns...>::operator std::string()
-{
- std::stringstream ss;
- for (const auto& c : cache)
- ss << c << " ";
-
- this->cache.clear();
- return util::rtrim(ss.str());
-}
-
-template<typename... Columns>
-int Table<Columns...>::size() const noexcept
-{
- return this->columnPack.size();
-}
-
-} // namespace tsqb
+++ /dev/null
-/*
- * Copyright (c) 2017-present Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-/*
- * @file tuple-helper.hxx
- * @author Sangwan Kwon (sangwan.kwon@samsung.com)
- * @brief Iterator method for tuple
- */
-
-#pragma once
-
-#include <tuple>
-
-namespace tsqb {
-namespace tuple_helper {
-namespace internal {
-
-template<int n, typename T, typename C>
-class Iterator {
-public:
- Iterator(const T& tuple, C&& closure) {
- Iterator<n - 1, T, C> iter(tuple, std::forward<C>(closure));
- closure(std::get<n-1>(tuple));
- }
-};
-
-template<typename T, typename C>
-class Iterator<0, T, C> {
-public:
- Iterator(const T&, C&&) {}
-};
-
-} // namespace internal
-
-template<typename T, typename C>
-void for_each(const T& tuple, C&& closure)
-{
- using Iter = internal::Iterator<std::tuple_size<T>::value, T, C>;
- Iter iter(tuple, std::forward<C>(closure));
-}
-
-} // namspace tuple-hepler
-} // namspace tsqb
+++ /dev/null
-/*
- * Copyright (c) 2017-present Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-/*
- * @file type.hxx
- * @author Sangwan Kwon (sangwan.kwon@samsung.com)
- * @brief Make type safety with compile time checking
- */
-
-#pragma once
-
-#include <type_traits>
-
-namespace tsqb {
-namespace type {
-
-template<typename L, typename R>
-bool cast_compare(L l, R r)
-{
- return l == reinterpret_cast<L>(r);
-}
-
-template<typename L, typename R>
-bool compare(L l, R r)
-{
- return std::is_same<L, R>::value;
-}
-
-template<typename L, typename R>
-void assert_compare(L l, R r)
-{
- static_assert(std::is_same<L, R>::value, "Type is unsafe.");
-}
-
-} // namespace type
-} // namespace tsqb
+++ /dev/null
-/*
- * Copyright (c) 2017-present Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-/*
- * @file util.hxx
- * @author Sangwan Kwon (sangwan.kwon@samsung.com)
- */
-
-#pragma once
-
-#include <string>
-#include <algorithm>
-#include <cctype>
-
-namespace tsqb {
-namespace util {
-
-inline std::string rtrim(std::string&& s)
-{
- auto predicate = [](unsigned char c){ return !std::isspace(c); };
- auto base = std::find_if(s.rbegin(), s.rend(), predicate).base();
- s.erase(base, s.end());
- return s;
-}
-
-} // namespace util
-} // namespace tsqb
+++ /dev/null
-/*
- * Copyright (c) 2017-present Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-/*
- * @file tsqb-tests.cpp
- * @author Sangwan Kwon (sangwan.kwon@samsung.com)
- * @brief Testcases of tsqb
- */
-
-#include "tsqb.hxx"
-
-#include <gtest/gtest.h>
-
-using namespace tsqb;
-
-struct Admin {
- int id;
- std::string pkg;
- int uid;
- std::string key;
- int removable;
-};
-
-struct ManagedPolicy {
- int id;
- int aid;
- int pid;
- int value;
-};
-
-struct PolicyDefinition {
- int id;
- int scope;
- std::string name;
- int ivalue;
-};
-
-auto admin = make_table("admin", make_column("id", &Admin::id),
- make_column("pkg", &Admin::pkg),
- make_column("uid", &Admin::uid),
- make_column("key", &Admin::key),
- make_column("removable", &Admin::removable));
-
-auto managedPolicy = make_table("managed_policy",
- make_column("id", &ManagedPolicy::id),
- make_column("aid", &ManagedPolicy::aid),
- make_column("pid", &ManagedPolicy::pid),
- make_column("value", &ManagedPolicy::value));
-
-auto policyDefinition = make_table("policy_definition",
- make_column("id", &PolicyDefinition::id),
- make_column("scope", &PolicyDefinition::scope),
- make_column("name", &PolicyDefinition::name),
- make_column("ivalue", &PolicyDefinition::ivalue));
-
-auto db = make_database("dpm", admin, managedPolicy, policyDefinition);
-
-class TsqbTests : public testing::Test {};
-
-TEST_F(TsqbTests, SELECT)
-{
- std::string select1 = admin.select(&Admin::id, &Admin::pkg, &Admin::uid, &Admin::key);
- std::string select2 = admin.select(&Admin::id, &Admin::uid, &Admin::key);
-
- EXPECT_EQ(select1, "SELECT id, pkg, uid, key FROM admin");
- EXPECT_EQ(select2, "SELECT id, uid, key FROM admin");
-}
-
-TEST_F(TsqbTests, SELECT_ALL)
-{
- std::string select = admin.selectAll();
-
- EXPECT_EQ(select, "SELECT * FROM admin");
-}
-
-TEST_F(TsqbTests, SELECT_WHERE)
-{
- std::string select1 = admin.select(&Admin::uid, &Admin::key)
- .where(expr(&Admin::id) > 3);
- std::string select2 = admin.selectAll().where(expr(&Admin::uid) > 3);
- std::string select3 = admin.selectAll().where(expr(&Admin::uid) > 3 &&
- expr(&Admin::pkg) == "dpm");
- std::string select4 = admin.selectAll().where(expr(&Admin::uid) > 3 ||
- expr(&Admin::pkg) == "dpm");
-
- EXPECT_EQ(select1, "SELECT uid, key FROM admin WHERE id > ?");
- EXPECT_EQ(select2, "SELECT * FROM admin WHERE uid > ?");
- EXPECT_EQ(select3, "SELECT * FROM admin WHERE uid > ? AND pkg = ?");
- EXPECT_EQ(select4, "SELECT * FROM admin WHERE uid > ? OR pkg = ?");
-}
-
-TEST_F(TsqbTests, SELECT_DISTINCT)
-{
- std::string select = admin.select(distinct(&Admin::uid, &Admin::key))
- .where(expr(&Admin::id) > 3);
-
- EXPECT_EQ(select, "SELECT DISTINCT uid, key FROM admin WHERE id > ?");
-}
-
-TEST_F(TsqbTests, UPDATE)
-{
- int uid = 0, id = 1;
- std::string update1 = admin.update(&Admin::id, &Admin::pkg, &Admin::uid, &Admin::key);
- std::string update2 = admin.update(&Admin::key).where(expr(&Admin::uid) == uid &&
- expr(&Admin::id) == id);
- std::string update3 = admin.update(&Admin::key, &Admin::pkg)
- .where(expr(&Admin::uid) == 0 && expr(&Admin::id) == 1);
-
- EXPECT_EQ(update1, "UPDATE admin SET id = ?, pkg = ?, uid = ?, key = ?");
- EXPECT_EQ(update2, "UPDATE admin SET key = ? WHERE uid = ? AND id = ?");
- EXPECT_EQ(update3, "UPDATE admin SET key = ?, pkg = ? WHERE uid = ? AND id = ?");
-}
-
-TEST_F(TsqbTests, DELETE)
-{
- std::string delete1 = admin.remove();
- std::string delete2 = admin.remove().where(expr(&Admin::pkg) == "dpm" &&
- expr(&Admin::uid) == 3);
-
- EXPECT_EQ(delete1, "DELETE FROM admin");
- EXPECT_EQ(delete2, "DELETE FROM admin WHERE pkg = ? AND uid = ?");
-}
-
-TEST_F(TsqbTests, INSERT)
-{
- std::string insert1 = admin.insert(&Admin::id, &Admin::pkg, &Admin::uid, &Admin::key);
- std::string insert2 = admin.insert(&Admin::id, &Admin::pkg, &Admin::key);
-
- EXPECT_EQ(insert1, "INSERT INTO admin (id, pkg, uid, key) VALUES (?, ?, ?, ?)");
- EXPECT_EQ(insert2, "INSERT INTO admin (id, pkg, key) VALUES (?, ?, ?)");
-}
-
-TEST_F(TsqbTests, TYPE_SAFE)
-{
-/*
- * Below cause complie error since expression types are dismatch.
-
- std::string type_unsafe1 = admin.selectAll().where(expr(&Admin::uid) > "dpm");
- std::string type_unsafe2 = admin.selectAll().where(expr(&Admin::uid) == "dpm");
- std::string type_unsafe3 = admin.selectAll().where(expr(&Admin::pkg) == 3);
- int pkg = 3;
- std::string type_unsafe4 = admin.selectAll().where(expr(&Admin::pkg) < pkg);
- std::string type_unsafe5 = admin.remove().where(expr(&Admin::pkg) == "dpm" &&
- expr(&Admin::uid) == "dpm");
-*/
-}
-
-TEST_F(TsqbTests, MULTI_SELECT)
-{
- std::string multiSelect1 = db.select(&Admin::uid, &Admin::key,
- &ManagedPolicy::id, &ManagedPolicy::value);
- std::string multiSelect2 = db.select(&Admin::uid, &Admin::key,
- &ManagedPolicy::id, &ManagedPolicy::value)
- .where(expr(&Admin::uid) > 0 && expr(&ManagedPolicy::id) == 3);
-
- EXPECT_EQ(multiSelect1, "SELECT admin.uid, admin.key, managed_policy.id, "
- "managed_policy.value FROM admin, managed_policy");
- EXPECT_EQ(multiSelect2, "SELECT admin.uid, admin.key, managed_policy.id, "
- "managed_policy.value FROM admin, managed_policy "
- "WHERE admin.uid > ? AND managed_policy.id = ?");
-}
-
-TEST_F(TsqbTests, JOIN)
-{
- std::string join1 = db.select(&Admin::uid, &Admin::key)
- .join<PolicyDefinition>(condition::Join::LEFT_OUTER);
- std::string join2 = db.select(&Admin::uid, &Admin::key)
- .join<ManagedPolicy>(condition::Join::CROSS);
- std::string join3 = db.select(&ManagedPolicy::value)
- .join<PolicyDefinition>()
- .on(expr(&ManagedPolicy::pid) == expr(&PolicyDefinition::id))
- .join<Admin>()
- .on(expr(&ManagedPolicy::aid) == expr(&Admin::id))
- .where(expr(&ManagedPolicy::pid) == 99);
-
- EXPECT_EQ(join1, "SELECT admin.uid, admin.key FROM admin "
- "LEFT OUTER JOIN policy_definition");
- EXPECT_EQ(join2, "SELECT admin.uid, admin.key FROM admin "
- "CROSS JOIN managed_policy");
- EXPECT_EQ(join3, "SELECT managed_policy.value FROM managed_policy "
- "INNER JOIN policy_definition "
- "ON managed_policy.pid = policy_definition.id "
- "INNER JOIN admin ON managed_policy.aid = admin.id "
- "WHERE managed_policy.pid = ?");
-}
+++ /dev/null
-/*
- * Copyright (c) 2017-present Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-/*
- * @file tsqb.hxx
- * @author Sangwan Kwon (sangwan.kwon@samsung.com)
- * @brief TSQB is type-safe query builder
- */
-
-#pragma once
-
-#include "include/database.hxx"
-#include "include/table.hxx"
-#include "include/column.hxx"
-#include "include/expression.hxx"
-#include "include/condition.hxx"
-#include "include/util.hxx"