lib/swift/db.rb in swift-0.7.2 vs lib/swift/db.rb in swift-0.8.0

- old
+ new

@@ -8,66 +8,57 @@ def returning? false end end # Mysql - class Postgres < Adapter + class Sqlite3 < Adapter def initialize options = {} - super options.update(driver: 'postgresql') + super options.update(driver: 'sqlite3') end def returning? - true + false end + def migrate! scheme + keys = scheme.header.keys + serial = scheme.header.find(&:serial) + fields = scheme.header.map{|p| field_definition(p)}.join(', ') + fields += ", primary key (#{keys.join(', ')})" unless serial or keys.empty? + + execute("drop table if exists #{scheme.store}") + execute("create table #{scheme.store} (#{fields})") + end + def field_type attribute case attribute - when Type::IO then 'bytea' - else super + when Type::String then 'text' + when Type::Integer then attribute.serial ? 'integer primary key' : 'integer' + when Type::Float then 'float' + when Type::BigDecimal then 'numeric' + when Type::Time then 'timestamp' + when Type::Date then 'date' + when Type::Boolean then 'boolean' + when Type::IO then 'blob' + else 'text' end end - end # Postgres + end # Sqlite3 - class DB2 < Adapter + class Postgres < Adapter def initialize options = {} - super options.update(driver: 'db2') + super options.update(driver: 'postgresql') end def returning? - false + true end - def migrate! - keys = scheme.header.keys - fields = scheme.header.map{|p| field_definition(p)}.join(', ') - fields += ", primary key (#{keys.join(', ')})" unless keys.empty? - - sql = <<-SQL - select count(*) as exists from syscat.tables - where tabschema = CURRENT_SCEMA and tabname = '#{scheme.store.upcase}' - SQL - - execute(sql) {|result| execute("drop table #{scheme.store}") if result[:exists] > 0 } - execute("create table #{scheme.store} (#{fields})") - end - def field_type attribute case attribute - when Type::String then 'clob(2g)' - when Type::Integer then attribute.serial ? 'integer not null generated by default as identity' : 'integer' - when Type::Boolean then 'char(1)' - when Type::Float then 'real' - when Type::BigDecimal then 'double' + when Type::IO then 'bytea' else super end end - - def prepare_create scheme - prepare_cached(scheme, :create) do - values = (['?'] * scheme.header.insertable.size).join(', ') - sql = "insert into #{scheme.store} (#{scheme.header.insertable.join(', ')}) values (#{values})" - scheme.header.serial ? "select #{scheme.header.serial} from final table (#{sql})" : sql - end - end - end # DB2 + end # Postgres end # DB end # Swift