long ReadTableId( Schema schema, string name, out bool isView, out string definition )
 {
   var r = new RowCursor( SysTable );
   var start = new StringStart( name );
   foreach( IndexFileRecord ixr in SysTableByName.From( start.Compare, false ) )
   {
     if ( (string)(ixr.Col[0]._O) == name )
     {
       long id = ixr.Col[1].L;
       r.Get( id );
       if ( r.V[1].L == schema.Id ) 
       {
         isView = r.V[3].L != 0;
         definition = (string)r.V[4]._O;
         return id;
       }
     }
   }
   isView = false; definition = null;
   return -1;
 }
Exemplo n.º 2
0
        // Functions related to Indexing.

        public void InitIndex(long indexId) // Called during CREATE INDEX to index existing rows.
        {
            IndexFile       ixf = IxDict[indexId];
            var             rc  = new RowCursor(this);
            int             n   = ixf.Inf.BaseIx.Length;
            IndexFileRecord ixr = new IndexFileRecord(n + 1);

            for (long id = 1; id <= RowCount; id += 1)
            {
                if (rc.Get(id))
                {
                    // Create the IndexFileRecord to be inserted.
                    for (int i = 0; i < n; i += 1)
                    {
                        ixr.Col[i] = rc.V[ixf.Inf.BaseIx[i]];
                    }
                    ixr.Col[n].L = id; // Append the id of the row.
                    ixf.Insert(ref ixr);
                }
            }
            Dirty = true;
        }
  // Indexes.

  public void CreateIndex( string schemaName, string tableName, string indexName, string [] names, Exec e )
  {
    Table t = (Table)GetTable( schemaName, tableName, e );
    long tid = t.TableId;

    long indexId = GetIndexId( tid, indexName );
    if ( indexId != 0 ) e.Error( "Index already exists" );

    int [] colIx = new int[ names.Length ];
    for ( int i=0; i < names.Length; i +=1 )
    {
      colIx[ i ] = t.ColumnIx( names[i], e );
    }    

    // Create the index.
    {
      var r = new RowCursor( SysIndex );
      r.V[1].L = tid;
      r.V[2].O = indexName;
      indexId = r.Insert();
    }
    // Create the index columns.
    {
      var r = new RowCursor( SysIndexCol );
      r.V[1].L = tid;
      r.V[2].L = indexId;
      for ( int i = 0; i < names.Length; i += 1 )
      {
        r.V[3].L = colIx[ i ];
        r.Insert();
      }
    }

    if ( Normal ) 
    {
      t.OpenIndexes();
      t.InitIndex( indexId );
    }
  }
  public IndexInfo [] ReadIndexes( long tableId )
  {
    var ix = new G.List<IndexInfo>();
    var r = new RowCursor( SysIndexCol );
    long CurIx = 0;
    int IxNum = 0;

    // Use SysIndexColIndex to avoid scanning the entire SysIndexCol table.
    var start = new LongStart( tableId );
    foreach( IndexFileRecord ixr in SysIndexColIndex.From( start.Compare, false ) )
    {
      if ( ixr.Col[0].L == tableId )
      {
        r.Get( ixr.Col[1].L );
        var ii = new IndexInfo();
        ii.IndexId = r.V[2].L;
        if ( ii.IndexId != CurIx ) { IxNum = 0; CurIx = ii.IndexId; }
        ii.IxNum = IxNum++;
        ii.ColIx = (int)r.V[3].L; 
        ix.Add( ii );
      } else break;
    }
    return ix.ToArray();
  }
Exemplo n.º 5
0
  public void CreateTable( string schemaName, string tableName, ColInfo cols, string definition, bool isView, bool alter, Exec e )
  {
    Schema schema = GetSchema( schemaName, true, e );

    bool isView1; string definition1;
    long tableId = tableId = ReadTableId( schema, tableName, out isView1, out definition1 );

    if ( alter ) 
    {
      if ( tableId == 0 || !isView1 ) e.Error( "View does not exist" );
    }
    else if ( Normal && schema.GetCachedTable( tableName ) != null || tableId >= 0 ) 
      e.Error( "Table/View " + schemaName + "." + tableName + " already exists" );

    var r = new RowCursor( SysTable );
    r.V[1].L = schema.Id;
    r.V[2].O = tableName;
    r.V[3].L = isView ? 1 : 0;
    r.V[4].O = isView ? definition.Trim() : "";

    if ( alter ) r.Update( tableId ); else tableId = r.Insert();

    if ( isView ) ResetCache(); else SaveColumns( tableId, cols );
  }