##
# Project: one.migration: Scripts for the migration from opennebula 1.4 to 2.0.1 
# Description:  This bash source file contains functions for the SQL migration.
#   
# @author Marlon Nerling, Abacus Research AG, 9301 Wittenbach, Schweiz http://www.abacus.ch Copyright (C) 2010.
#   
# @see The GNU Public License (GPL)
#
## 
# 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
#

ORIGINAL_DB=/tmp/one.db
ONEMIGRATION_COMMON='true'
DEBUG=true
debug(){
	[ -z "$DEBUG" ] && return 0
	echo "[DEBUG]: $@" 1>&2
}
query_sqlite(){
	local query="$@"; 
	debug " Query $query"
	echo "$query" | sqlite3 $ORIGINAL_DB  
}
query_sqlite_scape(){
	query_sqlite "$@" | sed -e "s:|:\' \':g" -e "s:^:\':" -e "s:$:\':"
}

query_sqlite_prepare(){
	query_sqlite "$@" | sed -e "s:|:\', \':g" -e "s:^:\':" -e "s:$:\':"
}

get_attributes(){
	local table="${1?"[FAIL]: $FUNCNAME waits for a attribute type as parameter"}"; 
	shift
	local id="$1"
	local i=0
	while read name value 
	do
		echo "$name=$value" 
	done < <( query_sqlite "SELECT name,value FROM $table WHERE id=$id;" | sed -e 's:|:\ :g' )
}
