ResultSet_T SqlServerConnection_executeQuery(T C, const char *sql, va_list ap) {
	va_list ap_copy;
	const char *tail;
	SQLHSTMT hstmt;

	assert(C);
	StringBuffer_clear(C->sb);
	va_copy(ap_copy, ap);
	StringBuffer_vappend(C->sb, sql, ap_copy);
	va_end(ap_copy);

	C->lastError = SQLAllocStmt(C->db->hdbc,&hstmt);


	C->lastError = SQLPrepare(hstmt,
		StringBuffer_toString(C->sb), StringBuffer_length(C->sb)); 
	if(!SQLSERVERSUCCESS(C->lastError)) {
		getSqlErr(C,hstmt);
		return NULL;
	}
	C->lastError = SQLExecute(hstmt);
	if(SQLSERVERSUCCESS(C->lastError))
		return ResultSet_new(SqlServerResultSet_new(hstmt, C->maxRows, false), (Rop_T)&sqlserverrops);
	else {
		getSqlErr(C,hstmt);
	}
	return NULL;
}
Beispiel #2
0
void SqlServerPreparedStatement_setLLong(T P, int parameterIndex, long long int x) {
	TEST_INDEX
		P->params[i].type.llong = x;
		P->lastError = SQLBindParameter(P->stmt,
			parameterIndex,
			SQL_PARAM_INPUT,
			SQL_C_SBIGINT,
			SQL_BIGINT,
			0,
			0,
			&P->params[i].type.llong,
			0,
			NULL); 
		if (!SQLSERVERSUCCESS(P->lastError))
		{
			THROW(SQLException, "SQLBindParameter int error");
		}
		//SQL_BIGINT
		//
		//
    //    SqlServer3_reset(P->stmt);
    //    P->lastError = SqlServer3_bind_int64(P->stmt, parameterIndex, x);
    //    if (P->lastError == SqlServer_RANGE)
    //            THROW(SQLException, "Parameter index is out of range");
}
int SqlServerConnection_ping(T C) {
	assert(C);
	//executeSQL(C, "select 1;");
	//SQLCloseCursor(C->db->hstmt);
	return (SQLSERVERSUCCESS(C->lastError));

}
static SqlServer_T doConnect(URL_T url, char **error) {
	RETCODE retcode;
	SqlServer_T db = (SqlServer_T)malloc(sizeof(struct SqlServer_S));

	/*retcode   =   SQLAllocHandle   (SQL_HANDLE_ENV,   NULL,   &db->henv);      
	retcode   =   SQLSetEnvAttr(db->henv,   SQL_ATTR_ODBC_VERSION,      
		(SQLPOINTER)SQL_OV_ODBC3,      
		SQL_IS_INTEGER);    */  
	retcode = SQLAllocEnv(&db->henv);
	//2.Á¬½Ó¾ä±ú     
	retcode   =   SQLAllocConnect(db->henv,   &db->hdbc);
	//retcode   =   SQLAllocHandle(SQL_HANDLE_DBC,   db->henv,   &db->hdbc);      
	retcode   =   SQLConnect(db->hdbc,  
		URL_getParameter(url, "db"),   
		strlen( URL_getParameter(url, "db") ),   
		URL_getUser(url),   
		strlen(URL_getUser(url)),   
		URL_getPassword(url),   
		strlen(URL_getPassword(url)));  
	if (SQLSERVERSUCCESS(retcode))
	{
		return db;
	}
	else {
		SQLFreeHandle(SQL_HANDLE_DBC, db->hdbc);     
		SQLFreeHandle(SQL_HANDLE_ENV, db->henv);
		free(db);
		return NULL;
	}
}
Beispiel #5
0
ResultSet_T SqlServerPreparedStatement_executeQuery(T P) {
        assert(P);
		P->lastError = SQLExecute(P->stmt);
        if (SQLSERVERSUCCESS(P->lastError))
               return ResultSet_new(SqlServerResultSet_new(P->stmt, P->maxRows, true), (Rop_T)&sqlserverrops);
       

	
        return NULL;
}
Beispiel #6
0
void SqlServerPreparedStatement_execute(T P) {
	P->lastError = SQLExecute(P->stmt);
	if (!SQLSERVERSUCCESS(P->lastError))
	{
#if _DEBUG
		SqlServerConnection_getLastError(P->stmt);
#endif
		THROW(SQLException, "SqlServerPreparedStatement_execute error");
	}
}
int SqlServerConnection_execute(T C, const char *sql, va_list ap) {
	va_list ap_copy;
	assert(C);
	StringBuffer_clear(C->sb);
	va_copy(ap_copy, ap);
	StringBuffer_vappend(C->sb, sql, ap_copy);
	va_end(ap_copy);
	executeSQL(C, StringBuffer_toString(C->sb));
	return (SQLSERVERSUCCESS(C->lastError));

}
Beispiel #8
0
void SqlServerPreparedStatement_setBlob(T P, int parameterIndex, const void *x, int size) {
        assert(P);
		P->lastError = SQLBindParameter(P->stmt,
			parameterIndex,
			SQL_PARAM_INPUT,
			SQL_C_FLOAT,
			SQL_REAL,
			size,
			0,
			x,
			0,
			NULL); 
		if (!SQLSERVERSUCCESS(P->lastError))
		{
			THROW(SQLException, "SQLBindParameter Blob error");
		}
}
Beispiel #9
0
void SqlServerPreparedStatement_setDouble(T P, int parameterIndex, double x) {
	TEST_INDEX
		P->params[i].type.real = x;
		P->lastError = SQLBindParameter(P->stmt,
			parameterIndex,
			SQL_PARAM_INPUT,
			SQL_C_DOUBLE,
			SQL_DOUBLE,
			0,
			0,
			&P->params[i].type.real,
			0,
			NULL); 
		if (!SQLSERVERSUCCESS(P->lastError))
		{
			THROW(SQLException, "SQLBindParameter double error");
		}
}
Beispiel #10
0
void SqlServerPreparedStatement_setInt(T P, int parameterIndex, int x) {
        TEST_INDEX
		P->params[i].type.integer = x;
		P->lastError = SQLBindParameter(P->stmt,
			parameterIndex,
			SQL_PARAM_INPUT,
			SQL_C_LONG,
			SQL_INTEGER,
			0,
			0,
			&P->params[i].type.integer,
			0,
			NULL); 
		if (!SQLSERVERSUCCESS(P->lastError))
		{
			THROW(SQLException, "SQLBindParameter int error");
		}
}
Beispiel #11
0
void SqlServerPreparedStatement_setString(T P, int parameterIndex, const char *x) {
        int size = 0;
		assert(P);
         size = x ? (int)strlen(x) : 0; 
		P->lastError = SQLBindParameter(P->stmt,
			 parameterIndex,
			 SQL_PARAM_INPUT,
			 SQL_C_CHAR,
			 SQL_CHAR,
			 size,
			 0,
			 x,
			 0,
			 NULL); 
		 if (!SQLSERVERSUCCESS(P->lastError))
		 {
			 THROW(SQLException, "SQLBindParameter error");
		 }
		 

}
PreparedStatement_T SqlServerConnection_prepareStatement(T C, const char *sql, va_list ap) {
	va_list ap_copy;
	const char *tail;
	HSTMT hstmt;

	assert(C);
	StringBuffer_clear(C->sb);
	va_copy(ap_copy, ap);
	StringBuffer_vappend(C->sb, sql, ap_copy);
	va_end(ap_copy);

	C->lastError = SQLAllocStmt(C->db->hdbc,&hstmt);
	C->lastError = SQLPrepare(hstmt,StringBuffer_toString(C->sb),strlen(StringBuffer_toString(C->sb))); 
	//The third argument with an array of the same size , but not the same database column  

    if (SQLSERVERSUCCESS(C->lastError)) {
        int paramCount = 0;
        return PreparedStatement_new(SqlServerPreparedStatement_new(C->db, hstmt, C->maxRows), (Pop_T)&sqlserverpops, paramCount);
    }
	else {
		getSqlErr(C,hstmt);
	}
	return NULL;
}
long long int SqlServerConnection_rowsChanged(T C) {
	assert(C);
	return (SQLSERVERSUCCESS(C->lastError));
	// return (long long int)SqlServer3_changes(C->db);
}
long long int SqlServerConnection_lastRowId(T C) {
	assert(C);
	executeSQL(C, "select scope_identity()");
	return (SQLSERVERSUCCESS(C->lastError));
	//return SqlServer3_last_insert_rowid(C->db);
}
int SqlServerConnection_rollback(T C) {
	assert(C);
	executeSQL(C, "ROLLBACK TRANSACTION;");
	return (SQLSERVERSUCCESS(C->lastError));

}
int SqlServerConnection_commit(T C) {
	assert(C);
	executeSQL(C, "COMMIT TRANSACTION;");
	return (SQLSERVERSUCCESS(C->lastError));

}
int SqlServerConnection_beginTransaction(T C) {
	assert(C);
	executeSQL(C, "BEGIN TRANSACTION;");
	return (SQLSERVERSUCCESS(C->lastError));

}
T SqlServerResultSet_new(void *stmt, int maxRows, int keep) {
	T R;
	WORD wclos = 0;
	RETCODE retcode = 0;
	int i = 0;
	assert(stmt);
	NEW(R);
	R->stmt = stmt;
	R->keep = keep;
	R->maxRows = maxRows;
	retcode = SQLNumResultCols(R->stmt,&wclos);
	R->columnCount = wclos;
	if (!SQLSERVERSUCCESS(retcode))
	{
		SqlServerConnection_getLastError(stmt);
		SqlServerResultSet_free(&R);
		return NULL;
	}
	R->columns = CALLOC(R->columnCount, sizeof (struct odbc_column_t));

	for(i = 1; i <= R->columnCount; i++) {
		SQLSMALLINT ftype;
		SQLSMALLINT ty;
		retcode = SQLDescribeCol(
			stmt,
			i, 
			R->columns[i-1].field.name, 
			sizeof(R->columns[i-1].field.name),
			&R->columns[i-1].field.name_length,
			&ty,
			&R->columns[i-1].field.max_length,
			&R->columns[i-1].field.decimals,
			&R->columns[i-1].field.cannull); 
		if (!SQLSERVERSUCCESS(retcode)) {
			SqlServerConnection_getLastError(stmt);
			SqlServerResultSet_free(&R);
			return NULL;
		}
		R->columns[i-1].field.type = ty;
		switch (R->columns[i-1].field.type)
		{
		case SQL_CHAR:
		case SQL_VARCHAR:
		case SQL_DECIMAL:
		case SQL_NUMERIC:
			ftype = SQL_C_CHAR;
			break;
		case SQL_INTEGER:
			ftype =SQL_C_LONG;
			break;
		case SQL_SMALLINT:
			ftype =SQL_C_SHORT;
			break;
		case SQL_REAL:
			ftype =SQL_C_FLOAT;
			break;
		case SQL_FLOAT:
		case SQL_DOUBLE:
			ftype = SQL_C_DOUBLE;
			break;
		case SQL_TYPE_BIGINT:
			ftype = SQL_C_SBIGINT;
			break;
		default:
			//SqlServerConnection_getLastError(stmt);
			//SqlServerResultSet_free(&R);
			//THROW(SQLException, "Column type unknow ");
			//return NULL;
			break;
		}
		ftype = SQL_C_CHAR;

		R->columns[i-1].buffer = ALLOC(STRLEN + 1);
		retcode = SQLBindCol(stmt,
			i,
			ftype,
			R->columns[i-1].buffer,
			STRLEN,
			&R->columns[i-1].real_length);
		if (!SQLSERVERSUCCESS(retcode)) {
			SqlServerConnection_getLastError(stmt);
			SqlServerResultSet_free(&R);
			return NULL;
		}
		
	}


	return R;
}