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

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sun Aug 8 11:30:53 CDT 2010


Author: ivaxer
Date: Sun Aug  8 11:30:50 2010
New Revision: 281287

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=281287
Log:
implemented the copy() function of odbc stoarge 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=281287&r1=281286&r2=281287
==============================================================================
--- team/ivaxer/ast_storage/res/res_storage_odbc.c (original)
+++ team/ivaxer/ast_storage/res/res_storage_odbc.c Sun Aug  8 11:30:50 2010
@@ -92,6 +92,14 @@
 	const char *objectname;
 };
 
+struct copy_query_data {
+	const char *sql;
+	const char *from_pathname;
+	const char *to_pathname;
+	const char *from_objectname;
+	const char *to_objectname;
+};
+
 static const struct ast_storage_be odbc_se;
 
 static int se_release(struct ast_storage *st)
@@ -667,12 +675,70 @@
 	return -1;
 }
 
+static SQLHSTMT prepare_copy_query(struct odbc_obj *obj, void *arg)
+{
+	SQLHSTMT stmt;
+	SQLRETURN ret;
+	struct copy_query_data *qdata = (struct copy_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;
+	}
+
+	ret = SQLPrepare(stmt, (SQLCHAR *)qdata->sql, SQL_NTS);
+	if (!SQL_SUCCEEDED(ret)) {
+		ast_log(LOG_WARNING, "SQL Prepare failed!\n[%s]", qdata->sql);
+		SQLFreeHandle(SQL_HANDLE_STMT, stmt);
+		return NULL;
+	}
+
+	SQLBindParameter(stmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(qdata->to_pathname), 0, (void *)qdata->to_pathname, 0, NULL);
+	SQLBindParameter(stmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(qdata->to_objectname), 0, (void *)qdata->to_objectname, 0, NULL);
+	SQLBindParameter(stmt, 3, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(qdata->from_pathname), 0, (void *)qdata->from_pathname, 0, NULL);
+	SQLBindParameter(stmt, 4, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(qdata->from_objectname), 0, (void *)qdata->from_objectname, 0, NULL);
+
+	return stmt;
+}
+
+static int se_copy(struct ast_storage *st, const char *from, const char *to) {
+	struct odbc_storage_pvt *ost = (struct odbc_storage_pvt*) st->storage_pvt;
+	struct copy_query_data qdata;
+	char sql[256];
+	SQLHSTMT stmt;
+
+	if (!from || !to) {
+		ast_log(LOG_WARNING, "from or to is null!\n");
+		return -1;
+	}
+
+	snprintf(sql, sizeof(sql), "INSERT INTO %s (pathname, filename, recording, extension) (SELECT ?, ?, recording, extension FROM %s WHERE pathname = ? AND filename = ?)", ost->tablename, ost->tablename);
+
+	/* TODO: 'to' and 'from' can contain a path */
+	qdata.sql = sql;
+	qdata.from_pathname = ost->pathname;
+	qdata.to_pathname = ost->pathname;
+	qdata.from_objectname = from;
+	qdata.to_objectname = to;
+
+	stmt = ast_odbc_prepare_and_execute(ost->conn, prepare_copy_query, (void *) &qdata);
+	if (!stmt) {
+		ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n", sql);
+		return -1;
+	}
+
+	SQLFreeHandle(SQL_HANDLE_STMT, stmt);
+	return 0;
+}
+
 static const struct ast_storage_be odbc_se = {
 	.name = "odbc",
 	.create = se_create,
 	.release = se_release,
 	.get = se_get,
 	.put = se_put,
+	.copy = se_copy,
 	.read = se_read,
 	.write = se_write,
 	.del = se_del,
@@ -907,6 +973,47 @@
 	return AST_TEST_PASS;
 }
 
+AST_TEST_DEFINE(copy_test)
+{
+	struct ast_storage *st;
+	int ret;
+	const char uri[] = "asterisk-storage-test/storage/testpath";
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = "copy_test";
+		info->category = "main/storage/odbc";
+		info->summary = "copy test";
+		info->description =
+			"Tests the copy functions";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	st = se_create(uri);
+	if (!st) {
+		return AST_TEST_FAIL;
+	}
+
+	ret = se_copy(st, "copy_test", "copy_test_new");
+
+	/* TODO: add checks here */
+
+	if (ret) {
+		ast_test_status_update(test, "copy() failed\n");
+		return AST_TEST_FAIL;
+	}
+
+	/* TODO: add checks here */
+
+	ret = se_release(st);
+	if (ret) {
+		return AST_TEST_FAIL;
+	}
+
+	return AST_TEST_PASS;
+}
 #endif
 
 static int load_module(void)
@@ -916,6 +1023,7 @@
 	AST_TEST_REGISTER(get_test);
 	AST_TEST_REGISTER(put_test);
 	AST_TEST_REGISTER(del_test);
+	AST_TEST_REGISTER(copy_test);
 #endif
 	return ast_register_storage(&odbc_se);
 }
@@ -927,6 +1035,7 @@
 	AST_TEST_UNREGISTER(get_test);
 	AST_TEST_UNREGISTER(put_test);
 	AST_TEST_UNREGISTER(del_test);
+	AST_TEST_UNREGISTER(copy_test);
 #endif
 	return ast_unregister_storage(odbc_se.name);
 }




More information about the asterisk-commits mailing list