Initial commit

This commit is contained in:
Paul Reeves 2023-01-25 14:54:48 +01:00
parent 42c9399639
commit 6869e1e4ed
4 changed files with 625 additions and 0 deletions

483
test/execsql.sh Executable file
View File

@ -0,0 +1,483 @@
#!/bin/bash
license() {
echo This is a dummy function to allow the editors code folding ability to hide this text
#=============================================================================
# Initial Developer's Public License.
# The contents of this file are subject to the Initial Developer's Public
# License Version 1.0 (the "License"). You may not use this file except
# in compliance with the License. You may obtain a copy of the License at
# http://www.ibphoenix.com?a=ibphoenix&page=ibp_idpl
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is copyright 2004-2023 Paul Reeves.
#
# The Initial Developer of the Original Code is Paul Reeves
#
# All Rights Reserved.
#
#=============================================================================
# To Do
#
# Add Debug/Verbose/better logging
:
}
todo() {
# To Do
#
# Add Debug/Verbose/better logging
:
}
GETOPT=$(getopt -o b:c:d:f:i:hs:u:p:z: --long firebirdbin:,firebirdconf:,firebirddb:,firebird:,input:,help,savedb:,user:,pass:,firebirdserverversion: -n "$0" -- "$@")
EXECFILENAME=$(basename "$0")
declare -i FIREBIRDMAJORVERSION=0
declare -i FIREBIRDMINORVERSION=0
ShowHelp(){
tabs 3,5,9,13,17,21,25,29,33,37,41,45,49,53,57,61,65,69,73,77
DOCNOTE=$(cat << MSG
\n
### $(basename $0)\n
\t
\t\n
\tRun this script from a console prompt. It will execute ddl\n
\tto (re)create a firebird database.\n
\t\n
\t\t -i | --input ALIASNAME \n
\t\t\t\tPass the name of the master script to execute.\n
\t\t\t\tThis master script must have the same name as the alias used\n
\t\t\t\tfor the database that is being worked on.\n
\t\n
\t\t\t\tIf the alias does not exist in alias.conf it will be added.\n
\t\n
\t\t\t\t\t\$ALIAS = \$FIREBIRD_CONF\\\$ALIAS.fdb\n
\t\n
\t\t\t\tNOTE: Do not add the script file type (.sql)\n
\t\n
### Options\n
\t\n
\t Other available parameters:\n
\t\n
\t\t -f | --firebirdconf a/path/to/firebird/conf files\n
\t\t\t\tAssign a path to the FIREBIRD_CONF variable\n
\t\t\t\tDefault is /opt/firebird\n
\t\n
\t\t -d | --firebirddb a/path/to/firebird/db\n
\t\t\t\tAssign a path to the FB_DATA variable\n
\t\t\t\tDefault is /srv/data/\n
\t\n
\t\t -s | --savedb /a/backup/dir \n
\t\t\t\tDo a gbak backup of the existing db before executing ddl\n
\t\n
\t\t -h | --help\n
\t\t\t\tThis help screen.\n
\t\n
\t\n
MSG
)
local _help=$BASE_NAME-readme.md
echo -e $DOCNOTE > ${_help}
cat ${_help}
}
#################### Start of generic functions #############################
Pause() {
local _aprompt
if [ "${1:-}" = "" ]; then
_aprompt="Press any key to continue..."
else
_aprompt="$*"
fi
#shellcheck disable=SC2162
read -p "$_aprompt"
}
GetTime(){
export LOGSTAMP=$(date "+%Y-%m-%d %H:%M:%S")
export DATESTAMP=$(date +%Y%m%d)
export TIMESTAMP=$(date +%Y%m%d-%H%M%S)
export TAGSTAMP=$(date +:%Y%m%d%H%M)
case "${1:-}" in
LOGSTAMP)
echo LOGSTAMP is "$LOGSTAMP"
;;
DATESTAMP)
echo DATE is "$DATESTAMP"
;;
TIMESTAMP)
echo TIMESTAMP is "$TIMESTAMP"
;;
TAGSTAMP)
echo TAGSTAMP is "$TAGSTAMP"
;;
esac
}
GetLogStamp() {
GetTime "LOGSTAMP"
}
Debug() {
# Debug is intended for printing values assigned to variables etc.
if [ "${DEBUG:-}" = "1" ]; then
GetTime
local FUNC=$1
shift
echo -e $LOGSTAMP $FUNC "-" "$@"
fi
}
Verbose() {
# Verbose is intended to give the user a more
# detailed narrative of programm execution.
# shellcheck disable=SC2166
if [ "${VERBOSE:-}" == "1" -o "${DEBUG:-}" == "1" ]; then
GetTime
if [ "${2:-}" = "" ]; then
echo -e $LOGSTAMP "$@"
else
local FUNC=${1:-}
shift
echo -e $LOGSTAMP $FUNC "-" "$@"
fi
fi
}
WriteLog() {
# Param 1 - Logfile
# Param 2 - FUNCNAME
# We automatically call Verbose after shifting LOGFILE
# We then shift FUNCNAME and write out to the log.
GetTime
local LOGFILE=${1:-}
shift
Verbose "$@"
# shellcheck disable=SC2166
if [ ! "${VERBOSE:-}" = 1 -o ! "${DEBUG:-}" = 1 ]; then
shift
fi
if [ ! "${LOGFILE:-}" = "" ]; then
echo -e $LOGSTAMP "-" "$@" >> $LOGFILE
fi
}
Stop() {
# Echo error message and stop execution.
### TODO
exit $1
}
Error(){
#====
echo ""
echo " Error in $0"
echo " $*"
echo ""
return 1
}
######################### END OF GENERIC FUNCTIONS #########################
# Parse commandline options.
ParseCommandLine() {
set +x
if [ $# -le 0 ]; then
#ShowHelp
return 2
fi
eval set -- "$GETOPT"
while [ -n "$1" ]; do
case "$1" in
-h|--help) return 2 ;;
-b|--firebirdbin)
if [ "$2" ] > /dev/null 2>&1; then
export FIREBIRD_BIN="$2";
fi
shift;
;;
-c|--firebirdconf)
if [ "$2" ] > /dev/null 2>&1; then
export FIREBIRD_CONF="$2";
fi
shift;
;;
-d|--firebirddb)
if [ "$2" ] > /dev/null 2>&1; then
export FIREBIRD_DATA="$2";
fi
shift;
;;
-f|--firebird)
if [ "$2" ] > /dev/null 2>&1; then
export FIREBIRD="$2";
fi
shift;
;;
-i|--input)
if [ "$2" ] > /dev/null 2>&1; then
ALIAS="$2";
fi
shift;
;;
-s|-savedb)
if [ "$2" ] > /dev/null 2>&1; then
export FB_BACKUP_PATH="$2";
fi
shift;
;;
-u|--user)
if [ "$2" ] > /dev/null 2>&1; then
export ISC_USER="$2";
fi
shift;
;;
-p|--pass)
if [ "$2" ] > /dev/null 2>&1; then
export ISC_PASSWORD="$2";
fi
shift;
;;
-z|--firebirdserverversion)
if [ "$2" ] > /dev/null 2>&1; then
export FIREBIRDMAJORVERSION=$(echo "$2" | cut --delimiter "." -f 1 );
export FIREBIRDMINORVERSION=$(echo "$2" | cut --delimiter "." -f 2 );
fi
shift;
;;
esac
shift;
done
if [ -z "$FIREBIRD" ]; then
export FIREBIRD=/opt/firebird
fi
if [ -z "$FIREBIRD_BIN" ]; then
export FIREBIRD_BIN=/opt/firebird/bin
fi
if [ -z "$FB_DATA" ]; then
export FB_DATA=/srv/data/active
fi
if [ -z "$FIREBIRD_CONF" ]; then
export FIREBIRD_CONF=/opt/firebird
fi
set +x
if [ -z "$ALIAS" ]; then
return 1
fi
if [ -z "$ISC_USER" ]; then
ISC_USER=DBOWNER
fi
if [ -z "$ISC_PASSWORD" ]; then
ISC_PASSWORD=masterkey
fi
#echo FIREBIRDMAJORVERSION is $FIREBIRDMAJORVERSION
if [ $FIREBIRDMAJORVERSION -eq 0 ]; then
#set -x
FIREBIRDMAJORVERSION=$(GetFirebirdServerMajorVersion)
echo FIREBIRDMAJORVERSION is "$FIREBIRDMAJORVERSION"
#set +x
fi
if [ ${FIREBIRDMAJORVERSION:-0} -ge 3 ]; then
DBCONF=databases.conf
else
DBCONF=aliases.conf
fi
#echo FIREBIRDMINORVERSION is $FIREBIRDMINORVERSION
if [ "$FIREBIRDMINORVERSION" == "" ]; then
FIREBIRDMINORVERSION=$(GetFirebirdServerMinorVersion)
echo FIREBIRDMINORVERSION is "$FIREBIRDMINORVERSION"
fi
case "$FIREBIRDMAJORVERSION" in
2)
ODS=112
;;
3)
ODS=120
;;
4)
ODS=130
;;
5)
ODS=140
;;
esac
export ISC_USER
export ISC_PASSWORD
}
GetFirebirdServerMajorVersion() {
# This will return Firebird or Avalerion or ...?
# echo $(${FIREBIRD_BIN}/firebird -z) | cut --delimiter " " -f 6
echo $(${FIREBIRD_BIN}/firebird -z) | cut --delimiter " " -f 7 | cut -d "." -f 1
}
GetFirebirdServerMinorVersion() {
echo $(${FIREBIRD_BIN}/firebird -z) | cut --delimiter " " -f 7 | cut -d "." -f 2 > /dev/null
}
#============
main() {
BASE_NAME=$(basename $0 .sh )
ParseCommandLine $*
case $? in
1) Error $*; exit;;
2) ShowHelp; exit;;
esac
export ISC_USER=SYSDBA
export ISC_PASSWORD=masterkey
echo "$(date) Started"
#Delete old files
for afile in autogrant.ddl grants_isql.txt $ALIAS.txt
do
if [ -f $afile ]; then
rm $afile > /dev/null 2>&1
fi
done
# Check we have an alias for %1
# If it doesn't exist then check for FB_DATA and
# try and create it.
cat $FIREBIRD_CONF/$DBCONF | grep $ALIAS > /dev/null 2>&1
if [ $? -gt 0 ]; then
echo "Adding $ALIAS to $FIREBIRD_CONF/$DBCONF"
echo $ALIAS = $FB_DATA/ods$ODS/$ALIAS.fdb >> $FIREBIRD_CONF/$DBCONF
else
echo "Found $ALIAS in $FIREBIRD_CONF/$DBCONF"
fi
if [ $(whoami) = "root" ]; then
mkdir -p $FB_DATA/ods$ODS
chown firebird.firebird $FB_DATA/ods$ODS
chmod 770 $FB_DATA/ods$ODS
fi
#run gbak on the existing database, if any.
if [ -d "$FB_BACKUP_PATH" ]; then
gbak -b -user $ISC_USER -pas $ISC_PASSWORD -se localhost:service_mgr $ALIAS \
$FB_BACKUP_PATH/$ALIAS.gbak
else
echo "Not backing up old database $ALIAS"
fi
echo "-- (Re)create empty database" > recreate_db.ddl
echo "-- This file is automatically regenerated by $EXECFILENAME" >> recreate_db.ddl
echo "-- Do not edit." >> recreate_db.ddl
echo "-- " >> recreate_db.ddl
#generate database (re)creation script.
DBFILENAME=$(cat $FIREBIRD_CONF/$DBCONF | grep -v ^# | grep $ALIAS --max-count=1 | cut -d \= -f 2 | tr -d ' ')
echo DBFILENAME is $DBFILENAME
if [ -f $DBFILENAME ]; then
# echo "connect 'localhost:$ALIAS';" >> recreate_db.ddl
echo "connect '$ALIAS';" >> recreate_db.ddl
echo "drop database;" >> recreate_db.ddl
echo "" >> recreate_db.ddl
fi
#echo "create database 'localhost:$ALIAS';" >> recreate_db.ddl
echo "create database '$ALIAS';" >> recreate_db.ddl
echo "commit;" >> recreate_db.ddl
#echo "connect '$ALIAS';" >> recreate_db.ddl
#echo "CREATE OR ALTER USER SYSDBA PASSWORD 'masterkey' using plugin Srp;" >> #recreate_db.ddl
#echo "commit;" >> recreate_db.ddl
#Pause
#run creation script
if [ -f $ALIAS.sql ]; then
echo Running $ALIAS.sql...
${FIREBIRD_BIN}/isql -b -user FWFRAMEWORK_OWNER -pass masterkey -role RDB$ADMIN -e -i $ALIAS.sql -o $ALIAS.txt -m -m2 > /dev/null
else
echo Could not find $ALIAS.sql.
echo Exiting.
exit;
fi
if [ $(whoami) = "root" ]; then
chown firebird.firebird $DBFILENAME
chmod 660 $DBFILENAME
fi
#print errors
echo Checking for Errors
cat $ALIAS.txt | grep -B 16 -A 16 "Statement failed"
if [ $? -gt 0 ]; then
echo No errors found.
echo Script completed successfully.
echo
echo ====================================
echo
echo Here are the last lines from $ALIAS.txt
echo
tail -n 20 $ALIAS.txt
echo
fi
echo "$(date) Completed."
}
main "$@"

