Skip to content

Commit 4a2812a

Browse files
committed
FS-11727: [mod_mariadb] Add MariaDB (MySQL) support
1 parent 65cd6ec commit 4a2812a

23 files changed

+1598
-4
lines changed

Freeswitch.2017.sln

+15
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Databases", "Databases", "{
584584
EndProject
585585
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mod_pgsql", "src\mod\databases\mod_pgsql\mod_pgsql.2017.vcxproj", "{1BA65811-5453-46F6-8190-9ECEEFEB7DF2}"
586586
EndProject
587+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mod_mariadb", "src\mod\databases\mod_mariadb\mod_mariadb.2017.vcxproj", "{0B612F84-7533-4DEC-AEDD-5C9CBCF15EAC}"
588+
EndProject
587589
Global
588590
GlobalSection(SolutionConfigurationPlatforms) = preSolution
589591
All|Win32 = All|Win32
@@ -2675,6 +2677,18 @@ Global
26752677
{1BA65811-5453-46F6-8190-9ECEEFEB7DF2}.Release|Win32.Build.0 = Release|Win32
26762678
{1BA65811-5453-46F6-8190-9ECEEFEB7DF2}.Release|x64.ActiveCfg = Release|x64
26772679
{1BA65811-5453-46F6-8190-9ECEEFEB7DF2}.Release|x64.Build.0 = Release|x64
2680+
{0B612F84-7533-4DEC-AEDD-5C9CBCF15EAC}.All|Win32.ActiveCfg = Release|Win32
2681+
{0B612F84-7533-4DEC-AEDD-5C9CBCF15EAC}.All|Win32.Build.0 = Release|Win32
2682+
{0B612F84-7533-4DEC-AEDD-5C9CBCF15EAC}.All|x64.ActiveCfg = Release|x64
2683+
{0B612F84-7533-4DEC-AEDD-5C9CBCF15EAC}.All|x64.Build.0 = Release|x64
2684+
{0B612F84-7533-4DEC-AEDD-5C9CBCF15EAC}.Debug|Win32.ActiveCfg = Debug|Win32
2685+
{0B612F84-7533-4DEC-AEDD-5C9CBCF15EAC}.Debug|Win32.Build.0 = Debug|Win32
2686+
{0B612F84-7533-4DEC-AEDD-5C9CBCF15EAC}.Debug|x64.ActiveCfg = Debug|x64
2687+
{0B612F84-7533-4DEC-AEDD-5C9CBCF15EAC}.Debug|x64.Build.0 = Debug|x64
2688+
{0B612F84-7533-4DEC-AEDD-5C9CBCF15EAC}.Release|Win32.ActiveCfg = Release|Win32
2689+
{0B612F84-7533-4DEC-AEDD-5C9CBCF15EAC}.Release|Win32.Build.0 = Release|Win32
2690+
{0B612F84-7533-4DEC-AEDD-5C9CBCF15EAC}.Release|x64.ActiveCfg = Release|x64
2691+
{0B612F84-7533-4DEC-AEDD-5C9CBCF15EAC}.Release|x64.Build.0 = Release|x64
26782692
EndGlobalSection
26792693
GlobalSection(SolutionProperties) = preSolution
26802694
HideSolutionNode = FALSE
@@ -2886,6 +2900,7 @@ Global
28862900
{B19AE6FC-BFFF-428D-B483-3BBEAECCC618} = {E72B5BCB-6462-4D23-B419-3AF1A4AC3D78}
28872901
{BC1FD72E-1CD5-4525-A7F5-17C5740BFDED} = {EB910B0D-F27D-4B62-B67B-DE834C99AC5B}
28882902
{1BA65811-5453-46F6-8190-9ECEEFEB7DF2} = {31C2761D-20E0-4BF8-98B9-E32F0D8DD6E1}
2903+
{0B612F84-7533-4DEC-AEDD-5C9CBCF15EAC} = {31C2761D-20E0-4BF8-98B9-E32F0D8DD6E1}
28892904
EndGlobalSection
28902905
GlobalSection(ExtensibilityGlobals) = postSolution
28912906
SolutionGuid = {09840DE7-9208-45AA-9667-1A71EE93BD1E}

build/modules.conf.in

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ codecs/mod_opus
7979
#codecs/mod_silk
8080
#codecs/mod_siren
8181
#codecs/mod_theora
82+
#databases/mod_mariadb
8283
databases/mod_pgsql
8384
dialplans/mod_dialplan_asterisk
8485
#dialplans/mod_dialplan_directory

build/modules.conf.most

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ codecs/mod_sangoma_codec
7676
codecs/mod_silk
7777
codecs/mod_siren
7878
codecs/mod_theora
79+
#databases/mod_mariadb
7980
databases/mod_pgsql
8081
dialplans/mod_dialplan_asterisk
8182
dialplans/mod_dialplan_directory
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<configuration name="pre_load_modules.conf" description="Modules">
22
<modules>
33
<!-- Databases -->
4+
<!-- <load module="mod_mariadb"/> -->
45
<load module="mod_pgsql"/>
56
</modules>
67
</configuration>

conf/rayo/autoload_configs/switch.conf.xml

+1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
-->
169169
<!-- <param name="core-db-dsn" value="pgsql://hostaddr=127.0.0.1 dbname=freeswitch user=freeswitch password='' options='-c client_min_messages=NOTICE' application_name='freeswitch'" /> -->
170170
<!-- <param name="core-db-dsn" value="postgresql://freeswitch:@127.0.0.1/freeswitch?options=-c%20client_min_messages%3DNOTICE&application_name=freeswitch" /> -->
171+
<!-- <param name="core-db-dsn" value="mariadb://Server=localhost;Database=freeswitch;Uid=freeswitch;Pwd=pass;" /> -->
171172
<!-- <param name="core-db-dsn" value="dsn:username:password" /> -->
172173
<!--
173174
Allow to specify the sqlite db at a different location (In this example, move it to ramdrive for
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<configuration name="pre_load_modules.conf" description="Modules">
22
<modules>
33
<!-- Databases -->
4+
<!-- <load module="mod_mariadb"/> -->
45
<load module="mod_pgsql"/>
56
</modules>
67
</configuration>

conf/testing/autoload_configs/switch.conf.xml

+1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@
173173
-->
174174
<!-- <param name="core-db-dsn" value="pgsql://hostaddr=127.0.0.1 dbname=freeswitch user=freeswitch password='' options='-c client_min_messages=NOTICE'" /> -->
175175
<!-- <param name="core-db-dsn" value="postgresql://freeswitch:@127.0.0.1/freeswitch?options=-c%20client_min_messages%3DNOTICE" /> -->
176+
<!-- <param name="core-db-dsn" value="mariadb://Server=localhost;Database=freeswitch;Uid=freeswitch;Pwd=pass;" /> -->
176177
<!-- <param name="core-db-dsn" value="dsn:username:password" /> -->
177178
<!--
178179
Allow to specify the sqlite db at a different location (In this example, move it to ramdrive for
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<configuration name="pre_load_modules.conf" description="Modules">
22
<modules>
33
<!-- Databases -->
4+
<!-- <load module="mod_mariadb"/> -->
45
<load module="mod_pgsql"/>
56
</modules>
67
</configuration>

conf/vanilla/autoload_configs/switch.conf.xml

+1
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@
182182
-->
183183
<!-- <param name="core-db-dsn" value="pgsql://hostaddr=127.0.0.1 dbname=freeswitch user=freeswitch password='' options='-c client_min_messages=NOTICE'" /> -->
184184
<!-- <param name="core-db-dsn" value="postgresql://freeswitch:@127.0.0.1/freeswitch?options=-c%20client_min_messages%3DNOTICE" /> -->
185+
<!-- <param name="core-db-dsn" value="mariadb://Server=localhost;Database=freeswitch;Uid=freeswitch;Pwd=pass;" /> -->
185186
<!-- <param name="core-db-dsn" value="dsn:username:password" /> -->
186187
<!--
187188
Allow to specify the sqlite db at a different location (In this example, move it to ramdrive for

configure.ac

+6
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,11 @@ AC_SUBST([POSTGRESQL_CFLAGS], [$POSTGRESQL_CFLAGS])
705705
AC_SUBST([POSTGRESQL_LDFLAGS], [$POSTGRESQL_LDFLAGS])
706706
AC_SUBST([POSTGRESQL_LIBDIR], [$POSTGRESQL_LIBDIR])
707707

708+
709+
PKG_CHECK_MODULES([MARIADB], [libmariadb >= 3.0.9],[
710+
AM_CONDITIONAL([HAVE_MARIADB],[true])],[
711+
AC_MSG_RESULT([no]); AM_CONDITIONAL([HAVE_MARIADB],[false])])
712+
708713
AC_ARG_ENABLE(deprecated-core-db-events,
709714
[AS_HELP_STRING([--enable-deprecated-core-db-events], [Keep deprecated core db events])],,[enable_deprecated_core_db_events="no"])
710715

@@ -1925,6 +1930,7 @@ AC_CONFIG_FILES([Makefile
19251930
src/mod/codecs/mod_siren/Makefile
19261931
src/mod/codecs/mod_skel_codec/Makefile
19271932
src/mod/codecs/mod_theora/Makefile
1933+
src/mod/databases/mod_mariadb/Makefile
19281934
src/mod/databases/mod_pgsql/Makefile
19291935
src/mod/dialplans/mod_dialplan_asterisk/Makefile
19301936
src/mod/dialplans/mod_dialplan_directory/Makefile

debian/bootstrap.sh

+2
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,7 @@ Depends: \${misc:Depends}, freeswitch (= \${binary:Version}),
623623
freeswitch-mod-memcache (= \${binary:Version}),
624624
freeswitch-mod-nibblebill (= \${binary:Version}),
625625
freeswitch-mod-oreka (= \${binary:Version}),
626+
freeswitch-mod-mariadb (= \${binary:Version}),
626627
freeswitch-mod-pgsql (= \${binary:Version}),
627628
freeswitch-mod-png (= \${binary:Version}),
628629
freeswitch-mod-redis (= \${binary:Version}),
@@ -863,6 +864,7 @@ Depends: \${misc:Depends}, freeswitch (= \${binary:Version}),
863864
freeswitch-mod-memcache-dbg (= \${binary:Version}),
864865
freeswitch-mod-nibblebill-dbg (= \${binary:Version}),
865866
freeswitch-mod-oreka-dbg (= \${binary:Version}),
867+
freeswitch-mod-mariadb-dbg (= \${binary:Version}),
866868
freeswitch-mod-pgsql-dbg (= \${binary:Version}),
867869
freeswitch-mod-png-dbg (= \${binary:Version}),
868870
freeswitch-mod-redis-dbg (= \${binary:Version}),

debian/control-modules

+5
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,11 @@ Description: Adds mod_yuv
393393

394394
## mod/databases
395395

396+
Module: databases/mod_mariadb
397+
Description: Adds mod_mariadb
398+
Adds mod_mariadb.
399+
Build-Depends: libmariadb-dev
400+
396401
Module: databases/mod_pgsql
397402
Description: Adds mod_pgsql
398403
Adds mod_pgsql.

freeswitch.spec

+15-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# spec file for package freeswitch
44
#
55
# includes module(s): freeswitch-devel freeswitch-codec-passthru-amr freeswitch-codec-passthru-amrwb freeswitch-codec-passthru-g729
6-
# freeswitch-codec-passthru-g7231 freeswitch-lua freeswitch-pgsql freeswitch-perl freeswitch-python freeswitch-v8 freeswitch-signalwire
6+
# freeswitch-codec-passthru-g7231 freeswitch-lua freeswitch-mariadb freeswitch-pgsql freeswitch-perl freeswitch-python freeswitch-v8 freeswitch-signalwire
77
# freeswitch-lan-de freeswitch-lang-en freeswitch-lang-fr freeswitch-lang-hu freeswitch-lang-ru freeswitch-freetdm
88
# and others
99
#
@@ -842,6 +842,16 @@ Theora Video Codec support for FreeSWITCH open source telephony platform.
842842
# FreeSWITCH Database Modules
843843
######################################################################################################################
844844

845+
%package database-mariadb
846+
Summary: MariaDB native support for FreeSWITCH
847+
Group: System/Libraries
848+
Requires: %{name} = %{version}-%{release}
849+
Requires: mariadb-connector-c
850+
BuildRequires: mariadb-connector-c-devel
851+
852+
%description database-mariadb
853+
MariaDB native support for FreeSWITCH.
854+
845855
%package database-pgsql
846856
Summary: PostgreSQL native support for FreeSWITCH
847857
Group: System/Libraries
@@ -1534,7 +1544,7 @@ CODECS_MODULES+="codecs/mod_sangoma_codec"
15341544
# Database Modules
15351545
#
15361546
######################################################################################################################
1537-
DATABASES_MODULES="databases/mod_pgsql"
1547+
DATABASES_MODULES="databases/mod_mariadb databases/mod_pgsql"
15381548

15391549
######################################################################################################################
15401550
#
@@ -2290,6 +2300,9 @@ fi
22902300
#
22912301
######################################################################################################################
22922302

2303+
%files database-mariadb
2304+
%{MODINSTDIR}/mod_mariadb.so*
2305+
22932306
%files database-pgsql
22942307
%{MODINSTDIR}/mod_pgsql.so*
22952308

libs/.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -862,4 +862,5 @@ libks-*/
862862
libks*
863863
signalwire-client-c-*/
864864
signalwire-client-c-*
865-
865+
mariadb-connector-c-*/
866+
mariadb-connector-c-*
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
include $(top_srcdir)/build/modmake.rulesam
2+
MODNAME=mod_mariadb
3+
4+
if HAVE_MARIADB
5+
6+
mod_LTLIBRARIES = mod_mariadb.la
7+
mod_mariadb_la_SOURCES = mariadb_dsn.cpp mod_mariadb.c
8+
mod_mariadb_la_CFLAGS = $(AM_CFLAGS) $(MARIADB_CFLAGS)
9+
mod_mariadb_la_CXXFLAGS = -std=c++11 $(MARIADB_CFLAGS)
10+
mod_mariadb_la_LIBADD = $(switch_builddir)/libfreeswitch.la $(MARIADB_LIBS)
11+
mod_mariadb_la_LDFLAGS = -avoid-version -module -no-undefined -shared
12+
13+
else
14+
install: error
15+
all: error
16+
error:
17+
$(error You must install libmariadb-dev to build mod_mariadb. MariaDB supports non-blocking operations starting with version 5.5.21. libmariadb version should be >= 3.0.9 to support MySQL 8.0)
18+
endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/*
2+
* mod_mariadb for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3+
* Copyright (C) 2019, Andrey Volk <[email protected]>
4+
*
5+
* Version: MPL 1.1
6+
*
7+
* The contents of this file are subject to the Mozilla Public License Version
8+
* 1.1 (the "License"); you may not use this file except in compliance with
9+
* the License. You may obtain a copy of the License at
10+
* http://www.mozilla.org/MPL/
11+
*
12+
* Software distributed under the License is distributed on an "AS IS" basis,
13+
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14+
* for the specific language governing rights and limitations under the
15+
* License.
16+
*
17+
* The Original Code is ported from FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18+
*
19+
* The Initial Developer of the Original Code is
20+
* Anthony Minessale II <[email protected]>
21+
* Portions created by the Initial Developer are Copyright (C)
22+
* the Initial Developer. All Rights Reserved.
23+
*
24+
* Contributor(s):
25+
* Andrey Volk <[email protected]>
26+
*
27+
* mariadb_dsn.cpp -- Connection string parser for MariaDB FreeSWITCH module
28+
*
29+
*/
30+
#include <switch.h>
31+
#include "mariadb_dsn.hpp"
32+
33+
#include <string>
34+
#include <iterator>
35+
#include <vector>
36+
#include <unordered_map>
37+
#include <regex>
38+
#include <algorithm>
39+
#include <sstream>
40+
41+
class mariadb_dsn {
42+
43+
std::string _host = "localhost";
44+
std::string _user;
45+
std::string _passwd;
46+
std::string _db;
47+
int _port = 3306;
48+
std::string _unix_socket;
49+
std::string _character_set;
50+
unsigned long _clientflag;
51+
52+
public:
53+
54+
template<typename Out>
55+
void split(const std::string &s, char delim, Out result) {
56+
std::stringstream ss(s);
57+
std::string item;
58+
while (std::getline(ss, item, delim)) {
59+
*(result++) = item;
60+
}
61+
}
62+
63+
std::vector<std::string> split(const std::string &s, char delim) {
64+
std::vector<std::string> elems;
65+
split(s, delim, std::back_inserter(elems));
66+
return elems;
67+
}
68+
69+
mariadb_dsn(MYSQL *mysql, const char *dsn, unsigned long clientflag)
70+
{
71+
_clientflag = clientflag;
72+
73+
if (dsn) {
74+
std::vector<std::string> params = split(std::string(dsn), ';');
75+
76+
for (auto &param : params) {
77+
std::vector<std::string> pair = split(param, '=');
78+
if (pair.size() >= 2) {
79+
std::string key = std::regex_replace(pair[0], std::regex("^ +| +$|( ) +"), "$1");
80+
std::transform(key.begin(), key.end(), key.begin(), ::tolower);
81+
std::string value = pair[1];
82+
83+
if ("server" == key || "host" == key) {
84+
_host = value;
85+
} else if ("uid" == key || "user" == key || "username" == key) {
86+
_user = value;
87+
} else if ("pwd" == key || "passwd" == key || "password" == key) {
88+
_passwd = value;
89+
} else if ("database" == key || "db" == key) {
90+
_db = value;
91+
} else if ("port" == key) {
92+
_port = std::stoi(value);
93+
} else if ("option" == key || "options" == key) {
94+
unsigned long option;
95+
std::stringstream(value) >> option;
96+
_clientflag |= option;
97+
} else if ("charset" == key) {
98+
std::string charset = std::regex_replace(value, std::regex("^ +| +$|( ) +"), "$1");
99+
int err = mysql_optionsv(mysql, MYSQL_SET_CHARSET_NAME, (void *)charset.c_str());
100+
if (err) {
101+
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mysql_optionsv returned an error [MYSQL_SET_CHARSET_NAME=%s]: %s\n", charset.c_str(), mysql_error(mysql));
102+
}
103+
}
104+
}
105+
}
106+
107+
}
108+
}
109+
110+
const char* host() const
111+
{
112+
return _host.c_str();
113+
}
114+
115+
const char* user() const
116+
{
117+
return _user.c_str();
118+
}
119+
120+
const char* passwd() const
121+
{
122+
return _passwd.c_str();
123+
}
124+
125+
const char* db() const
126+
{
127+
return _db.c_str();
128+
}
129+
130+
const int port() const
131+
{
132+
return _port;
133+
}
134+
135+
const char* unix_socket() const
136+
{
137+
return ("" == _unix_socket) ? NULL : _unix_socket.c_str();
138+
}
139+
140+
unsigned long clientflag()
141+
{
142+
return _clientflag;
143+
}
144+
};
145+
146+
MYSQL* STDCALL mysql_dsn_connect(MYSQL *mysql, const char *connection_string, unsigned long clientflag)
147+
{
148+
mariadb_dsn dsn(mysql, connection_string, clientflag);
149+
return mysql_real_connect(mysql, dsn.host(), dsn.user(), dsn.passwd(), dsn.db(), dsn.port(), dsn.unix_socket(), dsn.clientflag());
150+
}
151+
152+
/* For Emacs:
153+
* Local Variables:
154+
* mode:c
155+
* indent-tabs-mode:t
156+
* tab-width:4
157+
* c-basic-offset:4
158+
* End:
159+
* For VIM:
160+
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
161+
*/

0 commit comments

Comments
 (0)