recordx_sqlite 0.2.1 → 0.2.2

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
2
  SHA1:
3
- metadata.gz: 0d43fa3a3d3ec544a14841f54971aa63226b0c34
4
- data.tar.gz: 994fa803b3ada9d6d4da3702363d75b5d6f90b55
3
+ metadata.gz: b39ece816738cb30475c51a1c909e6d28def0bb6
4
+ data.tar.gz: ca7f8cf2c3e34d4d211b63c2d33e2938737f276c
5
5
  SHA512:
6
- metadata.gz: 07ac1b311e412d966064286d56e39bdcf95813be9cba0c3528f39c2e60572bdb25faffbb76708dd367a9598ef12a96495a21c6ab79a1803800e4c488a8ec2aef
7
- data.tar.gz: 132384723b69a4ad2cb1b5b863c234455daef4d2845f8a658c2479dfef3cee2f697b2cd77b2a307187d5b9b8dbf0472045eb67bf0fa78b511e4890255d42b058
6
+ metadata.gz: feb4feb620c5fdf74a688e193b4a46fcbacedde444a267d1b78dd20ea42c148491f75c4d095b7928d49b405c0b6c73b206554b39e601080d58cb00afcf7f0923
7
+ data.tar.gz: e94d739dcbc954dfdacbe9424c4c0216efef87a49614b4399540b52be3bb8603b958b0808a1381daf4c2415d1eb90304a4a105f14f55ee588e2af160c82e7c8b
checksums.yaml.gz.sig CHANGED
Binary file
@@ -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: 'select * from ' + table)
12
+ sql: nil)
13
13
 
14
14
  @db = SQLite3::Database.new dbfile
15
15
 
16
16
  @db.results_as_hash = true
17
- @table, @primary_key, @sql = table, pk.to_sym, sql
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 execute method refresh()
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.1
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-22 00:00:00.000000000 Z
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