Skip to content

Commit

Permalink
Add extensive documentation to the ActiveRecord::AbstractAdapter. #2250
Browse files Browse the repository at this point in the history
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2371 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
Marcel Molina committed Sep 27, 2005
1 parent 3fa38c4 commit b9fa354
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 38 deletions.
@@ -1,4 +1,5 @@
module ActiveRecord
# The root class of all active record objects.
class Base
class ConnectionSpecification #:nodoc:
attr_reader :config, :adapter_method
Expand Down
@@ -1,11 +1,12 @@
module ActiveRecord
module ConnectionAdapters # :nodoc:
# TODO: Document me!
module DatabaseStatements
# Returns an array of record hashes with the column names as a keys and fields as values.
# Returns an array of record hashes with the column names as keys and
# column values as values.
def select_all(sql, name = nil) end

# Returns a record hash with the column names as a keys and fields as values.
# Returns a record hash with the column names as keys and column values
# as values.
def select_one(sql, name = nil) end

# Returns a single value from a record
Expand All @@ -21,8 +22,11 @@ def select_values(sql, name = nil)
result.map{ |v| v.values.first }
end

# Executes the statement
def execute(sql, name = nil) end
# Executes the SQL statement in the context of this connection.
# This abstract method raises a NotImplementedError.
def execute(sql, name = nil)
raise NotImplementedError, "execute is an abstract method"
end

# Returns the last auto-generated ID from the affected table.
def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) end
Expand Down Expand Up @@ -54,16 +58,23 @@ def begin_db_transaction() end
# Commits the transaction (and turns on auto-committing).
def commit_db_transaction() end

# Rolls back the transaction (and turns on auto-committing). Must be done if the transaction block
# raises an exception or returns false.
# Rolls back the transaction (and turns on auto-committing). Must be
# done if the transaction block raises an exception or returns false.
def rollback_db_transaction() end

def add_limit!(sql, options) #:nodoc:
# Alias for #add_limit_offset!.
def add_limit!(sql, options)
return unless options
add_limit_offset!(sql, options)
end

def add_limit_offset!(sql, options) #:nodoc:
# Appends +LIMIT+ and +OFFSET+ options to a SQL statement.
# This method *modifies* the +sql+ parameter.
# ===== Examples
# add_limit_offset!('SELECT * FROM suppliers', {:limit => 10, :offset => 50})
# generates
# SELECT * FROM suppliers LIMIT 10 OFFSET 50
def add_limit_offset!(sql, options)
return if options[:limit].nil?
sql << " LIMIT #{options[:limit]}"
sql << " OFFSET #{options[:offset]}" if options.has_key?(:offset) and !options[:offset].nil?
Expand Down
@@ -1,7 +1,8 @@
module ActiveRecord
module ConnectionAdapters # :nodoc:
# TODO: Document me!
module Quoting
# Quotes the column value to help prevent
# {SQL injection attacks}[http://en.wikipedia.org/wiki/SQL_injection].
def quote(value, column = nil)
case value
when String
Expand All @@ -22,10 +23,14 @@ def quote(value, column = nil)
end
end

