[Freeswitch-svn] [commit] r3829 - freeswitch/branches/mishehu/src/mod/event_handlers/mod_cdr

Freeswitch SVN mishehu at freeswitch.org
Tue Dec 26 00:24:03 EST 2006


Author: mishehu
Date: Tue Dec 26 00:24:03 2006
New Revision: 3829

Modified:
   freeswitch/branches/mishehu/src/mod/event_handlers/mod_cdr/sqlitecdr.cpp
   freeswitch/branches/mishehu/src/mod/event_handlers/mod_cdr/sqlitecdr.h

Log:
Working on SqliteCDR, adding the ability of the logger to add and alter the tables as necessary.  As of yet untested, even in compilation.

Modified: freeswitch/branches/mishehu/src/mod/event_handlers/mod_cdr/sqlitecdr.cpp
==============================================================================
--- freeswitch/branches/mishehu/src/mod/event_handlers/mod_cdr/sqlitecdr.cpp	(original)
+++ freeswitch/branches/mishehu/src/mod/event_handlers/mod_cdr/sqlitecdr.cpp	Tue Dec 26 00:24:03 2006
@@ -73,6 +73,9 @@
 switch_core_db_t* SqliteCDR::db = 0;
 switch_core_db_stmt_t* SqliteCDR::stmt=0;
 switch_core_db_stmt_t* SqliteCDR::stmt_chanvars=0;
+switch_core_db_stmt_t* SqliteCDR::stmt_begin=0;
+switch_core_db_stmt_t* SqliteCDR::stmt_commit=0;
+std::string SqliteCDR::display_name = "SqliteCDR - The sqlite3 cdr logging backend";
 
 void SqliteCDR::connect(switch_xml_t& cfg, switch_xml_t& xml, switch_xml_t& settings, switch_xml_t& param)
 {
@@ -183,7 +186,7 @@
 			std::map<std::string,bool> temp_chanvars_map;
 			// Now to copy out all the chanvars from the list into 
 			
-			std::hash<std::string,bool> temp_sql_tables;
+			std::map<std::string,bool> temp_sql_tables;
 			temp_sql_tables["freeswitchcdr"] = 0;
 			temp_sql_tables["chanvars"] = 0;
 			
@@ -201,8 +204,144 @@
 			
 			if(!temp_sql_tables["freeswitchcdr"])
 			{
+				switch_console_printf(SWITCH_CHANNEL_LOG,"Creating the freeswitchcdr table in the SQLite mod_cdr database file.\n");
 				// Must create the missing freeswitchcdr table.
+				char sql_query_create_freeswitchcdr[] = "CREATE TABLE freeswitchcdr (\n"
+					"callid INTEGER AUTOINCREMENT PRIMARY KEY,\n"
+					"callstartdate INTEGER NOT NULL,\n"
+					"callanswerdate INTEGER NOT NULL,\n"
+					"calltransferdate INTEGER NOT NULL,\n"
+					"callenddate INTEGER NOT NULL,\n"
+					"originated INTEGER default 0,\n"
+					"clid TEXT default \"Freeswitch - Unknown\",\n"
+					"src TEXT NOT NULL,\n"
+					"dst TEXT NOT NULL,\n"
+					"ani TEXT default \"\",\n"
+					"aniii TEXT default \"\",\n"
+					"dialplan TEXT default \"\",\n"
+					"myuuid TEXT NOT NULL,\n"
+					"destuuid TEXT NOT NULL,\n"
+					"srcchannel TEXT NOT NULL,\n"
+					"dstchannel TEXT NOT NULL,\n"
+					"lastapp TEXT default \"\",\n"
+					"lastdata TEXT default \"\",\n"
+					"billusec INTEGER default 0,\n"
+					"disposition INTEGER default 0,\n" /* 0 = Busy or Unanswered, 1 = Answered */
+					"hangupcause INTEGER default 0\n",
+					"amaflags INTEGER default 0\n"
+					");\n";
+					
+				switch_core_db_exec(db, sql_query_create_freeswitchcdr, NULL, NULL, NULL);
+			}
+			
+			if(!temp_sql_tables["chanvars"])
+			{
+				switch_console_printf(SWITCH_CHANNEL_LOG,"Creating the chanvars table in the SQLite mod_cdr database file.\n");
+				// Must create the missing chanvars table.
+				sql_query_create_chanvars[] = "CREATE TABLE chanvars (\n"
+					"callid INTEGER default 0,\n""
+					"varname TEXT NOT NULL,\n"
+					"varvalue TEXT default \"\"\n"
+					");\n";
+				
+				switch_core_db_exec(db, sql_query_create_chanvars, NULL, NULL, NULL);
+			}
+			
+			if(chanvars_fixed_list.size() > 0)
+			{
+				// Now to check if the freeswitchcdr schema matches
+				std::map<std::string,std::string> freeswitchcdr_columns;
+				char sql_query_get_schema_of_freeswitchcdr[] = "SELECT * FROM SQLITE_MASTER WHERE name=\"freeswitchcdr\"";
+				char **result2;
+				nrow = 0;
+				ncol = 0;
+				char *errormessage2;
+				sql_rc = switch_core_db_get_table(db,sql_query_get_schema_of_freeswitchcdr,&result,&nrow,&ncol,&errormessage);
+				
+				if(sql_rc == SQLITE_OK)
+					for(int i = 0; i < nrows; i++)
+					{
+						std::string resultstring = result2[i][4];
+						std::string::size_type j = resultstring.find('(',i);
+						
+						std::string tempstring1,tempstring2;
+						
+						for(std::string::size_type i = j+1 ; j != std::string::npos; )
+						{
+							j = resultstring.find(' ',i);
+							if(j > 0)
+							{
+								tempstring = resultstring.substr(i,(j-i));
+								i = j+1;
+								j =resultstring.find(',',i);
+								if(j == std::string::npos)
+									tempstring2 = resultstring.substr(i,(resultstring.size() - i));
+								else
+									tempstring2 = resultstring.substr(i,(j-i));
+							
+								freeswitchcdr_columns[tempstring1] = tempstring2;
+							}
+							else
+								switch_console_printf(SWITCH_CHANNEL_LOG,"There has been a parsing problem with the freeswitchcdr schema.\n");
+						}
+					}
 				
+				// Now to actually compare what we have in the config against the db schema
+				std::map<std::string,std::string> freeswitchcdr_add_columns;
+				std::list<std::string>::iterator iItr, iEnd;
+				switch_size_t i = 0;
+				for(iItr = chanvars_fixed_list.begin(), iEnd = chanvars_fixed_list.end(); iItr != iEnd; iItr++, i++)
+				{
+					switch(chanvars_fixed_types[i])
+					{
+						case CDR_INTEGER:
+						case CDR_TINY:
+							if(freeswitchcdr_columns.find(*iItr) != freeswitchcdr_columns.end())
+							{
+								if(freeswitchcdr_columns[*iItr] != "INTEGER")
+									switch_console_printf(SWITCH_CHANNEL_LOG,"WARNING: SqliteCDR freeswitchcdr table column type mismatch: Column \"%s\" is not of an INTEGER type.  This is not necessarily fatal, but may result in unexpected behavior.\n",iItr->c_str());
+							}
+							else
+								freeswitchcdr_add_columns[*iItr] = "INTEGER";
+							break;
+						case CDR_DOUBLE:
+							if(freeswitchcdr_columns.find(*iItr) != freeswitchcdr_columns.end())
+							{
+								if(freeswitchcdr_columns[*iItr] != "REAL")
+									switch_console_printf(SWITCH_CHANNEL_LOG,"WARNING: SqliteCDR freeswitchcdr table column type mismatch: Column \"%s\" is not of a REAL type.  This is not necessarily fatal, but may result in unexpected behavior.\n",iItr->c_str());
+							}
+							else
+								freeswitchcdr_add_columns[*iItr] = "REAL";
+							break;
+						case CDR_DECIMAL:
+						case CDR_STRING:
+							if(freeswitchcdr_columns.find(*iItr) != freeswitchcdr_columns.end())
+							{
+								if(freeswitchcdr_columns[*iItr] != "TEXT")
+									switch_console_printf(SWITCH_CHANNEL_LOG,"WARNING: SqliteCDR freeswitchcdr table column type mismatch: Column \"%s\" is not of a TEXT type.  This is not necessarily fatal, but may result in unexpected behavior.\n",iItr->c_str());
+							}
+							else
+								freeswitchcdr_add_columns[*iItr] = "TEXT";
+							break;
+						default:
+							switch_console_printf("Oh bother, I should not have fallen into this hole in the switch/case statement.  Please notify the author.\n");
+					}
+				}
+				
+				if(freeswitchcdr_add_columns.size())
+				{
+					switch_console_printf(SWITCH_CHANNEL_LOG,"Updating the freeswitchcdr table schema.\n");
+					std::string tempsql_freeswitchcdr_alter_table = "ALTER TABLE freeswithcdr ADD ";
+					std::map<std::string, std::string>::iterator iItr, iEnd;
+					for(iItr = freeswitchcdr_add_columns.begin(), iEnd = freeswitchcdr_add_columns.end(); iItr != iEnd; iItr++)
+					{
+						std::string sql_query_freeswitchcdr_alter_table = tempsql_freeswitchcdr_alter_table;
+						sql_query_freeswitchcdr_alter_table.append(iItr->first);
+						sql_query_freeswitchcdr_alter_table.append(" ");
+						sql_query_freeswitchcdr_alter_table.append(iItr->second);
+						switch_core_db_exec(db, sql_query_freeswitchcdr_alter_table.c_str(), NULL, NULL, NULL);
+					}
+				}
 			}
 		}
 	}
