forked from rhomobile/rhodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrhom.txt
876 lines (672 loc) · 26.3 KB
/
rhom.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
# Using the Local Database with Rhom
Rhom is a mini database object mapper for Rhodes. It provides a high level interface to make it very powerful and simple to use a local database. That database is SQLite on all platforms except BlackBerry where it is HSQLDB.
Rhom currently supports two model types: ***Property Bag (default)*** and ***Fixed Schema***
## Property Bag
With a property bag model, all data is stored in a single table using the object-attribute-value pattern also referred to as the [Entity-attribute-value model](http://en.wikipedia.org/wiki/Entity-attribute-value_model).
### Property Bag Advantages
* Simple to use, it doesn't require specifying attributes.
* Data migrations are not necessary.
* Attributes can be added or removed without modifying the database schema.
### Property Bag Disadvantages
* For some applications, the database size may be significantly larger than fixed schema. This is because each attribute is indexed for fast lookup.
* Sync process may be slightly slower because inserts are performed at attribute level.
In a property bag model, Rhom groups objects by their source id and object id. The following example illustrates this idea:
<pre>
Source ID: 1, Model Name: Account
+-----------+----------+--------------+----------------------+
| source_id | attrib | object | value |
+-----------+----------+--------------+------- --------------+
| 1 | name | 48f39f63741b | A.G. Parr PLC 37862 |
| 1 | industry | 48f39f63741b | Entertainment |
| 1 | name | 48f39f230529 | Jones Group |
| 1 | industry | 48f39f230529 | Sales |
+-----------+----------+--------------+----------------------+
</pre>
Here, Rhom will expose a class `Account` with two attributes: `name` and `industry`
:::ruby
account = Account.find('48f39f63741b')
account.name
#=> "A.G. Parr PLC 37862"
account.industry
#=> "Entertainment"
### Using Property Bag Models
To use a property bag model, simply generate a new model with some attributes:
:::term
$ rhodes model product name,brand,price,quantity,sku
This will generate a file called `product.rb` which looks like:
:::ruby
class Product
include Rhom::PropertyBag
# Uncomment the following line to enable sync with Product.
# enable :sync
#add model specifc code here
end
There are several features you can enable or disable in the model, below is a complete list:
:::ruby
class SomeModel
include Rhom::PropertyBag
# rhoconnect settings
# Enable sync for this model.
# Default is disabled.
enable :sync
# Set the type of sync this model
# will use (default :incrmental).
# Set to :bulk_only to disable incremental
# sync and only use bulk sync.
set :sync_type, :bulk_only
# Set the sync priority for this model.
# 1000 is default, set to lower number
# for a higher priority.
set :sync_priority, 1
# Instruct Rhom to send all attributes
# to RhoConnect when an object is updated.
# Default is disabled, only changed attributes
# are sent.
enable :full_update
# RhoConnect provides a simple way to keep data out of redis.
# If you have sensitive data that you do not want saved in redis,
# add the pass_through option in settings/settings.yml for each source.
# Add pass_through to client model definition
enable :pass_through
# model settings
# Define how data is partitioned for this model.
# For synced models default is :user.
# For non-synced models default is :local
# If you have an :app partition
# for your RhoConnect source adapter and use bulk sync,
# set this to :app also.
set :partition, :app
# Define blob attributes for the model.
# :blob Declare property as a blob type
#
# :overwrite (optional) Overwrite client copy
# of blob with new copy from server.
# This is useful when RhoConnect modifies
# images sent from Rhodes, for example
# zooming or cropping.
property :image_url, :blob, :overwrite
# You can define your own properties also
property :mycustomproperty, 'hello'
end
## Fixed Schema
With a fixed schema model, each model has a separate database table and each attribute exists as a column in the table. In this sense, fixed schema models are similar to traditional relational tables.
### Fixed Schema Advantages
* Smaller database size, indexes can be specified only on specific attributes.
* Sync process may perform faster because whole objects are inserted at a time.
### Fixed Schema Disadvantages
* Schema changes must be handled with data migrations.
* Database performance may be slow unless you specify proper indexes.
### Using Fixed Schema Models
Using a fixed schema model involves an additional step to using a property bag model.
First, generate the model using the `rhodes` command:
:::term
$ rhodes model product name,brand,price,quantity,sku
Next, change the include statement in `product.rb` to `include Rhom::FixedSchema` and add the attributes:
:::ruby
class Product
include Rhom::FixedSchema
# Uncomment the following line to enable sync with Product.
# enable :sync
property :name, :string
property :brand, :string
property :price, :string
property :quantity, :string
property :sku, :string
property :int_prop, :integer
property :float_prop, :float
property :date_prop, :date #translate to integer type
property :time_prop, :time #translate to integer type
end
That's it! Now your model is a fixed schema model, the table will be generated automatically for you when the application launches.
Below is a full list of options available to fixed schema models:
:::ruby
class SomeModel
include Rhom::FixedSchema
# rhoconnect settings
# Enable sync for this model.
# Default is disabled.
enable :sync
# Set the type of sync this model
# will use (default :incrmental).
# Set to :bulk_only to disable incremental
# sync and only use bulk sync.
set :sync_type, :bulk_only
# Set the sync priority for this model.
# 1000 is default, set to lower number
# for a higher priority.
set :sync_priority, 1
# Instruct Rhom to send all attributes
# to RhoConnect when an object is updated.
# Default is disabled, only changed attributes
# are sent.
enable :full_update
# RhoConnect provides a simple way to keep data out of redis.
# If you have sensitive data that you do not want saved in redis,
# add the pass_through option in settings/settings.yml for each source.
# Add pass_through to client model definition
enable :pass_through
# model settings
# Define how data is partitioned for this model.
# Default is :user. If you have an :app partition
# for your RhoConnect source adapter and use bulk sync,
# set this to :app also.
set :partition, :app
# Set the current version of the fixed schema.
# Your application may use it for data migrations.
set :schema_version, '1.0'
# Define fixed schema attributes.
# :string and :blob types are supported.
property :name, :string
property :tag, :string
property :phone, :string
property :image_url, :blob
# Define a named index on a set of attributes.
# For example, this will create index for name and tag columns.
index :by_name_tag, [:name, :tag]
# Define a unique named index on a set of attributes.
# For example, this will create unique index for the phone column.
unique_index :by_phone, [:phone]
# Define blob attributes for the model.
# :blob Declare property as a blob type
#
# :overwrite (optional) Overwrite client copy
# of blob with new copy from server.
# This is useful when RhoConnect modifies
# images sent from Rhodes, for example
# zooming or cropping.
property :image_url, :blob, :overwrite
# You can define your own properties also
property :mycustomproperty, 'hello'
end
## Fixed Schema Data Migrations
Rhom provides an application hook to migrate the data manually. You can also use this hook to run business logic related to updating the database. For example, your application may want to display a customized alert notifying the user that a migration is in progress and it may take a few moments.
To use this hook, first we need to track the `:schema_version` in our model:
:::ruby
class Product
include Rhom::FixedSchema
set :schema_version, '1.1'
end
Next, we will implement the following hook in our `application.rb` class:
#### `on_migrate_source(old_version, new_src)`
This is called on application start when `:schema_version` has changed.
:::ruby
class AppApplication < Rho::RhoApplication
# old_version String containing old version value (i.e. '1.0')
# new_src Hash with source information:
# 'schema_version', 'name', 'schema'
# new_src['schema']['sql'] contains new schema sql
def on_migrate_source(old_version, new_src)
# ... do something like alert user ...
db = Rho::RHO.get_src_db(new_src['name'])
db.execute_sql("ALTER TABLE #{new_src['name']} ADD COLUMN mytest VARCHAR DEFAULT null")
true # does not create table
end
end
**NOTE: To modify schema without recreate table, you can use only ADD COLUMN command, you cannot remove column or change type(This is sqlite limitation) **
Return `false` to run the custom sql specified by the new_src['schema']['sql'] string:
:::ruby
def on_migrate_source(old_version, new_src)
# ... do something like alert user ...
false # create table by source schema - useful only for non-synced models
end
**NOTE: For sync sources, you cannot just recreate table without data copy. Because server will not send this data at sync time. **
## Property Bag Data Migrations
No data migration required, since all attributes are dynamic.
If you want to remove all local data when upgrading to new application version: change `app_db_version` in `rhoconfig.txt`.
This scenario will work for Property Bag and Fixed Schema models.
## Rhom API
Below is the full list of methods available to Rhom models:
### `clear_notification`
Used to clear the notification for the object, see the [sync notification section](/rhodes/synchronization#notifications) for more details.
### `delete_all(conditions)`
Deletes all rhom objects for a source, optionally filtering by conditions:
:::ruby
# :conditions Delete only objects matching these criteria.
# Supports find() conditions.
# :op See advanced find syntax
Account.delete_all(:conditions => {'industry'=>'electronics'})
### `destroy`
Delete a rhom object.
:::ruby
@account = Account.find(:all).first
@account.destroy
### `find(*args)`
Returns rhom object(s) based on the following arguments:
:::ruby
# :all returns all objects w/ optional conditions
#
# :first returns first object matching conditions
#
# :count returns number of objects matching conditions
#
# :conditions (optional) hash of attribute/values to match
# supports sql fragment(i.e. "name like 'rhomobile')
# or sql fragment with binding (you have to define :select with sql queries)
# (i.e. ["name like ?", "'#{company#}'"])
# Note: use single comma around string values
#
# :order (optional) attribute(s) to order the list
#
# :orderdir (optional) order direction('ASC' (default), 'DESC' )
#
# :select (optional) array of string attributes to return
# with the object. This is useful if your model
# has a lot of attributes but your query only needs
# a few of them.
#
# :per_page (optional) maximum number of items return
#
# :offset (optional) offset from beginning of the list
acct = Account.find "3560c0a0-ef58-2f40-68a5-48f39f63741b"
acct.name
#=> "A.G. Parr PLC 37862"
accts = Account.find(:all, :select => ['name','address'])
accts[0].name
#=> "A.G. Parr PLC 37862"
accts[0].telephone
#=> nil
**NOTE: Use SQL fragments with caution. They are considerably slower than advanced queries [described below](/rhodes/rhom#advanced-queries). You also have to specify :select parameter.**
#### Order Examples
The `:order` argument accepts several forms:
* `:order` by one attribute:
:::ruby
@accts = Account.find(
:all,
:order => 'name',
:orderdir => 'DESC'
)
* `:order` by one attribute with a block:
:::ruby
@accts = Account.find(:all, :order => 'name') do |x,y|
y <=> x
end
* `:order` with a block:
:::ruby
@accts = Account.find(:all) do |item1,item2|
item2.name <=> item1.name
end
* `:order` by multiple attributes:
:::ruby
@accts = Account.find(
:all,
:order => ['name', 'industry'],
:orderdir => ['ASC', 'DESC']
)
### `find_all(*args)`
Alias for find(:all,*args).
### `find_by_sql(sql_query)`
Returns rhom object(s) based on sql_query. This method works only for schema models:
:::ruby
@accts = Account.find_by_sql("SELECT * FROM Account")
### `new(attributes = nil)`
Creates a new rhom object and assigns given attributes, or initializes an empty rhom object.
:::ruby
@account = Account.new(
{"name" => "ABC Inc.","address" => "555 5th St."}
)
@account.name
#=> "ABC Inc."
### `create(attributes)`
Creates a new rhom object and saves to the database.
**NOTE: This is the fastest way to insert a single item into the database.**
:::ruby
@account = Account.create(
{"name" => "some new record", "industry" => "electronics"}
)
### `paginate(*args)`
Calls `find` with a limit on the # of records. This emulates rails' classic pagination syntax. Default page size is 10.
:::ruby
# :page which page to return, used as offset
# in combination with :per_page
#
# :per_page number of records to return (used as limit)
#
# :conditions same as find with :conditions
#
# :order same as find with :order
#
# :select same as find with :select
Account.paginate(:page => 0)
#=> returns first 10 records
Account.paginate(:page => 1, :per_page => 20)
#=> returns records 21-40
Account.paginate(
:page => 5,
:conditions => {'industry' => 'Technology'},
:order => 'name'
) #=> you can have :conditions and :order as well
### `sync(callback = nil, callback_data = "", show_status_popup = nil, query_params = "")`
Start the sync process for a model. If the callback is set, `SyncEngine.set_notification` is called before `SyncEngine.dosync`.
query_params will pass to sync server
:::ruby
Account.sync( url_for(:action => :sync_callback) )
Account.sync( url_for(:action => :sync_callback), "", false, "param1=123¶m2=abc" )
### `set_notification(url, params = nil)`
Set a notification to be called when the sync is complete for this model. This is useful for example if you want to refresh the current list page or display an alert when new data is synchronized. See the [sync notification docs](/rhodes/synchronization#notifications) for more information.
:::ruby
Account.set_notification( url_for(:action => :sync_notify) )
### `update_attributes(attributes)`
Updates the current rhom object's attributes and saves it to the database
**NOTE: This is the fastest way to add or update item attributes.**
:::ruby
@account = Account.find(
:all,
:conditions => {'name' => 'ABC Inc.'}
)
@account.update_attributes(
{"name" => "ABC Inc.", "industry" => "Technology"}
)
@account.industry
#=> "Technology"
### `save`
Saves the current rhom object to the database.
:::ruby
@account = Account.new(
{"name" => "some new record", "industry" => "electronics"}
)
@account.save
### `can_modify`
Before displaying an edit page for an object, your application can check if the object is currently being accessed by the sync process. If it is, you should disable editing of the object. `can_modify` could return true, for example, on a new local record that was created and sent to the RhoConnect application, but no response has been received yet.
:::ruby
def edit
@product = Product.find(@params['id'])
if @product && [email protected]_modify
render :action => :show_edit_error
else
render :action => :edit
end
end
### `changed?`
Determine if a rhom model has local database changes that need to be synchronized.
:::ruby
def should_sync_product_object
if Product.changed?
#... do stuff ...
end
end
## Associations
For sync-enabled models, Rhom offers associations as a means to automatically trigger sync updates for dependent objects. This is useful where you have relationships between backend service objects.
For example, you can have a list of customers who have purchased a product:
:::ruby
class Customer
include Rhom::PropertyBag
# Declare container model and attribute.
belongs_to :product_id, 'Product'
end
In your `product_controller.rb`, assign the `:belongs_to` attribute when a product is created:
:::ruby
def create
@product = Product.new(@params['product'])
@product.save
cust = Customer.find(:first) # find the customer
cust.product_id = @product.object
cust.save
redirect :action => :index
end
You can also define polymorphic associations, or associations across multiple classes.
Using array notation:
:::ruby
belongs_to :parent_id, ['Product', 'Cases']
Or multiple declarations:
:::ruby
belongs_to :parent_id, 'Product'
belongs_to :parent_id, 'Cases'
**NOTE: After a new product is created, the `:product_id` for the `Customer` records will be updated to the new value.**
## Accessing Sync Info with RhomSource
Rhom exposes sync information as a `RhomSource` object. You can use this information for alerts, status pages, etc.
To access a RhomSource, load it by name:
:::ruby
@source = RhomSource.find('source_name')
Here are the available statistics:
### `source_id`
Returns the id of a source.
:::ruby
@source.source_id
#=> 1
### `name`
Name of the source.
:::ruby
@source.name
#=> "Product"
### `last_updated`
Last time the source was synchronzied (in `Time.at` format).
:::ruby
@source.last_updated.to_s
#=> "Wed Jan 19 18:35:05 -0800 2011"
For example, to show the formatted time for the `Product` model:
:::ruby
RhomSource.find(
Product.get_source_name
).last_updated.strftime("%m/%d/%Y, %I:%M%p")
#=> "01/19/2011, 06:40PM"
### `last_inserted_size`
Number of records inserted on last sync.
:::ruby
@source.last_inserted_size
#=> 3
### `last_deleted_size`
Number of records deleted on last sync.
:::ruby
@source.last_deleted_size
#=> 1
### `last_sync_duration`
This returns the duration in seconds of the last sync for this source.
:::ruby
@source.last_sync_duration
#=> 7
### `last_sync_success`
Returns 1 if last sync was successful, 0 if it failed.
:::ruby
@source.last_sync_success
#=> 1
### `distinct_objects`
Number of records for this source.
:::ruby
@source.distinct_objects
#=> 837
## Resetting the Database
Rhodes provides the following functions for recovering the database from a bad or corrupt state, or if the RhoConnect server returns errors.
### `Rhom::Rhom.database_full_reset(reset_client_info=false, reset_local_models=true)`
Deletes all records from the property bag and model tables.
:::ruby
# reset_client_info If set to true, client_info
# table will be cleaned.
#
# reset_local_models If set to true, local(non-synced models)
# will be cleaned.
Rhom::Rhom.database_full_reset(false,true)
### `Rhom::Rhom.database_full_reset_and_logout`
Perform a full reset and then logout the RhoConnect client.
:::ruby
Rhom::Rhom.database_full_reset_and_logout
### `Rhom::Rhom.database_fullclient_reset_and_logout`
Equivalent to `Rhom::Rhom.database_full_reset(true)` followed by `SyncEngine.logout`.
:::ruby
Rhom::Rhom.database_fullclient_reset_and_logout
**NOTE: If you receive a sync error "Unknown client" message in your sync callback, this means that the RhoConnect server no longer knows about the client and a `Rhom::Rhom.database_fullclient_reset_and_logout` is recommended. This error requires proper intervention in your app so you can handle the state before resetting the client. For example, your sync notification could contain the following:**
:::ruby
if @params['error_message'].downcase == 'unknown client'
puts "Received unknown client, resetting!"
Rhom::Rhom.database_fullclient_reset_and_logout
end
### `Rhom::Rhom.database_local_reset`
Reset only local(non-sync-enabled) models.
:::ruby
Rhom::Rhom.database_local_reset
### `Rhom::Rhom.database_full_reset_ex( :models => [model_name1, model_name2], :reset_client_info=>false, :reset_local_models => true)`
Deletes all records from the property bag and model tables, if models are set then reset only selected models
:::ruby
# models Array of models names to reset
# reset_client_info If set to true, client_info
# table will be cleaned.
#
# reset_local_models If set to true, local(non-synced models)
# will be cleaned.
Rhom::Rhom.database_full_reset_ex(:models => ['Product', 'Customer'])
## Seeding the Database
If your application requires seeding some initial data, you can use the following function:
### `Rho::RhoUtils.load_offline_data(table_array, seed_prefix_directory)`
:::ruby
# table_array Array containing table names
# corresponding to pipe-delimited files.
#
# seed_prefix_directory Relative path to directory containing
# a 'fixtures' directory of files.
Rho::RhoUtils.load_offline_data(table_array, seed_prefix_directory)
For example, in the rhodes/spec/framework_spec, we use `load_offline_data` to seed the device database for each test:
:::ruby
Rho::RhoUtils.load_offline_data(
['client_info','object_values'], 'spec'
)
In this example, there is a 'spec/fixtures' directory which contains a `client_info.txt` and `object_values.txt` pipe-delimited files. These files are structured as follows:
`client_info.txt`:
<pre>
client_id|last_sync_success
67320d31-e42e-4156-af91-5d9bd7175b08|
</pre>
`object_values.txt`:
<pre>
source_name|attrib|object|value
Case|status|4900dc4c072c|New|
Case|assigned_user_id|4900dc4c072c|48fce5e9fb16|
Case|work_log|4900dc4c072c||
Case|priority|4900dc4c072c|High|
...
</pre>
**NOTE: The column names are always the first line of the file.**
## Advanced Queries
### `find(*args)` (advanced conditions)
Rhom also supports advanced find `:conditions`. Using advanced `:conditions`, rhom can optimize the query for the property bag table.
Let's say we have the following SQL fragment condition:
:::ruby
Product.find(
:all,
:conditions => [
"LOWER(description) like ? or LOWER(title) like ?",
query,
query
],
:select => ['title','description']
)
Using advanced `:conditions`, this becomes:
:::ruby
Product.find(
:all,
:conditions => {
{
:func => 'LOWER',
:name => 'description',
:op => 'LIKE'
} => query,
{
:func => 'LOWER',
:name => 'title',
:op => 'LIKE'
} => query
},
:op => 'OR',
:select => ['title','description']
)
You can also use the 'IN' operator:
:::ruby
Product.find(
:all,
:conditions => {
{
:name => "image_uri",
:op => "IN"
} => "'15704','15386'"
}
)
# or use array notation
Product.find(
:all,
:conditions => {
{
:name => "image_uri",
:op => "IN"
} => ["15704","15386"]
}
)
You can also group `:conditions`:
:::ruby
cond1 = {
:conditions => {
{
:func => 'UPPER',
:name => 'name',
:op => 'LIKE'
} => query,
{
:func => 'UPPER',
:name => 'industry',
:op => 'LIKE'
} => query
},
:op => 'OR'
}
cond2 = {
:conditions => {
{
:name => 'description',
:op => 'LIKE'
} => 'Hello%'
}
}
@accts = Account.find(
:all,
:conditions => [cond1, cond2],
:op => 'AND',
:select => ['name','industry','description']
)
## Find by numeric field
To use number comparison conditions in find use CAST :
:::ruby
@accts = Account.find(:all,
:conditions => { {:func=> 'CAST', :name=>'rating as INTEGER', :op=>'<'} => 3 } )
#or using sql query:
size = 3
@accts = Account.find(:all,
:conditions => ["CAST(rating as INTEGER)< ?", "#{size}"], :select => ['rating'] )
## Database Encryption
If your application requires that the local database is encrypted on the filesystem, you can enable it by setting a flag in `build.yml`:
:::yaml
encrypt_database: 1
**NOTE: Database encryption is not supported for applications that use bulk sync at this time.**
### Platform Notes
* iOS: Uses AES 128 encryption algorithm from iOS SDK.
* Android: Uses AES 128 ecryption algorithm from Android SDK.
* Windows Mobile: Uses RC4 algorithm from Windows Mobile SDK.
### Blackberry Notes
* Any Blackberry versions: Uses AES 128 ecryption algorithm from Blackberry JDK with HSQL database
* Blackberry JDE >= 5.0 with SQLITE: Uses built-in encryption library for SQLite database.
**NOTE: Bulk sync is not supported in this mode.**
* Any Blackberry Version: The user can turn on memory encryption (device memory and sdcard), This policy can also can be enforced by the Blackberry enterprise server: <a href="http://docs.blackberry.com/en/admin/deliverables/3940/file_encryption_STO.pdf"/>
**NOTE: In this case you have to use HSQLDB even on Blackberry device OS >= 5.0, because SQLite does not encrypt database file. You can force Rhodes to use HSQLDB for all Blackberry OS versions by adding the following to `build.yml`:**
:::yaml
bb:
use_sqlite: 0
## Perfomance Tips
* Before test application for perfomance set warning log level in rhoconfig.txt(MUST set for Blackberry testing):
MinSeverity = 3
* All database modification operations can be slow, especially on big databases. So optimize object modification - prepare data and call create/update_attributes once
* Do not use sql conditions in Model.find, use Advanced Queries.
* Use Model.create to insert object to database
* Use update_attributes to add or modify object attributes
* To insert/update multiple object/models use database transaction
db = ::Rho::RHO.get_src_db('Model')
db.start_transaction
begin
items.each do |item|
# create hash of attribute/value pairs
data = {
:field1 => item['value1'],
:field2 => item['value2']
}
# Creates a new Model object and saves it
new_item = Model.create(data)
end
db.commit
rescue
db.rollback
end