# Quotes a string, escaping any ' (single quote) and \ (backslash)
# characters.
def quote_string(s)
s.gsub(/\\/, '\&\&').gsub(/'/, "''") # ' (for ruby-mode)
end

# Returns a quoted form of the column name. This is highly adapter
# specific.
def quote_column_name(name)
name
end
Expand Down
@@ -1,18 +1,23 @@
module ActiveRecord
module ConnectionAdapters #:nodoc:
class Column #:nodoc:
# An abstract definition of a column in a table.
class Column
attr_reader :name, :default, :type, :limit, :null
# The name should contain the name of the column, such as "name" in "name varchar(250)"
# The default should contain the type-casted default of the column, such as 1 in "count int(11) DEFAULT 1"
# The type parameter should either contain :integer, :float, :datetime, :date, :text, or :string
# The sql_type is just used for extracting the limit, such as 10 in "varchar(10)"

# Instantiates a new column in the table.
#
# +name+ is the column's name, as in <tt><b>supplier_id</b> int(11)</tt>.
# +default+ is the type-casted default value, such as <tt>sales_stage varchar(20) default <b>'new'</b></tt>.
# +sql_type+ is only used to extract the column's length, if necessary. For example, <tt>company_name varchar(<b>60</b>)</tt>.
# +null+ determines if this column allows +NULL+ values.
def initialize(name, default, sql_type = nil, null = true)
@name, @type, @null = name, simplified_type(sql_type), null
# have to do this one separately because type_cast depends on #type
@default = type_cast(default)
@limit = extract_limit(sql_type) unless sql_type.nil?
end

# Returns the Ruby class that corresponds to the abstract data type.
def klass
case type
when :integer then Fixnum
Expand All @@ -27,6 +32,7 @@ def klass
end
end

# Casts value (which is a String) to an appropriate instance.
def type_cast(value)
if value.nil? then return nil end
case type
Expand All @@ -44,14 +50,20 @@ def type_cast(value)
end
end

# Returns the human name of the column name.
#
# ===== Examples
# Column.new('sales_stage', ...).human_name #=> 'Sales stage'
def human_name
Base.human_attribute_name(@name)
end

# Used to convert from Strings to BLOBs
def string_to_binary(value)
value
end

# Used to convert from BLOBs to Strings
def binary_to_string(value)
value
end
Expand Down Expand Up @@ -108,7 +120,7 @@ def simplified_type(field_type)
end
end
end

class IndexDefinition < Struct.new(:table, :name, :unique, :columns) #:nodoc:
end

Expand All @@ -130,22 +142,58 @@ def add_column_options!(sql, options)
end
end

class TableDefinition #:nodoc:
# Represents a SQL table in an abstract way.
# Columns are stored as ColumnDefinition in the #columns attribute.
class TableDefinition
attr_accessor :columns

def initialize(base)
@columns = []
@base = base
end

# Appends a primary key definition to the table definition.
# Can be called multiple times, but this is probably not a good idea.
def primary_key(name)
column(name, native[:primary_key])
end


# Returns a ColumnDefinition for the column with name +name+.
def [](name)
@columns.find {|column| column.name == name}
end

# Instantiates a new column for the table.
# The +type+ parameter must be one of the following values:
# <tt>:primary_key</tt>, <tt>:string</tt>, <tt>:text</tt>,
# <tt>:integer</tt>, <tt>:float</tt>, <tt>:datetime</tt>,
# <tt>:timestamp</tt>, <tt>:time</tt>, <tt>:date</tt>,
# <tt>:binary</tt>, <tt>:boolean</tt>.
#
# Available options are (none of these exists by default):
# * <tt>:limit</tt>:
# Requests a maximum column length (<tt>:string</tt>, <tt>:text</tt>,
# <tt>:binary</tt> or <tt>:integer</tt> columns only)
# * <tt>:default</tt>:
# The column's default value. You cannot explicitely set the default
# value to +NULL+. Simply leave off this option if you want a +NULL+
# default value.
# * <tt>:null</tt>:
# Allows or disallows +NULL+ values in the column. This option could
# have been named <tt>:null_allowed</tt>.
#
# This method returns <tt>self</tt>.
#
# ===== Examples
# # Assuming def is an instance of TableDefinition
# def.column(:granted, :boolean)
# #=> granted BOOLEAN
#
# def.column(:picture, :binary, :limit => 2.megabytes)
# #=> picture BLOB(2097152)
#
# def.column(:sales_stage, :string, :limit => 20, :default => 'new', :null => false)
# #=> sales_stage VARCHAR(20) DEFAULT 'new' NOT NULL
def column(name, type, options = {})
column = self[name] || ColumnDefinition.new(@base, name, type)
column.limit = options[:limit] || native[type.to_sym][:limit] if options[:limit] or native[type.to_sym]
Expand All @@ -155,6 +203,9 @@ def column(name, type, options = {})
self
end

# Returns a String whose contents are the column definitions
# concatenated together. This string can then be pre and appended to
# to generate the final SQL to create the table.
def to_sql
@columns * ', '
end
Expand Down

0 comments on commit b9fa354

Please sign in to comment.