Exemple #1
0
static SQLRETURN
MNDBDataSources(ODBCEnv *env,
		SQLUSMALLINT Direction,
		SQLCHAR *ServerName,
		SQLSMALLINT BufferLength1,
		SQLSMALLINT *NameLength1,
		SQLCHAR *Description,
		SQLSMALLINT BufferLength2,
		SQLSMALLINT *NameLength2)
{
	(void) Direction;
	(void) ServerName;
	(void) BufferLength1;
	(void) NameLength1;
	(void) Description;
	(void) BufferLength2;
	(void) NameLength2;

	if (env->sql_attr_odbc_version == 0) {
		/* Function sequence error */
		addEnvError(env, "HY010", NULL, 0);
		return SQL_ERROR;
	}

	/* TODO: implement the requested behavior */

	/* Driver does not support this function */
	addEnvError(env, "IM001", NULL, 0);
	return SQL_ERROR;
}
Exemple #2
0
static SQLRETURN
MNDBAllocDbc(ODBCEnv *env, SQLHANDLE *OutputHandlePtr)
{
	if (env->sql_attr_odbc_version == 0) {
		/* Function sequence error */
		addEnvError(env, "HY010", NULL, 0);
		return SQL_ERROR;
	}
	if (OutputHandlePtr == NULL) {
		/* Invalid use of null pointer */
		addEnvError(env, "HY009", NULL, 0);
		return SQL_ERROR;
	}
	*OutputHandlePtr = (SQLHANDLE *) newODBCDbc(env);
#ifdef ODBCDEBUG
	ODBCLOG("new dbc %p\n", *OutputHandlePtr);
#endif
	return *OutputHandlePtr == NULL ? SQL_ERROR : SQL_SUCCESS;
}
Exemple #3
0
static SQLRETURN
ODBCFreeEnv_(ODBCEnv *env)
{
	if (env->sql_attr_odbc_version == 0) {
		/* Function sequence error */
		addEnvError(env, "HY010", NULL, 0);
		return SQL_ERROR;
	}

	/* check if no associated connections are still active */
	if (env->FirstDbc != NULL) {
		/* Function sequence error */
		addEnvError(env, "HY010", NULL, 0);
		return SQL_ERROR;
	}

	/* Ready to destroy the env handle */
	destroyODBCEnv(env);
	return SQL_SUCCESS;
}
Exemple #4
0
SQLRETURN SQL_API
SQLDataSourcesW(SQLHENV EnvironmentHandle,
		SQLUSMALLINT Direction,
		SQLWCHAR *ServerName,
		SQLSMALLINT BufferLength1,
		SQLSMALLINT *NameLength1,
		SQLWCHAR *Description,
		SQLSMALLINT BufferLength2,
		SQLSMALLINT *NameLength2)
{
	ODBCEnv *env = (ODBCEnv *) EnvironmentHandle;
	SQLRETURN rc;
	SQLCHAR *server, *descr;
	SQLSMALLINT length1, length2;

#ifdef ODBCDEBUG
	ODBCLOG("SQLDataSourcesW " PTRFMT " %s\n",
		PTRFMTCAST EnvironmentHandle, translateDirection(Direction));
#endif

	if (!isValidEnv(env))
		return SQL_INVALID_HANDLE;

	clearEnvErrors(env);

	server = malloc(100);
	descr = malloc(100);
	if (server == NULL || descr == NULL) {
		/* Memory allocation error */
		addEnvError(env, "HY001", NULL, 0);
		if (server)
			free(server);
		if (descr)
			free(descr);
		return SQL_ERROR;
	}

	rc = MNDBDataSources(env, Direction,
			     server, 100, &length1,
			     descr, 100, &length2);

	if (SQL_SUCCEEDED(rc)) {
		fixWcharOut(rc, server, length1,
			    ServerName, BufferLength1, NameLength1,
			    1, addEnvError, env);
		fixWcharOut(rc, descr, length2,
			    Description, BufferLength2, NameLength2,
			    1, addEnvError, env);
	}
	free(server);
	free(descr);

	return rc;
}
Exemple #5
0
/*
 * Creates a new allocated ODBCDbc object and initializes it.
 *
 * Precondition: none
 * Postcondition: returns a new ODBCDbc object
 */
