⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 e32dbmodule.cpp

📁 python s60 1.4.5版本的源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
  TRAPD(error, (self->view_obj->view.FirstL() ));
  self->view_obj->rowReady=0;
  RETURN_ERROR_OR_PYNONE(error);
}

extern "C" PyObject *
dbview_count_line(dbview_object *self)
{
  ASSERT_ISPREPARED;
  TInt number = 0;

  TRAPD(error, (number = self->view_obj->view.CountL() ));
  if (error != KErrNone)
    return SPyErr_SetFromSymbianOSErr(error);
  else
    return Py_BuildValue("i", number);
}

extern "C" PyObject *
dbview_colCount(dbview_object *self)
{
  ASSERT_ISPREPARED;
  return Py_BuildValue("i", self->view_obj->view.ColCount());
}

extern "C" PyObject *
dbview_next_line(dbview_object *self)
{
  ASSERT_ISPREPARED;
  if (self->view_obj->view.AtEnd()) {
    PyErr_SetString(PyExc_RuntimeError, "End of view, there is no next line.");
    return NULL;
  }
  // Note: Due to a bug in Symbian, NextL will crash the entire system 
  // if you go beyond the last row in the view.
  TRAPD(error, (self->view_obj->view.NextL() ));
  self->view_obj->rowReady=0;
  RETURN_ERROR_OR_PYNONE(error);
}

extern "C" PyObject *
dbview_get_line(dbview_object *self)
{
  ASSERT_ISPREPARED;
  ASSERT_ATROW;
  TRAPD(error, (self->view_obj->view.GetL() ));
  self->view_obj->rowReady=(error == KErrNone);
  RETURN_ERROR_OR_PYNONE(error);
}

static PyObject *
PyUnicode_FromColumnL(RDbView& view, TDbColNo column)
{
  RDbColReadStream readStream;    
  DPYPRINTF("xopenlc");
  readStream.OpenLC(view, column);
  DPYPRINTF("get collength");
  int colLength=view.ColLength(column);
  DPYPRINTF("collength %d", colLength);
  DPYPRINTF("fromunicode");
  PyObject *obj=PyUnicode_FromUnicode(NULL,colLength);
  DPYPRINTF("asunicode");
  TPtr16 ptr(PyUnicode_AsUnicode(obj),colLength);
  DPYPRINTF("readl");
  readStream.ReadL(ptr);
  DPYPRINTF("pop");
  CleanupStack::PopAndDestroy(&readStream);
  DPYPRINTF("return");
  return obj;
}


// Extracts a Unicode object from a LONG VARCHAR column.
static PyObject *
col_longtext(dbview_object *self, PyObject *args)
{
  ASSERT_ISPREPARED;
  ASSERT_ATROW;
  ASSERT_ROWREADY;
  int column = 0;
  if (!PyArg_ParseTuple(args, "i", &column))
    return NULL;

  ASSERT_VALIDCOLUMN;

  // GCC and MSVC6 interpret this differently.
  // MSVC6 sees this as a _function_ declaration.
  // RDbView& view(self->view_obj->view); 
  RDbView& view=self->view_obj->view;
  /* check if the given column is the proper type*/
  if ( (view.ColType((TDbColNo)column)) != EDbColLongText) {
    PyErr_SetString(PyExc_TypeError, "Wrong column type. Required EDbColLongText");
    return NULL;
  } 
 
  PyObject *obj=NULL;
  DPYPRINTF("Getting column value...");
  TRAPD(error,obj=PyUnicode_FromColumnL(view,column));
  DPYPRINTF("Got column value.");
  if (error != KErrNone) {
    pyprintf("Symbian error %d\n",error);
    return SPyErr_SetFromSymbianOSErr(error);
  }
  return obj;
}

extern "C" PyObject *
dbview_col_raw(dbview_object *self, PyObject *args)
{
  ASSERT_ISPREPARED;
  ASSERT_ATROW;
  ASSERT_ROWREADY;
  int column = 0;

  if (!PyArg_ParseTuple(args, "i", &column))
    return NULL;

  ASSERT_VALIDCOLUMN;
  
  /* check if the given column is the proper type*/
  int coltype=self->view_obj->view.ColType((TDbColNo)column);
  if ( coltype == EDbColLongText || coltype == EDbColLongBinary) {
    PyErr_SetString(PyExc_TypeError, "This function doesn't support LONG columns.");
    return NULL;
  } 
  TPtrC8 text = (self->view_obj->view.ColDes8((TDbColNo)column));
  return Py_BuildValue("s#", text.Ptr(), text.Length());
}

extern "C" PyObject *
dbview_col_rawtime(dbview_object *self, PyObject *args)
{
  ASSERT_ISPREPARED;
  ASSERT_ATROW;
  ASSERT_ROWREADY;
  int column = 0;
  if (!PyArg_ParseTuple(args, "i", &column))
    return NULL;
  ASSERT_VALIDCOLUMN;
  /* check if the given column is the proper type*/
  int coltype=self->view_obj->view.ColType((TDbColNo)column);
  if ( coltype != EDbColDateTime ) {
    PyErr_SetString(PyExc_TypeError, "Column must be of date/time type.");
    return NULL;
  } 
  return Py_BuildValue("L", self->view_obj->view.ColTime(column).Int64());    
}

// Generic column access function. 
// Returns the proper Python value for the given column type.
extern "C" PyObject *
dbview_col(dbview_object *self, PyObject *args)
{
  ASSERT_ISPREPARED;
  ASSERT_ATROW;
  ASSERT_ROWREADY;
  int column;
  if (!PyArg_ParseTuple(args, "i", &column))
    return NULL;
  ASSERT_VALIDCOLUMN;
  RDbView& view=self->view_obj->view;
  double doubleValue;
  switch (view.ColType(column)) {
  case EDbColLongText: 
    return col_longtext(self,args);
  case EDbColText16: // VARCHAR, CHAR
    {
      TPtrC16 textValue = view.ColDes16(column);
      return Py_BuildValue("u#", textValue.Ptr(), textValue.Length());
    }
  case EDbColText8:    
  case EDbColBinary:
    return dbview_col_raw(self,args);
  case EDbColDateTime: 
    {     
      // There will be roundoff errors here.
#ifndef EKA2
      double unixtime=
	(view.ColTime(column).Int64()-TTime(KUnixEpoch).Int64()).GetTReal();
#else
      double unixtime= 
	TReal64(view.ColTime(column).Int64()-TTime(KUnixEpoch).Int64());
#endif
      unixtime/=1e6;
      unixtime-=(TLocale().UniversalTimeOffset()).Int();
      return Py_BuildValue("d", unixtime);
    } 
  case EDbColInt64:    
    return Py_BuildValue("L", view.ColInt64(column));    
  case EDbColInt32:   // INTEGER
  case EDbColInt16:   // SMALLINT
  case EDbColInt8:    // TINYINT
    return Py_BuildValue("l", view.ColInt32(column));
  case EDbColUint32:  // UNSIGNED INTEGER
  case EDbColUint16:  // UNSIGNED SMALLINT
  case EDbColUint8:   // UNSIGNED TINYINT
  case EDbColBit:     // BIT
    return Py_BuildValue("l", view.ColUint32(column));
  case EDbColReal32:  // REAL
    doubleValue=view.ColReal32(column);
    return Py_BuildValue("d", doubleValue);
  case EDbColReal64:  // FLOAT, DOUBLE, DOUBLE PRECISION
    doubleValue=view.ColReal64(column);
    return Py_BuildValue("d", doubleValue);
  default:
    char buf[80];
    sprintf(buf,"Unknown column type: %d %d",view.ColType(column),EDbColBit);
    PyErr_SetString(PyExc_TypeError, buf);
    return NULL;    
  }
}

/* Interpret the given long integer as a Symbian time value
   (microseconds since 0 AD, local time) and return the value as a
   Unicode string, formatted in a form that, surrounded with #-marks,
   is suitable for use as a date/time literal in an SQL INSERT or
   UPDATE statement. */

_LIT(KFormatTxt,"%1-%2-%3 %-B%J:%T:%S.%C%+B");

static PyObject *
format_ttime(TTime timeValue)
{
  TBuf<32> timeString;  
  TRAPD(error,(timeValue.FormatL(timeString,KFormatTxt)));
  if (error != KErrNone) {
    return SPyErr_SetFromSymbianOSErr(error);
  }
  return Py_BuildValue("u#",timeString.Ptr(),timeString.Length());
}


extern "C" PyObject *
format_rawtime(PyObject *self, PyObject *args)
{
#ifdef EKA2
  LONG_LONG tv;
  if (!PyArg_ParseTuple(args, "L", &tv))
    return NULL;
  unsigned int tv_low, tv_high; 
  /* Assuming little-endian platform here. */
  tv_low=*(unsigned int *)(&tv); 
  tv_high=*((unsigned int *)(&tv)+1);
  TInt64 hi_and_lo;
  TUint32* lo=(TUint32*)&hi_and_lo;
  TUint32* hi=((TUint32*)&hi_and_lo)+1;
  *lo=tv_low;
  *hi=tv_high;
  TTime timeValue=hi_and_lo;
  return format_ttime(timeValue);  
#else  
  LONG_LONG tv;
  if (!PyArg_ParseTuple(args, "L", &tv))
    return NULL;
  unsigned int tv_low, tv_high; 
  /* Assuming little-endian platform here. I'm sure hell will freeze
     over before that changes. */
  tv_low=*(unsigned int *)(&tv); 
  tv_high=*((unsigned int *)(&tv)+1);
  TTime timeValue=TInt64(tv_high, tv_low);
  return format_ttime(timeValue);
#endif
}


extern "C" PyObject *
format_time(PyObject *self, PyObject *args)
{
  double unixtime;
  if (!PyArg_ParseTuple(args, "d", &unixtime))
    return NULL;
  TTime timeValue=
    TTime(TTime(KUnixEpoch).Int64()+
	  (TInt64(unixtime)+TInt64(TLocale().UniversalTimeOffset().Int()))*TInt64(1e6));
  return format_ttime(timeValue);
}

