Menu

Some remarks

Help
2004-07-22
2004-07-26
  • Leopold Schwinger

    Hello Folks!

    I played a bit with PDO and found some interessting results:

    Environment:
    - Windows XP Professional, ServicePack 1
    - Python 2.3.4
    - PostgreSQL 7.4.1 via Cygwin
    - psycopg 1.1.12
    - PDO 1.2.2

    This is my table, I tested with:

    id|mod_id|table_name
    --+------+------------------
    36|    35|'ADMIN_LANG'
    37|    35|'ADMIN_TEXT'
    38|    35|'ADMIN_ERROR'
    39|    35|'ADMIN_TRACE'
    40|    35|'ADMIN_TRACEPOS'
    49|    48|'BASIC_USER'
    50|    48|'BASIC_GROUP'
    51|    48|'BASIC_GROUPPOS'
    52|    48|'BASIC_MANDATE'
    53|    48|'BASIC_MANDATEPOS'
    54|    48|'BASIC_MENU'
    55|    48|'BASIC_PRIV'
    71|    70|'LEAD_STATUS'
    72|    70|'LEAD_LIST'

    This is my Script:

    <script>
    import psycopg
    import pdo

    myDB = pdo.connect("Module=psycopg;<...>")

    if myDB.active:
        sStmt = "SELECT * FROM MOD_TABLE"
        kRS = myDB.open(sStmt)
        # print kRS.__dict__ --> this does not work (see below)
        print " === Resultset === "
        print "row_ount:         " + str(kRS.row_count())
        print "keys:             " + str(kRS.fields.keys())
        print "has_key: ID       " + str(kRS.has_key('ID'))      # just to check for case-sensitivity
        print "has_key: id       " + str(kRS.has_key('id'))
        print "array_from_column " + str(kRS.array_from_column('table_name'))
       
        myA = kRS.array_from_column('table_name')
        print len(myA)
       
        print "=" * 10
        while kRS.next():
            print kRS.fields['id'].value, kRS.fields['mod_id'].value, kRS.fields['table_name'].value

    </script>

    Is not too heavy, so here are my remarks:

    1.) DOCUMENTATION:
    resultset.row_count is a method, so you have to call resultset.row_count() (with paranthesis)

    ===============================================================

    2.) It seems, that the resultset.array_from_column(<key>) method ignores the first entry.
    so recordset.row_count() has n entries and recordset.array_from_colum(<key>) has n-1 entries

    P.S: what about NULL-Values?.
    Example:

    id | name
    ---+-----
    1  | one
    2  | two
    3  | <NULL>
    4  | four
    5  | <NULL>

    If you make recordset.array_from_column('nr'), should the resulting array have 3 or 5 entries?

    ===============================================================

    3.) If I have a resultset and i call
        print myResultSet.__dict__
    I get the error-message:
    <message>
    D:\develop\python\database>pdo_test_1.py
    === Resultset ===
    __dict__:
    {'_resultsource__column': 0, 'fields': {'mod_id': Traceback (most recent call last):
      File "D:\develop\python\database\pdo_test_1.py", line 17, in ?
        print kRS.__dict__
    TypeError: 'NoneType' object is not callable
    </message>

    ==> Well, the funny thing is, that the TypeError appears after 'mod_id'

    ===============================================================

    4.) It is possible to set the connection.paramstyle-Parameter
    to whatevervalue you want, shouldn't be this checked for
    the 5 possible values: 'qmark', 'numeric', 'named', 'format' and 'pyformat'

    so
         myDB.paramstyle = 'foobar' works. I even
         think, that connection.paramstyle is completely ignored...
        
    because

    <script>
        myDB.paramstyle = 'qmark'
        sStmt = "SELECT * FROM MOD_TABLE WHERE MOD_ID = %(MOD_ID)s"
        kRS = myDB.open(sStmt, {'MOD_ID': mod_id})
    </script>

    works, though it shouldn't, or?

     
    • Jonathan M. Franz

      Heyo, we're going to fix the docs for #1, comments on the rest are below.  Please note, this is my quickie off-the-cuff remarks, so I won't have final answers on everything, but I wanted to let you know we're looking into things.

      #2 is a good point I hadn't thought of, I think we'll add a extra argument to allow for the default value to use if None/Null values are encountered.  We're looking into the off-by-1 problem as well.

      #3 is very wierd, we're setting up a postgreesql on cygwin system to test with to dig into it.

      #4 is actualy a docs problem, coupled with a lack of a  __setattr__() method to prevent writing to paramstyle.  paramstyle should be read-only, and assigning to it will not change the actual underlying paramstyle the driver is expecting, so your example works.  In the next poiint release we'll raise an error on attempts to assign to paramstyle

       
    • Bryan J Gudorf

      Bryan J Gudorf - 2004-07-26

          The documentation issue with row_count() as well as the issue with the first row not being grabbed in array_from_column is now addressed and fixed in PDO 1.3.0

       
    • Bryan J Gudorf

      Bryan J Gudorf - 2004-07-26

      Also in reference to your question about nulls.

      If you have the following resultset:
      id | name
      ---+-----
      1 | one
      2 | two
      3 | <NULL>
      4 | four
      5 | <NULL>

      and you do an array_from_column on it:

      >>d = rs.array_from_column("name")

      you will get back 5 entries. The entries with NULL Values will show up as None:

      >>d
      ['one','two',None,'four',None]

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.