File: Mysql.rb

package info (click to toggle)
ruby-dbd-mysql 0.4.4%2Bgem2deb-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 292 kB
  • ctags: 159
  • sloc: ruby: 1,403; sql: 47; makefile: 2
file content (130 lines) | stat: -rw-r--r-- 4,353 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#--
# 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 'dbi'
require "mysql"
require "thread"   # for Mutex

module DBI
    module DBD
        #
        # DBD::Mysql - Database Driver for the MySQL database system.
        #
        # Requires DBI and the 'mysql' gem or package to work.
        #
        # Only things that extend DBI's results are documented.
        #
        module Mysql
            VERSION          = "0.4.4"
            DESCRIPTION      = "MySQL DBI DBD, Leverages 'mysql' low-level driver"

            MyError = ::MysqlError

            #
            # returns 'Mysql'
            # 
            # See DBI::TypeUtil#convert for more information.
            #
            def self.driver_name
                "Mysql"
            end

            DBI::TypeUtil.register_conversion(driver_name) do |obj|
                newobj = case obj
                         when ::DBI::Binary
                             obj = obj.to_s.gsub(/\\/) { "\\\\" }
                             obj = obj.to_s.gsub(/'/) { "''" }
                             "'#{obj}'"
                         when ::DateTime
                             "'#{obj.strftime("%Y-%m-%d %H:%M:%S")}'"
                         when ::Time
                             "'#{obj.strftime("%H:%M:%S")}'"
                         when ::Date
                             "'#{obj.strftime("%Y-%m-%d")}'"
                         when ::NilClass
                             "NULL"
                         else
                             obj
                         end

                if newobj.object_id == obj.object_id
                    [newobj, true]
                else
                    [newobj, false]
                end
            end
        end
    end
end

#
# Utility Methods for the MySQL DBD.
#

module DBI::DBD::Mysql::Util
    private

    # Raise exception using information from MysqlError object e.
    # For state value, use SQLSTATE value if mysql-ruby defines
    # sqlstate method, otherwise nil.
    def error(e)
        sqlstate = e.respond_to?("sqlstate") ? e.sqlstate : nil
        raise DBI::DatabaseError.new(e.message, e.errno, sqlstate)
    end

end # module Util

module DBI::DBD::Mysql::Type
    #
    # Custom handling for DATE types in MySQL. See DBI::Type for more
    # information.
    #
    class Date < DBI::Type::Null
        def self.parse(obj)
            obj = super
            return obj unless obj

            case obj.class
            when ::Date
                return obj
            when ::String
                return ::Date.strptime(obj, "%Y-%m-%d")
            else
                return ::Date.parse(obj.to_s)   if obj.respond_to? :to_s
                return ::Date.parse(obj.to_str) if obj.respond_to? :to_str
                return obj
            end
        end
    end
end

require 'dbd/mysql/driver'
require 'dbd/mysql/database'
require 'dbd/mysql/statement'