Subversion Repositories oidplus

Rev

Rev 223 | Rev 264 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
5
 * Copyright 2019 Daniel Marschall, ViaThinkSoft
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
 
20
$prefix = isset($_REQUEST['prefix']) ? $_REQUEST['prefix'] : '';
78 daniel-mar 21
$database = isset($_REQUEST['database']) ? $_REQUEST['database'] : '';
239 daniel-mar 22
$slang = isset($_REQUEST['slang']) ? $_REQUEST['slang'] : 'mysql';
2 daniel-mar 23
 
239 daniel-mar 24
if (($slang != 'mysql') && ($slang != 'pgsql') && ($slang != 'mssql')) die('Unknown slang');
223 daniel-mar 25
 
239 daniel-mar 26
$cont = trim(file_get_contents(__DIR__.'/sql/struct_'.$slang.'.sql'))."\n\n".
27
        trim(file_get_contents(__DIR__.'/sql/wellknown_country_'.$slang.'.sql'))."\n\n".
28
        trim(file_get_contents(__DIR__.'/sql/wellknown_other_'.$slang.'.sql'))."\n\n".
29
        trim(file_get_contents(__DIR__.'/sql/example_'.$slang.'.sql'))."\n\n";
2 daniel-mar 30
 
158 daniel-mar 31
$table_names = array('objects', 'asn1id', 'iri', 'ra', 'config', 'log', 'log_user', 'log_object');
2 daniel-mar 32
foreach ($table_names as $table) {
239 daniel-mar 33
        if ($slang == 'mysql') {
34
                $cont = str_replace('`'.$table.'`', '`'.$prefix.$table.'`', $cont);
35
        }
36
        if ($slang == 'pgsql') {
37
                $cont = str_replace('"'.$table.'"', '"'.$prefix.$table.'"', $cont);
38
                $cont = str_replace('"index_'.$table, '"index_'.$prefix.$table, $cont);
39
        }
40
        if ($slang == 'mssql') {
41
                $cont = str_replace('['.$table.']', '['.$prefix.$table.']', $cont);
42
                $cont = str_replace('dbo.'.$table, 'dbo.'.$prefix.$table, $cont);
43
                $cont = str_replace('PK_'.$table, 'PK_'.$prefix.$table, $cont);
44
                $cont = str_replace('DF__'.$table, 'DF__'.$prefix.$table, $cont);
45
        }
2 daniel-mar 46
}
47
 
50 daniel-mar 48
if (php_sapi_name() != 'cli') {
49
        header('Content-Type:text/sql');
111 daniel-mar 50
        header('Content-Disposition: inline; filename="struct_with_examples.sql"');
50 daniel-mar 51
}
78 daniel-mar 52
 
111 daniel-mar 53
if (!empty($database)) {
239 daniel-mar 54
        if ($slang == 'mysql') {
223 daniel-mar 55
                echo "CREATE DATABASE IF NOT EXISTS `$database`;\n\n";
56
                echo "USE `$database`;\n\n";
57
        }
239 daniel-mar 58
        if ($slang == 'pgsql') {
59
                echo "-- CREATE DATABASE $database;\n\n";
60
                echo "-- \connect $database;\n\n";
61
        }
62
        if ($slang == 'mssql') {
63
                echo "USE [$database]\n\n";
64
                echo "GO\n\n";
65
        }
111 daniel-mar 66
}
2 daniel-mar 67
echo $cont;