public override void Open()
        {
            if (_connectionString == null || _connectionString.Length == 0)
            {
                throw ExceptionHelper.ConnectionStringNotInitialized();
            }

            IsConnecting = true;
            try
            {
                if (JdbcConnection != null && !JdbcConnection.isClosed())
                {
                    throw ExceptionHelper.ConnectionAlreadyOpen(_internalState);
                }

                JdbcConnection = ConnectionProvider.GetConnection(ConnectionStringBuilder);

                IsOpened = true;

                OnStateChange(new StateChangeEventArgs(ConnectionState.Closed, ConnectionState.Open));
            }
            catch (SQLWarning warning)
            {
                OnSqlWarning(warning);
            }
            catch (SQLException exp)
            {
                OnSqlException(exp);
            }
            finally
            {
                IsConnecting = false;
            }
        }
Esempio n. 2
0
        public override void Close()
        {
            ConnectionState orig = State;

            try {
                ClearReferences();
                if (JdbcConnection != null && !JdbcConnection.isClosed())
                {
                    if (!JdbcConnection.getAutoCommit())
                    {
                        JdbcConnection.rollback();
                    }
                    JdbcConnection.close();
                }
            }
            catch (Exception e) {
                // suppress exception
#if DEBUG
                Console.WriteLine("Exception catched at Conection.Close() : {0}\n{1}\n{2}", e.GetType().FullName, e.Message, e.StackTrace);
#endif
            }
            finally {
                JdbcConnection = null;
                lock (_internalStateSync) {
                    _internalState = ConnectionState.Closed;
                }
            }

            ConnectionState current = State;
            if (current != orig)
            {
                OnStateChange(new StateChangeEventArgs(orig, current));
            }
        }
        internal void ValidateBeginTransaction()
        {
            if (State != ConnectionState.Open)
            {
                throw new InvalidOperationException(String.Format("{0} requires an open and available Connection. The connection's current state is {1}.", new object[] { "BeginTransaction", State }));
            }

            if (!JdbcConnection.getAutoCommit())
            {
                throw new System.InvalidOperationException("Parallel transactions are not supported.");
            }
        }
Esempio n. 4
0
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         try {
             if (JdbcConnection != null && !JdbcConnection.isClosed())
             {
                 JdbcConnection.close();
             }
             JdbcConnection = null;
         }
         catch (java.sql.SQLException exp) {
             throw CreateException(exp);
         }
     }
     base.Dispose(disposing);
 }
