LKDBUtils.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //
  2. // NSObject+LKUtils.m
  3. // LKDBHelper
  4. //
  5. // Created by LJH on 13-4-15.
  6. // Copyright (c) 2013年 ljh. All rights reserved.
  7. //
  8. #import "LKDBUtils.h"
  9. @implementation LKDateFormatter {
  10. dispatch_semaphore_t _lock;
  11. }
  12. - (instancetype)init {
  13. self = [super init];
  14. if (self) {
  15. _lock = dispatch_semaphore_create(1);
  16. self.generatesCalendarDates = NO;
  17. self.dateStyle = NSDateFormatterNoStyle;
  18. self.timeStyle = NSDateFormatterNoStyle;
  19. self.AMSymbol = nil;
  20. self.PMSymbol = nil;
  21. self.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  22. self.calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
  23. if ([LKDBUtils respondsToSelector:@selector(onCreateWithDateFormatter:)]) {
  24. [LKDBUtils onCreateWithDateFormatter:self];
  25. }
  26. }
  27. return self;
  28. }
  29. - (dispatch_semaphore_t)lock {
  30. return _lock;
  31. }
  32. //防止 iOS5 多线程 格式化时间时 崩溃
  33. - (NSDate *)dateFromString:(NSString *)string {
  34. dispatch_semaphore_wait(_lock, DISPATCH_TIME_FOREVER);
  35. NSDate *date = [super dateFromString:string];
  36. dispatch_semaphore_signal(_lock);
  37. return date;
  38. }
  39. - (NSString *)stringFromDate:(NSDate *)date {
  40. dispatch_semaphore_wait(_lock, DISPATCH_TIME_FOREVER);
  41. NSString *string = [super stringFromDate:date];
  42. dispatch_semaphore_signal(_lock);
  43. return string;
  44. }
  45. @end
  46. @implementation LKNumberFormatter
  47. - (instancetype)init {
  48. self = [super init];
  49. if (self) {
  50. if ([LKDBUtils respondsToSelector:@selector(onCreateWithNumberFormatter:)]) {
  51. [LKDBUtils onCreateWithNumberFormatter:self];
  52. }
  53. }
  54. return self;
  55. }
  56. - (NSString *)stringFromNumber:(NSNumber *)number {
  57. NSString *string = [number stringValue];
  58. if (!string) {
  59. string = [NSString stringWithFormat:@"%lf", [number doubleValue]];
  60. }
  61. return string;
  62. }
  63. - (NSNumber *)numberFromString:(NSString *)string {
  64. NSNumber *number = nil;
  65. if ([string rangeOfString:@"."].length > 0) {
  66. number = [NSNumber numberWithDouble:string.doubleValue];
  67. } else {
  68. number = [NSNumber numberWithLongLong:string.longLongValue];
  69. }
  70. return number;
  71. }
  72. @end
  73. @implementation LKDBUtils
  74. + (BOOL)createDirectoryWithFilePath:(NSString *)filePath {
  75. NSString *dirPath = filePath.stringByDeletingLastPathComponent;
  76. if (!dirPath) {
  77. return NO;
  78. }
  79. NSFileManager *fileManager = [NSFileManager defaultManager];
  80. BOOL isDir = NO;
  81. BOOL isCreated = [fileManager fileExistsAtPath:dirPath isDirectory:&isDir];
  82. #ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
  83. NSDictionary *attributes = @{NSFileProtectionKey: NSFileProtectionNone};
  84. #else
  85. NSDictionary *attributes = nil;
  86. #endif
  87. if (!isCreated || !isDir) {
  88. NSError *error = nil;
  89. BOOL success = [fileManager createDirectoryAtPath:dirPath
  90. withIntermediateDirectories:YES
  91. attributes:attributes
  92. error:&error];
  93. if (!success) {
  94. LKErrorLog(@"create dir error: %@", error.debugDescription);
  95. /// 下个主线程继续尝试次
  96. dispatch_async(dispatch_get_main_queue(), ^{
  97. [fileManager createDirectoryAtPath:dirPath
  98. withIntermediateDirectories:YES
  99. attributes:attributes
  100. error:nil];
  101. });
  102. }
  103. return success;
  104. } else {
  105. /**
  106. * @brief Disk I/O error when device is locked
  107. * https://github.com/ccgus/fmdb/issues/262
  108. */
  109. #ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
  110. [fileManager setAttributes:attributes
  111. ofItemAtPath:dirPath
  112. error:nil];
  113. #endif
  114. return YES;
  115. }
  116. }
  117. + (NSString *)getDocumentPath {
  118. #ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
  119. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  120. NSString *documentsDirectory = [paths objectAtIndex:0];
  121. return documentsDirectory;
  122. #else
  123. NSString *homePath = [[NSBundle mainBundle] resourcePath];
  124. return homePath;
  125. #endif
  126. }
  127. + (NSString *)getDirectoryForDocuments:(NSString *)dir {
  128. NSString *dirPath = [[self getDocumentPath] stringByAppendingPathComponent:dir];
  129. BOOL isDir = NO;
  130. BOOL isCreated = [[NSFileManager defaultManager] fileExistsAtPath:dirPath isDirectory:&isDir];
  131. if (isCreated == NO || isDir == NO) {
  132. NSError *error = nil;
  133. BOOL success = [[NSFileManager defaultManager] createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:&error];
  134. if (success == NO)
  135. MOLogV(@"create dir error: %@", error.debugDescription);
  136. }
  137. return dirPath;
  138. }
  139. + (NSString *)getPathForDocuments:(NSString *)filename {
  140. return [[self getDocumentPath] stringByAppendingPathComponent:filename];
  141. }
  142. + (NSString *)getPathForDocuments:(NSString *)filename inDir:(NSString *)dir {
  143. return [[self getDirectoryForDocuments:dir] stringByAppendingPathComponent:filename];
  144. }
  145. + (BOOL)isFileExists:(NSString *)filepath {
  146. return [[NSFileManager defaultManager] fileExistsAtPath:filepath];
  147. }
  148. + (BOOL)deleteWithFilepath:(NSString *)filepath {
  149. return [[NSFileManager defaultManager] removeItemAtPath:filepath error:nil];
  150. }
  151. + (NSArray *)getFilenamesWithDir:(NSString *)dir {
  152. NSFileManager *fileManager = [NSFileManager defaultManager];
  153. NSArray *fileList = [fileManager contentsOfDirectoryAtPath:dir error:nil];
  154. return fileList;
  155. }
  156. + (BOOL)checkStringIsEmpty:(NSString *)string {
  157. if (string == nil) {
  158. return YES;
  159. }
  160. if ([string isKindOfClass:[NSString class]] == NO) {
  161. return YES;
  162. }
  163. if (string.length == 0) {
  164. return YES;
  165. }
  166. return [[self getTrimStringWithString:string] isEqualToString:@""];
  167. }
  168. + (NSString *)getTrimStringWithString:(NSString *)string {
  169. return [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  170. }
  171. + (NSDateFormatter *)getDBDateFormat {
  172. static NSDateFormatter *format;
  173. static dispatch_once_t onceToken;
  174. dispatch_once(&onceToken, ^{
  175. format = [[LKDateFormatter alloc] init];
  176. format.dateFormat = @"yyyy-MM-dd HH:mm:ss";
  177. });
  178. return format;
  179. }
  180. + (NSString *)stringWithDate:(NSDate *)date {
  181. NSDateFormatter *formatter = [self getDBDateFormat];
  182. NSString *datestr = [formatter stringFromDate:date];
  183. if (datestr.length > 19) {
  184. datestr = [datestr substringToIndex:19];
  185. }
  186. return datestr;
  187. }
  188. + (NSDate *)dateWithString:(NSString *)str {
  189. NSDateFormatter *formatter = [self getDBDateFormat];
  190. NSDate *date = [formatter dateFromString:str];
  191. return date;
  192. }
  193. + (NSNumberFormatter *)numberFormatter {
  194. static NSNumberFormatter *numberFormatter = nil;
  195. static dispatch_once_t onceToken;
  196. dispatch_once(&onceToken, ^{
  197. numberFormatter = [[LKNumberFormatter alloc] init];
  198. });
  199. return numberFormatter;
  200. }
  201. @end
  202. inline NSString *LKSQLTypeFromObjcType(NSString *objcType) {
  203. if ([LKSQL_Convert_IntType rangeOfString:objcType].length > 0) {
  204. return LKSQL_Type_Int;
  205. }
  206. if ([LKSQL_Convert_FloatType rangeOfString:objcType].length > 0) {
  207. return LKSQL_Type_Double;
  208. }
  209. if ([LKSQL_Convert_BlobType rangeOfString:objcType].length > 0) {
  210. return LKSQL_Type_Blob;
  211. }
  212. return LKSQL_Type_Text;
  213. }
  214. @implementation LKDBQueryParams
  215. @end