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

📄 column.cs

📁 C#数据库中间层处理,支持主流数据库,如sql、oracle等
💻 CS
📖 第 1 页 / 共 2 页
字号:
			switch (type) 
			{

				case NULL:
					return null;

				case INTEGER:
					return ((int)a / count);

				case FLOAT:
				case REAL:
					return ((float) a / count);

				case DOUBLE:
					return ((double) a / count);

				case NUMERIC:
				case DECIMAL:
					return ((decimal) a / (decimal)count);

				case TINYINT:
					return ((byte) a / count);

				case SMALLINT:
					return ((short) a / count);

				case BIGINT:
					return ((long) a / count);

				default:
					Trace.error(Trace.SUM_OF_NON_NUMERIC);
			}

			return null;
		}

		/**
		 * Method declaration
		 *
		 *
		 * @param a
		 * @param b
		 * @param type
		 *
		 * @return
		 *
		 * @throws Exception
		 */
		public static object min(object a, object b, int type) 
		{
			if (a == null) 
			{
				return b;
			}

			if (b == null) 
			{
				return a;
			}

			if (compare(a, b, type) < 0) 
			{
				return a;
			}

			return b;
		}

		/**
		 * Method declaration
		 *
		 *
		 * @param a
		 * @param b
		 * @param type
		 *
		 * @return
		 *
		 * @throws Exception
		 */
		public static object max(object a, object b, int type) 
		{
			if (a == null) 
			{
				return b;
			}

			if (b == null) 
			{
				return a;
			}

			if (compare(a, b, type) > 0) 
			{
				return a;
			}

			return b;
		}

		/**
		 * Method declaration
		 *
		 *
		 * @param a
		 * @param b
		 * @param type
		 *
		 * @return
		 *
		 * @throws Exception
		 */
		public static int compare(object a, object b, int type) 
		{
			int i = 0;

			// null handling: null==null and smaller any value
			// todo: implement standard SQL null handling
			// it is also used for grouping ('null' is one group)
			if (a == null) 
			{
				if (b == null) 
				{
					return 0;
				}

				return -1;
			}

			if (b == null) 
			{
				return 1;
			}

			switch (type) 
			{

				case NULL:
					return 0;

				case INTEGER:
					return ((int)a > (int)b) ? 1 : ((int)b > (int)a ? -1 : 0);

				case FLOAT:
				case REAL:
					return ((float)a > (float)b) ? 1 : ((float)b > (float)a ? -1 : 0);

				case DOUBLE:
					return ((double)a > (double)b) ? 1 : ((double)b > (double)a ? -1 : 0);

				case VARCHAR:

				case CHAR:

				case LONGVARCHAR:
					i = ((string) a).CompareTo((string) b);
					break;

				case VARCHAR_IGNORECASE:
					i = ((string) a).ToUpper().CompareTo(((string) b).ToUpper());

					break;

				case DATE:
				case TIME:
				case TIMESTAMP:
					if ((DateTime)a > (DateTime) b)
					{
						return 1;
					} 
					if ((DateTime)a >(DateTime) b)
					{
						return -1;
					} 
					else 
					{
						return 0;
					}

				case NUMERIC:
				case DECIMAL:
					return ((decimal)a > (decimal)b) ? 1 : ((decimal)b > (decimal)a ? -1 : 0);

				case BIT:
					return (((bool)a == (bool)b) ? 0 : 1);

				case TINYINT:
					return ((byte)a > (byte)b) ? 1 : ((byte)b > (byte)a ? -1 : 0);

				case SMALLINT:
					return ((short)a > (short)b) ? 1 : ((short)b > (short)a ? -1 : 0);

				case BIGINT:
					return ((long)a > (long)b) ? 1 : ((long)b > (long)a ? -1 : 0);

				case BINARY:

				case VARBINARY:

				case LONGVARBINARY:

				case OTHER:
					i = ((ByteArray) a).compareTo((ByteArray) b);

					break;

				default:
					throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED, type);
			}

			return (i > 0) ? 1 : (i < 0 ? -1 : 0);
		}

		/**
		 * Method declaration
		 *
		 *
		 * @param s
		 * @param type
		 *
		 * @return
		 *
		 * @throws Exception
		 */
		public static object convertstring(string s, int type) 
		{
			if (s == null) 
			{
				return null;
			}

			switch (type) 
			{

				case NULL:
					return null;

				case INTEGER:
					return s.ToInt32();

				case FLOAT:
				case REAL:
					return s.ToSingle();

				case DOUBLE:
					return s.ToDouble();

				case VARCHAR_IGNORECASE:

				case VARCHAR:

				case CHAR:

				case LONGVARCHAR:
					return s;

				case DATE:
				case TIME:
				case TIMESTAMP:
					return s.ToDateTime();

				case NUMERIC:

				case DECIMAL:
					return s.ToDecimal();

				case BIT:
					return s.ToBoolean();

				case TINYINT:
					return s.ToByte();

				case SMALLINT:
					return s.ToInt16();

				case BIGINT:
					return s.ToInt64();

				case BINARY:

				case VARBINARY:

				case LONGVARBINARY:

				case OTHER:
					return new ByteArray(s);

				default:
					throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED, type);
			}
		}

		/**
		 * Method declaration
		 *
		 *
		 * @param o
		 *
		 * @return
		 */
		public static string convertobject(object o) 
		{
			if (o == null) 
			{
				return null;
			}

			return o.ToString();
		}

		/**
		 * Method declaration
		 *
		 *
		 * @param o
		 * @param type
		 *
		 * @return
		 *
		 * @throws Exception
		 */
		public static object convertobject(object o, int type) 
		{
			if (o == null) 
			{
				return null;
			}
			return convertstring(o.ToString(), type);
		}

		/**
		 * Method declaration
		 *
		 *
		 * @param o
		 * @param type
		 *
		 * @return
		 *
		 * @throws Exception
		 */
		public static string createstring(object o, int type) 
		{
			if (o == null) 
			{
				return "NULL";
			}

			switch (type) 
			{

				case NULL:
					return "NULL";

				case BINARY:

				case VARBINARY:

				case LONGVARBINARY:

				case DATE:

				case TIME:

				case TIMESTAMP:

				case OTHER:
					return "'" + o.ToString() + "'";

				case VARCHAR_IGNORECASE:

				case VARCHAR:

				case CHAR:

				case LONGVARCHAR:
					return createstring((string) o);

				default:
					return o.ToString();
			}
		}

		/**
		 * Method declaration
		 *
		 *
		 * @param s
		 *
		 * @return
		 */
		public static string createstring(string s) 
		{
			StringBuilder b = new StringBuilder();
			b.Append("\'");

			if (s != null) 
			{
				for (int i = 0, len = s.Length; i < len; i++) 
				{
					char c = s.Substring(i,1).ToChar();

					if (c == '\'') 
					{
						b.Append(c.ToString());
					}

					b.Append(c.ToString());
				}
			}

			return b.Append('\'').ToString();
		}

		/**
		 * Method declaration
		 *
		 *
		 * @param in
		 * @param l
		 *
		 * @return
		 *
		 * @throws IOException
		 * @throws Exception
		 */
		public static object[] readData(BinaryReader din, int l) 
		{
			object[] data = new object[l];

			for (int i = 0; i < l; i++) 
			{
				int    type = din.ReadInt32();
				object o = null;
				switch (type) 
				{

					case NULL:
						o = null;

						break;

					case FLOAT:
					case REAL:
						o = din.ReadSingle();

						break;

					case DOUBLE:
						o = din.ReadDouble();

						break;

					case VARCHAR_IGNORECASE:

					case VARCHAR:

					case CHAR:

					case LONGVARCHAR:
						o = din.ReadString();

						break;

					case DATE:
					case TIME:
					case TIMESTAMP:
						o = DateTime.Parse(din.ReadString());

						break;

					case NUMERIC:

					case DECIMAL:
						o = Decimal.Parse(din.ReadString());

						break;

					case BIT:
						o = din.ReadBoolean();

						break;

					case TINYINT:
						o = din.ReadByte();

						break;

					case SMALLINT:
						o = din.ReadInt16();

						break;

					case INTEGER:
						o = din.ReadInt32();

						break;

					case BIGINT:
						o = din.ReadInt64();

						break;

					case BINARY:

					case VARBINARY:

					case LONGVARBINARY:

					case OTHER:
						int len = din.ReadInt32();
						byte[] b = new byte[len];
						din.ReadBytes(len);
						o = new ByteArray(b);

						break;

					default:
						throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED, type);
				}

				data[i] = o;
			}

			return data;
		}

		/**
		 * Method declaration
		 *
		 *
		 * @param out
		 * @param data
		 * @param t
		 *
		 * @throws IOException
		 */
		public static void writeData(BinaryWriter dout, object[] data, Table t) 
		{
			int len = t.getInternalColumnCount();
			int[] type = new int[len];

			for (int i = 0; i < len; i++) 
			{
				type[i] = t.getType(i);
			}

			writeData(dout, len, type, data);
		}

		/**
		 * Method declaration
		 *
		 *
		 * @param out
		 * @param l
		 * @param type
		 * @param data
		 *
		 * @throws IOException
		 */
		public static void writeData(BinaryWriter dout, int l, int[] type, object[] data) 
		{
			for (int i = 0; i < l; i++) 
			{
				object o = data[i];

				if (o == null) 
				{
					dout.Write(NULL);
				} 
				else 
				{
					int t = type[i];

					dout.Write(t);

					switch (t) 
					{

						case NULL:
							o = null;
							break;

						case FLOAT:
						case REAL:
							dout.Write((float)o);
							break;

						case DOUBLE:
							dout.Write((double)o);
							break;

						case BIT:
							dout.Write((bool)o);
							break;

						case TINYINT:
							dout.Write((byte)o);
							break;

						case SMALLINT:
							dout.Write((short)o);
							break;
	
						case INTEGER:
							dout.Write((int)o);
							break;

						case BIGINT:
							dout.Write((long)o);
							break;

						case BINARY:
						case VARBINARY:
						case LONGVARBINARY:
						case OTHER:
							byte[] b = ((ByteArray)o).byteValue();
							dout.Write(b.Length);
							dout.Write(b);
							break;

						default:
							dout.WriteString(o.ToString());
							break;
					}
				}
			}
		}

		/**
		 * Method declaration
		 *
		 *
		 * @param data
		 * @param t
		 *
		 * @return
		 */
		public static int getSize(object[] data, Table t) 
		{
			int l = data.Length;
			int[] type = new int[l];

			for (int i = 0; i < l; i++) 
			{
				type[i] = t.getType(i);
			}

			return getSize(data, l, type);
		}

		/**
		 * Method declaration
		 *
		 *
		 * @param data
		 * @param l
		 * @param type
		 *
		 * @return
		 */
		unsafe private static int getSize(object[] data, int l, int[] type) 
		{
			int s = 0;

			for (int i = 0; i < l; i++) 
			{
				object o = data[i];

				s += 4;    // type

				if (o != null) 
				{
					switch (type[i]) 
					{

						case FLOAT:
						case REAL:
							s += sizeof(float);
							break;

						case DOUBLE:
							s += sizeof(double);
							break;

						case BIT:
							s += sizeof(bool);
							break;

						case TINYINT:
							s += sizeof(byte);
							break;

						case SMALLINT:
							s += sizeof(short);
							break;
	
						case INTEGER:
							s += sizeof(int);
							break;

						case BIGINT:
							s += sizeof(long);
							break;

						case BINARY:
						case VARBINARY:
						case LONGVARBINARY:
						case OTHER:
							s += 4;
							s += ((ByteArray)o).byteValue().Length;

							break;

						default:
							s += o.ToString().Length;
							s += 1; 
					}
				}
			}

			return s;
		}

	}
}

⌨️ 快捷键说明

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