Class: KnjDB_sqlite3

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

Overview

This class handels SQLite3-specific behaviour.

Defined Under Namespace

Classes: Columns, Indexes, Sqlspecs, Tables

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(knjdb_ob) ⇒ KnjDB_sqlite3

Constructor. This should not be called manually.



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 7

def initialize(knjdb_ob)
  @sep_table = "`"
  @sep_col = "`"
  @sep_val = "'"
  
  @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__)}/../../../jruby/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.



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

def cols
  @cols
end

#connObject (readonly)

Returns the value of attribute conn.



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

def conn
  @conn
end

#indexesObject

Returns the value of attribute indexes.



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

def indexes
  @indexes
end

#knjdbObject (readonly)

Returns the value of attribute knjdb.



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

def knjdb
  @knjdb
end

#sep_colObject (readonly)

Returns the value of attribute sep_col.



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

def sep_col
  @sep_col
end

#sep_tableObject (readonly)

Returns the value of attribute sep_table.



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

def sep_table
  @sep_table
end

#sep_valObject (readonly)

Returns the value of attribute sep_val.



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

def sep_val
  @sep_val
end

#symbolizeObject (readonly)

Returns the value of attribute symbolize.



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

def symbolize
  @symbolize
end

#tablesObject

Returns the value of attribute tables.



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

def tables
  @tables
end

Instance Method Details

#closeObject

Closes the connection to the database.



91
92
93
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3.rb', line 91

def close
  @conn.close
end

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

Escapes a string to be used as a column.



75
76
77
78
79
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3.rb', line 75

def esc_col(string)
  string = string.to_s
  raise "Invalid column-string: #{string}" if string.index(@sep_col) != nil
  return string
end

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

Escapes a string to be safe to used in a query.



68
69
70
71
72
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3.rb', line 68

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

Returns the last inserted ID.



85
86
87
88
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3.rb', line 85

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 Also known as: query_ubuf

Executes a query against the driver.



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

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.to_s.index("query does not return ResultSet") != nil
          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} (SQL: #{string})"
  end
end

#transactionObject

Starts a transaction, yields the database and commits.



96
97
98
99
100
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3.rb', line 96

def transaction
  @conn.transaction do
    yield(@knjdb)
  end
end