其实很简单,我把ADO.rb给改了,加了编码转换,这样rails端使用起来就不需要做太特殊修改了。
#
# DBD::ADO
#
# Copyright (c) 2001, 2002 Michael Neumann <neumann@s-direktnet.de>
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
# THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# $Id$
#
require "win32ole"
require "iconv"
module DBI
module DBD
module ADO
VERSION = "0.1"
USED_DBD_VERSION = "0.1"
class Driver < DBI::BaseDriver
def initialize
super(USED_DBD_VERSION)
end
def connect(dbname, user, auth, attr)
# connect to database
handle = WIN32OLE.new('ADODB.Connection')
handle.Open(dbname)
handle.BeginTrans() # start new Transaction
return Database.new(handle, attr)
rescue RuntimeError => err
raise DBI::DatabaseError.new(@iconv_out.iconv(err.message))
end
end
class Database < DBI::BaseDatabase
def disconnect
@handle.RollbackTrans()
@handle.Close()
rescue RuntimeError => err
raise DBI::DatabaseError.new(@iconv_out.iconv(err.message))
end
def prepare(statement)
# TODO: create Command instead?
Statement.new(@handle, statement, self)
end
def commit
# TODO: raise error if AutoCommit on => better in DBI?
@handle.CommitTrans()
@handle.BeginTrans()
rescue RuntimeError => err
raise DBI::DatabaseError.new(@iconv_out.iconv(err.message))
end
def rollback
# TODO: raise error if AutoCommit on => better in DBI?
@handle.RollbackTrans()
@handle.BeginTrans()
rescue RuntimeError => err
raise DBI::DatabaseError.new(@iconv_out.iconv(err.message))
end
def []=(attr, value)
if attr == 'AutoCommit' then
# TODO: commit current transaction?
@attr[attr] = value
else
super
end
end
end # class Database
class Statement < DBI::BaseStatement
include SQL::BasicBind
include SQL::BasicQuote
def initialize(handle, statement, db)
@iconv_in = Iconv.new('gb18030','utf-8')
@iconv_out = Iconv.new('utf-8','gb18030')
@handle = handle
@statement = @iconv_in.iconv(statement)
@params = []
@db = db
end
def bind_param(param, value, attribs)
raise InterfaceError, "only ? parameters supported" unless param.is_a? Fixnum
@params[param-1] = value
end
def execute
# TODO: use Command and Parameter
# TODO: substitute all ? by the parametes
#sql = bind(self, @statement, @params)
@res_handle = @handle.Execute(@statement)
# TODO: SELECT and AutoCommit finishes the result-set
# what to do?
if @db['AutoCommit'] == true and not SQL.query?(@statement) then
@db.commit
end
rescue RuntimeError => err
raise DBI::DatabaseError.new(@iconv_out.iconv(err.message))
end
def finish
# if DCL, DDL or INSERT UPDATE and DELETE, this gives an Error
# because no Result-Set is available
if @res_handle.Fields.Count() != 0 then
@res_handle.Close()
end
rescue RuntimeError => err
raise DBI::DatabaseError.new(err.message)
end
def fetch
retval = fetch_currentrow
@res_handle.MoveNext() unless retval.nil?
retval
rescue RuntimeError => err
raise DBI::DatabaseError.new(@iconv_out.iconv(err.message))
end
def fetch_scroll(direction, offset)
case direction
when DBI::SQL_FETCH_NEXT
return fetch
when DBI::SQL_FETCH_PRIOR
# TODO: check if already the first?
#return nil if @res_handle.AbsolutePosition()
@res_handle.MovePrevious()
return fetch_currentrow
when DBI::SQL_FETCH_FIRST
@res_handle.MoveFirst()
return fetch_currentrow
when DBI::SQL_FETCH_LAST
@res_handle.MoveLast()
return fetch_currentrow
when DBI::SQL_FETCH_RELATIVE
@res_handle.Move(offset)
return fetch_currentrow
when DBI::SQL_FETCH_ABSOLUTE
ap = @res_handle.AbsolutePositon()
@res_handle.Move(offset-ap)
return fetch_currentrow
else
raise DBI::InterfaceError
end
rescue RuntimeError => err
raise DBI::DatabaseError.new(@iconv_out.iconv(err.message))
end
def column_info
num_cols = @res_handle.Fields().Count()
retval = Array.new(num_cols)
for i in 0...num_cols do
retval[i] = {'name' => @iconv_out.iconv(@res_handle.Fields(i).Name())}
end
retval
rescue RuntimeError => err
raise DBI::DatabaseError.new(@iconv_out.iconv(err.message))
end
def rows
# TODO: how to get the RPC in ADO?
nil
end
private
def fetch_currentrow
return nil if @res_handle.EOF() or @res_handle.BOF()
# TODO: don't create new Array each time
num_cols = @res_handle.Fields().Count()
retval = Array.new(num_cols)
for i in 0...num_cols do
if @res_handle.Fields(i).Value().is_a? String
retval[i] = @iconv_out.iconv(@res_handle.Fields(i).Value())
else
retval[i] = @res_handle.Fields(i).Value()
end
end
retval
end
end
end # module ADO
end # module DBD
end # module DBI
用上面的文件内容替换掉原来的ADO.rb。如果你没装过ADO.rb,直接把内容另存为ruby\lib\ruby\site_ruby\1.8\DBD\ADO\ADO.rb就可以了。
分享到:
相关推荐
在这个实例中,我们将探讨如何在Rails应用中使用SQLite3数据库,通过`sqlite3.exe`这个小巧而强大的数据库管理工具来实现数据存储。SQLite3是嵌入式的数据库引擎,无需单独的服务器进程,非常适合用于开发阶段或者轻...
此外,数据库连接也可以作为桥梁,比如都使用SQL Server,那么数据共享就变得相对简单。 在实践这些集成技巧时,开发环境的设置也很关键。Visual Studio提供了强大的跨语言支持,可以方便地调试和测试这些集成方案...
- **小型项目或模块**:对于规模较小的应用程序或者特定的功能模块,SubSonic能够提供简洁高效的解决方案。 - **已有.NET项目的扩展**:对于已经使用ASP.NET框架的项目,SubSonic能够轻松集成,提高开发效率。 ####...
OMToolkit 是一个集成 Web 服务器、Web 框架以及对象-关系数据库的工具包,旨在简化开发过程,提供一站式的解决方案。通过这个工具,开发者可以快速构建基于 Web 的应用程序,同时利用面向对象的设计思想来处理数据...
它不仅为开发者提供了高效的开发环境,同时也为企业提供了灵活的应用部署解决方案。PaaS的核心价值在于能够帮助用户或企业快速构建所需的软件应用和服务,而无需关注底层基础设施的细节。 PaaS服务通常包括一系列...