DbMotion 0.0.6 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/DbMotion.rb +1 -0
- data/lib/DbMotion/database.rb +159 -70
- data/lib/DbMotion/model.rb +128 -0
- data/lib/DbMotion/version.rb +1 -1
- metadata +3 -2
data/lib/DbMotion.rb
CHANGED
data/lib/DbMotion/database.rb
CHANGED
@@ -1,90 +1,179 @@
|
|
1
1
|
module DbMotion
|
2
|
-
class Database
|
2
|
+
class Database
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
def self.shared
|
5
|
+
@@db
|
6
|
+
end
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
def self.setSharedSession(_db)
|
9
|
+
@@db=_db
|
10
|
+
end
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
12
|
+
def create_entry(_name)
|
13
|
+
NSEntityDescription.insertNewObjectForEntityForName(_name, inManagedObjectContext:@context)
|
14
|
+
end
|
15
|
+
|
16
|
+
def get_obj_id_from_url(_url)
|
17
|
+
@context.persistentStoreCoordinator.managedObjectIDForURIRepresentation(url)
|
18
|
+
end
|
19
|
+
|
20
|
+
def get_url_from_obj_id(_obj_id)
|
21
|
+
_obj_id.URIRepresentation
|
22
|
+
end
|
23
|
+
|
24
|
+
def find_by_obj_id(_obj_id)
|
25
|
+
error_ptr = Pointer.new(:object)
|
26
|
+
@context.existingObjectWithID(_obj_id, error:error_ptr)
|
27
|
+
end
|
28
|
+
|
29
|
+
# Example: _sort=[NSSortDescriptor.alloc.initWithKey('name', ascending:true)]
|
30
|
+
def find_entry(_name, _sort=[])
|
31
|
+
request = NSFetchRequest.alloc.init
|
32
|
+
request.entity = NSEntityDescription.entityForName(_name, inManagedObjectContext:@context)
|
33
|
+
request.sortDescriptors = _sort
|
34
|
+
error_ptr = Pointer.new(:object)
|
35
|
+
@context.executeFetchRequest(request, error:error_ptr)
|
36
|
+
end
|
20
37
|
|
21
|
-
|
22
|
-
|
23
|
-
|
38
|
+
def delete_entry(_obj)
|
39
|
+
@context.deleteObject(_obj)
|
40
|
+
end
|
24
41
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
42
|
+
def save
|
43
|
+
error_ptr = Pointer.new(:object)
|
44
|
+
@context.save(error_ptr)
|
45
|
+
end
|
29
46
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
end
|
46
|
-
@@db=self
|
47
|
-
self
|
48
|
-
end
|
47
|
+
def open(_db_file, withModel: _model)
|
48
|
+
@model=_model.versions.last
|
49
|
+
@store = NSPersistentStoreCoordinator.alloc.initWithManagedObjectModel(@model)
|
50
|
+
@store_file = File.join(NSHomeDirectory(), 'Documents', _db_file)
|
51
|
+
@store_url = NSURL.fileURLWithPath(@store_file)
|
52
|
+
unless compatible?
|
53
|
+
migrate(_model.versions)
|
54
|
+
end
|
55
|
+
@context = NSManagedObjectContext.alloc.init
|
56
|
+
@context.persistentStoreCoordinator = @store
|
57
|
+
error_ptr = Pointer.new(:object)
|
58
|
+
unless @store.addPersistentStoreWithType(NSSQLiteStoreType, configuration:nil, URL:@store_url, options:nil, error:error_ptr)
|
59
|
+
raise "Can't add persistent SQLite store: #{error_ptr[0].description}"
|
60
|
+
end
|
61
|
+
self
|
62
|
+
end
|
49
63
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
64
|
+
def open(_db_file, withMOMFile: _mom_file)
|
65
|
+
@mom_file=_mom_file % "cur"
|
66
|
+
@mom_url = NSURL.fileURLWithPath("#{NSBundle.mainBundle.resourcePath}/#{@mom_file}")
|
67
|
+
@model=NSManagedObjectModel.alloc.initWithContentsOfURL(@mom_url)
|
68
|
+
@store = NSPersistentStoreCoordinator.alloc.initWithManagedObjectModel(@model)
|
69
|
+
@store_file = File.join(NSHomeDirectory(), 'Documents', _db_file)
|
70
|
+
@store_url = NSURL.fileURLWithPath(@store_file)
|
71
|
+
unless compatible?
|
72
|
+
migrate_mom(_mom_file)
|
73
|
+
end
|
74
|
+
@context = NSManagedObjectContext.alloc.init
|
75
|
+
@context.persistentStoreCoordinator = @store
|
76
|
+
error_ptr = Pointer.new(:object)
|
77
|
+
unless @store.addPersistentStoreWithType(NSSQLiteStoreType, configuration:nil, URL:@store_url, options:nil, error:error_ptr)
|
78
|
+
raise "Can't add persistent SQLite store: #{error_ptr[0].description}"
|
79
|
+
end
|
80
|
+
self
|
56
81
|
end
|
57
|
-
compatible
|
58
|
-
end
|
59
82
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
migmgr=NSMigrationManager.alloc.initWithSourceModel(old_model, destinationModel
|
75
|
-
mapping_model = NSMappingModel.inferredMappingModelForSourceModel(old_model, destinationModel
|
83
|
+
def compatible?
|
84
|
+
error_ptr = Pointer.new(:object)
|
85
|
+
metadata=NSPersistentStoreCoordinator.metadataForPersistentStoreOfType(NSSQLiteStoreType, URL:@store_url, error:error_ptr)
|
86
|
+
compatible=true
|
87
|
+
if metadata
|
88
|
+
compatible=@model.isConfiguration(nil, compatibleWithStoreMetadata:metadata)
|
89
|
+
end
|
90
|
+
compatible
|
91
|
+
end
|
92
|
+
|
93
|
+
def migrate_mom_model(_old_mom_url, _new_mom_url)
|
94
|
+
error_ptr = Pointer.new(:object)
|
95
|
+
new_model=NSManagedObjectModel.alloc.initWithContentsOfURL(_new_mom_url)
|
96
|
+
old_model=NSManagedObjectModel.alloc.initWithContentsOfURL(_old_mom_url)
|
97
|
+
migmgr=NSMigrationManager.alloc.initWithSourceModel(old_model, destinationModel:new_model)
|
98
|
+
mapping_model = NSMappingModel.inferredMappingModelForSourceModel(old_model, destinationModel:new_model, error:error_ptr)
|
76
99
|
new_store_url = NSURL.fileURLWithPath(@store_url.path+".new")
|
77
|
-
|
100
|
+
ret=migmgr.migrateStoreFromURL(@store_url, type:NSSQLiteStoreType, options:nil,
|
78
101
|
withMappingModel:mapping_model, toDestinationURL:new_store_url,
|
79
102
|
destinationType:NSSQLiteStoreType, destinationOptions:nil,
|
80
103
|
error:error_ptr)
|
104
|
+
NSLog "Migration result #{ret}."
|
81
105
|
NSFileManager.defaultManager.removeItemAtPath(@store_url.path, error:error_ptr)
|
82
|
-
#-TODO: evtl. move statt loeschen
|
83
106
|
NSFileManager.defaultManager.moveItemAtPath(new_store_url.path, toPath:@store_url.path, error:error_ptr)
|
84
|
-
break
|
85
107
|
end
|
86
|
-
end
|
87
108
|
|
88
|
-
|
109
|
+
def migrate_mom(_old_mom_files)
|
110
|
+
old_mom_files=_old_mom_files % '%03d'
|
111
|
+
error_ptr = Pointer.new(:object)
|
112
|
+
metadata=NSPersistentStoreCoordinator.metadataForPersistentStoreOfType(NSSQLiteStoreType, URL:@store_url, error:error_ptr)
|
113
|
+
return false unless metadata
|
114
|
+
db_version=-1
|
115
|
+
0.upto(999) do |version|
|
116
|
+
old_mom_file=old_mom_files % version
|
117
|
+
NSLog [:migrate, "old mom file", old_mom_file].inspect
|
118
|
+
old_mom_url = NSURL.fileURLWithPath("#{NSBundle.mainBundle.resourcePath}/#{old_mom_file}")
|
119
|
+
NSLog [:migrate, "old mom url", old_mom_url].inspect
|
120
|
+
old_model=NSManagedObjectModel.alloc.initWithContentsOfURL(old_mom_url)
|
121
|
+
compatible=old_model.isConfiguration(nil, compatibleWithStoreMetadata:metadata)
|
122
|
+
next unless compatible
|
123
|
+
db_version=version
|
124
|
+
NSLog "Found version #{version}"
|
125
|
+
break
|
126
|
+
end
|
127
|
+
db_version.upto(999) do |version|
|
128
|
+
old_mom_file=old_mom_files % version
|
129
|
+
old_mom_url = NSURL.fileURLWithPath("#{NSBundle.mainBundle.resourcePath}/#{old_mom_file}")
|
130
|
+
new_mom_file=old_mom_files % (version+1)
|
131
|
+
new_mom_file="#{NSBundle.mainBundle.resourcePath}/#{new_mom_file}"
|
132
|
+
last=false
|
133
|
+
unless NSFileManager.defaultManager.fileExistsAtPath(new_mom_file)
|
134
|
+
new_mom_file=_old_mom_files % "cur"
|
135
|
+
new_mom_file="#{NSBundle.mainBundle.resourcePath}/#{new_mom_file}"
|
136
|
+
last=true
|
137
|
+
end
|
138
|
+
NSLog [:migrate_from_to, old_mom_file, new_mom_file].inspect
|
139
|
+
migrate_mom_model(old_mom_url, NSURL.fileURLWithPath(new_mom_file))
|
140
|
+
break if last
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
def migrate_model(_old_model, _new_model)
|
145
|
+
error_ptr = Pointer.new(:object)
|
146
|
+
migmgr=NSMigrationManager.alloc.initWithSourceModel(_old_model, destinationModel:_new_model)
|
147
|
+
mapping_model = NSMappingModel.inferredMappingModelForSourceModel(_old_model, destinationModel:_new_model, error:error_ptr)
|
148
|
+
new_store_url = NSURL.fileURLWithPath(@store_url.path+".new")
|
149
|
+
ret=migmgr.migrateStoreFromURL(@store_url, type:NSSQLiteStoreType, options:nil,
|
150
|
+
withMappingModel:mapping_model, toDestinationURL:new_store_url,
|
151
|
+
destinationType:NSSQLiteStoreType, destinationOptions:nil,
|
152
|
+
error:error_ptr)
|
153
|
+
NSLog "Migration result #{ret}."
|
154
|
+
NSFileManager.defaultManager.removeItemAtPath(@store_url.path, error:error_ptr)
|
155
|
+
NSFileManager.defaultManager.moveItemAtPath(new_store_url.path, toPath:@store_url.path, error:error_ptr)
|
156
|
+
end
|
157
|
+
|
158
|
+
def migrate(_models)
|
159
|
+
error_ptr = Pointer.new(:object)
|
160
|
+
metadata=NSPersistentStoreCoordinator.metadataForPersistentStoreOfType(NSSQLiteStoreType, URL:@store_url, error:error_ptr)
|
161
|
+
return false unless metadata
|
162
|
+
found=false
|
163
|
+
old_model=nil
|
164
|
+
_models.each do |model|
|
165
|
+
unless found
|
166
|
+
found=model.isConfiguration(nil, compatibleWithStoreMetadata:metadata)
|
167
|
+
if found
|
168
|
+
old_model=model
|
169
|
+
NSLog "Found version #{model}"
|
170
|
+
end
|
171
|
+
else
|
172
|
+
migrate_model(old_mode, model)
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
end
|
89
178
|
end
|
90
179
|
|
@@ -0,0 +1,128 @@
|
|
1
|
+
module DbMotion
|
2
|
+
class Model
|
3
|
+
|
4
|
+
def self.get_version(_version)
|
5
|
+
@@models["#{sprintf "%03d", _version}"]
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.versions
|
9
|
+
@@models.keys.sort.map do |v|
|
10
|
+
@@models[v]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.version(_version, &block)
|
15
|
+
@@models||={}
|
16
|
+
e = Entities.new(&block)
|
17
|
+
model = NSManagedObjectModel.alloc.init
|
18
|
+
model.entities=e.entities.values
|
19
|
+
@@models["#{sprintf "%03d", _version}"]=model
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
class Entities
|
25
|
+
attr_reader :entities
|
26
|
+
|
27
|
+
def initialize(&block)
|
28
|
+
@entities = {}
|
29
|
+
block.call(self)
|
30
|
+
end
|
31
|
+
|
32
|
+
def entity(_name, &block)
|
33
|
+
e = Entity.new(&block)
|
34
|
+
entity = NSEntityDescription.alloc.init
|
35
|
+
entity.name = _name
|
36
|
+
entity.managedObjectClassName = _name
|
37
|
+
entity.properties = e.attributes.map do |type, name|
|
38
|
+
property = NSAttributeDescription.alloc.init
|
39
|
+
property.name = name
|
40
|
+
property.attributeType = type
|
41
|
+
property.optional = true
|
42
|
+
property
|
43
|
+
end
|
44
|
+
@entities[_name]=entity
|
45
|
+
end
|
46
|
+
|
47
|
+
def relation(_relation)
|
48
|
+
# Example "model1 <->> model2"
|
49
|
+
relation=_relation.split(/ /)
|
50
|
+
if relation.size!=3
|
51
|
+
puts "Unknown relation"
|
52
|
+
return
|
53
|
+
end
|
54
|
+
if (relation[1]=~/^<{1,2}\->{1,2}$/).nil?
|
55
|
+
puts "Unknown relation type"
|
56
|
+
return
|
57
|
+
end
|
58
|
+
model1_info=relation[0].split(/:/)
|
59
|
+
model2_info=relation[2].split(/:/)
|
60
|
+
unless @entities.has_key?(model1_info[0]) and @entities.has_key?(model2_info[0])
|
61
|
+
puts "Relation partner not found #{relation}"
|
62
|
+
return
|
63
|
+
end
|
64
|
+
|
65
|
+
model1 = NSRelationshipDescription.alloc.init
|
66
|
+
model2 = NSRelationshipDescription.alloc.init
|
67
|
+
|
68
|
+
model1.name = model1_info[1]
|
69
|
+
model1.destinationEntity = @entities[model2_info[0]]
|
70
|
+
model1.inverseRelationship = model2
|
71
|
+
model1.deleteRule = NSNullifyDeleteRule
|
72
|
+
if relation[1]=~/>>$/
|
73
|
+
# ->>
|
74
|
+
model1.minCount = 0
|
75
|
+
model1.maxCount = -1
|
76
|
+
else
|
77
|
+
# ->
|
78
|
+
model1.minCount = 1
|
79
|
+
model1.maxCount = 1
|
80
|
+
model1.optional = true
|
81
|
+
end
|
82
|
+
|
83
|
+
model2.name = model2_info[1]
|
84
|
+
model2.destinationEntity = @entities[model1_info[0]]
|
85
|
+
model2.inverseRelationship = model1
|
86
|
+
model2.deleteRule = NSNullifyDeleteRule
|
87
|
+
if relation[1]=~/^<</
|
88
|
+
# <<-
|
89
|
+
model2.minCount = 0
|
90
|
+
model2.maxCount = -1
|
91
|
+
else
|
92
|
+
# <-
|
93
|
+
model2.minCount = 1
|
94
|
+
model2.maxCount = 1
|
95
|
+
model2.optional = true
|
96
|
+
end
|
97
|
+
|
98
|
+
@entities[model1_info[0]].properties=@entities[model1_info[0]].properties+[model1]
|
99
|
+
@entities[model2_info[0]].properties=@entities[model2_info[0]].properties+[model2]
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
103
|
+
|
104
|
+
class Entity
|
105
|
+
attr_reader :attributes
|
106
|
+
|
107
|
+
MAPPING={
|
108
|
+
string: NSStringAttributeType,
|
109
|
+
integer: NSInteger16AttributeType,
|
110
|
+
double: NSDoubleAttributeType,
|
111
|
+
date: NSDateAttributeType,
|
112
|
+
bindata: NSBinaryDataAttributeType
|
113
|
+
}
|
114
|
+
|
115
|
+
def initialize(&block)
|
116
|
+
@attributes = []
|
117
|
+
block.call(self)
|
118
|
+
end
|
119
|
+
|
120
|
+
def method_missing(meth, *args, &block)
|
121
|
+
if MAPPING.has_key?(meth.to_sym)
|
122
|
+
@attributes << [MAPPING[meth.to_sym], args[0]]
|
123
|
+
else
|
124
|
+
puts "Key #{meth} not found"
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
data/lib/DbMotion/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: DbMotion
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-05-13 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: DbMotion a simple adaption of CoreData.
|
15
15
|
email:
|
@@ -26,6 +26,7 @@ files:
|
|
26
26
|
- README.md
|
27
27
|
- lib/DbMotion.rb
|
28
28
|
- lib/DbMotion/database.rb
|
29
|
+
- lib/DbMotion/model.rb
|
29
30
|
- lib/DbMotion/version.rb
|
30
31
|
homepage: https://github.com/rheoli/DbMotion
|
31
32
|
licenses: []
|