DIO - Database Interface Objects DIO Database Interface Objects ::DIO::handle interface objectName -option option -option option ... Description DIO is designed to be a generic, object-oriented interface to SQL databases. Its main goal is to be as generic as possible, but since not all SQL databases support the exact same syntaxes, keeping code generic between databases is left to the abilities of the programmer. DIO simply provides a way to keep the Tcl interface generic. - The name of the database interface. Currently supported interfaces are and . If is specified, DIO creates an object of that name. If there is no given, DIO will automatically generate a unique object ID Options -host hostname The hostname of the computer to connect to. If none is given, DIO assumes the local host. -port portNumber The port number to connect to on . -user username The username you wish to login to the server as. -pass password The password to login to the server with. -db database The name of the database to connect to. -table tableName The default table to use when using built-in commands for storing and fetching. -keyfield keyFieldname The default field to use as the primary key when using built-in commands for storing and fetching. -autokey 1 0 If this option is set to 1, DIO will attempt to determine an automatic key for when storing and fetching. In most databases, this requires that the also be specified. In the case of MySQL, where sequences do not exist, autokey must be used in conjunction with a table which has a field specified as AUTO. -sequence sequenceName If DIO is automatically generating keys, it will use this sequence as a means to gain a unique number for the stored key. DIO Object Commands objectName array request Execute as a SQL query and create an array from the first record found. The array is set with the fields of the table and the values of the record found. objectName autokey value boolean Return the current autokey value. If is specified, it sets a new value for the autokey option. objectName close Close the current database connection. This command is automatically called when the DIO object is destroyed. objectName count Return a count of the number of rows in the specified (or current) table. objectName db value Return the current database. If is specified, it sets a new value for the database. In most cases, the DIO object will automatically connect to the new database when this option is changed. objectName delete key -option option ... Delete a record from the database where the primary key matches . objectName destroy Destroy the DIO object. objectName errorinfo value contains the value of the last error, if any, to occur while executing a request. When a request fails for any reason, this variable is filled with the error message from the SQL interface package. objectName exec request Execute as an SQL query. When the exec command is called, the query is executed, and a DIO result object is returned. From there, the result object can be used to obtain information about the query status and records in a generic way. See Result Object Commands objectName fetch key arrayName -option option ... Fetch a record from the database where the primary key matches and store the result in an array called . objectName forall request arrayName body Execute an SQL select and iteratively fill the array named with elements named with the matching field names, and values containing the matching values, repeatedly executing the specified code for each row returned. objectName host value Return the current host value. If is specified, it sets a new value for the host. objectName insert table arrayName -option option ... Insert fields from into the specified in the database. objectName interface Return the database interface type, such as Postgresql or Mysql. objectName keyfield value Return the current keyfield. If is specified, it sets a new value for the keyfield. can contain multiple key fields as a Tcl list, if the table has multiple key fields. objectName keys pattern -option option ... Return a list of keys in the database. If is specified, only the keys matching will be returned. objectName lastkey Return the last key that was used from . If sequence has not been specified, this command returns an empty string. objectName list request Execute as a SQL query and return a list of the first column of each record found. objectName makekey arrayName keyfield Given an array containing key-value pairs and an optional list of key fields (we use the object's keyfield if none is specified), if we're doing auto keys, create and return a new key, otherwise if it's a single key, just return its value from the array, else if there are multiple keys, return all the keys' values from the array as a list. objectName nextkey Increment and return the next key to be used. If sequence has not been specified, this command returns an empty string. objectName open Open the connection to the current database. This command is automatically called from any command which accesses the database. objectName pass value Return the current pass value. If is specified, it sets a new value for the password. objectName port value Return the current port value. If is specified, it sets a new value for the port. objectName quote string Return the specified quoted in a way that makes it acceptable as a value in a SQL statement. objectName search -option option ... Search the current table, or the specified table if -table tableName is specified, for rows matching one or more fields as key-value pairs, and return a query result handle. See Result Object Commands For example, set res [DIO search -table people -firstname Bob] objectName sequence value Return the current sequence value. If is specified, it sets a new value for the sequence. objectName store arrayName -option option ... Store the contents of in the database, where the keys are the field names and the array's values are the corresponding values. Do an SQL insert if the corresponding row doesn't exist, or an update if it does. The table name must have been previously set or specified with -table, and the key field(s) must have been previously set or specified with -keyfield. Please note that the store method has significantly higher overhead than the update or insert methods, so if you know you are inserting a row rather than updating one, it is advisable to use the insert method and, likewise, if you know you are updating rather than inserting, to use the update method. objectName string request Execute as a SQL query and return a string containing the first record found. objectName table value Return the current table. If is specified, it sets a new value for the table. objectName update arrayName -option option ... Updates the row matching the contents of in the database. The matching row must already exist. The table can have already been set or can be specified with -table, and the key field(s) must either have been set or specified with -keyfield. objectName user value Return the current user value. If is specified, it sets a new value for the user. Result Object Commands resultObj autocache value Return the current autocache value. If is specified, it sets a new value for autocache. If autocache is true, the result object will automatically cache rows as you use them. This means that the first time you execute a forall command, each row is being cached in the result object itself and will no longer need to access the SQL result. Default is true. resultObj cache Cache the results of the current SQL result in the result object itself. This means that even if the database connection is closed and all the results of the DIO object are lost, this result object will still maintain a cached copy of its records. resultObj errorcode value Return the current errorcode value. If is specified, it sets a new value for errorcode. contains the current code from the SQL database which specifies the result of the query statement which created this object. This variable can be used to determine the success or failure of a query. resultObj errorinfo value Return the current errorinfo value. If is specified, it sets a new value for errorinfo. If an error occurred during the SQL query, DIO attempts to set the value of to the resulting error message. resultObj fields value Return the current fields value. If is specified, it sets a new value for fields. contains the list of fields used in this query. The fields are in order of the fields retrieved for each row. resultObj forall -type varName body Execute over each record in the result object. Types: -array Create as an array where the indexes are the names of the fields in the table and the values are the values of the current row. -keyvalue Set to a list containing key-value pairs of fields and values from the current row. (-field value -field value) -list Set to a list that contains the values of the current row. resultObj next -type varName Retrieve the next record in the result object. Types: -array Create as an array where the indexes are the names of the fields in the table and the values are the values of the current row. -keyvalue Set to a list containing key-value pairs of fields and values from the current row. (-field value -field value) -list Set to a list that contains the values of the current row. resultObj numrows value Return the current numrows value. If is specified, it sets a new value for numrows. is the number of rows in this result. resultObj resultid value Return the current resultid value. If is specified, it sets a new value for resultid. in most databases is the result pointer which was given us by the database. This variable is not generic and should not really be used, but it's there if you want it. resultObj rowid value Return the current rowid value. If is specified, it sets a new value for rowid. contains the number of the current result record in the result object. This variable should not really be accessed outside of the result object, but it's there if you want it.