<TD class="line-numbers" style=‘padding: 12px 6px; text-align: right; color: rgb(170, 170, 170); line-height: 1.4; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 12px; border-right-color: rgb(238, 238, 238); border-right-width: 1px; border-right-style: solid; min-width: 27px; box-sizing: border-box; -webkit-user-select: none;‘>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
<TD class="line-data" style=‘padding: 12px 0px 12px 12px; width: 1189px; line-height: 1.4; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 12px;‘>
<PRE class="line-pre" style=‘padding: 0px; width: 1189px; line-height: 1.4; font-family: Consolas, "Liberation Mono", Courier, monospace; margin-top: 0px; margin-bottom: 0px;‘>
#import
“DYLogKeeper+DB.h”
#import
“DYDB.h”
#import
<FMDB/FMDatabase.h>
#import
“DYUUID.h”
#import
“NSDate+NSDateFormaterCategory.h”
DYLogKeeper*rs2logkeeper(FMResultSet*rs){
DYLogKeeper*obj=[[DYLogKeeperalloc]init];
obj.localId=[rsstringForColumn:@”local_id”];
obj.logkeeperId=[rsstringForColumn:@”logkeeper_id”];
obj.addtime=[rsdateForColumn:@”add_time”];
obj.content=[rsstringForColumn:@”content”];
obj.deviceId=[rsstringForColumn:@”device_id”];
obj.deviceType=[rsintForColumn:@”device_type”];
obj.channel=[rsintForColumn:@”channel”];
returnobj;
}
@implementationDYLogKeeper(DB)
+(void)createSqliteTable{
DLog(@”check table is
exists?”);
FMDatabase*db=[[DYDBsharedDB]connect];
NSString*existsSql=[NSStringstringWithFormat:@”select
count(name) as countNum from sqlite_master where type = ‘table’ and
name = ‘%@’”,@”log_keepers”];
DLog(@”%@”,existsSql);
FMResultSet*rs=[dbexecuteQuery:existsSql];
if([rsnext]){
NSIntegercount=[rsintForColumn:@”countNum”];
DLog(@”The table count: %d”,count);
if(count==1){
DLog(@”log_keepers table is
existed.”);
return;
}
DLog(@”log_keepers is not
existed.”);
}
[rsclose];
DLog(@”create table ….”);
NSString*filePath=[[NSBundlemainBundle]pathForResource:@”log_keepers_table”ofType:@”sql”];
DLog(@”logkeeper sql file:
%@”,filePath);
NSError*error;
NSString*sql=[NSStringstringWithContentsOfFile:filePathencoding:NSUTF8StringEncodingerror:&error];
if(error!=nil){
DLog(@”fail to read sql file:
%@”,[errordescription]);
return;
}
DLog(@”—sql:n%@”,sql);
DLog(@”execute sql ….”);
if([dbexecuteUpdate:sql]){
DLog(@”create table
succes….”);
}else{
DLog(@”fail to execute update
sql..”);
}
[dbclose];
}
+(BOOL)insert:(DYLogKeeper*)logkeeper{
DLog(@”insert logkeeper”);
DLog(@”convert int value to NSNumber
…”);
logkeeper.localId=[DYUUIDuuidString];
NSNumber*channelNum=[NSNumbernumberWithInt:logkeeper.channel];
DLog(@”— channelNum: %@”,channelNum);
NSNumber*typeNumber=[NSNumbernumberWithInt:logkeeper.deviceType];
DLog(@”— deviceType: %@”,typeNumber);
NSString*sql=@”insert into log_keepers(local_id,
logkeeper_id, add_time, content, device_id, device_type, channel)
values(?, ?, ?, ?, ?, ?, ?)”;
FMDatabase*db=[[DYDBsharedDB]connect];
if(db==nil){
DLog(@”fail to create db..”);
}
BOOLret=[dbexecuteUpdate:sql,logkeeper.localId,logkeeper.logkeeperId,
logkeeper.addtime,logkeeper.content,
logkeeper.deviceId,typeNumber,channelNum];
[dbclose];
returnret;
}
+(BOOL)updateContent:(NSString*)content
localId:(NSString*)localId{
DLog(@”update logkeeper
content”);
NSString*sql=@”update log_keepers set content =
? where local_id = ?”;
FMDatabase*db=[[DYDBsharedDB]connect];
BOOLret=[dbexecuteUpdate:sql,content,localId];
[dbclose];
returnret;
}
-(BOOL)remove:(DYLogKeeper*)logkeeper{
DLog(@”remove logkeeper: %@”,logkeeper.localId);
NSString*sql=@”delete from log_keepers where
local_id = ?”;
FMDatabase*db=[[DYDBsharedDB]connect];
BOOLret=[dbexecuteUpdate:sql,logkeeper.localId];
[dbclose];
returnret;
}
-(DYLogKeeper*)findById:(NSString*)localId{
DLog(@”find logkeeper by id:
%@”,localId);
FMDatabase*db=[[DYDBsharedDB]connect];
FMResultSet*rs=[dbexecuteQuery:@”select
* from log_keepers where local_id = ?”,localId];
DYLogKeeper*ret;
if([rsnext]){
ret=rs2logkeeper(rs);
}
[dbclose];
returnret;
}
+(NSArray*)findOfStartDate:(NSDate*)starttoDate:(NSDate*)toDate{
DLog(@”find logkeeper between date
….”);
NSString*sql=@”select * from log_keepers where
add_time between ? and ?”;
FMDatabase*db=[[DYDBsharedDB]connect];
FMResultSet*rs=[dbexecuteQuery:sql,start,toDate];
NSMutableArray*array=[NSMutableArrayarrayWithCapacity:32];
while([rsnext]){
DYLogKeeper*logkeeper=rs2logkeeper(rs);
[arrayaddObject:logkeeper];
}
[rsclose];
[dbclose];
returnarray;
}
@end