baza 0.0.23 → 0.0.24

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: a140880f4a29b160732e615d71edcbef2067558f
4
- data.tar.gz: 18778633d92ec1fd6a90ebe77b070b07b4955698
3
+ metadata.gz: 0ca360eb0771fa4e40e286f2693d2e2957b9ee44
4
+ data.tar.gz: f9e91f37f01b60f6333fa1561d907c3ab7759d24
5
5
  SHA512:
6
- metadata.gz: 684f412f2eb01b8f400b8fe3ba2b082a67fc606612d569140615fc38dbe05e0e899f2555e8fea9e1aeee804404b222be71049114153aadbe7d0e804ee6cf60b8
7
- data.tar.gz: f5738e3752f928fd38b705149fc03c465283dfc302e8b4d7a4b734d811f0b02d6566d22ffcd95b8d7c025e267167d79a4c8d9e2eff87a89b6fff22076eb7d7d0
6
+ metadata.gz: ffbb2c02f6d404ceb64d4ebf7dca19f5a31d43e5fcfd0f43aac60fa42d61d459fb285c2308ec477f2ad8383cd7c2e8a2eac6746716e338841db2677608b1e448
7
+ data.tar.gz: ad1a9f018bed9a0e3ed570a2058d28ee6c83fa43ba821fe3f3b55de458cbde5f40685c5a21a4cbc0f4265f92f0ba9e376a348e0ab03ca3a824b0613a8510747b
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.23
1
+ 0.0.24
data/baza.gemspec CHANGED
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: baza 0.0.23 ruby lib
5
+ # stub: baza 0.0.24 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "baza".freeze
9
- s.version = "0.0.23"
9
+ s.version = "0.0.24"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib".freeze]
13
13
  s.authors = ["Kasper Johansen".freeze]
14
- s.date = "2017-01-22"
14
+ s.date = "2017-01-27"
15
15
  s.description = "A database abstraction layer, model framework and database framework.".freeze
16
16
  s.email = "kj@gfish.com".freeze
