Class: Tina4::Drivers::MssqlDriver
- Inherits:
-
Object
- Object
- Tina4::Drivers::MssqlDriver
- Defined in:
- lib/tina4/drivers/mssql_driver.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Instance Method Summary collapse
- #apply_limit(sql, limit, offset = 0) ⇒ Object
- #begin_transaction ⇒ Object
- #close ⇒ Object
- #columns(table_name) ⇒ Object
- #commit ⇒ Object
- #connect(connection_string, username: nil, password: nil) ⇒ Object
- #execute(sql, params = []) ⇒ Object
- #execute_query(sql, params = []) ⇒ Object
- #last_insert_id ⇒ Object
- #placeholder ⇒ Object
- #placeholders(count) ⇒ Object
- #rollback ⇒ Object
- #tables ⇒ Object
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
6 7 8 |
# File 'lib/tina4/drivers/mssql_driver.rb', line 6 def connection @connection end |
Instance Method Details
#apply_limit(sql, limit, offset = 0) ⇒ Object
60 61 62 |
# File 'lib/tina4/drivers/mssql_driver.rb', line 60 def apply_limit(sql, limit, offset = 0) "#{sql} OFFSET #{offset} ROWS FETCH NEXT #{limit} ROWS ONLY" end |
#begin_transaction ⇒ Object
64 65 66 |
# File 'lib/tina4/drivers/mssql_driver.rb', line 64 def begin_transaction @connection.execute("BEGIN TRANSACTION").do end |
#close ⇒ Object
27 28 29 |
# File 'lib/tina4/drivers/mssql_driver.rb', line 27 def close @connection&.close end |
#columns(table_name) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/tina4/drivers/mssql_driver.rb', line 81 def columns(table_name) sql = "SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ?" rows = execute_query(sql, [table_name]) rows.map do |r| { name: r[:COLUMN_NAME] || r[:column_name], type: r[:DATA_TYPE] || r[:data_type], nullable: (r[:IS_NULLABLE] || r[:is_nullable]) == "YES", default: r[:COLUMN_DEFAULT] || r[:column_default], primary_key: false } end end |
#commit ⇒ Object
68 69 70 |
# File 'lib/tina4/drivers/mssql_driver.rb', line 68 def commit @connection.execute("COMMIT").do end |
#connect(connection_string, username: nil, password: nil) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/tina4/drivers/mssql_driver.rb', line 8 def connect(connection_string, username: nil, password: nil) begin require "tiny_tds" rescue LoadError raise LoadError, "The 'tiny_tds' gem is required for MSSQL connections. Install one of:\n" \ " bundle add tiny_tds # if your project uses Bundler\n" \ " gem install tiny_tds # bare driver" end uri = parse_connection(connection_string) @connection = TinyTds::Client.new( host: uri[:host], port: uri[:port] || 1433, username: username || uri[:username], password: password || uri[:password], database: uri[:database] ) end |
#execute(sql, params = []) ⇒ Object
39 40 41 42 43 |
# File 'lib/tina4/drivers/mssql_driver.rb', line 39 def execute(sql, params = []) effective_sql = interpolate_params(sql, params) result = @connection.execute(effective_sql) result.do end |
#execute_query(sql, params = []) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/tina4/drivers/mssql_driver.rb', line 31 def execute_query(sql, params = []) effective_sql = interpolate_params(sql, params) result = @connection.execute(effective_sql) rows = result.each(symbolize_keys: true).to_a result.cancel if result.respond_to?(:cancel) rows end |
#last_insert_id ⇒ Object
45 46 47 48 49 50 |
# File 'lib/tina4/drivers/mssql_driver.rb', line 45 def last_insert_id result = @connection.execute("SELECT SCOPE_IDENTITY() AS id") row = result.first result.cancel if result.respond_to?(:cancel) row[:id]&.to_i end |
#placeholder ⇒ Object
52 53 54 |
# File 'lib/tina4/drivers/mssql_driver.rb', line 52 def placeholder "?" end |
#placeholders(count) ⇒ Object
56 57 58 |
# File 'lib/tina4/drivers/mssql_driver.rb', line 56 def placeholders(count) (["?"] * count).join(", ") end |
#rollback ⇒ Object
72 73 74 |
# File 'lib/tina4/drivers/mssql_driver.rb', line 72 def rollback @connection.execute("ROLLBACK").do end |
#tables ⇒ Object
76 77 78 79 |
# File 'lib/tina4/drivers/mssql_driver.rb', line 76 def tables rows = execute_query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'") rows.map { |r| r[:TABLE_NAME] || r[:table_name] } end |