baza 0.0.15 → 0.0.16

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  #This class handels every MySQL-column, that can be returned from a table-object.
2
- class Baza::Driver::Mysql::Column
2
+ class Baza::Driver::Mysql::Column < Baza::Column
3
3
  attr_reader :args, :name
4
4
 
5
5
  #Constructor. Should not be called manually.
@@ -1,4 +1,4 @@
1
- class Baza::Driver::Mysql::Index
1
+ class Baza::Driver::Mysql::Index < Baza::Index
2
2
  attr_reader :args, :columns
3
3
 
4
4
  def initialize(args)
@@ -53,7 +53,7 @@ class Baza::Driver::Mysql::Index
53
53
 
54
54
  #Returns true if the index is a unique-index.
55
55
  def unique?
56
- if @args[:data][:Index_type] == "UNIQUE"
56
+ if @args[:data][:Index_type] == "UNIQUE" || @args[:data][:Non_unique].to_i == 0
57
57
  return true
58
58
  else
59
59
  return false
@@ -65,12 +65,4 @@ class Baza::Driver::Mysql::Index
65
65
  return true if @args[:data][:Key_name] == "PRIMARY"
66
66
  return false
67
67
  end
68
-
69
- def to_s
70
- return "#<Baza::Driver::Mysql::Index name: \"#{name}\", columns: #{@columns}, primary: #{primary?}, unique: #{unique?}>"
71
- end
72
-
73
- def inspect
74
- to_s
75
- end
76
68
  end
@@ -1,4 +1,4 @@
1
- class Baza::Driver::Mysql::Table
1
+ class Baza::Driver::Mysql::Table < Baza::Table
2
2
  attr_reader :list, :name
3
3
 
4
4
  def initialize(args)
@@ -6,8 +6,8 @@ class Baza::Driver::Mysql::Table
6
6
  @db = args[:db]
7
7
  @data = args[:data]
8
8
  @subtype = @db.opts[:subtype]
9
- @list = Wref_map.new
10
- @indexes_list = Wref_map.new
9
+ @list = Wref::Map.new
10
+ @indexes_list = Wref::Map.new
11
11
  @name = @data[:Name].to_sym
12
12
  @tables = args[:tables]
13
13
 
@@ -48,7 +48,7 @@ class Baza::Driver::Mysql::Table
48
48
  def column(name)
49
49
  name = name.to_sym
50
50
 
51
- if col = @list.get!(name)
51
+ if col = @list.get(name)
52
52
  return @list[name]
53
53
  end
54
54
 
@@ -63,11 +63,11 @@ class Baza::Driver::Mysql::Table
63
63
  @db.cols
64
64
  ret = {}
65
65
  sql = "SHOW FULL COLUMNS FROM `#{@db.esc_table(name)}`"
66
- sql << " WHERE `Field` = '#{@db.esc(args[:name])}'" if args and args.key?(:name)
66
+ sql << " WHERE `Field` = '#{@db.esc(args[:name])}'" if args && args.key?(:name)
67
67
 
68
68
  @db.q(sql) do |d_cols|
69
69
  column_name = d_cols[:Field].to_sym
70
- obj = @list.get!(name)
70
+ obj = @list.get(name)
71
71
 
72
72
  unless obj