105
test/grants.ddl Normal file
View File

@ -0,0 +1,105 @@
-- exit;
/*
* Initial Developer's Public License.
* The contents of this file are subject to the Initial Developer's Public
* License Version 1.0 (the "License"). You may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://www.ibphoenix.com?a=ibphoenix&page=ibp_idpl
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is copyright 2004-2020 Paul Reeves.
*
* The Initial Developer of the Original Code is Paul Reeves
*
* All Rights Reserved.
*
*/
set list on;
set echo off;
-- These files should be deleted by the calling batch file
-- however if this script is run standalone then uncomment these lines.
--shell del autogrant.ddl;
--shell del grants_isql.txt;
output autogrant.ddl;
select distinct ' EXECUTE ON PROCEDURE ' || RDB$PROCEDURE_NAME || ' to SYSDBA;' as "GRANT"
from RDB$PROCEDURES
where RDB$PROCEDURE_NAME not starting with 'RDB$' and RDB$SYSTEM_FLAG = 0;
select distinct ' EXECUTE ON FUNCTION ' || RDB$FUNCTION_NAME || ' to SYSDBA;' as "GRANT"
from RDB$FUNCTIONS
where RDB$FUNCTION_NAME not starting with 'RDB$' and RDB$SYSTEM_FLAG = 0;
select ' ALL ON ' || RDB$RELATION_NAME || ' to SYSDBA;' as "GRANT"
from RDB$RELATIONS
where RDB$RELATION_NAME not starting with 'RDB$'
and RDB$RELATION_NAME not starting with 'MON$'
and RDB$RELATION_NAME not starting with 'SEC$' ;
select distinct ' ALL ON ' || RDB$VIEW_NAME || ' to SYSDBA;' as "GRANT"
from RDB$VIEW_RELATIONS
where RDB$VIEW_NAME not starting with 'RDB$';
select distinct ' EXECUTE ON PROCEDURE ' || RDB$PROCEDURE_NAME || ' to FWFRAMEWORK_OWNER;' as "GRANT"
from RDB$PROCEDURES
where RDB$PROCEDURE_NAME not starting with 'RDB$' and RDB$SYSTEM_FLAG = 0;
select distinct ' EXECUTE ON FUNCTION ' || RDB$FUNCTION_NAME || ' to FWFRAMEWORK_OWNER;' as "GRANT"
from RDB$FUNCTIONS
where RDB$FUNCTION_NAME not starting with 'RDB$' and RDB$SYSTEM_FLAG = 0;
select ' ALL ON ' || RDB$RELATION_NAME || ' to FWFRAMEWORK_OWNER;' as "GRANT"
from RDB$RELATIONS
where RDB$RELATION_NAME not starting with 'RDB$'
and RDB$RELATION_NAME not starting with 'MON$'
and RDB$RELATION_NAME not starting with 'SEC$' ;
select distinct ' ALL ON ' || RDB$VIEW_NAME || ' to FWFRAMEWORK_OWNER;' as "GRANT"
from RDB$VIEW_RELATIONS
where RDB$VIEW_NAME not starting with 'RDB$';
-- FIX ME - we need to grant admin to framework_owner, check that the full list is generated and then look
-- at revoking the output, or hard-coding the grant list
select ' usage on generator ' || trim(r.RDB$GENERATOR_NAME) || ' to ' || trim(s.SEC$USER_NAME) || ';' as "GRANT"
from RDB$GENERATORS r
cross join SEC$USERS s
where r.RDB$SYSTEM_FLAG <>1;
/*
-- Uncomment this section to grant all to public. Or use this as a
-- template to grant to specific users.
select ' ALL ON ' || RDB$RELATION_NAME || ' to public;' as "GRANT"
from RDB$RELATIONS
where RDB$RELATION_NAME not starting with 'RDB$'
and RDB$RELATION_NAME not starting with 'MON$'
and RDB$RELATION_NAME not starting with 'SEC$' ;
select distinct ' ALL ON ' || RDB$VIEW_NAME || ' to public;' as "GRANT"
from RDB$VIEW_RELATIONS
where RDB$VIEW_NAME not starting with 'RDB$';
-- This is too broad!
--select distinct ' EXECUTE ON PROCEDURE ' || RDB$PROCEDURE_NAME || ' to public;' as "GRANT"
--from RDB$PROCEDURES
--where RDB$PROCEDURE_NAME not starting with 'RDB$' and RDB$SYSTEM_FLAG = 0;
*/
output grants_isql.txt;
set echo on;
input autogrant.ddl;
commit;

12
test/testudrkit.ddl Normal file
View File

@ -0,0 +1,12 @@
/*
create tables for test db
*/
create sequence test_div_seq;
commit;
create domain D_ID as BIGINT;
create table test_div(
test_div_id d_id generated always as identity
);

25
test/testudrkit.sql Normal file
View File

@ -0,0 +1,25 @@
set echo on;
set sql dialect 3;
set names UTF8;
input recreate_db.ddl;
-- create db objects
input testudrkit.ddl;
-- insert some test data
input testudrkit.dml;
-- run some tests
input testudrkit-tests.dml;
/* Successfully completed execution of testudrkit.sql */
EXIT;