summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibSQL/Result.cpp
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2022-02-09 16:02:49 -0500
committerLinus Groh <mail@linusgroh.de>2022-02-10 12:20:35 +0000
commit373b46730230486210adfcc9a0c9484921f8ade0 (patch)
treebe18670ec72bc527fe18ba9c236eacf7147ca7c8 /Userland/Libraries/LibSQL/Result.cpp
parentaff17a2fbd8ee26a5ee5b78425015de842d7b085 (diff)
downloadserenity-373b46730230486210adfcc9a0c9484921f8ade0.zip
LibSQL+SQLServer: Move LibSQL/SQLResult.[h,cpp] to LibSQL/Result.[h,cpp]
Rename the file to match the new class name.
Diffstat (limited to 'Userland/Libraries/LibSQL/Result.cpp')
-rw-r--r--Userland/Libraries/LibSQL/Result.cpp71
1 files changed, 71 insertions, 0 deletions
diff --git a/Userland/Libraries/LibSQL/Result.cpp b/Userland/Libraries/LibSQL/Result.cpp
new file mode 100644
index 0000000000..104c8ee4d3
--- /dev/null
+++ b/Userland/Libraries/LibSQL/Result.cpp
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2022, Jan de Visser <jan@de-visser.net>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/StringBuilder.h>
+#include <LibSQL/Result.h>
+
+namespace SQL {
+
+void Result::insert(Tuple const& row, Tuple const& sort_key)
+{
+ if (!m_result_set.has_value())
+ m_result_set = ResultSet {};
+ m_result_set->insert_row(row, sort_key);
+}
+
+void Result::limit(size_t offset, size_t limit)
+{
+ VERIFY(has_results());
+
+ if (offset > 0) {
+ if (offset > m_result_set->size()) {
+ m_result_set->clear();
+ return;
+ }
+
+ m_result_set->remove(0, offset);
+ }
+
+ if (m_result_set->size() > limit)
+ m_result_set->remove(limit, m_result_set->size() - limit);
+}
+
+String Result::error_string() const
+{
+ VERIFY(is_error());
+
+ StringView error_code;
+ StringView error_description;
+
+ switch (m_error) {
+#undef __ENUMERATE_SQL_ERROR
+#define __ENUMERATE_SQL_ERROR(error, description) \
+ case SQLErrorCode::error: \
+ error_code = #error##sv; \
+ error_description = description##sv; \
+ break;
+ ENUMERATE_SQL_ERRORS(__ENUMERATE_SQL_ERROR)
+#undef __ENUMERATE_SQL_ERROR
+ default:
+ VERIFY_NOT_REACHED();
+ }
+
+ StringBuilder builder;
+ builder.appendff("{}: ", error_code);
+
+ if (m_error_message.has_value()) {
+ if (error_description.find("{}"sv).has_value())
+ builder.appendff(error_description, *m_error_message);
+ else
+ builder.appendff("{}: {}", error_description, *m_error_message);
+ } else {
+ builder.append(error_description);
+ }
+
+ return builder.build();
+}
+
+}