yptools 1.0.11 → 1.0.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2221 @@
1
+ # -*- coding: UTF-8 -*-
2
+
3
+ require 'pathname'
4
+ require 'colored'
5
+ require 'plist'
6
+ require 'json'
7
+ require_relative '../log/yp_log'
8
+ require_relative '../mgc/yp_makegarbagecode'
9
+
10
+
11
+ class YPAutoCreate
12
+
13
+ @yp_filePath
14
+ @yp_fileName
15
+ @yp_fileBasename
16
+ @yp_fileSuffix
17
+ @yp_fileDirname
18
+ @yp_path
19
+ @yp_completePath
20
+ @yp_json
21
+
22
+ @yp_json_mode
23
+ @yp_json_module
24
+ @yp_json_table
25
+ @yp_json_property
26
+ @yp_json_index
27
+ @yp_json_unique
28
+ @yp_json_normal
29
+ @yp_json_struct
30
+
31
+ @yp_dao
32
+ @yp_dao_h
33
+ @yp_dao_m
34
+ @yp_create_h
35
+ @yp_create_m
36
+
37
+ @yp_propertys
38
+ @yp_propertys_type
39
+ @yp_primarykey
40
+ @yp_bool_haveprimarykey
41
+ @yp_bool_havepropertys
42
+ @yp_uniques
43
+ @yp_normals
44
+ @yp_struct
45
+ @yp_copyArray
46
+ @yp_bool_autoincrement
47
+ @yp_need_log #控制是否使用YPLog 默认先关闭
48
+
49
+ def self.createObjcSQL (filePath)
50
+
51
+ @yp_filePath = filePath
52
+
53
+ fileName = File.basename(filePath)
54
+ fileBasename = File.basename(filePath, ".*")
55
+ fileSuffix = File.extname(filePath)
56
+ fileDirname = File.dirname(filePath)
57
+
58
+ yp_path = `pwd`
59
+ yp_path = yp_path.sub("\n","")
60
+
61
+ yp_completePath = fileDirname + "/#{fileName}"
62
+ yp_file = File.read(filePath)
63
+ yp_json = JSON.parse(yp_file)
64
+
65
+ yp_json_mode = yp_json["mode"]
66
+ yp_json_module = yp_json["module"]
67
+ yp_json_table = yp_json["table"]
68
+ yp_json_property = yp_json["property"]
69
+
70
+ yp_json_index = Hash.new
71
+ if yp_json["index"] != nil
72
+ yp_json_index = yp_json["index"]
73
+ end
74
+
75
+ yp_json_unique = Hash.new
76
+ if yp_json["unique"] != nil
77
+ yp_json_unique = yp_json["unique"]
78
+ end
79
+
80
+ yp_json_normal = Hash.new
81
+ if yp_json["normal"] != nil
82
+ yp_json_normal = yp_json["normal"]
83
+ end
84
+
85
+ yp_json_struct = Hash.new
86
+ if yp_json["struct"] != nil
87
+ yp_json_struct = yp_json["struct"]
88
+ end
89
+
90
+ yp_error = ""
91
+ if yp_json_mode.class == NilClass || yp_json_mode.length == 0
92
+ yp_error = "mode 参数为空了"
93
+ end
94
+
95
+ if yp_json_module.class == NilClass || yp_json_module.length == 0
96
+ yp_error = "module 参数为空了"
97
+ end
98
+
99
+ if yp_json_table.class == NilClass || yp_json_table.length == 0
100
+ yp_error = "table 参数为空了"
101
+ end
102
+
103
+ if yp_json_property.class == NilClass
104
+ yp_error = "property 参数为空了"
105
+ elsif yp_json_property.class != Hash
106
+ yp_error = "property 不是json"
107
+ end
108
+
109
+ if yp_json_index.class != Hash && yp_json_index.class != NilClass
110
+ yp_error = "index 不是json"
111
+ end
112
+
113
+ if yp_json_struct.class != Hash && yp_json_struct.class != NilClass
114
+ yp_error = "struct 不是json"
115
+ end
116
+
117
+ if yp_error.length != 0
118
+ yp_log_fail "构建失败,#{yp_error}"
119
+ return
120
+ end
121
+
122
+ yp_dao = "#{yp_json_module}Dao"
123
+ yp_dao_h = "/#{yp_dao}.h"
124
+ yp_dao_m = "/#{yp_dao}.m"
125
+
126
+ yp_create_h = fileDirname + yp_dao_h
127
+ yp_create_m = fileDirname + yp_dao_m
128
+
129
+ if File.exists?(yp_create_h)
130
+ File.delete(yp_create_h)
131
+ end
132
+
133
+ if File.exists?(yp_create_m)
134
+ File.delete(yp_create_m)
135
+ end
136
+
137
+ # model
138
+ yp_h_contents = Array.new
139
+ yp_m_contents = Array.new
140
+
141
+ yp_h_contents.push "#import <Foundation/Foundation.h>"
142
+ yp_h_contents.push("\n")
143
+ yp_h_contents.push '#import "FMDatabaseQueue.h"'
144
+ yp_h_contents.push("\n")
145
+ yp_h_contents.push '#import <CoreGraphics/CoreGraphics.h>'
146
+ yp_h_contents.push("\n")
147
+ yp_h_contents.push("\n")
148
+
149
+ yp_m_contents.push "#import \"#{yp_json_module}Dao.h\""
150
+ yp_m_contents.push("\n")
151
+ yp_m_contents.push '#import <FMDB/FMDB.h>'
152
+ yp_m_contents.push("\n")
153
+ if @yp_need_log == 1
154
+ yp_m_contents.push '#import <YPLog/YPLog.h>'
155
+ yp_m_contents.push("\n")
156
+ end
157
+ yp_m_contents.push("\n")
158
+ yp_m_contents.push("#define DATABASE_NAME @\"welldown_enc.sqlite\"")
159
+ yp_m_contents.push("\n")
160
+ yp_m_contents.push("\n")
161
+
162
+ yp_h_contents.push "@interface #{yp_json_module} : NSObject <NSCopying>"
163
+ yp_h_contents.push("\n")
164
+
165
+ yp_m_contents.push "@implementation #{yp_json_module}"
166
+ yp_m_contents.push("\n")
167
+ yp_m_contents.push "- (id)copyWithZone:(NSZone *)zone {"
168
+ yp_m_contents.push("\n")
169
+ yp_m_contents.push " #{yp_json_module} *copy = (#{yp_json_module} *)[[[self class] allocWithZone:zone] init];"
170
+ yp_m_contents.push("\n")
171
+
172
+ propertyStr = ""
173
+ copyArray = ["NSString","NSDate"]
174
+ @yp_copyArray = copyArray
175
+
176
+ yp_m_descriptions = Array.new
177
+ yp_m_descriptions.push " NSMutableString *result = [NSMutableString string];"
178
+ yp_m_descriptions.push("\n")
179
+
180
+ yp_propertys = yp_json_property.keys
181
+ @yp_propertys = yp_propertys
182
+
183
+ yp_propertys_type = Array.new
184
+ yp_propertys.each { |key|
185
+ value = yp_json_property[key]
186
+ yp_propertys_type.push value
187
+ if copyArray.include?(value)
188
+ propertyStr = "@property (nonatomic, copy) #{value} *#{key};"
189
+ yp_m_descriptions.push " [result appendFormat:@\"#{key} %@, \\r\", self.#{key}];"
190
+ else
191
+ propertyStr = "@property (nonatomic) #{value} #{key};"
192
+ yp_m_descriptions.push " [result appendFormat:@\"#{key} %@, \\r\", @(self.#{key})];"
193
+ end
194
+ yp_h_contents.push(propertyStr)
195
+ yp_h_contents.push("\n")
196
+
197
+ yp_m_contents.push " copy.#{key} = self.#{key};"
198
+ yp_m_contents.push("\n")
199
+
200
+ yp_m_descriptions.push("\n")
201
+ }
202
+
203
+ @yp_propertys_type = yp_propertys_type
204
+
205
+ yp_h_contents.push "@end"
206
+ yp_h_contents.push("\n")
207
+
208
+ yp_m_contents.push(" return copy;")
209
+ yp_m_contents.push("\n")
210
+ yp_m_contents.push "}"
211
+ yp_m_contents.push("\n")
212
+
213
+ yp_m_contents.push("- (NSString *)description {")
214
+ yp_m_contents.push("\n")
215
+ yp_m_contents += yp_m_descriptions
216
+ yp_m_contents.push(" return result;")
217
+ yp_m_contents.push("\n")
218
+ yp_m_contents.push("}")
219
+ yp_m_contents.push("\n")
220
+
221
+
222
+ yp_m_contents.push "@end"
223
+ yp_m_contents.push("\n")
224
+
225
+ yp_struct = yp_json_struct.keys
226
+ yp_struct.each { |structKey|
227
+ structValue = yp_json_struct[structKey]
228
+ structValue_propertys = structValue
229
+
230
+ yp_h_contents.push("\n")
231
+ yp_h_contents.push "@interface #{structKey} : NSObject <NSCopying>"
232
+ yp_h_contents.push("\n")
233
+
234
+ yp_m_contents.push("\n")
235
+ yp_m_contents.push "@implementation #{structKey}"
236
+ yp_m_contents.push("\n")
237
+ yp_m_contents.push "- (id)copyWithZone:(NSZone *)zone {"
238
+ yp_m_contents.push("\n")
239
+ yp_m_contents.push " #{structKey} *copy = (#{structKey} *)[[[self class] allocWithZone:zone] init];"
240
+ yp_m_contents.push("\n")
241
+
242
+
243
+ yp_m_struct_descriptions = Array.new
244
+ yp_m_struct_descriptions.push " NSMutableString *result = [NSMutableString string];"
245
+ yp_m_struct_descriptions.push("\n")
246
+
247
+ structValue_propertys.each { |structValueKey|
248
+ structValue_propertys_value = yp_json_property[structValueKey]
249
+ if copyArray.include?(structValue_propertys_value)
250
+ propertyStr = "@property (nonatomic, copy) #{structValue_propertys_value} *#{structValueKey};"
251
+ yp_m_struct_descriptions.push " [result appendFormat:@\"#{structValueKey} %@, \\r\", self.#{structValueKey}];"
252
+ else
253
+ propertyStr = "@property (nonatomic) #{structValue_propertys_value} #{structValueKey};"
254
+ yp_m_struct_descriptions.push " [result appendFormat:@\"#{structValueKey} %@, \\r\", @(self.#{structValueKey})];"
255
+ end
256
+ yp_h_contents.push(propertyStr)
257
+ yp_h_contents.push("\n")
258
+
259
+ yp_m_contents.push " copy.#{structValueKey} = self.#{structValueKey};"
260
+ yp_m_contents.push("\n")
261
+
262
+ yp_m_struct_descriptions.push("\n")
263
+ }
264
+ yp_h_contents.push "@end"
265
+ yp_h_contents.push("\n")
266
+
267
+ yp_m_contents.push(" return copy;")
268
+ yp_m_contents.push("\n")
269
+ yp_m_contents.push "}"
270
+ yp_m_contents.push("\n")
271
+
272
+ yp_m_contents.push("- (NSString *)description {")
273
+ yp_m_contents.push("\n")
274
+ yp_m_contents += yp_m_struct_descriptions
275
+ yp_m_contents.push(" return result;")
276
+ yp_m_contents.push("\n")
277
+ yp_m_contents.push("}")
278
+ yp_m_contents.push("\n")
279
+
280
+ yp_m_contents.push "@end"
281
+ yp_m_contents.push("\n")
282
+ }
283
+
284
+
285
+ # 赋值
286
+
287
+ yp_primarykey = yp_json_index["primary-autoincrement"]
288
+ if yp_primarykey
289
+ @yp_bool_autoincrement = 1
290
+ elsif
291
+ yp_primarykey = yp_json_index["primary"]
292
+ @yp_bool_autoincrement = 0
293
+ end
294
+
295
+ yp_bool_haveprimarykey = yp_propertys.include?(yp_primarykey)
296
+ yp_bool_havepropertys = yp_propertys.count != 0
297
+
298
+ yp_uniques = Array.new
299
+ if yp_json_unique != nil
300
+ yp_uniques = yp_json_unique.keys
301
+ end
302
+
303
+ yp_normals = Array.new
304
+ if yp_json_normal != nil
305
+ yp_normals = yp_json_normal.keys
306
+ end
307
+
308
+ @yp_filePath = filePath
309
+ @yp_fileName = fileName
310
+ @yp_fileBasename = fileBasename
311
+ @yp_fileSuffix = fileSuffix
312
+ @yp_fileDirname = fileDirname
313
+ @yp_path = yp_path
314
+ @yp_completePath = yp_completePath
315
+ @yp_json = yp_json
316
+
317
+ @yp_json_mode = yp_json_mode
318
+ @yp_json_module = yp_json_module
319
+ @yp_json_table = yp_json_table
320
+ @yp_json_property = yp_json_property
321
+ @yp_json_index = yp_json_index
322
+ @yp_json_unique = yp_json_unique
323
+ @yp_json_normal = yp_json_normal
324
+ @yp_json_struct = yp_json_struct
325
+
326
+ @yp_dao = yp_dao
327
+ @yp_dao_h = yp_dao_h
328
+ @yp_dao_m = yp_dao_m
329
+ @yp_create_h = yp_create_h
330
+ @yp_create_m = yp_create_m
331
+
332
+ @yp_primarykey = yp_primarykey
333
+ @yp_bool_haveprimarykey = yp_bool_haveprimarykey
334
+ @yp_bool_havepropertys = yp_bool_havepropertys
335
+ @yp_uniques = yp_uniques
336
+ @yp_normals = yp_normals
337
+ @yp_struct = yp_struct
338
+
339
+ # dao
340
+
341
+ yp_h_contents.push("\n")
342
+
343
+ yp_h_contents.push "@interface #{yp_dao} : NSObject"
344
+ yp_h_contents.push("\n")
345
+
346
+ yp_m_contents.push("\n")
347
+
348
+ yp_m_contents.push "@implementation #{yp_dao} {"
349
+ yp_m_contents.push("\n")
350
+
351
+ yp_m_contents.push(" FMDatabaseQueue* _dbQueue;")
352
+ yp_m_contents.push("\n")
353
+ yp_m_contents.push(" NSString* _path;")
354
+ yp_m_contents.push("\n")
355
+ yp_m_contents.push("}")
356
+ yp_m_contents.push("\n")
357
+
358
+ yp_methods = Array.new
359
+ yp_methods_m = Array.new
360
+ yp_methods.push "+ (instancetype)get"
361
+ yp_methods_m.push self.get
362
+ yp_methods.push "- (BOOL)openWithPath:(NSString *)path"
363
+ yp_methods_m.push self.openWithPath
364
+ yp_methods.push "- (FMDatabaseQueue *)getQueue"
365
+ yp_methods_m.push self.getQueue
366
+
367
+ yp_methods.push ""
368
+ yp_methods_m.push self.hideMethod
369
+
370
+ yp_methods.push "- (BOOL)insert#{yp_json_module}:(#{yp_json_module} *)record aRid:(int64_t *)rid"
371
+ yp_methods_m.push self.insertModel
372
+ yp_methods.push "- (BOOL)batchInsert#{yp_json_module}:(NSArray *)records"
373
+ yp_methods_m.push self.batchInstallModel
374
+
375
+ if yp_bool_haveprimarykey
376
+ yp_methods.push "- (BOOL)delete#{yp_json_module}ByPrimaryKey:(int64_t)key"
377
+ yp_methods_m.push self.deleteByPrimaryKey
378
+ end
379
+
380
+ yp_methods.push "- (BOOL)delete#{yp_json_module}BySQLCondition:(NSString *)condition"
381
+ yp_methods_m.push self.deleteBySQLCondition
382
+
383
+ if yp_bool_haveprimarykey
384
+ yp_methods.push "- (BOOL)batchUpdate#{yp_json_module}:(NSArray *)records"
385
+ yp_methods_m.push self.batchUpdateModels
386
+ end
387
+
388
+ if yp_bool_haveprimarykey
389
+ yp_methods.push "- (BOOL)update#{yp_json_module}ByPrimaryKey:(int64_t)key a#{yp_json_module}:(#{yp_json_module} *)a#{yp_json_module}"
390
+ yp_methods_m.push self.updateByPrimaryKey
391
+ end
392
+
393
+ yp_methods.push "- (BOOL)update#{yp_json_module}BySQLCondition:(NSString *)condition a#{yp_json_module}:(#{yp_json_module} *)a#{yp_json_module}"
394
+ yp_methods_m.push self.updateBySQLCondition
395
+
396
+ if yp_bool_haveprimarykey
397
+ yp_methods.push "- (#{yp_json_module} *)select#{yp_json_module}ByPrimaryKey:(int64_t)key"
398
+ yp_methods_m.push self.selectByPrimaryKey
399
+ end
400
+
401
+ yp_methods.push "- (NSArray *)select#{yp_json_module}BySQLCondition:(NSString *)condition"
402
+ yp_methods_m.push self.selectBySQLCondition
403
+
404
+ yp_methods.push "- (int)select#{yp_json_module}Count:(NSString *)condition"
405
+ yp_methods_m.push self.selectCount
406
+
407
+ yp_h_contents.push("\n")
408
+ yp_h_contents.push("// basic")
409
+ yp_h_contents.push("\n")
410
+
411
+ yp_m_contents.push("\n")
412
+ yp_m_contents.push("// basic")
413
+ yp_m_contents.push("\n")
414
+
415
+ yp_methods.each { |method|
416
+ mIndex = yp_methods.index method
417
+ if method.length != 0
418
+ yp_h_contents.push(method + ";")
419
+ yp_h_contents.push("\n")
420
+
421
+ yp_m_contents.push(method + " {")
422
+ yp_m_contents.push("\n")
423
+ yp_m_contents += yp_methods_m[mIndex]
424
+ if yp_m_contents.last != "\n"
425
+ yp_m_contents.push("\n")
426
+ end
427
+ yp_m_contents.push("}")
428
+ yp_m_contents.push("\n")
429
+ else
430
+ yp_m_contents += yp_methods_m[mIndex]
431
+ end
432
+ }
433
+
434
+ if yp_uniques.count > 0
435
+ yp_h_contents.push("\n")
436
+ yp_h_contents.push("// unique")
437
+ yp_h_contents.push("\n")
438
+
439
+ yp_m_contents.push("\n")
440
+ yp_m_contents.push("// unique")
441
+ yp_m_contents.push("\n")
442
+ end
443
+
444
+ yp_methods_unique = Array.new
445
+ yp_methods_unique_m = Array.new
446
+ yp_uniques.each { | uniqueKey |
447
+ unique_delete_line = "- (BOOL)delete#{yp_json_module}By#{uniqueKey}"
448
+ unique_update_line = "- (BOOL)update#{yp_json_module}By#{uniqueKey}"
449
+ unique_select_line = "- (#{yp_json_module} *)select#{yp_json_module}By#{uniqueKey}"
450
+ uniques = yp_json_unique[uniqueKey]
451
+ uniques.each { | unique_value |
452
+ unique_type = yp_json_property[unique_value]
453
+ unique_head_str = unique_value == uniques.first ? ":" : " #{unique_value}:"
454
+ if copyArray.include?(unique_type)
455
+ unique_delete_line += unique_head_str + "(#{unique_type} *)#{unique_value}"
456
+ unique_update_line += unique_head_str + "(#{unique_type} *)#{unique_value}"
457
+ unique_select_line += unique_head_str + "(#{unique_type} *)#{unique_value}"
458
+ else
459
+ unique_delete_line += unique_head_str + "(#{unique_type})#{unique_value}"
460
+ unique_update_line += unique_head_str + "(#{unique_type})#{unique_value}"
461
+ unique_select_line += unique_head_str + "(#{unique_type})#{unique_value}"
462
+ end
463
+ }
464
+ yp_methods_unique.push unique_delete_line
465
+ yp_methods_unique_m.push self.deleteByUnique(uniqueKey)
466
+ yp_methods_unique.push (unique_update_line + " a#{yp_json_module}:(#{yp_json_module} *)a#{yp_json_module}")
467
+ yp_methods_unique_m.push self.updateByUnique(uniqueKey)
468
+ yp_methods_unique.push unique_select_line
469
+ yp_methods_unique_m.push self.selectByUnique(uniqueKey)
470
+ }
471
+
472
+ yp_methods_unique.each { |method|
473
+ yp_h_contents.push(method + ";")
474
+ yp_h_contents.push("\n")
475
+
476
+ yp_m_contents.push(method + " {")
477
+ yp_m_contents.push("\n")
478
+ mIndex = yp_methods_unique.index method
479
+ yp_m_contents += yp_methods_unique_m[mIndex]
480
+ yp_m_contents.push("\n")
481
+ yp_m_contents.push("}")
482
+ yp_m_contents.push("\n")
483
+ }
484
+
485
+ if yp_normals.count > 0
486
+ yp_h_contents.push("\n")
487
+ yp_h_contents.push("// normal")
488
+ yp_h_contents.push("\n")
489
+
490
+ yp_m_contents.push("\n")
491
+ yp_m_contents.push("// normal")
492
+ yp_m_contents.push("\n")
493
+ end
494
+
495
+ yp_methods_normal = Array.new
496
+ yp_methods_normal_m = Array.new
497
+ yp_normals.each { | normalKey |
498
+ normal_delete_line = "- (BOOL)delete#{yp_json_module}By#{normalKey}"
499
+ normal_update_line = "- (BOOL)update#{yp_json_module}By#{normalKey}"
500
+ normal_select_line = "- (NSArray *)select#{yp_json_module}By#{normalKey}"
501
+ normals = yp_json_normal[normalKey]
502
+ normals.each { | normal_value |
503
+ normal_type = yp_json_property[normal_value]
504
+ normal_head_str = normal_value == normals.first ? ":" : " #{normal_value}:"
505
+ if copyArray.include?(normal_type)
506
+ normal_delete_line += normal_head_str + "(#{normal_type} *)#{normal_value}"
507
+ normal_update_line += normal_head_str + "(#{normal_type} *)#{normal_value}"
508
+ normal_select_line += normal_head_str + "(#{normal_type} *)#{normal_value}"
509
+ else
510
+ normal_delete_line += normal_head_str + "(#{normal_type})#{normal_value}"
511
+ normal_update_line += normal_head_str + "(#{normal_type})#{normal_value}"
512
+ normal_select_line += normal_head_str + "(#{normal_type})#{normal_value}"
513
+ end
514
+ }
515
+ yp_methods_normal.push normal_delete_line
516
+ yp_methods_normal_m.push self.deleteByNormal(normalKey)
517
+ yp_methods_normal.push (normal_update_line + " a#{yp_json_module}:(#{yp_json_module} *)a#{yp_json_module}")
518
+ yp_methods_normal_m.push self.updateByNormal(normalKey)
519
+ yp_methods_normal.push normal_select_line
520
+ yp_methods_normal_m.push self.selectByNormal(normalKey)
521
+ }
522
+
523
+ yp_methods_normal.each { |method|
524
+ yp_h_contents.push(method + ";")
525
+ yp_h_contents.push("\n")
526
+
527
+ yp_m_contents.push(method + " {")
528
+ yp_m_contents.push("\n")
529
+ mIndex = yp_methods_normal.index method
530
+ yp_m_contents += yp_methods_normal_m[mIndex]
531
+ yp_m_contents.push("\n")
532
+ yp_m_contents.push("}")
533
+ yp_m_contents.push("\n")
534
+ }
535
+
536
+ if yp_struct.count > 0
537
+ yp_h_contents.push("\n")
538
+ yp_h_contents.push("// struct")
539
+ yp_h_contents.push("\n")
540
+
541
+ yp_m_contents.push("\n")
542
+ yp_m_contents.push("// struct")
543
+ yp_m_contents.push("\n")
544
+ end
545
+
546
+ yp_methods_struct = Array.new
547
+ yp_methods_struct_m = Array.new
548
+ yp_struct.each { | structKey |
549
+ if yp_bool_haveprimarykey
550
+ yp_methods_struct.push "- (BOOL)update#{structKey}ByPrimaryKey:(int64_t)key a#{structKey}:(#{structKey} *)a#{structKey}"
551
+ yp_methods_struct_m.push self.updateStructByPrimaryKey(structKey)
552
+ end
553
+
554
+ yp_uniques.each { | uniqueKey |
555
+ struct_unique_method_str = "- (BOOL)update#{structKey}By#{uniqueKey}"
556
+ uniques = yp_json_unique[uniqueKey]
557
+ uniques.each { | unique_value |
558
+ unique_type = yp_json_property[unique_value]
559
+ unique_head_str = unique_value == uniques.first ? ":" : " #{unique_value}:"
560
+ if copyArray.include?(unique_type)
561
+ struct_unique_method_str += unique_head_str + "(#{unique_type} *)#{unique_value}"
562
+ else
563
+ struct_unique_method_str += unique_head_str + "(#{unique_type})#{unique_value}"
564
+ end
565
+ }
566
+ struct_unique_method_str += " a#{structKey}:(#{structKey} *)a#{structKey}"
567
+ yp_methods_struct.push struct_unique_method_str
568
+ yp_methods_struct_m.push self.updateStructByUniqueKey(structKey, uniqueKey)
569
+ }
570
+
571
+ yp_normals.each { | normalKey |
572
+ struct_normal_method_str = "- (BOOL)update#{structKey}By#{normalKey}"
573
+ normals = yp_json_normal[normalKey]
574
+ normals.each { | normal_value |
575
+ normal_type = yp_json_property[normal_value]
576
+ normal_head_str = normal_value == normals.first ? ":" : " #{normal_value}:"
577
+ if copyArray.include?(normal_type)
578
+ struct_normal_method_str += normal_head_str + "(#{normal_type} *)#{normal_value}"
579
+ else
580
+ struct_normal_method_str += normal_head_str + "(#{normal_type})#{normal_value}"
581
+ end
582
+ }
583
+ struct_normal_method_str += " a#{structKey}:(#{structKey} *)a#{structKey}"
584
+ yp_methods_struct.push struct_normal_method_str
585
+ yp_methods_struct_m.push self.updateStructByNormalKey(structKey, normalKey)
586
+ }
587
+
588
+ yp_methods_struct.push "- (BOOL)update#{structKey}BySQLCondition:(NSString *)condition a#{structKey}:(#{structKey} *)a#{structKey}"
589
+ yp_methods_struct_m.push self.updateStructBySQLCondition(structKey)
590
+ if yp_bool_haveprimarykey
591
+ yp_methods_struct.push "- (#{structKey} *)select#{structKey}ByPrimaryKey:(int64_t)key"
592
+ yp_methods_struct_m.push self.selectStructBySQLCondition(structKey)
593
+ end
594
+
595
+ yp_uniques.each { | uniqueKey |
596
+ struct_unique_method_str = "- (#{structKey} *)select#{structKey}By#{uniqueKey}"
597
+ uniques = yp_json_unique[uniqueKey]
598
+ uniques.each { | unique_value |
599
+ unique_type = yp_json_property[unique_value]
600
+ unique_head_str = unique_value == uniques.first ? ":" : " #{unique_value}:"
601
+ if copyArray.include?(unique_type)
602
+ struct_unique_method_str += unique_head_str + "(#{unique_type} *)#{unique_value}"
603
+ else
604
+ struct_unique_method_str += unique_head_str + "(#{unique_type})#{unique_value}"
605
+ end
606
+ }
607
+ yp_methods_struct.push struct_unique_method_str
608
+ yp_methods_struct_m.push self.selectStructByUnique(structKey, uniqueKey)
609
+ }
610
+
611
+ yp_normals.each { | normalKey |
612
+ struct_normal_method_str = "- (NSArray *)select#{structKey}By#{normalKey}"
613
+ normals = yp_json_normal[normalKey]
614
+ normals.each { | normal_value |
615
+ normal_type = yp_json_property[normal_value]
616
+ normal_head_str = normal_value == normals.first ? ":" : " #{normal_value}:"
617
+ if copyArray.include?(normal_type)
618
+ struct_normal_method_str += normal_head_str + "(#{normal_type} *)#{normal_value}"
619
+ else
620
+ struct_normal_method_str += normal_head_str + "(#{normal_type})#{normal_value}"
621
+ end
622
+ }
623
+ yp_methods_struct.push struct_normal_method_str
624
+ yp_methods_struct_m.push self.selectStructByNormal(structKey, normalKey)
625
+ }
626
+
627
+ yp_methods_struct.push "- (NSArray *)select#{structKey}BySQLCondition:(NSString *)condition"
628
+ yp_methods_struct_m.push self.selectStructSQLCondition(structKey)
629
+ }
630
+
631
+ yp_methods_struct.each { |method|
632
+ yp_h_contents.push(method + ";")
633
+ yp_h_contents.push("\n")
634
+
635
+ yp_m_contents.push(method + " {")
636
+ yp_m_contents.push("\n")
637
+ mIndex = yp_methods_struct.index method
638
+ yp_m_contents += yp_methods_struct_m[mIndex]
639
+ yp_m_contents.push("\n")
640
+ yp_m_contents.push("}")
641
+ yp_m_contents.push("\n")
642
+ }
643
+
644
+ yp_h_contents.push("\n")
645
+ yp_h_contents.push "@end"
646
+ yp_h_contents.push("\n")
647
+
648
+ yp_m_contents.push("\n")
649
+ yp_m_contents.push "@end"
650
+ yp_m_contents.push("\n")
651
+
652
+ yp_create_file_h = File.new(yp_create_h, "w+")
653
+ yp_create_file_m = File.new(yp_create_m, "w+")
654
+
655
+ yp_h_contents.each { |lineText|
656
+ yp_create_file_h.syswrite(lineText);
657
+ }
658
+
659
+ yp_m_contents.each { |lineText|
660
+ yp_create_file_m.syswrite(lineText);
661
+ }
662
+
663
+ end
664
+
665
+ def self.insertModel
666
+ codes = Array.new
667
+ tableName = @yp_json_table
668
+ valueNameTemp = Array.new
669
+ recordsNameTemp = Array.new
670
+ propertyNameTemp = Array.new
671
+ @yp_json_property.each { | property, type |
672
+ sqlType = self.sqlType(type)
673
+ if property == @yp_primarykey && @yp_bool_autoincrement == 1
674
+ next
675
+ end
676
+
677
+ valueNameTemp.push "?"
678
+ if @yp_copyArray.include?(type)
679
+ recordsNameTemp.push "record.#{property}"
680
+ else
681
+ recordsNameTemp.push "@(record.#{property})"
682
+ end
683
+ propertyNameTemp.push property
684
+
685
+ }
686
+ valueName = "(" + valueNameTemp.join(", ") + ")"
687
+ recordsName = recordsNameTemp.join(", ")
688
+ propertyName = "(" + propertyNameTemp.join(", ") + ")"
689
+
690
+ codes.push "if (!_dbQueue) return NO;"
691
+ codes.push "NSString* sql = @\"INSERT INTO #{tableName} #{propertyName} VALUES #{valueName}\";"
692
+ codes.push "__block BOOL errorOccurred = NO;"
693
+ codes.push "[_dbQueue inTransaction:^(FMDatabase *db, BOOL *rollBack) {"
694
+ codes.push " errorOccurred = ![db executeUpdate:sql, #{recordsName}];"
695
+ codes.push " if (errorOccurred) {"
696
+ codes.push " *rollBack = YES;"
697
+ codes.push " } else {"
698
+ codes.push " if (rid != nil) *rid = [db lastInsertRowId];"
699
+ codes.push " }"
700
+ codes.push "}];"
701
+ codes.push "return !errorOccurred;"
702
+
703
+ temp = Array.new
704
+ codes.each { | code |
705
+ temp.push " " + code
706
+ temp.push "\n"
707
+ }
708
+ return temp
709
+ end
710
+
711
+ def self.batchInstallModel
712
+ codes = Array.new
713
+ tableName = @yp_json_table
714
+ propertyName = "(" + @yp_propertys.join(", ") + ")"
715
+ valueNameTemp = Array.new
716
+ recordsNameTemp = Array.new
717
+
718
+ propertyNameTemp = Array.new
719
+ @yp_json_property.each { | property, type |
720
+ sqlType = self.sqlType(type)
721
+ if property == @yp_primarykey && @yp_bool_autoincrement == 1
722
+ next
723
+ end
724
+
725
+ valueNameTemp.push "?"
726
+
727
+ if @yp_copyArray.include?(type)
728
+ recordsNameTemp.push "record.#{property}"
729
+ else
730
+ recordsNameTemp.push "@(record.#{property})"
731
+ end
732
+ propertyNameTemp.push property
733
+
734
+ }
735
+
736
+ valueName = "(" + valueNameTemp.join(", ") + ")"
737
+ recordsName = recordsNameTemp.join(", ")
738
+ propertyName = "(" + propertyNameTemp.join(", ") + ")"
739
+
740
+ codes.push "if (records.count == 0) return YES;"
741
+ codes.push "if (!_dbQueue) return NO;"
742
+ codes.push "NSString* sql = @\"INSERT INTO #{tableName} #{propertyName} VALUES #{valueName}\";"
743
+ codes.push "__block BOOL errorOccurred = NO;"
744
+ codes.push "[_dbQueue inTransaction:^(FMDatabase *db, BOOL *rollBack) {"
745
+ codes.push " for (#{@yp_json_module}* record in records) {"
746
+ codes.push " errorOccurred = ![db executeUpdate:sql, #{recordsName}];"
747
+ codes.push " if (errorOccurred) {"
748
+ codes.push " }"
749
+ codes.push " }"
750
+ codes.push "}];"
751
+ codes.push "return YES;"
752
+
753
+ temp = Array.new
754
+ codes.each { | code |
755
+ temp.push " " + code
756
+ temp.push "\n"
757
+ }
758
+ return temp
759
+ end
760
+
761
+ def self.deleteByPrimaryKey
762
+ codes = Array.new
763
+ tableName = @yp_json_table
764
+ propertyName = "(" + @yp_propertys.join(", ") + ")"
765
+ valueNameTemp = Array.new
766
+ recordsNameTemp = Array.new
767
+ @yp_json_property.each { | property, type |
768
+ valueNameTemp.push "?"
769
+
770
+ if @yp_copyArray.include?(type)
771
+ recordsNameTemp.push "record.#{property}"
772
+ else
773
+ recordsNameTemp.push "@(record.#{property})"
774
+ end
775
+ }
776
+ valueName = "(" + valueNameTemp.join(", ") + ")"
777
+ recordsName = "(" + recordsNameTemp.join(", ") + ")"
778
+
779
+
780
+ codes.push "if (!_dbQueue) return NO;"
781
+ codes.push "NSString* sql = @\"DELETE FROM #{tableName} WHERE #{@yp_primarykey}=?\";"
782
+ codes.push "__block BOOL errorOccurred = NO;"
783
+ codes.push "[_dbQueue inTransaction:^(FMDatabase *db, BOOL *rollBack) {"
784
+ codes.push " errorOccurred = ![db executeUpdate:sql, @(key)];"
785
+ codes.push " if (errorOccurred) {"
786
+ codes.push " *rollBack = YES;"
787
+ codes.push " }"
788
+ codes.push "}];"
789
+ codes.push "return !errorOccurred;"
790
+
791
+ temp = Array.new
792
+ codes.each { | code |
793
+ temp.push " " + code
794
+ temp.push "\n"
795
+ }
796
+ return temp
797
+ end
798
+
799
+ def self.deleteBySQLCondition
800
+ codes = Array.new
801
+ tableName = @yp_json_table
802
+ propertyName = "(" + @yp_propertys.join(", ") + ")"
803
+ valueNameTemp = Array.new
804
+ recordsNameTemp = Array.new
805
+ @yp_json_property.each { | property, type |
806
+ valueNameTemp.push "?"
807
+
808
+ if @yp_copyArray.include?(type)
809
+ recordsNameTemp.push "record.#{property}"
810
+ else
811
+ recordsNameTemp.push "@(record.#{property})"
812
+ end
813
+ }
814
+ valueName = "(" + valueNameTemp.join(", ") + ")"
815
+ recordsName = "(" + recordsNameTemp.join(", ") + ")"
816
+
817
+ codes.push "if (!_dbQueue) return NO;"
818
+ codes.push "NSString *sql = @\"DELETE FROM #{tableName}\";"
819
+ codes.push "if(condition != nil) {"
820
+ codes.push " sql = [NSString stringWithFormat:@\"%@ WHERE %@\", sql, condition];"
821
+ codes.push "}"
822
+ codes.push "__block BOOL errorOccurred = NO;"
823
+ codes.push "[_dbQueue inTransaction:^(FMDatabase *db, BOOL *rollBack) {"
824
+ codes.push " errorOccurred = ![db executeUpdate:sql];"
825
+ codes.push " if (errorOccurred) {"
826
+ codes.push " *rollBack = YES;"
827
+ codes.push " }"
828
+ codes.push "}];"
829
+ codes.push "return !errorOccurred;"
830
+
831
+ temp = Array.new
832
+ codes.each { | code |
833
+ temp.push " " + code
834
+ temp.push "\n"
835
+ }
836
+ return temp
837
+ end
838
+
839
+ def self.batchUpdateModels
840
+ codes = Array.new
841
+ tableName = @yp_json_table
842
+ propertyName = "(" + @yp_propertys.join(", ") + ")"
843
+ valueNameTemp = Array.new
844
+ recordsNameTemp = Array.new
845
+ @yp_json_property.each { | property, type |
846
+ valueNameTemp.push "#{property}=?"
847
+
848
+ if @yp_copyArray.include?(type)
849
+ recordsNameTemp.push "record.#{property}"
850
+ else
851
+ recordsNameTemp.push "@(record.#{property})"
852
+ end
853
+ }
854
+
855
+ recordsNameTemp.push "@(record.#{@yp_primarykey})"
856
+
857
+ valueName = "(" + valueNameTemp.join(", ") + ")"
858
+ recordsName = "(" + recordsNameTemp.join(", ") + ")"
859
+
860
+ codes.push "if (records.count == 0) return YES;"
861
+ codes.push "if (!_dbQueue) return NO;"
862
+ codes.push "NSString* sql = @\"UPDATE #{tableName} SET #{valueNameTemp.join(", ")} WHERE #{@yp_primarykey}=?\";"
863
+ codes.push "__block BOOL errorOccurred = NO;"
864
+ codes.push "[_dbQueue inTransaction:^(FMDatabase *db, BOOL *rollBack) {"
865
+ codes.push " for (#{@yp_json_module} *record in records) {"
866
+ codes.push " errorOccurred = ![db executeUpdate:sql, #{recordsNameTemp.join(", ")}];"
867
+ codes.push " if (errorOccurred) {"
868
+ codes.push " }"
869
+ codes.push " }"
870
+ codes.push "}];"
871
+ codes.push "return YES;"
872
+
873
+ temp = Array.new
874
+ codes.each { | code |
875
+ temp.push " " + code
876
+ temp.push "\n"
877
+ }
878
+ return temp
879
+ end
880
+
881
+ def self.updateByPrimaryKey
882
+ codes = Array.new
883
+ tableName = @yp_json_table
884
+ propertyName = "(" + @yp_propertys.join(", ") + ")"
885
+ valueNameTemp = Array.new
886
+ recordsNameTemp = Array.new
887
+ @yp_json_property.each { | property, type |
888
+ valueNameTemp.push "#{property}=?"
889
+
890
+ if @yp_copyArray.include?(type)
891
+ recordsNameTemp.push "a#{@yp_json_module}.#{property}"
892
+ else
893
+ recordsNameTemp.push "@(a#{@yp_json_module}.#{property})"
894
+ end
895
+ }
896
+
897
+ recordsNameTemp.push "key"
898
+
899
+ valueName = "(" + valueNameTemp.join(", ") + ")"
900
+ recordsName = "(" + recordsNameTemp.join(", ") + ")"
901
+
902
+
903
+ codes.push "if (!_dbQueue) return NO;"
904
+ codes.push "NSString* sql = @\"UPDATE #{tableName} SET #{valueNameTemp.join(", ")} WHERE #{@yp_primarykey}=?\";"
905
+ codes.push "__block BOOL errorOccurred = NO;"
906
+ codes.push "[_dbQueue inTransaction:^(FMDatabase *db, BOOL *rollBack) {"
907
+ codes.push " errorOccurred = ![db executeUpdate:sql, #{recordsNameTemp.join(", ")}];"
908
+ codes.push " if (errorOccurred) {"
909
+ codes.push " *rollBack = YES;"
910
+ codes.push " }"
911
+ codes.push "}];"
912
+ codes.push "return !errorOccurred;"
913
+
914
+ temp = Array.new
915
+ codes.each { | code |
916
+ temp.push " " + code
917
+ temp.push "\n"
918
+ }
919
+ return temp
920
+ end
921
+
922
+ def self.updateBySQLCondition
923
+ codes = Array.new
924
+ tableName = @yp_json_table
925
+ propertyName = "(" + @yp_propertys.join(", ") + ")"
926
+ valueNameTemp = Array.new
927
+ recordsNameTemp = Array.new
928
+ @yp_json_property.each { | property, type |
929
+ valueNameTemp.push "#{property}=?"
930
+
931
+ if @yp_copyArray.include?(type)
932
+ recordsNameTemp.push "a#{@yp_json_module}.#{property}"
933
+ else
934
+ recordsNameTemp.push "@(a#{@yp_json_module}.#{property})"
935
+ end
936
+ }
937
+
938
+ valueName = "(" + valueNameTemp.join(", ") + ")"
939
+ recordsName = "(" + recordsNameTemp.join(", ") + ")"
940
+
941
+ codes.push "if (!_dbQueue) return NO;"
942
+ codes.push "NSString* sql = @\"UPDATE #{@yp_json_table} SET #{valueNameTemp.join(", ")}\";"
943
+ codes.push "if(condition != nil) {"
944
+ codes.push " sql = [NSString stringWithFormat:@\"%@ WHERE %@\", sql, condition];"
945
+ codes.push "}"
946
+ codes.push "__block BOOL errorOccurred = NO;"
947
+ codes.push "[_dbQueue inTransaction:^(FMDatabase *db, BOOL *rollBack) {"
948
+ codes.push " errorOccurred = ![db executeUpdate:sql, #{recordsNameTemp.join(", ")}];"
949
+ codes.push " if (errorOccurred) {"
950
+ codes.push " *rollBack = YES;"
951
+ codes.push " }"
952
+ codes.push "}];"
953
+ codes.push "return !errorOccurred;"
954
+
955
+
956
+ temp = Array.new
957
+ codes.each { | code |
958
+ temp.push " " + code
959
+ temp.push "\n"
960
+ }
961
+ return temp
962
+ end
963
+
964
+ def self.selectByPrimaryKey
965
+ codes = Array.new
966
+ tableName = @yp_json_table
967
+ propertyName = "(" + @yp_propertys.join(", ") + ")"
968
+ valueNameTemp = Array.new
969
+ recordsNameTemp = Array.new
970
+ @yp_json_property.each { | property, type |
971
+ valueNameTemp.push "#{property}=?"
972
+
973
+ if @yp_copyArray.include?(type)
974
+ recordsNameTemp.push "a#{@yp_json_module}.#{property}"
975
+ else
976
+ recordsNameTemp.push "@(a#{@yp_json_module}.#{property})"
977
+ end
978
+ }
979
+
980
+ valueName = "(" + valueNameTemp.join(", ") + ")"
981
+ recordsName = "(" + recordsNameTemp.join(", ") + ")"
982
+
983
+ codes.push "if (!_dbQueue) return nil;"
984
+ codes.push "__block #{@yp_json_module}* record = [[#{@yp_json_module} alloc] init];"
985
+ codes.push "NSString* sql = @\"SELECT #{@yp_propertys.join(", ")} FROM #{@yp_json_table} WHERE #{@yp_primarykey}=?\";"
986
+ codes.push "__block BOOL found = NO;"
987
+ codes.push "[_dbQueue inDatabase:^(FMDatabase *db) {"
988
+ codes.push " FMResultSet *resultSet = [db executeQuery:sql, @(key)];"
989
+ codes.push " while (resultSet.next) {"
990
+
991
+ @yp_json_property.each { | property , type |
992
+ method = self.codeStringForColumnWithType(type)
993
+ codes.push " record.#{property} = [resultSet #{method}:@\"#{property}\"];"
994
+ }
995
+
996
+ codes.push " found = YES;"
997
+ codes.push " break;"
998
+ codes.push " }"
999
+ codes.push " [resultSet close];"
1000
+ codes.push "}];"
1001
+ codes.push "if(!found) return nil;"
1002
+ codes.push "return record;"
1003
+
1004
+ temp = Array.new
1005
+ codes.each { | code |
1006
+ temp.push " " + code
1007
+ temp.push "\n"
1008
+ }
1009
+ return temp
1010
+ end
1011
+
1012
+ def self.selectBySQLCondition
1013
+ codes = Array.new
1014
+ tableName = @yp_json_table
1015
+ propertyName = "(" + @yp_propertys.join(", ") + ")"
1016
+ valueNameTemp = Array.new
1017
+ recordsNameTemp = Array.new
1018
+ @yp_json_property.each { | property, type |
1019
+ valueNameTemp.push "#{property}=?"
1020
+
1021
+ if @yp_copyArray.include?(type)
1022
+ recordsNameTemp.push "a#{@yp_json_module}.#{property}"
1023
+ else
1024
+ recordsNameTemp.push "@(a#{@yp_json_module}.#{property})"
1025
+ end
1026
+ }
1027
+
1028
+ valueName = "(" + valueNameTemp.join(", ") + ")"
1029
+ recordsName = "(" + recordsNameTemp.join(", ") + ")"
1030
+
1031
+
1032
+
1033
+ codes.push "if (!_dbQueue) return nil;"
1034
+ codes.push "__block NSMutableArray* records = [[NSMutableArray alloc] init];"
1035
+ codes.push "NSString* sql = @\"SELECT #{@yp_propertys.join(", ")} FROM #{@yp_json_table}\";"
1036
+ codes.push "if(condition != nil) {"
1037
+ codes.push " sql = [NSString stringWithFormat:@\"%@ WHERE %@\", sql, condition];"
1038
+ codes.push "}"
1039
+ codes.push "__block BOOL found = NO;"
1040
+ codes.push "[_dbQueue inDatabase:^(FMDatabase *db) {"
1041
+ codes.push " FMResultSet *resultSet = [db executeQuery:sql];"
1042
+ codes.push " while (resultSet.next) {"
1043
+ codes.push " #{@yp_json_module}* record = [[#{@yp_json_module} alloc] init];"
1044
+
1045
+ @yp_json_property.each { | property , type |
1046
+ method = self.codeStringForColumnWithType(type)
1047
+ codes.push " record.#{property} = [resultSet #{method}:@\"#{property}\"];"
1048
+ }
1049
+
1050
+ codes.push " found = YES;"
1051
+ codes.push " [records addObject:record];"
1052
+ codes.push " }"
1053
+ codes.push " [resultSet close];"
1054
+ codes.push "}];"
1055
+ codes.push "if(!found) return nil;"
1056
+ codes.push "return records;"
1057
+
1058
+ temp = Array.new
1059
+ codes.each { | code |
1060
+ temp.push " " + code
1061
+ temp.push "\n"
1062
+ }
1063
+ return temp
1064
+ end
1065
+
1066
+ def self.selectCount
1067
+ codes = Array.new
1068
+ tableName = @yp_json_table
1069
+ propertyName = "(" + @yp_propertys.join(", ") + ")"
1070
+ valueNameTemp = Array.new
1071
+ recordsNameTemp = Array.new
1072
+ @yp_json_property.each { | property, type |
1073
+ valueNameTemp.push "#{property}=?"
1074
+
1075
+ if @yp_copyArray.include?(type)
1076
+ recordsNameTemp.push "a#{@yp_json_module}.#{property}"
1077
+ else
1078
+ recordsNameTemp.push "@(a#{@yp_json_module}.#{property})"
1079
+ end
1080
+ }
1081
+
1082
+ valueName = "(" + valueNameTemp.join(", ") + ")"
1083
+ recordsName = "(" + recordsNameTemp.join(", ") + ")"
1084
+
1085
+ codes.push "if (!_dbQueue) return -1;"
1086
+ codes.push "__block int count = 0;"
1087
+ codes.push "NSString* sql = @\"SELECT COUNT(*) FROM #{@yp_json_table}\";"
1088
+ codes.push "if(condition != nil) {"
1089
+ codes.push " sql = [NSString stringWithFormat:@\"%@ WHERE %@\", sql, condition];"
1090
+ codes.push "}"
1091
+ codes.push "[_dbQueue inDatabase:^(FMDatabase *db) {"
1092
+ codes.push " FMResultSet *resultSet = [db executeQuery:sql];"
1093
+ codes.push " if (resultSet.next) {"
1094
+ codes.push " count = [resultSet intForColumnIndex:0];"
1095
+ codes.push " }"
1096
+ codes.push " [resultSet close];"
1097
+ codes.push "}];"
1098
+ codes.push "return count;"
1099
+
1100
+ temp = Array.new
1101
+ codes.each { | code |
1102
+ temp.push " " + code
1103
+ temp.push "\n"
1104
+ }
1105
+ return temp
1106
+ end
1107
+
1108
+ def self.deleteByUnique (uniqueName)
1109
+ codes = Array.new
1110
+ tableName = @yp_json_table
1111
+ propertyName = "(" + @yp_propertys.join(", ") + ")"
1112
+ valueNameTemp = Array.new
1113
+ recordsNameTemp = Array.new
1114
+ @yp_json_property.each { | property, type |
1115
+ valueNameTemp.push "#{property}=?"
1116
+
1117
+ if @yp_copyArray.include?(type)
1118
+ recordsNameTemp.push "a#{@yp_json_module}.#{property}"
1119
+ else
1120
+ recordsNameTemp.push "@(a#{@yp_json_module}.#{property})"
1121
+ end
1122
+ }
1123
+
1124
+ valueName = "(" + valueNameTemp.join(", ") + ")"
1125
+ recordsName = "(" + recordsNameTemp.join(", ") + ")"
1126
+
1127
+ condition_array = Array.new
1128
+ unique_array = Array.new
1129
+ @yp_json_unique[uniqueName].each { | property |
1130
+ condition_array.push "#{property}=?"
1131
+ type = @yp_json_property[property]
1132
+ if @yp_copyArray.include?(type)
1133
+ unique_array.push "#{property}"
1134
+ else
1135
+ unique_array.push "@(#{property})"
1136
+ end
1137
+ }
1138
+
1139
+ conditionString = condition_array.join(" and ")
1140
+ uniqueString = unique_array.join(", ")
1141
+
1142
+ codes.push "if (!_dbQueue) return NO;"
1143
+ codes.push "NSString* sql = @\"DELETE FROM #{@yp_json_table} WHERE #{conditionString}\";"
1144
+ codes.push "__block BOOL errorOccurred = NO;"
1145
+ codes.push "[_dbQueue inTransaction:^(FMDatabase *db, BOOL *rollBack) {"
1146
+ codes.push " errorOccurred = ![db executeUpdate:sql, #{uniqueString}];"
1147
+ codes.push " if (errorOccurred) {"
1148
+ codes.push " *rollBack = YES;"
1149
+ codes.push " }"
1150
+ codes.push "}];"
1151
+ codes.push "return !errorOccurred;"
1152
+
1153
+ temp = Array.new
1154
+ codes.each { | code |
1155
+ temp.push " " + code
1156
+ temp.push "\n"
1157
+ }
1158
+ return temp
1159
+ end
1160
+
1161
+ def self.updateByUnique (uniqueName)
1162
+ codes = Array.new
1163
+ tableName = @yp_json_table
1164
+ propertyName = "(" + @yp_propertys.join(", ") + ")"
1165
+ valueNameTemp = Array.new
1166
+ recordsNameTemp = Array.new
1167
+ @yp_json_property.each { | property, type |
1168
+ valueNameTemp.push "#{property}=?"
1169
+
1170
+ if @yp_copyArray.include?(type)
1171
+ recordsNameTemp.push "a#{@yp_json_module}.#{property}"
1172
+ else
1173
+ recordsNameTemp.push "@(a#{@yp_json_module}.#{property})"
1174
+ end
1175
+ }
1176
+
1177
+ valueName = valueNameTemp.join(", ")
1178
+ recordsName = "(" + recordsNameTemp.join(", ") + ")"
1179
+
1180
+ condition_array = Array.new
1181
+ unique_array = Array.new
1182
+ @yp_json_unique[uniqueName].each { | property |
1183
+ condition_array.push "#{property}=?"
1184
+ type = @yp_json_property[property]
1185
+ if @yp_copyArray.include?(type)
1186
+ unique_array.push "#{property}"
1187
+ else
1188
+ unique_array.push "@(#{property})"
1189
+ end
1190
+ }
1191
+
1192
+ conditionString = condition_array.join(" and ")
1193
+
1194
+ recordsNameTemp += unique_array
1195
+ uniqueString = recordsNameTemp.join(", ")
1196
+
1197
+ codes.push "if (!_dbQueue) return NO;"
1198
+ codes.push "NSString* sql = @\"UPDATE #{@yp_json_table} SET #{valueName} WHERE #{conditionString}\";"
1199
+ codes.push "__block BOOL errorOccurred = NO;"
1200
+ codes.push "[_dbQueue inTransaction:^(FMDatabase *db, BOOL *rollBack) {"
1201
+ codes.push " errorOccurred = ![db executeUpdate:sql, #{uniqueString}];"
1202
+ codes.push " if (errorOccurred) {"
1203
+ codes.push " *rollBack = YES;"
1204
+ codes.push " }"
1205
+ codes.push "}];"
1206
+ codes.push "return !errorOccurred;"
1207
+
1208
+ temp = Array.new
1209
+ codes.each { | code |
1210
+ temp.push " " + code
1211
+ temp.push "\n"
1212
+ }
1213
+ return temp
1214
+ end
1215
+
1216
+ def self.selectByUnique (uniqueName)
1217
+ codes = Array.new
1218
+ tableName = @yp_json_table
1219
+ propertyName = "(" + @yp_propertys.join(", ") + ")"
1220
+ valueNameTemp = Array.new
1221
+ recordsNameTemp = Array.new
1222
+ @yp_json_property.each { | property, type |
1223
+ valueNameTemp.push "#{property}"
1224
+
1225
+ if @yp_copyArray.include?(type)
1226
+ recordsNameTemp.push "#{property}"
1227
+ else
1228
+ recordsNameTemp.push "@(#{property})"
1229
+ end
1230
+ }
1231
+
1232
+ valueName = valueNameTemp.join(", ")
1233
+ recordsName = "(" + recordsNameTemp.join(", ") + ")"
1234
+
1235
+ condition_array = Array.new
1236
+ unique_array = Array.new
1237
+ @yp_json_unique[uniqueName].each { | property |
1238
+ condition_array.push "#{property}=?"
1239
+ type = @yp_json_property[property]
1240
+ if @yp_copyArray.include?(type)
1241
+ unique_array.push "#{property}"
1242
+ else
1243
+ unique_array.push "@(#{property})"
1244
+ end
1245
+ }
1246
+
1247
+ conditionString = condition_array.join(" and ")
1248
+
1249
+ uniqueString = unique_array.join(", ")
1250
+
1251
+ codes.push "if (!_dbQueue) return nil;"
1252
+ codes.push "__block #{@yp_json_module}* record = [[#{@yp_json_module} alloc] init];"
1253
+ codes.push "NSString* sql = @\"SELECT #{valueName} FROM #{@yp_json_table} WHERE #{conditionString}\";"
1254
+ codes.push "__block BOOL found = NO;"
1255
+ codes.push "[_dbQueue inDatabase:^(FMDatabase *db) {"
1256
+ codes.push " FMResultSet *resultSet = [db executeQuery:sql, #{uniqueString}];"
1257
+ codes.push " while (resultSet.next) {"
1258
+ @yp_json_property.each { | property , type |
1259
+ method = self.codeStringForColumnWithType(type)
1260
+ codes.push " record.#{property} = [resultSet #{method}:@\"#{property}\"];"
1261
+ }
1262
+ codes.push " found = YES;"
1263
+ codes.push " break;"
1264
+ codes.push " }"
1265
+ codes.push " [resultSet close];"
1266
+ codes.push "}];"
1267
+ codes.push "if(!found) return nil;"
1268
+ codes.push "return record;"
1269
+
1270
+ temp = Array.new
1271
+ codes.each { | code |
1272
+ temp.push " " + code
1273
+ temp.push "\n"
1274
+ }
1275
+ return temp
1276
+ end
1277
+
1278
+ def self.deleteByNormal(normalKey)
1279
+ codes = Array.new
1280
+ tableName = @yp_json_table
1281
+ propertyName = "(" + @yp_propertys.join(", ") + ")"
1282
+ valueNameTemp = Array.new
1283
+ recordsNameTemp = Array.new
1284
+ @yp_json_property.each { | property, type |
1285
+ valueNameTemp.push "#{property}=?"
1286
+
1287
+ if @yp_copyArray.include?(type)
1288
+ recordsNameTemp.push "a#{@yp_json_module}.#{property}"
1289
+ else
1290
+ recordsNameTemp.push "@(a#{@yp_json_module}.#{property})"
1291
+ end
1292
+ }
1293
+
1294
+ valueName = "(" + valueNameTemp.join(", ") + ")"
1295
+ recordsName = "(" + recordsNameTemp.join(", ") + ")"
1296
+
1297
+ condition_array = Array.new
1298
+ unique_array = Array.new
1299
+ @yp_json_normal[normalKey].each { | property |
1300
+ condition_array.push "#{property}=?"
1301
+ type = @yp_json_property[property]
1302
+ if @yp_copyArray.include?(type)
1303
+ unique_array.push "#{property}"
1304
+ else
1305
+ unique_array.push "@(#{property})"
1306
+ end
1307
+ }
1308
+
1309
+ conditionString = condition_array.join(" and ")
1310
+ uniqueString = unique_array.join(", ")
1311
+
1312
+ codes.push "if (!_dbQueue) return NO;"
1313
+ codes.push "NSString* sql = @\"DELETE FROM #{@yp_json_table} WHERE #{conditionString}\";"
1314
+ codes.push "__block BOOL errorOccurred = NO;"
1315
+ codes.push "[_dbQueue inTransaction:^(FMDatabase *db, BOOL *rollBack) {"
1316
+ codes.push " errorOccurred = ![db executeUpdate:sql, #{uniqueString}];"
1317
+ codes.push " if (errorOccurred) {"
1318
+ codes.push " *rollBack = YES;"
1319
+ codes.push " }"
1320
+ codes.push "}];"
1321
+ codes.push "return !errorOccurred;"
1322
+
1323
+ temp = Array.new
1324
+ codes.each { | code |
1325
+ temp.push " " + code
1326
+ temp.push "\n"
1327
+ }
1328
+ return temp
1329
+ end
1330
+
1331
+ def self.updateByNormal(normalKey)
1332
+ codes = Array.new
1333
+ tableName = @yp_json_table
1334
+ propertyName = "(" + @yp_propertys.join(", ") + ")"
1335
+ valueNameTemp = Array.new
1336
+ recordsNameTemp = Array.new
1337
+ @yp_json_property.each { | property, type |
1338
+ valueNameTemp.push "#{property}=?"
1339
+
1340
+ if @yp_copyArray.include?(type)
1341
+ recordsNameTemp.push "a#{@yp_json_module}.#{property}"
1342
+ else
1343
+ recordsNameTemp.push "@(a#{@yp_json_module}.#{property})"
1344
+ end
1345
+ }
1346
+
1347
+ valueName = valueNameTemp.join(", ")
1348
+ recordsName = "(" + recordsNameTemp.join(", ") + ")"
1349
+
1350
+ condition_array = Array.new
1351
+ unique_array = Array.new
1352
+ @yp_json_normal[normalKey].each { | property |
1353
+ condition_array.push "#{property}=?"
1354
+ type = @yp_json_property[property]
1355
+ if @yp_copyArray.include?(type)
1356
+ unique_array.push "#{property}"
1357
+ else
1358
+ unique_array.push "@(#{property})"
1359
+ end
1360
+ }
1361
+
1362
+ conditionString = condition_array.join(" and ")
1363
+
1364
+ recordsNameTemp += unique_array
1365
+ uniqueString = recordsNameTemp.join(", ")
1366
+
1367
+ codes.push "if (!_dbQueue) return NO;"
1368
+ codes.push "NSString* sql = @\"UPDATE #{@yp_json_table} SET #{valueName} WHERE #{conditionString}\";"
1369
+ codes.push "__block BOOL errorOccurred = NO;"
1370
+ codes.push "[_dbQueue inTransaction:^(FMDatabase *db, BOOL *rollBack) {"
1371
+ codes.push " errorOccurred = ![db executeUpdate:sql, #{uniqueString}];"
1372
+ codes.push " if (errorOccurred) {"
1373
+ codes.push " *rollBack = YES;"
1374
+ codes.push " }"
1375
+ codes.push "}];"
1376
+ codes.push "return !errorOccurred;"
1377
+
1378
+ temp = Array.new
1379
+ codes.each { | code |
1380
+ temp.push " " + code
1381
+ temp.push "\n"
1382
+ }
1383
+ return temp
1384
+ end
1385
+
1386
+ def self.selectByNormal(normalKey)
1387
+ codes = Array.new
1388
+ tableName = @yp_json_table
1389
+ propertyName = "(" + @yp_propertys.join(", ") + ")"
1390
+ valueNameTemp = Array.new
1391
+ recordsNameTemp = Array.new
1392
+ @yp_json_property.each { | property, type |
1393
+ valueNameTemp.push "#{property}"
1394
+
1395
+ if @yp_copyArray.include?(type)
1396
+ recordsNameTemp.push "#{property}"
1397
+ else
1398
+ recordsNameTemp.push "@(#{property})"
1399
+ end
1400
+ }
1401
+
1402
+ valueName = valueNameTemp.join(", ")
1403
+ recordsName = "(" + recordsNameTemp.join(", ") + ")"
1404
+
1405
+ condition_array = Array.new
1406
+ unique_array = Array.new
1407
+ @yp_json_normal[normalKey].each { | property |
1408
+ condition_array.push "#{property}=?"
1409
+ type = @yp_json_property[property]
1410
+ if @yp_copyArray.include?(type)
1411
+ unique_array.push "#{property}"
1412
+ else
1413
+ unique_array.push "@(#{property})"
1414
+ end
1415
+ }
1416
+
1417
+ conditionString = condition_array.join(" and ")
1418
+
1419
+ uniqueString = unique_array.join(", ")
1420
+
1421
+ codes.push "if (!_dbQueue) return nil;"
1422
+ codes.push "__block NSMutableArray* records = [[NSMutableArray alloc] init];"
1423
+ codes.push "NSString* sql = @\"SELECT #{valueName} FROM #{@yp_json_table} WHERE #{conditionString}\";"
1424
+ codes.push "__block BOOL found = NO;"
1425
+ codes.push "[_dbQueue inDatabase:^(FMDatabase *db) {"
1426
+ codes.push " FMResultSet *resultSet = [db executeQuery:sql, #{uniqueString}];"
1427
+ codes.push " while (resultSet.next) {"
1428
+ codes.push " __block #{@yp_json_module}* record = [[#{@yp_json_module} alloc] init];"
1429
+ @yp_json_property.each { | property , type |
1430
+ method = self.codeStringForColumnWithType(type)
1431
+ codes.push " record.#{property} = [resultSet #{method}:@\"#{property}\"];"
1432
+ }
1433
+ codes.push " found = YES;"
1434
+ codes.push " [records addObject:record];"
1435
+ codes.push " }"
1436
+ codes.push " [resultSet close];"
1437
+ codes.push "}];"
1438
+ codes.push "if(!found) return nil;"
1439
+ codes.push "return records;"
1440
+
1441
+ temp = Array.new
1442
+ codes.each { | code |
1443
+ temp.push " " + code
1444
+ temp.push "\n"
1445
+ }
1446
+ return temp
1447
+ end
1448
+
1449
+ def self.updateStructByPrimaryKey(structKey)
1450
+ codes = Array.new
1451
+ tableName = @yp_json_table
1452
+ propertyName = "(" + @yp_propertys.join(", ") + ")"
1453
+ valueNameTemp = Array.new
1454
+ recordsNameTemp = Array.new
1455
+ @yp_json_property.each { | property, type |
1456
+ valueNameTemp.push "#{property}"
1457
+
1458
+ if @yp_copyArray.include?(type)
1459
+ recordsNameTemp.push "#{property}"
1460
+ else
1461
+ recordsNameTemp.push "@(#{property})"
1462
+ end
1463
+ }
1464
+
1465
+ valueName = valueNameTemp.join(", ")
1466
+ recordsName = "(" + recordsNameTemp.join(", ") + ")"
1467
+
1468
+ condition_array = Array.new
1469
+ struct_array = Array.new
1470
+ @yp_json_struct[structKey].each { | property |
1471
+ condition_array.push "#{property}=?"
1472
+ type = @yp_json_property[property]
1473
+ if @yp_copyArray.include?(type)
1474
+ struct_array.push "a#{structKey}.#{property}"
1475
+ else
1476
+ struct_array.push "@(a#{structKey}.#{property})"
1477
+ end
1478
+ }
1479
+
1480
+ conditionString = condition_array.join(" and ")
1481
+
1482
+ structString = struct_array.join(", ")
1483
+
1484
+ codes.push "if (!_dbQueue) return NO;"
1485
+ codes.push "NSString* sql = @\"UPDATE #{@yp_json_table} SET #{conditionString} WHERE #{@yp_primarykey}=?\";"
1486
+ codes.push "__block BOOL errorOccurred = NO;"
1487
+ codes.push "[_dbQueue inTransaction:^(FMDatabase *db, BOOL *rollBack) {"
1488
+ codes.push " errorOccurred = ![db executeUpdate:sql, #{structString}, @(key)];"
1489
+ codes.push " if (errorOccurred) {"
1490
+ codes.push " *rollBack = YES;"
1491
+ codes.push " }"
1492
+ codes.push "}];"
1493
+ codes.push "return !errorOccurred;"
1494
+
1495
+ temp = Array.new
1496
+ codes.each { | code |
1497
+ temp.push " " + code
1498
+ temp.push "\n"
1499
+ }
1500
+ return temp
1501
+ end
1502
+
1503
+ def self.updateStructByUniqueKey(structKey, uniqueKey)
1504
+ codes = Array.new
1505
+ tableName = @yp_json_table
1506
+ propertyName = "(" + @yp_propertys.join(", ") + ")"
1507
+ valueNameTemp = Array.new
1508
+ recordsNameTemp = Array.new
1509
+ @yp_json_property.each { | property, type |
1510
+ valueNameTemp.push "#{property}"
1511
+
1512
+ if @yp_copyArray.include?(type)
1513
+ recordsNameTemp.push "#{property}"
1514
+ else
1515
+ recordsNameTemp.push "@(#{property})"
1516
+ end
1517
+ }
1518
+
1519
+ valueName = valueNameTemp.join(", ")
1520
+ recordsName = "(" + recordsNameTemp.join(", ") + ")"
1521
+
1522
+ condition_array = Array.new
1523
+ struct_array = Array.new
1524
+ @yp_json_struct[structKey].each { | property |
1525
+ condition_array.push "#{property}=?"
1526
+ type = @yp_json_property[property]
1527
+ if @yp_copyArray.include?(type)
1528
+ struct_array.push "a#{structKey}.#{property}"
1529
+ else
1530
+ struct_array.push "@(a#{structKey}.#{property})"
1531
+ end
1532
+ }
1533
+
1534
+ conditionString = condition_array.join(" and ")
1535
+
1536
+ structString = struct_array.join(", ")
1537
+
1538
+ unique_condition_array = Array.new
1539
+ unique_array = Array.new
1540
+ @yp_json_unique[uniqueKey].each { | property |
1541
+ unique_condition_array.push "#{property}=?"
1542
+ type = @yp_json_property[property]
1543
+ if @yp_copyArray.include?(type)
1544
+ unique_array.push "#{property}"
1545
+ else
1546
+ unique_array.push "@(#{property})"
1547
+ end
1548
+ }
1549
+
1550
+ unique_conditionString = unique_condition_array.join(" and ")
1551
+
1552
+ uniqueString = unique_array.join(", ")
1553
+
1554
+ codes.push "if (!_dbQueue) return NO;"
1555
+ codes.push "NSString* sql = @\"UPDATE #{@yp_json_table} SET #{conditionString} WHERE #{unique_conditionString}\";"
1556
+ codes.push "__block BOOL errorOccurred = NO;"
1557
+ codes.push "[_dbQueue inTransaction:^(FMDatabase *db, BOOL *rollBack) {"
1558
+ codes.push " errorOccurred = ![db executeUpdate:sql, #{structString}, #{uniqueString}];"
1559
+ codes.push " if (errorOccurred) {"
1560
+ codes.push " *rollBack = YES;"
1561
+ codes.push " }"
1562
+ codes.push "}];"
1563
+ codes.push "return !errorOccurred;"
1564
+
1565
+ temp = Array.new
1566
+ codes.each { | code |
1567
+ temp.push " " + code
1568
+ temp.push "\n"
1569
+ }
1570
+ return temp
1571
+ end
1572
+
1573
+ def self.updateStructByNormalKey(structKey, normalKey)
1574
+ codes = Array.new
1575
+ tableName = @yp_json_table
1576
+ propertyName = "(" + @yp_propertys.join(", ") + ")"
1577
+ valueNameTemp = Array.new
1578
+ recordsNameTemp = Array.new
1579
+ @yp_json_property.each { | property, type |
1580
+ valueNameTemp.push "#{property}"
1581
+
1582
+ if @yp_copyArray.include?(type)
1583
+ recordsNameTemp.push "#{property}"
1584
+ else
1585
+ recordsNameTemp.push "@(#{property})"
1586
+ end
1587
+ }
1588
+
1589
+ valueName = valueNameTemp.join(", ")
1590
+ recordsName = "(" + recordsNameTemp.join(", ") + ")"
1591
+
1592
+ condition_array = Array.new
1593
+ struct_array = Array.new
1594
+ @yp_json_struct[structKey].each { | property |
1595
+ condition_array.push "#{property}=?"
1596
+ type = @yp_json_property[property]
1597
+ if @yp_copyArray.include?(type)
1598
+ struct_array.push "a#{structKey}.#{property}"
1599
+ else
1600
+ struct_array.push "@(a#{structKey}.#{property})"
1601
+ end
1602
+ }
1603
+
1604
+ conditionString = condition_array.join(" and ")
1605
+
1606
+ structString = struct_array.join(", ")
1607
+
1608
+ normal_condition_array = Array.new
1609
+ normal_array = Array.new
1610
+ @yp_json_normal[normalKey].each { | property |
1611
+ normal_condition_array.push "#{property}=?"
1612
+ type = @yp_json_property[property]
1613
+ if @yp_copyArray.include?(type)
1614
+ normal_array.push "#{property}"
1615
+ else
1616
+ normal_array.push "@(#{property})"
1617
+ end
1618
+ }
1619
+
1620
+ normal_conditionString = normal_condition_array.join(" and ")
1621
+
1622
+ normalString = normal_array.join(", ")
1623
+
1624
+ codes.push "if (!_dbQueue) return NO;"
1625
+ codes.push "NSString* sql = @\"UPDATE #{@yp_json_table} SET #{conditionString} WHERE #{normal_conditionString}\";"
1626
+ codes.push "__block BOOL errorOccurred = NO;"
1627
+ codes.push "[_dbQueue inTransaction:^(FMDatabase *db, BOOL *rollBack) {"
1628
+ codes.push " errorOccurred = ![db executeUpdate:sql, #{structString}, #{normalString}];"
1629
+ codes.push " if (errorOccurred) {"
1630
+ codes.push " *rollBack = YES;"
1631
+ codes.push " }"
1632
+ codes.push "}];"
1633
+ codes.push "return !errorOccurred;"
1634
+
1635
+ temp = Array.new
1636
+ codes.each { | code |
1637
+ temp.push " " + code
1638
+ temp.push "\n"
1639
+ }
1640
+ return temp
1641
+ end
1642
+
1643
+ def self.updateStructBySQLCondition(structKey)
1644
+ codes = Array.new
1645
+ tableName = @yp_json_table
1646
+ propertyName = "(" + @yp_propertys.join(", ") + ")"
1647
+ valueNameTemp = Array.new
1648
+ recordsNameTemp = Array.new
1649
+ @yp_json_property.each { | property, type |
1650
+ valueNameTemp.push "#{property}"
1651
+
1652
+ if @yp_copyArray.include?(type)
1653
+ recordsNameTemp.push "#{property}"
1654
+ else
1655
+ recordsNameTemp.push "@(#{property})"
1656
+ end
1657
+ }
1658
+
1659
+ valueName = valueNameTemp.join(", ")
1660
+ recordsName = "(" + recordsNameTemp.join(", ") + ")"
1661
+
1662
+ condition_array = Array.new
1663
+ struct_array = Array.new
1664
+ @yp_json_struct[structKey].each { | property |
1665
+ condition_array.push "#{property}=?"
1666
+ type = @yp_json_property[property]
1667
+ if @yp_copyArray.include?(type)
1668
+ struct_array.push "a#{structKey}.#{property}"
1669
+ else
1670
+ struct_array.push "@(a#{structKey}.#{property})"
1671
+ end
1672
+ }
1673
+
1674
+ conditionString = condition_array.join(" and ")
1675
+
1676
+ structString = struct_array.join(", ")
1677
+
1678
+ codes.push "if (!_dbQueue) return NO;"
1679
+ codes.push "NSString* sql = @\"UPDATE #{@yp_json_table} SET #{conditionString}\";"
1680
+ codes.push "if(condition != nil) {"
1681
+ codes.push " sql = [NSString stringWithFormat:@\"%@ WHERE %@\", sql, condition];"
1682
+ codes.push "}"
1683
+ codes.push "__block BOOL errorOccurred = NO;"
1684
+ codes.push "[_dbQueue inTransaction:^(FMDatabase *db, BOOL *rollBack) {"
1685
+ codes.push " errorOccurred = ![db executeUpdate:sql, #{structString}];"
1686
+ codes.push " if (errorOccurred) {"
1687
+ codes.push " *rollBack = YES;"
1688
+ codes.push " }"
1689
+ codes.push "}];"
1690
+ codes.push "return !errorOccurred;"
1691
+
1692
+ temp = Array.new
1693
+ codes.each { | code |
1694
+ temp.push " " + code
1695
+ temp.push "\n"
1696
+ }
1697
+ return temp
1698
+ end
1699
+
1700
+ def self.selectStructBySQLCondition(structKey)
1701
+ codes = Array.new
1702
+ tableName = @yp_json_table
1703
+ propertyName = "(" + @yp_propertys.join(", ") + ")"
1704
+ valueNameTemp = Array.new
1705
+ recordsNameTemp = Array.new
1706
+ @yp_json_property.each { | property, type |
1707
+ valueNameTemp.push "#{property}"
1708
+
1709
+ if @yp_copyArray.include?(type)
1710
+ recordsNameTemp.push "#{property}"
1711
+ else
1712
+ recordsNameTemp.push "@(#{property})"
1713
+ end
1714
+ }
1715
+
1716
+ valueName = valueNameTemp.join(", ")
1717
+ recordsName = "(" + recordsNameTemp.join(", ") + ")"
1718
+
1719
+ condition_array = Array.new
1720
+ struct_array = Array.new
1721
+ struct_property_array = Array.new
1722
+ @yp_json_struct[structKey].each { | property |
1723
+ struct_property_array.push property
1724
+ condition_array.push "#{property}=?"
1725
+ type = @yp_json_property[property]
1726
+ if @yp_copyArray.include?(type)
1727
+ struct_array.push "a#{structKey}.#{property}"
1728
+ else
1729
+ struct_array.push "@(a#{structKey}.#{property})"
1730
+ end
1731
+ }
1732
+
1733
+ struct_property_arrayString = struct_property_array.join(", ")
1734
+
1735
+ conditionString = condition_array.join(" and ")
1736
+
1737
+ structString = struct_array.join(", ")
1738
+
1739
+ codes.push "if (!_dbQueue) return nil;"
1740
+ codes.push "__block #{structKey}* record = [[#{structKey} alloc] init];"
1741
+ codes.push "NSString* sql = @\"SELECT #{struct_property_arrayString} FROM #{@yp_json_table} WHERE #{@yp_primarykey}=?\";"
1742
+ codes.push "__block BOOL found = NO;"
1743
+ codes.push "[_dbQueue inDatabase:^(FMDatabase *db) {"
1744
+ codes.push " FMResultSet *resultSet = [db executeQuery:sql, @(key)];"
1745
+ codes.push " while (resultSet.next) {"
1746
+
1747
+ struct_property_array.each { | property |
1748
+ type = @yp_json_property[property]
1749
+ method = self.codeStringForColumnWithType(type)
1750
+ codes.push " record.#{property} = [resultSet #{method}:@\"#{property}\"];"
1751
+ }
1752
+
1753
+ codes.push " found = YES;"
1754
+ codes.push " break;"
1755
+ codes.push " }"
1756
+ codes.push " [resultSet close];"
1757
+ codes.push "}];"
1758
+ codes.push "if(!found) return nil;"
1759
+ codes.push "return record;"
1760
+
1761
+
1762
+ temp = Array.new
1763
+ codes.each { | code |
1764
+ temp.push " " + code
1765
+ temp.push "\n"
1766
+ }
1767
+ return temp
1768
+ end
1769
+
1770
+ def self.selectStructByUnique(structKey, uniqueKey)
1771
+ codes = Array.new
1772
+ tableName = @yp_json_table
1773
+ propertyName = "(" + @yp_propertys.join(", ") + ")"
1774
+ valueNameTemp = Array.new
1775
+ recordsNameTemp = Array.new
1776
+ @yp_json_property.each { | property, type |
1777
+ valueNameTemp.push "#{property}"
1778
+
1779
+ if @yp_copyArray.include?(type)
1780
+ recordsNameTemp.push "#{property}"
1781
+ else
1782
+ recordsNameTemp.push "@(#{property})"
1783
+ end
1784
+ }
1785
+
1786
+ valueName = valueNameTemp.join(", ")
1787
+ recordsName = "(" + recordsNameTemp.join(", ") + ")"
1788
+
1789
+ condition_array = Array.new
1790
+ struct_array = Array.new
1791
+ struct_property_array = Array.new
1792
+ @yp_json_struct[structKey].each { | property |
1793
+ struct_property_array.push property
1794
+ condition_array.push "#{property}=?"
1795
+ type = @yp_json_property[property]
1796
+ if @yp_copyArray.include?(type)
1797
+ struct_array.push "a#{structKey}.#{property}"
1798
+ else
1799
+ struct_array.push "@(a#{structKey}.#{property})"
1800
+ end
1801
+ }
1802
+
1803
+ struct_property_arrayString = struct_property_array.join(", ")
1804
+
1805
+ conditionString = condition_array.join(" and ")
1806
+
1807
+ structString = struct_array.join(", ")
1808
+
1809
+ unique_condition_array = Array.new
1810
+ unique_array = Array.new
1811
+ @yp_json_unique[uniqueKey].each { | property |
1812
+ unique_condition_array.push "#{property}=?"
1813
+ type = @yp_json_property[property]
1814
+ if @yp_copyArray.include?(type)
1815
+ unique_array.push "#{property}"
1816
+ else
1817
+ unique_array.push "@(#{property})"
1818
+ end
1819
+ }
1820
+
1821
+ unique_conditionString = unique_condition_array.join(" and ")
1822
+
1823
+ uniqueString = unique_array.join(", ")
1824
+
1825
+ codes.push "if (!_dbQueue) return nil;"
1826
+ codes.push "__block #{structKey}* record = [[#{structKey} alloc] init];"
1827
+ codes.push "NSString* sql = @\"SELECT #{struct_property_arrayString} FROM #{@yp_json_table} WHERE #{unique_conditionString}\";"
1828
+ codes.push "__block BOOL found = NO;"
1829
+ codes.push "[_dbQueue inDatabase:^(FMDatabase *db) {"
1830
+ codes.push " FMResultSet *resultSet = [db executeQuery:sql, #{uniqueString}];"
1831
+ codes.push " while (resultSet.next) {"
1832
+ struct_property_array.each { | property |
1833
+ type = @yp_json_property[property]
1834
+ method = self.codeStringForColumnWithType(type)
1835
+ codes.push " record.#{property} = [resultSet #{method}:@\"#{property}\"];"
1836
+ }
1837
+ codes.push " found = YES;"
1838
+ codes.push " break;"
1839
+ codes.push " }"
1840
+ codes.push " [resultSet close];"
1841
+ codes.push "}];"
1842
+ codes.push "if(!found) return nil;"
1843
+ codes.push "return record;"
1844
+
1845
+
1846
+ temp = Array.new
1847
+ codes.each { | code |
1848
+ temp.push " " + code
1849
+ temp.push "\n"
1850
+ }
1851
+ return temp
1852
+ end
1853
+
1854
+ def self.selectStructByNormal(structKey, normalKey)
1855
+ codes = Array.new
1856
+ tableName = @yp_json_table
1857
+ propertyName = "(" + @yp_propertys.join(", ") + ")"
1858
+ valueNameTemp = Array.new
1859
+ recordsNameTemp = Array.new
1860
+ @yp_json_property.each { | property, type |
1861
+ valueNameTemp.push "#{property}"
1862
+
1863
+ if @yp_copyArray.include?(type)
1864
+ recordsNameTemp.push "#{property}"
1865
+ else
1866
+ recordsNameTemp.push "@(#{property})"
1867
+ end
1868
+ }
1869
+
1870
+ valueName = valueNameTemp.join(", ")
1871
+ recordsName = "(" + recordsNameTemp.join(", ") + ")"
1872
+
1873
+ condition_array = Array.new
1874
+ struct_array = Array.new
1875
+ struct_property_array = Array.new
1876
+ @yp_json_struct[structKey].each { | property |
1877
+ struct_property_array.push property
1878
+ condition_array.push "#{property}=?"
1879
+ type = @yp_json_property[property]
1880
+ if @yp_copyArray.include?(type)
1881
+ struct_array.push "a#{structKey}.#{property}"
1882
+ else
1883
+ struct_array.push "@(a#{structKey}.#{property})"
1884
+ end
1885
+ }
1886
+
1887
+ struct_property_arrayString = struct_property_array.join(", ")
1888
+
1889
+ conditionString = condition_array.join(" and ")
1890
+
1891
+ structString = struct_array.join(", ")
1892
+
1893
+ normal_condition_array = Array.new
1894
+ normal_array = Array.new
1895
+ @yp_json_normal[normalKey].each { | property |
1896
+ normal_condition_array.push "#{property}=?"
1897
+ type = @yp_json_property[property]
1898
+ if @yp_copyArray.include?(type)
1899
+ normal_array.push "#{property}"
1900
+ else
1901
+ normal_array.push "@(#{property})"
1902
+ end
1903
+ }
1904
+
1905
+ normal_conditionString = normal_condition_array.join(" and ")
1906
+
1907
+ normalString = normal_array.join(", ")
1908
+
1909
+ codes.push "if (!_dbQueue) return nil;"
1910
+ codes.push "__block NSMutableArray* records = [[NSMutableArray alloc] init];"
1911
+ codes.push "NSString* sql = @\"SELECT #{struct_property_arrayString} FROM #{@yp_json_table} WHERE #{normal_conditionString}\";"
1912
+ codes.push "__block BOOL found = NO;"
1913
+ codes.push "[_dbQueue inDatabase:^(FMDatabase *db) {"
1914
+ codes.push " FMResultSet *resultSet = [db executeQuery:sql, #{normalString}];"
1915
+ codes.push " while (resultSet.next) {"
1916
+ codes.push " #{structKey}* record = [[#{structKey} alloc] init];"
1917
+ struct_property_array.each { | property |
1918
+ type = @yp_json_property[property]
1919
+ method = self.codeStringForColumnWithType(type)
1920
+ codes.push " record.#{property} = [resultSet #{method}:@\"#{property}\"];"
1921
+ }
1922
+ codes.push " found = YES;"
1923
+ codes.push " [records addObject:record];"
1924
+ codes.push " }"
1925
+ codes.push " [resultSet close];"
1926
+ codes.push "}];"
1927
+ codes.push "if(!found) return nil;"
1928
+ codes.push "return records;"
1929
+
1930
+ temp = Array.new
1931
+ codes.each { | code |
1932
+ temp.push " " + code
1933
+ temp.push "\n"
1934
+ }
1935
+ return temp
1936
+ end
1937
+
1938
+ def self.selectStructSQLCondition(structKey)
1939
+ codes = Array.new
1940
+ tableName = @yp_json_table
1941
+ propertyName = "(" + @yp_propertys.join(", ") + ")"
1942
+ valueNameTemp = Array.new
1943
+ recordsNameTemp = Array.new
1944
+ @yp_json_property.each { | property, type |
1945
+ valueNameTemp.push "#{property}"
1946
+
1947
+ if @yp_copyArray.include?(type)
1948
+ recordsNameTemp.push "#{property}"
1949
+ else
1950
+ recordsNameTemp.push "@(#{property})"
1951
+ end
1952
+ }
1953
+
1954
+ valueName = valueNameTemp.join(", ")
1955
+ recordsName = "(" + recordsNameTemp.join(", ") + ")"
1956
+
1957
+ condition_array = Array.new
1958
+ struct_array = Array.new
1959
+ struct_property_array = Array.new
1960
+ @yp_json_struct[structKey].each { | property |
1961
+ struct_property_array.push property
1962
+ condition_array.push "#{property}=?"
1963
+ type = @yp_json_property[property]
1964
+ if @yp_copyArray.include?(type)
1965
+ struct_array.push "a#{structKey}.#{property}"
1966
+ else
1967
+ struct_array.push "@(a#{structKey}.#{property})"
1968
+ end
1969
+ }
1970
+
1971
+ struct_property_arrayString = struct_property_array.join(", ")
1972
+
1973
+ conditionString = condition_array.join(" and ")
1974
+
1975
+ structString = struct_array.join(", ")
1976
+
1977
+ codes.push "if (!_dbQueue) return nil;"
1978
+ codes.push "__block NSMutableArray* records = [[NSMutableArray alloc] init];"
1979
+ codes.push "NSString* sql = @\"SELECT #{struct_property_arrayString} FROM #{@yp_json_table}\";"
1980
+ codes.push "if(condition != nil) {"
1981
+ codes.push " sql = [NSString stringWithFormat:@\"%@ WHERE %@\", sql, condition];"
1982
+ codes.push "}"
1983
+ codes.push "__block BOOL found = NO;"
1984
+ codes.push "[_dbQueue inDatabase:^(FMDatabase *db) {"
1985
+ codes.push " FMResultSet *resultSet = [db executeQuery:sql];"
1986
+ codes.push " while (resultSet.next) {"
1987
+ codes.push " #{structKey}* record = [[#{structKey} alloc] init];"
1988
+ struct_property_array.each { | property |
1989
+ type = @yp_json_property[property]
1990
+ method = self.codeStringForColumnWithType(type)
1991
+ codes.push " record.#{property} = [resultSet #{method}:@\"#{property}\"];"
1992
+ }
1993
+ codes.push " found = YES;"
1994
+ codes.push " [records addObject:record];"
1995
+ codes.push " }"
1996
+ codes.push " [resultSet close];"
1997
+ codes.push "}];"
1998
+ codes.push "if(!found) return nil;"
1999
+ codes.push "return records;"
2000
+
2001
+ temp = Array.new
2002
+ codes.each { | code |
2003
+ temp.push " " + code
2004
+ temp.push "\n"
2005
+ }
2006
+ return temp
2007
+ end
2008
+
2009
+ def self.getQueue
2010
+ codes = Array.new
2011
+
2012
+ codes.push "return _dbQueue;"
2013
+
2014
+ temp = Array.new
2015
+ codes.each { | code |
2016
+ temp.push " " + code
2017
+ temp.push "\n"
2018
+ }
2019
+ return temp
2020
+ end
2021
+
2022
+ def self.get
2023
+ codes = Array.new
2024
+
2025
+ codes.push "static #{@yp_dao} *sI;"
2026
+ codes.push "static dispatch_once_t onceToken;"
2027
+ codes.push "dispatch_once(&onceToken, ^{"
2028
+ codes.push " sI = [[#{@yp_dao} alloc] initPrivate];"
2029
+ codes.push "});"
2030
+ codes.push "return sI;"
2031
+
2032
+ temp = Array.new
2033
+ codes.each { | code |
2034
+ temp.push " " + code
2035
+ temp.push "\n"
2036
+ }
2037
+ return temp
2038
+ end
2039
+
2040
+ def self.openWithPath
2041
+ codes = Array.new
2042
+
2043
+ codes.push "@synchronized(self) {"
2044
+ codes.push " if(_dbQueue != nil) {"
2045
+ codes.push " if([path isEqual:_path]) return YES;"
2046
+ codes.push " }"
2047
+ codes.push " _path = path;"
2048
+ codes.push " NSString *dbDirectoryPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:path];"
2049
+ codes.push " NSFileManager *fileManager = [NSFileManager defaultManager];"
2050
+ codes.push " if (![fileManager fileExistsAtPath:dbDirectoryPath]) {"
2051
+ codes.push " [fileManager createDirectoryAtPath:dbDirectoryPath withIntermediateDirectories:YES attributes:nil error:nil];"
2052
+ codes.push " }"
2053
+ codes.push " NSString* dbPath = [dbDirectoryPath stringByAppendingPathComponent:DATABASE_NAME];"
2054
+ codes.push " _dbQueue = [FMDatabaseQueue databaseQueueWithPath:dbPath];"
2055
+ codes.push "}"
2056
+ codes.push "[self _createTables];"
2057
+ codes.push "return YES;"
2058
+
2059
+ temp = Array.new
2060
+ codes.each { | code |
2061
+ temp.push " " + code
2062
+ temp.push "\n"
2063
+ }
2064
+ return temp
2065
+ end
2066
+
2067
+ def self.hideMethod
2068
+ codes = Array.new
2069
+
2070
+ codes.push "- (instancetype)init {"
2071
+ codes.push " @throw [NSException exceptionWithName:@\"Can't init!\" reason:@\"instance class!\" userInfo:nil];"
2072
+ codes.push " return nil;"
2073
+ codes.push "}"
2074
+
2075
+ codes.push "- (void)dealloc {"
2076
+ codes.push " [[NSNotificationCenter defaultCenter] removeObserver:self];"
2077
+ codes.push "}"
2078
+
2079
+ codes.push "- (void)resetDatebaseDBQueue {"
2080
+ codes.push " @synchronized(self) {"
2081
+ codes.push " _dbQueue = nil;"
2082
+ codes.push " _path = nil;"
2083
+ codes.push " }"
2084
+ codes.push "}"
2085
+ codes.push "- (instancetype)initPrivate {"
2086
+ codes.push " self = [super init];"
2087
+ codes.push " if (!self) {"
2088
+ codes.push " return nil;"
2089
+ codes.push " }"
2090
+ codes.push " _dbQueue = nil;"
2091
+ codes.push " _path = nil;"
2092
+ codes.push " [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resetDatebaseDBQueue) name:@\"kYPDatebaseServiceResetDBQueue\" object:nil];"
2093
+ codes.push " return self;"
2094
+ codes.push "}"
2095
+
2096
+
2097
+ codes.push "- (void)_createTables {"
2098
+ codes.push " __block NSString* sql = nil;"
2099
+ codes.push " sql = @\"SELECT * FROM #{@yp_json_table} limit 1\";"
2100
+ codes.push " [_dbQueue inDatabase:^(FMDatabase *db) {"
2101
+ codes.push " FMResultSet *resultSet = [db executeQuery:sql];"
2102
+ codes.push " if(resultSet != nil) {"
2103
+
2104
+ @yp_json_property.each { | property, type |
2105
+ sqlType = self.sqlType(type)
2106
+ if property != @yp_primarykey
2107
+ codes.push " if([resultSet columnIndexForName:@\"#{property}\"] < 0) {"
2108
+ codes.push " sql = @\"ALTER TABLE #{@yp_json_table} ADD COLUMN #{property} #{sqlType}\";"
2109
+ codes.push " [db executeUpdate:sql];"
2110
+ codes.push " }"
2111
+ end
2112
+ }
2113
+ codes.push " [resultSet close];"
2114
+ codes.push " } else {"
2115
+
2116
+ createProperty = Array.new
2117
+ @yp_json_property.each { | property, type |
2118
+ sqlType = self.sqlType(type)
2119
+ if property != @yp_primarykey
2120
+ createProperty.push property + " " + sqlType
2121
+ elsif
2122
+ if @yp_bool_autoincrement == 1
2123
+ createProperty.push property + " " + sqlType + " " + "PRIMARY KEY AUTOINCREMENT"
2124
+ elsif
2125
+ createProperty.push property + " " + sqlType + " " + "PRIMARY KEY"
2126
+ end
2127
+ end
2128
+ }
2129
+
2130
+ @yp_json_unique.each { | property, type |
2131
+ createProperty.push "UNIQUE(#{type.join(", ")})"
2132
+ }
2133
+
2134
+ createPropertyStr = createProperty.join(", ")
2135
+
2136
+ codes.push " sql = @\" CREATE TABLE IF NOT EXISTS #{@yp_json_table}(#{createPropertyStr})\";"
2137
+ codes.push " if ([db executeUpdate:sql]) {"
2138
+ if @yp_need_log
2139
+ codes.push " YPLogDebug(@\"create table #{@yp_json_table} success\");"
2140
+ elsif
2141
+ codes.push " NSLog(@\"create table #{@yp_json_table} success\");"
2142
+ end
2143
+ codes.push " } else {"
2144
+ if @yp_need_log
2145
+ codes.push " YPLogError(@\"create table #{@yp_json_table} failed\");"
2146
+ elsif
2147
+ codes.push " NSLog(@\"create table #{@yp_json_table} failed\");"
2148
+ end
2149
+ codes.push " return;"
2150
+ codes.push " }"
2151
+ codes.push " }"
2152
+ codes.push " }];"
2153
+
2154
+ @yp_json_normal.each { | property, type |
2155
+ codes.push " sql = @\"SELECT * FROM sqlite_master where tbl_name = '#{@yp_json_table}' and type = 'index' and name = '#{property}'\";"
2156
+ codes.push " [_dbQueue inDatabase:^(FMDatabase *db) {"
2157
+ codes.push " FMResultSet *resultSet = [db executeQuery:sql];"
2158
+ codes.push " if(!resultSet.next) {"
2159
+ codes.push " sql = @\"CREATE INDEX #{property} ON #{@yp_json_table}(#{type.join(", ")})\";"
2160
+ codes.push " if ([db executeUpdate:sql]) {"
2161
+ if @yp_need_log
2162
+ codes.push " YPLogDebug(@\"create index #{@yp_json_table} success\");"
2163
+ elsif
2164
+ codes.push " NSLog(@\"create index #{@yp_json_table} success\");"
2165
+ end
2166
+ codes.push " } else {"
2167
+ if @yp_need_log
2168
+ codes.push " YPLogError(@\"create table #{@yp_json_table} failed\");"
2169
+ elsif
2170
+ codes.push " NSLog(@\"create table #{@yp_json_table} failed\");"
2171
+ end
2172
+ codes.push " return;"
2173
+ codes.push " }"
2174
+ codes.push " }"
2175
+ codes.push " [resultSet close];"
2176
+ codes.push " }];"
2177
+ }
2178
+ codes.push "}"
2179
+
2180
+ temp = Array.new
2181
+ codes.each { | code |
2182
+ temp.push code
2183
+ temp.push "\n"
2184
+ }
2185
+ return temp
2186
+ end
2187
+
2188
+
2189
+ def self.sqlType (type)
2190
+ if type == "long" || type == "int64_t" || type == "int32_t" || type == "NSInteger" || type == "NSUInteger"
2191
+ return "INTEGER"
2192
+ elsif type == "NSDate"
2193
+ return "TIMESTAMP"
2194
+ elsif type == "BOOL"
2195
+ return "INTEGER"
2196
+ elsif type == "CGFloat"
2197
+ return "FLOAT"
2198
+ elsif type == "NSString"
2199
+ return "TEXT"
2200
+ else
2201
+ return "TEXT"
2202
+ end
2203
+ end
2204
+
2205
+ def self.codeStringForColumnWithType (type)
2206
+ if type == "long" || type == "int64_t" || type == "int32_t" || type == "NSInteger" || type == "NSUInteger"
2207
+ return "longLongIntForColumn"
2208
+ elsif type == "NSDate"
2209
+ return "dateForColumn"
2210
+ elsif type == "BOOL"
2211
+ return "boolForColumn"
2212
+ elsif type == "CGFloat"
2213
+ return "doubleForColumn"
2214
+ elsif type == "NSString"
2215
+ return "stringForColumn"
2216
+ else
2217
+ return "stringForColumn"
2218
+ end
2219
+ end
2220
+
2221
+ end