17
17
  s.extra_rdoc_files = [
@@ -37,6 +37,8 @@ Gem::Specification.new do |s|
37
37
  "lib/baza/base_sql_driver.rb",
38
38
  "lib/baza/cloner.rb",
39
39
  "lib/baza/column.rb",
40
+ "lib/baza/commands.rb",
41
+ "lib/baza/commands/select.rb",
40
42
  "lib/baza/database.rb",
41
43
  "lib/baza/database_model.rb",
42
44
  "lib/baza/database_model_functionality.rb",
@@ -105,86 +105,14 @@ class Baza::BaseSqlDriver
105
105
 
106
106
  SELECT_ARGS_ALLOWED_KEYS = [:limit, :limit_from, :limit_to].freeze
107
107
  # Makes a select from the given arguments: table-name, where-terms and other arguments as limits and orders. Also takes a block to avoid raping of memory.
108
- def select(tablename, arr_terms = nil, args = nil, &block)
109
- # Set up vars.
110
- sql = ""
111
- args_q = nil
112
- select_sql = "*"
113
-
114
- # Give 'cloned_ubuf' argument to 'q'-method.
115
- args_q = {cloned_ubuf: true} if args && args[:cloned_ubuf]
116
-
117
- # Set up IDQuery-stuff if that is given in arguments.
118
- if args && args[:idquery]
119
- if args.fetch(:idquery) == true
120
- select_sql = "#{sep_col}id#{sep_col}"
121
- col = :id
122
- else
123
- select_sql = "#{sep_col}#{escape_column(args.fetch(:idquery))}#{sep_col}"
124
- col = args.fetch(:idquery)
125
- end
126
- end
127
-
128
- sql = "SELECT #{select_sql} FROM"
129
-
130
- if tablename.is_a?(Array)
131
- sql << " #{@sep_table}#{tablename.first}#{@sep_table}.#{@sep_table}#{tablename.last}#{@sep_table}"
132
- else
133
- sql << " #{@sep_table}#{tablename}#{@sep_table}"
134
- end
135
-
136
- if !arr_terms.nil? && !arr_terms.empty?
137
- sql << " WHERE #{sql_make_where(arr_terms)}"
138
- end
139
-
140
- unless args.nil?
141
- if args[:orderby]
142
- sql << " ORDER BY"
143
-
144
- if args.fetch(:orderby).is_a?(Array)
145
- first = true
146
- args.fetch(:orderby).each do |order_by|
147
- sql << "," unless first
148
- first = false if first
149
- sql << " #{sep_col}#{escape_column(order_by)}#{sep_col}"
150
- end
151
- else
152
- sql << " #{sep_col}#{escape_column(args.fetch(:orderby))}#{sep_col}"
153
- end
154
- end
155
-
156
- sql << " LIMIT #{args[:limit]}" if args[:limit]
157
-
158
- if args[:limit_from] && args[:limit_to]
159
- begin
160
- Float(args[:limit_from])
161
- rescue
162
- raise "'limit_from' was not numeric: '#{args.fetch(:limit_from)}'."
163
- end
164
-
165
- begin
166
- Float(args[:limit_to])
167
- rescue
168
- raise "'limit_to' was not numeric: '#{args[:limit_to]}'."
169
- end
170
-
171
- sql << " LIMIT #{args.fetch(:limit_from)}, #{args.fetch(:limit_to)}"
172
- end
173
- end
174
-
175
- # Do IDQuery if given in arguments.
176
- if args && args[:idquery]
177
- res = Baza::Idquery.new(db: @db, table: tablename, query: sql, col: col, &block)
178
- else
179
- res = @db.q(sql, args_q, &block)
180
- end
181
-
182
- # Return result if a block wasnt given.
183
- if block
184
- return nil
185
- else
186
- return res
187
- end
108
+ def select(table_name, terms = nil, args = nil, &block)
109
+ Baza::Commands::Select.new(
110
+ args: args,
111
+ block: block,
112
+ db: @db,
113
+ table_name: table_name,
114
+ terms: terms
115
+ ).execute
188
116
  end
189
117
 
190
118
  def count(tablename, arr_terms = nil)
@@ -0,0 +1,3 @@
1
+ class Baza::Commands
2
+ AutoAutoloader.autoload_sub_classes(self, __FILE__)
3
+ end
@@ -0,0 +1,110 @@
1
+ class Baza::Commands::Select
2
+ def initialize(args)
3
+ @args = args.fetch(:args)
4
+ @block = args.fetch(:block)
5
+ @db = args.fetch(:db)
6
+ @sql = ""
7
+ @table_name = args.fetch(:table_name)
8
+ @terms = args.fetch(:terms)
9
+ end
10
+
11
+ def execute
12
+ # Give 'cloned_ubuf' argument to 'q'-method.
13
+ if @args
14
+ @args_q = {cloned_ubuf: true} if @args[:cloned_ubuf]
15
+ @args_q = {unbuffered: true} if @args[:unbuffered]
16
+ end
17
+
18
+ add_select_sql
19
+ add_terms_sql
20
+ add_order_sql
21
+ add_limit_sql
22
+
23
+ result = execute_query
24
+
25
+ # Return result if a block wasnt given.
26
+ if @block
27
+ nil
28
+ else
29
+ result
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def add_select_sql
36
+ # Set up IDQuery-stuff if that is given in arguments.
37
+ if @args && @args[:idquery]
38
+ if @args.fetch(:idquery) == true
39
+ select_sql = "#{@db.sep_col}id#{@db.sep_col}"
40
+ @col = :id
41
+ else
42
+ select_sql = "#{@db.sep_col}#{@db.escape_column(@args.fetch(:idquery))}#{@db.sep_col}"
43
+ @col = @args.fetch(:idquery)
44
+ end
45
+ end
46
+
47
+ select_sql ||= "*"
48
+ @sql << "SELECT #{select_sql} FROM"
49
+
50
+ if @table_name.is_a?(Array)
51
+ @sql << " #{@sep_table}#{@table_name.first}#{@sep_table}.#{@sep_table}#{@table_name.last}#{@sep_table}"
52
+ else
53
+ @sql << " #{@sep_table}#{@table_name}#{@sep_table}"
54
+ end
55
+ end
56
+
57
+ def add_terms_sql
58
+ @sql << " WHERE #{@db.sql_make_where(@terms)}" if !@terms.nil? && !@terms.empty?
59
+ end
60
+
61
+ def add_order_sql
62
+ return if @args.nil?
63
+
64
+ if @args[:orderby]
65
+ @sql << " ORDER BY"
66
+
67
+ if @args.fetch(:orderby).is_a?(Array)
68
+ first = true
69
+ @args.fetch(:orderby).each do |order_by|
70
+ @sql << "," unless first
71
+ first = false if first
72
+ @sql << " #{@db.sep_col}#{@db.escape_column(order_by)}#{@db.sep_col}"
73
+ end
74
+ else
75
+ @sql << " #{@db.sep_col}#{@db.escape_column(@args.fetch(:orderby))}#{@db.sep_col}"
76
+ end
77
+ end
78
+ end
79
+
80
+ def add_limit_sql
81
+ return if @args.nil?
82
+
83
+ @sql << " LIMIT #{@args[:limit]}" if @args[:limit]
84
+
85
+ if @args[:limit_from] && @args[:limit_to]
86
+ begin
87
+ Float(@args[:limit_from])
88
+ rescue
89
+ raise "'limit_from' was not numeric: '#{@args.fetch(:limit_from)}'."
90
+ end
91
+
92
+ begin
93
+ Float(@args[:limit_to])
94
+ rescue
95
+ raise "'limit_to' was not numeric: '#{@args[:limit_to]}'."
96
+ end
97
+
98
+ @sql << " LIMIT #{@args.fetch(:limit_from)}, #{@args.fetch(:limit_to)}"
99
+ end
100
+ end
101
+
102
+ def execute_query
103
+ # Do IDQuery if given in arguments.
104
+ if @args && @args[:idquery]
105
+ Baza::Idquery.new(db: @db, table: @table_name, query: @sql, col: @col, &@block)
106
+ else
107
+ @db.q(@sql, @args_q, &@block)
108
+ end
109
+ end
110
+ end
@@ -1,2 +1,5 @@
1
1
  class Baza::Driver::Sqlite3::Database < Baza::Database
2
+ def use
3
+ # Dont do anything since the file only contains one database
4
+ end
2
5
  end
data/lib/baza/dump.rb CHANGED
@@ -4,8 +4,9 @@ class Baza::Dump
4
4
  #===Examples
5
5
  # dump = Baza::Dump.new(:db => db)
6
6
  def initialize(args)
7
- @args = args
8
- @debug = @args[:debug]
7
+ @db = args.fetch(:db)
8
+ @debug = args[:debug]
9
+ @tables = args[:tables]
9
10
  end
10
11
 
11
12
  # Method used to update the status.
@@ -20,13 +21,13 @@ class Baza::Dump
20
21
 
21
22
  # Dumps all tables into the given IO.
22
23
  def dump(io)
23
- print "Going through tables.\n" if @debug
24
+ debug "Going through tables."
24
25
  @rows_count = 0
25
26
 
26
- if @args[:tables]
27
- tables = @args[:tables]
27
+ if @tables
28
+ tables = @tables
28
29
  else
29
- tables = @args[:db].tables.list
30
+ tables = @db.tables.list
30
31
  end
31
32
 
32
33
  if @on_status
@@ -39,7 +40,7 @@ class Baza::Dump
39
40
  end
40
41
 
41
42
  tables.each do |table_obj|
42
- table_obj = @args[:db].tables[table_obj] if table_obj.is_a?(String) || table_obj.is_a?(Symbol)
43
+ table_obj = @db.tables[table_obj] if table_obj.is_a?(String) || table_obj.is_a?(Symbol)
43
44
  next if table_obj.native?
44
45
 
45
46
  # Figure out keys.
@@ -50,7 +51,7 @@ class Baza::Dump
50
51
 
51
52
  @table_obj = table_obj
52
53
  update_status
53
- puts "Dumping table: '#{table_obj.name}'." if @debug
54
+ debug "Dumping table: '#{table_obj.name}'."
54
55
  dump_table(io, table_obj)
55
56
  end
56
57
  end
@@ -60,13 +61,17 @@ class Baza::Dump
60
61
  @on_status = block
61
62
  end
62
63
 
64
+ def debug(message)
65
+ puts message if @debug
66
+ end
67
+
63
68
  # Dumps the given table into the given IO.
64
69
  def dump_table(io, table_obj)
65
70
  create_data = table_obj.data
66
71
  create_data.delete(:name)
67
72
 
68
73
  # Get SQL for creating table and add it to IO.
69
- sqls = @args[:db].tables.create(table_obj.name, create_data, return_sql: true)
74
+ sqls = @db.tables.create(table_obj.name, create_data, return_sql: true)
70
75
  sqls.each do |sql|
71
76
  io.write("#{sql};\n")
72
77
  end
@@ -82,10 +87,14 @@ class Baza::Dump
82
87
  end
83
88
 
84
89
 
90
+ debug "Dumping data for table: #{table_obj.name}"
91
+
85
92
  # Set up rows and way to fill rows.
86
93
  rows = []
87
- block_data = proc do |row|
88
- rows << row
94
+
95
+
96
+ @db.select(table_obj.name, nil, unbuffered: true) do |row|
97
+ rows << row.values
89
98
  @rows_count += 1
90
99
 
91
100
  if rows.length >= 1000
@@ -95,24 +104,14 @@ class Baza::Dump
95
104
  end
96
105
 
97
106
 
98
- # If a primary column is found then use IDQuery. Otherwise use cloned unbuffered query.
99
- args = {idquery: prim_col.name} if prim_col
100
-
101
-
102
- # Clone the connecting with array-results and execute query.
103
- @args[:db].clone_conn(result: "array") do |db|
104
- db.select(table_obj.name, nil, args, &block_data)
105
- end
106
-
107
-
108
107
  # Dump the last rows if any.
109
108
  dump_insert_multi(io, table_obj, rows) unless rows.empty?
110
109
  end
111
110
 
112
111
  # Dumps the given rows from the given table into the given IO.
113
112
  def dump_insert_multi(io, table_obj, rows)
114
- print "Inserting #{rows.length} into #{table_obj.name}.\n" if @debug
115
- sqls = @args[:db].insert_multi(table_obj.name, rows, return_sql: true, keys: @keys)
113
+ debug "Inserting #{rows.length} into #{table_obj.name}."
114
+ sqls = @db.insert_multi(table_obj.name, rows, return_sql: true, keys: @keys)
116
115
  sqls.each do |sql|
117
116
  io.write("#{sql};\n")
118
117
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: baza
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.23
4
+ version: 0.0.24
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kasper Johansen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-22 00:00:00.000000000 Z
11
+ date: 2017-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: array_enumerator
@@ -344,6 +344,8 @@ files:
344
344
  - lib/baza/base_sql_driver.rb
345
345
  - lib/baza/cloner.rb
346
346
  - lib/baza/column.rb
347
+ - lib/baza/commands.rb
348
+ - lib/baza/commands/select.rb
347
349
  - lib/baza/database.rb
348
350
  - lib/baza/database_model.rb
349
351
  - lib/baza/database_model_functionality.rb