Esempio n. 5
0
        private static void Main(string[] args)
        {
            Console.Write("Driver Path : ");
            var driverPath = Console.ReadLine();

            Console.Write("Driver Class : ");
            var driverClass = Console.ReadLine();

            Console.Write("JDBC Url : ");
            var jdbcUrl = Console.ReadLine();

            var builder = new JdbcConnectionStringBuilder
            {
                FetchSize   = -1,
                DriverPath  = driverPath,
                DriverClass = driverClass,
                JdbcUrl     = jdbcUrl
            };

            using var connection = new JdbcConnection(builder);
            connection.Open();

            while (true)
            {
                Console.Write("SQL > ");

                var sql = Console.ReadLine();

                using var command = connection.CreateCommand(sql);

                try
                {
                    using var reader = command.ExecuteReader();
                    PrintResult(reader);
                }
                catch (DbException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
Esempio n. 6
0
        public DataTable GetOleDbSchemaTable(Guid schema, object[] restrictions)
        {
            if (State != ConnectionState.Open)
            {
                throw ExceptionHelper.ConnectionNotOpened("GetOleDbSchemaTable", State.ToString());
            }

            try
            {
                string[] fixedRestrictions = new string[4];
                if (restrictions != null)
                {
                    if (restrictions.Length > 4)
                    {
                        throw new OleDbException("The parameter is incorrect", null, this);
                    }

                    for (int i = 0, count = restrictions.Length; i < count; i++)
                    {
                        if (restrictions[i] != null)
                        {
                            if (!(restrictions[i] is string))
                            {
                                throw new OleDbException("The parameter is incorrect", null, this);
                            }

                            fixedRestrictions[i] = (string)restrictions[i];
                        }
                    }
                }

                DataTable schemaTable = new DataTable("Tables");
                schemaTable.Columns.Add("TABLE_CATALOG");
                schemaTable.Columns.Add("TABLE_SCHEMA");
                schemaTable.Columns.Add("TABLE_NAME");
                schemaTable.Columns.Add("TABLE_TYPE");
                schemaTable.Columns.Add("TABLE_GUID");
                schemaTable.Columns.Add("DESCRIPTION");
                schemaTable.Columns.Add("TABLE_PROPID");
                schemaTable.Columns.Add("DATE_CREATED");
                schemaTable.Columns.Add("DATE_MODIFIED");

                java.sql.ResultSet tableRes = JdbcConnection.getMetaData().getTables(
                    fixedRestrictions[0],
                    fixedRestrictions[1],
                    fixedRestrictions[2],
                    new string[] { fixedRestrictions[3] });

                try
                {
                    while (tableRes.next())
                    {
                        DataRow row = schemaTable.NewRow();
                        row["TABLE_CATALOG"] = tableRes.getString("TABLE_CAT");
                        row["TABLE_SCHEMA"]  = tableRes.getString("TABLE_SCHEM");
                        row["TABLE_NAME"]    = tableRes.getString("TABLE_NAME");
                        row["TABLE_TYPE"]    = tableRes.getString("TABLE_TYPE");
                        row["DESCRIPTION"]   = tableRes.getString("REMARKS");

                        schemaTable.Rows.Add(row);
                    }
                }
                finally
                {
                    tableRes.close();
                }

                return(schemaTable);
            }
            catch (SQLException e)
            {
                throw CreateException(e);
            }
        }
        internal ArrayList GetProcedureColumns(String procedureString, AbstractDbCommand command)
        {
            ArrayList col = new ArrayList();

            try
            {
                ObjectNameResolver[] nameResolvers = SyntaxPatterns;
                java.sql.ResultSet   res           = null;
                string catalog = null;
                string schema  = null;
                string spname  = null;

                java.sql.DatabaseMetaData metadata = JdbcConnection.getMetaData();
                bool storesUpperCaseIdentifiers    = false;
                bool storesLowerCaseIdentifiers    = false;
                try
                {
                    storesUpperCaseIdentifiers = metadata.storesUpperCaseIdentifiers();
                    storesLowerCaseIdentifiers = metadata.storesLowerCaseIdentifiers();
                }
                catch (SQLException e)
                {
                    // suppress
                }

                for (int i = 0; i < nameResolvers.Length; i++)
                {
                    ObjectNameResolver nameResolver = nameResolvers[i];
                    Match match = nameResolver.Match(procedureString);

                    if (match.Success)
                    {
                        spname  = ObjectNameResolver.GetName(match);
                        schema  = ObjectNameResolver.GetSchema(match);
                        catalog = ObjectNameResolver.GetCatalog(match);

                        // make all identifiers uppercase or lowercase according to database metadata
                        if (storesUpperCaseIdentifiers)
                        {
                            spname  = (spname.Length > 0) ? spname.ToUpper() : null;
                            schema  = (schema.Length > 0) ? schema.ToUpper() : null;
                            catalog = (catalog.Length > 0) ? catalog.ToUpper() : null;
                        }
                        else if (storesLowerCaseIdentifiers)
                        {
                            spname  = (spname.Length > 0) ? spname.ToLower() : null;
                            schema  = (schema.Length > 0) ? schema.ToLower() : null;
                            catalog = (catalog.Length > 0) ? catalog.ToLower() : null;
                        }
                        else
                        {
                            spname  = (spname.Length > 0) ? spname : null;
                            schema  = (schema.Length > 0) ? schema : null;
                            catalog = (catalog.Length > 0) ? catalog : null;
                        }

                        // catalog from db is always in correct caps
                        if (catalog == null)
                        {
                            catalog = JdbcConnection.getCatalog();
                        }

                        try
                        {
                            // always get the first procedure that db returns
                            res = metadata.getProcedures(catalog, schema, spname);
                            if (res.next())
                            {
                                catalog = res.getString(1);
                                schema  = res.getString(2);
                                spname  = res.getString(3);
                                break;
                            }

                            spname = null;
                        }
                        catch // suppress exception
                        {
                            return(null);
                        }
                        finally
                        {
                            if (res != null)
                            {
                                res.close();
                            }
                        }
                    }
                }

                if (spname == null || spname.Length == 0)
                {
                    return(null);
                }

                try
                {
                    // get procedure columns based o  procedure metadata
                    res = metadata.getProcedureColumns(catalog, schema, spname, null);
                    while (res.next())
                    {
                        // since there is still a possibility that some of the parameters to getProcedureColumn were nulls,
                        // we need to filter the results with strict matching
                        if ((res.getString(1) != catalog) || (res.getString(2) != schema) || (res.getString(3) != spname))
                        {
                            continue;
                        }

                        AbstractDbParameter parameter = (AbstractDbParameter)command.CreateParameter();

                        parameter.SetParameterName(res);
                        parameter.SetParameterDbType(res);
                        parameter.SetSpecialFeatures(res);

                        //get parameter direction
                        short direction = res.getShort("COLUMN_TYPE");
                        if (direction == 1) //DatabaseMetaData.procedureColumnIn
                        {
                            parameter.Direction = ParameterDirection.Input;
                        }
                        else if (direction == 2) //DatabaseMetaData.procedureColumnInOut
                        {
                            parameter.Direction = ParameterDirection.InputOutput;
                        }
                        else if (direction == 4) //DatabaseMetaData.procedureColumnOut
                        {
                            parameter.Direction = ParameterDirection.Output;
                        }
                        else if (direction == 5) //DatabaseMetaData.procedureColumnReturn
                        {
                            parameter.Direction = ParameterDirection.ReturnValue;
                        }

                        //get parameter precision and scale
                        parameter.SetParameterPrecisionAndScale(res);

                        parameter.SetParameterSize(res);
                        parameter.SetParameterIsNullable(res);

                        col.Add(parameter);
                    }
                }
                finally
                {
                    if (res != null)
                    {
                        res.close();
                    }
                }
            }
            catch (Exception e)
            {
                //supress
#if DEBUG
                Console.WriteLine("Exception catched at AbstractDBConnection.GetProcedureColumns() : {0}\n{1}\n{2}", e.GetType().FullName, e.Message, e.StackTrace);
#endif
            }
            return(col);
        }