The trouble we encountered today is caused by a lack of careful design considerations for the previous two days.
In the traditional design concept, the storage process that can be extended during later running is usually handed over to the database.
For example, check the data when saving the data. Format the data when obtaining the data.
I also considered this in my initial design. When designing a data storage table, I considered the additional processing that can be selected when saving, checking, and obtaining records:
CREATE TABLE IF NOT EXISTS TCI_TYPE(FCITYPEIDINTUNSIGNED,FCISERIALINTUNSIGNED,FCISUBTYPEINTUNSIGNED,FCIITEMTYPE INT UNSIGNED,FEDITABLEINTUNSIGNED,FDISPLAYTYPE INT UNSIGNED,FISNULLINT UNSIGNED,FMINLENINT UNSIGNED,FMAXLENINT UNSIGNED,FDISPLAYXINT UNSIGNED,FDISPLAYYINT UNSIGNED,FGETDATAVARCHAR(255),FSETDATAVARCHAR(255),FCHKDATAVARCHAR(255),FCINAMEVARCHAR(255),FCITAGVARCHAR(255),FCIREMARKVARCHAR(255),FCIDEFAULTVARCHAR(255)) ENGINE=InnoDB;call sp_dropindex("TCI_TYPE","IND_TCI_TYPEID");CREATE UNIQUE INDEX IND_TCI_TYPEID ON TCI_TYPE(FCITYPEID,FCISERIAL);
In the above table, fgetdata, fsetdata, and fchkdata set this record to obtain this record and perform additional processing when checking this record.
In the initial design, we considered executing the MySQL SP to complete this work.
For example, if fgetdata is filled in with an SP name, execute SP when this record is obtained, and then use the value returned by SP as the display value to display it to the interface,
After the interface is modified, save it when executing the fsetdata Sp (if any.
The original intention of this design is to allow SP flexibility to be used to determine and process other data (other data in the global database and the table) when obtaining and storing records.
There is no problem with this design-if all the data is stored in the database.
In the past two days, I tend to read the database data into the memory globally, and then write it back to the database after the operation is completed. This is also because, in fact, it is difficult for me to edit and modify data through simple operations, or to frequently store data to the database, in addition, we also risk data inconsistency, or save the data after the memory is edited completely. As a perfectionist, I naturally chose the design method for editing the complete data and then saving it.
Then, the previously imagined database SP will fail to complete the later check at the runtime! Because the data is still in the memory during get, set, and chk operations, it has not been written into the database!
Well, no sp. Is there any other method?
Python. Later, write some Python scripts to replace SP to complete get, set, and chk.
It should be feasible and convenient, especially ironpython.
That is to say, how to operate complex data structures in C ++ in Python.
Today, I briefly verified how C ++ calls Python and python to access and operate class data in C ++:
In C ++, the first method to execute python is reference:
using namespace IronPython::Hosting;
using namespace IronPython::Runtime::Types;using namespace Microsoft::Scripting::Hosting;
These two can be included in ironpython
Ironpython. dll and Microsoft. Scripting. dll are referenced.
Then:
ScriptEngine^ engine = Python::CreateEngine();ScriptScope^ scope = engine->CreateScope();ScriptSource^ source = engine->CreateScriptSourceFromFile(L"C:\\develop\\test\\helloworld.py");source->Execute(scope);
Create an engine, a scope, and a source.
Python called by C ++ uses C ++ Variables
C ++ defines the following two simple classes:
public ref class CmdbObjectItem{public:CmdbObjectItem();public:intm_iObjectID;intm_iItemType;System::String^ m_Tag;System::String^m_ItemValue;};public ref class CmdbObject{public:CmdbObject();public:intm_iObjectID;intm_iObjectType;intm_iBelongsToID;System::String ^m_ItemValue;List<CmdbObjectItem^>^m_SubItem;};
We want to use the content in javasbobject in Python, and add the javasbobjectitem class member to m_subitem of javasbobject.
First, instantiate a jsonbobject in C ++.
Export bobject ^ Co = gcnew export bobject (); export bobjectitem ^ CI = gcnew export bobjectitem (); ci-> m_itemvalue = l "test value"; Co-> m_iobjectid = 123; co-> m_subitem-> Add (CI );
CO is our objective bobject, which is intended for Python:
scope->SetVariable("CmdbObject",co);
scope->SetVariable("CmdbObjectItemType", DynamicHelpers::GetPythonTypeFromType(CmdbObjectItem::typeid));
In this way, CO is set with the variable "effecbobject", which can be used directly in the python program and a javasbobjectitemtype class, corresponding to javasbobjectitem, python can directly new this class.
#-*-Coding: 936-*-Def welcome (required bobject): Print required bobject. m_iobjectid: m_iobjecttid1_bobject.m_iobjectid = required bobject in the Co object passed by C ++. m_iobjectid + 1 is equivalent to co-> m_iobjectid ++ print upload bobject. m_iobjectidfor I in your bobject. m_subitem: traverse the list of C ++ <symbol bobjectitem ^> ^ m_subitemprint I. m_itemvaluecc = cmdbobjectitemtype () Create a New cmdbobjectitem instance CC. m_itemvalue = "added by Python" + '% d' % added bobject. m_iobjectid1_bobject.m_subitem.add (CC) Add to list <symbol bobjectitem ^> ^ m_subitemwelcome (symbol bobject) Python program entry point.
Is it a character set ?!
Note that the first line of the python program has
# -*- coding: 936 -*-
If you cancel this line... an error is reported during execution. Because Python does not know the encoding mode of this file.
Therefore, a 936 value is specified here.
Of course, it seems okay if you save this python program as a formatted UTF-8.
When editing with vs2010, the default format selected for saving is 936, so we can write 936 here.
Operation Efficiency
Createengine is slow. Well, it is really slow.
However, it seems that the "Create" method can be used for big users.
Void testpython (scriptengine ^ engine, required bobject ^ Co) {scriptscope ^ scope = engine-> createlist (); scriptsource ^ source = engine-> createscriptsourcefromfile (L "C: \ develop \ test \ helloworld. PY "); Scope-> setvariable (" Export bobjectitemtype ", dynamichelpers: getpythontypefromtype (export bobjectitem: typeid); Scope-> setvariable (" Export bobject ", CO ); source-> execute (scope); Console: writeline ("after: {0}", co-> m_iobjectid); For each (effecbobjectitem ^ t in co-> m_subitem) console: writeline ("item: {0}", T-> m_itemvalue);} int main (array <system: String ^> ^ ARGs) {scriptengine ^ engine = Python: createengine (); required bobject ^ Co = gcnew required bobject (); required bobjectitem ^ CI = gcnew required bobjectitem (); ci-> m_itemvalue = l "test value"; Co-> m_iobjectid = 123; Co-> m_subitem-> Add (CI); testpython (engine, CO); Console :: readline (); testpython (engine, CO); Console: Readline (); testpython (engine, CO); Console: Readline (); testpython (engine, CO ); console: Readline (); testpython (engine, CO); Console: Readline (); testpython (engine, CO); Console: Readline (); Console :: writeline (L "Hello World"); Return 0 ;}
The execution result is as follows:
123
124
Test Value
After: 124
Item: Test Value
Item: Add 124 by Python
124
125
Test Value
Add Python 124
After: 125
Item: Test Value
Item: Add 124 by Python
Item: Add 125 by Python
125
126
Test Value
Add Python 124
Add Python 125
After: 126
Item: Test Value
Item: Add 124 by Python
Item: Add 125 by Python
Item: Add 126 by Python