Class: CArray

Inherits:
Object
  • Object
show all
Defined in:
lib/carray-io-sqlite3/core.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load_sqlite3(expr, *args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/carray-io-sqlite3/core.rb', line 5

def self.load_sqlite3 (expr, *args)
  case expr
  when String
    file = expr
    sql, *vars = *args
    db = SQLite3::Database.new(file)
    names, *table = db.execute2(sql, *vars)
    db.close
  when SQLite3::Database
    db = expr
    sql, *vars = *args
    names, *table = db.execute2(sql, *vars)
  when SQLite3::Statement
    stmt = expr
    vars = args
    names = stmt.columns
    table = stmt.execute!(*vars)
  else
    raise "invalid 1st arg"
  end
  table = table.to_ca
  table.extend(CA::TableMethods)
  table.column_names = names
  return table
end

Instance Method Details

#to_sqlite3(database: nil, table:, datatype: "numeric", schema: nil, transaction: true) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/carray-io-sqlite3/core.rb', line 31

def to_sqlite3 (database: nil, table:, datatype: "numeric", schema: nil, transaction: true)
  unless rank <= 2
    raise "to_sqlite3 is valid only for 2-dim array (table)"
  end
  case database
  when SQLite3::Database
  when String
    if File.exist? database
      database = SQLite3::Database.open(database)
    else
      database = SQLite3::Database.new(database)        
    end
  else
    database = SQLite3::Database.new ":memory:"
  end
  if respond_to?(:column_names) and column_names
    vars = column_names
  else
    if rank == 1
      vars = ["c0"]
    else
      vars = CArray.object(dim1).seq.map{|s| "c#{s}" }
    end
  end
  
  if schema
    database.execute "create table if not exists #{table} (" + schema + ")"          
  else
    database.execute "create table if not exists #{table} (" + vars.map{|s| s + " " + datatype }.join(",") + ")"
  end

  insert = database.prepare %{ insert or replace into #{table} values (#{(["?"]*vars.size).join(",")}) }
  database.transaction if transaction
  if rank == 1
    dim0.times do |i|
      insert.execute [self[i]]
    end
  else
    begin
      dim0.times do |i|
        begin
         insert.execute self[i,nil].to_a
        rescue
          puts self[i,nil].to_a
          raise $!
        end
      end
    end
  end
  database.commit if transaction
  insert.close
  return database
end

#unblob(type, subdim = nil) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/carray-io-sqlite3/core.rb', line 85

def unblob (type, subdim = nil)
  bytes = CArray.sizeof(type)
  elem = nil
  self.each do |e|
    unless e.nil?
      elem = e
      break
    end
  end
  if elem == nil and subdim == nil
    raise "all element is nil, please specify dimension."
  end
  unless subdim
    subdim = [elem.bytesize/bytes]
  end
  out = CArray.new(type, dim + subdim)
  needed_bytes = out.elements/self.elements*bytes
  ref = out.refer(:fixlen, dim, :bytes=>needed_bytes)
  if elem and needed_bytes != elem.bytesize 
    self.each_addr do |addr|
      if self[addr].nil?
        ref[[addr]] = UNDEF
      else
        ref[[addr]].load_binary self[addr]
      end
    end
  elsif self.any_equal?(nil)
    copy  = self.to_ca
    sel   = self.eq(nil)
    copy[sel] = "\0" * needed_bytes
    ref.load_binary copy.join
    ref[sel] = UNDEF
  else
    ref.load_binary self.join
  end
  return out
end