Class: RDBI::Driver::MySQL::Database
- Inherits:
-
RDBI::Database
- Object
- RDBI::Database
- RDBI::Driver::MySQL::Database
- Extended by:
- MethLab
- Defined in:
- lib/rdbi/driver/mysql.rb
Instance Attribute Summary collapse
-
#cast_booleans ⇒ Object
Returns the value of attribute cast_booleans.
-
#my_conn ⇒ Object
readonly
Returns the value of attribute my_conn.
Instance Method Summary collapse
- #commit ⇒ Object
- #disconnect ⇒ Object
-
#initialize(*args) ⇒ Database
constructor
A new instance of Database.
- #new_statement(query) ⇒ Object
- #ping ⇒ Object
- #rollback ⇒ Object
- #schema ⇒ Object
- #table_schema(table_name) ⇒ Object
- #transaction(&block) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Database
Returns a new instance of Database.
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 |
# File 'lib/rdbi/driver/mysql.rb', line 69 def initialize(*args) super(*args) args = args[0] self.database_name = args[:database] || args[:dbname] || args[:db] username = args[:username] || args[:user] password = args[:password] || args[:pass] # FIXME flags? raise ArgumentError, "database name not provided" unless self.database_name raise ArgumentError, "username not provided" unless username @cast_booleans = args[:cast_booleans] || false @my_conn = if args[:host] || args[:hostname] Mysql.connect( args[:host] || args[:hostname], username, password, self.database_name, args[:port] ) elsif args[:sock] || args[:socket] Mysql.connect( nil, username, password, self.database_name, nil, args[:sock] || args[:socket] ) else raise ArgumentError, "either :host, :hostname, :socket, or :sock must be provided as a connection argument" end @preprocess_quoter = proc do |x, named, indexed| @my_conn.quote((named[x] || indexed[x]).to_s) end end |
Instance Attribute Details
#cast_booleans ⇒ Object
Returns the value of attribute cast_booleans.
67 68 69 |
# File 'lib/rdbi/driver/mysql.rb', line 67 def cast_booleans @cast_booleans end |
#my_conn ⇒ Object (readonly)
Returns the value of attribute my_conn.
66 67 68 |
# File 'lib/rdbi/driver/mysql.rb', line 66 def my_conn @my_conn end |
Instance Method Details
#commit ⇒ Object
134 135 136 137 138 139 140 |
# File 'lib/rdbi/driver/mysql.rb', line 134 def commit if ! in_transaction? raise RDBI::TransactionError.new( "Cannot commit when not in a transaction" ) end @my_conn.commit super end |
#disconnect ⇒ Object
112 113 114 115 |
# File 'lib/rdbi/driver/mysql.rb', line 112 def disconnect @my_conn.close super end |
#new_statement(query) ⇒ Object
142 143 144 |
# File 'lib/rdbi/driver/mysql.rb', line 142 def new_statement(query) Statement.new(query, self) end |
#ping ⇒ Object
198 199 200 201 202 203 204 205 |
# File 'lib/rdbi/driver/mysql.rb', line 198 def ping begin @my_conn.ping return 1 rescue raise RDBI::DisconnectedError, "not connected" end end |
#rollback ⇒ Object
126 127 128 129 130 131 132 |
# File 'lib/rdbi/driver/mysql.rb', line 126 def rollback if ! in_transaction? raise RDBI::TransactionError.new( "Cannot rollback when not in a transaction" ) end @my_conn.rollback super end |
#schema ⇒ Object
189 190 191 192 193 194 195 196 |
# File 'lib/rdbi/driver/mysql.rb', line 189 def schema schemata = [] execute( "SELECT table_name FROM information_schema.tables where table_schema = ?", self.database_name ) do |res| schemata = res.fetch( :all ) end schemata.collect { |x| table_schema(x[0]) } end |
#table_schema(table_name) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/rdbi/driver/mysql.rb', line 146 def table_schema( table_name ) info_row = nil execute( "SELECT table_type FROM information_schema.tables WHERE table_schema = ? and table_name = ?", self.database_name, table_name.to_s ) do |res| info_row = res.fetch(1)[0] end if info_row.nil? return nil end sch = RDBI::Schema.new( [], [] ) sch.tables << table_name.to_sym case info_row[ 0 ] when 'BASE TABLE' sch.type = :table when 'VIEW' sch.type = :view end execute( "SELECT column_name, data_type, is_nullable FROM information_schema.columns WHERE table_schema = ? and table_name = ?", self.database_name, table_name.to_s ) do |res| res.fetch( :all ).each do |row| col = RDBI::Column.new col.name = row[0].to_sym col.type = row[1].to_sym # TODO: ensure this ruby_type is solid, especially re: dates and times col.ruby_type = row[1].to_sym col.nullable = row[2] == "YES" sch.columns << col end end sch end |
#transaction(&block) ⇒ Object
117 118 119 120 121 122 123 124 |
# File 'lib/rdbi/driver/mysql.rb', line 117 def transaction(&block) if in_transaction? raise RDBI::TransactionError.new( "Already in transaction (not supported by MySQL)" ) end @my_conn.autocommit(false) super &block @my_conn.autocommit(true) end |