Merge pull request #54 from VVatashi/feature_pgsql

Add support for PostgreSQL (pgsql) PDO driver
This commit is contained in:
Trevor Slocum 2018-04-12 11:41:23 -07:00 committed by GitHub
commit cdfe7803d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 111 additions and 48 deletions

View File

@ -13,9 +13,14 @@ if (TINYIB_DBDSN == '') { // Build a default (likely MySQL) DSN
$dsn = TINYIB_DBDSN;
}
if (TINYIB_DBDRIVER === 'pgsql') {
$options = array(PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
} else {
$options = array(PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
}
try {
$dbh = new PDO($dsn, TINYIB_DBUSERNAME, TINYIB_DBPASSWORD, $options);
@ -24,14 +29,28 @@ try {
}
// Create the posts table if it does not exist
if (TINYIB_DBDRIVER === 'pgsql') {
$query = "SELECT COUNT(*) FROM pg_catalog.pg_tables WHERE tablename LIKE " . $dbh->quote(TINYIB_DBPOSTS);
$posts_exists = $dbh->query($query)->fetchColumn() != 0;
} else {
$dbh->query("SHOW TABLES LIKE " . $dbh->quote(TINYIB_DBPOSTS));
if ($dbh->query("SELECT FOUND_ROWS()")->fetchColumn() == 0) {
$posts_exists = $dbh->query("SELECT FOUND_ROWS()")->fetchColumn() != 0;
}
if (!$posts_exists) {
$dbh->exec($posts_sql);
}
// Create the bans table if it does not exist
if (TINYIB_DBDRIVER === 'pgsql') {
$query = "SELECT COUNT(*) FROM pg_catalog.pg_tables WHERE tablename LIKE " . $dbh->quote(TINYIB_DBBANS);
$bans_exists = $dbh->query($query)->fetchColumn() != 0;
} else {
$dbh->query("SHOW TABLES LIKE " . $dbh->quote(TINYIB_DBBANS));
if ($dbh->query("SELECT FOUND_ROWS()")->fetchColumn() == 0) {
$bans_exists = $dbh->query("SELECT FOUND_ROWS()")->fetchColumn() != 0;
}
if (!$bans_exists) {
$dbh->exec($bans_sql);
}

View File

@ -3,6 +3,49 @@ if (!defined('TINYIB_BOARD')) {
die('');
}
if (TINYIB_DBDRIVER === 'pgsql') {
$posts_sql = 'CREATE TABLE "' . TINYIB_DBPOSTS . '" (
"id" bigserial NOT NULL,
"parent" integer NOT NULL,
"timestamp" integer NOT NULL,
"bumped" integer NOT NULL,
"ip" varchar(39) NOT NULL,
"name" varchar(75) NOT NULL,
"tripcode" varchar(10) NOT NULL,
"email" varchar(75) NOT NULL,
"nameblock" varchar(255) NOT NULL,
"subject" varchar(75) NOT NULL,
"message" text NOT NULL,
"password" varchar(255) NOT NULL,
"file" text NOT NULL,
"file_hex" varchar(75) NOT NULL,
"file_original" varchar(255) NOT NULL,
"file_size" integer NOT NULL default \'0\',
"file_size_formatted" varchar(75) NOT NULL,
"image_width" smallint NOT NULL default \'0\',
"image_height" smallint NOT NULL default \'0\',
"thumb" varchar(255) NOT NULL,
"thumb_width" smallint NOT NULL default \'0\',
"thumb_height" smallint NOT NULL default \'0\',
"stickied" smallint NOT NULL default \'0\',
"moderated" smallint NOT NULL default \'1\',
PRIMARY KEY ("id")
);
CREATE INDEX ON "' . TINYIB_DBPOSTS . '"("parent");
CREATE INDEX ON "' . TINYIB_DBPOSTS . '"("bumped");
CREATE INDEX ON "' . TINYIB_DBPOSTS . '"("stickied");
CREATE INDEX ON "' . TINYIB_DBPOSTS . '"("moderated");';
$bans_sql = 'CREATE TABLE "' . TINYIB_DBBANS . '" (
"id" bigserial NOT NULL,
"ip" varchar(39) NOT NULL,
"timestamp" integer NOT NULL,
"expire" integer NOT NULL,
"reason" text NOT NULL,
PRIMARY KEY ("id")
);
CREATE INDEX ON "' . TINYIB_DBBANS . '"("ip");';
} else {
$posts_sql = "CREATE TABLE `" . TINYIB_DBPOSTS . "` (
`id` mediumint(7) unsigned NOT NULL auto_increment,
`parent` mediumint(7) unsigned NOT NULL,
@ -44,6 +87,7 @@ $bans_sql = "CREATE TABLE `" . TINYIB_DBBANS . "` (
PRIMARY KEY (`id`),
KEY `ip` (`ip`)
)";
}
function cleanString($string) {
$search = array("<", ">");