Lua database/MySQL operation

Keywords: Database MySQL SQL encoding

0x00 Packaging Prerequisites

This time, it is only encapsulated in the function interface layer, which was originally intended to be encapsulated in business class calls. There are still many knowledge points to review, so it is abandoned.

Now let's talk about the guidelines for encapsulating interfaces:

1. Interface single responsibility, which I have been advocating and abiding by the packaging interface, just do one thing, more than the author's own `hey', others do not understand, such interface is not strong.

2. Every language has its grammatical characteristics, so we must understand them well in advance, so as to facilitate the judgment and processing of illegal operations.

3. Realize the function before writing the interface, and then cut it according to the function code, so that it can be encapsulated in mind and control the granularity of the cutting function. For example, when I encapsulated ConnectMySqlDb (), I also encapsulated environment variables. Later, when I released environment handles and connection handles, environment handles could not be found. This makes it easy for me to extract environment variables and encapsulate them into a single interface for ConnectMySqlDb () and

CloseConnect () is used. Perfect solution.

4. Establish milestones, define the boundaries of tasks, and do not make bottomless holes in the brain. This is really important, endless brain hole, will only drag themselves and make the project indefinitely, steadfastly do milestones. It's like my interface, which completes the simple function of adding, deleting, modifying and checking. Transactions and other things remain in the second edition for writing.


0x01 Code Implementation

----Reserve log records
function Log(msg)
	--The log file is written here. /var/log
	print(msg);

end

--Creating environment handles
function CreateEnv()

	luasql = require "luasql.mysql"
	env_handle = luasql.mysql();

	return env_handle;
end

function ConnectMySqlDb(env_handle, database_name, user_name, user_pwd, ip, port)

	--Direct error reporting without environment handle
	if(nil == env_handle) then
		Log("No environment handle created,Unable to call ConnectMysqlDb!");

		return nil;
	end

	if(database_name == nil) and (user_name == nil) and
		(user_pwd == nil) and (ip == nil) and (port == nil)
	then
		----Connect to the default database
		conn = env_handle:connect("reacher", "root", "admin", "127.0.0.1", 3306);
		if nil == conn then
			print("ConnectMysqlDb->connect There is a problem connecting to the default database!");
		end
		return conn;
	else
		----Custom Connection Database
		conn = env_handle:connect(database_name, user_name, user_pwd, ip, port);
		if nil == conn then
			print("ConnectMysqlDb->connect Connecting custom databases is problematic!");
		end

		return conn;
	end

end

function  CloseConnect(env_handle, conn_handle)

	env_handle:close();  --Close database connection
	conn_handle:close()   --Close the database environment

end

--Query the data in the data table
function  ExecuteSelectSqlString(connc_handle,  sql_string)
	if((nil == connc_handle) or 
	       (nil == sql_string)) then
		Log("SelectData There is a problem with the parameters.");
		
		return -1;
	end

	cursor,errorString  = connc_handle:execute(sql_string);
	if  (nil ~=  errorString) then
		Log(errorString);
		return -1;
	end
	
	row = cursor:fetch ({}, "a");
	if( -1 == row) then
		Log("no get row!");
		return 1;
	else
		while row do
			print(string.format("Id: %s, Name: %s", row.id, row.name))
			-- reusing the table of results
			row = cursor:fetch (row, "a")
		end
	end
	
	return 1;
end

--Execution of additions, deletions and modifications sql Sentence
function ExecuteSqlString(connc_handle,  sql_string)
 	
	if((nil == connc_handle) or (nil == sql_string)) then
		Log("ExecuteSqlString There is a problem with the parameters.");
		return -1;
	end

	cursor,errorString  = connc_handle:execute(sql_string);
	if  (nil ~=  errorString) then
		Log(errorString);
		return -1;
	end
		
	return 1;	

end


dofile("test.lua");

--int main()
--{
	func_execute_status = 0;

	--Connect to the database
	env_handle = CreateEnv();
	if(nil == env_handle) then
		Log("env handle create fail!");

		os.exit(1);
	end

	--
	conn_mysql_handle = ConnectMySqlDb(env_handle);
	if nil == conn_mysql_handle  then
		print("no connect mysql db!");
	else
		print("connect mysql db!");
	end

	--Execute custom statements
	--ExecuteSqlString(conn_handle,SqlString)
	--conn_mysql_handle:execute Success in implementation is 0.
	func_execute_status = conn_mysql_handle:execute"SET NAMES UTF8";

	sql_string = "select * from student";
	func_execute_status  =  ExecuteSelectSqlString(conn_mysql_handle, sql_string);
	if(1 ~= func_execute_status) then
		Log("SelectData no execute !");
	end
	
	insert_string  =  "insert into student(id, name)values(4, 'messi')";
	func_execute_status =  ExecuteSqlString(conn_mysql_handle,insert_string);
	print(func_execute_status);
	if(1 ~= func_execute_status) then
		Log("ExecuteSqlString no execute !");
	end
	
	--Close environment variable handles and connection handles
	CloseConnect(env_handle, conn_mysql_handle);

--	return 0;
--}


Experience Summary of 0x02

1. Don't be afraid to report errors. The basic grammar is forgotten. You dig and jump by yourself. Fortunately, you have a good mentality.

2. Deug's experience: This requires blowing the benefits of the interface and debugging it separately. Think about it, the interface you encapsulate is not too coupled with other interfaces, debugging only requires logging functions in key places, the reason for restoring the error is basically sure, and then the logic is tidied up and easily repaired.

3. Be good at writing documentation. 1. Manage your own task list and complete the unfinished task at a glance.

2. Understand where your encoding barriers are and upgrade your skills after completing your tasks.

3. Communicate with the leader after completion. I have finished. Everyone is a team. Don't be a lone hero.





Posted by gotit on Mon, 27 May 2019 17:10:32 -0700