Class: KnjDB_sqlite3

Inherits:
Object show all
Defined in:
lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3.rb

Defined Under Namespace

Classes: Columns, Indexes, Tables

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(knjdb_ob) ⇒ KnjDB_sqlite3

Returns a new instance of KnjDB_sqlite3.



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
30
31
32
33
34
35
36
37
38
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3.rb', line 5

def initialize(knjdb_ob)
  @escape_table = "`"
  @escape_col = "`"
  @escape_val = "'"
  @esc_table = "`"
  @esc_col = "`"
  
  @knjdb = knjdb_ob
  @path = @knjdb.opts[:path] if @knjdb.opts[:path]
  @path = @knjdb.opts["path"] if @knjdb.opts["path"]
  @symbolize = true if !@knjdb.opts.key?(:return_keys) or @knjdb.opts[:return_keys] == "symbols"
  
  @knjdb.opts[:subtype] = "java" if !@knjdb.opts.key?(:subtype) and RUBY_ENGINE == "jruby"
  raise "No path was given." if !@path
  
  if @knjdb.opts[:subtype] == "java"
    if @knjdb.opts[:sqlite_driver]
      require @knjdb.opts[:sqlite_driver]
    else
      require "#{File.dirname(__FILE__)}/../../sqlitejdbc-v056.jar"
    end
    
    require "java"
    import "org.sqlite.JDBC"
    @conn = java.sql.DriverManager::getConnection("jdbc:sqlite:#{@knjdb.opts[:path]}")
    @stat = @conn.createStatement
  elsif @knjdb.opts[:subtype] == "rhodes"
    @conn = SQLite3::Database.new(@path, @path)
  else
    @conn = SQLite3::Database.open(@path)
    @conn.results_as_hash = true
    @conn.type_translation = false
  end
end

Instance Attribute Details

#colsObject

Returns the value of attribute cols.



3
4
5
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3.rb', line 3

def cols
  @cols
end

#connObject (readonly)

Returns the value of attribute conn.



2
3
4
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3.rb', line 2

def conn
  @conn
end

#esc_col(string) ⇒ Object (readonly) Also known as: esc_table

Returns the value of attribute esc_col.



2
3
4
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3.rb', line 2

def esc_col
  @esc_col
end

#escape_colObject (readonly)

Returns the value of attribute escape_col.



2
3
4
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3.rb', line 2

def escape_col
  @escape_col
end

#escape_tableObject (readonly)

Returns the value of attribute escape_table.



2
3
4
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3.rb', line 2

def escape_table
  @escape_table
end

#escape_valObject (readonly)

Returns the value of attribute escape_val.



2
3
4
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3.rb', line 2

def escape_val
  @escape_val
end

#indexesObject

Returns the value of attribute indexes.



3
4
5
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3.rb', line 3

def indexes
  @indexes
end

#knjdbObject (readonly)

Returns the value of attribute knjdb.



2
3
4
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3.rb', line 2

def knjdb
  @knjdb
end

#symbolizeObject (readonly)

Returns the value of attribute symbolize.



2
3
4
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3.rb', line 2

def symbolize
  @symbolize
end

#tablesObject

Returns the value of attribute tables.



3
4
5
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3.rb', line 3

def tables
  @tables
end

Instance Method Details

#closeObject



83
84
85
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3.rb', line 83

def close
  @conn.close
end

#escape(string) ⇒ Object Also known as: esc



63
64
65
66
67
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3.rb', line 63

def escape(string)
  #This code is taken directly from the documentation so we dont have to rely on the SQLite3::Database class. This way it can also be used with JRuby and IronRuby...
  #http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html
  return string.to_s.gsub(/'/, "''")
end

#lastIDObject



78
79
80
81
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3.rb', line 78

def lastID
  return @conn.last_insert_row_id if @conn.respond_to?(:last_insert_row_id)
  return self.query("SELECT last_insert_rowid() AS id").fetch[:id].to_i
end

#query(string) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3.rb', line 40

def query(string)
  begin
    if @knjdb.opts[:subtype] == "rhodes"
      return KnjDB_sqlite3_result.new(self, @conn.execute(string, string))
    elsif @knjdb.opts[:subtype] == "java"
      begin
        return KnjDB_sqlite3_result_java.new(self, @stat.executeQuery(string))
      rescue java.sql.SQLException => e
        if e.message == "java.sql.SQLException: query does not return ResultSet"
          return KnjDB_sqlite3_result_java.new(self, nil)
        else
          raise e
        end
      end
    else
      return KnjDB_sqlite3_result.new(self, @conn.execute(string))
    end
  rescue => e
    #Add SQL to the error message.
    raise e.class, "#{e.message}\n\nSQL: #{string}"
  end
end