sack 1.2.2 → 1.2.4
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
- data/lib/sack/connectors/mysql.rb +45 -0
- data/lib/sack/connectors/sqlite3.rb +1 -2
- data/lib/sack/database.rb +1 -2
- data/lib/sack/database/data.rb +17 -16
- data/lib/sack/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 90bcc0a090fb37382416f80c5e3a617b38ff1498
|
4
|
+
data.tar.gz: 6f84ea73b3ad0237ab2a64ebd172aa424e712d5f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4379c6f945742fde6e37a7c1ec0ceb6815e8696f43ddc35f80c63cdd7ede14b323d986a2d0f46cd5869bdf84c82b5cf274158f0ff87ce9a5866694790e877786
|
7
|
+
data.tar.gz: d71837eef03ece759e12af73f241cb0850d537aed3314e8e5c8e60de9dfbb548dd87a87526c7e72f039813965fa38912d42d7c7e29a1720d2ab5fb13e6ef6d2a
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# Sack
|
2
|
+
# by Eresse <eresse@eresse.net>
|
3
|
+
|
4
|
+
# External Includes
|
5
|
+
require 'mysql2'
|
6
|
+
|
7
|
+
# Internal Includes
|
8
|
+
require 'sack/database/sanitizer'
|
9
|
+
|
10
|
+
# Sack Module
|
11
|
+
module Sack
|
12
|
+
|
13
|
+
# Connectors Module
|
14
|
+
module Connectors
|
15
|
+
|
16
|
+
# MySQL Connector Module:
|
17
|
+
# Provides MySQL connectivity for Sack Database.
|
18
|
+
module MySQLConnector
|
19
|
+
|
20
|
+
# Open:
|
21
|
+
# Opens a connection to a MySQL database.
|
22
|
+
# @param [String] conn_string The connection string (host=myServerAddress;database=myDataBase;username=myUsername;password=myPassword;)
|
23
|
+
# @return [Object] Database connection
|
24
|
+
def self.open conn_string
|
25
|
+
Mysql2::Client.new Hash[*(conn_string.split(';').inject([]) { |a, e| a + e.split('=') })]
|
26
|
+
end
|
27
|
+
|
28
|
+
# Close:
|
29
|
+
# Closes a previously-opened database connection.
|
30
|
+
# @param [Object] dbc Database connection
|
31
|
+
def self.close dbc
|
32
|
+
dbc.close
|
33
|
+
end
|
34
|
+
|
35
|
+
# Execute
|
36
|
+
# Executes an SQL statement with parameters
|
37
|
+
# @param [Object] dbc Database connection
|
38
|
+
# @param [String] q Statement
|
39
|
+
# @return [Array] Statement results
|
40
|
+
def self.exec dbc, q
|
41
|
+
dbc.query(q).rows
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -36,10 +36,9 @@ module Sack
|
|
36
36
|
# Executes an SQL statement with parameters
|
37
37
|
# @param [Object] dbc Database connection
|
38
38
|
# @param [String] q Statement
|
39
|
-
# @param [Array] params Statement parameters
|
40
39
|
# @return [Array] Statement results
|
41
40
|
def self.exec dbc, q
|
42
|
-
dbc.
|
41
|
+
dbc.execute q
|
43
42
|
end
|
44
43
|
end
|
45
44
|
end
|
data/lib/sack/database.rb
CHANGED
@@ -3,7 +3,6 @@
|
|
3
3
|
|
4
4
|
# External Includes
|
5
5
|
require 'thread'
|
6
|
-
require 'sqlite3'
|
7
6
|
|
8
7
|
# Internal Includes
|
9
8
|
require 'sack/database/data'
|
@@ -65,7 +64,7 @@ module Sack
|
|
65
64
|
db = @connector.open @connstring
|
66
65
|
|
67
66
|
# Yield Block
|
68
|
-
yield Data.new(db, @schema) if block_given?
|
67
|
+
yield Data.new(@connector, db, @schema) if block_given?
|
69
68
|
|
70
69
|
# Close Database
|
71
70
|
db.close
|
data/lib/sack/database/data.rb
CHANGED
@@ -22,9 +22,10 @@ module Sack
|
|
22
22
|
|
23
23
|
# Construct:
|
24
24
|
# Builds a *Data* object around a _db_ instance, set to operate on _schema_.
|
25
|
-
# @param [
|
25
|
+
# @param [Object] db Database instance obtained by DBMS file
|
26
26
|
# @param [Hash] schema Schema definition - see README
|
27
|
-
def initialize db, schema
|
27
|
+
def initialize connector, db, schema
|
28
|
+
@connector = connector
|
28
29
|
@db = db
|
29
30
|
@schema = schema
|
30
31
|
end
|
@@ -35,7 +36,7 @@ module Sack
|
|
35
36
|
# @param [Hash] fields A hash mapping of field names to type definition arrays (from _FTYPES_)
|
36
37
|
def create_table name, fields
|
37
38
|
fq = fields.collect { |fname, ftype| "#{Sanitizer.field_name fname} #{ftype.respond_to?(:each) ? ftype.collect { |e| FTYPES[Sanitizer.ftype e] }.join(' ') : FTYPES[Sanitizer.ftype ftype]}" }.join ', '
|
38
|
-
@
|
39
|
+
@connector.exec @db, "create table #{Sanitizer.table @schema, name} (#{fq});"
|
39
40
|
end
|
40
41
|
|
41
42
|
# Alter Table:
|
@@ -43,7 +44,7 @@ module Sack
|
|
43
44
|
# @param [Symbol] name Table name
|
44
45
|
# @param [Hash] fields A hash mapping of field names to type definition arrays (from _FTYPES_)
|
45
46
|
def alter_table name, fields
|
46
|
-
fields.each { |fname, ftype| @
|
47
|
+
fields.each { |fname, ftype| @connector.exec @db, "alter table #{Sanitizer.table @schema, name} add #{Sanitizer.field_name fname} #{ftype.respond_to?(:each) ? ftype.collect { |e| FTYPES[Sanitizer.ftype e] }.join(' ') : FTYPES[Sanitizer.ftype ftype]};" rescue nil }
|
47
48
|
end
|
48
49
|
|
49
50
|
# Count:
|
@@ -51,7 +52,7 @@ module Sack
|
|
51
52
|
# @param [Symbol] table Table name
|
52
53
|
# @return [Fixnum] The number of rows present in the given _table_
|
53
54
|
def count table
|
54
|
-
@
|
55
|
+
@connector.exec(@db, "select count(*) from #{Sanitizer.table @schema, table};")[0][0]
|
55
56
|
end
|
56
57
|
|
57
58
|
# Find:
|
@@ -72,7 +73,7 @@ module Sack
|
|
72
73
|
# @param [Object] id ID on which to filter
|
73
74
|
# @return [Array] An array of Hashes, each representing one row
|
74
75
|
def fetch table, id
|
75
|
-
hash_res(table.to_sym, @
|
76
|
+
hash_res(table.to_sym, @connector.exec(@db, Statement.prep("select * from #{Sanitizer.table @schema, table} where id = ?;", [id])))
|
76
77
|
end
|
77
78
|
|
78
79
|
# Fetch By Field:
|
@@ -82,7 +83,7 @@ module Sack
|
|
82
83
|
# @param [Object] val Field value
|
83
84
|
# @return [Array] An array of Hashes, each representing one row
|
84
85
|
def fetch_by table, field, val
|
85
|
-
hash_res(table.to_sym, @
|
86
|
+
hash_res(table.to_sym, @connector.exec(@db, Statement.prep("select * from #{Sanitizer.table @schema, table} where #{Sanitizer.field @schema, table, field} = ?;", [val])))
|
86
87
|
end
|
87
88
|
|
88
89
|
# Fetch All:
|
@@ -90,7 +91,7 @@ module Sack
|
|
90
91
|
# @param [Symbol] table Table name
|
91
92
|
# @return [Array] An array of Hashes, each representing one row
|
92
93
|
def fetch_all table
|
93
|
-
hash_res(table.to_sym, @
|
94
|
+
hash_res(table.to_sym, @connector.exec(@db, "select * from #{Sanitizer.table @schema, table};"))
|
94
95
|
end
|
95
96
|
|
96
97
|
# Create:
|
@@ -98,7 +99,7 @@ module Sack
|
|
98
99
|
# @param [Symbol] table Table name
|
99
100
|
# @param [Hash] fields Fields to be inserted
|
100
101
|
def create table, fields
|
101
|
-
@
|
102
|
+
@connector.exec @db, Statement.prep("insert into #{Sanitizer.table @schema, table} (#{Generator.fields @schema, table, fields}) values (#{Generator.marks fields});", Generator.values(fields.values))
|
102
103
|
end
|
103
104
|
|
104
105
|
# Update:
|
@@ -107,7 +108,7 @@ module Sack
|
|
107
108
|
# @param [Object] id ID on which to filter
|
108
109
|
# @param [Hash] fields Fields to be updated
|
109
110
|
def update table, id, fields
|
110
|
-
@
|
111
|
+
@connector.exec @db, Statement.prep("update #{Sanitizer.table @schema, table} set #{Generator.update_marks @schema, table, fields} where id = ?;", [Generator.values(fields.values), id].flatten)
|
111
112
|
end
|
112
113
|
|
113
114
|
# Update By Field:
|
@@ -117,7 +118,7 @@ module Sack
|
|
117
118
|
# @param [Object] val Field value
|
118
119
|
# @param [Hash] fields Fields to be updated
|
119
120
|
def update_by table, field, val, fields
|
120
|
-
@
|
121
|
+
@connector.exec @db, Statement.prep("update #{Sanitizer.table @schema, table} set #{Generator.update_marks @schema, table, fields} where #{Sanitizer.field @schema, table, field} = ?;", [Generator.values(fields.values), val].flatten)
|
121
122
|
end
|
122
123
|
|
123
124
|
# Save:
|
@@ -138,7 +139,7 @@ module Sack
|
|
138
139
|
# @param [Symbol] field Field name
|
139
140
|
# @param [Object] val Field value
|
140
141
|
def delete_by table, field, val
|
141
|
-
@
|
142
|
+
@connector.exec @db, Statement.prep("delete from #{Sanitizer.table @schema, table} where #{Sanitizer.field @schema, table, field} = ?;", [val])
|
142
143
|
end
|
143
144
|
|
144
145
|
# Destroy By Field:
|
@@ -146,7 +147,7 @@ module Sack
|
|
146
147
|
# @param [Symbol] table Table name
|
147
148
|
# @param [Object] id ID of rows to be removed
|
148
149
|
def delete table, id
|
149
|
-
@
|
150
|
+
@connector.exec @db, Statement.prep("delete from #{Sanitizer.table @schema, table} where id = ?;", [id])
|
150
151
|
end
|
151
152
|
|
152
153
|
# Execute statement:
|
@@ -155,13 +156,13 @@ module Sack
|
|
155
156
|
# @param [Array] params Statement parameters
|
156
157
|
# @return [Object] Whatever the statement returned
|
157
158
|
def exec q, params = []
|
158
|
-
@
|
159
|
+
@connector.exec @db, Statement.prep(q, params)
|
159
160
|
end
|
160
161
|
|
161
162
|
# Pull Results into Hash:
|
162
|
-
# Converts rows returned by
|
163
|
+
# Converts rows returned by DBMS into Hashes matching the provided schema.
|
163
164
|
# @param [Symbol] table Table name
|
164
|
-
# @param [Array] x Results returned by
|
165
|
+
# @param [Array] x Results returned by DBMS
|
165
166
|
# @return [Array] An array of Hashes, each representing a single row
|
166
167
|
def hash_res table, x
|
167
168
|
x
|
data/lib/sack/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eresse
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-10-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -95,6 +95,7 @@ files:
|
|
95
95
|
- Rakefile
|
96
96
|
- lib/sack.rb
|
97
97
|
- lib/sack/connectors.rb
|
98
|
+
- lib/sack/connectors/mysql.rb
|
98
99
|
- lib/sack/connectors/sqlite3.rb
|
99
100
|
- lib/sack/database.rb
|
100
101
|
- lib/sack/database/data.rb
|