/// <summary> /// Verify if the delete operation can be performed. /// </summary> /// <param name="row"></param> internal void CheckDelete(object[] row) { if (_type == ConstraintType.ForeignKey || _type == ConstraintType.Unique) { // deleting references are never a problem // unique constraints are checked by the unique index return; } // must be called synchronized because of _refData for (int i = 0; i < _len; i++) { object o = row[_mainColumns[i]]; if (o == null) { // if one column is null then integrity is not checked return; } _refData[_refColumns[i]] = o; } // there must be no record in the 'slave' table TracingHelper.Check(_refIndex.Find(_refData) == null, TracingHelper.INTEGRITY_CONSTRAINT_VIOLATION); }
/// <summary> /// Finalizes a nested transaction. /// </summary> /// <param name="rollback"></param> internal void EndNestedTransaction(bool rollback) { TracingHelper.Check(!_closed, TracingHelper.CONNECTION_IS_CLOSED); TracingHelper.Assert(_nestedTransaction, "EndNestedTransaction"); int i = _transaction.Count - 1; if (rollback) { while (i >= _nestedOldTransIndex) { Transaction t = (Transaction)_transaction[i]; t.Rollback(); i--; } } _nestedTransaction = false; _autoCommit = _nestedOldAutoCommit; if (_autoCommit == true) { _transaction.RemoveRange(_nestedOldTransIndex, (_transaction.Count - _nestedOldTransIndex)); } }
/// <summary> /// Verify if the insert operation can be performed. /// </summary> /// <param name="row"></param> internal void CheckInsert(object[] row) { if (_type == ConstraintType.Main || _type == ConstraintType.Unique) { // inserts in the main table are never a problem // unique constraints are checked by the unique index return; } // must be called synchronized because of _mainData for (int i = 0; i < _len; i++) { object o = row[_refColumns[i]]; if (o == null) { // if one column is null then integrity is not checked return; } _mainData[_mainColumns[i]] = o; } // a record must exist in the main table TracingHelper.Check(_mainIndex.Find(_mainData) != null, TracingHelper.INTEGRITY_CONSTRAINT_VIOLATION); }
/// <summary> /// Check if the expression is resolved, recursively. /// </summary> public void CheckResolved() { TracingHelper.Check(_type != ExpressionType.DatabaseColumn || tFilter != null, TracingHelper.COLUMN_NOT_FOUND, sColumn); if (eArg != null) { eArg.CheckResolved(); } if (eArg2 != null) { eArg2.CheckResolved(); } if (sSelect != null) { sSelect.CheckResolved(); } if (fFunction != null) { fFunction.CheckResolved(); } }
internal Row(Table t, BinaryReader din, int pos, Row before) { tTable = t; int index = tTable.IndexCount; Pos = pos; nFirstIndex = new Node(this, din, 0); Node n = nFirstIndex; for (int i = 1; i < index; i++) { n.nNext = new Node(this, din, i); n = n.nNext; } int l = tTable.InternalColumnCount; oData = Column.ReadData(din, l); int iCurrent = din.ReadInt32(); LogHelper.Publish(String.Format("Row read with {0} columns. Row read position from file: {1}. Current position: {2}.", oData.Length, iCurrent, Pos), LogEntryType.Debug); TracingHelper.Check(iCurrent == Pos, TracingHelper.INPUTSTREAM_ERROR); Insert(before); LastAccess = CurrentAccess++; }
/// <summary> /// Sets the auto commit mode for this channel. /// </summary> /// <param name="autoCommit"></param> public void SetAutoCommit(bool autoCommit) { TracingHelper.Check(!_closed, TracingHelper.CONNECTION_IS_CLOSED); Commit(); _autoCommit = autoCommit; }
/// <summary> /// Add an insert transaction to the current transaction list. /// </summary> /// <param name="table"></param> /// <param name="row"></param> internal void AddTransactionInsert(Table table, object[] row) { TracingHelper.Check(!_closed, TracingHelper.CONNECTION_IS_CLOSED); if (!_autoCommit) { Transaction t = new Transaction(false, table, row); _transaction.Add(t); } }
/// <summary> /// Begins a new nested transaction. /// </summary> internal void BeginNestedTransaction() { TracingHelper.Check(!_closed, TracingHelper.CONNECTION_IS_CLOSED); TracingHelper.Assert(!_nestedTransaction, "beginNestedTransaction"); _nestedOldAutoCommit = _autoCommit; // now all transactions are logged _autoCommit = false; _nestedOldTransIndex = _transaction.Count; _nestedTransaction = true; }
public void CheckDropIndex(string index) { for (int i = 0; i < iIndexCount; i++) { if (index.Equals(GetIndex(i).Name)) { TracingHelper.Check(i != 0, TracingHelper.DROP_PRIMARY_KEY); return; } } throw TracingHelper.Error(TracingHelper.INDEX_NOT_FOUND, index); }
/// <summary> /// Gets a declare object value. /// </summary> /// <param name="name"></param> internal object GetDeclareValue(string name) { TracingHelper.Check(!_closed, TracingHelper.CONNECTION_IS_CLOSED); Declare declare = _variables[name] as Declare; if (declare != null) { return(declare.Value); } else { throw TracingHelper.Error(TracingHelper.VARIABLE_NOT_DECLARED); } }
/// <summary> /// Sets a declare object value. /// </summary> /// <param name="name"></param> /// <param name="value"></param> internal void SetDeclareValue(string name, object value) { TracingHelper.Check(!_closed, TracingHelper.CONNECTION_IS_CLOSED); Declare declare = _variables[name] as Declare; if (declare != null) { declare.Value = value; declare.Expression.SetArg(value); } else { throw TracingHelper.Error(TracingHelper.VARIABLE_NOT_DECLARED); } }
/// <summary> /// Rollbacks all pending transactions. /// </summary> public void Rollback() { TracingHelper.Check(!_closed, TracingHelper.CONNECTION_IS_CLOSED); int i = _transaction.Count - 1; while (i >= 0) { Transaction t = (Transaction)_transaction[i]; t.Rollback(); i--; } _transaction.Clear(); }
/// <summary> /// </summary> private void SaveSorted(int count) { if (count < 1) { return; } Sort(_rowWriter, 0, count - 1); try { BinaryWriter b = new BinaryWriter(_fileStream); LogHelper.Publish("Saving rows to cache.", LogEntryType.Debug); for (int i = 0; i < count; i++) { LogHelper.Publish(String.Format("Writing row number {0}. File at position: {1}.", i, _fileStream.Position), LogEntryType.Debug); if (_fileStream.Position < _rowWriter[i].Pos) { _fileStream.Seek(_rowWriter[i].Pos, SeekOrigin.Begin); } if (_fileStream.Position > _rowWriter[i].Pos) { TracingHelper.Error(TracingHelper.INPUTSTREAM_ERROR); } byte[] row = _rowWriter[i].Write(); TracingHelper.Check((row.Length == _rowWriter[i].Size), TracingHelper.SERIALIZATION_FAILURE); LogHelper.Publish(String.Format("Byte array size: {0}. Row Pos: {1}. Row Size: {2}", row.Length, _rowWriter[i].Pos, _rowWriter[i].Size), LogEntryType.Debug); b.Write(row); } } catch (Exception e) { throw TracingHelper.Error(TracingHelper.FILE_IO_ERROR, "saveSorted " + e); } }
/// <summary> /// GetUser method declaration. /// This method is used to return an instance of a particular User object, /// given the user name and password. /// </summary> /// <remarks> /// An ACCESS_IS_DENIED exception will be thrown if an attempt /// is made to get the PUBLIC user. /// </remarks> /// <param name="name">user name</param> /// <param name="password">user password</param> /// <returns>The requested User object</returns> public User GetUser(string name, string password) { TracingHelper.Check(!name.Equals("PUBLIC"), TracingHelper.ACCESS_IS_DENIED); if (name == null) { name = ""; } if (password == null) { password = ""; } User u = Get(name); u.CheckPassword(password); return(u); }
public object GetValue(ColumnType type) { Resolve(); Result r = GetResult(2, null); // 2 records are (already) too much int size = r.Size; int len = r.ColumnCount; TracingHelper.Check(size == 1 && len == 1, TracingHelper.SINGLE_VALUE_EXPECTED); object o = r.Root.Data[0]; if (r.Type[0] == type) { return(o); } string s = Column.ConvertToString(o, type); return(Column.ConvertString(s, type)); }
public void AddColumn(string name, ColumnType type, bool nullable, bool identity) { if (identity) { TracingHelper.Check(type == ColumnType.Integer, TracingHelper.WRONG_DATA_TYPE, name); TracingHelper.Check(iIdentityColumn == -1, TracingHelper.SECOND_PRIMARY_KEY, name); iIdentityColumn = iColumnCount; } if (type == ColumnType.Timestamp) { iTimestampColumn = iColumnCount; } TracingHelper.Assert(iPrimaryKey == -1, "Table.addColumn"); vColumn.Add(new Column(name, nullable, type, identity)); iColumnCount++; }
/// <summary> /// DropUser method declaration. /// This method is used to drop a user. Since we are using a vector /// to hold the User objects, we must iterate through the ArrayList looking /// for the name. The user object is currently set to null, and all access /// rights revoked. /// </summary> /// <remarks> /// An ACCESS_IS_DENIED exception will be thrown if an attempt /// is made to drop the PUBLIC user. /// </remarks> /// <param name="name">name of the user to be dropped</param> public void DropUser(string name) { TracingHelper.Check(!name.Equals("PUBLIC"), TracingHelper.ACCESS_IS_DENIED); for (int i = 0; i < uUser.Count; i++) { User u = (User)uUser[i]; if (u != null && u.Name.Equals(name)) { // todo: find a better way. Problem: removeElementAt would not // work correctly while others are connected uUser[i] = null; u.RevokeAll(); // in case the user is referenced in another way return; } } throw TracingHelper.Error(TracingHelper.USER_NOT_FOUND, name); }
private void GetToken() { if (bWait) { bWait = false; return; } while (iIndex < iLength && Char.IsWhiteSpace(cCommand[iIndex])) { iIndex++; } sToken = ""; if (iIndex >= iLength) { iType = 0; return; } bool point = false, digit = false, exp = false, afterexp = false; bool end = false; char c = cCommand[iIndex]; char cfirst = '0'; StringBuilder name = new StringBuilder(); if (Char.IsLetter(c)) { iType = TokenType.NAME; } else if ("[](),*=;+%".IndexOf(c) >= 0) { iType = TokenType.SPECIAL; iIndex++; sToken = new String(c, 1); return; } else if (Char.IsDigit(c)) { iType = TokenType.NUMBER; digit = true; } else if ("@".IndexOf(c) >= 0) { cfirst = c; iType = TokenType.VARIABLE; } else if ("!<>|/-".IndexOf(c) >= 0) { cfirst = c; iType = TokenType.SPECIAL; } else if (c == '\"') { iType = TokenType.QUOTED_IDENTIFIER; } else if (c == '\'') { iType = TokenType.STRING; name.Append('\''); } else if (c == '.') { iType = TokenType.FLOAT; point = true; } else { throw TracingHelper.Error(TracingHelper.UnexpectedToken, "" + c); } int start = iIndex++; while (true) { if (iIndex >= iLength) { c = ' '; end = true; TracingHelper.Check(iType != TokenType.STRING && iType != TokenType.QUOTED_IDENTIFIER, TracingHelper.UNEXPECTED_END_OF_COMMAND); } else { c = cCommand[iIndex]; } switch (iType) { case TokenType.NAME: if (Char.IsLetter(c) || Char.IsDigit(c) || c.Equals('_')) { break; } sToken = sCommand.Substring(start, (iIndex - start)).ToUpper(); if (c == '.') { sLongNameFirst = sToken; iIndex++; GetToken(); // todo: eliminate recursion sLongNameLast = sToken; iType = TokenType.LONG_NAME; sToken = sLongNameFirst + "." + sLongNameLast; } return; case TokenType.QUOTED_IDENTIFIER: if (c == '\"') { iIndex++; if (iIndex >= iLength) { sToken = name.ToString(); return; } c = cCommand[iIndex]; if (c == '.') { sLongNameFirst = name.ToString(); iIndex++; GetToken(); // todo: eliminate recursion sLongNameLast = sToken; iType = TokenType.LONG_NAME; sToken = sLongNameFirst + "." + sLongNameLast; return; } if (c != '\"') { sToken = name.ToString(); return; } } name.Append(c); break; case TokenType.VARIABLE: if (char.IsWhiteSpace(c) || char.IsPunctuation(c) || #if !POCKETPC char.IsSeparator(c) || #endif iIndex >= iLength) { sToken = name.ToString(); return; } name.Append(c); break; case TokenType.STRING: if (c == '\'') { iIndex++; if (iIndex >= iLength || cCommand[iIndex] != '\'') { sToken = name.ToString(); return; } } name.Append(c); break; case TokenType.REMARK: if (end) { // unfinished remark // maybe print error here iType = 0; return; } else if (c == '*') { iIndex++; if (iIndex < iLength && cCommand[iIndex] == '/') { // using recursion here iIndex++; GetToken(); return; } } break; case TokenType.REMARK_LINE: if (end) { iType = 0; return; } else if (c == '\r' || c == '\n') { // using recursion here GetToken(); return; } break; case TokenType.SPECIAL: if (c == '/' && cfirst == '/') { iType = TokenType.REMARK_LINE; break; } else if (c == '-' && cfirst == '-') { iType = TokenType.REMARK_LINE; break; } else if (c == '*' && cfirst == '/') { iType = TokenType.REMARK; break; } else if (">=|".IndexOf(c) >= 0) { break; } sToken = sCommand.Substring(start, (iIndex - start)); return; case TokenType.FLOAT: case TokenType.NUMBER: if (Char.IsDigit(c)) { digit = true; } else if (c == '.') { iType = TokenType.FLOAT; if (point) { throw TracingHelper.Error(TracingHelper.UnexpectedToken, "."); } point = true; } else if (c == 'E' || c == 'e') { if (exp) { throw TracingHelper.Error(TracingHelper.UnexpectedToken, "E"); } afterexp = true; // first character after exp may be + or - point = true; exp = true; } else if (c == '-' && afterexp) { afterexp = false; } else if (c == '+' && afterexp) { afterexp = false; } else { afterexp = false; if (!digit) { if (point && start == iIndex - 1) { sToken = "."; iType = TokenType.SPECIAL; return; } throw TracingHelper.Error(TracingHelper.UnexpectedToken, "" + c); } sToken = sCommand.Substring(start, (iIndex - start)); return; } break; } iIndex++; } }
/// <summary> /// Check a desired right over an specific database object. /// </summary> /// <param name="databaseObject"></param> /// <param name="right"></param> internal void Check(string databaseObject, AccessType right) { TracingHelper.Check(!_closed, TracingHelper.CONNECTION_IS_CLOSED); _user.Check(databaseObject, right); }
/// <summary> /// Test condition for True or False value. /// </summary> /// <returns></returns> public bool Test() { switch (_type) { case ExpressionType.True: return(true); case ExpressionType.Not: TracingHelper.Assert(eArg2 == null, "Expression.test"); return(!eArg.Test()); case ExpressionType.And: return(eArg.Test() && eArg2.Test()); case ExpressionType.Or: return(eArg.Test() || eArg2.Test()); case ExpressionType.Like: // todo: now for all tests a new 'like' object required! string s = (string)eArg2.GetValue(ColumnType.VarChar); ColumnType type = eArg._columnType; Like l = new Like(s, cLikeEscape, type == ColumnType.VarCharIgnoreCase); string c = (string)eArg.GetValue(ColumnType.VarChar); return(l.Compare(c)); case ExpressionType.In: return(eArg2.TestValueList(eArg.GetValue(), eArg._columnType)); case ExpressionType.Exists: Result r = eArg.sSelect.GetResult(1, null); // 1 is already enough return(r.Root != null); } TracingHelper.Check(eArg != null, TracingHelper.GENERAL_ERROR); object o = eArg.GetValue(); ColumnType dtype = eArg._columnType; TracingHelper.Check(eArg2 != null, TracingHelper.GENERAL_ERROR); object o2 = eArg2.GetValue(dtype); int result = Column.Compare(o, o2, dtype); switch (_type) { case ExpressionType.Equal: return(result == 0); case ExpressionType.Bigger: return(result > 0); case ExpressionType.BiggerEqual: return(result >= 0); case ExpressionType.SmallerEqual: return(result <= 0); case ExpressionType.Smaller: return(result < 0); case ExpressionType.NotEqual: return(result != 0); } TracingHelper.Assert(false, "Expression.test2"); return(false); }
/// <summary> /// Gets the expression resulting value. /// </summary> /// <returns></returns> public object GetValue() { switch (_type) { case ExpressionType.Value: return(_data); case ExpressionType.DatabaseColumn: try { return(tFilter.oCurrentData[iColumn]); } catch { throw TracingHelper.Error(TracingHelper.COLUMN_NOT_FOUND, sColumn); } case ExpressionType.Function: return(fFunction.GetValue()); case ExpressionType.Query: return(sSelect.GetValue(_columnType)); case ExpressionType.Negate: return(Column.Negate(eArg.GetValue(_columnType), _columnType)); case ExpressionType.Count: // count(*): sum(1); count(col): sum(col<>null) if (eArg.Type == ExpressionType.Asterix || eArg.GetValue() != null) { return(1); } return(0); case ExpressionType.Maximum: case ExpressionType.Minimum: case ExpressionType.Sum: case ExpressionType.Average: return(eArg.GetValue()); case ExpressionType.Exists: return(Test()); case ExpressionType.Convert: return(eArg.GetValue(_columnType)); case ExpressionType.CaseWhen: if (eArg.Test()) { return(eArg2.eArg.GetValue()); } else { return(eArg2.eArg2.GetValue()); } case ExpressionType.Variable: return(eArg.GetValue()); } // todo: simplify this object a = null, b = null; if (eArg != null) { a = eArg.GetValue(_columnType); } if (eArg2 != null) { b = eArg2.GetValue(_columnType); } switch (_type) { case ExpressionType.Add: return(Column.Add(a, b, _columnType)); case ExpressionType.Subtract: return(Column.Subtract(a, b, _columnType)); case ExpressionType.Multiply: return(Column.Multiply(a, b, _columnType)); case ExpressionType.Divide: return(Column.Divide(a, b, _columnType)); case ExpressionType.Concat: return(Column.Concat(a, b, _columnType)); case ExpressionType.IfNull: return(a == null ? b : a); case ExpressionType.Equal: if (eArg.Type == ExpressionType.Variable) { TracingHelper.Check(eArg2 != null, TracingHelper.GENERAL_ERROR); return(eArg2.GetValue(eArg._columnType)); } else { return(Test()); } default: // must be comparisation // todo: make sure it is return(Test()); } }
/// <summary> /// Resolve the current expression. /// </summary> /// <param name="filter"></param> public void Resolve(TableFilter filter) { if (filter != null && _type == ExpressionType.DatabaseColumn) { if (sTable == null || filter.Name.Equals(sTable)) { int i = filter.Table.SearchColumn(sColumn); if (i != -1) { // todo: other error message: multiple tables are possible TracingHelper.Check(tFilter == null || tFilter == filter, TracingHelper.COLUMN_NOT_FOUND, sColumn); tFilter = filter; iColumn = i; sTable = filter.Name; _columnType = filter.Table.GetColumnType(i); } } } // currently sets only data type // todo: calculate fixed expressions if possible if (eArg != null) { eArg.Resolve(filter); } if (eArg2 != null) { eArg2.Resolve(filter); } if (sSelect != null) { sSelect.Resolve(filter, false); sSelect.Resolve(); } if (fFunction != null) { fFunction.Resolve(filter); } if (_columnType != ColumnType.Null) { return; } switch (_type) { case ExpressionType.Function: _columnType = fFunction.GetReturnType(); break; case ExpressionType.Query: _columnType = sSelect.eColumn[0].ColumnType; break; case ExpressionType.Negate: _columnType = eArg.ColumnType; break; case ExpressionType.Add: case ExpressionType.Subtract: case ExpressionType.Multiply: case ExpressionType.Divide: _columnType = eArg._columnType; break; case ExpressionType.Concat: _columnType = ColumnType.VarChar; break; case ExpressionType.Not: case ExpressionType.BiggerEqual: case ExpressionType.Bigger: case ExpressionType.Smaller: case ExpressionType.SmallerEqual: case ExpressionType.NotEqual: case ExpressionType.Like: case ExpressionType.And: case ExpressionType.Or: case ExpressionType.In: case ExpressionType.Exists: _columnType = ColumnType.Bit; break; case ExpressionType.Equal: if (this.IsVarAssign) { _columnType = eArg2.ColumnType; } else { _columnType = ColumnType.Bit; } break; case ExpressionType.Count: _columnType = ColumnType.Integer; break; case ExpressionType.Maximum: case ExpressionType.Minimum: case ExpressionType.Sum: case ExpressionType.Average: _columnType = eArg.ColumnType; break; case ExpressionType.Convert: // it is already set break; case ExpressionType.IfNull: case ExpressionType.CaseWhen: _columnType = eArg2.ColumnType; break; case ExpressionType.Variable: _columnType = eArg.ColumnType; break; } }
/// <summary> /// Verifies if the database is in read only state. /// </summary> internal void CheckReadWrite() { TracingHelper.Check(!_closed, TracingHelper.CONNECTION_IS_CLOSED); TracingHelper.Check(!_readOnly, TracingHelper.DATABASE_IS_READONLY); }
/// <summary> /// Sets the current user password. /// </summary> /// <param name="password"></param> internal void SetPassword(string password) { TracingHelper.Check(!_closed, TracingHelper.CONNECTION_IS_CLOSED); _user.Password = password; }
/// <summary> /// Checks the user password. /// </summary> /// <param name="test">The password to test.</param> internal void CheckPassword(string test) { TracingHelper.Check(test.Equals(_password), TracingHelper.ACCESS_IS_DENIED); }
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()); }
/// <summary> /// Executes an SQL statement and return the results. /// </summary> /// <param name="statement">The SQL statement to execute.</param> /// <param name="channel">The channel to use.</param> /// <returns>The Result object.</returns> public Result Execute(string statement, Channel channel) { if (TracingHelper.TraceEnabled) { TracingHelper.Write(statement); } Tokenizer c = new Tokenizer(statement); Parser p = new Parser(this, c, channel); Result rResult = new Result(); string newStatement = string.Empty; int updateCount = 0; try { if (_log != null && _log.cCache != null) { _log.cCache.CleanUp(); } if (TracingHelper.AssertEnabled) { TracingHelper.Assert(!channel.IsNestedTransaction); } TracingHelper.Check(channel != null, TracingHelper.ACCESS_IS_DENIED); TracingHelper.Check(!_shutDown, TracingHelper.DATABASE_IS_SHUTDOWN); while (true) { int begin = c.Position; bool script = false; string sToken = c.GetString(); if (sToken.Equals("")) { break; } switch (sToken) { case "SELECT": rResult = p.ProcessSelect(); break; case "INSERT": rResult = p.ProcessInsert(); break; case "UPDATE": rResult = p.ProcessUpdate(); break; case "DELETE": rResult = p.ProcessDelete(); break; case "ALTER": rResult = p.ProcessAlter(); break; case "CREATE": rResult = ProcessCreate(c, channel); script = true; break; case "DROP": rResult = ProcessDrop(c, channel); script = true; break; case "GRANT": rResult = ProcessGrantOrRevoke(c, channel, true); script = true; break; case "REVOKE": rResult = ProcessGrantOrRevoke(c, channel, false); script = true; break; case "CONNECT": rResult = ProcessConnect(c, channel); break; case "DISCONNECT": rResult = ProcessDisconnect(c, channel); break; case "SET": rResult = ProcessSet(c, channel); script = true; break; case "SCRIPT": rResult = ProcessScript(c, channel); break; case "COMMIT": rResult = ProcessCommit(c, channel); script = true; break; case "ROLLBACK": rResult = ProcessRollback(c, channel); script = true; break; case "SHUTDOWN": rResult = ProcessShutdown(c, channel); break; case "CHECKPOINT": rResult = ProcessCheckpoint(channel); break; case "CALL": rResult = p.ProcessCall(); break; case "SHOW": rResult = ProcessShow(c, channel); break; case "DECLARE": rResult = p.ProcessDeclare(); script = true; break; case ";": continue; default: throw TracingHelper.Error(TracingHelper.UnexpectedToken, sToken); } if (rResult != null && rResult.UpdateCount > updateCount) { updateCount = rResult.UpdateCount; } if (script && _log != null) { int end = c.Position; _log.Write(channel, c.GetPart(begin, end)); } } } catch (Exception e) { rResult = new Result(TracingHelper.GetMessage(e) + " in statement [" + statement + "]"); } if (rResult != null && rResult.UpdateCount < updateCount) { rResult.SetUpdateCount(updateCount); } return(rResult); }
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; }
/// <summary> /// Check if the user has administrative priviledges. /// </summary> internal void CheckAdmin() { TracingHelper.Check(IsAdmin, TracingHelper.ACCESS_IS_DENIED); }