recordx_sqlite 0.2.1 → 0.2.2
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 +4 -4
 - checksums.yaml.gz.sig +0 -0
 - data/lib/recordx_sqlite.rb +45 -9
 - data.tar.gz.sig +0 -0
 - metadata +2 -2
 - metadata.gz.sig +0 -0
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA1:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: b39ece816738cb30475c51a1c909e6d28def0bb6
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: ca7f8cf2c3e34d4d211b63c2d33e2938737f276c
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: feb4feb620c5fdf74a688e193b4a46fcbacedde444a267d1b78dd20ea42c148491f75c4d095b7928d49b405c0b6c73b206554b39e601080d58cb00afcf7f0923
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: e94d739dcbc954dfdacbe9424c4c0216efef87a49614b4399540b52be3bb8603b958b0808a1381daf4c2415d1eb90304a4a105f14f55ee588e2af160c82e7c8b
         
     | 
    
        checksums.yaml.gz.sig
    CHANGED
    
    | 
         Binary file 
     | 
    
        data/lib/recordx_sqlite.rb
    CHANGED
    
    | 
         @@ -9,24 +9,40 @@ require 'recordx' 
     | 
|
| 
       9 
9 
     | 
    
         
             
            class RecordxSqlite
         
     | 
| 
       10 
10 
     | 
    
         | 
| 
       11 
11 
     | 
    
         
             
              def initialize(dbfile, table: '', primary_key: :id, pk: primary_key, 
         
     | 
| 
       12 
     | 
    
         
            -
                             sql:  
     | 
| 
      
 12 
     | 
    
         
            +
                             sql: nil)
         
     | 
| 
       13 
13 
     | 
    
         | 
| 
       14 
14 
     | 
    
         
             
                @db = SQLite3::Database.new dbfile
         
     | 
| 
       15 
15 
     | 
    
         | 
| 
       16 
16 
     | 
    
         
             
                @db.results_as_hash = true
         
     | 
| 
       17 
     | 
    
         
            -
                 
     | 
| 
      
 17 
     | 
    
         
            +
                
         
     | 
| 
      
 18 
     | 
    
         
            +
                if table.is_a? String then
         
     | 
| 
      
 19 
     | 
    
         
            +
                  
         
     | 
| 
      
 20 
     | 
    
         
            +
                  @table, @primary_key = table, pk.to_sym
         
     | 
| 
      
 21 
     | 
    
         
            +
                  
         
     | 
| 
      
 22 
     | 
    
         
            +
                elsif table.is_a? Hash 
         
     | 
| 
      
 23 
     | 
    
         
            +
                  
         
     | 
| 
      
 24 
     | 
    
         
            +
                  h = table
         
     | 
| 
      
 25 
     | 
    
         
            +
                  @table = h.keys.first
         
     | 
| 
      
 26 
     | 
    
         
            +
                  @primary_key = h[@table].keys.first
         
     | 
| 
      
 27 
     | 
    
         
            +
                  
         
     | 
| 
      
 28 
     | 
    
         
            +
                  create_table(@table, h[@table]) if @db.table_info(@table).empty?
         
     | 
| 
      
 29 
     | 
    
         
            +
                        
         
     | 
| 
      
 30 
     | 
    
         
            +
                end
         
     | 
| 
      
 31 
     | 
    
         
            +
                
         
     | 
| 
      
 32 
     | 
    
         
            +
                @sql =  sql || 'select * from ' + @table.to_s
         
     | 
| 
      
 33 
     | 
    
         
            +
                
         
     | 
| 
       18 
34 
     | 
    
         
             
                @a = nil
         
     | 
| 
       19 
35 
     | 
    
         | 
| 
       20 
36 
     | 
    
         
             
              end
         
     | 
| 
       21 
37 
     | 
    
         | 
| 
       22 
     | 
    
         
            -
              # note: when using method all() you will need to  
     | 
| 
      
 38 
     | 
    
         
            +
              # note: when using method all() you will need to call method refresh() 
         
     | 
| 
       23 
39 
     | 
    
         
             
              # first if a record had recently been added since the recordset was loaded
         
     | 
| 
       24 
40 
     | 
    
         
             
              #
         
     | 
| 
       25 
41 
     | 
    
         
             
              def all()    
         
     | 
| 
       26 
42 
     | 
    
         
             
                query(@sql) unless @a
         
     | 
| 
       27 
43 
     | 
    
         
             
                @a
         
     | 
| 
       28 
44 
     | 
    
         
             
              end
         
     | 
| 
       29 
     | 
    
         
            -
             
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
       30 
46 
     | 
    
         
             
              def create(h={})
         
     | 
| 
       31 
47 
     | 
    
         | 
| 
       32 
48 
     | 
    
         
             
                fields = h.keys
         
     | 
| 
         @@ -35,14 +51,16 @@ class RecordxSqlite 
     | 
|
| 
       35 
51 
     | 
    
         
             
                sql = "INSERT INTO #{@table} (#{fields.join(', ')})
         
     | 
| 
       36 
52 
     | 
    
         
             
                VALUES (#{(['?'] * fields.length).join(', ')})"
         
     | 
| 
       37 
53 
     | 
    
         | 
| 
       38 
     | 
    
         
            -
                @db.execute(sql, values) 
     | 
| 
      
 54 
     | 
    
         
            +
                @db.execute(sql, values)
         
     | 
| 
      
 55 
     | 
    
         
            +
                
         
     | 
| 
      
 56 
     | 
    
         
            +
                 :create
         
     | 
| 
       39 
57 
     | 
    
         
             
              end
         
     | 
| 
       40 
     | 
    
         
            -
             
     | 
| 
      
 58 
     | 
    
         
            +
             
     | 
| 
       41 
59 
     | 
    
         
             
              def find(id)
         
     | 
| 
       42 
60 
     | 
    
         
             
                query(@sql) unless @a
         
     | 
| 
       43 
61 
     | 
    
         
             
                @a.find {|x| x.method(@primary_key).call == id}
         
     | 
| 
       44 
62 
     | 
    
         
             
              end
         
     | 
| 
       45 
     | 
    
         
            -
             
     | 
| 
      
 63 
     | 
    
         
            +
             
     | 
| 
       46 
64 
     | 
    
         
             
              def query(sql=@sql)
         
     | 
| 
       47 
65 
     | 
    
         | 
| 
       48 
66 
     | 
    
         
             
                @sql = sql
         
     | 
| 
         @@ -54,7 +72,7 @@ class RecordxSqlite 
     | 
|
| 
       54 
72 
     | 
    
         
             
                end    
         
     | 
| 
       55 
73 
     | 
    
         | 
| 
       56 
74 
     | 
    
         
             
              end  
         
     | 
| 
       57 
     | 
    
         
            -
             
     | 
| 
      
 75 
     | 
    
         
            +
             
     | 
| 
       58 
76 
     | 
    
         
             
              def refresh()
         
     | 
| 
       59 
77 
     | 
    
         
             
                query(@sql)
         
     | 
| 
       60 
78 
     | 
    
         
             
                'refreshed'
         
     | 
| 
         @@ -73,6 +91,24 @@ WHERE #{@primary_key.to_s}='#{id}';" 
     | 
|
| 
       73 
91 
     | 
    
         
             
                @db.execute(s)
         
     | 
| 
       74 
92 
     | 
    
         | 
| 
       75 
93 
     | 
    
         
             
              end
         
     | 
| 
      
 94 
     | 
    
         
            +
             
     | 
| 
      
 95 
     | 
    
         
            +
              private
         
     | 
| 
      
 96 
     | 
    
         
            +
             
     | 
| 
      
 97 
     | 
    
         
            +
              def create_table(name, cols)
         
     | 
| 
      
 98 
     | 
    
         
            +
                
         
     | 
| 
      
 99 
     | 
    
         
            +
                fields = cols.map do |k,v|
         
     | 
| 
      
 100 
     | 
    
         
            +
             
     | 
| 
      
 101 
     | 
    
         
            +
                  types = { string: :text, integer: :int, float: :real,   date: :date }
         
     | 
| 
      
 102 
     | 
    
         
            +
                  type = types[v.class.to_s.downcase.to_sym].to_s.upcase
         
     | 
| 
      
 103 
     | 
    
         
            +
                  "%s %s" % [k.to_s, type]
         
     | 
| 
      
 104 
     | 
    
         
            +
             
     | 
| 
      
 105 
     | 
    
         
            +
                end
         
     | 
| 
      
 106 
     | 
    
         
            +
             
     | 
| 
      
 107 
     | 
    
         
            +
                sql = "CREATE TABLE %s (\n  %s PRIMARY KEY,\n  %s\n);" % 
         
     | 
| 
      
 108 
     | 
    
         
            +
                  [name, fields.first, fields[1..-1].join(",\n  ")]
         
     | 
| 
      
 109 
     | 
    
         
            +
             
     | 
| 
      
 110 
     | 
    
         
            +
                @db.execute sql
         
     | 
| 
       76 
111 
     | 
    
         | 
| 
       77 
     | 
    
         
            -
            end
         
     | 
| 
      
 112 
     | 
    
         
            +
              end  
         
     | 
| 
       78 
113 
     | 
    
         | 
| 
      
 114 
     | 
    
         
            +
            end
         
     | 
    
        data.tar.gz.sig
    CHANGED
    
    | 
         Binary file 
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: recordx_sqlite
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0.2. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.2.2
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - James Robertson
         
     | 
| 
         @@ -31,7 +31,7 @@ cert_chain: 
     | 
|
| 
       31 
31 
     | 
    
         
             
              pewreBQ0QCPuPbV1FPlyX5N5qsGIMHFIYqUlaykOA8156Y6fbIbqQsLwxaSr8WkD
         
     | 
| 
       32 
32 
     | 
    
         
             
              eojajR/fTTvliw==
         
     | 
| 
       33 
33 
     | 
    
         
             
              -----END CERTIFICATE-----
         
     | 
| 
       34 
     | 
    
         
            -
            date: 2017-06- 
     | 
| 
      
 34 
     | 
    
         
            +
            date: 2017-06-23 00:00:00.000000000 Z
         
     | 
| 
       35 
35 
     | 
    
         
             
            dependencies:
         
     | 
| 
       36 
36 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       37 
37 
     | 
    
         
             
              name: sqlite3
         
     | 
    
        metadata.gz.sig
    CHANGED
    
    | 
         Binary file 
     |