class KnjDB_sqlite3::Columns attr_reader :db, :driver def initialize(args) @args = args @db = @args[:db] @driver = @args[:driver] end def data_sql(data) raise "No type given." if !data["type"] type = data["type"] if type == "enum" type = "varchar" data.delete("maxlength") end data["maxlength"] = 255 if type == "varchar" and !data.key?("maxlength") type = "integer" if @db.int_types.index(type) and (data["autoincr"] or data["primarykey"]) sql = "`#{data["name"]}` #{type}" sql << "(#{data["maxlength"]})" if data["maxlength"] and !data["autoincr"] sql << "(11)" if !data.key?("maxlength") and !data["autoincr"] sql << " PRIMARY KEY" if data["primarykey"] sql << " NOT NULL" if !data["null"] and data.key?("null") if data.key?("default_func") sql << " DEFAULT #{data["default_func"]}" elsif data.key?("default") and data["default"] != false sql << " DEFAULT '#{@db.escape(data["default"])}'" end return sql end end class KnjDB_sqlite3::Columns::Column attr_reader :args def initialize(args) @args = args @db = @args[:db] end def name return @args[:data][:name] end def table return @db.tables[@args[:table_name]] end def data return { "type" => self.type, "name" => self.name, "null" => self.null?, "maxlength" => self.maxlength, "default" => self.default, "primarykey" => self.primarykey?, "autoincr" => self.autoincr? } end def type if !@type if match = @args[:data][:type].match(/^([A-z]+)$/) @maxlength = false type = match[0] elsif match = @args[:data][:type].match(/^decimal\((\d+),(\d+)\)$/) @maxlength = "#{match[1]},#{match[2]}" type = "decimal" elsif match = @args[:data][:type].match(/^enum\((.+)\)$/) @maxlength = match[1] type = "enum" elsif match = @args[:data][:type].match(/^(.+)\((\d+)\)$/) @maxlength = match[2] type = match[1] end if type == "integer" @type = "int" else @type = type end end return @type end def null? return false if @args[:data][:notnull].to_i == 1 return true end def maxlength self.type return @maxlength if @maxlength return false end def default def_val = @args[:data][:dflt_value] if def_val.to_s.slice(0..0) == "'" def_val = def_val.to_s.slice(0) end if def_val.to_s.slice(-1..-1) == "'" def_val = def_val.to_s.slice(0, def_val.length - 1) end return false if @args[:data][:dflt_value].to_s.length == 0 return def_val end def primarykey? return true if @args[:data][:name] == "id" return false if @args[:data][:pk].to_i == 0 return true end def autoincr? return true if @args[:data][:name] == "id" return true if @args[:data][:pk].to_i >= 1 return false end def drop @args[:table].copy( "drops" => self.name ) end def change(data) newdata = data.clone newdata["name"] = self.name if !newdata.key?("name") newdata["type"] = self.type if !newdata.key?("type") newdata["maxlength"] = self.maxlength if !newdata.key?("maxlength") and self.maxlength newdata["null"] = self.null? if !newdata.key?("null") newdata["default"] = self.default if !newdata.key?("default") newdata.delete("primarykey") if newdata.key?("primarykey") @type = nil @maxlength = nil new_table = self.table.copy( "alter_columns" => { self.name.to_s => newdata } ) end end