Erlang : SQLite related stuff

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SQLite related functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

is_sqlite_enabled() ->
	try application:get_env(team, sqlite_enabled) of
		% sqlite enabled
		{ok, true}->
			true;
		{ok, false}->
		% sqlite disabled
			false;
		_ ->
			?INFO("ERROR - Check sqlite_enabled field in team sys.config", [])
	catch
		Type:Error ->
			?ERROR("~p", [{Type, Error}])
	end.

get_drv_path() ->
	try application:get_env(team, sqlite_drv_path) of
		{ok, DrvPath}->
			%?DEBUG("driver path : ~p", [DrvPath]),
			DrvPath;		
		_ ->
			?INFO("ERROR - Check sqlite_drv_path field in team sys.config", [])
	catch
		Type:Error ->
			?ERROR("~p", [{Type, Error}])
	end.

get_drv_name() ->
	try application:get_env(team, sqlite_drv_name) of
		{ok, DrvName}->
			%?DEBUG("driver name : ~p", [DrvName]),
			DrvName;		
		_ ->
			?INFO("ERROR - Check sqlite_drv_name field in team sys.config", [])
	catch
		Type:Error ->
			?ERROR("~p", [{Type, Error}])
	end.

% team_db:load_sqlite_driver().
load_sqlite_driver() ->
	try erl_ddll:load(get_drv_path(), get_drv_name()) of
		ok ->
			?DEBUG("sqlite3 driver loaded", []);
		Else -> 
			?DEBUG("sqlite3 error loading driver ~p", [Else])
	catch
		Type:Error ->
			?ERROR("~p", [{Type, Error}])
	end.

% team_db:unload_sqlite_driver().
unload_sqlite_driver() ->
	try erl_ddll:unload(get_drv_name()) of
		ok ->
			?DEBUG("sqlite3 driver unloaded", []);
		Else ->
			?DEBUG("sqlite3 error unloading driver ~p", [Else])
	catch
		Type:Error ->
			?ERROR("~p", [{Type, Error}])
	end.
	
create_db_conn(DbName, DbPath) ->
	?DEBUG("~p",[{create_db_conn, [DbName, DbPath]}]),
	case catch sqlite3:open(list_to_atom(DbName), [{db, DbPath ++ DbName ++ ".db"}]) of
		{ok, Pid} ->
			?DEBUG("db connection created ~p", [Pid]);
		{error, {already_started, Pid_}} ->
			?DEBUG("already started ~p", [Pid_]),
			close_db_conn(DbName, DbPath),
			create_db_conn(DbName, DbPath);
		Else ->
			?DEBUG("error creating db conn ~p", [Else])
	end.

close_db_conn(DbName, DbPath) ->
	?DEBUG("~p",[{close_db_conn, [DbName]}]),
	case catch sqlite3:close(list_to_atom(DbName)) of
		ok ->
			?DEBUG("db connection closed", []);
		%{'EXIT',{noproc, X_ }} ->			
		Else ->
			?DEBUG("error closing db conn ~p", [Else])
	end.

get_db_path(EA) ->
	try application:get_env(team, sqlite_db_path) of
		{ok, Path}->
			Path ++ "/" ++ EA ++ "/";		
		_ ->
			?INFO("ERROR - Check sqlite_db_path field in team sys.config", [])
	catch
		Type:Error ->
			?ERROR("~p", [{Type, Error}])
	end.

get_numlist_tblname() ->
	try application:get_env(team, sqlite_numlist_tblname) of
		{ok, TblName}->
			TblName;		
		_ ->
			?INFO("ERROR - Check sqlite_numlist_tblname field in team sys.config", [])
	catch
		Type:Error ->
			?ERROR("~p", [{Type, Error}])
	end.

get_numfield_name() ->
	try application:get_env(team, sqlite_numfield_name) of
		{ok, FieldName}->
			FieldName;		
		_ ->
			?INFO("ERROR - Check sqlite_numfield_name field in team sys.config", [])
	catch
		Type:Error ->
			?ERROR("~p", [{Type, Error}])
	end.

get_msgfield_name() ->
	try application:get_env(team, sqlite_msgfield_name) of
		{ok, FieldName}->
			FieldName;		
		_ ->
			?INFO("ERROR - Check sqlite_msgfield_name field in team sys.config", [])
	catch
		Type:Error ->
			?ERROR("~p", [{Type, Error}])
	end.

