[freetds] ODBC Meta procs

Peter Deacon peterd at iea-software.com
Wed Apr 2 13:03:37 EST 2003


On Wed, 2 Apr 2003, Peter Deacon wrote:

This patches my previous meta.diff patch.  PARM_ADD would fail if you
specify more than one parameter.

Also updated SQLColumns to handle odbc v3 column name conversions.

Have Fun!
Peter
-------------- next part --------------
--- n:\freetds\src\odbc\odbc.c	2003-04-02 02:07:41.000000000 -0800
+++ odbc.c	2003-04-02 10:00:59.000000000 -0800
@@ -112,6 +112,8 @@
 #define PARM_ADD(p,parm,string,size,quoted) \
 if (size > 0) \
 	{\
+	if (*p != '\0')\
+		p++;\
 	strcpy(p,parm);\
 	p+=strlen(parm);\
 	if (quoted == TRUE)\
@@ -1988,55 +1990,56 @@
 	   SQLSMALLINT cbTableName, SQLCHAR FAR * szColumnName,	/* column_name */
 	   SQLSMALLINT cbColumnName)
 {
-	char szQuery[4096];
-	int nTableName = odbc_get_string_size(cbTableName, szTableName);
-	int nTableOwner = odbc_get_string_size(cbSchemaName, szSchemaName);
-	int nTableQualifier = odbc_get_string_size(cbCatalogName, szCatalogName);
-	int nColumnName = odbc_get_string_size(cbColumnName, szColumnName);
-	int bNeedComma = 0;
-
-	INIT_HSTMT;
-
-	sprintf(szQuery, "exec sp_columns ");
-
-	if (nTableName) {
-		strcat(szQuery, "@table_name = '");
-		strncat(szQuery, (const char *) szTableName, nTableName);
-		strcat(szQuery, "'");
-		bNeedComma = 1;
-	}
-
-	if (nTableOwner) {
-		if (bNeedComma)
-			strcat(szQuery, ", ");
-		strcat(szQuery, "@table_owner = '");
-		strncat(szQuery, (const char *) szSchemaName, nTableOwner);
-		strcat(szQuery, "'");
-		bNeedComma = 1;
-	}
-
-	if (nTableQualifier) {
-		if (bNeedComma)
-			strcat(szQuery, ", ");
-		strcat(szQuery, "@table_qualifier = '");
-		strncat(szQuery, (const char *) szCatalogName, nTableQualifier);
-		strcat(szQuery, "'");
-		bNeedComma = 1;
-	}
-
-	if (nColumnName) {
-		if (bNeedComma)
-			strcat(szQuery, ", ");
-		strcat(szQuery, "@column_name = '");
-		strncat(szQuery, (const char *) szColumnName, nColumnName);
-		strcat(szQuery, "'");
-		bNeedComma = 1;
-	}
-
-	if (SQL_SUCCESS != odbc_set_stmt_query(stmt, szQuery, strlen(szQuery)))
-		return SQL_ERROR;
-
-	return _SQLExecute(stmt);
+int retcode = SQL_SUCCESS;
+char *proc;
+char *p;
+INIT_HSTMT;
+
+cbCatalogName = odbc_get_string_size(cbCatalogName,szCatalogName);
+cbSchemaName = odbc_get_string_size(cbSchemaName,szSchemaName);
+cbTableName = odbc_get_string_size(cbTableName,szTableName);
+cbColumnName = odbc_get_string_size(cbColumnName,szColumnName);
+
+if ((proc = (char *)malloc(cbCatalogName + cbSchemaName + cbTableName + cbColumnName + 110)) != NULL)
+	{
+	strcpy(proc,"sp_columns "); /* 11 */
+	p = proc + 11;
+
+	PARM_ADD(p,"@table_name=",szTableName,cbTableName,TRUE);
+	PARM_ADD(p,"@table_owner=",szSchemaName,cbSchemaName,TRUE);
+	PARM_ADD(p,"@table_qualifier=",szCatalogName,cbCatalogName,TRUE);
+	PARM_ADD(p,"@column_name=",szColumnName,cbColumnName,TRUE);
+	PARM_END(p);
+
+	if (odbc_set_stmt_query(stmt,proc,p - proc) == SQL_SUCCESS)
+		{
+		if (SQL_SUCCEEDED(_SQLExecute(stmt)))
+			{
+			if (stmt->hdbc->henv->odbc_ver >= 3)
+				{
+				odbc_col_setname(stmt,1,"TABLE_CAT");
+				odbc_col_setname(stmt,2,"TABLE_SCHEM");
+				odbc_col_setname(stmt,7,"COLUMN_SIZE");
+				odbc_col_setname(stmt,8,"BUFFER_LENGTH");
+				odbc_col_setname(stmt,9,"DECIMAL_DIGITS");
+				odbc_col_setname(stmt,10,"NUM_PREC_RADIX");
+				}
+			odbc_upper_column_names(stmt);
+			}
+		else
+			retcode = SQL_ERROR;
+		}
+	else
+		retcode = SQL_ERROR;
+
+	free(proc);
+	}
+else
+	{
+	retcode = SQL_ERROR;
+	odbc_errs_add(&stmt->errs,ODBCERR_MEMORY,NULL);
+	}
+return retcode;
 }
 
 SQLRETURN SQL_API


More information about the FreeTDS mailing list