sql_server 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.textile +7 -0
- data/README.textile +58 -0
- data/lib/SqlServer/version.rb +3 -0
- data/lib/SqlServer.rb +76 -0
- metadata +76 -0
data/CHANGELOG.textile
ADDED
data/README.textile
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
h1. SqlServer
|
2
|
+
|
3
|
+
|_.Author|Ed Botzum ("contact@edbotz.us":mailto:contact@edbotz.us)|
|
4
|
+
|_.Copyright|Copyright (c) 2010 edbotz|
|
5
|
+
|_.License|MIT|
|
6
|
+
|
7
|
+
Used to interact with Microsoft SQL Server.
|
8
|
+
Based on the work of "David Mullet":http://rubyonwindows.blogspot.com/2007/03/ruby-ado-and-sqlserver.html and "Miro's":http://thinkwrap.wordpress.com/2007/04/25/accessing-sql-server-2005-from-ruby/ "snippet":http://snippets.dzone.com/posts/show/3906
|
9
|
+
|
10
|
+
h2. TODOS:
|
11
|
+
|
12
|
+
* Add support to execute commands.
|
13
|
+
|
14
|
+
h2. Rquirements
|
15
|
+
|
16
|
+
Only runs on Windows because it takes advantage of MDAC for best performace and ease of use.
|
17
|
+
|
18
|
+
* Win32 Ole - "http://rubygems.org/gems/win32ole-pp":http://rubygems.org/gems/win32ole-pp
|
19
|
+
* Microsoft Data Access Components - "http://msdn2.microsoft.com/en-us/data/aa937730.aspx":http://msdn2.microsoft.com/en-us/data/aa937730.aspx
|
20
|
+
|
21
|
+
h2. Sample Usage
|
22
|
+
|
23
|
+
<pre>
|
24
|
+
db = SqlServer.new( { :host => '10.2.1.135',
|
25
|
+
:user_name => 'user',
|
26
|
+
:password => 'password',
|
27
|
+
:database => 'database' } )
|
28
|
+
|
29
|
+
names = db.query('SELECT first, last, middle FROM names')
|
30
|
+
names.each { |name| puts "#{name['last']}, #{name['first']} #{name['middle']}" }
|
31
|
+
</pre>
|
32
|
+
|
33
|
+
h2. License
|
34
|
+
|
35
|
+
<pre>
|
36
|
+
Copyright (c) 2010 Ed Botzum
|
37
|
+
|
38
|
+
Permission is hereby granted, free of charge, to any person
|
39
|
+
obtaining a copy of this software and associated documentation
|
40
|
+
files (the "Software"), to deal in the Software without
|
41
|
+
restriction, including without limitation the rights to use,
|
42
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
43
|
+
copies of the Software, and to permit persons to whom the
|
44
|
+
Software is furnished to do so, subject to the following
|
45
|
+
conditions:
|
46
|
+
|
47
|
+
The above copyright notice and this permission notice shall be
|
48
|
+
included in all copies or substantial portions of the Software.
|
49
|
+
|
50
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
51
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
52
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
53
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
54
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
55
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
56
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
57
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
58
|
+
</pre>
|
data/lib/SqlServer.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
#!/usr/bin/env ruby -wKU
|
2
|
+
require 'win32ole'
|
3
|
+
|
4
|
+
class << Hash
|
5
|
+
def create(keys, values)
|
6
|
+
self[*keys.zip(values).flatten]
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class SqlServer
|
11
|
+
attr_accessor :connection
|
12
|
+
|
13
|
+
def initialize(params = {})
|
14
|
+
# Set the connection parameters
|
15
|
+
params = { :host => nil, :user_name => nil, :password => nil,
|
16
|
+
:database => nil, :timeout => nil }.merge!(params)
|
17
|
+
@connection = open(params)
|
18
|
+
end
|
19
|
+
|
20
|
+
def query(sql)
|
21
|
+
# Create an instance of an ADO Recordset
|
22
|
+
recordset = WIN32OLE.new('ADODB.Recordset')
|
23
|
+
|
24
|
+
# Open the recordset, using an SQL statement and the
|
25
|
+
# existing ADO connection
|
26
|
+
recordset.Open(sql, @connection)
|
27
|
+
|
28
|
+
# Create and populate an array of field names
|
29
|
+
fields = []
|
30
|
+
data = []
|
31
|
+
recordset.Fields.each do |field|
|
32
|
+
fields << field.Name
|
33
|
+
end
|
34
|
+
|
35
|
+
begin
|
36
|
+
# Move to the first record/row, if any exist
|
37
|
+
recordset.MoveFirst
|
38
|
+
|
39
|
+
# Grab all records
|
40
|
+
data = recordset.GetRows
|
41
|
+
rescue Exception => e
|
42
|
+
# Do nothing, it's an empty recordset
|
43
|
+
end
|
44
|
+
recordset.Close
|
45
|
+
|
46
|
+
# An ADO Recordset's GetRows method returns an array
|
47
|
+
# of columns, so we'll use the transpose method to
|
48
|
+
# convert it to an array of rows
|
49
|
+
data = data.transpose
|
50
|
+
|
51
|
+
result_set = []
|
52
|
+
data.each { |row| result_set << Hash.create(fields, row) }
|
53
|
+
|
54
|
+
return result_set
|
55
|
+
end
|
56
|
+
|
57
|
+
def close
|
58
|
+
@connection.Close
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def open(params)
|
64
|
+
connection_string = "Provider=SQLNCLI;"
|
65
|
+
connection_string << "Server=#{params[:host]};"
|
66
|
+
connection_string << "Uid=#{params[:user_name]};"
|
67
|
+
connection_string << "Pwd=#{params[:password]};"
|
68
|
+
connection_string << "Database=#{params[:database]};"
|
69
|
+
|
70
|
+
connection = WIN32OLE.new('ADODB.Connection')
|
71
|
+
connection.Open(connection_string)
|
72
|
+
connection.CommandTimeout = params[:timeout] unless params[:timeout].nil?
|
73
|
+
|
74
|
+
return connection
|
75
|
+
end
|
76
|
+
end
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sql_server
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
version: 0.0.2
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Ed Botzum
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-04-22 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: win32ole-pp
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :runtime
|
31
|
+
version_requirements: *id001
|
32
|
+
description: Uses ole to interact with the Mocrosoft ADO objects and interface with Microsft SQL Server.
|
33
|
+
email: contact@edbotz.us
|
34
|
+
executables: []
|
35
|
+
|
36
|
+
extensions: []
|
37
|
+
|
38
|
+
extra_rdoc_files: []
|
39
|
+
|
40
|
+
files:
|
41
|
+
- lib/SqlServer/version.rb
|
42
|
+
- lib/SqlServer.rb
|
43
|
+
- README.textile
|
44
|
+
- CHANGELOG.textile
|
45
|
+
has_rdoc: true
|
46
|
+
homepage: http://ebot.github.com/sql-server
|
47
|
+
licenses: []
|
48
|
+
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
segments:
|
66
|
+
- 0
|
67
|
+
version: "0"
|
68
|
+
requirements: []
|
69
|
+
|
70
|
+
rubyforge_project:
|
71
|
+
rubygems_version: 1.3.6
|
72
|
+
signing_key:
|
73
|
+
specification_version: 3
|
74
|
+
summary: Fast and easy Microsoft Sql Server querying.
|
75
|
+
test_files: []
|
76
|
+
|