get_scalar_result(Camp, Sql) ->
	try sqlite3:sql_exec(list_to_atom(Camp), Sql) of
		[{columns, _}, {rows,[{Result}]}] ->
			Result;
		Else -> 
			?DEBUG("scalar result : ~p", [Else])
	catch
		Type:Error ->
			?DEBUG("sql exec exception ~p", [{Type, Error}])
	end.


% team_db:get_row_count("camp:system:x_num_only").
get_row_count(Key) ->
	[ _, EA, Camp | _ ] = string:tokens(Key, ":"),
	?DEBUG("~p",[{get_row_count, [EA, Camp]}]),
	Sql = "SELECT COUNT(*) FROM " ++ atom_to_list(get_numlist_tblname()) ++ ";",
	RowCount = get_scalar_result(Camp, Sql),
	RowCount.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% team_db:create_db_conn("camp:system:x_num_only").

create_db_conn(Key) ->
	[ _, EA, Camp | _ ] = string:tokens(Key, ":"),
	create_db_conn(Camp, get_db_path(EA)).	

% team_db:close_db_conn("camp:system:x_num_only").
close_db_conn(Key) ->
	[ _, _, Camp | _ ] = string:tokens(Key, ":"),
	close_db_conn(Camp, null).


fetch_chunk(number_only, DbName, StartFrom, ChunkSize) ->
	Sql = "SELECT " ++ atom_to_list(get_numfield_name()) ++ " FROM " ++ atom_to_list(get_numlist_tblname())  ++  " LIMIT " ++ integer_to_list(StartFrom) ++ ","  ++ integer_to_list(ChunkSize) ++ ";",
	[{columns, _}, {rows, Rows}] = sqlite3:sql_exec(list_to_atom(DbName), Sql),	
	Rows.

fetch_chunk(DbName, StartFrom, ChunkSize) ->
	Sql = "SELECT " ++ atom_to_list(get_numfield_name()) ++ " , " ++ atom_to_list(get_msgfield_name()) ++ " FROM " ++ atom_to_list(get_numlist_tblname())  ++  " LIMIT " ++ integer_to_list(StartFrom) ++ ","  ++ integer_to_list(ChunkSize) ++ ";",
	[{columns, _}, {rows, Rows}] = sqlite3:sql_exec(list_to_atom(DbName), Sql),	
	Rows.


sq_create_db_conn(Key) ->
	case is_sqlite_enabled() of
	  true ->
		 create_db_conn(Key)
  end. 

sq_close_db_conn(Key) ->
	case is_sqlite_enabled() of
	  true ->
		  close_db_conn(Key)
  end. 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Default Fucntions - modified
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% scard is modified to deal with sqlite
%% original fucntion team_db:scard
sq_scard(Key) -> 
	case is_sqlite_enabled() of
	  true ->
		 create_db_conn(Key),
		 RowCount = get_row_count(Key),
		 close_db_conn(Key),
		 RowCount;
	  false ->
		  scard(?DB0, Key)
  end. 

%% sunionstore is modified to deal with sqlite
%% original function team_db:sunionstore/3
sq_sunionstore(DstKey, Keys) ->  
	case is_sqlite_enabled() of
	  true ->
		 ?DEBUG("~p", [{sunionstore}]); 
	  false ->
		  sunionstore(?DB0, DstKey, Keys)
	end.

%% spop is modified to deal with sqlite
%% original function team_db:spop/2
sq_spop(Key, Chunk) ->	
	%?DEBUG("~p", [{Key, Chunk}]),
	case is_sqlite_enabled() of
	  true ->	
		 %?DEBUG("~p", [{sq_spop, true}]),
		  case Chunk of
			  [ {Dst} | Rest ] ->
				  ?DEBUG("~p", [binary_to_list(Dst)]),
				  {binary_to_list(Dst), Rest};
			  [ {Dst, Msg} | Rest ] ->
				  ?DEBUG("~p", [{binary_to_list(Dst), binary_to_list(Msg)}]),
				  {binary_to_list(Dst), binary_to_list(Msg), Rest}
		  end;
	  false ->
  		{spop(?DB0, Key), Chunk}
	end.

%% hget is modified to deal with sqlite
%% original function team_db:hget/2
sq_hget(Key, Field, Msg) ->
	case is_sqlite_enabled() of
	  true ->
		  ?DEBUG("~p", [Msg]),
		  [Msg];
		 %%["{\"<num>\":\"x\",\"<ppc>\":\"xyz\"}"];
	  false ->
		   hget(?DB0, Key, Field)
	end.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: