cubicweb/dataimport/pgstore.py
changeset 12567 26744ad37953
parent 12504 362fdb399ff5
equal deleted inserted replaced
12566:6b3523f81f42 12567:26744ad37953
    15 #
    15 #
    16 # You should have received a copy of the GNU Lesser General Public License along
    16 # You should have received a copy of the GNU Lesser General Public License along
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    18 """Postgres specific store"""
    18 """Postgres specific store"""
    19 
    19 
    20 from __future__ import print_function
       
    21 
       
    22 import warnings
    20 import warnings
    23 import os.path as osp
    21 import os.path as osp
    24 from io import StringIO
    22 from io import StringIO
    25 from time import asctime
    23 from time import asctime
    26 from datetime import date, datetime, time
    24 from datetime import date, datetime, time
    27 from collections import defaultdict
    25 from collections import defaultdict
    28 
    26 import pickle
    29 from six import string_types, integer_types, text_type, add_metaclass
       
    30 from six.moves import cPickle as pickle, range
       
    31 
    27 
    32 from cubicweb.utils import make_uid
    28 from cubicweb.utils import make_uid
    33 from cubicweb.server.sqlutils import SQL_PREFIX
    29 from cubicweb.server.sqlutils import SQL_PREFIX
    34 from cubicweb.dataimport.stores import NoHookRQLObjectStore
    30 from cubicweb.dataimport.stores import NoHookRQLObjectStore
    35 
    31 
   106     '''Convert None value to "NULL"'''
   102     '''Convert None value to "NULL"'''
   107     return u'NULL'
   103     return u'NULL'
   108 
   104 
   109 def _copyfrom_buffer_convert_number(value, **opts):
   105 def _copyfrom_buffer_convert_number(value, **opts):
   110     '''Convert a number into its string representation'''
   106     '''Convert a number into its string representation'''
   111     return text_type(value)
   107     return str(value)
   112 
   108 
   113 def _copyfrom_buffer_convert_string(value, **opts):
   109 def _copyfrom_buffer_convert_string(value, **opts):
   114     '''Convert string value.
   110     '''Convert string value.
   115     '''
   111     '''
   116     escape_chars = ((u'\\', u'\\\\'), (u'\t', u'\\t'), (u'\r', u'\\r'),
   112     escape_chars = ((u'\\', u'\\\\'), (u'\t', u'\\t'), (u'\r', u'\\r'),
   138                                      value.second, value.microsecond)
   134                                      value.second, value.microsecond)
   139 
   135 
   140 # (types, converter) list.
   136 # (types, converter) list.
   141 _COPYFROM_BUFFER_CONVERTERS = [
   137 _COPYFROM_BUFFER_CONVERTERS = [
   142     (type(None), _copyfrom_buffer_convert_None),
   138     (type(None), _copyfrom_buffer_convert_None),
   143     (integer_types + (float,), _copyfrom_buffer_convert_number),
   139     ((int, float), _copyfrom_buffer_convert_number),
   144     (string_types, _copyfrom_buffer_convert_string),
   140     (str, _copyfrom_buffer_convert_string),
   145     (datetime, _copyfrom_buffer_convert_datetime),
   141     (datetime, _copyfrom_buffer_convert_datetime),
   146     (date, _copyfrom_buffer_convert_date),
   142     (date, _copyfrom_buffer_convert_date),
   147     (time, _copyfrom_buffer_convert_time),
   143     (time, _copyfrom_buffer_convert_time),
   148 ]
   144 ]
   149 
   145 
   183                 # database point of view.
   179                 # database point of view.
   184                 value = None
   180                 value = None
   185             for types, converter in _COPYFROM_BUFFER_CONVERTERS:
   181             for types, converter in _COPYFROM_BUFFER_CONVERTERS:
   186                 if isinstance(value, types):
   182                 if isinstance(value, types):
   187                     value = converter(value, **convert_opts)
   183                     value = converter(value, **convert_opts)
   188                     assert isinstance(value, text_type)
   184                     assert isinstance(value, str)
   189                     break
   185                     break
   190             else:
   186             else:
   191                 raise ValueError("Unsupported value type %s" % type(value))
   187                 raise ValueError("Unsupported value type %s" % type(value))
   192             # We push the value to the new formatted row
   188             # We push the value to the new formatted row
   193             # if the value is not None and could be converted to a string.
   189             # if the value is not None and could be converted to a string.