05-30-2020, 07:30 +0200
AuthorPost
Turbo  03-03-2013, 18:43   | mySQL Database Connect MSN  Homepage
Moderator
Member since 06/2007
5 Posts
Location: Netherlands
  1. _____           _             ____
#|_   _|   _ _ __| |__   ___   |  _ \  _____   __
  1. | || | | | '__| '_ \ / _ \  | | | |/ _ \ \ / /
  2. | || |_| | |  | |_) | (_) | | |_| |  __/\ V /
  3. |_| \__,_|_|  |_.__/ \___/  |____/ \___| \_/
#
  1. -------------------------------------------------------------------
  2. | Author:    Björn 'Turbo' B.                             |
  3. -------------------------------------------------------------------
#
  1. -------------------------------------------------------------------
  2. | Require:    package mysqltcl                              |
  3. |         If you don`t have the package than download it at:  |
  4. |            (http://www.xdobry.de/mysqltcl/#downloads)        |
  5. -------------------------------------------------------------------
#
  1. -------------------------------------------------------------------
  2. |         Copyright © 2008 Turbo - All Rights Reserved.        |
  3. -------------------------------------------------------------------


namespace eval ::db {


    # - Mysql Configuration

    variable database ""
    variable username ""
    variable password ""

    ## Hostname can be used as empty or you fill in a hostname, such as localhost.
    variable hostname "localhost"

    # - Mysql Module Check

    if {[catch {package require mysqltcl} error ]} {
        putlog "Could not load mysqltcl, if you don't have it, download it at (http://www.xdobry.de/mysqltcl/#downloads)"
        return
    } else {
        package require mysqltcl
        putlog "Package Mysqltcl Version '[::mysql::baseinfo clientversion]' Loaded."
    }

    # - Bindings

    bind msg nm connect [namespace current]::connect
    bind msg nm disconnect [namespace current]::disconnect
    bind msg nm status [namespace current]::status

    ## End Of Configuration

    ## Don`t touch anything beneath this (only if you know what you are doing)

    proc connect {nick host hand text} {
        global mysqlhandle
        variable database
        variable username
        variable password
        variable hostname
        if {$database == "" || $username == "" || $password == ""} {
            putserv "NOTICE $nick :(mySQL) Please check your mySQL configuration."
        } elseif {[string length $password] < 6} {
            putserv "NOTICE $nick :(mySQL) Password unsafe, make sure your password has more than 6 chars."
        } elseif {[info exists mysqlhandle] && [::mysql::state $mysqlhandle] == "IN_USE"} {
            putserv "NOTICE $nick :(mySQL) Already connected to a database."
        } else {
            putserv "NOTICE $nick :(mySQL) Connecting to server:"
            if {[catch { set mysqlhandle [::mysql::connect -user $username -db $database -host $hostname -password $password] } error]} {
                putserv "NOTICE $nick :(mySQL) Error:"
                foreach mysqlerror [split $error \n] {
                    putserv "NOTICE $nick :(mySQL) $mysqlerror"
                }
                putserv "NOTICE $nick :(mySQL) End of error."
            } else {
                putserv "NOTICE $nick :(mySQL) Connected to $database"
                putserv "NOTICE $nick :(mySQL) Handle: $mysqlhandle"
            }
            timer 300 [namespace current]::reconnect
        }
    }

    proc disconnect {nick host hand text} {
        global mysqlhandle
        if {![info exists mysqlhandle]} {
            putserv "NOTICE $nick :(mySQL) No mysql handle found, maybe you need to connect first."
        } elseif {[::mysql::state $mysqlhandle] == "NOT_A_HANDLE"} {
            putserv "NOTICE $nick :(mySQL) There is no connection found."
        } elseif {[::mysql::ping $mysqlhandle] == "0"} {
            putserv "NOTICE $nick :(mySQL) Connecting found but mysql server has gone, please connect again."
        } else {
            ::mysql::close $mysqlhandle
            unset mysqlhandle
            putserv "NOTICE $nick :(mySQL) Disconnected from the database."
            foreach mysqltimer [timers] {
                if {[string match *reconnect* $mysqltimer]} {
                    killtimer [lindex [split [join $mysqltimer] ] 2]
                }
            }
        }
    }

    proc status {nick host hand text} {
        global mysqlhandle
        if {![info exists mysqlhandle]} {
            putserv "NOTICE $nick :(mySQL) No mysql handle, connect first."
        } elseif {[::mysql::state $mysqlhandle] == "NOT_A_HANDLE"} {
            putserv "NOTICE $nick :(mySQL) No status possible when not connected."
        } else {
            putserv "NOTICE $nick :(mySQL) State of $mysqlhandle: [::mysql::state $mysqlhandle]"
            putserv "NOTICE $nick :(mySQL) Name of the database: [::mysql::info $mysqlhandle dbname]"
            putserv "NOTICE $nick :(mySQL) Serverversion: [::mysql::info $mysqlhandle serverversion]"
            putserv "NOTICE $nick :(mySQL) Hostname: [::mysql::info $mysqlhandle host]"
            putserv "NOTICE $nick :(mySQL) Tables: [::mysql::info $mysqlhandle tables]"

        }
    }

    proc reconnect {} {
        global mysqlhandle
        variable database
        variable username
        variable password
        variable hostname
        ::mysql::close $mysqlhandle
        if {[catch { set mysqlhandle [::mysql::connect -user $username -db $database -host $hostname -password $password] } error]} {
            foreach mysqlerror [split $error \n] {
                putlog "(mySQL) $mysqlerror"
            }
        } else {
            putlog "Reconnected to: $database"
        }
        timer 300 [namespace current]::reconnect
    }

}

putlog "DBconnect.tcl Is Coded By 'Björn 'Turbo' B.'"
Advanced options for this topic:

Ignore this topic (Do not list this topic in the "unread topics" search. You are currently not ignoring this topic.)
Hide this topic (Hidden topics are not displayed in the topics list. This topic is currently not hidden.)
Go to forum

Unclassified NewsBoard 1.5.3-d | © 2003-4 by Yves Goergen | Time: 181.2 msec, CPU time: 86 msec, 30 Database queries in 124 msec | 4 kB (18.2 kB) | Timezone: +0200