⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 table.cs

📁 C#数据库中间层处理,支持主流数据库,如sql、oracle等
💻 CS
📖 第 1 页 / 共 2 页
字号:
		 */
		public int getType(int i) 
		{
			return getColumn(i).iType;
		}

		/**
		 * Method declaration
		 *
		 *
		 * @param column
		 *
		 * @throws Exception
		 */
		public void createPrimaryKey(int column) 
		{
			Trace.assert(iPrimaryKey == -1, "Table.createPrimaryKey(column)");

			iVisibleColumns = iColumnCount;
			iPrimaryKey = column;

			int[] col = new int[1];
			col[0] = column;

			createIndex(col, "SYSTEM_PK", true);
		}

		/**
		 * Method declaration
		 *
		 *
		 * @throws Exception
		 */
		public void createPrimaryKey() 
		{
			Trace.assert(iPrimaryKey == -1, "Table.createPrimaryKey");
			addColumn("SYSTEM_ID", Column.INTEGER, true, true);
			createPrimaryKey(iColumnCount - 1);

			iVisibleColumns = iColumnCount - 1;
		}

		/**
		 * Method declaration
		 *
		 *
		 * @param index
		 *
		 * @throws Exception
		 */
		public void createIndex(Index index) 
		{
			createIndex(index.getColumns(), index.getName(), index.isUnique());
		}

		/**
		 * Method declaration
		 *
		 *
		 * @param column
		 * @param name
		 * @param unique
		 *
		 * @throws Exception
		 */
		public void createIndex(int[] column, string name,
			bool unique) 
		{
			Trace.assert(iPrimaryKey != -1, "createIndex");

			for (int i = 0; i < iIndexCount; i++) 
			{
				Index index = getIndex(i);

				if (name.Equals(index.getName())) 
				{
					throw Trace.error(Trace.INDEX_ALREADY_EXISTS);
				}
			}

			int s = column.Length;

			// The primary key field is added for non-unique indexes
			// making all indexes unique
			int[] col = new int[unique ? s : s + 1];
			int[] type = new int[unique ? s : s + 1];

			for (int j = 0; j < s; j++) 
			{
				col[j] = column[j];
				type[j] = getColumn(col[j]).iType;
			}

			if (!unique) 
			{
				col[s] = iPrimaryKey;
				type[s] = getColumn(iPrimaryKey).iType;
			}

			Index newindex = new Index(name, col, type, unique);

			if (iIndexCount != 0) 
			{
				Trace.assert(isEmpty(), "createIndex");
			}

			vIndex.Add(newindex);

			iIndexCount++;
		}

		/**
		 * Method declaration
		 *
		 *
		 * @param index
		 *
		 * @throws Exception
		 */
		public void checkDropIndex(string index) 
		{
			for (int i = 0; i < iIndexCount; i++) 
			{
				if (index.Equals(getIndex(i).getName())) 
				{
					Trace.check(i != 0, Trace.DROP_PRIMARY_KEY);

					return;
				}
			}

			throw Trace.error(Trace.INDEX_NOT_FOUND, index);
		}

		/**
		 * Method declaration
		 *
		 *
		 * @return
		 */
		public bool isEmpty() 
		{
			return getIndex(0).getRoot() == null;
		}

		/**
		 * Method declaration
		 *
		 *
		 * @return
		 */
		public object[] getNewRow()
		{
			return new object[iColumnCount];
		}

		/**
		 * Method declaration
		 *
		 *
		 * @param from
		 *
		 * @throws Exception
		 */
		public void moveData(Table from) 
		{
			Index index = from.getPrimaryIndex();
			Node  n = index.first();

			while (n != null) 
			{
				if (Trace.STOP) 
				{
					Trace.stop();
				}

				object[] o = n.getData();

				insertNoCheck(o, null);

				n = index.next(n);
			}

			index = getPrimaryIndex();
			n = index.first();

			while (n != null) 
			{
				if (Trace.STOP) 
				{
					Trace.stop();
				}

				object[] o = n.getData();

				from.deleteNoCheck(o, null);

				n = index.next(n);
			}
		}

		/**
		 * Method declaration
		 *
		 *
		 * @param col
		 * @param deleted
		 * @param inserted
		 *
		 * @throws Exception
		 */
		public void checkUpdate(int[] col, Result deleted,
			Result inserted) 
		{
			if (dDatabase.isReferentialIntegrity()) 
			{
				for (int i = 0; i < iConstraintCount; i++) 
				{
					Constraint v = (Constraint) vConstraint[i];

					v.checkUpdate(col, deleted, inserted);
				}
			}
		}

		/**
		 * Method declaration
		 *
		 *
		 * @param result
		 * @param c
		 *
		 * @throws Exception
		 */
		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.rRoot;
			int    len = result.getColumnCount();

			while (r != null) 
			{
				object[] row = getNewRow();

				for (int i = 0; i < len; i++) 
				{
					row[i] = r.data[i];
				}

				insert(row, c);

				r = r.next;
			}
		}

		/**
		 * Method declaration
		 *
		 *
		 * @param row
		 * @param c
		 *
		 * @throws Exception
		 */
		public void insert(object[] row, Channel c) 
		{
			if (dDatabase.isReferentialIntegrity()) 
			{
				for (int i = 0; i < iConstraintCount; i++) 
				{
					((Constraint) vConstraint[i]).checkInsert(row);
				}
			}

			insertNoCheck(row, c);
		}

		/**
		 * Method declaration
		 *
		 *
		 * @param row
		 * @param c
		 *
		 * @throws Exception
		 */
		public void insertNoCheck(object[] row, Channel c) 
		{
			insertNoCheck(row, c, true);
		}

		/**
		 * Method declaration
		 *
		 *
		 * @param row
		 * @param c
		 * @param log
		 *
		 * @throws Exception
		 */
		public void insertNoCheck(object[] row, Channel c,
			bool log) 
		{
			int i;

			if (iIdentityColumn != -1) 
			{
				if (row[iIdentityColumn] == null)
				{
					if (c != null) 
					{
						c.setLastIdentity(iIdentityId);
					}

					row[iIdentityColumn] = iIdentityId++;
				} 
				else 
				{
					i = (int) row[iIdentityColumn];

					if (iIdentityId <= i) 
					{
						if (c != null) 
						{
							c.setLastIdentity(i);
						}

						iIdentityId = i + 1;
					}
				}
			}

			for (i = 0; i < iColumnCount; i++) 
			{
				if (row[i] == null &&!getColumn(i).isNullable()) 
				{
					throw Trace.error(Trace.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));
			}
		}

		/**
		 * Method declaration
		 *
		 *
		 * @param row
		 * @param c
		 *
		 * @throws Exception
		 */
		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);

		}

		/**
		 * Method declaration
		 *
		 *
		 * @param row
		 * @param c
		 *
		 * @throws Exception
		 */
		public void deleteNoCheck(object[] row, Channel c) 
		{
			deleteNoCheck(row, c, true);
		}

		/**
		 * Method declaration
		 *
		 *
		 * @param row
		 * @param c
		 * @param log
		 *
		 * @throws Exception
		 */
		public void deleteNoCheck(object[] row, Channel c,
			bool log) 
		{
			for (int i = 1; i < iIndexCount; i++) 
			{
				getIndex(i).delete(row, false);
			}

			// must delete data last
			getIndex(0).delete(row, true);

			if (c != null) 
			{
				c.addTransactionDelete(this, row);
			}

			if (lLog != null) 
			{
				lLog.write(c, getDeleteStatement(row));
			}
		}

		/**
		 * Method declaration
		 *
		 *
		 * @param row
		 *
		 * @return
		 *
		 * @throws Exception
		 */
		public string getInsertStatement(object[] row) 
		{
			StringBuilder a = new StringBuilder();
			a.Append("INSERT INTO ");

			a.Append(getName());
			a.Append(" VALUES(");

			for (int i = 0; i < iVisibleColumns; i++) 
			{
				a.Append(Column.createstring(row[i], getColumn(i).iType));
				a.Append(',');
			}
			a.Remove((a.Length - 1),1); // pop off the last comma
			a.Append(')');

			return a.ToString();
		}

		/**
		 * Method declaration
		 *
		 *
		 * @return
		 */
		public bool isCached() 
		{
			return bCached;
		}

		/**
		 * Method declaration
		 *
		 *
		 * @param s
		 *
		 * @return
		 */
		public Index getIndex(string s) 
		{
			for (int i = 0; i < iIndexCount; i++) 
			{
				Index h = getIndex(i);

				if (s.Equals(h.getName())) 
				{
					return h;
				}
			}

			// no such index
			return null;
		}

		/**
		 * Method declaration
		 *
		 *
		 * @param i
		 *
		 * @return
		 */
		public Column getColumn(int i) 
		{
			return (Column) vColumn[i];
		}

		/**
		 * Method declaration
		 *
		 *
		 * @param i
		 *
		 * @return
		 */
		private Index getIndex(int i) 
		{
			return (Index) vIndex[i];
		}

		/**
		 * Method declaration
		 *
		 *
		 * @param row
		 *
		 * @return
		 *
		 * @throws Exception
		 */
		private string getDeleteStatement(object[] row) 
		{
			StringBuilder a = new StringBuilder();
			a.Append("DELETE FROM ");

			a.Append(sName);
			a.Append(" WHERE ");

			if (iVisibleColumns < iColumnCount) 
			{
				for (int i = 0; i < iVisibleColumns; i++) 
				{
					a.Append(getColumn(i).sName);
					a.Append('=');
					a.Append(Column.createstring(row[i], getColumn(i).iType));

					if (i < iVisibleColumns - 1) 
					{
						a.Append(" AND ");
					}
				}
			} 
			else 
			{
				a.Append(getColumn(iPrimaryKey).sName);
				a.Append("=");
				a.Append(Column.createstring(row[iPrimaryKey],
					getColumn(iPrimaryKey).iType));
			}

			return a.ToString();
		}

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -