Class: Og::SqliteAdapter
Overview
The SQLite adapter. This adapter communicates with an SQLite3 rdbms. For extra documentation see lib/og/adapter.rb
Instance Attribute Summary
Attributes inherited from Adapter
Instance Method Summary collapse
- #calc_field_index(klass, db) ⇒ Object
- #create_table(klass, db) ⇒ Object
- #drop_db(database, user = nil, password = nil) ⇒ Object
-
#insert_code(klass, db, pre_cb, post_cb) ⇒ Object
def write_prop(p) if p.klass.ancestors.include?(Integer) return “@#pp.symbol” elsif p.klass.ancestors.include?(Float) return “@#pp.symbol” elsif p.klass.ancestors.include?(String) return “#selfself.class.escape(@#pp.symbol)” elsif p.klass.ancestors.include?(Time) return %|#@@#{p@#{p.symbol ? “‘##{self#{self.class.timestamp(@#pp.symbol)}’” : ‘NULL’}| elsif p.klass.ancestors.include?(Date) return %|#@@#{p@#{p.symbol ? “‘##{self#{self.class.date(@#pp.symbol)}’” : ‘NULL’}| elsif p.klass.ancestors.include?(TrueClass) return “#@@#{p@#{p.symbol ? "‘t’" : ‘NULL’ }” else return %|#@@#{p@#{p.symbol ? “‘##{self#{self.class.escape(@#pp.symbol.to_yaml)}’” : “””}| end end.
- #new_connection(db) ⇒ Object
Methods inherited from Adapter
#create_db, #create_fields, date, encode, escape, #eval_og_insert, #eval_og_oid, #eval_og_read, #eval_og_update, for_name, #initialize, join_table, parse_date, parse_timestamp, #props_for_insert, #read_prop, table, timestamp, #write_prop
Constructor Details
This class inherits a constructor from Og::Adapter
Instance Method Details
#calc_field_index(klass, db) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/og/adapters/sqlite.rb', line 85 def calc_field_index(klass, db) res = db.query "SELECT * FROM #{klass::DBTABLE} LIMIT 1" = db.managed_classes[klass] columns = res.columns for idx in (0...columns.size) .field_index[columns[idx]] = idx end ensure res.close end |
#create_table(klass, db) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/og/adapters/sqlite.rb', line 99 def create_table(klass, db) conn = db.get_connection fields = create_fields(klass) sql = "CREATE TABLE #{klass::DBTABLE} (#{fields.join(', ')}" # Create table constrains if klass. and constrains = klass.[:sql_constrain] sql << ", #{constrains.join(', ')}" end sql << ");" # Create indices if klass. and indices = klass.[:sql_index] for data in indices idx, = *data idx = idx.to_s pre_sql, post_sql = [:pre], [:post] idxname = idx.gsub(/ /, "").gsub(/,/, "_").gsub(/\(.*\)/, "") sql << " CREATE #{pre_sql} INDEX #{klass::DBTABLE}_#{idxname}_idx #{post_sql} ON #{klass::DBTABLE} (#{idx});" end end begin conn.store.query(sql).close Logger.info "Created table '#{klass::DBTABLE}'." rescue Exception => ex # gmosx: any idea how to better test this? if ex.to_s =~ /table .* already exists/i Logger.debug "Table already exists" if $DBG return else raise end end # Create join tables if needed. Join tables are used in # 'many_to_many' relations. if klass. and joins = klass.[:sql_join] for data in joins # the class to join to and some options. join_class, = *data # gmosx: dont use DBTABLE here, perhaps the join class # is not managed yet. join_table = "#{self.class.join_table(klass, join_class)}" join_src = "#{self.class.encode(klass)}_oid" join_dst = "#{self.class.encode(join_class)}_oid" begin conn.store.query("CREATE TABLE #{join_table} ( key1 integer NOT NULL, key2 integer NOT NULL )").close conn.store.query("CREATE INDEX #{join_table}_key1_idx ON #{join_table} (key1)").close conn.store.query("CREATE INDEX #{join_table}_key2_idx ON #{join_table} (key2)").close rescue Exception => ex # gmosx: any idea how to better test this? if ex.to_s =~ /table .* already exists/i Logger.debug "Join table already exists" if $DBG else raise end end end end ensure db.put_connection end |
#drop_db(database, user = nil, password = nil) ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/og/adapters/sqlite.rb', line 20 def drop_db(database, user = nil, password = nil) begin FileUtils.rm("#{database}.db") super rescue Logger.error "Cannot drop '#{database}'!" end end |
#insert_code(klass, db, pre_cb, post_cb) ⇒ Object
def write_prop(p) if p.klass.ancestors.include?(Integer) return “@#Og::SqliteAdapter.pp.symbol” elsif p.klass.ancestors.include?(Float) return “@#Og::SqliteAdapter.pp.symbol” elsif p.klass.ancestors.include?(String) return “#Og::SqliteAdapter.selfself.class.escape(@#Og::SqliteAdapter.pp.symbol)” elsif p.klass.ancestors.include?(Time) return %|#Og::SqliteAdapter.@@#{p@#{p.symbol ? “‘#Og::SqliteAdapter#{self#{self.class.timestamp(@#Og::SqliteAdapter.pp.symbol)}’” : ‘NULL’}| elsif p.klass.ancestors.include?(Date) return %|#Og::SqliteAdapter.@@#{p@#{p.symbol ? “‘#Og::SqliteAdapter#{self#{self.class.date(@#Og::SqliteAdapter.pp.symbol)}’” : ‘NULL’}| elsif p.klass.ancestors.include?(TrueClass) return “#Og::SqliteAdapter.@@#{p@#{p.symbol ? "‘t’" : ‘NULL’ }” else return %|#Og::SqliteAdapter.@@#{p@#{p.symbol ? “‘#Og::SqliteAdapter#{self#{self.class.escape(@#Og::SqliteAdapter.pp.symbol.to_yaml)}’” : “””}| end end
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 |
# File 'lib/og/adapters/sqlite.rb', line 47 def insert_code(klass, db, pre_cb, post_cb) props = props_for_insert(klass) values = props.collect { |p| write_prop(p) }.join(',') sql = "INSERT INTO #{klass::DBTABLE} (#{props.collect {|p| p.name}.join(',')}) VALUES (#{values})" %{ #{pre_cb} conn.store.query("#{sql}").close @oid = conn.store.last_insert_row_id #{post_cb} } =begin props = props_for_insert(klass) placeholders = Array.new(props.size, '?').join(',') values = props.collect { |p| write_prop(p) }.join(',') sql = "INSERT INTO #{klass::DBTABLE} (#{props.collect {|p| p.name}.join(',')}) VALUES (#{placeholders})" klass.class_eval %{ cattr_accessor :og_insert_statement } klass.og_insert_statement = db.prepare(sql) %{ #{pre_cb} @@og_insert_statement.execute(#{values}) @oid = conn.store.last_insert_row_id #{post_cb} } =end end |
#new_connection(db) ⇒ Object
81 82 83 |
# File 'lib/og/adapters/sqlite.rb', line 81 def new_connection(db) return Og::SqliteConnection.new(db) end |