CN107577817A - A kind of reading/writing method of entity data bak - Google Patents

A kind of reading/writing method of entity data bak Download PDF

Info

Publication number
CN107577817A
CN107577817A CN201710912771.1A CN201710912771A CN107577817A CN 107577817 A CN107577817 A CN 107577817A CN 201710912771 A CN201710912771 A CN 201710912771A CN 107577817 A CN107577817 A CN 107577817A
Authority
CN
China
Prior art keywords
database
type
data
sql statement
user input
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN201710912771.1A
Other languages
Chinese (zh)
Inventor
邱扬
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Beijing Kuwo Technology Co Ltd
Original Assignee
Beijing Kuwo Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Beijing Kuwo Technology Co Ltd filed Critical Beijing Kuwo Technology Co Ltd
Priority to CN201710912771.1A priority Critical patent/CN107577817A/en
Publication of CN107577817A publication Critical patent/CN107577817A/en
Pending legal-status Critical Current

Links

Landscapes

  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

The present invention relates to a kind of reading/writing method of entity data bak, when needing entity data bak quickly to read, user input data is obtained first, then user input data is sent to database object model, data type in user input data is resolved to type corresponding to database by the database object model, OC analysis of object in user input data is structure corresponding to database table by the database object model, structure corresponding to the database object model type and database table according to corresponding to the database parsed, generate SQL statement, SQL statement is sent to database by the database object model, database performs SQL statement and returns to Query Result.The present invention, by SQL statement encapsulation that is cumbersome, repeating, do not have to repeat when accessing database to write SQL statement, corresponding function can be achieved with a Codabar code, reduce workload, improve development efficiency, effectively avoid mistake caused by the duplication of labour.

Description

