[asterisk-commits] ivaxer: branch ivaxer/ast_storage r275588 - /team/ivaxer/ast_storage/res/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Jul 12 10:08:23 CDT 2010


Author: ivaxer
Date: Mon Jul 12 10:08:20 2010
New Revision: 275588

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=275588
Log:
implemented the put() function of the odbc storage backend

Modified:
    team/ivaxer/ast_storage/res/res_storage_odbc.c

Modified: team/ivaxer/ast_storage/res/res_storage_odbc.c
URL: http://svnview.digium.com/svn/asterisk/team/ivaxer/ast_storage/res/res_storage_odbc.c?view=diff&rev=275588&r1=275587&r2=275588
==============================================================================
--- team/ivaxer/ast_storage/res/res_storage_odbc.c (original)
+++ team/ivaxer/ast_storage/res/res_storage_odbc.c Mon Jul 12 10:08:20 2010
@@ -74,6 +74,16 @@
 	const char *sql;
 };
 
+struct put_query_data {
+	char *sql;
+	char *pathname;
+	char *objectname;
+	char *ext;
+	void *data;
+	SQLLEN data_len;
+	SQLLEN data_ind;
+};
+
 static const struct ast_storage_be odbc_se;
 
 static struct ast_storage_fileobject *create_fileobject(void) {
@@ -431,9 +441,108 @@
 	return NULL;
 }
 
-static int se_put(struct ast_storage *st, struct ast_storage_fileobject *fo)
-{
-	return -1;
+
+static SQLHSTMT execute_put_query(struct odbc_obj *obj, void *arg) {
+	SQLRETURN ret;
+	SQLHSTMT stmt;
+	struct put_query_data *pdata = (struct put_query_data *) arg;
+
+
+	ret = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
+	if (!SQL_SUCCEEDED(ret)) {
+		ast_log(LOG_WARNING, "SQL Alloc Handle failed!");
+		return NULL;
+	}
+
+	SQLBindParameter(stmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(pdata->pathname), 0, (void *)pdata->pathname, 0, NULL);
+	SQLBindParameter(stmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(pdata->objectname), 0, (void *)pdata->objectname, 0, NULL);
+	SQLBindParameter(stmt, 3, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(pdata->ext), 0, (void *)pdata->ext, 0, NULL);
+	SQLBindParameter(stmt, 4, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_LONGVARBINARY, pdata->data_len, 0, (void *)pdata->data, 0, &pdata->data_ind);
+
+	ret = SQLExecDirect(stmt, (unsigned char *)pdata->sql, SQL_NTS);
+	if(!SQL_SUCCEEDED(ret)) {
+		ast_log(LOG_WARNING, "SQL Exec Direct failed!");
+		SQLFreeHandle(SQL_HANDLE_STMT, stmt);
+		return NULL;
+	}
+
+	return stmt;
+}
+
+static int se_put(struct ast_storage *st, struct ast_storage_fileobject *fo, const char *exts)
+{
+	SQLHSTMT stmt;
+	struct odbc_storage_pvt *ost = (struct odbc_storage_pvt *) st->storage_pvt;
+	struct ast_storage_fileinst *fi;
+	char sql[128], ext[16];
+	struct put_query_data pdata;
+	int fd, fdlen;
+	void *fdm;
+	int putret = -1;
+
+
+	if (!fo) {
+		ast_log(LOG_WARNING, "fileobject is null!\n");
+		return -1;
+	}
+
+	if (exts) {
+		char *ptr;
+		ast_copy_string(ext, exts, sizeof(ext));
+		ptr = strchr(ext, '|');
+		if (ptr) {
+			*ptr = '\0';
+		}
+	}
+
+	AST_RWLIST_RDLOCK(&fo->files);
+	AST_RWLIST_TRAVERSE(&fo->files, fi, list) {
+		if (!strcasecmp(fi->ext, ext) || !exts) {
+			break;
+		}
+	}
+	AST_RWLIST_UNLOCK(&fo->files);
+
+	if(!fi) {
+		ast_log(LOG_WARNING, "Unable to find file instance for put()\n");
+		return -1;
+	}
+
+	fd = open(fi->localfile, O_RDONLY);
+	if (fd == -1) {
+		ast_log(LOG_WARNING, "Unable to open file '%s': [%d]: %s\n", fi->localfile, errno, strerror(errno));
+		return -1;
+	}
+
+	fdlen = lseek(fd, 0, SEEK_END);
+
+	fdm = mmap(NULL, fdlen, PROT_READ, MAP_SHARED, fd, 0);
+	if (fdm == MAP_FAILED) {
+		ast_log(LOG_WARNING, "Unable to map file '%s': [%d]: %s\n", fi->localfile, errno, strerror(errno));
+		return -1;
+	}
+
+	snprintf(sql, sizeof(sql), "INSERT INTO %s (pathname,filename,extension,recording) VALUES (?,?,?,?)", ost->tablename);
+
+	pdata.sql = sql;
+	pdata.pathname = ost->pathname;
+	pdata.objectname = fo->objectname;
+	pdata.ext = fi->ext;
+	pdata.data = fdm;
+	pdata.data_len = pdata.data_ind = fdlen;
+
+	stmt = ast_odbc_direct_execute(ost->conn, execute_put_query, (void *) &pdata);
+	if(!stmt) {
+		ast_log(LOG_WARNING, "SQL Execute failed!");
+		goto error;
+	}
+
+	putret = 0;
+error:
+	SQLFreeHandle(SQL_HANDLE_STMT, stmt);
+	munmap(fdm, fdlen);
+	close(fd);
+	return 0;
 }
 
 static int se_del(struct ast_storage *st, struct ast_storage_fileobject *fo)
@@ -573,6 +682,53 @@
 
         return AST_TEST_PASS;
 }
