## little helper for fbsql
## usage: 
## set sqlcmd [::fbsql::con host user pass name]
## $sqlcmd "SQL STATEMENT HERE"
## catch {$sqlcmd disconnect}
##
## second example for escaping out some things that can cause problems:
## $sqlcmd "INSERT INTO `table` (a,b) VALUES ('[::fbsql::escape $text1]','[::fbsql::escape $text2]')
##
## not very complex...but it works ;}

namespace eval ::fbsql {
namespace export con escape
variable session 0 sqlcmd "sql"

	proc con {{host {}} {user {}} {pass {}} {name {}}} {
		# take advantage of the sessions ability of fbsql
		if {($::fbsql::session == 0) || ($::fbslql::session > 10)} {
			set ::fbsql::session 0; set sqlcmd "sql"
		} else {set sqlcmd "sql$::fbsql::session"}
		if {[catch {$sqlcmd connect $host $user $pass} connect_error] != 0} {
			catch {$sqlcmd disconnect}; return -code error $connect_error
		}
		if {[catch {$sqlcmd selectdb $name} select_error] != 0} {
			catch {$sqlcmd disconnect}; return -code error $select_error
		}
		incr ::fbsql::session; return "$sqlcmd"
	}

	proc escape {text} {
		string map {\\ \\\\ \| \\| \[ \\[  \] \\] \{ \\{ \} \\} $ \\$ \` \\` \' \\' \" \\"} $text
	}
}