ironruby-dbi 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ === 0.1.0 / 2010-01-13
2
+
3
+ * First gem
@@ -0,0 +1,34 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ lib/dbd/mssql.rb
6
+ lib/dbd/mssql/database.rb
7
+ lib/dbd/mssql/driver.rb
8
+ lib/dbd/mssql/statement.rb
9
+ lib/dbd/mssql/types.rb
10
+ lib/dbi.rb
11
+ lib/dbi/base_classes.rb
12
+ lib/dbi/base_classes/database.rb
13
+ lib/dbi/base_classes/driver.rb
14
+ lib/dbi/base_classes/statement.rb
15
+ lib/dbi/binary.rb
16
+ lib/dbi/columninfo.rb
17
+ lib/dbi/exceptions.rb
18
+ lib/dbi/handles.rb
19
+ lib/dbi/handles/database.rb
20
+ lib/dbi/handles/driver.rb
21
+ lib/dbi/handles/statement.rb
22
+ lib/dbi/row.rb
23
+ lib/dbi/sql.rb
24
+ lib/dbi/sql/preparedstatement.rb
25
+ lib/dbi/sql_type_constants.rb
26
+ lib/dbi/trace.rb
27
+ lib/dbi/types.rb
28
+ lib/dbi/typeutil.rb
29
+ lib/dbi/utils.rb
30
+ lib/dbi/utils/date.rb
31
+ lib/dbi/utils/tableformatter.rb
32
+ lib/dbi/utils/time.rb
33
+ lib/dbi/utils/timestamp.rb
34
+ lib/dbi/utils/xmlformatter.rb
@@ -0,0 +1,21 @@
1
+ = IronRuby DBI
2
+
3
+ * http://github.com/casualjim/jschementi
4
+
5
+ == DESCRIPTION:
6
+
7
+ ADO.NET compatible DBD extension to ruby-dbo
8
+
9
+ == INSTALL:
10
+
11
+ * gem install ironruby-dbi
12
+
13
+ == DEVELOPERS:
14
+
15
+ After checking out the source, run:
16
+
17
+ $ rake newb
18
+
19
+ This task will install any missing dependencies, run the tests/specs,
20
+ and generate the RDoc.
21
+
@@ -0,0 +1,22 @@
1
+ require 'rubygems'
2
+ require 'hoe'
3
+
4
+ $:.unshift 'lib'
5
+ require 'dbd/mssql'
6
+
7
+ Hoe.spec 'ironruby-dbi' do
8
+ developer 'Ivan Porto Carrero', 'ivan@flanders.co.nz'
9
+ self.version = DBI::DBD::MSSQL::VERSION
10
+ self.url = 'http://github.com/casualjim/ironruby-dbi'
11
+ extra_deps << ['deprecated', '>=2.0.1']
12
+ end
13
+
14
+ desc "Tests the dbd driver for adonet"
15
+ task :test_dbd do
16
+ ruby("test/ts_dbd.rb")
17
+ end
18
+
19
+ task :test => [:test_dbd]
20
+
21
+ task :default => :test
22
+
@@ -0,0 +1,191 @@
1
+ require 'dbi'
2
+
3
+ module DBI
4
+ module DBD
5
+
6
+
7
+
8
+ module MSSQL
9
+
10
+ VERSION = "0.1.0"
11
+ USED_DBD_VERSION = "0.4.0"
12
+ DESCRIPTION = "ADO.NET DBI DBD"
13
+
14
+ require File.dirname(__FILE__) + "/mssql/types"
15
+
16
+ # Hash to translate MS SQL Server type names to DBI SQL type constants
17
+ #
18
+ # Only used in #mssql_type_info.
19
+ #
20
+ MSSQL_TO_XOPEN = {
21
+ "TINYINT" => [DBI::SQL_TINYINT, 1, nil],
22
+ "SMALLINT" => [DBI::SQL_SMALLINT, 2, nil],
23
+ "INT" => [DBI::SQL_INTEGER, 4, nil],
24
+ "INTEGER" => [DBI::SQL_INTEGER, 4, nil],
25
+ "BIGINT" => [DBI::SQL_BIGINT, 8, nil],
26
+ "REAL" => [DBI::SQL_REAL, 24, nil],
27
+ "FLOAT" => [DBI::SQL_FLOAT, 12, nil],
28
+ "DECIMAL" => [DBI::SQL_DECIMAL, 18, nil],
29
+ "NUMERIC" => [DBI::SQL_DECIMAL, 18, nil],
30
+ "MONEY" => [DBI::SQL_DECIMAL, 8, 4],
31
+ "SMALLMONEY" => [DBI::SQL_DECIMAL, 4, 4],
32
+ "DATE" => [DBI::SQL_DATE, 10, nil],
33
+ "TIME" => [DBI::SQL_TIME, 8, nil],
34
+ "DATETIME2" => [DBI::SQL_TIMESTAMP, 19, nil],
35
+ "DATETIME" => [DBI::SQL_TIMESTAMP, 19, nil],
36
+ "CHAR" => [DBI::SQL_CHAR, 1, nil],
37
+ "VARCHAR" => [DBI::SQL_VARCHAR, 255, nil],
38
+ "NCHAR" => [DBI::SQL_CHAR, 1, nil],
39
+ "NVARCHAR" => [DBI::SQL_VARCHAR, 255, nil],
40
+ "TEXT" => [DBI::SQL_VARCHAR, 65535, nil],
41
+ "NTEXT" => [DBI::SQL_VARCHAR, 131070, nil],
42
+ "BINARY" => [DBI::SQL_VARBINARY, 65535, nil],
43
+ "VARBINARY" => [DBI::SQL_VARBINARY, 16277215, nil],
44
+ "IMAGE" => [DBI::SQL_LONGVARBINARY, 2147483657, nil],
45
+ "BIT" => [DBI::SQL_BIT, 1, nil],
46
+ "UNIQUEIDENTIFIER" => [DBI::SQL_VARCHAR, 20, nil],
47
+ "XML" => [DBI::SQL_VARCHAR, 65535, nil],
48
+ "TIMESTAMP" => [DBI::SQL_VARCHAR, 18, nil],
49
+ nil => [DBI::SQL_OTHER, nil, nil]
50
+ }
51
+
52
+ MSSQL_TYPEMAP = {
53
+ "TINYINT" => DBI::Type::Integer,
54
+ "SMALLINT" => DBI::Type::Integer,
55
+ "INT" => DBI::Type::Integer,
56
+ "INTEGER" => DBI::Type::Integer,
57
+ "BIGINT" => DBI::Type::Integer,
58
+ "REAL" => DBI::Type::Float,
59
+ "FLOAT" => DBI::Type::Float,
60
+ "DECIMAL" => DBI::DBD::MSSQL::Type::Decimal,
61
+ "NUMERIC" => DBI::DBD::MSSQL::Type::Decimal,
62
+ "MONEY" => DBI::DBD::MSSQL::Type::Decimal,
63
+ "SMALLMONEY" => DBI::DBD::MSSQL::Type::Decimal,
64
+ "DATE" => DBI::DBD::MSSQL::Type::Date,
65
+ "TIME" => DBI::DBD::MSSQL::Type::Timestamp,
66
+ "DATETIME2" => DBI::DBD::MSSQL::Type::Timestamp,
67
+ "DATETIME" => DBI::DBD::MSSQL::Type::Timestamp,
68
+ "CHAR" => DBI::Type::Varchar,
69
+ "VARCHAR" => DBI::Type::Varchar,
70
+ "NCHAR" => DBI::Type::Varchar,
71
+ "NVARCHAR" => DBI::Type::Varchar,
72
+ "TEXT" => DBI::Type::Varchar,
73
+ "NTEXT" => DBI::Type::Varchar,
74
+ "BINARY" => DBI::Type::Varchar,
75
+ "VARBINARY" => DBI::Type::Varchar,
76
+ "IMAGE" => DBI::Type::Varchar,
77
+ "BIT" => DBI::Type::Boolean,
78
+ "UNIQUEIDENTIFIER" => DBI::Type::Varchar,
79
+ "XML" => DBI::Type::Varchar,
80
+ "TIMESTAMP" => DBI::Type::Varchar,
81
+ nil => DBI::DBD::MSSQL::Type::Null,
82
+ System::DBNull => DBI::DBD::MSSQL::Type::Null
83
+ }
84
+
85
+
86
+
87
+ def self.driver_name
88
+ "mssql"
89
+ end
90
+
91
+ DBI::TypeUtil.register_conversion(driver_name) do |obj|
92
+
93
+
94
+ newobj =
95
+ case obj
96
+ when ::DBI::Timestamp, ::Numeric, ::DBI::Binary
97
+ obj.to_s
98
+ when ::DateTime
99
+ "#{obj.strftime("%Y-%m-%d %H:%M:%S")}"
100
+ when ::Time
101
+ "#{obj.strftime("%H:%M:%S")}"
102
+ when ::Date
103
+ "#{obj.strftime("%Y-%m-%d")}"
104
+ when ::NilClass
105
+ System::DBNull.value
106
+ when ::String, System::String
107
+ obj
108
+ when ::TrueClass
109
+ "1"
110
+ when ::FalseClass
111
+ "0"
112
+ when ::BigDecimal
113
+ obj.to_s("F")
114
+ when ::Numeric
115
+ obj.to_s
116
+ else
117
+ obj
118
+ end
119
+
120
+ if newobj.object_id == obj.object_id and not (obj.is_a?(::String) || obj.is_a?(System::String))
121
+ [newobj, true]
122
+ else
123
+ [newobj, false]
124
+ end
125
+ end
126
+
127
+
128
+ CLR_TYPES = {
129
+ :TINYINT => "byte",
130
+ :SMALLINT => "short",
131
+ :BIGINT => "long",
132
+ :INT => "int",
133
+ :FLOAT => "double",
134
+ :REAL => "float",
135
+ :SMALLMONEY => "decimal",
136
+ :MONEY => "decimal",
137
+ :NUMERIC => "decimal",
138
+ :DECIMAL => "decimal",
139
+ :BIT => "bool",
140
+ :UNIQUEIDENTIFIER => "Guid",
141
+ :VARCHAR => "string",
142
+ :NVARCHAR => "string",
143
+ :TEXT => "string",
144
+ :NTEXT => "string",
145
+ :CHAR => "char",
146
+ :NCHAR => "char",
147
+ :VARBINARY => "byte[]",
148
+ :IMAGE => "byte[]",
149
+ :DATETIME => "DateTime"
150
+ }
151
+
152
+ SQL_TYPE_NAMES = {
153
+ :BIT => "BIT",
154
+ :TINYINT => "TINYINT",
155
+ :SMALLINT => "SMALLINT",
156
+ :INTEGER => "INTEGER",
157
+ :INT => "INTEGER",
158
+ :BIGINT => "BIGINT",
159
+ :FLOAT => "FLOAT",
160
+ :REAL => "REAL",
161
+ :DOUBLE => "DOUBLE",
162
+ :NUMERIC => "NUMERIC",
163
+ :DECIMAL => "DECIMAL",
164
+ :CHAR => "CHAR",
165
+ :NCHAR => "CHAR",
166
+ :VARCHAR => "VARCHAR",
167
+ :NVARCHAR => "VARCHAR",
168
+ :LONGVARCHAR => "LONG VARCHAR",
169
+ :TEXT => "LONG VARCHAR",
170
+ :NTEXT => "LONG VARCHAR",
171
+ :DATE => "DATE",
172
+ :DATETIME => "DATETIME",
173
+ :TIME => "TIME",
174
+ :TIMESTAMP => "TIMESTAMP",
175
+ :BINARY => "BINARY",
176
+ :VARBINARY => "VARBINARY",
177
+ :LONGVARBINARY => "LONG VARBINARY",
178
+ :IMAGE => "BLOB",
179
+ :BLOB => "BLOB",
180
+ :CLOB => "CLOB",
181
+ :OTHER => "",
182
+ :BOOLEAN => "BOOLEAN",
183
+ :UNIQUEIDENTIFIER => "VARCHAR"
184
+ }
185
+ end
186
+ end
187
+ end
188
+
189
+ require File.dirname(__FILE__) + "/mssql/driver"
190
+ require File.dirname(__FILE__) + "/mssql/database"
191
+ require File.dirname(__FILE__) + "/mssql/statement"
@@ -0,0 +1,148 @@
1
+ module DBI
2
+ module DBD
3
+ module MSSQL
4
+
5
+ class Database < DBI::BaseDatabase
6
+
7
+
8
+ def initialize(dbd_db, attr)
9
+ super
10
+ self['AutoCommit'] = true
11
+ end
12
+
13
+ def disconnect
14
+ unless @trans.nil?
15
+ @trans.rollback unless @attr['AutoCommit']
16
+ @trans = nil
17
+ end
18
+ @handle.close
19
+ rescue RuntimeError, System::Data::SqlClient::SqlException => err
20
+ raise DBI::DatabaseError.new(err.message)
21
+ end
22
+
23
+ def prepare(statement)
24
+ Statement.new(statement, self)
25
+ end
26
+
27
+ def ping
28
+ cmd = @handle.create_command
29
+ cmd.command_text = "Select 1"
30
+ begin
31
+ cmd.execute_non_query
32
+ return true
33
+ rescue
34
+ return false
35
+ end
36
+ rescue RuntimeError, System::Data::SqlClient::SqlException => err
37
+ raise DBI::DatabaseError.new(err.message)
38
+ end
39
+
40
+ def tables
41
+ @handle.get_schema("Tables").rows.collect { |row| row["TABLE_NAME"].to_s }
42
+ rescue RuntimeError, System::Data::SqlClient::SqlException => err
43
+ raise DBI::DatabaseError.new(err.message)
44
+ end
45
+
46
+ def current_transaction
47
+ @trans
48
+ end
49
+
50
+ def has_transaction?
51
+ !@trans.nil?
52
+ end
53
+
54
+ def columns(table)
55
+ sql = "select object_name(c.object_id) as table_name, c.column_id, c.name, type_name(system_type_id) as sql_type,
56
+ max_length, is_nullable, precision, scale, object_definition(c.default_object_id) as default_value,
57
+ convert(bit,(Select COUNT(*) from sys.indexes as i
58
+ inner join sys.index_columns as ic
59
+ on ic.index_id = i.index_id and ic.object_id = i.object_id
60
+ inner join sys.columns as c2 on ic.column_id = c2.column_id and i.object_id = c2.object_id
61
+ WHERE i.is_primary_key = 1 and ic.column_id = c.column_id and i.object_id=c.object_id)) as is_primary_key,
62
+ convert(bit,(Select COUNT(*) from sys.indexes as i
63
+ inner join sys.index_columns as ic
64
+ on ic.index_id = i.index_id and ic.object_id = i.object_id
65
+ inner join sys.columns as c2 on ic.column_id = c2.column_id and i.object_id = c2.object_id
66
+ WHERE i.is_primary_key = 0
67
+ and i.is_unique_constraint = 0 and ic.column_id = c.column_id and i.object_id=c.object_id)) as is_index,
68
+ convert(bit,(Select Count(*) from sys.indexes as i inner join sys.index_columns as ic
69
+ on ic.index_id = i.index_id and ic.object_id = i.object_id
70
+ inner join sys.columns as c2 on ic.column_id = c2.column_id and i.object_id = c2.object_id
71
+ WHERE (i.is_unique_constraint = 1) and ic.column_id = c.column_id and i.object_id=c.object_id)) as is_unique
72
+ from sys.columns as c
73
+ WHERE object_name(c.object_id) = @table_name
74
+ order by table_name"
75
+ stmt = prepare(sql)
76
+ stmt.bind_param("table_name", table)
77
+ stmt.execute
78
+ ret = stmt.fetch_all.collect do |row|
79
+ dtn = row[3].upcase
80
+ ColumnInfo.new({
81
+ 'name' => row[2].to_s,
82
+ 'dbi_type' => MSSQL_TYPEMAP[dtn],
83
+ 'mssql_type_name' => dtn,
84
+ 'sql_type' =>MSSQL_TO_XOPEN[dtn][0],
85
+ 'type_name' => DBI::SQL_TYPE_NAMES[MSSQL_TO_XOPEN[dtn][0]],
86
+ 'precision' => row[6].zero? ? row[4] : row[6],
87
+ 'default' => row[8],
88
+ 'scale' => row[7],
89
+ 'nullable' => row[5],
90
+ 'primary' => row[9],
91
+ 'indexed' => row[10],
92
+ 'unique' => row[11]
93
+ })
94
+ end
95
+ stmt.finish
96
+ ret
97
+ end
98
+
99
+ def commit
100
+ unless @attr['AutoCommit']
101
+ @trans.commit if @trans
102
+ @trans = @handle.begin_transaction
103
+ end
104
+ rescue RuntimeError, System::Data::SqlClient::SqlException => err
105
+ raise DBI::DatabaseError.new(err.message)
106
+ end
107
+
108
+ def rollback
109
+ unless @attr['AutoCommit']
110
+ @trans.rollback if @trans
111
+ @trans = @handle.begin_transaction
112
+ end
113
+ rescue RuntimeError, System::Data::SqlClient::SqlException => err
114
+ raise DBI::DatabaseError.new(err.message)
115
+ end
116
+
117
+ def do(stmt, bindvars={})
118
+ st = prepare(stmt)
119
+ bindvars.each { |k, v| st.bind_param(k, v) }
120
+ res = st.execute
121
+ st.finish
122
+ return res
123
+ rescue RuntimeError, System::Data::SqlClient::SqlException => err
124
+ raise DBI::DatabaseError.new(err.message)
125
+ ensure
126
+ st.finish if st
127
+ end
128
+
129
+ def current_connection
130
+ @handle
131
+ end
132
+
133
+ def []=(attr, value)
134
+ if attr == 'AutoCommit' and @attr[attr] != value
135
+ @trans.commit if @trans
136
+ unless value
137
+ @trans = @handle.begin_transaction unless @trans
138
+ else
139
+ @trans = nil
140
+ end
141
+ end
142
+ @attr[attr] = value
143
+ end
144
+
145
+ end # class Database
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,31 @@
1
+ module DBI
2
+ module DBD
3
+ module MSSQL
4
+
5
+ class Driver < DBI::BaseDriver
6
+
7
+ PROVIDER_KEY = :mssql
8
+
9
+ def initialize
10
+ super(USED_DBD_VERSION, PROVIDER_KEY)
11
+ end
12
+
13
+ def create_database(connection, attr)
14
+ Database.new(connection, attr)
15
+ end
16
+
17
+ private
18
+
19
+ def data_sources
20
+ conn = factory.create_connection
21
+ conn.open
22
+ ret = conn.get_schema("Databases").rows.collect { |db| db.to_s unless %w(master tempdb model msdb).include? db.to_s }
23
+ conn.close
24
+ ret
25
+ end
26
+
27
+ end
28
+
29
+ end
30
+ end
31
+ end