diff options
author | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-01-20 01:29:50 +0000 |
---|---|---|
committer | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-01-20 01:29:50 +0000 |
commit | 8362bf63dea22bbf6736609b0f49c152f975eb63 (patch) | |
tree | 0eea3928e39e50fae91d4e68b21b1e6cbae25604 /kexi/tools/delete_column | |
download | koffice-8362bf63dea22bbf6736609b0f49c152f975eb63.tar.gz koffice-8362bf63dea22bbf6736609b0f49c152f975eb63.zip |
Added old abandoned KDE3 version of koffice
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/applications/koffice@1077364 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'kexi/tools/delete_column')
-rw-r--r-- | kexi/tools/delete_column/Makefile.am | 5 | ||||
-rw-r--r-- | kexi/tools/delete_column/README | 17 | ||||
-rwxr-xr-x | kexi/tools/delete_column/kexi_delete_column | 136 | ||||
-rwxr-xr-x | kexi/tools/delete_column/kexi_delete_column_gui | 82 | ||||
-rw-r--r-- | kexi/tools/delete_column/kexi_delete_column_gui_transl_pl.sh | 10 |
5 files changed, 250 insertions, 0 deletions
diff --git a/kexi/tools/delete_column/Makefile.am b/kexi/tools/delete_column/Makefile.am new file mode 100644 index 00000000..1f019ae8 --- /dev/null +++ b/kexi/tools/delete_column/Makefile.am @@ -0,0 +1,5 @@ + +bin_SCRIPTS = kexi_delete_column kexi_delete_column_gui + +message_pldir = $(kde_locale)/pl/LC_MESSAGES +message_pl_DATA = kexi_delete_column_gui_transl_pl.sh diff --git a/kexi/tools/delete_column/README b/kexi/tools/delete_column/README new file mode 100644 index 00000000..3e263591 --- /dev/null +++ b/kexi/tools/delete_column/README @@ -0,0 +1,17 @@ +== "Delete column" utility for Kexi == + +This script deletes a single table column from a .kexi (SQLite 3) +database file without removing data from the table. + +Rationale: +Kexi 1.x does not offer deleting columns from a table +without removing its data. The kexi_delete_column script does exactly this. + +Usage: +Type kexi_delete_column for list of arguments. + +GUI tool: +There is also a GUI tool 'kexi_delete_column_gui' that is basically +a wrapper running on top of kexi_delete_column +The tool works without arguments - it uses KDE dialogs instead ('kdialog'). + diff --git a/kexi/tools/delete_column/kexi_delete_column b/kexi/tools/delete_column/kexi_delete_column new file mode 100755 index 00000000..22566c61 --- /dev/null +++ b/kexi/tools/delete_column/kexi_delete_column @@ -0,0 +1,136 @@ +#!/bin/sh +# +# Copyright (C) 2006-2007 Jaroslaw Staniek <js@iidea.pl> +# +# 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; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, +# Boston, MA 02110-1301, USA. +# + +usage { + echo "This script deletes a single table column from a .kexi (SQLite 3) +database file without removing data from the table. + +Usage: + $0 database_name table_name column_name + +- {database_name}.old backup file is created before proceeding +- database_name, table_name, column_name must exist +- note that queries, forms and other objects referencing + to the altered table can become invalid and have to be + fixed by hand + +Example: to delete 'price' column from table 'products', type + $0 db.kexi products price" +} + +exit_with_error { + rm -f "$temp_db" + echo $* + echo "Error." + exit 1 +} + +check { + [ -n "$*" ] && exit_with_error "$*" +} + +ksqlite="ksqlite -noheader" +ksqlite_header="ksqlite -header" + +if [ $# -lt 3 ] ; then + usage + exit 0 +fi +database_name=$1 +table_name=$2 +column_name=$3 + +temp_db=`mktemp "$database_name"XXXXXXXX` || exit_with_error +cp "$database_name" "$temp_db" || exit_with_error + +# 1. alter the table physically + +prepare_new_create_table_statement { + # possible problems: typename ( number , number ) may contain "," + + schema=`echo ".schema '$table_name';" | $ksqlite "$database_name" | \ + grep "^CREATE TABLE $table_name " | \ + sed -e "s/[^(]*(\(.*\));/\1/" || exit_with_error` + + IFS="," + for coldef in $schema ; do + col=`echo $coldef | sed "s/^[ ]*\([^ ]*\) .*$/\1/"` + if [ "$col" != "$column_name" ] ; then + echo -n ,$coldef + fi + done | cut -c2- + IFS=" " +} + +get_sql_column_names { + names=`$ksqlite_header "$temp_db" "SELECT * FROM '$temp_table_name' LIMIT 1;" | \ + head -n 1 || exit_with_error` + IFS="|" + for col in $names ; do + if [ "$col" != "$column_name" ] ; then + echo -n ", $col" + fi + done | cut -c3- + IFS=" " +} + +# 1.1. rename the original table to a temp name +temp_table_name=`mktemp "$table_name"XXXXXXXX` +msg=`$ksqlite "$temp_db" "ALTER TABLE '$table_name' RENAME TO '$temp_table_name';"` +check "$msg" + +# 1.2. create a new table without the removed column and copy the data +new_create_table_statement=`prepare_new_create_table_statement` +msg=`$ksqlite "$temp_db" "CREATE TABLE '$table_name' ($new_create_table_statement);"` +check "$msg" + +sql_column_names=`get_sql_column_names` +msg=`$ksqlite "$temp_db" "INSERT INTO '$table_name' SELECT $sql_column_names FROM '$temp_table_name';"` +check "$msg" + +# 1.3. drop the temporary table +msg=`$ksqlite "$temp_db" "DROP TABLE '$temp_table_name';"` +check "$msg" + + +# 2. alter information in the kexi__fields system table (schema) + +# 2.1. Get table's ID +table_id=`$ksqlite "$temp_db" "SELECT o_id FROM kexi__objects WHERE o_type=1 AND o_name='$table_name';" || exit_with_error` + +# 2.1. Get column's number +column_order=`$ksqlite "$temp_db" "SELECT f_order FROM kexi__fields WHERE t_id=$table_id AND f_name='$column_name';" || exit_with_error` + +$ksqlite "$temp_db" "DELETE FROM kexi__fields WHERE t_id=$table_id AND f_name='$column_name';" + +for fname in `$ksqlite "$temp_db" \ + "SELECT f_name FROM kexi__fields WHERE t_id=$table_id AND f_order>=$column_order ORDER BY f_order DESC;"` ; do + msg=`$ksqlite "$temp_db" "UPDATE kexi__fields SET f_order=$column_order WHERE t_id=$table_id AND f_name='$fname';"` + check "$msg" + column_order=`expr $column_order + 1` +done + +# 3. Copy the original database file to .old file and replace +# the original with the new one +cp "$database_name" "$database_name.old" || exit_with_error +mv "$temp_db" "$database_name" || exit_with_error + +exit 1 + diff --git a/kexi/tools/delete_column/kexi_delete_column_gui b/kexi/tools/delete_column/kexi_delete_column_gui new file mode 100755 index 00000000..0a87091c --- /dev/null +++ b/kexi/tools/delete_column/kexi_delete_column_gui @@ -0,0 +1,82 @@ +#!/bin/sh +# +# Copyright (C) 2006-2007 Jaroslaw Staniek <js@iidea.pl> +# +# 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; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, +# Boston, MA 02110-1301, USA. +# + +basedir=`dirname "$0"` + +setup_messages { + lang=`grep Language= ~/.kde/share/config/kdeglobals | head -n 1 | \ + sed -e 's/Language=\(.*\):.*/\1/'` + if [ -z "$lang" ] ; then lang="en" ; fi + + IFS=: + for dir in `kde-config --expandvars --path locale` ; do + transl_file="$dir"$lang"/LC_MESSAGES/kexi_delete_column_gui_transl_$lang.sh"; + if [ -f "$transl_file" ] ; then + source "$transl_file" + break + else + transl_file=; + fi + done + IFS=" " + if [ -z "$transl_file" ] ; then + transl_file="$basedir/kexi_delete_column_gui_transl_$lang.sh"; + if [ -f "$transl_file" ] ; then source "$transl_file"; else transl_file=; fi + fi +echo $transl_file + if [ -z "$transl_file" ] ; then + # default: english messages: + msg_filters="*.kexi|Kexi Project stored in a file +*.*|All files" + msg_select_db_file="Select database file" + msg_enter_table_name="Table name (not caption):" + msg_enter_column_name="Column name (not caption):" + fi +} # /setup_messages + +setup_messages + +ksqlite="ksqlite -noheader" + +database_name=`kdialog --title "$msg_select_db_file" --getopenfilename . "$msg_filters"` || exit 1 + +table_name=`kdialog --inputbox "$msg_enter_table_name"` || exit 1 + +# show list of columns and ask for one + +msg=`$ksqlite "$database_name" "SELECT f_name FROM kexi__objects o, kexi__fields f WHERE o.o_id=f.t_id AND o.o_name='$table_name' AND o_type=1 ORDER BY f_order;" || exit 1` + +command_file=`mktemp "$database_name"XXXXXXXX`".sh" +echo -n "kdialog --radiolist \"$msg_enter_column_name\"" > $command_file +echo $msg | while read f ; do + echo -n " $f $f off" >> $command_file +done + +column_name=`. $command_file || exit 1` +rm -f $command_file + +# call the command line tool + +msg=`sh kexi_delete_column "$database_name" "$table_name" "$column_name" 2>&1` + +[ -z "$msg" ] && exit 0 + +kdialog --error "$msg" +exit 1 diff --git a/kexi/tools/delete_column/kexi_delete_column_gui_transl_pl.sh b/kexi/tools/delete_column/kexi_delete_column_gui_transl_pl.sh new file mode 100644 index 00000000..ba46b949 --- /dev/null +++ b/kexi/tools/delete_column/kexi_delete_column_gui_transl_pl.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +# Translation file for kexi_delete_column_gui + +msg_filters="*.kexi|Projekt Kexi zapisany w pliku +*.*|Wszystkie pliki" + +msg_select_db_file="Wybierz plik bazy danych" +msg_enter_table_name="Nazwa tabeli (nie tytuł):" +msg_enter_column_name="Nazwa kolumny (nie tytuł):" |