/// <summary> /// Builds a channel based on an existing one. /// </summary> /// <param name="channel"></param> /// <param name="id"></param> public Channel(Channel channel, int id) : this() { _id = id; _database = channel._database; _user = channel._user; _autoCommit = true; _readOnly = channel._readOnly; }
private Result ProcessConnect(Tokenizer c, Channel channel) { c.GetThis("USER"); string username = c.GetStringToken(); c.GetThis("PASSWORD"); string password = c.GetStringToken(); User user = _access.GetUser(username, password); channel.Commit(); channel.SetUser(user); return new Result(); }
/// <summary> /// Writes a SQL statement to the transaction log. /// </summary> /// <param name="channel"></param> /// <param name="s"></param> public void Write(Channel channel, string s) { if (bRestoring || s == null || s.Equals("")) return; if (!bReadOnly) { int id = 0; if (channel != null) { id = channel.Id; } if (id != mLastId) { s = "/*C" + id + "*/" + s; mLastId = id; } try { lock( SyncLock ) { writeLine(wScript, s); #if !POCKETPC if (bWriteDelay) bNeedFlush = true; else #endif wScript.Flush(); } } catch (IOException e) { TracingHelper.Error(TracingHelper.FILE_IO_ERROR, sFileScript); LogHelper.Publish( "Unexpected error on Write.", e ); } catch( Exception e ) { LogHelper.Publish( "Unexpected error on Write.", e ); } lock( SyncLock ) { if (iLogSize > 0 && iLogCount++ > 100) { iLogCount = 0; if ((new FileInfo(sFileScript)).Length > iLogSize * 1024 * 1024) { Checkpoint(); } } } } }
/// <summary> /// Default constructor. /// </summary> /// <param name="db"></param> /// <param name="system"></param> /// <param name="name"></param> public Log(Database db, Channel system, string name) { dDatabase = db; cSystem = system; sName = name; sFileProperties = sName + ".cfg"; sFileScript = sName + ".log"; sFileCache = sName + ".data"; sFileBackup = sName + ".backup"; }
// [email protected] end changes from 1.50 // [email protected] begin changes from 1.50 public Result GetResult(int start, int cnt, Channel cChannel) { int maxrows=start+cnt; //<-new, cut definitly // [email protected] begin changes from 1.50 Resolve(); CheckResolved(); if (sUnion != null && sUnion.iResultLen != iResultLen) { throw TracingHelper.Error(TracingHelper.COLUMN_COUNT_DOES_NOT_MATCH); } int len = eColumn.Length; Result r = new Result(len); bool aggregated = false; bool grouped = false; for (int i = 0; i < len; i++) { Expression e = eColumn[i]; r.Type[i] = e.ColumnType; if (e.IsAggregate) { aggregated = true; } } object[] agg = null; if (aggregated) { agg = new object[len]; } if (iGroupLen > 0) { // has been set in Parser grouped = true; } bool simple_maxrows = false; if (maxrows != 0 && grouped == false && sUnion == null && iOrderLen == 0) { simple_maxrows = true; } int count = 0; int filter = tFilter.Length; bool[] first = new bool[filter]; int level = 0; while (level >= 0) { bool found = false; if( filter > 0 ) { TableFilter t = tFilter[level]; if (!first[level]) { found = t.FindFirst(); first[level] = found; } else { found = t.Next(); first[level] = found; } } if (!found) { level--; if( !OnlyVars ) continue; } if (level < filter - 1) { level++; continue; } if (eCondition == null || eCondition.Test()) { object[] row = new object[len]; for (int i = 0; i < len; i++) { row[i] = eColumn[i].GetValue(); if( cChannel != null && eColumn[i].IsVarAssign ) { cChannel.SetDeclareValue( eColumn[i].Arg.ColumnName, row[i] ); } } count++; if (aggregated && !grouped) { UpdateAggregateRow(agg, row, len); } else { r.Add(row); if (simple_maxrows && count >= maxrows) { break; } } } } if ( aggregated && !grouped ) { AddAggregateRow(r, agg, len, count); } else if ( grouped ) { int[] order = new int[iGroupLen]; int[] way = new int[iGroupLen]; for (int i = iResultLen, j = 0; j < iGroupLen; i++, j++) { order[j] = i; way[j] = 1; } r = SortResult(r, order, way); Record n = r.Root; Result x = new Result(len); for (int i = 0; i < len; i++) { x.Type[i] = r.Type[i]; } do { object[] row = new object[len]; count = 0; bool newgroup = false; while (n != null && newgroup == false) { count++; for (int i = 0; i < iGroupLen; i++) { if (n.Next == null) { newgroup = true; } else if (Column.Compare(n.Data[i], n.Next.Data[i], r.Type[i]) != 0) { // can't use .Equals because 'null' is also one group newgroup = true; } } UpdateAggregateRow(row, n.Data, len); n = n.Next; } AddAggregateRow(x, row, len, count); } while (n != null); r = x; } if (iOrderLen != 0) { int[] order = new int[iOrderLen]; int[] way = new int[iOrderLen]; for (int i = iResultLen, j = 0; j < iOrderLen; i++, j++) { order[j] = i; way[j] = eColumn[i].IsDescending ? -1 : 1; } r = SortResult(r, order, way); } // the result maybe is bigger (due to group and order by) // but don't tell this anybody else r.SetColumnCount( iResultLen ); if (bDistinct) { r = RemoveDuplicates(r); } for (int i = 0; i < iResultLen; i++) { Expression e = eColumn[i]; r.Label[i] = e.Alias; r.Table[i] = e.TableName; r.Name[i] = e.ColumnName; } if (sUnion != null) { Result x = sUnion.GetResult(0, cChannel); if (UnionType == SelectType.Union) { r.Append(x); r = RemoveDuplicates(r); } else if (UnionType == SelectType.UnionAll) { r.Append(x); } else if (UnionType == SelectType.Intersect) { r = RemoveDuplicates(r); x = RemoveDuplicates(x); r = RemoveDifferent(r, x); } else if (UnionType == SelectType.Except) { r = RemoveDuplicates(r); x = RemoveDuplicates(x); r = RemoveSecond(r, x); } } if (maxrows > 0 &&!simple_maxrows) { TrimResult(r, maxrows); } // [email protected] begin changes from 1.50 if (start > 0) { //then cut the first 'start' elements TrimResultFront( r, start ); } // [email protected] end changes from 1.50 return r; }
public Result GetScript(bool bDrop, bool bInsert, bool bCached, Channel channel) { channel.CheckAdmin(); Result r = new Result(1); r.Type[0] = ColumnType.VarChar; r.Table[0] = "SYSTEM_SCRIPT"; r.Label[0] = "COMMAND"; r.Name[0] = "COMMAND"; StringBuilder a = new StringBuilder(); for (int i = 0; i < tTable.Count; i++) { Table t = (Table) tTable[i]; if (bDrop) { AddRow(r, "DROP TABLE \"" + t.Name + "\""); } a.Remove(0,a.Length); a.Append("CREATE "); if (t.IsCached) { a.Append("CACHED "); } a.Append("TABLE "); a.Append('"'); a.Append(t.Name); a.Append('"'); a.Append("("); int columns = t.ColumnCount; Index pki = t.GetIndex("SYSTEM_PK"); int pk = (pki == null) ? -1 : pki.Columns[0]; for (int j = 0; j < columns; j++) { a.Append('"'); a.Append(t.GetColumnName(j)); a.Append('"'); a.Append(" "); a.Append(Column.GetColumnTypeString(t.GetType(j))); if (!t.GetColumnIsNullable(j)) { a.Append(" NOT NULL"); } if (j == t.IdentityColumn) { a.Append(" IDENTITY"); } if (j == pk) { a.Append(" PRIMARY KEY"); } if (j < columns - 1) { a.Append(","); } } ArrayList v = t.Constraints; for (int j = 0; j < v.Count; j++) { Constraint c = (Constraint) v[j]; if (c.ConstraintType == ConstraintType.ForeignKey) { a.Append(",FOREIGN KEY"); int[] col = c.RefTableColumns; a.Append(GetColumnList(c.RefTable, col, col.Length)); a.Append("REFERENCES "); a.Append(c.MainTable.Name); col = c.MainTableColumns; a.Append(GetColumnList(c.MainTable, col, col.Length)); } else if (c.ConstraintType == ConstraintType.Unique) { a.Append(",UNIQUE"); int[] col = c.MainTableColumns; a.Append(GetColumnList(c.MainTable, col, col.Length)); } } a.Append(")"); AddRow(r, a.ToString()); Index index = null; while (true) { index = t.GetNextIndex(index); if (index == null) { break; } string indexname = index.Name; if (indexname.Equals("SYSTEM_PK")) { continue; } else if (indexname.StartsWith("SYSTEM_FOREIGN_KEY")) { // foreign keys where created in the 'create table' continue; } else if (indexname.StartsWith("SYSTEM_CONSTRAINT")) { // constraints where created in the 'create table' continue; } a.Remove(0,a.Length); a.Append("CREATE "); if (index.IsUnique) { a.Append("UNIQUE "); } a.Append("INDEX "); a.Append(indexname); a.Append(" ON "); a.Append(t.Name); int[] col = index.Columns; int len = col.Length; if (!index.IsUnique) { len--; } a.Append(GetColumnList(t, col, len)); AddRow(r, a.ToString()); } if (bInsert) { Index primary = t.PrimaryIndex; Node x = primary.First(); bool integrity = true; if (x != null) { integrity = false; AddRow(r, "SET REFERENTIAL_INTEGRITY FALSE"); } while (x != null) { AddRow(r, t.GetInsertStatement(x.GetData())); x = primary.Next(x); } if (!integrity) { AddRow(r, "SET REFERENTIAL_INTEGRITY TRUE"); } } if (bCached && t.IsCached) { a.Remove(0,a.Length); a.Append("SET TABLE "); a.Append('"'); a.Append(t.Name); a.Append('"'); a.Append(" INDEX '"); a.Append(t.IndexRoots); a.Append("'"); AddRow(r, a.ToString()); } } ArrayList uList = aAccess.GetUsers(); for (int i = 0; i < uList.Count; i++) { User u = (User) uList[i]; // todo: this is not a nice implementation if (u == null) { continue; } string name = u.Name; if (!name.Equals("PUBLIC")) { a.Remove(0,a.Length); a.Append("CREATE USER "); a.Append(name); a.Append(" PASSWORD "); a.Append("\"" + u.Password + "\""); if (u.IsAdmin) { a.Append(" ADMIN"); } AddRow(r, a.ToString()); } Hashtable rights = u.Rights; if (rights == null) { continue; } foreach( string dbObject in rights.Keys) { AccessType right = (AccessType) rights[dbObject]; if (right == AccessType.None) { continue; } a.Remove(0,a.Length); a.Append("GRANT "); a.Append(Access.GetRight(right)); a.Append(" ON "); a.Append(dbObject); a.Append(" TO "); a.Append(u.Name); AddRow(r, a.ToString()); } } if (dDatabase.IsIgnoreCase) { AddRow(r, "SET IGNORECASE TRUE"); } Hashtable h = dDatabase.Alias; foreach(string alias in h.Keys) { string className = (string) h[alias]; AddRow(r, "CREATE ALIAS " + alias + " FOR \"" + className + "\""); } return r; }
public void close(Channel myChannel) { this.db.Execute("shutdown", myChannel); myChannel.Disconnect(); }
public Parser(Database db, Tokenizer tokenizer, Channel channel) { dDatabase = db; tTokenizer = tokenizer; cChannel = channel; }
public void Delete(object[] row, Channel c) { if (dDatabase.IsReferentialIntegrity) { for (int i = 0; i < iConstraintCount; i++) { ((Constraint) vConstraint[i]).CheckDelete(row); } } DeleteNoCheck(row, c); }
private Result ProcessDisconnect(Tokenizer tokenizer, Channel channel) { if (!channel.IsClosed) { channel.Disconnect(); _channel.Remove(channel.Id); } return new Result(); }
private Result ProcessDrop(Tokenizer tokenizer, Channel channel) { channel.CheckReadWrite(); channel.CheckAdmin(); string sToken = tokenizer.GetString(); if (sToken.Equals("TABLE")) { sToken = tokenizer.GetString(); if (sToken.Equals("IF")) { sToken = tokenizer.GetString(); // EXISTS sToken = tokenizer.GetString(); // <table> DropTable(sToken, true); } else { DropTable(sToken, false); } channel.Commit(); } else if (sToken.Equals("USER")) { _access.DropUser(tokenizer.GetStringToken()); } else if (sToken.Equals("INDEX")) { sToken = tokenizer.GetString(); if (!tokenizer.WasLongName) { throw TracingHelper.Error(TracingHelper.UnexpectedToken, sToken); } string table = tokenizer.LongNameFirst; string index = tokenizer.LongNameLast; Table t = GetTable(table, channel); t.CheckDropIndex(index); Table tn = t.MoveDefinition(index); tn.MoveData(t); DropTable(table); LinkTable(tn); channel.Commit(); } else { throw TracingHelper.Error(TracingHelper.UnexpectedToken, sToken); } return new Result(); }
/// <summary> /// Database class constructor. /// </summary> /// <param name="name">The database name to open or create.</param> public Database(string name) { if (TracingHelper.TraceEnabled) { TracingHelper.Write(); } _name = name; _table = new ArrayList(); _access = new Access(); _channel = new Hashtable(); _alias = new Hashtable(); _referentialIntegrity = true; Library.Register(_alias); _databaseInfo = new DatabaseInformation(this, _table, _access); bool newdatabase = false; Channel sys = new Channel(this, new User(null, null, true, null), true, false, 0); RegisterChannel(sys); if (name.Equals(".")) { newdatabase = true; } else { _log = new Log(this, sys, name); newdatabase = _log.Open(); } if (newdatabase) { Execute("CREATE USER SA PASSWORD \"\" ADMIN", sys); } //_access.grant("PUBLIC", "CLASS \"SharpHSQL.Library\"", Access.ALL); }
private void ProcessCreateTable(Tokenizer tokenizer, Channel channel, bool cached) { Table t; string sToken = tokenizer.GetName(); if (cached && _log != null) { t = new Table(this, true, sToken, true); } else { t = new Table(this, true, sToken, false); } tokenizer.GetThis("("); int primarykeycolumn = -1; int column = 0; bool constraint = false; while (true) { bool identity = false; sToken = tokenizer.GetString(); if (sToken.Equals("CONSTRAINT") || sToken.Equals("PRIMARY") || sToken.Equals("FOREIGN") || sToken.Equals("UNIQUE")) { tokenizer.Back(); constraint = true; break; } string sColumn = sToken; ColumnType iType = Column.GetColumnType(tokenizer.GetString()); if (iType == ColumnType.VarChar && _ignoreCase) { iType = ColumnType.VarCharIgnoreCase; } sToken = tokenizer.GetString(); if (iType == ColumnType.DbDouble && sToken.Equals("PRECISION")) { sToken = tokenizer.GetString(); } if (sToken.Equals("(")) { // overread length do { sToken = tokenizer.GetString(); } while (!sToken.Equals(")")); sToken = tokenizer.GetString(); } bool nullable = true; if (sToken.Equals("NULL")) { sToken = tokenizer.GetString(); } else if (sToken.Equals("NOT")) { tokenizer.GetThis("NULL"); nullable = false; sToken = tokenizer.GetString(); } if (sToken.Equals("IDENTITY")) { identity = true; TracingHelper.Check(primarykeycolumn == -1, TracingHelper.SECOND_PRIMARY_KEY, sColumn); sToken = tokenizer.GetString(); primarykeycolumn = column; } if (sToken.Equals("PRIMARY")) { tokenizer.GetThis("KEY"); TracingHelper.Check(identity || primarykeycolumn == -1, TracingHelper.SECOND_PRIMARY_KEY, sColumn); primarykeycolumn = column; sToken = tokenizer.GetString(); } t.AddColumn(sColumn, iType, nullable, identity); if (sToken.Equals(")")) { break; } if (!sToken.Equals(",")) { throw TracingHelper.Error(TracingHelper.UnexpectedToken, sToken); } column++; } if (primarykeycolumn != -1) { t.CreatePrimaryKey(primarykeycolumn); } else { t.CreatePrimaryKey(); } if (constraint) { int i = 0; while (true) { sToken = tokenizer.GetString(); string name = "SYSTEM_CONSTRAINT" + i; i++; if (sToken.Equals("CONSTRAINT")) { name = tokenizer.GetString(); sToken = tokenizer.GetString(); } if (sToken.Equals("PRIMARY")) { tokenizer.GetThis("KEY"); AddUniqueConstraintOn(tokenizer, channel, name, t); } else if (sToken.Equals("UNIQUE")) { AddUniqueConstraintOn(tokenizer, channel, name, t); } else if (sToken.Equals("FOREIGN")) { tokenizer.GetThis("KEY"); AddForeignKeyOn(tokenizer, channel, name, t); } sToken = tokenizer.GetString(); if (sToken.Equals(")")) { break; } if (!sToken.Equals(",")) { throw TracingHelper.Error(TracingHelper.UnexpectedToken, sToken); } } } channel.Commit(); LinkTable(t); }
private Result ProcessCreate(Tokenizer tokenizer, Channel channel) { channel.CheckReadWrite(); channel.CheckAdmin(); string sToken = tokenizer.GetString(); switch(sToken) { case "TABLE": ProcessCreateTable(tokenizer, channel, true); break; case "MEMORY": tokenizer.GetThis("TABLE"); ProcessCreateTable(tokenizer, channel, false); break; case "CACHED": tokenizer.GetThis("TABLE"); ProcessCreateTable(tokenizer, channel, true); break; case "USER": { string u = tokenizer.GetStringToken(); tokenizer.GetThis("PASSWORD"); string p = tokenizer.GetStringToken(); bool admin; if (tokenizer.GetString().Equals("ADMIN")) { admin = true; } else { admin = false; } _access.CreateUser(u, p, admin); } break; case "ALIAS": { string name = tokenizer.GetString(); sToken = tokenizer.GetString(); TracingHelper.Check(sToken.Equals("FOR"), TracingHelper.UnexpectedToken, sToken); sToken = tokenizer.GetString(); _alias[name] = sToken; } break; default: { bool unique = false; if (sToken.Equals("UNIQUE")) { unique = true; sToken = tokenizer.GetString(); } if (!sToken.Equals("INDEX")) { throw TracingHelper.Error(TracingHelper.UnexpectedToken, sToken); } string name = tokenizer.GetName(); tokenizer.GetThis("ON"); Table t = GetTable(tokenizer.GetString(), channel); AddIndexOn(tokenizer, channel, name, t, unique); } break; } return new Result(); }
public void rollback(Channel mc) { mc.Rollback(); }
public void DeleteNoCheck(object[] row, Channel channel) { DeleteNoCheck(row, channel, true); }
public void auto_commit(Channel mc, bool b) { mc.SetAutoCommit(b); }
public void DeleteNoCheck(object[] row, Channel channel, bool log) { for (int i = 1; i < iIndexCount; i++) { GetIndex(i).Delete(row, false); } // must delete data last GetIndex(0).Delete(row, true); if (channel != null) { channel.AddTransactionDelete(this, row); } if (lLog != null) { lLog.Write(channel, GetDeleteStatement(row)); } }
public void commit(Channel myChannel) { myChannel.Commit(); }
public void Insert(Result result, Channel c) { // if violation of constraints can occur, insert must be rolled back // outside of this function! Record r = result.Root; int len = result.ColumnCount; while (r != null) { object[] row = NewRow; for (int i = 0; i < len; i++) { row[i] = r.Data[i]; } Insert(row, c); r = r.Next; } }
public Table GetSystemTable(string name, Channel channel) { if (name.Equals("SYSTEM_PROCEDURES")) { Table t = CreateTable(name); t.AddColumn("PROCEDURE_" + META_CAT, ColumnType.VarChar); t.AddColumn("PROCEDURE_" + META_SCHEM, ColumnType.VarChar); t.AddColumn("PROCEDURE_NAME", ColumnType.VarChar); t.AddColumn("NUM_INPUT_PARAMS", ColumnType.Integer); t.AddColumn("NUM_OUTPUT_PARAMS", ColumnType.Integer); t.AddColumn("NUM_RESULT_SETS", ColumnType.Integer); t.AddColumn("REMARKS", ColumnType.VarChar); t.AddColumn("PROCEDURE_TYPE", ColumnType.SmallInt); t.CreatePrimaryKey(); return t; } else if (name.Equals("SYSTEM_PROCEDURECOLUMNS")) { Table t = CreateTable(name); t.AddColumn("PROCEDURE_" + META_CAT, ColumnType.VarChar); t.AddColumn("PROCEDURE_" + META_SCHEM, ColumnType.VarChar); t.AddColumn("PROCEDURE_NAME", ColumnType.VarChar); t.AddColumn("COLUMN_NAME", ColumnType.VarChar); t.AddColumn("COLUMN_TYPE", ColumnType.SmallInt); t.AddColumn("DATA_TYPE", ColumnType.SmallInt); t.AddColumn("TYPE_NAME", ColumnType.VarChar); t.AddColumn("PRECISION", ColumnType.Integer); t.AddColumn("LENGTH", ColumnType.Integer); t.AddColumn("SCALE", ColumnType.SmallInt); t.AddColumn("RADIX", ColumnType.SmallInt); t.AddColumn("NULLABLE", ColumnType.SmallInt); t.AddColumn("REMARKS", ColumnType.VarChar); t.CreatePrimaryKey(); return t; } else if (name.Equals("SYSTEM_TABLES")) { Table t = CreateTable(name); t.AddColumn("TABLE_" + META_CAT, ColumnType.VarChar); t.AddColumn("TABLE_" + META_SCHEM, ColumnType.VarChar); t.AddColumn("TABLE_NAME", ColumnType.VarChar); t.AddColumn("TABLE_TYPE", ColumnType.VarChar); t.AddColumn("REMARKS", ColumnType.VarChar); t.CreatePrimaryKey(); for (int i = 0; i < tTable.Count; i++) { Table table = (Table) tTable[i]; object[] o = t.NewRow; o[2] = table.Name; o[3] = "TABLE"; t.Insert(o, null); } return t; } else if (name.Equals("SYSTEM_SCHEMAS")) { Table t = CreateTable(name); t.AddColumn("TABLE_" + META_SCHEM, ColumnType.VarChar); t.CreatePrimaryKey(); return t; } else if (name.Equals("SYSTEM_CATALOGS")) { Table t = CreateTable(name); t.AddColumn("TABLE_" + META_CAT, ColumnType.VarChar); t.CreatePrimaryKey(); return t; } else if (name.Equals("SYSTEM_DATABASES")) { Table t = CreateTable(name); t.AddColumn("DATABASE", ColumnType.VarChar); t.CreatePrimaryKey(); return t; } else if (name.Equals("SYSTEM_TABLETYPES")) { Table t = CreateTable(name); t.AddColumn("TABLE_TYPE", ColumnType.VarChar); t.CreatePrimaryKey(); object[] o = t.NewRow; o[0] = "TABLE"; t.Insert(o, null); return t; } else if (name.Equals("SYSTEM_COLUMNS")) { Table t = CreateTable(name); t.AddColumn("TABLE_" + META_CAT, ColumnType.VarChar); t.AddColumn("TABLE_" + META_SCHEM, ColumnType.VarChar); t.AddColumn("TABLE_NAME", ColumnType.VarChar); t.AddColumn("COLUMN_NAME", ColumnType.VarChar); t.AddColumn("DATA_TYPE", ColumnType.SmallInt); t.AddColumn("TYPE_NAME", ColumnType.VarChar); t.AddColumn(META_COLUMN_SIZE, ColumnType.Integer); t.AddColumn(META_BUFFER_LENGTH, ColumnType.Integer); t.AddColumn(META_DECIMAL_DIGITS, ColumnType.Integer); t.AddColumn(META_NUM_PREC_RADIX, ColumnType.Integer); t.AddColumn("NULLABLE", ColumnType.Integer); t.AddColumn("REMARKS", ColumnType.VarChar); // Access and Intersolv do not return this fields t.AddColumn("COLUMN_DEF", ColumnType.VarChar); t.AddColumn("SQL_DATA_TYPE", ColumnType.VarChar); t.AddColumn("SQL_DATETIME_SUB", ColumnType.Integer); t.AddColumn("CHAR_OCTET_LENGTH", ColumnType.Integer); t.AddColumn("ORDINAL_POSITION", ColumnType.VarChar); t.AddColumn("IS_NULLABLE", ColumnType.VarChar); t.CreatePrimaryKey(); for (int i = 0; i < tTable.Count; i++) { Table table = (Table) tTable[i]; int columns = table.ColumnCount; for (int j = 0; j < columns; j++) { object[] o = t.NewRow; o[2] = table.Name; o[3] = table.GetColumnName(j); o[4] = table.GetColumnType(j); o[5] = Column.GetColumnTypeString(table.GetColumnType(j)); int nullable; if (table.GetColumnIsNullable(j)) { nullable = Convert.ToInt32(true); } else { nullable = Convert.ToInt32(false); } o[10] = nullable; if (table.IdentityColumn == j) { o[11] = "IDENTITY"; } t.Insert(o, null); } } return t; } else if (name.Equals("SYSTEM_COLUMNPRIVILEGES")) { Table t = CreateTable(name); t.AddColumn("TABLE_" + META_CAT, ColumnType.VarChar); t.AddColumn("TABLE_" + META_SCHEM, ColumnType.VarChar); t.AddColumn("TABLE_NAME", ColumnType.VarChar); t.AddColumn("COLUMN_NAME", ColumnType.VarChar); t.AddColumn("GRANTOR", ColumnType.VarChar); t.AddColumn("GRANTEE", ColumnType.VarChar); t.AddColumn("PRIVILEGE", ColumnType.VarChar); t.AddColumn("IS_GRANTABLE", ColumnType.VarChar); t.CreatePrimaryKey(); /* * // todo: get correct info * for(int i=0;i<tTable.size();i++) { * Table table=(Table)tTable.elementAt(i); * int columns=table.ColumnCount; * for(int j=0;j<columns;j++) { * object o[]=t.NewRow; * o[2]=table.Name; * o[3]=table.getColumnName(j); * o[4]="sa"; * o[6]="FULL"; * o[7]="NO"; * t.insert(o,null); * } * } */ return t; } else if (name.Equals("SYSTEM_TABLEPRIVILEGES")) { Table t = CreateTable(name); t.AddColumn("TABLE_" + META_CAT, ColumnType.VarChar); t.AddColumn("TABLE_" + META_SCHEM, ColumnType.VarChar); t.AddColumn("TABLE_NAME", ColumnType.VarChar); t.AddColumn("GRANTOR", ColumnType.VarChar); t.AddColumn("GRANTEE", ColumnType.VarChar); t.AddColumn("PRIVILEGE", ColumnType.VarChar); t.AddColumn("IS_GRANTABLE", ColumnType.VarChar); t.CreatePrimaryKey(); for (int i = 0; i < tTable.Count; i++) { Table table = (Table) tTable[i]; object[] o = t.NewRow; o[2] = table.Name; o[3] = "sa"; o[5] = "FULL"; t.Insert(o, null); } return t; } else if (name.Equals("SYSTEM_BESTROWIDENTIFIER")) { Table t = CreateTable(name); t.AddColumn("SCOPE", ColumnType.SmallInt); t.AddColumn("COLUMN_NAME", ColumnType.VarChar); t.AddColumn("DATA_TYPE", ColumnType.SmallInt); t.AddColumn("TYPE_NAME", ColumnType.VarChar); t.AddColumn(META_COLUMN_SIZE, ColumnType.Integer); t.AddColumn(META_BUFFER_LENGTH, ColumnType.Integer); t.AddColumn(META_DECIMAL_DIGITS, ColumnType.SmallInt); t.AddColumn("PSEUDO_COLUMN", ColumnType.SmallInt); t.CreatePrimaryKey(); return t; } else if (name.Equals("SYSTEM_VERSIONCOLUMNS")) { Table t = CreateTable(name); t.AddColumn("SCOPE", ColumnType.Integer); t.AddColumn("COLUMN_NAME", ColumnType.VarChar); t.AddColumn("DATA_TYPE", ColumnType.SmallInt); t.AddColumn("TYPE_NAME", ColumnType.VarChar); t.AddColumn(META_COLUMN_SIZE, ColumnType.SmallInt); t.AddColumn(META_BUFFER_LENGTH, ColumnType.Integer); t.AddColumn(META_DECIMAL_DIGITS, ColumnType.SmallInt); t.AddColumn("PSEUDO_COLUMN", ColumnType.SmallInt); t.CreatePrimaryKey(); return t; } else if (name.Equals("SYSTEM_PRIMARYKEYS")) { Table t = CreateTable(name); t.AddColumn("TABLE_" + META_CAT, ColumnType.VarChar); t.AddColumn("TABLE_" + META_SCHEM, ColumnType.VarChar); t.AddColumn("TABLE_NAME", ColumnType.VarChar); t.AddColumn("COLUMN_NAME", ColumnType.VarChar); t.AddColumn("KEY_SEQ", ColumnType.SmallInt); t.AddColumn("PK_NAME", ColumnType.VarChar); t.CreatePrimaryKey(); for (int i = 0; i < tTable.Count; i++) { Table table = (Table) tTable[i]; Index index = table.GetIndex("SYSTEM_PK"); int[] cols = index.Columns; int len = cols.Length; for (int j = 0; j < len; j++) { object[] o = t.NewRow; o[2] = table.Name; o[3] = table.GetColumnName(cols[j]); o[4] = j + 1; o[5] = "SYSTEM_PK"; t.Insert(o, null); } } return t; } else if (name.Equals("SYSTEM_IMPORTEDKEYS")) { Table t = CreateTable(name); t.AddColumn("PKTABLE_" + META_CAT, ColumnType.VarChar); t.AddColumn("PKTABLE_" + META_SCHEM, ColumnType.VarChar); t.AddColumn("PKTABLE_NAME", ColumnType.VarChar); t.AddColumn("PKCOLUMN_NAME", ColumnType.VarChar); t.AddColumn("FKTABLE_" + META_CAT, ColumnType.VarChar); t.AddColumn("FKTABLE_" + META_SCHEM, ColumnType.VarChar); t.AddColumn("FKTABLE_NAME", ColumnType.VarChar); t.AddColumn("FKCOLUMN_NAME", ColumnType.VarChar); t.AddColumn("KEY_SEQ", ColumnType.SmallInt); t.AddColumn("UPDATE_RULE", ColumnType.SmallInt); t.AddColumn("DELETE_RULE", ColumnType.SmallInt); t.AddColumn("FK_NAME", ColumnType.VarChar); t.AddColumn("PK_NAME", ColumnType.VarChar); t.AddColumn("DEFERRABILITY", ColumnType.SmallInt); t.CreatePrimaryKey(); return t; } else if (name.Equals("SYSTEM_EXPORTEDKEYS")) { Table t = CreateTable(name); t.AddColumn("PKTABLE_" + META_CAT, ColumnType.VarChar); t.AddColumn("PKTABLE_" + META_SCHEM, ColumnType.VarChar); t.AddColumn("PKTABLE_NAME", ColumnType.VarChar); t.AddColumn("PKCOLUMN_NAME", ColumnType.VarChar); t.AddColumn("FKTABLE_" + META_CAT, ColumnType.VarChar); t.AddColumn("FKTABLE_" + META_SCHEM, ColumnType.VarChar); t.AddColumn("FKTABLE_NAME", ColumnType.VarChar); t.AddColumn("FKCOLUMN_NAME", ColumnType.VarChar); t.AddColumn("KEY_SEQ", ColumnType.SmallInt); t.AddColumn("UPDATE_RULE", ColumnType.SmallInt); t.AddColumn("DELETE_RULE", ColumnType.SmallInt); t.AddColumn("FK_NAME", ColumnType.VarChar); t.AddColumn("PK_NAME", ColumnType.VarChar); t.AddColumn("DEFERRABILITY", ColumnType.SmallInt); t.CreatePrimaryKey(); return t; } else if (name.Equals("SYSTEM_CROSSREFERENCE")) { Table t = CreateTable(name); t.AddColumn("PKTABLE_" + META_CAT, ColumnType.VarChar); t.AddColumn("PKTABLE_" + META_SCHEM, ColumnType.VarChar); t.AddColumn("PKTABLE_NAME", ColumnType.VarChar); t.AddColumn("PKCOLUMN_NAME", ColumnType.VarChar); t.AddColumn("FKTABLE_" + META_CAT, ColumnType.VarChar); t.AddColumn("FKTABLE_" + META_SCHEM, ColumnType.VarChar); t.AddColumn("FKTABLE_NAME", ColumnType.VarChar); t.AddColumn("FKCOLUMN_NAME", ColumnType.VarChar); t.AddColumn("KEY_SEQ", ColumnType.Integer); t.AddColumn("UPDATE_RULE", ColumnType.SmallInt); t.AddColumn("DELETE_RULE", ColumnType.SmallInt); t.AddColumn("FK_NAME", ColumnType.VarChar); t.AddColumn("PK_NAME", ColumnType.VarChar); t.AddColumn("DEFERRABILITY", ColumnType.SmallInt); t.CreatePrimaryKey(); return t; } else if (name.Equals("SYSTEM_TYPEINFO")) { Table t = CreateTable(name); t.AddColumn("TYPE_NAME", ColumnType.VarChar); t.AddColumn("DATA_TYPE", ColumnType.SmallInt); t.AddColumn("PRECISION", ColumnType.Integer); t.AddColumn("LITERAL_PREFIX", ColumnType.VarChar); t.AddColumn("LITERAL_SUFFIX", ColumnType.VarChar); t.AddColumn("CREATE_PARAMS", ColumnType.VarChar); t.AddColumn("NULLABLE", ColumnType.SmallInt); t.AddColumn("CASE_SENSITIVE", ColumnType.VarChar); t.AddColumn("SEARCHABLE", ColumnType.SmallInt); t.AddColumn("UNSIGNED_ATTRIBUTE", ColumnType.Bit); t.AddColumn(META_FIXED_PREC_SCALE, ColumnType.Bit); t.AddColumn("AUTO_INCREMENT", ColumnType.Bit); t.AddColumn("LOCAL_TYPE_NAME", ColumnType.VarChar); t.AddColumn("MINIMUM_SCALE", ColumnType.SmallInt); t.AddColumn("MAXIMUM_SCALE", ColumnType.SmallInt); // this columns are not supported by Access and Intersolv t.AddColumn("SQL_DATE_TYPE", ColumnType.Integer); t.AddColumn("SQL_DATETIME_SUB", ColumnType.Integer); t.AddColumn("NUM_PREC_RADIX", ColumnType.Integer); t.CreatePrimaryKey(); for (int i = 0; i < Column.Types.Length; i++) { object[] o = t.NewRow; ColumnType type = Column.Types[i]; o[0] = Column.GetColumnTypeString(type); o[1] = type; o[2] = 0; // precision o[6] = true; // need Column to track nullable for this o[7] = true; // case sensitive o[8] = true;; o[9] = false; // unsigned o[10] = (type == ColumnType.Numeric || type == ColumnType.DbDecimal); o[11] = (type == ColumnType.Integer); o[12] = o[0]; o[13] = 0; o[14] = 0; // maximum scale o[15] = 0; o[16] = o[15]; o[17] = 10; t.Insert(o, null); } return t; } else if (name.Equals("SYSTEM_INDEXINFO")) { Table t = CreateTable(name); t.AddColumn("TABLE_" + META_CAT, ColumnType.VarChar); t.AddColumn("TABLE_" + META_SCHEM, ColumnType.VarChar); t.AddColumn("TABLE_NAME", ColumnType.VarChar); t.AddColumn("NON_UNIQUE", ColumnType.Bit); t.AddColumn("INDEX_QUALIFIER", ColumnType.VarChar); t.AddColumn("INDEX_NAME", ColumnType.VarChar); t.AddColumn("TYPE", ColumnType.SmallInt); t.AddColumn(META_ORDINAL_POSITON, ColumnType.SmallInt); t.AddColumn("COLUMN_NAME", ColumnType.VarChar); t.AddColumn(META_ASC_OR_DESC, ColumnType.VarChar); t.AddColumn("CARDINALITY", ColumnType.Integer); t.AddColumn("PAGES", ColumnType.Integer); t.AddColumn("FILTER_CONDITION", ColumnType.VarChar); t.CreatePrimaryKey(); for (int i = 0; i < tTable.Count; i++) { Table table = (Table) tTable[i]; Index index = null; while (true) { index = table.GetNextIndex(index); if (index == null) { break; } int[] cols = index.Columns; int len = cols.Length; // this removes the column that makes every index unique if (!index.IsUnique) { len--; } for (int j = 0; j < len; j++) { object[] o = t.NewRow; o[2] = table.Name; o[3] = !index.IsUnique; o[5] = index.Name; o[6] = 1; o[7] = (j + 1); o[8] = table.GetColumnName(cols[j]); o[9] = "A"; t.Insert(o, null); } } } return t; } else if (name.Equals("SYSTEM_UDTS")) { Table t = CreateTable(name); t.AddColumn("TYPE_" + META_CAT, ColumnType.VarChar); t.AddColumn("TYPE_" + META_SCHEM, ColumnType.VarChar); t.AddColumn("TYPE_NAME", ColumnType.VarChar); t.AddColumn("CLASS_NAME", ColumnType.Bit); t.AddColumn("DATA_TYPE", ColumnType.VarChar); t.AddColumn("REMARKS", ColumnType.VarChar); t.CreatePrimaryKey(); return t; } else if (name.Equals("SYSTEM_CONNECTIONINFO")) { Table t = CreateTable(name); t.AddColumn("KEY", ColumnType.VarChar); t.AddColumn("VALUE", ColumnType.VarChar); t.CreatePrimaryKey(); object[] o = t.NewRow; o[0] = "USER"; o[1] = channel.UserName; t.Insert(o, null); o = t.NewRow; o[0] = "READONLY"; o[1] = channel.IsReadOnly ? "TRUE" : "FALSE"; t.Insert(o, null); o = t.NewRow; o[0] = "MAXROWS"; o[1] = "" + channel.MaxRows; t.Insert(o, null); o = t.NewRow; o[0] = "DATABASE"; o[1] = "" + channel.Database.Name; t.Insert(o, null); o = t.NewRow; o[0] = "IDENTITY"; o[1] = "" + channel.LastIdentity; t.Insert(o, null); return t; } else if (name.Equals("SYSTEM_USERS")) { Table t = CreateTable(name); t.AddColumn("USER", ColumnType.VarChar); t.AddColumn("ADMIN", ColumnType.Bit); t.CreatePrimaryKey(); ArrayList v = aAccess.GetUsers(); for (int i = 0; i < v.Count; i++) { User u = (User) v[i]; // todo: this is not a nice implementation if (u == null) { continue; } string user = u.Name; if (!user.Equals("PUBLIC")) { object[] o = t.NewRow; o[0] = user; o[1] = u.IsAdmin; t.Insert(o, null); } } return t; } return null; }
public void Insert(object[] row, Channel channel) { if (dDatabase.IsReferentialIntegrity) { for (int i = 0; i < iConstraintCount; i++) { ((Constraint) vConstraint[i]).CheckInsert(row); } } InsertNoCheck(row, channel); }
public Result GetResult(int maxrows, Channel cChannel) { // [email protected] begin changes from 1.50 return GetResult( 0, maxrows, cChannel ); }
public void InsertNoCheck(object[] row, Channel channel) { InsertNoCheck(row, channel, true); }
public Function( string fqn, Channel channel ) { sFunction = fqn; this.channel = channel; fID = Library.FunctionID(channel.Database.Alias, fqn); if( fID > -1 ) // internal function { mMethod = methodCache[fqn] as MethodInfo; if( mMethod == null ) { int i = fqn.LastIndexOf('.'); TracingHelper.Check(i != -1, TracingHelper.UnexpectedToken, fqn); String classname = fqn.Substring(0, i); Type type = thisAssembly.GetType(classname, false); TracingHelper.Check(type != null, TracingHelper.ERROR_IN_FUNCTION, fqn); this.name = fqn.Substring(i+1); mMethod = type.GetMethod(name); TracingHelper.Check(mMethod != null, TracingHelper.UNKNOWN_FUNCTION, fqn); methodCache[fqn] = mMethod; } } else // external function { mMethod = methodCache[fqn] as MethodInfo; if( mMethod == null ) { int x = fqn.IndexOf(','); TracingHelper.Check(x != -1, TracingHelper.UnexpectedToken, fqn); string assembly = fqn.Substring(0, x); string className = fqn.Substring(x+1); int i = className.LastIndexOf('.'); TracingHelper.Check(i != -1, TracingHelper.UnexpectedToken, fqn); this.name = className.Substring(i+1); className = className.Substring(0, i); Assembly a = Assembly.Load(assembly); Type type = a.GetType(className, false); TracingHelper.Check(type != null, TracingHelper.ERROR_IN_FUNCTION, fqn); mMethod = type.GetMethod(name); TracingHelper.Check(mMethod != null, TracingHelper.UNKNOWN_FUNCTION, fqn); methodCache[fqn] = mMethod; } } ParameterInfo[] pi = mMethod.GetParameters(); if( pi != null && pi.Length > 0 ) eArg = new Expression[pi.Length]; else eArg = new Expression[]{}; aArgTypes = new Type[eArg.Length]; for( int i=0;i<aArgTypes.Length;i++) { aArgTypes[i] = pi[i].ParameterType; } cReturnType = mMethod.ReturnType; iReturnType = GetDataType( cReturnType ); iSqlArgCount = eArg.Length; }
public void InsertNoCheck(object[] row, Channel c, bool log) { int i; if (iIdentityColumn != -1) { if (row[iIdentityColumn] == null) { if (c != null) { c.LastIdentity = iIdentityId; } row[iIdentityColumn] = iIdentityId++; } else { i = (int) row[iIdentityColumn]; if (iIdentityId <= i) { if (c != null) { c.LastIdentity = i; } iIdentityId = i + 1; } } } if (iTimestampColumn != -1) { if (row[iTimestampColumn] == null) { row[iTimestampColumn] = DateTime.Now; } else { DateTime timestamp = DateTime.Now; DateTime original = (DateTime) row[iTimestampColumn]; // just in case to assure our timestamp is unique if ( timestamp == original ) { row[iTimestampColumn] = timestamp.AddMilliseconds(1); } else { row[iTimestampColumn] = timestamp; } } } for (i = 0; i < iColumnCount; i++) { if (row[i] == null &&!GetColumn(i).IsNullable) { throw TracingHelper.Error(TracingHelper.TRY_TO_INSERT_NULL); } } try { Row r = new Row(this, row); for (i = 0; i < iIndexCount; i++) { Node n = r.GetNode(i); GetIndex(i).Insert(n); } } catch (Exception e) { // rollback insert for (--i; i >= 0; i--) { GetIndex(i).Delete(row, i == 0); } throw e; // and throw error again } if (c != null) { c.AddTransactionInsert(this, row); } if (lLog != null) { lLog.Write(c, GetInsertStatement(row)); } }
/// <summary> /// Script the database objects to a file. /// </summary> /// <param name="db"></param> /// <param name="file"></param> /// <param name="full"></param> /// <param name="channel"></param> public static void ScriptToFile(Database db, string file, bool full, Channel channel) { if ((new FileInfo(file)).Exists) { // there must be no such file; overwriting not allowed for security throw TracingHelper.Error(TracingHelper.FILE_IO_ERROR, file); } try { DateTime time = DateTime.Now; // only ddl commands; needs not so much memory Result r; if (full) { // no drop, no insert, and no positions for cached tables r = db.GetScript(false, false, false, channel); } else { // no drop, no insert, but positions for cached tables r = db.GetScript(false, false, true, channel); } Record n = r.Root; StreamWriter w = new StreamWriter(file); while (n != null) { writeLine(w, (string) n.Data[0]); n = n.Next; } // inserts are done separetely to save memory ArrayList tables = db.Tables; for (int i = 0; i < tables.Count; i++) { Table t = (Table) tables[i]; // cached tables have the index roots set in the ddl script if (full ||!t.IsCached) { Index primary = t.PrimaryIndex; Node x = primary.First(); while (x != null) { writeLine(w, t.GetInsertStatement(x.GetData())); x = primary.Next(x); } } } w.Close(); TimeSpan execution = DateTime.Now.Subtract(time); if (TracingHelper.TraceEnabled) TracingHelper.Write((Int64)execution.TotalMilliseconds); } catch (IOException e) { TracingHelper.Error(TracingHelper.FILE_IO_ERROR, file + " " + e); } }
public Result query(Channel myChannel, string query) { var r = this.db.Execute(query, myChannel); this.errormsg = r.Error; return r; }
/// <summary> /// Opens the transaction log and runs it. /// </summary> private void RunScript() { if (TracingHelper.TraceEnabled) TracingHelper.Write(); if (!(new FileInfo(sFileScript)).Exists) return; bRestoring = true; dDatabase.IsReferentialIntegrity = false; ArrayList channel = new ArrayList(); channel.Add(cSystem); Channel current = cSystem; int size = 1; try { DateTime time = DateTime.Now; StreamReader r = new StreamReader(sFileScript); while (true) { string s = r.ReadLine(); if (s == null) { break; } if (s.StartsWith("/*C")) { int id = Int32.Parse(s.Substring(3,(s.IndexOf('*', 4)-3))); if( id > (channel.Count-1) ) { current = new Channel(cSystem, id); channel.Add(current); dDatabase.RegisterChannel(current); } else { current = (Channel)channel[id]; } s = s.Substring(s.IndexOf('/', 1) + 1); } if (!s.Equals("")) { dDatabase.Execute(s, current); } if (s.Equals("DISCONNECT")) { int id = current.Id; current = new Channel(cSystem, id); channel.RemoveAt(id); channel.Insert(id, current); } } r.Close(); for (int i = 0; i < size; i++) { current = (Channel) channel[i]; if (current != null) { current.Rollback(); } } TimeSpan execution = DateTime.Now.Subtract(time); if (TracingHelper.TraceEnabled) TracingHelper.Write((Int64)execution.TotalMilliseconds); } catch (IOException e) { throw TracingHelper.Error(TracingHelper.FILE_IO_ERROR, sFileScript + " " + e); } dDatabase.IsReferentialIntegrity = true; bRestoring = false; }
private Result ProcessCommit(Tokenizer c, Channel channel) { string sToken = c.GetString(); if (!sToken.Equals("WORK")) { c.Back(); } channel.Commit(); return new Result(); }