tdbc::connection(n)

✨ NAME ✨

Okay so welcome to tdbc::connection(n), the manual page for the Tcl Database Connectivity connection object β€” manual name folded in near the top like a little name tag, because we're cute like that. πŸŽ€

tdbc::connection β€” TDBC connection object. That's the name, bestie, and it's giving "database connection but make it Tcl." πŸ’…


πŸŽ€ SYNOPSIS πŸŽ€

Here's the whole synopsis, and no cap β€” every line, every flag, every placeholder is staying exactly as written. Don't touch the punctuation, don't rename the options, just vibe with it. πŸ¦‹

       package require tdbc 1.0
       package require tdbc::driver version

       tdbc::driver::connection create db ?-option value...?

       db configure ?-option value...?
       db close
       db foreignkeys ?-primary tableName? ?-foreign tableName?
       db prepare sql-code
       db preparecall call
       db primarykeys tableName
       db statements
       db resultsets
       db tables ?pattern?
       db columns table ?pattern?
       db begintransaction
       db commit
       db rollback
       db transaction script
       db allrows ?-as lists|dicts? ?-columnsvariable name? ?--? sql-code
              ?dictionary?
       db foreach ?-as lists|dicts? ?-columnsvariable name? ?--? varName
              sqlcode ?dictionary? script

Every one of those lines is part of the synopsis, so yes β€” ?--? stays, ?-as lists|dicts? stays, and ?-columnsvariable name? stays exactly where it is. πŸ’–

πŸ’– DESCRIPTION πŸ’–

okay so here's the whole vibe of a TDBC connection, bestie. Every database driver for TDBC (Tcl DataBase Connectivity) implements a connection object that represents a connection to a database. By convention, this object is created by the command tdbc::driver::connection create. That command takes the name of a Tcl command that's gonna represent the connection, plus a possible set of options (peep CONFIGURATION OPTIONS). It establishes a connection to the database and returns the name of the newly-created Tcl command. slay. ✨

The configure object command on a database connection? Okay, she's flexible:

  • Give her no arguments and she returns a list of alternating keywords and values representing the connection's current configuration.
  • Give her a single argument -option and she returns the configured value of that one option.
  • Otherwise she needs an even number of arguments, alternating options and values. The specified options get the specified values, and nothing is returned. (no take-backsies, no receipts πŸ’…)

πŸŽ€ close

The close object command on a database connection closes the connection. All active statements and result sets on the connection are closed. Any uncommitted transaction is rolled back. The object command is deleted. it's giving "log off, she's done." πŸ’€


🌸 prepare

The prepare object command on a database connection prepares a SQL statement for execution. The sql-code argument must contain a single SQL statement to be executed β€” one, bestie, not a whole stack. Bound variables may be included. The return value is a newly-created Tcl command that represents the statement. See tdbc::statement for a more detailed discussion of the SQL accepted by the prepare object command and the interface accepted by a statement. 🫢


πŸ¦‹ preparecall

On a database connection where the underlying database and driver support stored procedures, the preparecall object command prepares a call to a stored procedure for execution. The syntax of the stored procedure call is literally this, exactly:

?resultvar =? procname(?arg ?, arg...?)

The return value is a newly-created Tcl command that represents the statement. See tdbc::statement for the interface accepted by a statement. ✨


πŸ’œ statements

The statements object command returns a list of statements that have been created by prepare and preparecall statements against the given connection and have not yet been closed. it's the group chat roster of statements still in the building.


πŸ“ resultsets

The resultsets object command returns a list of result sets that have been obtained by executing statements prepared using the given connection and not yet closed. same energy, different list. 🌈


πŸ‘‘ tables

The tables object command lets your program query the connection for the names of tables that exist in the database. The optional pattern parameter is a pattern to match the name of a table. It may contain the SQL wild-card characters '%' and β€” okay, heads up, the manpage itself drops a word right here and then picks back up with and whose values are subdictionaries. So, quoting the source exactly: it may contain the SQL wild-card characters '%' and and whose values are subdictionaries. See the documentation for the individual database driver for the interpretation of the values. πŸ™ˆ (that's a source-text thing, not me being chaotic, I promise)


🧚 columns

The columns object command lets your program query the connection for the names of columns that exist in a given table. The optional pattern parameter is a pattern to match the name of a column. It may contain the SQL wild-card characters '%' and (same source gap, same story) whose values are dictionaries. Each of the subdictionaries will contain at least the following keys and values, and may contain others whose usage is determined by a specific database driver:

  • type β€” contains the data type of the column, and will generally be chosen from the set, bigint, binary, bit, char, date, decimal, double, float, integer, longvarbinary, longvarchar, numeric, real, time, timestamp, smallint, tinyint, varbinary, and varchar. (If the column has a type that cannot be represented as one of the above, type will contain a driver-dependent description of the type.)
  • precision β€” contains the precision of the column in bits, decimal digits, or the width in characters, according to the type.
  • scale β€” contains the scale of the column (the number of digits after the radix point), for types that support the concept.
  • nullable β€” contains 1 if the column can contain NULL values, and 0 otherwise.

πŸ’… primarykeys

The primarykeys object command lets your program query the connection for the primary keys belonging to a given table. The tableName parameter identifies the table being interrogated. The result is a list of dictionaries enumerating the keys (in a similar format to the list returned by $connection allrows -as dicts). The keys of the dictionary may include at least the following. Values that are NULL or meaningless in a given database are omitted. 🫢

  • tableCatalog β€” name of the catalog in which the table appears.
  • tableSchema β€” name of the schema in which the table appears.
  • tableName β€” name of the table owning the primary key.
  • constraintCatalog β€” name of the catalog in which the primary key constraint appears. In some database systems, this may not be the same as the table's catalog.
  • constraintSchema β€” name of the schema in which the primary key constraint appears. In some database systems, this may not be the same as the table's schema.
  • constraintName β€” name of the primary key constraint,
  • columnName β€” name of a column that is a member of the primary key.
  • ordinalPosition β€” ordinal position of the column within the primary key.

To these columns may be added additional ones that are specific to a particular database system. ✨


πŸ”₯ foreignkeys

The foreignkeys object command lets your program query the connection for foreign key relationships that apply to a particular table. The relationships may be constrained to the keys that appear in a particular table (-foreign tableName), the keys that refer to a particular table (-primary tableName), or both. At least one of -primary and -foreign should be specified, although some drivers will enumerate all foreign keys in the current catalog if both options are omitted.

The result of the foreignkeys object command is a list of dictionaries, with one list element per key (in a similar format to the list returned by $connection allrows -as dicts). The keys of the dictionary may include at least the following. Values that are NULL or meaningless in a given database are omitted. πŸ’€

  • foreignConstraintCatalog β€” catalog in which the foreign key constraint appears.
  • foreignConstraintSchema β€” schema in which the foreign key constraint appears.
  • foreignConstraintName β€” name of the foreign key constraint.
  • primaryConstraintCatalog β€” catalog holding the primary key constraint (or unique key constraint) on the column to which the foreign key refers.
  • primaryConstraintSchema β€” schema holding the primary key constraint (or unique key constraint) on the column to which the foreign key refers.
  • primaryConstraintName β€” name of the primary key constraint (or unique key constraint) on the column to which the foreign key refers.
  • updateAction β€” action to take when an UPDATE statement invalidates the constraint. The value will be CASCADE, SET DEFAULT, SET NULL, RESTRICT, or NO ACTION.
  • deleteAction β€” action to take when a DELETE statement invalidates the constraint. The value will be CASCADE, SET DEFAULT, SET NULL, RESTRICT, or NO ACTION.
  • primaryCatalog β€” catalog name in which the primary table (the one to which the foreign key refers) appears.
  • primarySchema β€” schema name in which the primary table (the one to which the foreign key refers) appears.
  • primaryTable β€” table name of the primary table (the one to which the foreign key refers).
  • primaryColumn β€” name of the column to which the foreign key refers.
  • foreignCatalog β€” name of the catalog in which the table containing the foreign key appears.
  • foreignSchema β€” name of the schema in which the table containing the foreign key appears.
  • foreignTable β€” name of the table containing the foreign key.
  • foreignColumn β€” name of the column appearing in the foreign key.
  • ordinalPosition β€” position of the column in the foreign key, if the key is a compound key.

🌟 begintransaction

The begintransaction object command on a database connection begins a transaction on the database. Now listen: if the underlying database does not support atomic, consistent, isolated, durable transactions, the begintransaction object command returns an error reporting the fact. Similarly, if multiple begintransaction commands are executed withough an intervening commit or rollback command, an error is returned unless the underlying database supports nested transactions. (yes, "withough" β€” we stan a typo origin story 😭 but the rule stands: don't stack transactions unless the database says nested is fine.)


🌈 commit

The commit object command on a database connection ends the most recent transaction started by begintransaction and commits changes to the database. periodt. βœ…


πŸ¦‹ rollback

The rollback object command on a database connection rolls back the most recent transaction started by begintransaction. The state of the database is as if nothing happened during the transaction. genuinely iconic β€” the "no memory of this" of databases πŸ’…


πŸŽ€ transaction

The transaction object command on a database connection presents a simple way of bundling a database transaction. Here's the whole choreography, bestie:

  1. It begins a transaction.
  2. It evaluates the supplied script argument as a Tcl script in the caller's scope.
  3. If script terminates normally, or by break, continue, or return, the transaction is committed (and any action requested by break, continue, or return takes place).
  4. If the commit fails for any reason, the error in the commit is treated as an error in the script.
  5. In the case of an error in script or in the commit, the transaction is rolled back and the error is rethrown.
  6. Any nonstandard return code from the script causes the transaction to be rolled back and then is rethrown.

she handles the cleanup so you don't have to. main character energy fr fr πŸ‘‘


⭐ allrows

The allrows object command prepares a SQL statement (given by the sql-code parameter) to execute against the database. It then executes it (see tdbc::statement for details) with the optional dictionary parameter giving bind variables. Finally, it uses the allrows object command on the result set (see tdbc::resultset) to construct a list of the results. Finally, both result set and statement are closed. The return value is the list of results. one-shot, tidy, done. 🫢


πŸ“ foreach

The foreach object command prepares a SQL statement (given by the sql-code parameter) to execute against the database. It then executes it (see tdbc::statement for details) with the optional dictionary parameter giving bind variables. Finally, it uses the foreach object command on the result set (see tdbc::resultset) to evaluate the given script for each row of the results. Finally, both result set and statement are closed, even if the given script results in a return, an error, or an unusual return code. no leaked handles on her watch ✨

🌈 CONFIGURATION OPTIONS 🌈

Okay bestie, these are the config options you can pass when the connection is created and on the connection's configure object command β€” and yes, there may also be others specific to a database driver. πŸ‘€βœ¨

  • -encoding name β€” Sets the encoding used to connect to the database. name should be one of the names accepted by the encoding command. This option is usually unnecessary; most database drivers can figure out the encoding in use by themselves. πŸŽ€
  • -isolation level β€” Sets the transaction isolation level needed for transactions on the database. The acceptable values for level are shown under TRANSACTION ISOLATION LEVELS. 🎚️
  • -timeout ms β€” Sets the maximum time to wait for an operation on the database engine before reporting an error to the caller. The ms argument gives the maximum time in milliseconds. A value of zero β€” the default β€” specifies that the calling process is to wait indefinitely for database operations. ⏳
  • -readonly flag β€” Specifies that the connection will not modify the database if the Boolean parameter flag is true, or that it may modify the database if flag is false. If flag is true, this option may have the effect of raising the transaction isolation level to readonly. πŸ”’

πŸ¦‹ TRANSACTION ISOLATION LEVELS πŸ¦‹

The acceptable values for the -isolation configuration option are as follows:

  • readuncommitted β€” Allows the transaction to read "dirty", that is, uncommitted data. This isolation level may compromise data integrity, does not guarantee that foreign keys or uniqueness constraints are satisfied, and in general does not guarantee data consistency. πŸ’€
  • readcommitted β€” Forbids the transaction from reading "dirty" data, but does not guarantee repeatable reads; if a transaction reads a row of a database at a given time, there is no guarantee that the same row will be available at a later time in the same transaction. 🫢
  • repeatableread β€” Guarantees that any row of the database, once read, will have the same values for the life of a transaction. Still permits "phantom reads" (that is, newly-added rows appearing if a table is queried a second time). πŸ‘»
  • serializable β€” The most restrictive (and most expensive) level of transaction isolation. Any query to the database, if repeated, will return precisely the same results for the life of the transaction, exactly as if the transaction is the only user of the database. πŸ‘‘
  • readonly β€” Behaves like serializable in that the only results visible to the transaction are those that were committed prior to the start of the transaction, but forbids the transaction from modifying the database. 🚫

A database that does not implement one of these isolation levels will instead use the next more restrictive isolation level. If the given level of isolation cannot be obtained, the database interface throws an error reporting the fact. The default isolation level is readcommitted. 🌸

🚨 A script should not the isolation level when a transaction is in progress.

πŸ”— SEE ALSO πŸ”—

Go peek at encoding(n), tdbc(n), tdbc::resultset(n), tdbc::statement(n), and tdbc::tokenize(n). πŸ‘€πŸ’–


πŸŽ€ KEYWORDS πŸŽ€

TDBC, SQL, database, connectivity, connection, resultset, statement β€” iykyk. πŸ’…


Copyright (c) 2008 by Kevin B. Kenny.


That's the whole vibe, bestie! Source/version footer: Tcl 8.6, page tdbc::connection(n), and the original didn't print a date β€” so no date to copy, periodt. πŸ’–βœ¨