Subversion Repositories DevTools

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1293 dpurdie 1
###############################################################################
2
# Codestriker: Copyright (c) 2001, 2002 David Sitsky.  All rights reserved.
3
# sits@users.sourceforge.net
4
#
5
# This program is free software; you can redistribute it and modify it under
6
# the terms of the GPL.
7
 
8
package Codestriker::DB::ODBC;
9
 
10
use strict;
11
use DBI;
12
use Codestriker;
13
use Codestriker::DB::Database;
14
 
15
# Module for handling an ODBC database.
16
 
17
@Codestriker::DB::ODBC::ISA = ("Codestriker::DB::Database");
18
 
19
# Type mappings.
20
my $_TYPE = {
21
    $Codestriker::DB::Column::TYPE->{TEXT}	=> "ntext",
22
    $Codestriker::DB::Column::TYPE->{VARCHAR}	=> "nvarchar",
23
    $Codestriker::DB::Column::TYPE->{INT32}	=> "int",
24
    $Codestriker::DB::Column::TYPE->{INT16}	=> "smallint",
25
    $Codestriker::DB::Column::TYPE->{DATETIME}	=> "datetime",
26
    $Codestriker::DB::Column::TYPE->{FLOAT}	=> "float"
27
};
28
 
29
# Create a new ODBC database object.
30
sub new {
31
    my $type = shift;
32
 
33
    # Database is parent class.
34
    my $self = Codestriker::DB::Database->new();
35
    return bless $self, $type;
36
}
37
 
38
# Return the DBD module this is dependent on.
39
sub get_module_dependencies {
40
    return { name => 'DBD::ODBC', version => '0' };
41
}
42
 
43
# Retrieve a database connection.
44
sub get_connection {
45
    my $self = shift;
46
 
47
    # ODBC implementations support transactions, don't enable auto_commit.
48
    return $self->_get_connection(0, 1);
49
}
50
 
51
# Return the mapping for a specific type.
52
sub _map_type {
53
    my ($self, $type) = @_;
54
    return $_TYPE->{$type};
55
}
56
 
57
# Autoincrement type for ODBC.
58
sub _get_autoincrement_type {
59
    return "IDENTITY";
60
}
61
 
62
# Method for retrieving the list of current tables attached to the database.
63
# For ODBC for SQL SERVER, $dbh->tables doesn't work, need to retrieve data
64
# from the sysobjects table.
65
sub get_tables() {
66
    my $self = shift;
67
 
68
    my @tables = ();
69
    my $table_select = $self->{dbh}->table_info();
70
    while (my ($qual, $owner, $table_name, $type, $remarks) =
71
	   $table_select->fetchrow_array()) {
72
	push @tables, $table_name;
73
    }
74
    $table_select->finish();
75
 
76
    return @tables;
77
}
78
 
79
# Add a field to a specific table.  If the field already exists, then catch
80
# the error and continue silently.  The SYNTAX for SQL Server is slightly
81
# different to standard SQL, there is no "COLUMN" keyword after "ADD".
82
sub add_field {
83
    my ($self, $table, $field, $definition) = @_;
84
 
85
    my $dbh = $self->{dbh};
86
    my $rc = 0;
87
 
88
    eval {
89
	$dbh->{PrintError} = 0;
90
	my $field_type = $self->_map_type($definition);
91
 
92
	$dbh->do("ALTER TABLE $table ADD $field $field_type");
93
	print "Added new field $field to table $table.\n";
94
	$rc = 1;
95
	$self->commit();
96
    };
97
    if ($@) {
98
	eval { $self->rollback() };
99
    }
100
 
101
    $dbh->{PrintError} = 1;
102
 
103
    return $rc;
104
}
105
 
106
 
107
# Indicate if the LIKE operator can be applied on a "text" field.
108
# For ODBC (SQL Server), this is true.
109
sub has_like_operator_for_text_field {
110
    my $self = shift;
111
    return 1;
112
}
113
 
114
# Function for generating an SQL subexpression for a case insensitive LIKE
115
# operation.
116
sub case_insensitive_like {
117
    my ($self, $field, $expression) = @_;
118
 
119
    $expression = $self->{dbh}->quote($expression);
120
 
121
    # SQL Server is case insensitive by default, no need to do anything.
122
    return "$field LIKE $expression";
123
}
124
 
125
1;
126