| 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::MySQL;
|
|
|
9 |
|
|
|
10 |
use strict;
|
|
|
11 |
use DBI;
|
|
|
12 |
use Codestriker;
|
|
|
13 |
use Codestriker::DB::Database;
|
|
|
14 |
|
|
|
15 |
# Module for handling a MySQL database.
|
|
|
16 |
|
|
|
17 |
@Codestriker::DB::MySQL::ISA = ("Codestriker::DB::Database");
|
|
|
18 |
|
|
|
19 |
# Type mappings.
|
|
|
20 |
my $_TYPE = {
|
|
|
21 |
$Codestriker::DB::Column::TYPE->{TEXT} => "mediumtext",
|
|
|
22 |
$Codestriker::DB::Column::TYPE->{VARCHAR} => "varchar",
|
|
|
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 MySQL 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::mysql', version => '0' };
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
# Retrieve a database connection.
|
|
|
44 |
sub get_connection {
|
|
|
45 |
my $self = shift;
|
|
|
46 |
|
|
|
47 |
# Not all versions of MySQL upport transactions. Its easiest for now to
|
|
|
48 |
# just enable AUTO_COMMIT.
|
|
|
49 |
my $dbh = $self->_get_connection(1, 1);
|
|
|
50 |
|
|
|
51 |
# Older MySQL 3.X installations don't support these commands. Wrap them
|
|
|
52 |
# for now in a block and silently ignore the errors.
|
|
|
53 |
{
|
|
|
54 |
local $dbh->{RaiseError};
|
|
|
55 |
local $dbh->{PrintError};
|
|
|
56 |
$dbh->do("SET NAMES 'utf8'");
|
|
|
57 |
$dbh->do("SET character_set_results='utf8'");
|
|
|
58 |
}
|
|
|
59 |
return $dbh;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
# Return the mapping for a specific type.
|
|
|
63 |
sub _map_type {
|
|
|
64 |
my ($self, $type) = @_;
|
|
|
65 |
return $_TYPE->{$type};
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
# Autoincrement type for MySQL.
|
|
|
69 |
sub _get_autoincrement_type {
|
|
|
70 |
return "auto_increment";
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
# MySQL specific function adapted from Bugzilla.
|
|
|
74 |
sub get_field_def {
|
|
|
75 |
my ($self, $table, $field) = @_;
|
|
|
76 |
my $sth = $self->{dbh}->prepare("SHOW COLUMNS FROM $table");
|
|
|
77 |
$sth->execute;
|
|
|
78 |
|
|
|
79 |
while (my $ref = $sth->fetchrow_arrayref) {
|
|
|
80 |
next if $$ref[0] ne $field;
|
|
|
81 |
return $ref;
|
|
|
82 |
}
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
# Indicate if the LIKE operator can be applied on a "text" field.
|
|
|
86 |
# For MySQL, this is true.
|
|
|
87 |
sub has_like_operator_for_text_field {
|
|
|
88 |
my $self = shift;
|
|
|
89 |
return 1;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
# Function for generating an SQL subexpression for a case insensitive LIKE
|
|
|
93 |
# operation.
|
|
|
94 |
sub case_insensitive_like {
|
|
|
95 |
my ($self, $field, $expression) = @_;
|
|
|
96 |
|
|
|
97 |
$expression = $self->{dbh}->quote($expression);
|
|
|
98 |
|
|
|
99 |
# MySQL is case insensitive by default, no need to do anything.
|
|
|
100 |
return "$field LIKE $expression";
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
|
|
|
104 |
# These are no-ops for MySQL.
|
|
|
105 |
sub commit {}
|
|
|
106 |
sub rollback {}
|
|
|
107 |
|
|
|
108 |
1;
|
|
|
109 |
|