proc pub_query { nick host ident chan arg } {
	# $arg is [split] in order to turn it into a valid tcl list which can then be safely [lindex]'ed

	set arg [lindex [split $arg] 0] ;# We don't need the rest of it, do we?
	set results [sql "SELECT * FROM table WHERE name like '\\\%${arg}\\\%' ORDER BY date DESC"]
	set resultCount [llength $results]
	
	putserv "PRIVMSG #twizchan :(sqlbot) found $resultCount matches to $arg (diaplaying max of 10)"
	
	for {set i 0} {$i < (($resultCount >= 10) ? 10 : $resultCount)} {incr i} {
		set row [lindex $results $i]
		# example row:
		# 1085993751 tWiZtEr.Sucks.At.SQL NOTE
		# format: timestamp, title, section

		set timestamp [clock format [lindex $row 0] -format "\[\%m-\%d-\%y\]"]
		set title     [lindex $row 1]
		set section   [lindex $row 2]
		set params    [lrange $row 3 end]
		if {$title == ""} {
			set title (no title)
		}
		
		switch -- $section \
		REQUEST {
			# params for REQUEST: text timestamp
			set paramtext " [lindex $params 0] ([clock format [lindex $params 1] -format "\%m-\%d-\%y"])"
		} EXAMPLE {
			# params for EXAMPLE: text amount optionalcomment
			if {[lindex $params 2] == ""} {
				set comment "(no comment)"
			} else {
				set comment [lindex $params 2]
			}
			set paramtext " [lindex $params 0], amount of [lindex $params 1], $comment"
		} default {
			# we don't know the format, so we do minimal parsing
			set newlist ""
			foreach entry $params {
				if {$entry == ""} {
					lappend newlist "(blank entry)"
				} else {
					lappend newlist $entry
				}
			}
			set paramtext " [join $newlist]"
		}
		
		putserv "PRIVMSG #twizchan (sqlbot) #[expr $i + 1]: ${timestamp}- $title -$paramtext"
	}
}