示例#1
0
def test_sqlite_fix_current():
    now_string = '2015-12-17 00:00:00.000005Z'
    now = parse(now_string)

    now_string_tz = '2015-12-17 00:00:00Z'
    now_tz = parse(now_string_tz)

    # Patch the global sqlite3
    fix_sqlite3_datetime()

    conn = sql.connect(':memory:',
                       detect_types=sql.PARSE_DECLTYPES | sql.PARSE_COLNAMES)

    cur = conn.cursor()
    cur.execute("create table test(ts timestamp)")

    cur.execute("delete from test")
    cur.execute("insert into test(ts) values (?)", (now, ))

    cur.execute("select * from test")
    test_now = cur.fetchone()[0]

    cur.execute("delete from test")
    cur.execute("insert into test(ts) values (?)", (now_tz, ))

    cur.execute("select * from test")
    test_now_tz = cur.fetchone()[0]

    assert test_now == now
    assert test_now_tz == now_tz
示例#2
0
def test_sqlite_fix_current():
    now_string = '2015-12-17 00:00:00.000005Z'
    now = parse(now_string)
    
    now_string_tz = '2015-12-17 00:00:00Z'
    now_tz = parse(now_string_tz)
    
    # Patch the global sqlite3
    fix_sqlite3_datetime()
    
    conn = sql.connect(':memory:', detect_types=sql.PARSE_DECLTYPES|sql.PARSE_COLNAMES)
    
    cur = conn.cursor()
    cur.execute("create table test(ts timestamp)")
    
    cur.execute("delete from test")  
    cur.execute("insert into test(ts) values (?)", (now,))
    
    cur.execute("select * from test")
    test_now = cur.fetchone()[0]
    
    cur.execute("delete from test")  
    cur.execute("insert into test(ts) values (?)", (now_tz,))
    
    cur.execute("select * from test")
    test_now_tz = cur.fetchone()[0]
    
    assert test_now == now
    assert test_now_tz == now_tz
示例#3
0
def test_sqlite_fixes():
    """This is all in a single test so we don't have to muck around with 
    reloading modules."""
    import python_2_7_3_sqlite3 as sql_old
    conn = sql_old.connect(':memory:',
                           detect_types=sql_old.PARSE_DECLTYPES
                           | sql_old.PARSE_COLNAMES)

    cur = conn.cursor()
    cur.execute("create table test(ts timestamp)")

    now_string = '2015-12-17 00:00:00.000005Z'
    now = parse(now_string)

    now_string_tz = '2015-12-17 00:00:00Z'
    now_tz = parse(now_string_tz)

    cur.execute("insert into test(ts) values (?)", (now, ))

    # Verify that our private copy of sqlite3 from 2.7.3 does indeed break.
    try:
        cur.execute("select * from test")
        print "Did not raise expected exception"
        assert False
    except ValueError as e:
        assert e.message == "invalid literal for int() with base 10: '000005+00:00'"

    cur.execute("delete from test")

    cur.execute("insert into test(ts) values (?)", (now_tz, ))

    try:
        cur.execute("select * from test")
        print "Did not raise expected exception"
        assert False
    except ValueError as e:
        assert e.message == "invalid literal for int() with base 10: '00+00'"

    fix_sqlite3_datetime(sql_old)

    cur.execute("delete from test")
    cur.execute("insert into test(ts) values (?)", (now, ))

    cur.execute("select * from test")
    test_now = cur.fetchone()[0]

    cur.execute("delete from test")
    cur.execute("insert into test(ts) values (?)", (now_tz, ))

    cur.execute("select * from test")
    test_now_tz = cur.fetchone()[0]

    assert test_now == now
    assert test_now_tz == now_tz
示例#4
0
def test_sqlite_fixes():
    """This is all in a single test so we don't have to muck around with 
    reloading modules."""
    import python_2_7_3_sqlite3 as sql_old
    conn = sql_old.connect(':memory:', detect_types=sql_old.PARSE_DECLTYPES|sql_old.PARSE_COLNAMES)
    
    cur = conn.cursor()
    cur.execute("create table test(ts timestamp)")
    
    now_string = '2015-12-17 00:00:00.000005Z'
    now = parse(now_string)
    
    now_string_tz = '2015-12-17 00:00:00Z'
    now_tz = parse(now_string_tz)
    
    cur.execute("insert into test(ts) values (?)", (now,))
    
   # Verify that our private copy of sqlite3 from 2.7.3 does indeed break.
    try:
        cur.execute("select * from test")
        print "Did not raise expected exception"
        assert False
    except ValueError as e:
        assert e.message == "invalid literal for int() with base 10: '000005+00:00'"
     
    cur.execute("delete from test")   
    
    cur.execute("insert into test(ts) values (?)", (now_tz,))
    
    try:
        cur.execute("select * from test")
        print "Did not raise expected exception"
        assert False
    except ValueError as e:
        assert e.message == "invalid literal for int() with base 10: '00+00'"
        
    fix_sqlite3_datetime(sql_old)
    
    cur.execute("delete from test")  
    cur.execute("insert into test(ts) values (?)", (now,))
    
    cur.execute("select * from test")
    test_now = cur.fetchone()[0]
    
    cur.execute("delete from test")  
    cur.execute("insert into test(ts) values (?)", (now_tz,))
    
    cur.execute("select * from test")
    test_now_tz = cur.fetchone()[0]
    
    assert test_now == now
    assert test_now_tz == now_tz
import threading
from collections import defaultdict
from datetime import datetime

import os
import re
from basedb import DbDriver
from volttron.platform.agent import utils
from volttron.platform.agent import json as jsonapi

utils.setup_logging()
_log = logging.getLogger(__name__)

from volttron.platform.agent.utils import fix_sqlite3_datetime
#Make sure sqlite3 datetime adapters are updated.
fix_sqlite3_datetime()

"""
Implementation of SQLite3 database operation for
:py:class:`sqlhistorian.historian.SQLHistorian` and
:py:class:`sqlaggregator.aggregator.SQLAggregateHistorian`
For method details please refer to base class
:py:class:`volttron.platform.dbutils.basedb.DbDriver`
"""
class SqlLiteFuncts(DbDriver):
    def __init__(self, connect_params, table_names):
        database = connect_params['database']
        thread_name = threading.currentThread().getName()
        _log.debug(
            "initializing sqlitefuncts in thread {}".format(thread_name))
        if database == ':memory:':
示例#6
0
from volttron.platform.agent.base_tagging import BaseTaggingService
from volttron.platform.dbutils.sqlitefuncts import SqlLiteFuncts
from volttron.utils.docs import doc_inherit

from volttron.platform.messaging.health import (STATUS_BAD,
                                                Status)
__version__ = "1.1"

utils.setup_logging()
_log = logging.getLogger(__name__)

TAGGING_SERVICE_SETUP_FAILED = 'TAGGING_SERVICE_SETUP_FAILED'


# Register a better datetime parser in sqlite3.
fix_sqlite3_datetime()

def tagging_service(config_path, **kwargs):
    """
    This method is called by the :py:func:`service.tagging.main` to
    parse the passed config file or configuration dictionary object, validate
    the configuration entries, and create an instance of SQLTaggingService

    :param config_path: could be a path to a configuration file or can be a
                        dictionary object
    :param kwargs: additional keyword arguments if any
    :return: an instance of :py:class:`service.tagging.SQLTaggingService`
    """
    _log.debug("kwargs before init: {}".format(kwargs))
    if isinstance(config_path, dict):
        config_dict = config_path