73
73
  obj = Baza::Driver::Mysql::Column.new(
@@ -92,7 +92,7 @@ class Baza::Driver::Mysql::Table
92
92
  end
93
93
  end
94
94
 
95
- def indexes args = nil
95
+ def indexes(args = nil)
96
96
  @db.indexes
97
97
  ret = {}
98
98
 
@@ -101,7 +101,7 @@ class Baza::Driver::Mysql::Table
101
101
 
102
102
  @db.q(sql) do |d_indexes|
103
103
  next if d_indexes[:Key_name] == "PRIMARY"
104
- obj = @indexes_list.get!(d_indexes[:Key_name].to_s)
104
+ obj = @indexes_list.get(d_indexes[:Key_name].to_s)
105
105
 
106
106
  unless obj
107
107
  obj = Baza::Driver::Mysql::Index.new(
@@ -130,7 +130,7 @@ class Baza::Driver::Mysql::Table
130
130
  def index(name)
131
131
  name = name.to_s
132
132
 
133
- if index = @indexes_list.get!(name)
133
+ if index = @indexes_list.get(name)
134
134
  return index
135
135
  end
136
136
 
@@ -167,7 +167,7 @@ class Baza::Driver::Mysql::Table
167
167
  sql = ""
168
168
  end
169
169
 
170
- if args[:create] or !args.key?(:create)
170
+ if args[:create] || !args.key?(:create)
171
171
  sql << "CREATE"
172
172
  end
173
173
 
@@ -217,7 +217,7 @@ class Baza::Driver::Mysql::Table
217
217
  end
218
218
  end
219
219
 
220
- def rename newname
220
+ def rename(newname)
221
221
  newname = newname.to_sym
222
222
  oldname = name
223
223
 
@@ -10,7 +10,7 @@ class Baza::Driver::Mysql::Tables
10
10
  @db = @args[:db]
11
11
  @subtype = @db.opts[:subtype]
12
12
  @list_mutex = Monitor.new
13
- @list = Wref_map.new
13
+ @list = Wref::Map.new
14
14
  @list_should_be_reloaded = true
15
15
  end
16
16
 
@@ -23,10 +23,8 @@ class Baza::Driver::Mysql::Tables
23
23
  def [](table_name)
24
24
  table_name = table_name.to_sym
25
25
 
26
- begin
27
- return @list[table_name]
28
- rescue Wref::Recycled
29
- #ignore.
26
+ if table = @list[table_name]
27
+ return table
30
28
  end
31
29
 
32
30
  tables = []
@@ -52,9 +50,9 @@ class Baza::Driver::Mysql::Tables
52
50
  @db.q(sql) do |d_tables|
53
51
  raise "No name was given from: #{d_tables}" unless d_tables.is_a?(Hash) && d_tables[:Name]
54
52
  name = d_tables[:Name].to_sym
55
- obj = @list.get!(name)
53
+ obj = @list.get(name)
56
54
 
57
- if !obj
55
+ unless obj
58
56
  obj = Baza::Driver::Mysql::Table.new(
59
57
  db: @db,
60
58
  data: d_tables,
@@ -23,43 +23,52 @@ class Baza::Driver::Sqlite3
23
23
  conn: args[:object]
24
24
  }
25
25
  }
26
+ elsif args[:object].class.name == "Java::OrgSqlite::SQLiteConnection"
27
+ return {
28
+ type: :success,
29
+ args: {
30
+ type: :sqlite3,
31
+ conn: args[:object]
32
+ }
33
+ }
26
34
  end
27
35
 
28
36
  return nil
29
37
  end
30
38
 
31
39
  #Constructor. This should not be called manually.
32
- def initialize(baza_db_obj)
40
+ def initialize(baza_db)
33
41
  @sep_table = "`"
34
42
  @sep_col = "`"
35
43
  @sep_val = "'"
36
44
 
37
- @baza_db = baza_db_obj
38
- @path = @baza_db.opts[:path] if @baza_db.opts[:path]
39
- @path = @baza_db.opts["path"] if @baza_db.opts["path"]
45
+ @baza = baza_db
46
+ @path = @baza.opts[:path] if @baza.opts[:path]
47
+ @baza.opts[:subtype] ||= :java if RUBY_ENGINE == "jruby"
48
+ @subtype = @baza.opts[:subtype]
40
49
 
41
- @baza_db.opts[:subtype] = "java" if !@baza_db.opts.key?(:subtype) && RUBY_ENGINE == "jruby"
42
-
43
- if @baza_db.opts[:conn]
44
- @conn = @baza_db.opts[:conn]
50
+ if @baza.opts[:conn]
51
+ @conn = @baza.opts[:conn]
52
+ @stat = @conn.createStatement if @subtype == :java
45
53
  else
46
- raise "No path was given." if !@path
54
+ raise "No path was given." unless @path
47
55
 
48
- if @baza_db.opts[:subtype] == "java"
49
- if @baza_db.opts[:sqlite_driver]
50
- require @baza_db.opts[:sqlite_driver]
56
+ if @subtype == :java
57
+ if @baza.opts[:sqlite_driver]
58
+ require @baza.opts[:sqlite_driver]
51
59
  else
52
- require "#{File.dirname(__FILE__)}/../../../jruby/sqlitejdbc-v056.jar"
60
+ require "jdbc/sqlite3"
61
+ ::Jdbc::SQLite3.load_driver
53
62
  end
54
63
 
55
64
  require "java"
56
65
  import "org.sqlite.JDBC"
57
- @conn = java.sql.DriverManager::getConnection("jdbc:sqlite:#{@baza_db.opts[:path]}")
66
+ @conn = java.sql.DriverManager::getConnection("jdbc:sqlite:#{@baza.opts[:path]}")
58
67
  @stat = @conn.createStatement
59
- elsif @baza_db.opts[:subtype] == "rhodes"
60
- @conn = SQLite3::Database.new(@path, @path)
68
+ elsif @subtype == :rhodes
69
+ @conn = ::SQLite3::Database.new(@path, @path)
61
70
  else
62
- @conn = SQLite3::Database.open(@path)
71
+ @conn = ::SQLite3::Database.open(@path)
63
72
  @conn.results_as_hash = true
64
73
  @conn.type_translation = false
65
74
  end
@@ -68,9 +77,9 @@ class Baza::Driver::Sqlite3
68
77
 
69
78
  #Executes a query against the driver.
70
79
  def query(string)
71
- if @baza_db.opts[:subtype] == :rhodes
80
+ if @subtype == :rhodes
72
81
  return Baza::Driver::Sqlite3::Result.new(self, @conn.execute(string, string))
73
- elsif @baza_db.opts[:subtype] == :java
82
+ elsif @subtype == :java
74
83
  begin
75
84
  return Baza::Driver::Sqlite3::ResultJava.new(self, @stat.executeQuery(string))
76
85
  rescue java.sql.SQLException => e
@@ -118,17 +127,28 @@ class Baza::Driver::Sqlite3
118
127
 
119
128
  #Starts a transaction, yields the database and commits.
120
129
  def transaction
121
- @conn.transaction do
122
- yield(@baza_db)
130
+ if @subtype == :java
131
+ query("BEGIN TRANSACTION")
132
+
133
+ begin
134
+ yield(@baza)
135
+ query("COMMIT")
136
+ rescue => e
137
+ query("ROLLBACK")
138
+ end
139
+ else
140
+ @conn.transaction do
141
+ yield(@baza)
142
+ end
123
143
  end
124
144
  end
125
145
 
126
146
  def insert_multi(tablename, arr_hashes, args = nil)
127
147
  sql = [] if args && args[:return_sql]
128
148
 
129
- @baza_db.transaction do
149
+ @baza.transaction do
130
150
  arr_hashes.each do |hash|
131
- res = @baza_db.insert(tablename, hash, args)
151
+ res = @baza.insert(tablename, hash, args)
132
152
  sql << res if args && args[:return_sql]
133
153
  end
134
154
  end
@@ -1,5 +1,5 @@
1
1
  #This class handels all the SQLite3-columns.
2
- class Baza::Driver::Sqlite3::Column
2
+ class Baza::Driver::Sqlite3::Column < Baza::Column
3
3
  attr_reader :args
4
4
 
5
5
  #Constructor. This should not be called manually.
@@ -1,4 +1,4 @@
1
- class Baza::Driver::Sqlite3::Index
1
+ class Baza::Driver::Sqlite3::Index < Baza::Index
2
2
  attr_reader :args, :columns
3
3
 
4
4
  def initialize(args)
@@ -23,7 +23,7 @@ class Baza::Driver::Sqlite3::Index
23
23
  @db.query("DROP INDEX `#{name}`")
24
24
  end
25
25
 
26
- def rename newname
26
+ def rename(newname)
27
27
  newname = newname.to_sym
28
28
 
29
29
  create_args = data
@@ -37,6 +37,7 @@ class Baza::Driver::Sqlite3::Index
37
37
  def data
38
38
  return {
39
39
  name: name,
40
+ unique: unique?,
40
41
  columns: @columns
41
42
  }
42
43
  end
@@ -45,11 +46,7 @@ class Baza::Driver::Sqlite3::Index
45
46
  @columns
46
47
  end
47
48
 
48
- def to_s
49
- "#<Baza::Driver::Sqlite3::Index name: \"#{name}\", columns: #{@columns}>"
50
- end
51
-
52
- def inspect
53
- to_s
49
+ def unique?
50
+ @args[:data][:unique].to_i == 1
54
51
  end
55
52
  end
@@ -2,7 +2,7 @@
2
2
  class Baza::Driver::Sqlite3::ResultJava
3
3
  def initialize(driver, rs)
4
4
  @index = 0
5
- retkeys = driver.knjdb.opts[:return_keys]
5
+ retkeys = driver.baza.opts[:return_keys]
6
6
 
7
7
  if rs
8
8
  metadata = rs.getMetaData
@@ -1,4 +1,4 @@
1
- class Baza::Driver::Sqlite3::Table
1
+ class Baza::Driver::Sqlite3::Table < Baza::Table
2
2
  attr_reader :name, :type
3
3
 
4
4
  def initialize(args)
@@ -8,8 +8,8 @@ class Baza::Driver::Sqlite3::Table
8
8
  @type = @data[:type].to_sym
9
9
  @tables = args[:tables]
10
10
 
11
- @list = Wref_map.new
12
- @indexes_list = Wref_map.new
11
+ @list = Wref::Map.new
12
+ @indexes_list = Wref::Map.new
13
13
  end
14
14
 
15
15
  def maxlength
@@ -84,7 +84,7 @@ class Baza::Driver::Sqlite3::Table
84
84
 
85
85
  @db.q("PRAGMA table_info(`#{@db.esc_table(name)}`)") do |d_cols|
86
86
  column_name = d_cols[:name].to_sym
87
- obj = @list.get!(column_name)
87
+ obj = @list.get(column_name)
88
88
 
89
89
  unless obj
90
90
  obj = Baza::Driver::Sqlite3::Column.new(
@@ -223,6 +223,7 @@ class Baza::Driver::Sqlite3::Table
223
223
  end
224
224
  end
225
225
  end
226
+
226
227
  sql << ");"
227
228
  @db.query(sql)
228
229
 
@@ -250,20 +251,18 @@ class Baza::Driver::Sqlite3::Table
250
251
  @db.query("DROP TABLE `#{temp_name}`")
251
252
  end
252
253
 
253
- def index index_name
254
+ def index(index_name)
254
255
  index_name = index_name.to_sym
255
256
 
256
- begin
257
- return @indexes_list[index_name]
258
- rescue Wref::Recycled
259
- if @db.opts[:index_append_table_name]
260
- tryname = "#{name}__#{index_name}"
257
+ if index = @indexes_list[index_name]
258
+ return index
259
+ end
261
260
 
262
- begin
263
- return @indexes_list[tryname]
264
- rescue Wref::Recycled
265
- #ignore.
266
- end
261
+ if @db.opts[:index_append_table_name]
262
+ tryname = "#{name}__#{index_name}"
263
+
264
+ if index = @indexes_list[tryname]
265
+ return index
267
266
  end
268
267
  end
269
268
 
@@ -284,7 +283,7 @@ class Baza::Driver::Sqlite3::Table
284
283
 
285
284
  @db.q("PRAGMA index_list(`#{@db.esc_table(name)}`)") do |d_indexes|
286
285
  next if d_indexes[:Key_name] == "PRIMARY"
287
- obj = @indexes_list.get!(d_indexes[:name])
286
+ obj = @indexes_list.get(d_indexes[:name])
288
287
 
289
288
  unless obj
290
289
  obj = Baza::Driver::Sqlite3::Index.new(
@@ -326,8 +325,8 @@ class Baza::Driver::Sqlite3::Table
326
325
  index_data = {name: index_data, columns: [index_data]}
327
326
  end
328
327
 
329
- raise "No name was given in data: '#{index_data}'." if !index_data.key?(:name) or index_data[:name].to_s.strip.empty?
330
- raise "No columns was given on index #{index_data[:name]}." if index_data[:columns].empty?
328
+ raise "No name was given in data: '#{index_data}'." if !index_data.key?(:name) || index_data[:name].to_s.strip.empty?
329
+ raise "No columns was given on index #{index_data[:name]}." if !index_data[:columns] || index_data[:columns].empty?
331
330
 
332
331
  name = index_data[:name]
333
332
  name = "#{self.name}__#{name}" if @db.opts[:index_append_table_name]
@@ -6,17 +6,14 @@ class Baza::Driver::Sqlite3::Tables
6
6
  @db = @args[:db]
7
7
 
8
8
  @list_mutex = Mutex.new
9
- @list = Wref_map.new
9
+ @list = Wref::Map.new
10
10
  end
11
11
 
12
12
  def [](table_name)
13
13
  table_name = table_name.to_sym
14
14
 
15
- begin
16
- ret = @list[table_name]
15
+ if ret = @list.get(table_name)
17
16
  return ret
18
- rescue Wref::Recycled
19
- #ignore.
20
17
  end
21
18
 
22
19
  self.list do |table_obj|
@@ -34,9 +31,9 @@ class Baza::Driver::Sqlite3::Tables
34
31
  next if d_tables[:name] == "sqlite_sequence"
35
32
 
36
33
  tname = d_tables[:name].to_sym
37
- obj = @list.get!(tname)
34
+ obj = @list.get(tname)
38
35
 
39
- if !obj
36
+ unless obj
40
37
  obj = Baza::Driver::Sqlite3::Table.new(
41
38
  db: @db,
42
39
  data: d_tables,
@@ -0,0 +1,9 @@
1
+ class Baza::Index
2
+ def to_s
3
+ "#<#{self.class.name} name: \"#{name}\" unique=\"#{unique?}\" columns: #{@columns}>"
4
+ end
5
+
6
+ def inspect
7
+ to_s
8
+ end
9
+ end
@@ -453,7 +453,7 @@ class Baza::Model
453
453
  enum.each(&block)
454
454
  return nil
455
455
  elsif @ob.args[:array_enum]
456
- return Array_enumerator.new(enum)
456
+ return ArrayEnumerator.new(enum)
457
457
  else
458
458
  return enum.to_a
459
459
  end