📄 database.pm
字号:
#!/usr/bin/perl -w## Copyright (C) 2005 Luke Reeves## This program is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; either version 2 of the License, or# (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with this program; if not, write to the Free Software# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307# USA#package Insipid::Database;use strict;use warnings;use Insipid::Config;use Insipid::Schemas;use Insipid::Util;use DBI qw/:sql_types/;;BEGIN { use Exporter (); our (@ISA, @EXPORT); @ISA = qw(Exporter); @EXPORT = qw($dbname $dbuser $dbpass $dsn $dbh $dbtype get_option install $version); }our @EXPORT;our ($dsn, $dbh, $dbname, $dbuser, $dbpass, $dbtype, $version);$version = '0.9.15';$dbname = getconfig("dbname");$dbuser = getconfig("dbuser");$dbpass = getconfig("dbpass");if(defined(getconfig("dbtype"))) { $dbtype = getconfig("dbtype");} else { $dbtype = "mysql";}$dsn = "DBI:$dbtype:database=$dbname;host=localhost";$dbh = DBI->connect($dsn, $dbuser, $dbpass, { 'RaiseError' => 0}) or die $DBI::errstr;my %options;my $sql = "select name, value from options";my $sth = $dbh->prepare($sql);$sth->execute();while(my $hr = $sth->fetchrow_hashref) { $options{$hr->{'name'}} = $hr->{'value'};}if(need_upgrade() eq 1) { dbupgrade();}sub dbupgrade { my $sql = "update options set value = ? where (name = ?)"; my $sth = $dbh->prepare($sql); $sth->execute($version, 'version'); if($dbh->errstr) { print STDERR $dbh->errstr; } return;}# Check if we need an upgradesub need_upgrade { if(!defined($options{version})) { return 1; } if($options{version} ne $version) { return 1; } else { return 0; }}# Functionssub get_option { my ($name) = (@_); return $options{$name};}sub install { my ($sth, @creates); print "Content-Type: text/html\r\n\r\n"; print "<html><head><title>Insipid Installation</title></head><body>"; print "<p>Creating tables..."; if($dbtype eq 'mysql') { @creates = split(/\;/, $createMySQL); } else { @creates = split(/\;/, $createPostgres); } foreach(@creates) { my $sql = $_; if(length($sql) > 2) { $sth = $dbh->prepare($sql); $sth->execute() or print "<br />Error executing \"$sql\" - $DBI::errstr<br />"; } } print " done!</p>"; print "<p>Insipid's database has been installed. You can reload this " . "page to start using Insipid.</p>"; print "</body></html>";}1;__END__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -