swiss_db 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 99eb5c4d6a5ad8d2d1c99c2f2d21a1463a44724d
4
+ data.tar.gz: 222d5d2690e5a218bf06cfb0980c8f2af5b4a8dd
5
+ SHA512:
6
+ metadata.gz: 2576d27346e62729dc4f77a2c48284fc9bc0b1c6ebdc7f25eb32542f7a1137655abb487b66f66d00b50bb29b69f41dc5bd0d169bcd36a50ceb6bfd4104398a45
7
+ data.tar.gz: 39a86d8fbc8bf6e84fa1651b68c728906e28f228e18e08bc7d885dd22e84a06d9cefbc391d8102d08af15ddd08642daf6d4f9eade043a9fa3e98afe972fd7956
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # SwissDb
2
+
3
+ RubyMotion Android ActiveRecord-like ORM for SQLite
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'swiss_db'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install swiss_db
20
+
21
+ ## Usage
22
+
23
+ Schemas are the exact same from CoreDataQuery and go in the same place.
24
+
25
+ Models are as such:
26
+
27
+ ```ruby
28
+ class Model < SwissModel
29
+
30
+ set_table_name "model"
31
+ set_primary_key "primary_key_name"
32
+
33
+ end
34
+ ```
35
+
36
+ That's it! #all, #last, #first, #count, #save, #update_attributes and the usual are now available!
37
+
38
+ ## Development
39
+
40
+ After checking out the repo, run `bin/setup` to install dependencies.
41
+
42
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
43
+
44
+ ## Contributing
45
+
46
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jsilverMDX/swiss_db.
47
+
48
+
49
+ ## License
50
+
51
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
52
+
@@ -0,0 +1,75 @@
1
+ # SwissDB Cursor
2
+ # Helps move around a result set
3
+ # Convenience methods over the standard cursor
4
+ # Used by Swiss DataStore
5
+
6
+ class Cursor
7
+
8
+ FIELD_TYPE_BLOB = 4
9
+ FIELD_TYPE_FLOAT = 2
10
+ FIELD_TYPE_INTEGER = 1
11
+ FIELD_TYPE_NULL = 0
12
+ FIELD_TYPE_STRING = 3
13
+
14
+ attr_accessor :cursor, :model
15
+
16
+ def initialize(cursor, model)
17
+ @cursor = cursor
18
+ @model = model
19
+ @values = {}
20
+ end
21
+
22
+ def first
23
+ cursor.moveToFirst ? self : nil
24
+ end
25
+
26
+ def last
27
+ cursor.moveToLast ? self : nil
28
+ end
29
+
30
+ def [](pos)
31
+ cursor.moveToPosition(pos) ? self : nil
32
+ end
33
+
34
+ def count
35
+ cursor.getCount
36
+ end
37
+
38
+ def method_missing(methId, *args)
39
+ str = methId.id2name
40
+ if args.count == 0
41
+ index = cursor.getColumnIndex(str)
42
+ type = cursor.getType(index)
43
+ puts "getting field #{str} at index #{index} of type #{type}"
44
+ if type == FIELD_TYPE_STRING
45
+ cursor.getString(index)
46
+ elsif type == FIELD_TYPE_INTEGER
47
+ cursor.getInt(index)
48
+ elsif type == FIELD_TYPE_NULL
49
+ nil #??
50
+ elsif type == FIELD_TYPE_FLOAT
51
+ cursor.getFloat(index)
52
+ elsif type == FIELD_TYPE_BLOB
53
+ cursor.getBlob(index)
54
+ end
55
+ elsif args.count == 1
56
+ # assignment... add to values to save
57
+ @values[str.gsub!("=", "")] = args[0]
58
+ end
59
+ end
60
+
61
+ def save
62
+ primary_key = model.primary_key
63
+ pk_value = self.send(primary_key.to_sym)
64
+ model.store.update(model.table_name, @values, {primary_key => pk_value})
65
+ end
66
+
67
+ # we are updating an existing row.. makes more sense on cursor...
68
+ def update_attribute(key, value)
69
+ primary_key = model.primary_key
70
+ pk_value = self.send(primary_key.to_sym)
71
+ model.store.update(model.table_name, {key => value}, {primary_key => pk_value})
72
+ end
73
+
74
+
75
+ end
@@ -0,0 +1,8 @@
1
+
2
+ private static final java.lang.String DB_NAME = "swissdb";
3
+ private static final int VERSION = 1;
4
+
5
+ public DataStore(android.content.Context context){
6
+ super(context, DB_NAME, null, VERSION);
7
+ }
8
+
@@ -0,0 +1,96 @@
1
+ # main connection point
2
+ # creates and upgrades our database for us
3
+ # and provides low level SQL features
4
+
5
+ class DataStore < Android::Database::SQLite::SQLiteOpenHelper
6
+
7
+ DATABASE_NAME = "swissdb"
8
+ DATABASE_VERSION = 1
9
+ ContentValues = Android::Content::ContentValues
10
+
11
+ def writable_db
12
+ getWritableDatabase
13
+ end
14
+
15
+ def drop_db
16
+ $app_context.deleteDatabase(DATABASE_NAME)
17
+ end
18
+
19
+ def onUpgrade(db, oldVersion, newVersion)
20
+ # maybe drop if needed...
21
+ db.execSQL("DROP *")
22
+ onCreate(db)
23
+ end
24
+
25
+ #create
26
+ def onCreate(db)
27
+ puts "table creation... running schema"
28
+ # THIS RELIES ON SCHEMA CODE TO SUCCEED
29
+ # NOTE: I don't know a better way of passing the schema here
30
+ # If you do just change it. For now this works.
31
+ # Thanks.
32
+ $current_schema.each do |k, v|
33
+ create_table db, k, v
34
+ end
35
+ # database.execSQL("CREATE TABLE credentials(username TEXT, password TEXT)")
36
+ end
37
+
38
+ #insert
39
+ def insert(db=writable_db, table, hash_values)
40
+ puts "inserting data in #{table}"
41
+ values = ContentValues.new(hash_values.count)
42
+ hash_values.each do |k, v|
43
+ values.put(k, v)
44
+ end
45
+ result = db.insert(table, nil, values)
46
+ result
47
+ end
48
+ #retrieve
49
+ def select_all(db=writable_db, table, model)
50
+ puts "selecting data from #{table}"
51
+ cursor = db.rawQuery("select * from '#{table}'", nil)
52
+ Cursor.new(cursor, model) # we wrap their cursor
53
+ end
54
+
55
+ def select(db=writable_db, table, values, model)
56
+ puts "selecting data from #{table}"
57
+ value_str = values.map do |k, v|
58
+ "#{k} = '#{v}'"
59
+ end.join(" AND ")
60
+ sql = "select * from '#{table}' where #{value_str}"
61
+ puts sql
62
+ cursor = db.rawQuery(sql, nil)
63
+ Cursor.new(cursor, model) # we wrap their cursor
64
+ end
65
+
66
+ # update
67
+
68
+ def update(db=writable_db, table, values, where_values)
69
+ value_str = values.map do |k, v|
70
+ "'#{k}' = '#{v}'"
71
+ end.join(",")
72
+ where_str = where_values.map do |k, v|
73
+ "#{k} = '#{v}'"
74
+ end.join(",")
75
+ sql = "update '#{table}' set #{value_str} where #{where_str}"
76
+ puts sql
77
+ db.execSQL sql
78
+ end
79
+
80
+ #deleting all records
81
+
82
+ def destroy_all(db=writable_db, table) # WARNING!
83
+ puts "destroying all from #{table}"
84
+ db.delete(table, nil, nil)
85
+ end
86
+
87
+ # create table
88
+ def create_table(db=writable_db, table_name, fields)
89
+ fields_string = fields.map { |k, v| "#{k} #{v}" }.join(',')
90
+ sql = "CREATE TABLE #{table_name}(#{fields_string})"
91
+ puts sql
92
+ db.execSQL sql
93
+ end
94
+
95
+
96
+ end
@@ -0,0 +1,61 @@
1
+ # "Swiss", RubyMotion Android SQLite by VirtualQ
2
+
3
+
4
+ # schema loader stuff
5
+ # i know it's rough but it works
6
+
7
+ class Object
8
+
9
+ attr_accessor :current_schema
10
+
11
+ # convenience methods
12
+
13
+ def log(tag, str)
14
+ Android::Util::Log.d(tag, str)
15
+ end
16
+
17
+ def puts(str)
18
+ log "general", str
19
+ end
20
+
21
+ def current_schema
22
+ @current_schema
23
+ end
24
+
25
+ def schema(schema_name, &block)
26
+ puts "running schema for #{schema_name}"
27
+ @current_schema = {}
28
+ @database_name = schema_name
29
+ block.call
30
+ puts @current_schema.inspect
31
+ end
32
+
33
+ def entity(table_name, &block)
34
+ puts "adding entity #{table_name} to schema"
35
+ @table_name = table_name
36
+ @current_schema[@table_name] = {}
37
+ block.call
38
+ $current_schema = @current_schema # there was no other way. I couldn't get context to create the model here.
39
+ end
40
+
41
+ def add_column(name, type)
42
+ @current_schema[@table_name][name] = type
43
+ end
44
+
45
+ def boolean(column_name)
46
+ add_column column_name.to_s, "BOOLEAN"
47
+ end
48
+
49
+ def string(column_name)
50
+ add_column column_name.to_s, "VARCHAR"
51
+ end
52
+
53
+ def integer32(column_name)
54
+ add_column column_name.to_s, "INTEGER"
55
+ end
56
+
57
+ def double(column_name)
58
+ add_column column_name.to_s, "DOUBLE"
59
+ end
60
+
61
+ end
@@ -0,0 +1,80 @@
1
+ # Swiss Model
2
+ # An ActiveRecord like Model for RubyMotion Android
3
+
4
+ class SwissModel
5
+
6
+ # meh? .. won't work for now in java... created classes become java packages
7
+ # name will become the namespace of the package...
8
+ # def self.inherited(subclass)
9
+ # puts "New subclass: #{subclass.class.name.to_s}"
10
+ # end
11
+
12
+ # attr_accessor :table_name
13
+
14
+ def self.store
15
+ @@store ||= DataStore.new($app_context)
16
+ @@store
17
+ end
18
+
19
+ def self.set_table_name(table_name)
20
+ @@table_name = table_name
21
+ end
22
+
23
+ def self.table_name
24
+ @@table_name
25
+ end
26
+
27
+ def self.set_primary_key(primary_key)
28
+ @@primary_key = primary_key
29
+ end
30
+
31
+ def self.primary_key
32
+ @@primary_key
33
+ end
34
+
35
+ def self.all
36
+ # select_all
37
+ cursor = store.select_all(@@table_name, self)
38
+ cursor
39
+ end
40
+
41
+ def self.where(values)
42
+ # select <table> where <field> = <value>
43
+ cursor = store.select(@@table_name, values, self)
44
+ cursor
45
+ end
46
+
47
+ def self.first
48
+ # select all and get first
49
+ cursor = all.first
50
+ cursor
51
+ end
52
+
53
+ def self.last
54
+ # select all and get last
55
+ cursor = all.last
56
+ cursor
57
+ end
58
+
59
+ def self.create(obj)
60
+ # create a row
61
+ result = store.insert(@@table_name, obj)
62
+ if result == -1
63
+ puts "An error occured inserting values into #{@@table_name}"
64
+ else
65
+ return result
66
+ end
67
+ end
68
+
69
+ # def destroy
70
+ # # destroy this row
71
+ # end
72
+
73
+ def self.destroy_all!
74
+ # destroy all of this kind (empty table)
75
+ store.destroy_all(@@table_name)
76
+ end
77
+
78
+ # something for method missing that gets class and then returns it from the cursor
79
+
80
+ end
data/lib/swiss_db.rb ADDED
@@ -0,0 +1,23 @@
1
+ # -*- coding: utf-8 -*-
2
+ # SwissDB by jsilverMDX
3
+ if defined?(Motion::Project::Config)
4
+ lib_dir_path = File.dirname(File.expand_path(__FILE__))
5
+ Motion::Project::App.setup do |app|
6
+ # unless platform_name == "android"
7
+ # raise "Sorry, the platform #{platform_name} is not supported by SwissDB"
8
+ # end
9
+
10
+ # scans app.files until it finds app/ (the default)
11
+ # if found, it inserts just before those files, otherwise it will insert to
12
+ # the end of the list
13
+ insert_point = app.files.find_index { |file| file =~ /^(?:\.\/)?app\// } || 0
14
+
15
+ Dir.glob(File.join(lib_dir_path, "**/*.rb")).reverse.each do |file|
16
+ app.files.insert(insert_point, file)
17
+ end
18
+
19
+ # puts "APP FILES: #{app.files.inspect}"
20
+
21
+ end
22
+ end
23
+
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: swiss_db
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.3
5
+ platform: ruby
6
+ authors:
7
+ - Jonathan Silverman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Emulates ActiveRecord for SQLite Android
42
+ email:
43
+ - jsilverman2@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - README.md
49
+ - lib/swiss_db.rb
50
+ - lib/swiss_db/cursor.rb
51
+ - lib/swiss_db/data_store.java
52
+ - lib/swiss_db/data_store.rb
53
+ - lib/swiss_db/db.rb
54
+ - lib/swiss_db/swiss_model.rb
55
+ homepage: http://github.com/jsilverMDX
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.4.3
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: Android ActiveRecord ORM for RubyMotion
79
+ test_files: []