Class: KnjDB_sqlite3::Columns

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

Overview

This class handels the SQLite3-specific behaviour for columns.

Defined Under Namespace

Classes: Column

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Columns

Constructor. This should not be called manually.



6
7
8
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_columns.rb', line 6

def initialize(args)
  @args = args
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



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

def db
  @db
end

Instance Method Details

#data_sql(data) ⇒ Object

Returns SQL for a knjdb-compatible hash.



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
39
40
41
42
43
44
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_columns.rb', line 11

def data_sql(data)
  raise "No type given." if !data["type"]
  type = data["type"].to_s
  
  if type == "enum"
    type = "varchar"
    data.delete("maxlength")
  end
  
  data["maxlength"] = 255 if type == "varchar" and !data.key?("maxlength")
  data["maxlength"] = 11 if type == "int" and !data.key?("maxlength") and !data["autoincr"] and !data["primarykey"]
  type = "integer" if @args[: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 << " PRIMARY KEY" if data["primarykey"]
  sql << " AUTOINCREMENT" if data["autoincr"]
  
  if !data["null"] and data.key?("null")
    sql << " NOT NULL"
    
    if !data.key?("default") or !data["default"]
      data["default"] = 0 if type == "int"
    end
  end
  
  if data.key?("default_func")
    sql << " DEFAULT #{data["default_func"]}"
  elsif data.key?("default") and data["default"] != false
    sql << " DEFAULT '#{@args[:db].escape(data["default"])}'"
  end
  
  return sql
end