ODBCDbc *
newODBCDbc(ODBCEnv *env)
{
	ODBCDbc *dbc;

	assert(env);

	dbc = (ODBCDbc *) malloc(sizeof(ODBCDbc));
	if (dbc == NULL) {
		/* Memory allocation error */
		addEnvError(env, "HY001", NULL, 0);
		return NULL;
	}
	assert(dbc);

	dbc->Env = env;
	dbc->Error = NULL;
	dbc->RetrievedErrors = 0;

	dbc->dsn = NULL;
	dbc->uid = NULL;
	dbc->pwd = NULL;
	dbc->host = NULL;
	dbc->port = 0;
	dbc->dbname = NULL;

	dbc->Connected = false;
	dbc->has_comment = false;
	dbc->sql_attr_autocommit = SQL_AUTOCOMMIT_ON;	/* default is autocommit */
	dbc->sql_attr_metadata_id = SQL_FALSE;
	dbc->sql_attr_connection_timeout = 0;
	dbc->mid = NULL;
	dbc->major = 0;
	dbc->minor = 0;
	dbc->patch = 0;
	dbc->cachelimit = 0;
	dbc->qtimeout = 0;
	dbc->Mdebug = 0;

	dbc->FirstStmt = NULL;

	/* add this dbc to start of the administrative linked dbc list */
	dbc->next = env->FirstDbc;
	env->FirstDbc = dbc;

	dbc->Type = ODBC_DBC_MAGIC_NR;	/* set it valid */

	return dbc;
}
Exemple #6
0
SQLRETURN SQL_API
SQLDriversW(SQLHENV EnvironmentHandle,
	    SQLUSMALLINT Direction,
	    SQLWCHAR *DriverDescription,
	    SQLSMALLINT BufferLength1,
	    SQLSMALLINT *DescriptionLengthPtr,
	    SQLWCHAR *DriverAttributes,
	    SQLSMALLINT BufferLength2,
	    SQLSMALLINT *AttributesLengthPtr)
{
	(void) Direction;
	(void) DriverDescription;
	(void) BufferLength1;
	(void) DescriptionLengthPtr;
	(void) DriverAttributes;
	(void) BufferLength2;
	(void) AttributesLengthPtr;
	addEnvError((ODBCEnv *) EnvironmentHandle, "HY000",
		    "Driver Manager only function", 0);
	return SQL_ERROR;
}
Exemple #7
0
SQLRETURN
MNDBEndTran(SQLSMALLINT HandleType,
	    SQLHANDLE Handle,
	    SQLSMALLINT CompletionType)
{
	ODBCEnv *env = NULL;
	ODBCDbc *dbc = NULL;
	SQLHANDLE StatementHandle;
	RETCODE rc;

	/* check parameters HandleType and Handle for validity */
	switch (HandleType) {
	case SQL_HANDLE_DBC:
		dbc = (ODBCDbc *) Handle;
		if (!isValidDbc(dbc))
			return SQL_INVALID_HANDLE;
		clearDbcErrors(dbc);
		if (!dbc->Connected) {
			/* Connection does not exist */
			addDbcError(dbc, "08003", NULL, 0);
			return SQL_ERROR;
		}
		break;
	case SQL_HANDLE_ENV:
		env = (ODBCEnv *) Handle;
		if (!isValidEnv(env))
			return SQL_INVALID_HANDLE;
		clearEnvErrors(env);
		if (env->sql_attr_odbc_version == 0) {
			/* Function sequence error */
			addEnvError(env, "HY010", NULL, 0);
			return SQL_ERROR;
		}
		break;
	case SQL_HANDLE_STMT:
		if (isValidStmt((ODBCStmt *) Handle)) {
			clearStmtErrors((ODBCStmt *) Handle);
			/* Invalid attribute/option identifier */
			addStmtError((ODBCStmt *) Handle, "HY092", NULL, 0);
			return SQL_ERROR;
		}
		return SQL_INVALID_HANDLE;
	case SQL_HANDLE_DESC:
		if (isValidDesc((ODBCDesc *) Handle)) {
			clearDescErrors((ODBCDesc *) Handle);
			/* Invalid attribute/option identifier */
			addDescError((ODBCDesc *) Handle, "HY092", NULL, 0);
			return SQL_ERROR;
		}
		return SQL_INVALID_HANDLE;
	default:
		return SQL_INVALID_HANDLE;
	}

	/* check parameter CompletionType */
	if (CompletionType != SQL_COMMIT && CompletionType != SQL_ROLLBACK) {
		/* Invalid transaction operation code */
		if (HandleType == SQL_HANDLE_DBC)
			addDbcError(dbc, "HY012", NULL, 0);
		else
			addEnvError(env, "HY012", NULL, 0);
		return SQL_ERROR;
	}

	if (HandleType == SQL_HANDLE_ENV) {
		RETCODE rc1 = SQL_SUCCESS;

		for (dbc = env->FirstDbc; dbc; dbc = dbc->next) {
			assert(isValidDbc(dbc));
			if (!dbc->Connected)
				continue;
			rc = MNDBEndTran(SQL_HANDLE_DBC, dbc, CompletionType);
			if (rc == SQL_ERROR)
				rc1 = SQL_ERROR;
			else if (rc == SQL_SUCCESS_WITH_INFO &&
				 rc1 != SQL_ERROR)
				rc1 = rc;
		}
		return rc1;
	}

	assert(HandleType == SQL_HANDLE_DBC);

	if (dbc->sql_attr_autocommit == SQL_AUTOCOMMIT_ON) {
		/* nothing to do if in autocommit mode */
		return SQL_SUCCESS;
	}

	/* construct a statement object and excute a SQL COMMIT or ROLLBACK */
	rc = MNDBAllocStmt(dbc, &StatementHandle);
	if (SQL_SUCCEEDED(rc)) {
		ODBCStmt *stmt = (ODBCStmt *) StatementHandle;
		rc = MNDBExecDirect(stmt,
				    CompletionType == SQL_COMMIT ? (SQLCHAR *) "commit" : (SQLCHAR *) "rollback",
				    SQL_NTS);

		if (rc == SQL_ERROR || rc == SQL_SUCCESS_WITH_INFO) {
			/* get the error/warning and post in on the
			 * dbc handle */
			SQLCHAR sqlState[SQL_SQLSTATE_SIZE + 1];
			SQLINTEGER nativeErrCode;
			SQLCHAR msgText[SQL_MAX_MESSAGE_LENGTH + 1];

			(void) MNDBGetDiagRec(SQL_HANDLE_STMT, stmt, 1,
					      sqlState, &nativeErrCode,
					      msgText, sizeof(msgText), NULL);

			addDbcError(dbc, (char *) sqlState,
				    (char *) msgText + ODBCErrorMsgPrefixLength,
				    nativeErrCode);
		}
		/* clean up the statement handle */
		ODBCResetStmt(stmt);
		ODBCFreeStmt_(stmt);

		for (stmt = dbc->FirstStmt; stmt; stmt = stmt->next)
			ODBCResetStmt(stmt);
	} else {
		/* could not allocate a statement object */
		/* Memory management error */
		addDbcError(dbc, "HY013", NULL, 0);
		return SQL_ERROR;
	}

	return rc;
}