aboutsummaryrefslogtreecommitdiff
path: root/src/connection.cpp
diff options
context:
space:
mode:
authorRichard Knight <q@1bpm.net>2020-03-17 18:47:36 +0000
committerRichard Knight <q@1bpm.net>2020-03-17 18:47:36 +0000
commitc3853f6dd796615f332fefcaaaf563794d867ee4 (patch)
treebe3aa506a3631e9261a68894a9fad26ccd911f80 /src/connection.cpp
downloadcsound-sqldb-c3853f6dd796615f332fefcaaaf563794d867ee4.tar.gz
csound-sqldb-c3853f6dd796615f332fefcaaaf563794d867ee4.tar.bz2
csound-sqldb-c3853f6dd796615f332fefcaaaf563794d867ee4.zip
initial
Diffstat (limited to 'src/connection.cpp')
-rw-r--r--src/connection.cpp218
1 files changed, 218 insertions, 0 deletions
diff --git a/src/connection.cpp b/src/connection.cpp
new file mode 100644
index 0000000..5ac0aa1
--- /dev/null
+++ b/src/connection.cpp
@@ -0,0 +1,218 @@
+/*
+ connection.cpp
+ Copyright (C) 2019 Richard Knight
+
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 3 of the License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with this program; if not, write to the Free Software Foundation,
+ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+ */
+
+#include <plugin.h>
+#include "connection.h"
+
+
+const char* badDatabase = "database type not supported";
+const char* badConnection = "connection not open";
+
+// tried to do this with templates, failed with various approaches
+// hence the copious number of switches etc (horrible?). Due a revisit/refactor.
+
+void ConnectionData::Init(csnd::Csound* csound, LoginData* login) {
+ type = login->dbType;
+ switch (type) {
+#ifdef BUILD_POSTGRES
+ case POSTGRES:
+ postgres = (PostgresConnection*) csound->malloc(sizeof(PostgresConnection));
+ postgres->Init(csound, login);
+ break;
+#endif
+#ifdef BUILD_SQLITE
+ case SQLITE:
+ sqlite = (SqliteConnection*) csound->malloc(sizeof(SqliteConnection));
+ sqlite->Init(csound, login);
+ break;
+#endif
+#ifdef BUILD_MYSQL
+ case MYSQL:
+ mysql = (MySQLConnection*) csound->malloc(sizeof(MySQLConnection));
+ mysql->Init(csound, login);
+ break;
+#endif
+ default:
+ throw std::runtime_error(badDatabase);
+ }
+ open = true;
+}
+
+void ConnectionData::Close(csnd::Csound* csound) {
+ if (!open) {
+ throw std::runtime_error(badConnection);
+ }
+
+ switch (type) {
+#ifdef BUILD_POSTGRES
+ case POSTGRES:
+ postgres->Close(csound);
+ break;
+#endif
+#ifdef BUILD_SQLITE
+ case SQLITE:
+ sqlite->Close(csound);
+ break;
+#endif
+#ifdef BUILD_MYSQL
+ case MYSQL:
+ mysql->Close(csound);
+ break;
+#endif
+ default:
+ throw std::runtime_error(badDatabase);
+ }
+ open = false;
+}
+
+void ConnectionData::Exec(char* sql) {
+ if (!open) {
+ throw std::runtime_error(badConnection);
+ }
+
+ switch (type) {
+#ifdef BUILD_POSTGRES
+ case POSTGRES:
+ postgres->Exec(sql);
+ break;
+#endif
+#ifdef BUILD_SQLITE
+ case SQLITE:
+ sqlite->Exec(sql);
+ break;
+#endif
+#ifdef BUILD_MYSQL
+ case MYSQL:
+ mysql->Exec(sql);
+ break;
+#endif
+ default:
+ throw std::runtime_error(badDatabase);
+ }
+}
+
+MYFLT ConnectionData::Scalar(char* sql, int row=0, int col=0) {
+ if (!open) {
+ throw std::runtime_error(badConnection);
+ }
+
+ switch (type) {
+#ifdef BUILD_POSTGRES
+ case POSTGRES:
+ return postgres->Scalar(sql, row, col);
+ break;
+#endif
+#ifdef BUILD_SQLITE
+ case SQLITE:
+ return sqlite->Scalar(sql, row, col);
+ break;
+#endif
+#ifdef BUILD_MYSQL
+ case MYSQL:
+ return mysql->Scalar(sql, row, col);
+ break;
+#endif
+ default:
+ throw std::runtime_error(badDatabase);
+ }
+
+}
+
+char* ConnectionData::ScalarString(char* sql, int row=0, int col=0) {
+ if (!open) {
+ throw std::runtime_error(badConnection);
+ }
+
+ switch (type) {
+#ifdef BUILD_POSTGRES
+ case POSTGRES:
+ return postgres->ScalarString(sql, row, col);
+ break;
+#endif
+#ifdef BUILD_SQLITE
+ case SQLITE:
+ return sqlite->ScalarString(sql, row, col);
+ break;
+#endif
+#ifdef BUILD_MYSQL
+ case MYSQL:
+ return mysql->ScalarString(sql, row, col);
+ break;
+#endif
+ default:
+ throw std::runtime_error(badDatabase);
+ }
+
+}
+
+void ConnectionData::ArrayQuery(char* sql, csnd::Csound* csound, ARRAYDAT* array) {
+ if (!open) {
+ throw std::runtime_error(badConnection);
+ }
+
+ switch (type) {
+#ifdef BUILD_POSTGRES
+ case POSTGRES:
+ return postgres->ArrayQuery(sql, csound, array);
+ break;
+#endif
+#ifdef BUILD_SQLITE
+ case SQLITE:
+ return sqlite->ArrayQuery(sql, csound, array);
+ break;
+#endif
+#ifdef BUILD_MYSQL
+ case MYSQL:
+ return mysql->ArrayQuery(sql, csound, array);
+ break;
+#endif
+ default:
+ throw std::runtime_error(badDatabase);
+ }
+
+}
+
+void ConnectionData::ArrayQueryString(char* sql, csnd::Csound* csound, ARRAYDAT* array) {
+ if (!open) {
+ throw std::runtime_error(badConnection);
+ }
+
+ switch (type) {
+#ifdef BUILD_POSTGRES
+ case POSTGRES:
+ return postgres->ArrayQueryString(sql, csound, array);
+ break;
+#endif
+#ifdef BUILD_SQLITE
+ case SQLITE:
+ return sqlite->ArrayQueryString(sql, csound, array);
+ break;
+#endif
+#ifdef BUILD_MYSQL
+ case MYSQL:
+ return mysql->ArrayQueryString(sql, csound, array);
+ break;
+#endif
+ default:
+ throw std::runtime_error(badDatabase);
+ }
+
+}