iSalesDB.m 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. //
  2. // iSalesDB.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 14-6-24.
  6. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "iSalesDB.h"
  9. #import "AppDelegate.h"
  10. @implementation iSalesDB
  11. +(void)testdata
  12. {
  13. //---------------- init db --------------------
  14. // NSString* date;
  15. // NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  16. // [formatter setDateFormat:@"YYYY-MM-dd-hh-mm-ss"];
  17. // date = [formatter stringFromDate:[NSDate date]];
  18. //
  19. // NSString* dbname=[NSString stringWithFormat:@"%@.db",date];
  20. //
  21. // DebugLog (@"initializeDB");
  22. //
  23. // // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  24. // NSString *documents = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];//[paths objectAtIndex:0];
  25. // NSString *database_path = [documents stringByAppendingPathComponent:dbname];
  26. //
  27. //
  28. // // move db file from document to cache ;
  29. // // NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  30. // NSString *documentFolderPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] ;//[searchPaths objectAtIndex: 0];
  31. // NSString* dbFilePath = [documentFolderPath stringByAppendingPathComponent:dbname];
  32. // if ([[NSFileManager defaultManager] fileExistsAtPath: dbFilePath])
  33. // {
  34. // [[NSFileManager defaultManager] moveItemAtPath:dbFilePath toPath:database_path error:nil];
  35. //
  36. // }
  37. // // end move;
  38. //
  39. sqlite3 *db = [iSalesDB get_db];
  40. NSString* drop_model =@"DROP TABLE IF EXISTS MODEL;";
  41. NSString* drop_IMAGE =@"DROP TABLE IF EXISTS model_image;";
  42. NSString* drop_image_price =@"DROP TABLE IF EXISTS model_price;";
  43. NSString* drop_category =@"DROP TABLE IF EXISTS category;";
  44. NSString* drop_login =@"DROP TABLE IF EXISTS offline_login;";
  45. NSString* drop_cart =@"DROP TABLE IF EXISTS offline_cart;";
  46. NSString* drop_wish =@"DROP TABLE IF EXISTS offline_wishlist;";
  47. NSString* drop_contact =@"DROP TABLE IF EXISTS offline_contact;";
  48. NSString* drop_contact_image =@"DROP TABLE IF EXISTS contact_image;";
  49. [iSalesDB execSql:drop_model db:db];
  50. [iSalesDB execSql:drop_IMAGE db:db];
  51. [iSalesDB execSql:drop_image_price db:db];
  52. [iSalesDB execSql:drop_category db:db];
  53. [iSalesDB execSql:drop_login db:db];
  54. [iSalesDB execSql:drop_cart db:db];
  55. [iSalesDB execSql:drop_wish db:db];
  56. [iSalesDB execSql:drop_contact db:db];
  57. [iSalesDB execSql:drop_contact_image db:db];
  58. NSString* create_model=@"CREATE TABLE IF NOT EXISTS model ( _id INTEGER PRIMARY KEY, name VARCHAR(20), description VARCHAR(20), product_id INTEGER, color VARCHAR(20), legcolor VARCHAR(20) ,availability INTEGER, incoming_stock INTEGER , demension VARCHAR(20), seat_height VARCHAR(20), meterial VARCHAR(20), box_dim VARCHAR(20), volume VARCHAR(20), weight VARCHAR(20), model_set VARCHAR(20), load_ability VARCHAR(20),default_category VARCHAR(20), category VARCHAR(100),fabric_content VARCHAR(20), assembling VARCHAR(20), made_in VARCHAR(20), special_remarks VARCHAR(20),stockUom integer,fashion VARCHAR(20), isnew integer,property_field VARCHAR(20),property_display VARCHAR(20),selector_field VARCHAR(20),selector_display VARCHAR(20),ETA DATE);";
  59. NSString* create_image=@"CREATE TABLE IF NOT EXISTS model_image ( _id INTEGER PRIMARY KEY, name VARCHAR(20), url VARCHAR(256), type integer, product_id INTEGER);";
  60. NSString* create_model_price=@"CREATE TABLE IF NOT EXISTS model_price ( _id INTEGER PRIMARY KEY, product_id INTEGER,price float , type integer);";
  61. NSString* create_category=@"CREATE TABLE IF NOT EXISTS category ( _id INTEGER PRIMARY KEY, code VARCHAR(20),name VARCHAR(20));";
  62. NSString* create_offline_login=@"CREATE TABLE IF NOT EXISTS offline_login ( _id INTEGER PRIMARY KEY, username VARCHAR(40),password VARCHAR(40), can_show_price integer ,can_see_price integer,contact_id VARCHAR(20),user_type integer,can_cancel_order integer,can_set_cart_price integer,can_create_portfolio integer, can_delete_order integer,can_submit_order integer,can_set_tearsheet_price integer,can_create_order integer, mode VARCHAR(20));";
  63. NSString* create_offline_cart=@"CREATE TABLE IF NOT EXISTS offline_cart ( _id INTEGER PRIMARY KEY, product_id INTEGER,price float , discount float , so_no VARCHAR(40));";
  64. NSString* create_offline_wish=@"CREATE TABLE IF NOT EXISTS offline_wishlist ( _id INTEGER PRIMARY KEY, product_id INTEGER);";
  65. NSString* create_offline_contact=@"CREATE TABLE IF NOT EXISTS offline_contact ( _id INTEGER PRIMARY KEY, country VARCHAR(40),company_name VARCHAR(40),contact_id VARCHAR(20),addr_1 text, addr_2 text , addr_3 text, addr_4 text, zipcode varchar(20),state VARCHAR(40), city VARCHAR(40), first_name VARCHAR(40) ,last_name VARCHAR(40),phone VARCHAR(40),fax VARCHAR(40),email VARCHAR(40),notes text, price_type VARCHAR(40), sales_rep VARCHAR(40), type VARCHAR(40),create_time timestamp,editable integer,contact_name VARCHAR(40) , addr text);";
  66. NSString* create_contact_image=@"CREATE TABLE IF NOT EXISTS contact_image ( _id INTEGER PRIMARY KEY, name VARCHAR(20), url VARCHAR(256), contact_id VARCHAR(20));";
  67. // NSString* create_model_category=@"CREATE TABLE IF NOT EXISTS model_category ( _id INTEGER PRIMARY KEY, product_id INTEGER,code VARCHAR(20));";
  68. // CREATE TABLE users (_id integer PRIMARY KEY,name varchar(20),pass varchar(20));
  69. // [self execSql:create_actions_info db:db];
  70. // [self execSql:create_fields_info db:db];
  71. // [self execSql:create_search_history db:db];
  72. // [self execSql:create_push_message db:db];
  73. // [self execSql:create_favorites db:db];
  74. // [self execSql:create_history db:db];
  75. // [self execSql:create_location db:db];
  76. [iSalesDB execSql:create_model db:db];
  77. [iSalesDB execSql:create_image db:db];
  78. [iSalesDB execSql:create_model_price db:db];
  79. [iSalesDB execSql:create_category db:db];
  80. [iSalesDB execSql:create_offline_login db:db];
  81. [iSalesDB execSql:create_offline_cart db:db];
  82. [iSalesDB execSql:create_offline_wish db:db];
  83. [iSalesDB execSql:create_offline_contact db:db];
  84. [iSalesDB execSql:create_contact_image db:db];
  85. NSString * insert_user_queeniey=@"insert into offline_login(can_show_price,can_see_price,contact_id,user_type,can_cancel_order,can_set_cart_price,can_create_portfolio,can_delete_order,can_submit_order,can_set_tearsheet_price,can_create_order,mode,username,password) values(1,1,'NPD',1,1,1,1,1,1,1,1,'Regular Mode','QueenieY','lj0EPk2Th9zZCVwrcskZOA==')";
  86. [iSalesDB execSql:insert_user_queeniey db:db];
  87. // [self execSql:create_model_category db:db];
  88. // if( ![self checkForField:@"search_history" field:@"level" db:db])
  89. // {
  90. // NSString* alter_search_history = @"ALTER TABLE search_history ADD level INTEGER";
  91. // [self execSql:alter_search_history db:db];
  92. // }
  93. // NSTimeInterval time=[[NSDate date] timeIntervalSince1970];
  94. // double t = time-2592000; //NSTimeInterval返回的是double类型
  95. // NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  96. // [formatter setDateFormat:@"yyyy-MM-dd"];
  97. //
  98. // NSString*timestr=[formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:t]];
  99. //
  100. // // [self execSql:[NSString stringWithFormat:@"delete from search_history where h_time<%@",timestr ] db:db];
  101. // [self execSql:@"delete from search_history where julianday('now', 'localtime')-julianday(h_time, 'localtime')>30" db:db];
  102. // int ret=sqlite3_close(db);
  103. DebugLog (@"bottom of initializeDb");
  104. NSString* IMS_S=@"http://113.28.30.235:80/site//u/NPD/20150715/3857_1455_s.jpg";
  105. //NSString* IMS_M=@"http://113.28.30.235:80/site//u/NPD/20150715/3857_1455_m.jpg";
  106. NSString* IMS_L=@"http://113.28.30.235:80/site//u/NPD/20150715/3857_1455_l.jpg";
  107. //----------------- fill data -------------------
  108. NSString* img_url1 = IMS_S;
  109. // NSString* img_url2 = IMS_M;
  110. NSString* img_url3 = IMS_L;
  111. CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
  112. [iSalesDB execSql:@"begin" db:db];
  113. // NSString* exec = @"";
  114. for(int i=0;i<1000;i++)
  115. {
  116. int category = arc4random() % 4;
  117. NSString* sql = [NSString stringWithFormat:@"insert into model(ETA,property_display,property_field,selector_display,isnew,selector_field,fashion,stockUom,name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,meterial,box_dim,volume,weight,model_set,load_ability,default_category,category,fabric_content,assembling,made_in,special_remarks) values('07/13/2016','Leg Color','legcolor','Color',1,'color','108526-48-BS%d',1,'108526-48-BS%d','Charlotte Fabric Counter Stool Brushed Smoke Legs, Putty%d',%d,'red color%d','white color%d',15%d,25%d,'20.00\"w 22.00\"d 39.00\"h%d','26.0\"h','Solid Birch Wood%d','20.50\"w 23.00\"d 40.00\"h',10.91%d,23.00%d,'Sold in quantities of 1','Load ability%d','%d','%d','77%% Polyester, 15%% Cotton, 8%% Linen', 'Fully Assembled;%d','China%d','Special Remarks%d');",i,i,i,i,i,i,i,i,i,i,i,i,i,category,category,i,i,i];
  118. NSString* sql_1=[NSString stringWithFormat:@"insert into model_price (product_id,price,type) values(%d,12.%d,0);",i,i%100];
  119. NSString* sql_2=[NSString stringWithFormat:@"insert into model_price (product_id,price,type) values(%d,12.%d,1);",i,i%100];
  120. NSString* sql_3=[NSString stringWithFormat:@"insert into model_price (product_id,price,type) values(%d,12.%d,2);",i,i%100];
  121. NSString* sql_4=[NSString stringWithFormat:@"insert into model_image(product_id,type,url) values(%d,0,'%@')",i,img_url1];
  122. NSString* sql_5=[NSString stringWithFormat:@"insert into model_image(product_id,type,url) values(%d,1,'%@')",i,img_url3];
  123. // exec=[exec stringByAppendingString:sql];
  124. //
  125. // exec=[exec stringByAppendingString:sql_1];
  126. // exec=[exec stringByAppendingString:sql_2];
  127. // exec=[exec stringByAppendingString:sql_3];
  128. [iSalesDB execSql:sql db:db];
  129. [iSalesDB execSql:sql_1 db:db];
  130. [iSalesDB execSql:sql_2 db:db];
  131. [iSalesDB execSql:sql_3 db:db];
  132. [iSalesDB execSql:sql_4 db:db];
  133. [iSalesDB execSql:sql_5 db:db];
  134. }
  135. // [iSalesDB execSql:exec db:db];
  136. [iSalesDB execSql:@"update model set category='#001003#;' where category='0'" db:db];
  137. [iSalesDB execSql:@"update model set category='#001001#;' where category='1'" db:db];
  138. [iSalesDB execSql:@"update model set category='#001002#;' where category='2'" db:db];
  139. [iSalesDB execSql:@"update model set category='#001003#;' where category='3'" db:db];
  140. [iSalesDB execSql:@"commit" db:db];
  141. sqlite3_close(db);
  142. CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
  143. // ((double)(begintime-endtime))/(1000*1000);
  144. NSLog(@"time cost: %0.3f", end - start);
  145. }
  146. + (sqlite3*) get_db
  147. {
  148. sqlite3* db = nil;
  149. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  150. NSString *documents = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];//[paths objectAtIndex:0];
  151. NSString *database_path = [documents stringByAppendingPathComponent:DBNAME];
  152. DebugLog(@"DB path:%@",database_path);
  153. if (sqlite3_open([database_path UTF8String], &db) != SQLITE_OK) {
  154. DebugLog(@"sqlite3_open failed. msg:%s",sqlite3_errmsg(db));
  155. sqlite3_close(db);
  156. }
  157. return db;
  158. }
  159. + (NSString* ) sqliteEscape :(NSString*) keyWord
  160. {
  161. keyWord = [keyWord stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
  162. keyWord = [keyWord stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  163. keyWord = [keyWord stringByReplacingOccurrencesOfString:@"[" withString:@"/["];
  164. keyWord = [keyWord stringByReplacingOccurrencesOfString:@"]" withString:@"/]"];
  165. keyWord = [keyWord stringByReplacingOccurrencesOfString:@"%" withString:@"/%"];
  166. keyWord = [keyWord stringByReplacingOccurrencesOfString:@"&" withString:@"/&"];
  167. keyWord = [keyWord stringByReplacingOccurrencesOfString:@"_" withString:@"/_"];
  168. keyWord = [keyWord stringByReplacingOccurrencesOfString:@"(" withString:@"/("];
  169. keyWord = [keyWord stringByReplacingOccurrencesOfString:@")" withString:@"/)"];
  170. return keyWord;
  171. }
  172. + (void) save_pdf: (NSString*) temp_path filename:(NSString*) filename
  173. {
  174. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  175. if(appDelegate.bEnable_Cache==false)
  176. return ;
  177. if(temp_path==nil)
  178. return;
  179. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  180. NSString *cachefolder = [paths objectAtIndex:0];
  181. NSString *pdf_cache = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  182. NSFileManager* fileManager = [NSFileManager defaultManager];
  183. BOOL bdir=YES;
  184. if(! [fileManager fileExistsAtPath:pdf_cache isDirectory:&bdir])
  185. {
  186. NSError *error = [[NSError alloc] init];
  187. bool bsuccess=[fileManager createDirectoryAtPath:pdf_cache withIntermediateDirectories:YES attributes:nil error:&error];
  188. if(!bsuccess)
  189. DebugLog(@"Create cache folder failed");
  190. // if(bsuccess)
  191. // {
  192. // sqlite3 *db = [self get_db];
  193. //
  194. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  195. // sqlite3_close(db);
  196. // }
  197. }
  198. NSString *save_path = [pdf_cache stringByAppendingPathComponent:filename];
  199. NSError *error = [[NSError alloc] init];
  200. bool bsuccess=[fileManager copyItemAtPath:temp_path toPath:save_path error:&error];
  201. if(bsuccess)
  202. DebugLog(@"PDF SAVE SUCCESS,%@",filename);
  203. else
  204. DebugLog(@"PDF SAVE FAILED,%@",filename);
  205. }
  206. + (void) cache_img: (NSData*) imgData :(NSString*) name
  207. {
  208. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  209. if(appDelegate.bEnable_Cache==false)
  210. return ;
  211. if(imgData==nil)
  212. return;
  213. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  214. NSString *cachefolder = [paths objectAtIndex:0];
  215. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  216. NSFileManager* fileManager = [NSFileManager defaultManager];
  217. BOOL bdir=YES;
  218. if(! [fileManager fileExistsAtPath:img_cache isDirectory:&bdir])
  219. {
  220. NSError *error = [[NSError alloc] init];
  221. bool bsuccess=[fileManager createDirectoryAtPath:img_cache withIntermediateDirectories:YES attributes:nil error:&error];
  222. if(!bsuccess)
  223. DebugLog(@"Create cache folder failed");
  224. // if(bsuccess)
  225. // {
  226. // sqlite3 *db = [self get_db];
  227. //
  228. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  229. // sqlite3_close(db);
  230. // }
  231. }
  232. NSString *filePath = [img_cache stringByAppendingPathComponent:name];
  233. bool bsuccess=[imgData writeToFile:filePath atomically:YES];
  234. if(bsuccess)
  235. DebugLog(@"IMG CACHE SUCCESS,%@",name);
  236. else
  237. DebugLog(@"IMG CACHE FAILED,%@",name);
  238. }
  239. + (NSData*) load_cached_img:(NSString*) filename
  240. {
  241. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  242. if(appDelegate.bEnable_Cache==false)
  243. return nil;
  244. NSData* data = nil;
  245. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  246. NSString *cachefolder = [paths objectAtIndex:0];
  247. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  248. NSString *filePath = [img_cache stringByAppendingPathComponent:filename];
  249. NSFileManager* fileManager = [NSFileManager defaultManager];
  250. if( [fileManager fileExistsAtPath:filePath ])
  251. {
  252. data = [NSData dataWithContentsOfFile: filePath];
  253. }
  254. // NSString* sqliteQuery = [NSString stringWithFormat:@"SELECT img FROM img_cache WHERE name = '%@'", filename];
  255. // sqlite3_stmt* statement;
  256. // sqlite3 *db = [self get_db];
  257. //
  258. //// if( sqlite3_prepare_v2(db, [sqliteQuery UTF8String], -1, &statement, NULL) == SQLITE_OK )
  259. //// {
  260. //// if( sqlite3_step(statement) == SQLITE_ROW )
  261. //// {
  262. //// int length = sqlite3_column_bytes(statement, 0);
  263. //// data = [NSData dataWithBytes:sqlite3_column_blob(statement, 0) length:length];
  264. //// }
  265. //// }
  266. //
  267. // // Finalize and close database.
  268. // sqlite3_finalize(statement);
  269. // sqlite3_close(db);
  270. return data;
  271. }
  272. + (BOOL) initializeDb {
  273. DebugLog (@"initializeDB");
  274. return false;
  275. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  276. NSString *documents = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];//[paths objectAtIndex:0];
  277. NSString *database_path = [documents stringByAppendingPathComponent:DBNAME];
  278. // move db file from document to cache ;
  279. // NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  280. NSString *documentFolderPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] ;//[searchPaths objectAtIndex: 0];
  281. NSString* dbFilePath = [documentFolderPath stringByAppendingPathComponent:DBNAME];
  282. if ([[NSFileManager defaultManager] fileExistsAtPath: dbFilePath])
  283. {
  284. [[NSFileManager defaultManager] moveItemAtPath:dbFilePath toPath:database_path error:nil];
  285. }
  286. // end move;
  287. if (true)//! [[NSFileManager defaultManager] fileExistsAtPath: database_path])
  288. {
  289. sqlite3 *db = [self get_db];
  290. NSString* create_model=@"CREATE TABLE IF NOT EXISTS model ( _id INTEGER PRIMARY KEY, name VARCHAR(20), description VARCHAR(20), product_id INTEGER, color VARCHAR(20), legcolor VARCHAR(20) ,availability INTEGER, incoming_stock INTEGER ,ETA DATE, demension VARCHAR(20), seat_height VARCHAR(20), meterial VARCHAR(20), box_dim VARCHAR(20), volume VARCHAR(20), weight VARCHAR(20), model_set VARCHAR(20), load_ability VARCHAR(20),default_category VARCHAR(20), category VARCHAR(100),fabric_content VARCHAR(20), assembling VARCHAR(20), made_in VARCHAR(20), special_remarks VARCHAR(20),stockUom integer,fashion VARCHAR(20), isnew integer,property_field VARCHAR(20),property_display VARCHAR(20),selector_field VARCHAR(20),selector_display VARCHAR(20));";
  291. NSString* create_image=@"CREATE TABLE IF NOT EXISTS model_image ( _id INTEGER PRIMARY KEY, name VARCHAR(20), url VARCHAR(256), type integer, product_id INTEGER);";
  292. NSString* create_model_price=@"CREATE TABLE IF NOT EXISTS model_price ( _id INTEGER PRIMARY KEY, product_id INTEGER,price float , type integer, price_name varchar(30));";
  293. NSString* create_category=@"CREATE TABLE IF NOT EXISTS category ( _id INTEGER PRIMARY KEY, code VARCHAR(20),name VARCHAR(20));";
  294. NSString* create_offline_setting=@"CREATE TABLE IF NOT EXISTS offline_setting ( _id INTEGER PRIMARY KEY, name VARCHAR(20),value VARCHAR(40));";
  295. NSString* create_offline_login=@"CREATE TABLE IF NOT EXISTS offline_login ( _id INTEGER PRIMARY KEY, username VARCHAR(40),password VARCHAR(40), can_show_price integer ,can_see_price integer,contact_id VARCHAR(20),user_type integer,can_cancel_order integer,can_set_cart_price integer,can_create_portfolio integer, can_delete_order integer,can_submit_order integer,can_set_tearsheet_price integer,can_create_order integer, mode VARCHAR(20));";
  296. NSString* create_offline_cart=@"CREATE TABLE IF NOT EXISTS offline_cart ( _id INTEGER PRIMARY KEY, product_id INTEGER,price float , discount float , so_no VARCHAR(40));";
  297. NSString* create_offline_wish=@"CREATE TABLE IF NOT EXISTS offline_wishlist ( _id INTEGER PRIMARY KEY, product_id INTEGER);";
  298. NSString* create_offline_contact=@"CREATE TABLE IF NOT EXISTS offline_contact ( _id INTEGER PRIMARY KEY, country VARCHAR(40),company_name VARCHAR(40),contact_id VARCHAR(20),addr_1 text, addr_2 text , addr_3 text, addr_4 text, zipcode varchar(20),state VARCHAR(40), city VARCHAR(40), first_name VARCHAR(40) ,last_name VARCHAR(40),phone VARCHAR(40),fax VARCHAR(40),email VARCHAR(40),notes text, price_type VARCHAR(40), sales_rep VARCHAR(40), type VARCHAR(40),create_time timestamp,editable integer,contact_name VARCHAR(40) , addr text);";
  299. NSString* create_contact_image=@"CREATE TABLE IF NOT EXISTS contact_image ( _id INTEGER PRIMARY KEY, name VARCHAR(20), url VARCHAR(256), contact_id VARCHAR(20));";
  300. // CREATE TABLE users (_id integer PRIMARY KEY,name varchar(20),pass varchar(20));
  301. // [self execSql:create_actions_info db:db];
  302. // [self execSql:create_fields_info db:db];
  303. // [self execSql:create_search_history db:db];
  304. // [self execSql:create_push_message db:db];
  305. // [self execSql:create_favorites db:db];
  306. // [self execSql:create_history db:db];
  307. // [self execSql:create_location db:db];
  308. [self execSql:create_model db:db];
  309. [self execSql:create_image db:db];
  310. [self execSql:create_model_price db:db];
  311. [self execSql:create_category db:db];
  312. [self execSql:create_offline_setting db:db];
  313. [self execSql:create_offline_login db:db];
  314. [self execSql:create_offline_cart db:db];
  315. [self execSql:create_offline_wish db:db];
  316. [self execSql:create_offline_contact db:db];
  317. [self execSql:create_contact_image db:db];
  318. // if( ![self checkForField:@"search_history" field:@"level" db:db])
  319. // {
  320. // NSString* alter_search_history = @"ALTER TABLE search_history ADD level INTEGER";
  321. // [self execSql:alter_search_history db:db];
  322. // }
  323. // NSTimeInterval time=[[NSDate date] timeIntervalSince1970];
  324. // double t = time-2592000; //NSTimeInterval返回的是double类型
  325. // NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  326. // [formatter setDateFormat:@"yyyy-MM-dd"];
  327. //
  328. // NSString*timestr=[formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:t]];
  329. //
  330. // // [self execSql:[NSString stringWithFormat:@"delete from search_history where h_time<%@",timestr ] db:db];
  331. // [self execSql:@"delete from search_history where julianday('now', 'localtime')-julianday(h_time, 'localtime')>30" db:db];
  332. int ret=sqlite3_close(db);
  333. return ret;
  334. // int aaa = 0;
  335. }
  336. return YES;
  337. DebugLog (@"bottom of initializeDb");
  338. }
  339. +(void)execSql:(NSString *)sql db:(sqlite3 *)db
  340. {
  341. char *err;
  342. if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK) {
  343. DebugLog(@"sqlite3_exec failed msg:%s",sqlite3_errmsg(db));
  344. DebugLog(@"sqlite3_exec failed sql:%@",sql);
  345. // sqlite3_close(db);
  346. }
  347. }
  348. + (NSDictionary*) search_pdf:(int) offset limit:(int)limit keywords:(NSString*) keywords
  349. {
  350. sqlite3 *db = [self get_db];
  351. NSMutableDictionary* json = [[NSMutableDictionary alloc] init];
  352. [json setValue:@"1" forKey:@"result"];
  353. NSString *sqlQuery =nil;
  354. if(keywords.length>0)
  355. sqlQuery=[NSString stringWithFormat: @"SELECT _id,name,file_name,send_to,create_params,c_id,c_name,o_id,pdf_type,create_time FROM pdf_cache where lower(name) like '%%%@%%' order by create_time desc limit %d offset %d",keywords,limit,offset];
  356. else
  357. sqlQuery=[NSString stringWithFormat: @"SELECT _id,name,file_name,send_to,create_params,c_id,c_name,o_id,pdf_type,create_time FROM pdf_cache order by create_time desc limit %d offset %d",limit,offset];
  358. sqlite3_stmt * statement;
  359. int count = 0;
  360. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  361. {
  362. while (sqlite3_step(statement) == SQLITE_ROW) {
  363. int _id = sqlite3_column_int(statement, 0);
  364. char *name = (char*)sqlite3_column_text(statement, 1);
  365. NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  366. char *file_name = (char*)sqlite3_column_text(statement, 2);
  367. NSString *nsFileNameStr = [[NSString alloc]initWithUTF8String:file_name];
  368. char *type = (char*)sqlite3_column_text(statement, 8);
  369. NSString *nsTypeStr = [[NSString alloc]initWithUTF8String:type];
  370. char *create_time = (char*)sqlite3_column_text(statement, 9);
  371. NSString *nsTimeStr = [[NSString alloc]initWithUTF8String:create_time];
  372. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  373. [item setValue:[NSString stringWithFormat:@"%d",_id] forKey:@"_id"];
  374. [item setValue:nsNameStr forKey:@"name"];
  375. [item setValue:nsTimeStr forKey:@"create_time"];
  376. [item setValue:nsTypeStr forKey:@"type"];
  377. [item setValue:nsFileNameStr forKey:@"file_name"];
  378. [json setObject:item forKey:[NSString stringWithFormat:@"item_%d",count]];
  379. count++;
  380. }
  381. [json setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  382. [json setValue:@"2" forKey:@"result"];
  383. }
  384. sqlite3_close(db);
  385. return json;
  386. }
  387. +(void)execSql:(NSString *)sql
  388. {
  389. sqlite3 *db = [self get_db];
  390. [self execSql:sql db:db];
  391. sqlite3_close(db);
  392. }
  393. + (int) get_recordid:(NSString*) tablename where:(NSString*) whereclause
  394. {
  395. // [iSalesDB execSql: [NSString stringWithFormat: @"update fields_info set abandon = 1 where user ='%@'",user] db:db];
  396. return [self get_recordid:tablename where:whereclause order:@"_id"];
  397. }
  398. + (int) get_recordcount:(sqlite3*)db table:(NSString*) tablename where:(NSString*) whereclause
  399. {
  400. //sqlite3 *db = [self get_db];
  401. int ret = -1;
  402. NSString *sqlQuery = [NSString stringWithFormat:@"select count(0) from %@ where %@ ;",tablename,whereclause];
  403. DebugLog(@"get_recordcount sql:%@",sqlQuery);
  404. sqlite3_stmt * statement;
  405. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  406. {
  407. if (sqlite3_step(statement) == SQLITE_ROW)
  408. {
  409. // char *name = (char*)sqlite3_column_text(statement, 1);
  410. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  411. ret = sqlite3_column_int(statement, 0);
  412. // char *address = (char*)sqlite3_column_text(statement, 3);
  413. // NSString *nsAddressStr = [[NSString alloc]initWithUTF8String:address];
  414. }
  415. sqlite3_finalize(statement);
  416. }
  417. // sqlite3_close(db);
  418. return ret;
  419. }
  420. + (int) get_recordid:(NSString*) tablename where:(NSString*) whereclause order:(NSString*) orderby
  421. {
  422. sqlite3 *db = [self get_db];
  423. int ret = -1;
  424. NSString *sqlQuery = [NSString stringWithFormat:@"select _id from %@ where %@ orderby %@",tablename,whereclause,orderby];
  425. sqlite3_stmt * statement;
  426. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  427. {
  428. if (sqlite3_step(statement) == SQLITE_ROW)
  429. {
  430. // char *name = (char*)sqlite3_column_text(statement, 1);
  431. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  432. ret = sqlite3_column_int(statement, 0);
  433. // char *address = (char*)sqlite3_column_text(statement, 3);
  434. // NSString *nsAddressStr = [[NSString alloc]initWithUTF8String:address];
  435. }
  436. sqlite3_finalize(statement);
  437. }
  438. sqlite3_close(db);
  439. return ret;
  440. }
  441. + (int) get_recordid:(sqlite3*)db table:(NSString*) tablename where:(NSString*) whereclause
  442. {
  443. // [iSalesDB execSql: [NSString stringWithFormat: @"update fields_info set abandon = 1 where user ='%@'",user] db:db];
  444. return [self get_recordid:db table:tablename where:whereclause order:@"_id"];
  445. }
  446. + (int) get_recordid:(sqlite3*)db table:(NSString*) tablename where:(NSString*) whereclause order:(NSString*) orderby
  447. {
  448. int ret = -1;
  449. NSString *sqlQuery = [NSString stringWithFormat:@"select _id from %@ where %@ orderby %@",tablename,whereclause,orderby];
  450. sqlite3_stmt * statement;
  451. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  452. {
  453. if (sqlite3_step(statement) == SQLITE_ROW)
  454. {
  455. // char *name = (char*)sqlite3_column_text(statement, 1);
  456. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  457. ret = sqlite3_column_int(statement, 0);
  458. // char *address = (char*)sqlite3_column_text(statement, 3);
  459. // NSString *nsAddressStr = [[NSString alloc]initWithUTF8String:address];
  460. }
  461. sqlite3_finalize(statement);
  462. }
  463. return ret;
  464. }
  465. @end