@@ -462,6 +601,8 @@
 
 void SqliteCDR::disconnect()
 {
+	switch_core_db_finalize(stmt_chanvars);
+	switch_core_db_finalize(stmt);
 	switch_core_db_close(db);
 	activated = 0;
 	logchanvars = 0;

Modified: freeswitch/branches/mishehu/src/mod/event_handlers/mod_cdr/sqlitecdr.h
==============================================================================
--- freeswitch/branches/mishehu/src/mod/event_handlers/mod_cdr/sqlitecdr.h	(original)
+++ freeswitch/branches/mishehu/src/mod/event_handlers/mod_cdr/sqlitecdr.h	Tue Dec 26 00:24:03 2006
@@ -51,6 +51,7 @@
 		virtual bool is_activated();
 		virtual void tempdump_record();
 		virtual void reread_tempdumped_records();
+		virtual std::string get_display_name();
 
 	private:
 		static bool activated;
@@ -61,12 +62,15 @@
 		static switch_core_db *db;
 		static switch_core_db_stmt_t *stmt;
 		static switch_core_db_stmt_t *stmt_chanvars;
+		static switch_core_db_stmt_t *stmt_begin;
+		static switch_core_db_stmt_t *stmt_commit;
 		static bool connectionstate;
 		static bool logchanvars;
 		static std::list<std::string> chanvars_fixed_list;
 		static std::vector<switch_mod_cdr_sql_types_t> chanvars_fixed_types;
 		static std::list<std::string> chanvars_supp_list; // The supplemental list
 		static bool repeat_fixed_in_supp;
+		static std::string display_name;
 };
 
 #endif



More information about the Freeswitch-svn mailing list