Ejemplo n.º 1
0
        private VirtuosoCommand CollectInfo()
        {
            if (adapter == null)
            {
                throw new InvalidOperationException("The DataAdapter property is not set.");
            }

            VirtuosoCommand selectCommand = (VirtuosoCommand)adapter.SelectCommand;

            if (selectCommand == null)
            {
                throw new InvalidOperationException("The SelectCommand property is not set.");
            }

            VirtuosoConnection connection = (VirtuosoConnection)selectCommand.Connection;

            if (connection == null)
            {
                throw new InvalidOperationException("The Connection property is not set.");
            }

            if (columns != null)
            {
                return(selectCommand);
            }

            bool close = false;

            if (connection.State == ConnectionState.Closed)
            {
                connection.Open();
                close = true;
            }

            VirtuosoDataReader reader = null;

            try
            {
                reader    = (VirtuosoDataReader)selectCommand.ExecuteReader(CommandBehavior.KeyInfo | CommandBehavior.SchemaOnly);
                tableName = GetTableName(reader.Columns);
                columns   = reader.Columns;
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                if (close)
                {
                    connection.Close();
                }
            }

            return(selectCommand);
        }
Ejemplo n.º 2
0
        public void MultipleResultSets(TestCaseResult result)
        {
            DropProcedure();
            ExecuteNonQuery(
                "create procedure bar ()\n" +
                "{\n" +
                "  declare i int;\n" +
                "  declare c char;\n" +
                "  result_names (i);\n" +
                "  result (1);\n" +
                "  result (2);\n" +
                "  end_result ();\n" +
                "  result_names (c);\n" +
                "  result ('a');\n" +
                "  result ('b');\n" +
                "  return 0;\n" +
                "}\n"
                );

            VirtuosoCommand command = connection.CreateCommand();

            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "bar";

            VirtuosoDataReader reader = null;

            try
            {
                reader = command.ExecuteReader();
                result.FailIfNotEqual(1, reader.FieldCount);
                result.FailIfNotEqual("i", reader.GetName(0).ToLower());
                result.FailIfNotEqual(typeof(int), reader.GetFieldType(0));
                result.FailIfNotEqual(true, reader.Read());
                result.FailIfNotEqual(1, reader["i"]);
                result.FailIfNotEqual(true, reader.Read());
                result.FailIfNotEqual(2, reader["i"]);
                result.FailIfNotEqual(false, reader.Read());
                result.FailIfNotEqual(true, reader.NextResult());
                result.FailIfNotEqual(1, reader.FieldCount);
                result.FailIfNotEqual("c", reader.GetName(0).ToLower());
                result.FailIfNotEqual(typeof(string), reader.GetFieldType(0));
                result.FailIfNotEqual(true, reader.Read());
                result.FailIfNotEqual("a", reader["c"]);
                result.FailIfNotEqual(true, reader.Read());
                result.FailIfNotEqual("b", reader["c"]);
                result.FailIfNotEqual(false, reader.NextResult());
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                command.Dispose();
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Cleans up after garbage collected data reader.
        /// Does nothing if the data reader is still alive.
        /// </summary>
        internal void CloseFinalizedDataReader()
        {
            Debug.Assert(isFetching);
            Debug.Assert(dataReaderWeakRef != null);
            VirtuosoDataReader reader = (VirtuosoDataReader)dataReaderWeakRef.Target;

            if (reader == null)
            {
                CloseCursor(true, null);
                dataReaderWeakRef = null;
                isFetching        = false;
            }
        }
Ejemplo n.º 4
0
        internal void CloseDataReader()
        {
            Debug.Assert(dataReaderWeakRef != null);
            VirtuosoDataReader reader = (VirtuosoDataReader)dataReaderWeakRef.Target;

            if (!isCanceling)
            {
                Debug.Assert(isFetching);
                CloseCursor(true, reader);
            }

            dataReaderWeakRef = null;
            isFetching        = false;
        }
Ejemplo n.º 5
0
        public void TestGetString(TestCaseResult result)
        {
            InsertRowText();

            VirtuosoCommand cmd = connection.CreateCommand();

            cmd.CommandText = "select data from xmlt";

            VirtuosoDataReader rdr = cmd.ExecuteReader();

            rdr.Read();
            String x = rdr.GetString(0);

            FailIfXmlNotEqual(result, x, TheXml);
        }
Ejemplo n.º 6
0
        private void CloseCursor(bool keepResults, VirtuosoDataReader reader)
        {
            if (isExecuted)
            {
                if (keepResults && commandType == CommandType.StoredProcedure &&
                    innerCommand != null)
                {
                    innerCommand.GetParameters();
                }
            }
            if (innerCommand != null)
            {
                innerCommand.CloseCursor(isExecuted);
            }

            isExecuted = false;
        }
Ejemplo n.º 7
0
        public void TestGetValue(TestCaseResult result)
        {
            InsertRowText();

            VirtuosoCommand cmd = connection.CreateCommand();

            cmd.CommandText = "select data from xmlt";

            VirtuosoDataReader rdr = cmd.ExecuteReader();

            rdr.Read();
            object obj = rdr.GetValue(0);

            result.FailIfNotEqual(typeof(SqlXml).Name, obj.GetType().Name);
            SqlXml x = (SqlXml)obj;

            FailIfXmlNotEqual(result, x.ToString(), TheXml);
        }
Ejemplo n.º 8
0
        public void TestGetSqlXmlReader(TestCaseResult result)
        {
            InsertRowText();

            VirtuosoCommand cmd = connection.CreateCommand();

            cmd.CommandText = "select data from xmlt";

            VirtuosoDataReader rdr = cmd.ExecuteReader();

            rdr.Read();
            SqlXml x = rdr.GetSqlXml(0);

            XmlDocument doc = new XmlDocument();

            doc.Load(x.CreateReader());

            FailIfXmlNotEqual(result, doc.OuterXml, TheXml);
        }
Ejemplo n.º 9
0
        public void TestInsertSqlXml(TestCaseResult result)
        {
            VirtuosoCommand insert = connection.CreateCommand();

            insert.CommandText = "insert into xmlt (id, data) values (1, ?)";
            insert.Parameters.Add(new VirtuosoParameter(":0", new SqlXml(TheXml)));
            insert.ExecuteNonQuery();
            insert.Dispose();

            VirtuosoCommand cmd = connection.CreateCommand();

            cmd.CommandText = "select data from xmlt";

            VirtuosoDataReader rdr = cmd.ExecuteReader();

            rdr.Read();
            SqlXml x = rdr.GetSqlXml(0);

            FailIfXmlNotEqual(result, x.ToString(), TheXml);
        }
Ejemplo n.º 10
0
        private void DoGetDataTest(TestCaseResult result, string text, int column,
                                   CommandBehavior behavior, Selector selector, Sequence sequence)
        {
            VirtuosoCommand    cmd = null;
            VirtuosoDataReader dr  = null;

            try
            {
                cmd = new VirtuosoCommand(text, connection);
                dr  = cmd.ExecuteReader(behavior);
                CheckGetData(result, dr, column, selector, sequence);
            }
            finally
            {
                if (dr != null)
                {
                    dr.Close();
                }
                if (cmd != null)
                {
                    cmd.Dispose();
                }
            }
        }
Ejemplo n.º 11
0
        public VirtuosoDataReader ExecuteReader(CommandBehavior behavior)
#endif
        {
            /*
             * ExecuteReader should retrieve results from the data source
             * and return a DataReader that allows the user to process
             * the results.
             */
            Debug.WriteLineIf(CLI.FnTrace.Enabled, "VirtuosoCommand.ExecuteReader()");

            // There must be a valid and open connection.
            if (connection == null || connection.State != ConnectionState.Open)
            {
                throw new InvalidOperationException("Connection must be valid and open");
            }

            VirtuosoConnection.permission.Demand();

            CheckState();

            if (innerCommand == null)
            {
                innerCommand = connection.innerConnection.CreateInnerCommand(this);
            }
            innerCommand.SetTimeout(timeout);
            innerCommand.SetCommandBehavior(behavior);

            bool   schemaOnly = SchemaOnlyDataReader(behavior);
            string text       = GetCommandText();
            bool   isSparql   = text.TrimStart(null).StartsWith("sparql", StringComparison.OrdinalIgnoreCase);

            if (schemaOnly)
            {
                if (!isPrepared)
                {
                    if (!isSparql)
                    {
                        text = replaceNamedParams(text);
                    }
                    innerCommand.Prepare(text);
                }
            }
            else
            {
                if (!isSparql && parameters != null && parameters.Count > 0)
                {
                    VirtuosoParameterCollection _parameters = handleNamedParams(text, parameters);
                    Debug.Assert(_parameters.Count == parameters.Count, "Count mismatch in reordered parameter array");
                    text = replaceNamedParams(text);
                }
                innerCommand.SetParameters(parameters);
                if (!isPrepared)
                {
                    try
                    {
                        isExecuting = true;
                        innerCommand.Execute(text);
                        if (executeSecondaryStmt)
                        {
                            innerCommand.CloseCursor(false);
                            innerCommand.Execute(secondaryStmt);
                        }
                    }
                    finally
                    {
                        isExecuting = false;
                    }
                }
                else
                {
                    try
                    {
                        isExecuting = true;
                        innerCommand.Execute();
                        if (executeSecondaryStmt)
                        {
                            innerCommand.CloseCursor(false);
                            innerCommand.Execute(secondaryStmt);
                        }
                    }
                    finally
                    {
                        isExecuting = false;
                    }
                }
                isExecuted = true;
            }

            VirtuosoDataReader reader = new VirtuosoDataReader(connection, innerCommand, this, behavior, schemaOnly);

            dataReaderWeakRef = new WeakReference(reader);
            isFetching        = true;
            return(reader);
        }
Ejemplo n.º 12
0
        public void ResultSetAndOutputParameters(TestCaseResult result)
        {
            DropProcedure();
            DropProcedure();
            ExecuteNonQuery(
                "create procedure bar (out x integer)\n" +
                "{\n" +
                "  declare i int;\n" +
                "  result_names (i);\n" +
                "  result (1);\n" +
                "  result (2);\n" +
                "  x := 3;\n" +
                "  return 4;\n" +
                "}\n"
                );

            VirtuosoCommand command = connection.CreateCommand();

            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "bar";

            VirtuosoParameter returnValue = command.CreateParameter();

            returnValue.ParameterName = "ReturnValue";
            returnValue.Direction     = ParameterDirection.ReturnValue;
            returnValue.VirtDbType    = VirtDbType.Integer;
            command.Parameters.Add(returnValue);

            VirtuosoParameter x = command.CreateParameter();

            x.ParameterName = "x";
            x.Direction     = ParameterDirection.Output;
            x.VirtDbType    = VirtDbType.Integer;
            command.Parameters.Add(x);

            VirtuosoDataReader reader = null;
            bool closed = false;

            try
            {
                reader = command.ExecuteReader();
                result.FailIfNotEqual(1, reader.FieldCount);
                result.FailIfNotEqual("i", reader.GetName(0).ToLower());
                result.FailIfNotEqual(typeof(int), reader.GetFieldType(0));
                result.FailIfNotEqual(true, reader.Read());
                result.FailIfNotEqual(1, reader["i"]);
                result.FailIfNotEqual(true, reader.Read());
                result.FailIfNotEqual(2, reader["i"]);
                result.FailIfNotEqual(false, reader.Read());

                reader.Close();
                closed = true;

                result.FailIfNotEqual("Out Parameter", 3, x.Value);
                result.FailIfNotEqual("Return Value", 4, returnValue.Value);
            }
            finally
            {
                if (reader != null && !closed)
                {
                    reader.Close();
                }
                command.Dispose();
            }
        }
Ejemplo n.º 13
0
        public static void DeriveParameters(VirtuosoCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            if (command.CommandType != CommandType.StoredProcedure)
            {
                throw new InvalidOperationException("DeriveParameters supports only stored procedures.");
            }

            VirtuosoConnection connection = (VirtuosoConnection)command.Connection;

            if (connection == null)
            {
                throw new InvalidOperationException("The Connection property is not set.");
            }
            if (connection.State == ConnectionState.Closed)
            {
                throw new InvalidOperationException("The connection is closed.");
            }

            IInnerCommand      innerCommand = null;
            VirtuosoDataReader reader       = null;

            try
            {
                innerCommand = connection.innerConnection.CreateInnerCommand(null);
                innerCommand.GetProcedureColumns(command.CommandText);
                reader = new VirtuosoDataReader(connection, innerCommand, null, CommandBehavior.Default, false);

                command.Parameters.Clear();
                while (reader.Read())
                {
                    CLI.InOutType iotype = (CLI.InOutType)reader.GetInt16(reader.GetOrdinal("COLUMN_TYPE"));

                    ParameterDirection direction;
                    switch (iotype)
                    {
                    case CLI.InOutType.SQL_PARAM_INPUT:
                        direction = ParameterDirection.Input;
                        break;

                    case CLI.InOutType.SQL_PARAM_OUTPUT:
                        direction = ParameterDirection.Output;
                        break;

                    case CLI.InOutType.SQL_PARAM_INPUT_OUTPUT:
                        direction = ParameterDirection.InputOutput;
                        break;

                    case CLI.InOutType.SQL_PARAM_RETURN_VALUE:
                        direction = ParameterDirection.ReturnValue;
                        break;

                    default:
#if MONO
                        direction = ParameterDirection.Input;
#endif
                        continue;
                    }

                    string name = reader.GetString(reader.GetOrdinal("COLUMN_NAME"));
                    if (name == "" && iotype == CLI.InOutType.SQL_PARAM_RETURN_VALUE)
                    {
                        name = "ReturnValue";
                    }

                    CLI.SqlType sqlType = (CLI.SqlType)reader.GetInt16(reader.GetOrdinal("DATA_TYPE"));
                    DataType    type    = DataTypeInfo.MapSqlType(sqlType);
                    if (type == null)
                    {
                        throw new SystemException("Unknown data type");
                    }

                    int sizeOrdinal = reader.GetOrdinal("COLUMN_SIZE");
                    if (sizeOrdinal < 0)
                    {
                        sizeOrdinal = reader.GetOrdinal("PRECISION");
                    }
                    int size = reader.IsDBNull(sizeOrdinal) ? 0 : reader.GetInt32(sizeOrdinal);

                    int scaleOrdinal = reader.GetOrdinal("DECIMAL_DIGITS");
                    if (scaleOrdinal < 0)
                    {
                        scaleOrdinal = reader.GetOrdinal("SCALE");
                    }
                    short scale = reader.IsDBNull(scaleOrdinal) ? (short)0 : reader.GetInt16(scaleOrdinal);

                    int          nullableOrdinal = reader.GetOrdinal("NULLABLE");
                    CLI.Nullable nullable        = (CLI.Nullable)reader.GetInt16(nullableOrdinal);
                    bool         isNullable      = (nullable != CLI.Nullable.SQL_NO_NULLS);

                    VirtuosoParameter parameter = (VirtuosoParameter)command.CreateParameter();
                    parameter.ParameterName = name;
                    parameter.Direction     = direction;
                    parameter.VirtDbType    = type.vdbType;
                    parameter.Size          = type.GetFieldSize(size);
                    parameter.Precision     = type.GetPrecision(size);
                    parameter.Scale         = (byte)scale;
                    parameter.IsNullable    = isNullable;
                    command.Parameters.Add(parameter);
                }
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                if (innerCommand != null)
                {
                    innerCommand.Dispose();
                }
            }
        }
		private void CloseCursor (bool keepResults, VirtuosoDataReader reader)
		{
			if (isExecuted)
            {
			  if (keepResults && commandType == CommandType.StoredProcedure &&
innerCommand != null)
				  innerCommand.GetParameters ();
            }
            if (innerCommand != null)
			  innerCommand.CloseCursor (isExecuted);

			isExecuted = false;
		}
        public VirtuosoDataReader ExecuteReader (CommandBehavior behavior)
#endif
		{
			/*
			 * ExecuteReader should retrieve results from the data source
			 * and return a DataReader that allows the user to process 
			 * the results.
			 */
			Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoCommand.ExecuteReader()");

			// There must be a valid and open connection.
			if (connection == null || connection.State != ConnectionState.Open)
				throw new InvalidOperationException ("Connection must be valid and open");

			VirtuosoConnection.permission.Demand ();

			CheckState ();

			if (innerCommand == null)
				innerCommand = connection.innerConnection.CreateInnerCommand (this);
			innerCommand.SetTimeout (timeout);
			innerCommand.SetCommandBehavior (behavior);

			bool schemaOnly = SchemaOnlyDataReader (behavior);
			string text = GetCommandText ();
		        bool isSparql = text.TrimStart(null).StartsWith("sparql", StringComparison.OrdinalIgnoreCase);
			if (schemaOnly)
			{
				if (!isPrepared)
				{
                    			if (!isSparql)
						text = replaceNamedParams (text);
					innerCommand.Prepare (text);
				}
			}
			else
			{
				if (!isSparql && parameters != null && parameters.Count > 0)
				{
					VirtuosoParameterCollection _parameters = handleNamedParams (text, parameters);
					Debug.Assert (_parameters.Count == parameters.Count, "Count mismatch in reordered parameter array");
					text = replaceNamedParams(text);
				}
				innerCommand.SetParameters (parameters);
				if (!isPrepared)
				{
					try
					{
						isExecuting = true;
						innerCommand.Execute (text);
                                                if (executeSecondaryStmt)
                                                  {
                                                    innerCommand.CloseCursor(false);
						    innerCommand.Execute (secondaryStmt);
                                                  }
					}
					finally
					{
						isExecuting = false;
					}
				}
				else
				{
					try
					{
						isExecuting = true;
						innerCommand.Execute ();
                                                if (executeSecondaryStmt)
                                                  {
                                                    innerCommand.CloseCursor(false);
						    innerCommand.Execute (secondaryStmt);
                                                  }
					}
					finally
					{
						isExecuting = false;
					}
				}
				isExecuted = true;
			}

			VirtuosoDataReader reader = new VirtuosoDataReader (connection, innerCommand, this, behavior, schemaOnly);
			dataReaderWeakRef = new WeakReference (reader);
			isFetching = true;
			return reader;
		}
Ejemplo n.º 16
0
        private void CheckGetData(TestCaseResult result,
                                  VirtuosoDataReader dr, int column, Selector selector, Sequence sequence)
        {
            string name        = dr.GetName(column);
            int    tableColumn = checkTable.Columns.IndexOf(name);

            for (int row = 0; dr.Read(); row++)
            {
                //if (dr.IsDBNull (column))
                if (row == 0)
                {
                    continue;
                }

                long length;
                if (selector == Selector.GetBytes)
                {
                    length = dr.GetBytes(column, 0, null, 0, 0);
                }
                else                 //if (selector == Selector.GetChars)
                {
                    length = dr.GetChars(column, 0, null, 0, 0);
                }

                //Console.WriteLine ("row: {0}", row);
                //Console.WriteLine ("length: {0}", length);

                CompareSize(result, row, tableColumn, selector, length, 0);

                long   offset = 0;
                byte[] bytes  = new byte[BufferSize];
                char[] chars  = new char[BufferSize];
                int    count  = 0;
                while (offset < length)
                {
                    //Console.WriteLine ("offset: {0}", offset);

                    long nextLength;
                    if (selector == Selector.GetBytes)
                    {
                        for (int i = 0; i < bytes.Length; i++)
                        {
                            bytes[i] = 0;
                        }
                        nextLength = dr.GetBytes(column, offset, bytes, 0, bytes.Length);
                    }
                    else                     //if (selector == Selector.GetChars)
                    {
                        for (int i = 0; i < chars.Length; i++)
                        {
                            chars[i] = (char)0;
                        }
                        nextLength = dr.GetChars(column, offset, chars, 0, chars.Length);
                    }

                    result.FailIfEqual(this, 0, nextLength);
                    if (offset + nextLength < length)
                    {
                        result.FailIfNotEqual(this, (long)BufferSize, nextLength);
                    }
                    else
                    {
                        result.FailIfNotEqual(this, (long)(length - offset), nextLength);
                    }

                    if (selector == Selector.GetBytes)
                    {
                        CompareData(result, row, tableColumn, bytes, nextLength, offset);
                    }
                    else                     //if (selector == Selector.GetChars)
                    {
                        CompareData(result, row, tableColumn, chars, nextLength, offset);
                    }

                    if (sequence == Sequence.GetAll)
                    {
                        offset += nextLength;
                    }
                    else if (sequence == Sequence.GetHalf)
                    {
                        offset += 2 * nextLength;
                    }
                    else                     //if (sequence == Sequence.GetTwice)
                    {
                        count++;
                        if (count == 2)
                        {
                            count   = 0;
                            offset += 2 * nextLength;
                        }
                    }
                }
            }
        }