/
mysql_adapter.rb
139 lines (120 loc) · 4.11 KB
/
mysql_adapter.rb
1
2
3
4
5
6
7
8
9
10
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
45
46
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
module ActiveRecord::Comments::MysqlAdapter
def self.included base
base.extend ClassMethods
end
module ClassMethods
# MySQL implementation of ActiveRecord::Comments::BaseExt#comment
def mysql_comment table
table_options = create_table_sql(table).split("\n").last
if table_options =~ /COMMENT='/
/COMMENT='(.*)'/.match(table_options).captures.first
else
nil
end
end
# MySQL implementation of ActiveRecord::Comments::BaseExt#column_comment
def mysql_column_comment column, table
column_creation_sql = create_column_sql(column, table)
if column_creation_sql =~ /COMMENT '/
/COMMENT '(.*)'/.match(column_creation_sql).captures.first
else
nil
end
end
private
# Returns the SQL used to create the given table
#
# ==== Parameters
# table<~to_s>::
# The name of the table to get the 'CREATE TABLE' SQL for
#
# ==== Returns
# String:: the SQL used to create the table
#
# :api: private
def create_table_sql table = table_name
connection.execute("show create table `#{ table }`").all_hashes.first['Create Table']
end
# Returns the SQL used to create the given column for the given table
#
# ==== Parameters
# column<~to_s>::
# The name of the column to get the creation SQL for
#
# table<~to_s>::
# The name of the table to get the 'CREATE TABLE' SQL for
#
# ==== Returns
# String:: the SQL used to create the column
#
# :api: private
def create_column_sql column, table = table_name
full_table_create_sql = create_table_sql(table)
parts = full_table_create_sql.split("\n")
create_table = parts.shift # take off the first CREATE TABLE part
create_table_options = parts.pop # take off the last options for the table, leaving just the columns
sql_for_this_column = parts.find {|str| str =~ /^ *`#{ column }`/ }
sql_for_this_column.strip! if sql_for_this_column
sql_for_this_column
end
end
end
ActiveRecord::Base.send :include, ActiveRecord::Comments::MysqlAdapter
################ WE SHOULD DO EVERYTHING ON THE CONNECTION (ADAPTER) INSTEAD!!!!!!
module ActiveRecord::Comments::MysqlAdapterAdapter
# MySQL implementation of ActiveRecord::Comments::BaseExt#comment
def mysql_comment table
table_options = create_table_sql(table).split("\n").last
if table_options =~ /COMMENT='/
/COMMENT='(.*)'/.match(table_options).captures.first
else
nil
end
end
# MySQL implementation of ActiveRecord::Comments::BaseExt#column_comment
def mysql_column_comment column, table
column_creation_sql = create_column_sql(column, table)
if column_creation_sql =~ /COMMENT '/
/COMMENT '(.*)'/.match(column_creation_sql).captures.first
else
nil
end
end
private
# Returns the SQL used to create the given table
#
# ==== Parameters
# table<~to_s>::
# The name of the table to get the 'CREATE TABLE' SQL for
#
# ==== Returns
# String:: the SQL used to create the table
#
# :api: private
def create_table_sql table = table_name
execute("show create table `#{ table }`").all_hashes.first['Create Table']
end
# Returns the SQL used to create the given column for the given table
#
# ==== Parameters
# column<~to_s>::
# The name of the column to get the creation SQL for
#
# table<~to_s>::
# The name of the table to get the 'CREATE TABLE' SQL for
#
# ==== Returns
# String:: the SQL used to create the column
#
# :api: private
def create_column_sql column, table = table_name
full_table_create_sql = create_table_sql(table)
parts = full_table_create_sql.split("\n")
create_table = parts.shift # take off the first CREATE TABLE part
create_table_options = parts.pop # take off the last options for the table, leaving just the columns
sql_for_this_column = parts.find {|str| str =~ /^ *`#{ column }`/ }
sql_for_this_column.strip! if sql_for_this_column
sql_for_this_column
end
end
ActiveRecord::ConnectionAdapters::AbstractAdapter.send :include, ActiveRecord::Comments::MysqlAdapterAdapter