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
53 54 55 |
# File 'lib/tina4/drivers/mssql_driver.rb', line 53 def apply_limit(sql, limit, offset = 0) "#{sql} OFFSET #{offset} ROWS FETCH NEXT #{limit} ROWS ONLY" end |
#begin_transaction ⇒ Object
57 58 59 |
# File 'lib/tina4/drivers/mssql_driver.rb', line 57 def begin_transaction @connection.execute("BEGIN TRANSACTION").do end |
#close ⇒ Object
20 21 22 |
# File 'lib/tina4/drivers/mssql_driver.rb', line 20 def close @connection&.close end |
#columns(table_name) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/tina4/drivers/mssql_driver.rb', line 74 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
61 62 63 |
# File 'lib/tina4/drivers/mssql_driver.rb', line 61 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 |
# File 'lib/tina4/drivers/mssql_driver.rb', line 8 def connect(connection_string, username: nil, password: nil) require "tiny_tds" 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
32 33 34 35 36 |
# File 'lib/tina4/drivers/mssql_driver.rb', line 32 def execute(sql, params = []) effective_sql = interpolate_params(sql, params) result = @connection.execute(effective_sql) result.do end |
#execute_query(sql, params = []) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/tina4/drivers/mssql_driver.rb', line 24 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
38 39 40 41 42 43 |
# File 'lib/tina4/drivers/mssql_driver.rb', line 38 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
45 46 47 |
# File 'lib/tina4/drivers/mssql_driver.rb', line 45 def placeholder "?" end |
#placeholders(count) ⇒ Object
49 50 51 |
# File 'lib/tina4/drivers/mssql_driver.rb', line 49 def placeholders(count) (["?"] * count).join(", ") end |
#rollback ⇒ Object
65 66 67 |
# File 'lib/tina4/drivers/mssql_driver.rb', line 65 def rollback @connection.execute("ROLLBACK").do end |
#tables ⇒ Object
69 70 71 72 |
# File 'lib/tina4/drivers/mssql_driver.rb', line 69 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 |