public TableExpression GetTableOrView( string schemaName, string name, Exec e ) { Schema schema = GetSchema( schemaName, true, e ); TableExpression result = schema.GetCachedTable( name ); if ( result != null ) return result; bool isView; string definition; long tid = ReadTableId( schema, name, out isView, out definition ); if ( tid < 0 ) e.Error( schemaName + "." + name + " not found" ); if ( isView ) { TableExpression te = e.LoadView( definition, schemaName + "." + name ); te.TableId = tid; schema.TableDict[ name ] = te; return te; } else { // Fetch the Column Information from the SysColumn table. var names = new G.List<string>(); var types = new G.List<DataType>(); names.Add( "Id" ); types.Add( DataType.Bigint ); // Add the pre-defined "id" column. // Use SysColumnIndex to avoid scanning the entire SysColumn table. var start = new LongStart( tid ); var r = new RowCursor( SysColumn ); foreach( IndexFileRecord ixr in SysColumnIndex.From( start.Compare, false ) ) { if ( ixr.Col[0].L == tid ) { r.Get( ixr.Col[1].L ); names.Add( (string) r.V[2]._O ); types.Add( (DataType)r.V[3].L ); } else break; } Table t = new Table( this, schema, name, ColInfo.New(names,types), tid ); t.OpenIndexes(); return t; } }
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; }
// 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; }
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(); }