Example #1
0
 void Statement::ExecuteDirect(const std::string &query)
 {
   CloseCursor();
   if (SQLExecDirect(StmtHldr->GetHandle(), const_cast<SQLCHAR *>(reinterpret_cast<const SQLCHAR *>(query.c_str())), SQL_NTS) != SQL_SUCCESS)
     throw StatementException(GetLastDBError(SQL_HANDLE_STMT, StmtHldr->GetHandle()), seErrorExecute);
   IsOpen = true;
 }
Example #2
0
 void Statement::Execute()
 {
   CloseCursor();
   SQLRETURN RetCode = SQLExecute(StmtHldr->GetHandle());
   if (RetCode != SQL_SUCCESS)
     throw StatementException(GetLastDBError(SQL_HANDLE_STMT, StmtHldr->GetHandle()), seErrorExecute);
   IsOpen = true;
 }
Example #3
0
 Statement::~Statement()
 {
   try
   {
     CloseCursor();
   }
   catch (std::exception &)
   {
   }
   delete StmtHldr;
 }
Example #4
0
//-------------------------------------------------------
void
OdbcPersistor::Insert(const Safir::Dob::Typesystem::EntityId& entityId)
{
    int retries = 0;
    bool errorReported = false;
    m_debug << "Inserting " << entityId <<std::endl;
    bool paramSet = false;
    bool done = false;

    while (!done)
    {
        try
        {
            ConnectIfNeeded(m_odbcConnection, m_isOdbcConnected, retries);

            if (!m_insertIsValid)
            {
                m_helper.AllocStatement(&m_rowExistsStatement, m_odbcConnection);

                m_helper.AllocStatement(&m_insertStatement, m_odbcConnection);
                m_insertIsValid = true;

                m_helper.Prepare
                    (m_rowExistsStatement,
                     "SELECT count(*) as antal from PersistentEntity where typeId=? AND instance=? ");

                m_helper.Prepare
                    (m_insertStatement,
                     "INSERT INTO PersistentEntity (typeid, instance, typename) "
                     "values (?, ?, ?); ");

                m_helper.BindParamInt64(m_rowExistsStatement, 1, &m_type);
                m_helper.BindParamInt64(m_rowExistsStatement, 2, &m_instance);

                m_helper.BindColumnInt64(m_rowExistsStatement,1, &m_rowCount);

                m_helper.BindParamInt64(m_insertStatement, 1, &m_type);
                m_helper.BindParamInt64(m_insertStatement, 2, &m_instance);

                if (Safir::Dob::PersistenceParameters::TextColumnsAreUtf8())
                {
                    m_helper.BindParamString(m_insertStatement,
                                             3,
                                             Safir::Dob::PersistenceParameters::TypeNameColumnSize(),
                                             m_typename.get(),
                                             &m_currentTypenameSize);
                }
                else
                {
                    m_helper.BindParamStringW(m_insertStatement,
                                              3,
                                              Safir::Dob::PersistenceParameters::TypeNameColumnSize() / sizeof(wchar_t),
                                              m_typenameW.get(),
                                              &m_currentTypenameSize);
                }

                SetStmtTimeout(m_insertStatement);
                SetStmtTimeout(m_rowExistsStatement);

                paramSet = false;
            }

            if (!paramSet)
            {
                m_type = entityId.GetTypeId();
                m_instance = entityId.GetInstanceId().GetRawValue();

                const std::wstring typeName = Safir::Dob::Typesystem::Operations::GetName(m_type);
                if (Safir::Dob::PersistenceParameters::TextColumnsAreUtf8())
                {
                    const std::string typeNameUtf8 = Safir::Dob::Typesystem::Utilities::ToUtf8(typeName);

                    const size_t size = (typeNameUtf8.size() + 1)* sizeof (char);
                    if (size > static_cast<size_t>(Safir::Dob::PersistenceParameters::TypeNameColumnSize()))
                    {
                        throw Safir::Dob::Typesystem::SoftwareViolationException
                            (L"The size in bytes of '" + typeName +
                             L"' exceeds Safir.Dob.PersistenceParameters.TypeNameColumnSize",
                             __WFILE__, __LINE__);
                    }
                    memcpy(m_typename.get(), typeNameUtf8.c_str(), size);
                }
                else
                {
                    const size_t size = (typeName.size() + 1)* sizeof (wchar_t);
                    if (size > static_cast<size_t>(Safir::Dob::PersistenceParameters::TypeNameColumnSize()))
                    {
                        throw Safir::Dob::Typesystem::SoftwareViolationException
                            (L"The size in bytes of '" + typeName +
                             L"' exceeds Safir.Dob.PersistenceParameters.TypeNameColumnSize",
                             __WFILE__, __LINE__);
                    }
                    memcpy(m_typenameW.get(), typeName.c_str(), size);
                }
                m_currentTypenameSize = SQL_NTS;

                paramSet = true;
            }

            m_helper.Execute(m_rowExistsStatement);

            bool bExecuteInsert = false;
            if (m_helper.Fetch(m_rowExistsStatement))
            {
                // Insert if no row exist
                if (m_rowCount <= 0)
                {
                    bExecuteInsert = true;
                }
            }

            CloseCursor(m_rowExistsStatement);

            if (bExecuteInsert)
            {
                m_helper.Execute(m_insertStatement);
            }

            done = true;
            if (errorReported)
            {
                Safir::Logging::SendSystemLog(Safir::Logging::Informational,
                                                L"Successfully connected to the database");
                errorReported = false;
                retries = 0;
            }

        }
        catch(const OdbcException& e)
        {
            const std::wstring err = Safir::Dob::Typesystem::Utilities::ToWstring(e.what());
            m_debug << "Caught a ReconnectException in Insert:\n" << err << std::endl;
            if (retries > REPORT_AFTER_RECONNECTS && !errorReported)
            {
                Safir::Logging::SendSystemLog(Safir::Logging::Error,
                                              L"Insert: Failed to connect to the database, will keep trying. Exception info: " +
                                              err);
                errorReported = true;
            }

            DisconnectOdbcConnection();

            boost::this_thread::sleep_for(RECONNECT_EXCEPTION_DELAY);
        }

    }
}