+
+AST_TEST_DEFINE(put_test)
+{
+	struct ast_storage *st;
+	struct ast_storage_fileobject *fo;
+//	struct ast_storage_fileinst *fi;
+	int ret;
+	char uri[] = "asterisk-storage-test/storage/testpath";
+
+        switch (cmd) {
+        case TEST_INIT:
+                info->name = "put_test";
+                info->category = "main/storage/odbc";
+                info->summary = "get test";
+                info->description =
+                        "Tests the put function";
+                return AST_TEST_NOT_RUN;
+        case TEST_EXECUTE:
+                break;
+        }
+
+	st = se_create(uri);
+	if(!st) {
+		return AST_TEST_FAIL;
+	}
+
+	fo = se_get(st, "test", "wav");
+	if(!fo) {
+		return AST_TEST_FAIL;
+	}
+
+	fo->objectname[0] = 'g';
+
+	ret = se_put(st, fo, NULL);
+	if (ret) {
+		return AST_TEST_FAIL;
+	}
+
+	ast_storage_fileobject_release(fo);
+
+	ret = se_release(st);
+	if(ret) {
+		return AST_TEST_FAIL;
+	}
+
+        return AST_TEST_PASS;
+}
 #endif
 
 static int load_module(void)
@@ -580,6 +736,7 @@
 #ifdef TEST_FRAMEWORK
 	AST_TEST_REGISTER(creation_test);
 	AST_TEST_REGISTER(get_test);
+	AST_TEST_REGISTER(put_test);
 #endif
 	return ast_register_storage(&odbc_se);
 }
@@ -589,6 +746,7 @@
 #ifdef TEST_FRAMEWORK
 	AST_TEST_UNREGISTER(creation_test);
 	AST_TEST_UNREGISTER(get_test);
+	AST_TEST_UNREGISTER(put_test);
 #endif
 	return ast_unregister_storage(odbc_se.name);
 }




More information about the asterisk-commits mailing list