cubicweb/server/sqlutils.py
changeset 12567 26744ad37953
parent 12508 a8c1ea390400
child 12808 6cbb1e2a6e49
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 """SQL utilities functions and classes."""
    18 """SQL utilities functions and classes."""
    19 
    19 
    20 from __future__ import print_function
       
    21 
       
    22 import os
    20 import os
    23 import sys
    21 import sys
    24 import re
    22 import re
    25 import subprocess
    23 import subprocess
    26 from os.path import abspath
    24 from os.path import abspath
    27 from logging import getLogger
    25 from logging import getLogger
    28 from datetime import time, datetime, timedelta
    26 from datetime import time, datetime, timedelta
    29 
    27 
    30 from six import string_types, text_type
       
    31 from six.moves import filter
       
    32 
       
    33 from pytz import utc
    28 from pytz import utc
    34 
    29 
    35 from logilab import database as db, common as lgc
    30 from logilab import database as db, common as lgc
    36 from logilab.common.shellutils import ProgressBar, DummyProgressBar
    31 from logilab.common.shellutils import ProgressBar, DummyProgressBar
    37 from logilab.common.logging_ext import set_log_methods
    32 from logilab.common.logging_ext import set_log_methods
    50 
    45 
    51 def _run_command(cmd, extra_env=None):
    46 def _run_command(cmd, extra_env=None):
    52     env = os.environ.copy()
    47     env = os.environ.copy()
    53     for key, value in (extra_env or {}).items():
    48     for key, value in (extra_env or {}).items():
    54         env.setdefault(key, value)
    49         env.setdefault(key, value)
    55     if isinstance(cmd, string_types):
    50     if isinstance(cmd, str):
    56         print(cmd)
    51         print(cmd)
    57         return subprocess.call(cmd, shell=True, env=env)
    52         return subprocess.call(cmd, shell=True, env=env)
    58     else:
    53     else:
    59         print(' '.join(cmd))
    54         print(' '.join(cmd))
    60         return subprocess.call(cmd, env=env)
    55         return subprocess.call(cmd, env=env)
    79     if hasattr(cursor_or_execute, 'execute'):
    74     if hasattr(cursor_or_execute, 'execute'):
    80         execute = cursor_or_execute.execute
    75         execute = cursor_or_execute.execute
    81     else:
    76     else:
    82         execute = cursor_or_execute
    77         execute = cursor_or_execute
    83     sqlstmts_as_string = False
    78     sqlstmts_as_string = False
    84     if isinstance(sqlstmts, string_types):
    79     if isinstance(sqlstmts, str):
    85         sqlstmts_as_string = True
    80         sqlstmts_as_string = True
    86         sqlstmts = sqlstmts.split(delimiter)
    81         sqlstmts = sqlstmts.split(delimiter)
    87     if withpb:
    82     if withpb:
    88         if sys.stdout.isatty():
    83         if sys.stdout.isatty():
    89             pb = ProgressBar(len(sqlstmts), title=pbtitle)
    84             pb = ProgressBar(len(sqlstmts), title=pbtitle)
   473             if rset.description:
   468             if rset.description:
   474                 found_date = False
   469                 found_date = False
   475                 for row, rowdesc in zip(rset, rset.description):
   470                 for row, rowdesc in zip(rset, rset.description):
   476                     for cellindex, (value, vtype) in enumerate(zip(row, rowdesc)):
   471                     for cellindex, (value, vtype) in enumerate(zip(row, rowdesc)):
   477                         if vtype in ('TZDatetime', 'Date', 'Datetime') \
   472                         if vtype in ('TZDatetime', 'Date', 'Datetime') \
   478                            and isinstance(value, text_type):
   473                            and isinstance(value, str):
   479                             found_date = True
   474                             found_date = True
   480                             value = value.rsplit('.', 1)[0]
   475                             value = value.rsplit('.', 1)[0]
   481                             try:
   476                             try:
   482                                 row[cellindex] = strptime(value, '%Y-%m-%d %H:%M:%S')
   477                                 row[cellindex] = strptime(value, '%Y-%m-%d %H:%M:%S')
   483                             except Exception:
   478                             except Exception:
   484                                 row[cellindex] = strptime(value, '%Y-%m-%d')
   479                                 row[cellindex] = strptime(value, '%Y-%m-%d')
   485                             if vtype == 'TZDatetime':
   480                             if vtype == 'TZDatetime':
   486                                 row[cellindex] = row[cellindex].replace(tzinfo=utc)
   481                                 row[cellindex] = row[cellindex].replace(tzinfo=utc)
   487                         if vtype == 'Time' and isinstance(value, text_type):
   482                         if vtype == 'Time' and isinstance(value, str):
   488                             found_date = True
   483                             found_date = True
   489                             try:
   484                             try:
   490                                 row[cellindex] = strptime(value, '%H:%M:%S')
   485                                 row[cellindex] = strptime(value, '%H:%M:%S')
   491                             except Exception:
   486                             except Exception:
   492                                 # DateTime used as Time?
   487                                 # DateTime used as Time?
   515         def step(self, value):
   510         def step(self, value):
   516             if value is not None:
   511             if value is not None:
   517                 self.values.add(value)
   512                 self.values.add(value)
   518 
   513 
   519         def finalize(self):
   514         def finalize(self):
   520             return ', '.join(text_type(v) for v in self.values)
   515             return ', '.join(str(v) for v in self.values)
   521 
   516 
   522     cnx.create_aggregate("GROUP_CONCAT", 1, group_concat)
   517     cnx.create_aggregate("GROUP_CONCAT", 1, group_concat)
   523 
   518 
   524     def _limit_size(text, maxsize, format='text/plain'):
   519     def _limit_size(text, maxsize, format='text/plain'):
   525         if len(text) < maxsize:
   520         if len(text) < maxsize: