예제 #1
0
    /*
    ** Invoke a virtual table constructor (either xCreate or xConnect). The
    ** pointer to the function to invoke is passed as the fourth parameter
    ** to this procedure.
    */
    static int vtabCallConstructor(
      sqlite3 db,
      Table pTab,
      Module pMod,
      smdxCreateConnect xConstruct,
      ref string pzErr
    )
    {
      VtabCtx sCtx = new VtabCtx();
      VTable pVTable;
      int rc;
      string[] azArg = pTab.azModuleArg;
      int nArg = pTab.nModuleArg;
      string zErr = null;
      string zModuleName = sqlite3MPrintf( db, "%s", pTab.zName );

      //if ( String.IsNullOrEmpty( zModuleName ) )
      //{
      //  return SQLITE_NOMEM;
      //}

      pVTable = new VTable();//sqlite3DbMallocZero( db, sizeof( VTable ) );
      //if ( null == pVTable )
      //{
      //  sqlite3DbFree( db, ref zModuleName );
      //  return SQLITE_NOMEM;
      //}
      pVTable.db = db;
      pVTable.pMod = pMod;

      /* Invoke the virtual table constructor */
      //assert( &db->pVtabCtx );
      Debug.Assert( xConstruct != null );
      sCtx.pTab = pTab;
      sCtx.pVTable = pVTable;
      db.pVtabCtx = sCtx;
      rc = xConstruct( db, pMod.pAux, nArg, azArg, out pVTable.pVtab, out zErr );
      db.pVtabCtx = null;
      //if ( rc == SQLITE_NOMEM )
      //  db.mallocFailed = 1;

      if ( SQLITE_OK != rc )
      {
        if ( zErr == "" )
        {
          pzErr = sqlite3MPrintf( db, "vtable constructor failed: %s", zModuleName );
        }
        else
        {
          pzErr = sqlite3MPrintf( db, "%s", zErr );
          zErr = null;//sqlite3_free( zErr );
        }
        sqlite3DbFree( db, ref pVTable );
      }
      else if ( ALWAYS( pVTable.pVtab ) )
      {
        /* Justification of ALWAYS():  A correct vtab constructor must allocate
        ** the sqlite3_vtab object if successful.  */
        pVTable.pVtab.pModule = pMod.pModule;
        pVTable.nRef = 1;
        if ( sCtx.pTab != null )
        {
          string zFormat = "vtable constructor did not declare schema: %s";
          pzErr = sqlite3MPrintf( db, zFormat, pTab.zName );
          sqlite3VtabUnlock( pVTable );
          rc = SQLITE_ERROR;
        }
        else
        {
          int iCol;
          /* If everything went according to plan, link the new VTable structure
          ** into the linked list headed by pTab->pVTable. Then loop through the
          ** columns of the table to see if any of them contain the token "hidden".
          ** If so, set the Column.isHidden flag and remove the token from
          ** the type string.  */
          pVTable.pNext = pTab.pVTable;
          pTab.pVTable = pVTable;

          for ( iCol = 0; iCol < pTab.nCol; iCol++ )
          {
            if ( String.IsNullOrEmpty( pTab.aCol[iCol].zType ) )
              continue;
            StringBuilder zType = new StringBuilder( pTab.aCol[iCol].zType);
            int nType;
            int i = 0;
            //if ( zType )
            //  continue;
            nType = sqlite3Strlen30( zType );
            if ( sqlite3StrNICmp( "hidden", 0, zType.ToString(), 6 ) != 0 || ( zType.Length > 6 && zType[6] != ' ' ) )
            {
              for ( i = 0; i < nType; i++ )
              {
                if ( ( 0 == sqlite3StrNICmp( " hidden", zType.ToString().Substring( i ), 7 ) )
                 && ( i+7 == zType.Length || (zType[i + 7] == '\0' || zType[i + 7] == ' ' ))
                )
                {
                  i++;
                  break;
                }
              }
            }
            if ( i < nType )
            {
              int j;
              int nDel = 6 + ( zType.Length > i + 6 ? 1 : 0 );
              for ( j = i; ( j + nDel ) < nType; j++ )
              {
                zType[j] = zType[j + nDel];
              }
              if ( zType[i] == '\0' && i > 0 )
              {
                Debug.Assert( zType[i - 1] == ' ' );
                zType.Length = i;//[i - 1] = '\0';
              }
              pTab.aCol[iCol].isHidden = 1;
              pTab.aCol[iCol].zType = zType.ToString().Substring(0,j);
            }
          }
        }
      }

      sqlite3DbFree( db, ref zModuleName );
      return rc;
    }
