Skip to content

Commit

Permalink
introducing sqlite module
Browse files Browse the repository at this point in the history
  • Loading branch information
ionutrazvanionita committed Apr 3, 2015
1 parent 2682925 commit 0a2be49
Show file tree
Hide file tree
Showing 16 changed files with 2,992 additions and 0 deletions.
11 changes: 11 additions & 0 deletions modules/db_sqlite/Makefile
@@ -0,0 +1,11 @@
# $Id$
#
# WARNING: do not run this directly, it should be run by the master Makefile

include ../../Makefile.defs
auto_gen=
NAME=db_sqlite.so

LIBS+=-lsqlite3

include ../../Makefile.modules
117 changes: 117 additions & 0 deletions modules/db_sqlite/README
@@ -0,0 +1,117 @@
sqlite Module

Ionut-Razvan Ionita

<ionutionita@opensips.org>

Copyright © 2015 Voice Sistem SRL
Revision History
Revision $Revision: 5901 $ $Date$
__________________________________________________________

Table of Contents

1. Admin Guide

1.1. Overview
1.2. Dependencies

1.2.1. OpenSIPS Modules
1.2.2. External Libraries or Applications

1.3. Exported Parameters

1.3.1. alloc_limit (integer)
1.3.2. load_extension (string)

1.4. Exported Functions
1.5. Installation

List of Examples

1.1. Set alloc_limit parameter
1.2. Set db_sqlite_alloc_limit parameter

Chapter 1. Admin Guide

1.1. Overview

This is a module which provides SQLite support for OpenSIPS. It
implements the DB API defined in OpenSIPS.

1.2. Dependencies

1.2.1. OpenSIPS Modules

The following modules must be loaded before this module:
* No dependencies on other OpenSIPS modules.

1.2.2. External Libraries or Applications

The following libraries or applications must be installed
before running OpenSIPS with this module loaded:
* libsqlite3-dev - the development libraries of sqlite.

1.3. Exported Parameters

1.3.1. alloc_limit (integer)

Since the library does not support a function to return the
number of rows in a query, this number is obtained using
"count(*)" query. If we use multiple processes there is the
risk ,since "count(*)" query and the actual "select" query, the
number of rows in the result query to have changed, so realloc
will be needed if the number is bigger. Using alloc_limit
parameter you can specify the number with which the number of
allocated rows in the result is raised.

Default value is 10.

Example 1.1. Set alloc_limit parameter
...
modparam("db_sqlite", "alloc_limit", 25)
...

1.3.2. load_extension (string)

Since the library does not support a function to return the
number of rows in a query, this number is obtained using
"count(*)" query. If we use multiple processes there is the
risk ,since "count(*)" query and the actual "select" query, the
number of rows in the result query to have changed, so realloc
will be needed if the number is bigger. Using alloc_limit
parameter you can specify the number with which the number of
allocated rows in the result is raised. This parameter enables
extension loading, similiar to ".load" functionality in
sqlite3, extenions like sqlite3-pcre which enables REGEX
function. In order to use this functionality you must specify
the library path(.so file) and the entry point which represents
the function to be called by the sqlite library (read more at
sqlite load_extension official documentation), separated by ";"
delimiter. The entry point paramter can miss, so you won't need
to use the delimitier in this case.

Default no extenion is loaded.

Example 1.2. Set db_sqlite_alloc_limit parameter
...
modparam("db_sqlite", "load_extension", "/usr/lib/sqlite3/pcre.so")
modparam("db_sqlite", "load_extension", "/usr/lib/sqlite3/pcre.so;sqlite
3_extension_init")
...

1.4. Exported Functions

No function exported to be used from configuration file.

1.5. Installation

Because it dependes on an external library, the sqlite module
is not compiled and installed by default. You can use one of
the next options.
* - edit the "Makefile" and remove "db_sqlite" from
"excluded_modules" list. Then follow the standard procedure
to install OpenSIPS: "make all; make install".
* - from command line use: 'make all
include_modules="db_sqlite"; make install
include_modules="db_sqlite"'.
147 changes: 147 additions & 0 deletions modules/db_sqlite/db_sqlite.c
@@ -0,0 +1,147 @@
/**
*
* Copyright (C) 2015 OpenSIPS Foundation
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* opensips 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* History
* -------
* 2015-02-18 initial version (Ionut Ionita)
*/

#include "../../sr_module.h"
#include "../../db/db.h"
#include "../../db/db_cap.h"
#include "db_sqlite.h"
#include "dbase.h"

#include <sqlite3.h>
#define ALLOC_LIMIT 10
#define LDEXT_LIST_DELIM ';'

unsigned int db_sqlite_timeout_interval = 2; /* Default is 6 seconds */
unsigned int db_sliqte_exec_query_threshold = 0; /* Warning in case DB query
takes too long disabled by default*/
int db_sqlite_alloc_limit=ALLOC_LIMIT;



static int sqlite_mod_init(void);
static int db_sqlite_add_extension(modparam_t type, void *val);
struct db_sqlite_extension_list *extension_list=0;

/*
* MySQL database module interface
*/
static cmd_export_t cmds[] = {
{"db_bind_api", (cmd_function)db_sqlite_bind_api, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0}
};

/*
* Exported parameters
*/
static param_export_t params[] = {
{"alloc_limit", INT_PARAM, &db_sqlite_alloc_limit},
{"load_extension", STR_PARAM|USE_FUNC_PARAM,
(void *)db_sqlite_add_extension},
{0, 0, 0}
};


struct module_exports exports = {
"db_sqlite",
MOD_TYPE_SQLDB, /* class of this module */
MODULE_VERSION,
DEFAULT_DLFLAGS, /* dlopen flags */
NULL, /* OpenSIPS module dependencies */
cmds,
0, /* exported async functions */
params, /* module parameters */
0, /* exported statistics */
0, /* exported MI functions */
0, /* exported pseudo-variables */
0, /* extra processes */
sqlite_mod_init, /* module initialization function */
0, /* response function*/
0, /* destroy function */
0 /* per-child init function */
};

static int sqlite_mod_init(void)
{
return 0;
}


int db_sqlite_bind_api(const str* mod, db_func_t *dbb)
{
if(dbb==NULL)
return -1;

memset(dbb, 0, sizeof(db_func_t));

dbb->use_table = db_sqlite_use_table;
dbb->init = db_sqlite_init;
dbb->close = db_sqlite_close;
dbb->query = db_sqlite_query;
dbb->fetch_result = db_sqlite_fetch_result;
dbb->raw_query = db_sqlite_raw_query;
dbb->free_result = db_sqlite_free_result;
dbb->insert = db_sqlite_insert;
dbb->delete = db_sqlite_delete;
dbb->update = db_sqlite_update;
dbb->replace = db_sqlite_replace;
dbb->last_inserted_id = db_last_inserted_id;
dbb->insert_update = db_insert_update;
dbb->async_raw_query = NULL;
dbb->async_raw_resume = NULL;

dbb->cap = DB_CAP_ALL ^ DB_CAP_ASYNC_RAW_QUERY;
return 0;
}


static int db_sqlite_add_extension(modparam_t type, void *val)
{
struct db_sqlite_extension_list *node;

int len;

node=pkg_malloc(sizeof(struct db_sqlite_extension_list));
if (!node)
goto out;

len = strlen((char *)val);

node->ldpath=(char *)val;
node->entry_point=q_memchr(node->ldpath, LDEXT_LIST_DELIM, len);

if (node->entry_point) {
/* sqlite requires null terminated strings */
(node->entry_point++)[0] = '\0';
}

/* Reduce the overhead of introducing in the end */
node->next=extension_list;
extension_list=node;

return 0;
out:
LM_ERR("no more pkg mem\n");
return -1;
}
38 changes: 38 additions & 0 deletions modules/db_sqlite/db_sqlite.h
@@ -0,0 +1,38 @@
/**
*
* Copyright (C) 2015 OpenSIPS Foundation
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* opensips 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* History
* -------
* 2015-02-18 initial version (Ionut Ionita)
*/
#ifndef DB_SQLITE_H_
#define DB_SQLITE_H_

struct db_sqlite_extension_list {
char *ldpath;
char *entry_point;

struct db_sqlite_extension_list *next;
};

int db_sqlite_bind_api(const str* mod, db_func_t *dbb);

#endif

0 comments on commit 0a2be49

Please sign in to comment.