A kind of reading/writing method of entity data bak
Technical field
The present invention relates to database technical field, specifically a kind of reading/writing method of entity data bak.
Background technology
With the development of database technology, various demands continuously emerge, and constantly supervise further technological innovation, rise Level.In program development, programmer runs into following situation sometimes:
When company goes to work, the sometimes work on daytime does not finish, and programmer will take work home overtime work at night and continue to do.But It is that company is typically entity data bak with database for the supporting exploitation of program development, is deployed on development support server , the development support server is a server inside corporate lan, and service end normal operation is dependent on exploitation branch Hold the exploitation database on server.Due to being in, overtime work can not be accessed inside corporate lan, therefore cannot be connected Exploitation database on upper development support server, this results in service end cisco unity malfunction, function during program development It can not just debug.
In the prior art, a kind of solution method is exactly to use virtual data base.Just the use of company is journey when company goes to work Sequence develops supporting exploitation database;Go back home, just use the virtual data base analog development database in internal memory, do one A little basic function debuggings, are definitely generally enough.
Need largely to localize storage service in current app.In order to meet current business demand, we need in app Largely to use data base tool, this solution is intended to allow the convenient exploitation of programmer.
Entity data bak, which reads data, needs following steps:
1st, connected with Database,
2nd, sql query statements are write,
3rd, query statement is performed,
4th, Query Result is obtained,
5th, database is closed.
Obviously, today of frequent reading/data storage is frequently interacted, needed with database in needs, accesses data The workload in storehouse is very big, programmer it is cumbersome write SQL statement, not only workload is big, and easily causes to malfunction, it is necessary to This further is reasonably optimized.
The content of the invention
For defect present in prior art, it is an object of the invention to provide a kind of read-write side of entity data bak Method, SQL statement that is cumbersome, repeating is encapsulated, does not have to repeat when accessing database to write SQL statement, with a Codabar code Function corresponding to realization, reduces workload, improves development efficiency, effectively avoids mistake caused by the duplication of labour.
To achieve the above objectives, the present invention adopts the technical scheme that:
A kind of reading/writing method of entity data bak, it is characterised in that comprise the following steps:
When needing entity data bak quickly to read, user input data is obtained first,
Then user input data is sent to database object model,
Data type in user input data is resolved to type corresponding to database by the database object model,
OC analysis of object in user input data is structure corresponding to database table by the database object model,
Structure corresponding to the database object model type and database table according to corresponding to the database parsed, generation SQL statement,
SQL statement is sent to database by the database object model,
Database performs SQL statement and returns to Query Result.
On the basis of above-mentioned technical proposal, the database object model is designed using four layer architectures, including:
Data type analytic sheaf, for the data type in user input data to be resolved into type corresponding to database, wherein:
Data type in user input data includes:Numeric type, integer type, floating point type, Boolean type, character string class Type, binary data types, picture/mb-type, date type,
Function when passing through system operation:Class_copyPropertyList () obtains generic attribute relevant information array,
Function when passing through operation:Property_getAttributes () obtains respective attributes information,
Corresponding information is obtained with reference to table by comparison system type,
Type includes corresponding to database:INTEGER, REAL, TEXT, BLOB,
Object placement's layer, for being structure corresponding to database table by the OC analysis of object in user input data, and make data Structural support corresponding to the table of storehouse parses type corresponding to obtained database, wherein:
Function when passing through system operation:Class_copyPropertyList () obtains generic attribute relevant information array,
Function when passing through operation:Property_getAttributes () obtains respective attributes information,
Corresponding information is obtained with reference to table by comparison system type,
Object type resolver is coordinated to parse the type of each data, the information being then mapped in database, including number According to information such as storehouse type, database default value, database default sizes,
Function property_getName () is parsed in object during with reference to operation, the Key values corresponding to this type attribute, so Data value corresponding to type is obtained using KeyValue afterwards,
SQL statement generation layer, for structure, generation corresponding to the type according to corresponding to the database parsed and database table SQL statement,
Database-operation level, for SQL statement to be sent into database, for receiving the Query Result returned.
On the basis of above-mentioned technical proposal, in object placement's layer, defined according to user, produce whether upgrade database Option:
After object placement's layer parses structure corresponding to database table, and the existing database table of database is compared, and is on duty When different quantity exceeds user defined value, the option of upgrade database is produced.
On the basis of above-mentioned technical proposal, in SQL statement generation layer, according to the option of incoming upgrade database, Generate in SQL statement, increase corresponding database upgrade instruction.
The reading/writing method of entity data bak of the present invention, SQL statement that is cumbersome, repeating is encapsulated, accesses database Shi Buyong, which is repeated, writes SQL statement, and corresponding function can be achieved with a Codabar code, reduce workload, improve exploitation Efficiency, effectively avoid mistake caused by the duplication of labour.
Brief description of the drawings
The present invention has drawings described below:
The configuration diagram of Fig. 1 present invention.
The FB(flow block) of Fig. 2 present invention.
Embodiment
The present invention is described in further detail below in conjunction with accompanying drawing.
As shown in Figure 1, 2, the reading/writing method of entity data bak of the present invention, comprises the following steps:
When needing entity data bak quickly to read, user input data is obtained first,
Then user input data is sent to database object model,
Data type in user input data is resolved to type corresponding to database by the database object model,
OC analysis of object in user input data is structure corresponding to database table by the database object model,
Structure corresponding to the database object model type and database table according to corresponding to the database parsed, generation SQL statement,
SQL statement is sent to database by the database object model,
Database performs SQL statement and returns to Query Result.
On the basis of above-mentioned technical proposal, the database object model is designed using four layer architectures, including:
Data type analytic sheaf, for the data type in user input data to be resolved into type corresponding to database,
Object placement's layer, for being structure corresponding to database table by the OC analysis of object in user input data, and make data Structural support corresponding to the table of storehouse parses type corresponding to obtained database,
SQL statement generation layer, for structure, generation corresponding to the type according to corresponding to the database parsed and database table SQL statement,
Database-operation level, for SQL statement to be sent into database, for receiving the Query Result returned.
First have to parse data by data type analytic sheaf:
Preset kind has herein:Numeric type, integer type, floating point type, Boolean type, character string type, binary data Type, picture/mb-type, date type.
typedef NS_ENUM(NSUInteger, SSTypeCode) {
__TYPE_Unknown,
__TYPE_Number,
__TYPE_Integer,
__TYPE_Float,
__TYPE_BOOL,
__TYPE_String,
__TYPE_Data,
__TYPE_UIImage,
__TYPE_Date,
};
Wherein Number numeric types have integer type and float again.
Function when passing through system operation:Class_copyPropertyList () obtains generic attribute relevant information array, Function when passing through operation:Property_getAttributes () obtains respective attributes information.
It can be obtained such as by property_getAttributes () function:Tq,D,N;T@”NSNumber”,&,D,N;This The attribute information of class, corresponding information is obtained with reference to table by comparison system type.Such as q represent herein be NSInteger types etc..
So we can obtain particular type information by following algorithm:
+ (SSTypeCode)typeCodeWithAttributes:(const char *)attribute {
if (attribute[0] != 'T') return __TYPE_Unknown;
const char *type = &attribute[1];
if (type[0] == '@' && type[1] !='""') return __TYPE_Unknown;
if (type[0] == 'i') return __TYPE_Number;
if (type[0] == 'q') return __TYPE_Integer;
if (type[0] == 'd') return __TYPE_Float;
if (type[0] == 'B') return __TYPE_BOOL;
char typeClazz[128] = { 0 };
const char *clazz = &type[2];
const char *clazzEnd = strchr(clazz, '"');
if (clazzEnd && clazz != clazzEnd) {
unsigned int size = (unsigned int)(clazzEnd - clazz);
strncpy(&typeClazz[0], clazz, size);
if (!strcmp((const char *)typeClazz, "NSNumber")) {
return __TYPE_Number;
} else if (!strcmp((const char *)typeClazz, "NSString")) {
return __TYPE_String;
} else if (!strcmp((const char *)typeClazz, "NSData")) {
return __TYPE_Data;
} else if (!strcmp((const char *)typeClazz, "UIImage")) {
return __TYPE_UIImage;
} else if (!strcmp((const char *)typeClazz, “NSDate”)) {
return __TYPE_Date;
} else {
return __TYPE_Unknown;
}
}
return __TYPE_Unknown;
}
Due to only having in sqlite databases:INTEGER, REAL, TEXT, BLOB
So it can do and once change for OC object types and type of database:
+ (NSString *)sqlTypeWithTypeCode:(SSTypeCode)typeCode {
NSString *sqlType = @"TEXT";
switch (typeCode) {
case __TYPE_Number:
case __TYPE_Integer:
case __TYPE_BOOL:
sqlType = @"INTEGER";
break;
case __TYPE_Float:
sqlType = @"REAL";
break;
case __TYPE_Data:
case __TYPE_UIImage:
sqlType = @"BLOB";
break;
case __TYPE_String:
case __TYPE_Date:
sqlType = @"TEXT";
break;
default:
break;
}
return sqlType;
}
Then specific object type is parsed by object placement's layer, produces the mapping table between object and database.
In object placement's layer:Function when passing through system operation first:Class_copyPropertyList () is obtained Generic attribute relevant information array, function when passing through operation:Property_getAttributes () believes to obtain respective attributes Breath.
It can be obtained such as by property_getAttributes () function:Tq,D,N;T@”NSNumber”,&,D,N;This The attribute information of class, corresponding information is obtained with reference to table by comparison system type.Such as q represent herein be NSInteger types etc..
objc_property_t * propertes = class_copyPropertyList(clazz, & propertesCount);
const char * attributesChar = property_getAttributes(propertes[i]);
NSString * propertyStr = [NSString stringWithCString:propertyChar
encoding:NSUTF8StringEncoding];
Then we need predefined map field:
NSString * const kSqlStoreNamed = @"sql_store_name";
NSString * const kSqlStoreValue = @"sql_store_value";
NSString * const kSqlStoreType = @"sql_store_type";
NSString * const kSqlStoreSize = @"sql_store_size";
NSString * const kSqlStorePrimaryKey = @"sql_store_primaryKey";
NSString * const kSqlStoreUnique = @"sql_store_unique";
NSString * const kSqlStoreNotNull = @"sql_store_notNull";
NSString * const kSqlTableNamed = @"sql_table_name";
NSString * const kSqlStoreDefaultValue = @"sql_store_default_value";
NSString * const kSqlStoreAutoIncrement = @"sql_store_autoIncrement";
NSString * const kSqlStoreSelectWhereKey = @"kSqlStoreSelectWhereKey";
NSString * const kSqlStoreSelectWhereValue = @"kSqlStoreSelectWhereValu e";
NSString * const kSqlStoreSelectLimit = @"kSqlStoreSelectLimit";
NSString * const kSqlStoreSelectOffset = @"kSqlStoreSelectOffset";
This cooperation object type resolver is parsed to the type of each data, the information being then mapped in database, bag Include the information such as type of database, database default value, database default size.
Function property_getName () is parsed in object during with reference to operation, the Key corresponding to this type attribute Value, then obtains data value corresponding to type using KeyValue:
const char * propertyChar = property_getName(propertes[i]);
id value = [instanceObject valueForKey:propertyStr];
By above-mentioned steps, basic mapping algorithm is finally drawn:
- (void)mappedDataModelArrayWithClass:(Class)clazz
instanceObject:(id)instanceObject {
unsigned int propertesCount = 0;
objc_property_t * propertes = class_copyPropertyList(clazz, & propertesCount);
for (int i = 0 ; i < propertesCount ; i++) {
const char * propertyChar = property_getName(propertes[i]);
const char * attributesChar = property_getAttributes(propertes [i]);
NSString * propertyStr = [NSString stringWithCString: propertyChar
encoding: NSUTF8StringEncoding];
id value = [instanceObject valueForKey: propertyStr];
VTypeCode typeCode = [DataTypeEncoding getTypeWithAttribute:attributesChar];
NSString * typeStr = [DataTypeEncoding getSqlWithTypeCode:typeCode];
value = [NSString stringWithFormat:@"%@", value];
id defaultValue = [DataTypeEncoding defaultValueWithTypeCode:typeCode];
id defauleSize = [DataTypeEncoding defaultSizeWithTypeCode:typeCode];
NSDictionary * dic = @{
kSqlStoreNamed : propertyStr,
kSqlStoreValue : value,
kSqlStoreType : typeStr,
kSqlTableNamed : self.tableNameStr,
kSqlStoreDefaultValue : defautlValue,
kSqlStoreAutoIncrement : NO,
kSqlStorePrimaryKey : self.primaryKey,
kSqlStoreNotNull : NO,
kSqlStoreUnique : YES,
kSqlStoreSize : defauleSize
};
[_mappedDictionary setValue:value forKey:propertyStr];
[_mappedArray addObject:dic];
}
free(propertes);
}
Next need to produce query statement in SQL generation layers, it is necessary first to default basic query field, such as:
NSMutableString *selectSqlStr = [NSMutableString stringWithFormat:@" SELECT * FROM %@, [self tableNameWithClazz:clazz]];
Then the master database query statement of response is produced according to concrete operations information, and is looked into according to particular/special requirement to supplement Ask sentence:
NSMutableString *selectSqlStr = [NSMutableString stringWithFormat:@" SELECT * FROM %@ WHERE %@ = '%@'", [self tableNameWithClazz:clazz], whereKey, whereValue];
NSString *selectSqlStr = [NSString stringWithFormat:@"SELECT COUNT(*) AS numrows FROM %@", [self tableNameWithClazz:clazz]];
Then the information in object placement's layer parses the information to be inquired about.
if (limit) {
if (offSet) {
[selectSqlStr appendFormat:@" LIMIT %lu OFFSET %lu", (unsigned long)limit, (unsigned long)offSet];
} else {
[selectSqlStr appendFormat:@" LIMIT %lu", (unsigned long) limit];
}
}
if (whereKey && whereValue) {
selectSqlStr = [NSString stringWithFormat:@"SELECT COUNT(*) AS numrows FROM %@ WHERE %@ = '%@'", [self tableNameWithClazz:clazz], whereKey, whereValue];
}
Then query statement performs inquiry operation caused by:
[_databaseQueue inDatabase:^(FMDatabase *db) {
FMResultSet * resultSet = [db executeQuery:selectSqlStr];
if (resultSet) {
if ([resultSet next]) {
resultCount = (unsigned int)[resultSet unsignedLongLon gIntForColumn:@"numrows"];
}
}
[db closeOpenResultSets];
}];
On the basis of above-mentioned technical proposal, in object placement's layer, defined according to user, produce whether the choosing of upgrade database :
After object placement's layer parses structure corresponding to database table, and the existing database table of database is compared, and is on duty When different quantity exceeds user defined value, the option of upgrade database is produced.
On the basis of above-mentioned technical proposal, in SQL statement generation layer, according to the option of incoming upgrade database, Generate in SQL statement, increase corresponding database upgrade instruction.
The content not being described in detail in this specification belongs to prior art known to professional and technical personnel in the field.

Claims (4)

1. a kind of reading/writing method of entity data bak, it is characterised in that comprise the following steps:
When needing entity data bak quickly to read, user input data is obtained first,
Then user input data is sent to database object model,
Data type in user input data is resolved to type corresponding to database by the database object model,
OC analysis of object in user input data is structure corresponding to database table by the database object model,
Structure corresponding to the database object model type and database table according to corresponding to the database parsed, generation SQL statement,
SQL statement is sent to database by the database object model,
Database performs SQL statement and returns to Query Result.
2. the reading/writing method of entity data bak as claimed in claim 1, it is characterised in that:The database object model uses Four layer architectures design, including:
Data type analytic sheaf, for the data type in user input data to be resolved into type corresponding to database, wherein:
Data type in user input data includes:Numeric type, integer type, floating point type, Boolean type, character string class Type, binary data types, picture/mb-type, date type,
Function when passing through system operation:Class_copyPropertyList () obtains generic attribute relevant information array,
Function when passing through operation:Property_getAttributes () obtains respective attributes information,
Corresponding information is obtained with reference to table by comparison system type,
Type includes corresponding to database:INTEGER, REAL, TEXT, BLOB,
Object placement's layer, for being structure corresponding to database table by the OC analysis of object in user input data, and make data Structural support corresponding to the table of storehouse parses type corresponding to obtained database, wherein:
Function when passing through system operation:Class_copyPropertyList () obtains generic attribute relevant information array,
Function when passing through operation:Property_getAttributes () obtains respective attributes information,
Corresponding information is obtained with reference to table by comparison system type,
Object type resolver is coordinated to parse the type of each data, the information being then mapped in database, including number According to information such as storehouse type, database default value, database default sizes,
Function property_getName () is parsed in object during with reference to operation, the Key values corresponding to this type attribute, so Data value corresponding to type is obtained using KeyValue afterwards,
SQL statement generation layer, for structure, generation corresponding to the type according to corresponding to the database parsed and database table SQL statement,
Database-operation level, for SQL statement to be sent into database, for receiving the Query Result returned.
3. the reading/writing method of entity data bak as claimed in claim 2, it is characterised in that:In object placement's layer, according to user Definition, produce whether the option of upgrade database:
After object placement's layer parses structure corresponding to database table, and the existing database table of database is compared, and is on duty When different quantity exceeds user defined value, the option of upgrade database is produced.
4. the reading/writing method of entity data bak as claimed in claim 3, it is characterised in that:In SQL statement generation layer, according to biography The option of the upgrade database entered, in SQL statement is generated, increase corresponding database upgrade instruction.
CN201710912771.1A 2017-09-30 2017-09-30 A kind of reading/writing method of entity data bak Pending CN107577817A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201710912771.1A CN107577817A (en) 2017-09-30 2017-09-30 A kind of reading/writing method of entity data bak

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201710912771.1A CN107577817A (en) 2017-09-30 2017-09-30 A kind of reading/writing method of entity data bak

Publications (1)

Publication Number Publication Date
CN107577817A true CN107577817A (en) 2018-01-12

Family

ID=61039000

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201710912771.1A Pending CN107577817A (en) 2017-09-30 2017-09-30 A kind of reading/writing method of entity data bak

Country Status (1)

Country Link
CN (1) CN107577817A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113377867A (en) * 2021-06-10 2021-09-10 四川省明厚天信息技术股份有限公司 Data synchronization method and device and electronic equipment

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20050091231A1 (en) * 2003-10-24 2005-04-28 Shankar Pal System and method for storing and retrieving XML data encapsulated as an object in a database store
CN102346774A (en) * 2011-09-26 2012-02-08 深圳市信游天下网络科技有限公司 Database operation method and device
CN103853803A (en) * 2013-06-26 2014-06-11 携程计算机技术(上海)有限公司 Database configuration file encapsulation method and operation method as well as operation device thereof
WO2017044119A1 (en) * 2015-09-11 2017-03-16 Hewlett Packard Enterprise Development Lp Graph database and relational database mapping
CN107577812A (en) * 2017-09-29 2018-01-12 北京酷我科技有限公司 A kind of method for quickly reading of entity data bak

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20050091231A1 (en) * 2003-10-24 2005-04-28 Shankar Pal System and method for storing and retrieving XML data encapsulated as an object in a database store
CN102346774A (en) * 2011-09-26 2012-02-08 深圳市信游天下网络科技有限公司 Database operation method and device
CN103853803A (en) * 2013-06-26 2014-06-11 携程计算机技术(上海)有限公司 Database configuration file encapsulation method and operation method as well as operation device thereof
WO2017044119A1 (en) * 2015-09-11 2017-03-16 Hewlett Packard Enterprise Development Lp Graph database and relational database mapping
CN107577812A (en) * 2017-09-29 2018-01-12 北京酷我科技有限公司 A kind of method for quickly reading of entity data bak

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113377867A (en) * 2021-06-10 2021-09-10 四川省明厚天信息技术股份有限公司 Data synchronization method and device and electronic equipment
CN113377867B (en) * 2021-06-10 2022-10-21 四川省明厚天信息技术股份有限公司 Data synchronization method and device and electronic equipment

Similar Documents

Publication Publication Date Title
CN110321344B (en) Information query method and device for associated data, computer equipment and storage medium
CN103559217B (en) A kind of massive multicast data towards isomeric data storehouse warehouse-in implementation method
CN102375879B (en) Mobile GIS (Geographic Information System) system based on intelligent mobile phone and application thereof
CN111159191A (en) Data processing method, device and interface
CN101008952A (en) Method and device for generating persistence layer
CN111078702B (en) SQL sentence classification management and unified query method and device
CN103020158A (en) Report form creation method, device and system
CN106372176A (en) Method for supporting uniform SQL (Structured Query Language) query of embedded document
CN108304463A (en) A kind of data managing method and its database application component for database
CN102819609A (en) Modeling method for persisting data model
CN106326387B (en) A kind of Distributed Storage structure and date storage method and data query method
CN105159616A (en) Disk space management method and device
CN102708203A (en) Database dynamic management method based on XML metadata
CN108536745A (en) Tables of data extracting method, terminal, equipment and storage medium based on Shell
CN107291471A (en) A kind of Meta model framework system for supporting customizable data acquisition
CN102999600A (en) Method and system for automatically generating embedded database
CN102004787A (en) Method for combining multiple application scene forms based on office software plugins
CN104408128B (en) A kind of reading optimization method indexed based on B+ trees asynchronous refresh
CN110825775B (en) Data exchange system supporting interaction of stream data and batch data in Gaia system
CN105205103A (en) Method and device for accessing database
CN114443656A (en) Customizable automated data model analysis tool and use method thereof
CN107577817A (en) A kind of reading/writing method of entity data bak
CN103593445A (en) Data filling method and device
CN107577816A (en) A kind of method of entity data bak read-write
CN107562955A (en) A kind of rapid generation of SQL statement

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
RJ01 Rejection of invention patent application after publication
RJ01 Rejection of invention patent application after publication

Application publication date: 20180112