예제 #2
0
        /*
        ** Invoke a virtual table constructor (either xCreate or xConnect). The
        ** pointer to the function to invoke is passed as the fourth parameter
        ** to this procedure.
        */
        static int vtabCallConstructor(
            sqlite3 db,
            Table pTab,
            Module pMod,
            smdxCreateConnect xConstruct,
            ref string pzErr
            )
        {
            VtabCtx sCtx = new VtabCtx();
            VTable  pVTable;
            int     rc;

            string[] azArg       = pTab.azModuleArg;
            int      nArg        = pTab.nModuleArg;
            string   zErr        = null;
            string   zModuleName = sqlite3MPrintf(db, "%s", pTab.zName);

            //if ( String.IsNullOrEmpty( zModuleName ) )
            //{
            //  return SQLITE_NOMEM;
            //}

            pVTable = new VTable();//sqlite3DbMallocZero( db, sizeof( VTable ) );
            //if ( null == pVTable )
            //{
            //  sqlite3DbFree( db, ref zModuleName );
            //  return SQLITE_NOMEM;
            //}
            pVTable.db   = db;
            pVTable.pMod = pMod;

            /* Invoke the virtual table constructor */
            //assert( &db->pVtabCtx );
            Debug.Assert(xConstruct != null);
            sCtx.pTab    = pTab;
            sCtx.pVTable = pVTable;
            db.pVtabCtx  = sCtx;
            rc           = xConstruct(db, pMod.pAux, nArg, azArg, out pVTable.pVtab, out zErr);
            db.pVtabCtx  = null;
            //if ( rc == SQLITE_NOMEM )
            //  db.mallocFailed = 1;

            if (SQLITE_OK != rc)
            {
                if (zErr == "")
                {
                    pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
                }
                else
                {
                    pzErr = sqlite3MPrintf(db, "%s", zErr);
                    zErr  = null;//sqlite3_free( zErr );
                }
                sqlite3DbFree(db, ref pVTable);
            }
            else if (ALWAYS(pVTable.pVtab))
            {
                /* Justification of ALWAYS():  A correct vtab constructor must allocate
                ** the sqlite3_vtab object if successful.  */
                pVTable.pVtab.pModule = pMod.pModule;
                pVTable.nRef          = 1;
                if (sCtx.pTab != null)
                {
                    string zFormat = "vtable constructor did not declare schema: %s";
                    pzErr = sqlite3MPrintf(db, zFormat, pTab.zName);
                    sqlite3VtabUnlock(pVTable);
                    rc = SQLITE_ERROR;
                }
                else
                {
                    int iCol;

                    /* If everything went according to plan, link the new VTable structure
                    ** into the linked list headed by pTab->pVTable. Then loop through the
                    ** columns of the table to see if any of them contain the token "hidden".
                    ** If so, set the Column.isHidden flag and remove the token from
                    ** the type string.  */
                    pVTable.pNext = pTab.pVTable;
                    pTab.pVTable  = pVTable;

                    for (iCol = 0; iCol < pTab.nCol; iCol++)
                    {
                        if (String.IsNullOrEmpty(pTab.aCol[iCol].zType))
                        {
                            continue;
                        }
                        StringBuilder zType = new StringBuilder(pTab.aCol[iCol].zType);
                        int           nType;
                        int           i = 0;
                        //if ( zType )
                        //  continue;
                        nType = sqlite3Strlen30(zType);
                        if (sqlite3StrNICmp("hidden", 0, zType.ToString(), 6) != 0 || (zType.Length > 6 && zType[6] != ' '))
                        {
                            for (i = 0; i < nType; i++)
                            {
                                if ((0 == sqlite3StrNICmp(" hidden", zType.ToString().Substring(i), 7)) &&
                                    (i + 7 == zType.Length || (zType[i + 7] == '\0' || zType[i + 7] == ' '))
                                    )
                                {
                                    i++;
                                    break;
                                }
                            }
                        }
                        if (i < nType)
                        {
                            int j;
                            int nDel = 6 + (zType.Length > i + 6 ? 1 : 0);
                            for (j = i; (j + nDel) < nType; j++)
                            {
                                zType[j] = zType[j + nDel];
                            }
                            if (zType[i] == '\0' && i > 0)
                            {
                                Debug.Assert(zType[i - 1] == ' ');
                                zType.Length = i;//[i - 1] = '\0';
                            }
                            pTab.aCol[iCol].isHidden = 1;
                            pTab.aCol[iCol].zType    = zType.ToString().Substring(0, j);
                        }
                    }
                }
            }

            sqlite3DbFree(db, ref zModuleName);
            return(rc);
        }