recordx_sqlite 0.2.5 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 608b01a6d789ee7139213da5a65e9bd1160de9f5
4
- data.tar.gz: a58cf47dce501b6cac28b030704832d339f3c867
2
+ SHA256:
3
+ metadata.gz: 3db5139cd9c0e124459ca4a5f388ae556e6d7e07be90b5f6ed9c0c4e29e45005
4
+ data.tar.gz: 3d2966b40f159c7a07133ced693673540e0600f3a6da6cb5de8d0db33c6288cf
5
5
  SHA512:
6
- metadata.gz: b29579a30c0666b295b0c3de86331a4fc5b4f576ead42ac103c516976d565d13109c833110a32d743a528b8cc772844c427bfdc96e819a002ab916794c26b19f
7
- data.tar.gz: 4209e323bfe6d09b5f86b0ae09bcec5f1fbdd0ed7f823d4f77a95db7732919487af23775fd333889db21c1a14b6ebe379f38a361b544ac34092f63537dd30e0e
6
+ metadata.gz: 9509b33e7e585b1103c2fcd60971ac528d403a4c99cb20faab6d91a98b0a8f066ffec030af063e642dcd387841f9619e162d71a74b031b68349af54b711fa1e8
7
+ data.tar.gz: 20bc3cecc3f045b40db81d7e6b8f3008ec0b21a12418474466f1ed995bf0b37f0499815be62a47c0dd1c1fdf538122b1eb11ce9e1a5769ea44e382cb3b034eb3
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -4,14 +4,17 @@
4
4
 
5
5
  require 'sqlite3'
6
6
  require 'recordx'
7
+ require 'drb_sqlite'
7
8
 
8
9
 
9
10
  class RecordxSqlite
10
11
 
11
- def initialize(dbfile, table: '', primary_key: :id, pk: primary_key,
12
- sql: nil)
13
-
14
- @db = SQLite3::Database.new dbfile
12
+ def initialize(dbfile, table: nil, primary_key: :id, pk: primary_key,
13
+ sql: nil, pagesize: 10, debug: false)
14
+
15
+ @debug = debug
16
+ sqlite = dbfile =~ /^sqlite:\/\// ? DRbSQLite : SQLite3::Database
17
+ @db = sqlite.new dbfile
15
18
 
16
19
  @db.results_as_hash = true
17
20
 
@@ -26,19 +29,56 @@ class RecordxSqlite
26
29
  @primary_key = h[@table].keys.first
27
30
 
28
31
  create_table(@table, h[@table]) if @db.table_info(@table).empty?
32
+
33
+ else
34
+
35
+ if @db.respond_to? :tables then
36
+ @table = @db.tables.first
37
+ @primary_key = @db.fields(@table).first
38
+ else
39
+
40
+ sql = "SELECT name FROM sqlite_master WHERE type='table';"
41
+ r = @db.execute(sql)
42
+ puts 'r: ' + r.inspect if @debug
43
+
44
+ a = r.map {|x| x['name']}
45
+ puts 'a: ' + a.inspect if @debug
46
+
47
+ sys = a.grep /^sqlite_/
48
+ @table = (a - sys).first
49
+ puts '@table: ' + @table.inspect if @debug
50
+
51
+ @primary_key = @db.table_info(@table).map {|x| x['name'].to_sym }.first
52
+ puts '@primary_key: ' + @primary_key.inspect if @debug
53
+
54
+ end
29
55
 
30
56
  end
31
57
 
32
- @sql = sql || 'select * from ' + @table.to_s
33
-
58
+ @sql = sql = 'select * from ' + @table.to_s
59
+ @default_sql = @sql
60
+ @pagesize = pagesize
34
61
  @a = nil
35
62
 
63
+ # create the fhe find_by methods for each fields
64
+ @db.table_info(@table).each do |cols|
65
+ #puts 'cols: ' + cols.inspect
66
+ define_singleton_method ('find_by_' + cols['name']).to_sym do |val|
67
+ find_by(cols['name'], val)
68
+ end
69
+
70
+ define_singleton_method ('find_all_by_' + cols['name']).to_sym do |val|
71
+ find_all_by(cols['name'], val)
72
+ end
73
+ end
74
+
36
75
  end
37
76
 
38
77
  # note: when using method all() you will need to call method refresh()
39
78
  # first if a record had recently been added since the recordset was loaded
40
79
  #
41
80
  def all()
81
+ @sql = @default_sql
42
82
  query(@sql) unless @a
43
83
  @a
44
84
  end
@@ -71,15 +111,52 @@ class RecordxSqlite
71
111
 
72
112
  end
73
113
 
114
+ def find_by(field, val)
115
+
116
+ query(@sql) unless @a
117
+ @a.find {|x| x.method(field).call == val}
118
+
119
+ end
120
+
121
+ def find_all_by(field, val)
122
+
123
+ query(@sql) unless @a
124
+ @a.select {|x| x.method(field).call == val}
125
+
126
+ end
127
+
74
128
  # returns the 1st n rows
75
129
  #
76
130
  def first(n=1)
77
131
  query(@sql + ' LIMIT ' + n.to_s, cache: false)
78
132
  end
79
-
80
- def query(sql=@sql, cache: true)
133
+
134
+ def order(dir=:asc)
135
+ @sql += " ORDER BY #{@primary_key} #{dir.to_s.upcase}"
136
+ self
137
+ end
138
+
139
+ def page(n=1)
81
140
 
82
- rs = @db.query sql
141
+ query(@sql + " ORDER BY %s DESC LIMIT %s OFFSET %s" %
142
+ [@primary_key, @pagesize, @pagesize*(n-1)], cache: false)
143
+
144
+ end
145
+
146
+ def query(sql=@sql, cache: true, heading: true)
147
+
148
+ if heading then
149
+
150
+ rs = @db.query sql
151
+
152
+ else
153
+
154
+ @db.results_as_hash = false
155
+ rs = @db.query(sql).to_a
156
+ @db.results_as_hash = true
157
+
158
+ return rs
159
+ end
83
160
 
84
161
  a = rs.map do |h|
85
162
  h2 = h.inject({}) {|r, x| k, v = x; r.merge(k.to_sym => v)}
@@ -130,4 +207,4 @@ WHERE #{@primary_key.to_s}='#{id}';"
130
207
 
131
208
  end
132
209
 
133
- end
210
+ end
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.5
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -10,28 +10,32 @@ bindir: bin
10
10
  cert_chain:
11
11
  - |
12
12
  -----BEGIN CERTIFICATE-----
13
- MIIDljCCAn6gAwIBAgIBATANBgkqhkiG9w0BAQUFADBIMRIwEAYDVQQDDAlnZW1t
14
- YXN0ZXIxHjAcBgoJkiaJk/IsZAEZFg5qYW1lc3JvYmVydHNvbjESMBAGCgmSJomT
15
- 8ixkARkWAmV1MB4XDTE2MDcxNjE5MzI1NFoXDTE3MDcxNjE5MzI1NFow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ydHNvbi5ldTAN
28
+ BgkqhkiG9w0BAQsFAAOCAYEAj65GR5UHe6vDPnTp38ivVIZskSlTCafWvO+oMI1L
29
+ UO9sLOjS3XDon+VSafMQaWtlf/f0CLHjjImuj/UUMqBU+XxGIQTjw01kCuErBIYH
30
+ KNPAv7P2Jm7ZOMNubvFTjPE6KvoslFbXEs0FPTf+MjiP/J1F6pHx4rcnxWoQ2qSq
31
+ uK7Zi8j3bRGDUGdW8c1btU8bUe3XsdfIwYRUvDm3E9fiHThH4Ta3uPLfeN6TCAje
32
+ qz2w2H+8jTC9zGdKh31dK9GUKneJiJ6EW8G27E9QWlZ0jezMLrjqzekSPGYREJBT
33
+ 45cJ79L4CUPnD+9WR75jGp2G3em8e/4nUlh29GvXOHi5SzpjuqEgLRRHLMG19mrg
34
+ Uy13/a2YkENgSlY9Q9bZW7jpb1CwVscJ4n+HLVb0fj8HKwsceJzeYf9JXHzJ2ykt
35
+ 14GYRdzYfQotOw5CFtPk6K1OgKW7WoEyiUBKdJanTGfXkAOum4dHWdWD7mm0RXx/
36
+ VfP9/tWv/9cP4k9V2nUJNEmu
33
37
  -----END CERTIFICATE-----
34
- date: 2017-07-02 00:00:00.000000000 Z
38
+ date: 2021-02-27 00:00:00.000000000 Z
35
39
  dependencies:
36
40
  - !ruby/object:Gem::Dependency
37
41
  name: sqlite3
@@ -39,40 +43,60 @@ dependencies:
39
43
  requirements:
40
44
  - - "~>"
41
45
  - !ruby/object:Gem::Version
42
- version: '1.3'
46
+ version: '1.4'
43
47
  - - ">="
44
48
  - !ruby/object:Gem::Version
45
- version: 1.3.11
49
+ version: 1.4.1
46
50
  type: :runtime
47
51
  prerelease: false
48
52
  version_requirements: !ruby/object:Gem::Requirement
49
53
  requirements:
50
54
  - - "~>"
51
55
  - !ruby/object:Gem::Version
52
- version: '1.3'
56
+ version: '1.4'
53
57
  - - ">="
54
58
  - !ruby/object:Gem::Version
55
- version: 1.3.11
59
+ version: 1.4.1
56
60
  - !ruby/object:Gem::Dependency
57
61
  name: recordx
58
62
  requirement: !ruby/object:Gem::Requirement
59
63
  requirements:
60
64
  - - "~>"
61
65
  - !ruby/object:Gem::Version
62
- version: '0.4'
66
+ version: '0.5'
63
67
  - - ">="
64
68
  - !ruby/object:Gem::Version
65
- version: 0.4.0
69
+ version: 0.5.4
66
70
  type: :runtime
67
71
  prerelease: false
68
72
  version_requirements: !ruby/object:Gem::Requirement
69
73
  requirements:
70
74
  - - "~>"
71
75
  - !ruby/object:Gem::Version
72
- version: '0.4'
76
+ version: '0.5'
73
77
  - - ">="
74
78
  - !ruby/object:Gem::Version
75
- version: 0.4.0
79
+ version: 0.5.4
80
+ - !ruby/object:Gem::Dependency
81
+ name: drb_sqlite
82
+ requirement: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - "~>"
85
+ - !ruby/object:Gem::Version
86
+ version: '0.3'
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 0.3.3
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.3'
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: 0.3.3
76
100
  description:
77
101
  email: james@jamesrobertson.eu
78
102
  executables: []
@@ -100,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
124
  version: '0'
101
125
  requirements: []
102
126
  rubyforge_project:
103
- rubygems_version: 2.6.8
127
+ rubygems_version: 2.7.10
104
128
  signing_key:
105
129
  specification_version: 4
106
130
  summary: RecordX-SQLite is an object relational mapper primarily designed for updating
metadata.gz.sig CHANGED
Binary file