extern "C" PyObject *
dbview_col_length(dbview_object *self, PyObject *args)
{
  ASSERT_ISPREPARED;
  ASSERT_ATROW;
  ASSERT_ROWREADY;
  int column = 0;
  TInt value = 0;
  if (!PyArg_ParseTuple(args, "i", &column))
    return NULL;
  ASSERT_VALIDCOLUMN;
  value = self->view_obj->view.ColLength((TDbColNo)column);
  return Py_BuildValue("i", value);
}

extern "C" PyObject *
dbview_col_type(dbview_object *self, PyObject *args)
{
  ASSERT_ISPREPARED;
  int column = 0;
  if (!PyArg_ParseTuple(args, "i", &column))
    return NULL;
  ASSERT_VALIDCOLUMN;
  return Py_BuildValue("i", self->view_obj->view.ColType((TDbColNo)column));
}

extern "C" PyObject *
dbview_is_col_null(dbview_object *self, PyObject *args)
{
  ASSERT_ISPREPARED;
  ASSERT_ATROW;
  ASSERT_ROWREADY;
  int column = 0;
  if (!PyArg_ParseTuple(args, "i", &column))
    return NULL;
  ASSERT_VALIDCOLUMN;
  PyObject* rval = (self->view_obj->view.IsColNull((TDbColNo)column) 
		    ? Py_True : Py_False);
  Py_INCREF(rval);
  return rval;
}


const static PyMethodDef dbview_methods[] = { 
  {"col_count", (PyCFunction)dbview_colCount, METH_NOARGS},
  {"col_raw", (PyCFunction)dbview_col_raw, METH_VARARGS},
  {"col_length", (PyCFunction)dbview_col_length, METH_VARARGS},
  {"col_type", (PyCFunction)dbview_col_type, METH_VARARGS},
  {"col", (PyCFunction)dbview_col, METH_VARARGS},
  {"col_rawtime", (PyCFunction)dbview_col_rawtime, METH_VARARGS},
  {"count_line", (PyCFunction)dbview_count_line, METH_NOARGS},
  {"first_line", (PyCFunction)dbview_first_line, METH_NOARGS},
  {"get_line", (PyCFunction)dbview_get_line, METH_NOARGS},
  {"is_col_null", (PyCFunction)dbview_is_col_null, METH_VARARGS},
  {"next_line", (PyCFunction)dbview_next_line, METH_NOARGS},
  {"prepare", (PyCFunction)dbview_prepare, METH_VARARGS},
  {NULL, NULL}           // sentinel
};

static PyObject *
dbview_getattr(dbview_object *op, char *name)
{
  return Py_FindMethod((PyMethodDef*)dbview_methods,
                       (PyObject *)op, name);
}

static const PyTypeObject c_dbview_type = {
  PyObject_HEAD_INIT(NULL)
  0,                                         /*ob_size*/
  "e32db.Db_view",                             /*tp_name*/
  sizeof(dbview_object),                     /*tp_basicsize*/
  0,                                         /*tp_itemsize*/
  /* methods */
  (destructor)dbview_dealloc,                /*tp_dealloc*/
  0,                                         /*tp_print*/
  (getattrfunc)dbview_getattr,               /*tp_getattr*/
  0,                                         /*tp_setattr*/
  0,                                         /*tp_compare*/
  0,                                         /*tp_repr*/
  0,                                         /*tp_as_number*/
  0,                                         /*tp_as_sequence*/
  0,                                         /*tp_as_mapping*/
  0,                                         /*tp_hash*/
};

extern "C" {

  static const PyMethodDef e32db_methods[] = {
    {"Dbms", (PyCFunction)new_dbms_object, METH_NOARGS, NULL},
    {"Db_view", (PyCFunction)new_dbview_object, METH_NOARGS, NULL},
    {"format_time", (PyCFunction)format_time, METH_VARARGS, NULL},
    {"format_rawtime", (PyCFunction)format_rawtime, METH_VARARGS, NULL},
    {NULL,              NULL}           /* sentinel */
  };

  DL_EXPORT(void) inite32db(void)
  {
    PyTypeObject* dbms_type = PyObject_New(PyTypeObject, &PyType_Type);
    *dbms_type = c_dbms_type;
    dbms_type->ob_type = &PyType_Type;

    PyTypeObject* dbview_type = PyObject_New(PyTypeObject, &PyType_Type);
    *dbview_type = c_dbview_type;
    dbview_type->ob_type = &PyType_Type;

    SPyAddGlobalString("DbmsType", (PyObject*)dbms_type);
    SPyAddGlobalString("DBViewType", (PyObject*)dbview_type);

    Py_InitModule("e32db", (PyMethodDef*)e32db_methods);

    return;
  }
} /* extern "C" */

#ifndef EKA2
GLDEF_C TInt E32Dll(TDllReason)
{
  return KErrNone;
}
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -