- RETCODE rc;
- SQLCHAR colname[64];
- SQLSMALLINT name_len,col_type,col_scale,col_null;
- UDWORD col_prec;
- int i;
-
- if (row == NULL)
- return SQL_ERROR;
-
- //Debug(LDAP_DEBUG_TRACE,"==> backsql_BindRowAsStrings()\n",0,0,0);
- rc=SQLNumResultCols(sth,&row->ncols);
- if (rc != SQL_SUCCESS)
- {
- //Debug(LDAP_DEBUG_TRACE,"_SQLBindRowAsStrings(): SQLNumResultCols() failed:\n",0,0,0);
- backsql_PrintErrors(SQL_NULL_HENV,SQL_NULL_HDBC,sth,rc);
- }
- else
- {
- //Debug(LDAP_DEBUG_TRACE,"backsql_BindRowAsStrings: ncols=%d\n",(int)row->ncols,0,0);
- row->col_names=(char**)ch_calloc(row->ncols,sizeof(char*));
- row->cols=(char**)ch_calloc(row->ncols,sizeof(char*));
- row->col_prec=(UDWORD*)ch_calloc(row->ncols,sizeof(UDWORD));
- row->is_null=(SQLINTEGER*)ch_calloc(row->ncols,sizeof(SQLINTEGER));
- for (i=1;i<=row->ncols;i++)
- {
- rc=SQLDescribeCol(sth,(SQLSMALLINT)i,&colname[0],(SQLUINTEGER)sizeof(colname)-1,&name_len,&col_type,
- (UDWORD*) &col_prec,&col_scale,&col_null);
- row->col_names[i-1]=strdup(colname);
- //Debug(LDAP_DEBUG_TRACE,"backsql_BindRowAsStrings: col_name=%s, col_prec[%d]=%d\n",colname,(int)i,(int)col_prec);
- if (col_type == SQL_LONGVARCHAR || col_type== SQL_LONGVARBINARY)
- {
- //row->cols[i-1]=NULL;
- //row->col_prec[i-1]=-1;
- //such fields must be handled in some other way since they return 2G
- //as their precision (at least it does so with MS SQL Server w/native driver)
- //for now, we just set fixed precision for such fields - dirty hack, but...
- //no time to deal with SQLGetData()
- col_prec=MAX_ATTR_LEN;
- row->cols[i-1]=(char*)ch_calloc((col_prec+1),sizeof(char));
- row->col_prec[i-1]=col_prec;
- rc=SQLBindCol(sth,(SQLUSMALLINT)i,SQL_C_CHAR,(SQLPOINTER)row->cols[i-1],col_prec+1,
- &row->is_null[i-1]);
- }
- else
- {
- row->cols[i-1]=(char*)ch_calloc((col_prec+1),sizeof(char));
- row->col_prec[i-1]=col_prec;
- rc=SQLBindCol(sth,(SQLUSMALLINT)i,SQL_C_CHAR,(SQLPOINTER)row->cols[i-1],col_prec+1,
- &row->is_null[i-1]);
- }
- }
- }
- //Debug(LDAP_DEBUG_TRACE,"<== backsql_BindRowAsStrings()\n",0,0,0);
- return rc;
+ RETCODE rc;
+ SQLCHAR colname[ 64 ];
+ SQLSMALLINT name_len, col_type, col_scale, col_null;
+ UDWORD col_prec;
+ int i;
+
+ if ( row == NULL ) {
+ return SQL_ERROR;
+ }
+
+#ifdef BACKSQL_TRACE
+ Debug( LDAP_DEBUG_TRACE, "==> backsql_BindRowAsStrings()\n", 0, 0, 0 );
+#endif /* BACKSQL_TRACE */
+
+ rc = SQLNumResultCols( sth, &row->ncols );
+ if ( rc != SQL_SUCCESS ) {
+#ifdef BACKSQL_TRACE
+ Debug( LDAP_DEBUG_TRACE, "_SQLBindRowAsStrings(): "
+ "SQLNumResultCols() failed:\n", 0, 0, 0 );
+#endif /* BACKSQL_TRACE */
+
+ backsql_PrintErrors( SQL_NULL_HENV, SQL_NULL_HDBC, sth, rc );
+ } else {
+#ifdef BACKSQL_TRACE
+ Debug( LDAP_DEBUG_TRACE, "backsql_BindRowAsStrings: "
+ "ncols=%d\n", (int)row->ncols, 0, 0 );
+#endif /* BACKSQL_TRACE */
+
+ row->col_names = (BerVarray)ch_calloc( row->ncols + 1,
+ sizeof( struct berval ) );
+ row->cols = (char **)ch_calloc( row->ncols + 1,
+ sizeof( char * ) );
+ row->col_prec = (UDWORD *)ch_calloc( row->ncols,
+ sizeof( UDWORD ) );
+ row->is_null = (SQLINTEGER *)ch_calloc( row->ncols,
+ sizeof( SQLINTEGER ) );
+ for ( i = 1; i <= row->ncols; i++ ) {
+ rc = SQLDescribeCol( sth, (SQLSMALLINT)i, &colname[ 0 ],
+ (SQLUINTEGER)( sizeof( colname ) - 1 ),
+ &name_len, &col_type,
+ &col_prec, &col_scale, &col_null );
+ ber_str2bv( colname, 0, 1, &row->col_names[ i - 1 ] );
+#ifdef BACKSQL_TRACE
+ Debug( LDAP_DEBUG_TRACE, "backsql_BindRowAsStrings: "
+ "col_name=%s, col_prec[%d]=%d\n",
+ colname, (int)i, (int)col_prec );
+#endif /* BACKSQL_TRACE */
+ if ( col_type == SQL_LONGVARCHAR
+ || col_type == SQL_LONGVARBINARY) {
+#if 0
+ row->cols[ i - 1 ] = NULL;
+ row->col_prec[ i - 1 ] = -1;
+
+ /*
+ * such fields must be handled
+ * in some other way since they return 2G
+ * as their precision (at least it does so
+ * with MS SQL Server w/native driver)
+ * for now, we just set fixed precision
+ * for such fields - dirty hack, but...
+ * no time to deal with SQLGetData()
+ */
+#endif
+ col_prec = MAX_ATTR_LEN;
+ row->cols[ i - 1 ] = (char *)ch_calloc( col_prec + 1, sizeof( char ) );
+ row->col_prec[ i - 1 ] = col_prec;
+ rc = SQLBindCol( sth, (SQLUSMALLINT)i,
+ SQL_C_CHAR,
+ (SQLPOINTER)row->cols[ i - 1 ],
+ col_prec + 1,
+ &row->is_null[ i - 1 ] );
+ } else {
+ row->cols[ i - 1 ] = (char *)ch_calloc( col_prec + 1, sizeof( char ) );
+ row->col_prec[ i - 1 ] = col_prec;
+ rc = SQLBindCol( sth, (SQLUSMALLINT)i,
+ SQL_C_CHAR,
+ (SQLPOINTER)row->cols[ i - 1 ],
+ col_prec + 1,
+ &row->is_null[ i - 1 ] );
+ }
+ }
+
+ row->col_names[ i - 1 ].bv_val = NULL;
+ row->col_names[ i - 1 ].bv_len = 0;
+ row->cols[ i - 1 ] = NULL;
+ }
+
+#ifdef BACKSQL_TRACE
+ Debug( LDAP_DEBUG_TRACE, "<== backsql_BindRowAsStrings()\n", 0, 0, 0 );
+#endif /* BACKSQL_TRACE */
+
+ return rc;