cpdo  Artifact Content

Artifact 34519300fe07b0b45915ba8a17584f9ca63b7aab:

Wiki page [DSN] by stephan 2011-05-17 18:39:27.
D 2011-05-17T18:39:27.951
L DSN
P d7b5fe3287853879baab58a47cb046087e9657d2
U stephan
W 3117
<strong>ACHTUNG: THE CPDO WIKI IS NOW (AS OF 2011-May-17) MAINTAINED ON A DEDICATED WIKI SITE:</strong> [http://whiki.wanderinghorse.net/wikis/cpdo/?page=DSN]

A <em>DSN</em>, or Data Source Name (i think!), is a string which contains information about which database type we want to connect to and driver-specific parameters. They do not contain user/password information - those are provided as explicit connection arguments.

A DSN always starts with the name of the driver responsible for handling it, then a colon, then any driver-specific parameters, as shown in these examples:

   *  <tt>sqlite3:dbfile.sqlite</tt>
   *  <tt>mysql5:dbname=mydb;autocommit=true;port=3036;host=my.server.com</tt>

See the driver-specific pages for notes about driver-specific DSN parameters: [cpdo_sqlite3], [cpdo_mysql5]

Driver-specific parameters are simple key-value pairs, with each pair being separated by semicolons. Each driver has its own list of options it supports. (Drivers <em>may</em> ignore options they don't understand, for the sake of portability, or <em>may</em> return <tt>cpdo_rc.UnsupportedError</tt>.) DSNs are not intended to store complex data, like strings which themselves may contain semicolons. Numbers, boolean, and word-like values are fine, however. Boolean values can be set to true by setting them to a value which starts with any of the characters in the set <tt><nowiki>[yYtT1]</nowiki></tt> (the number 1, not the letter el) . All other values are treated as boolean false.

<em>To avoid confusion and subtle incompatibilities regarding case-sensitivity, i now hereby decree that Lower Case is Law, that all option names shall be all lower-case, always, and that lower-case shall be the exclusive form of all option names. Always and forever! Amen!</em>

If you feel you <em>must</em> use a separator character when creating new options, <em>please</em> use an underscore instead of a minus sign, to avoid potential token-parsing confusion later on (an underscore is conventionally legal in identifier strings, whereas minus signs are supported in some metalanguages but not in the vast majority of programming languages).

Example of connecting using a DSN, complete with error handling:

<verbatim>
cpdo_driver * db = NULL; // don't forget the NULL!
char const * dsn = "sqlite3:mydb.sqlite";
int rc = cpdo_driver_new_connect( &db, dsn, "user", "passwd" );
if( 0 != rc ) {
    printf("Error #%d (%s)\n", rc, cpdo_rc_string(rc) );
    if( db ) { // this means we found a driver but connection failed
        char const * msg = NULL;
        rc = 0 /* We do this to avoid a potentially confusing corner
                  case in error reporting when error_info() fails.
                  In that case, we would be reporting a cpdo_rc
                  code in place of the DB's (driver-dependent) error code.
                */;
        db->api->error_info(db, &msg, NULL, &rc);
        printf("Db error #%d: %s\n", rc, msg );
        db->api->close(db);
    }
    return ...;
}

... use db ... when done, close it ...

db->api->close(db);
</verbatim>

Z 9bb889fec028a32fd0103f5459563498