Permalink
Browse files

Included SQLTables(), SQLNumResultCols(), SQLFetch(), SQLGetData(), S…

…QLDisconnect(), SQLFreeHandle()
  • Loading branch information...
1 parent 7a5272b commit d46297eb55cb6635d0e783178879f915e9f97003 @BenoyRNair committed Nov 27, 2009
Showing with 203 additions and 12 deletions.
  1. +2 −0 README
  2. +36 −0 cgodbc.c
  3. +20 −0 cgodbc.h
  4. +59 −6 example.go
  5. +86 −6 godbc.go
View
2 README
@@ -18,6 +18,8 @@ The program does the following:
3. Connect to a data source
4. Detail errors returned from the database
5. Get database info/ properties
+6. Fetch and print the tables in the database
+7. Close database connections and free resources
Todo
1. Extend the go library to cover the entire ODBC specification.
View
@@ -6,6 +6,7 @@
* 22-Nov-09 Benoy R Nair First draft
* 23-Nov-09 Benoy R Nair For SQLDriverConnect()
* 23-Nov-09 Benoy R Nair For SQLGetInfo()
+ * 27-Nov-09 Benoy R Nair For SQLTables(), SQLNumResultCols(), SQLGetData()
*/
#include "cgodbc.h"
@@ -73,3 +74,38 @@ int GO_GetInfo_Int ( SQLHANDLE connectionHandle
, 0
, 0 ) );
}
+
+int GO_Tables ( SQLHANDLE statementHandle
+ , char * catalogName
+ , SQLSMALLINT nameLength1
+ , char * schemaName
+ , SQLSMALLINT nameLength2
+ , char * tableName
+ , SQLSMALLINT nameLength3
+ , char * tableType
+ , SQLSMALLINT nameLength4 )
+{
+ return ( SQLTables ( ( SQLHSTMT ) statementHandle
+ , nameLength1 == 0 ? NULL : ( SQLCHAR * ) catalogName
+ , nameLength1
+ , nameLength2 == 0 ? NULL : ( SQLCHAR * ) schemaName
+ , nameLength2
+ , nameLength3 == 0 ? NULL : ( SQLCHAR * ) tableName
+ , nameLength3
+ , nameLength4 == 0 ? NULL : ( SQLCHAR * ) tableType
+ , nameLength4 ) );
+}
+
+int GO_GetData_String ( SQLHANDLE statementHandle
+ , SQLUSMALLINT columnNumber
+ , SQLCHAR * targetValue
+ , SQLINTEGER bufferLength
+ , SQLINTEGER * indicator )
+{
+ return ( SQLGetData ( ( SQLHSTMT ) statementHandle
+ , columnNumber
+ , SQL_C_CHAR
+ , ( char * ) targetValue
+ , ( SQLLEN ) bufferLength
+ , ( SQLLEN * ) indicator ) );
+}
View
@@ -6,6 +6,7 @@
* 22-Nov-09 Benoy R Nair First draft
* 23-Nov-09 Benoy R Nair For SQLDriverConnect()
* 23-Nov-09 Benoy R Nair For SQLGetInfo()
+ * 27-Nov-09 Benoy R Nair For SQLTables(), SQLNumResultCols(), SQLGetData()
*/
#ifndef _CGODBC_H_
#define _CGODBC_H_
@@ -14,6 +15,9 @@
#include <sql.h>
#include <sqlext.h>
+#include <stdio.h>
+#include <stdlib.h>
+
int GO_SetEnvAttr ( SQLHANDLE environmentHandle
, SQLINTEGER attribute
, int value
@@ -41,4 +45,20 @@ int GO_GetInfo_Uint ( SQLHANDLE connectionHandle
int GO_GetInfo_Int ( SQLHANDLE connectionHandle
, SQLUSMALLINT infoType
, SQLSMALLINT * infoValue );
+
+int GO_Tables ( SQLHANDLE statementHandle
+ , char * catalogName
+ , SQLSMALLINT nameLength1
+ , char * schemaName
+ , SQLSMALLINT nameLength2
+ , char * tableName
+ , SQLSMALLINT nameLength3
+ , char * tableType
+ , SQLSMALLINT nameLength4 );
+
+int GO_GetData_String ( SQLHANDLE statementHandle
+ , SQLUSMALLINT columnNumber
+ , SQLCHAR * targetValue
+ , SQLINTEGER bufferLength
+ , SQLINTEGER * indicator );
#endif
View
@@ -6,21 +6,21 @@
* 22-Nov-09 Benoy R Nair First draft
* 23-Nov-09 Benoy R Nair For SQLDriverConnect()
* 23-Nov-09 Benoy R Nair For SQLGetDiagRec(), SQLGetInfo()
+ * 27-Nov-09 Benoy R Nair For SQLTables(), SQLNumResultCols(), SQLFetch(), SQLGetData(), SQLDisconnect(), SQLFreeHandle()
*/
package main
import "godbc"
import "fmt"
import "os"
-var env, dbc, stmt godbc.GS_HANDLE;
+var env, dbc, stmt * godbc.GS_HANDLE;
func init()
{
- godbc.NULL_HANDLE.GS_AllocHandle ( godbc.GS_HANDLE_ENV, &env );
+ _, env = godbc.NULL_HANDLE.GS_AllocHandle ( godbc.GS_HANDLE_ENV );
env.GS_SetEnvAttr ( godbc.GS_ATTR_ODBC_VERSION, godbc.GS_OV_ODBC3, 0 );
- env.GS_AllocHandle ( godbc.GS_HANDLE_DBC, &dbc );
- dbc.GS_AllocHandle ( godbc.GS_HANDLE_STMT, &stmt );
+ _, dbc = env.GS_AllocHandle ( godbc.GS_HANDLE_DBC );
}
func main()
@@ -29,6 +29,9 @@ func main()
listDrivers();
connect();
testGetInfo();
+ createStmt();
+ listTables();
+ cleanup();
}
func listDataSources()
@@ -100,7 +103,7 @@ func connect()
else
{
fmt.Printf ( "Unable to connect. Check the login credentials provided in the code.\n" );
- extractError ( "connect()", &dbc, godbc.GS_HANDLE_DBC );
+ extractError ( "connect()", dbc, godbc.GS_HANDLE_DBC );
os.Exit (1);
}
}
@@ -143,6 +146,57 @@ func testGetInfo()
}
}
+func createStmt()
+{
+ _, stmt = dbc.GS_AllocHandle ( godbc.GS_HANDLE_STMT );
+}
+
+func listTables()
+{
+ stmt.GS_Tables ( "", 0, "", 0, "", 0, "TABLE", godbc.GS_NTS );
+
+ _, columns := stmt.GS_NumResultCols();
+
+ fmt.Printf ( "Number of columns: %d\n", columns );
+
+ for row := 1;; row++
+ {
+ x := stmt.GS_Fetch();
+
+ if ! godbc.GS_Succeeded ( x )
+ {
+ break;
+ }
+
+ fmt.Printf ( "Row %d => ", row );
+
+ for i := 1; i <= columns; i++
+ {
+ ret, indicator, columnValue := stmt.GS_GetData_String ( i );
+
+ if godbc.GS_Succeeded ( ret )
+ {
+ if indicator == godbc.GS_NULL_DATA
+ {
+ columnValue = "NULL";
+ }
+
+ fmt.Printf ( "Col %d: \"%s\" ", i, columnValue );
+ }
+ }
+
+ fmt.Printf ( "\n" );
+ }
+}
+
+func cleanup()
+{
+ stmt.GS_FreeHandle ( godbc.GS_HANDLE_STMT );
+ dbc.GS_Disconnect();
+ dbc.GS_FreeHandle ( godbc.GS_HANDLE_DBC );
+ env.GS_FreeHandle ( godbc.GS_HANDLE_ENV );
+}
+
func extractError ( function string
, handle * godbc.GS_HANDLE
, handleType int )
@@ -165,7 +219,6 @@ func extractError ( function string
}
else
{
- fmt.Printf ( "unable to extract error\n" );
break;
}
}
View
@@ -6,8 +6,9 @@
* 22-Nov-09 Benoy R Nair First draft
* 23-Nov-09 Benoy R Nair For SQLDriverConnect()
* 23-Nov-09 Benoy R Nair For SQLGetDiagRec(), SQLGetInfo()
+ * 27-Nov-09 Benoy R Nair For SQLTables(), SQLNumResultCols(), SQLFetch(), SQLGetData(), SQLDisconnect(), SQLFreeHandle()
*/
-package godbc
+package godbc
/*
#include <stdio.h>
@@ -58,6 +59,12 @@ const
GS_GD_ANY_COLUMN = uint32 ( 1 );
GS_GD_ANY_ORDER = uint32 ( 2 );
+// use these to indicate string termination to some functions
+ GS_NTS = -3;
+
+// ret values
+ GS_NULL_DATA = -1;
+
BUFFER_SIZE = 256;
)
@@ -66,13 +73,16 @@ var
NULL_HANDLE GS_HANDLE;
)
-func ( inputHandle * GS_HANDLE ) GS_AllocHandle ( handleType int
- , outputHandle * GS_HANDLE )
- int
+func ( inputHandle * GS_HANDLE ) GS_AllocHandle ( handleType int )
+ ( int, * GS_HANDLE )
{
- return ( int ( C.SQLAllocHandle ( C.SQLSMALLINT ( handleType )
+ var outputHandle * GS_HANDLE = new ( GS_HANDLE );
+
+ x := int ( C.SQLAllocHandle ( C.SQLSMALLINT ( handleType )
, unsafe.Pointer ( inputHandle.GsHandle )
- , &outputHandle.GsHandle ) ) );
+ , &outputHandle.GsHandle ) );
+
+ return x, outputHandle;
}
func ( environmentHandle * GS_HANDLE ) GS_SetEnvAttr ( attribute int
@@ -236,6 +246,76 @@ func ( connectionHandle * GS_HANDLE ) GS_GetInfo_Int ( infoType int )
return returnInt, int ( intValue );
}
+func ( statementHandle * GS_HANDLE ) GS_Tables ( catalogName string
+ , nameLength1 int
+ , schemaName string
+ , nameLength2 int
+ , tableName string
+ , nameLength3 int
+ , tableType string
+ , nameLength4 int )
+ int
+{
+ return ( int ( C.GO_Tables ( unsafe.Pointer ( statementHandle.GsHandle )
+ , C.CString ( catalogName )
+ , C.SQLSMALLINT ( nameLength1 )
+ , C.CString ( schemaName )
+ , C.SQLSMALLINT ( nameLength2 )
+ , C.CString ( tableName )
+ , C.SQLSMALLINT ( nameLength3 )
+ , C.CString ( tableType )
+ , C.SQLSMALLINT ( nameLength4 ) ) ) );
+}
+
+func ( statementHandle * GS_HANDLE ) GS_NumResultCols()
+ ( int, int )
+{
+ var columnCount C.SQLSMALLINT;
+
+ returnInt := int ( C.SQLNumResultCols ( unsafe.Pointer ( statementHandle.GsHandle )
+ , &columnCount ) );
+
+ return returnInt, int ( columnCount );
+}
+
+func ( statementHandle * GS_HANDLE ) GS_Fetch()
+ int
+{
+ return ( int ( C.SQLFetch ( unsafe.Pointer ( statementHandle.GsHandle ) ) ) );
+}
+
+func ( statementHandle * GS_HANDLE ) GS_GetData_String ( columnNumber int )
+ ( int, int, string )
+{
+ var indicator C.SQLINTEGER;
+ buffer := ( * C.SQLCHAR ) ( C.calloc ( BUFFER_SIZE, 1 ) );
+
+ returnInt := int ( C.GO_GetData_String ( unsafe.Pointer ( statementHandle.GsHandle )
+ , C.SQLUSMALLINT ( columnNumber )
+ , buffer
+ , BUFFER_SIZE
+ , &indicator ) );
+
+ returnString := toStringTillNull ( buffer );
+
+ C.free ( unsafe.Pointer ( buffer ) );
+
+ return returnInt, int ( indicator ), returnString;
+}
+
+func ( connectionHandle * GS_HANDLE ) GS_Disconnect()
+ int
+{
+ return ( int ( C.SQLDisconnect ( unsafe.Pointer ( connectionHandle.GsHandle ) ) ) );
+}
+
+func ( inputHandle * GS_HANDLE ) GS_FreeHandle ( handleType int )
+ int
+{
+ return ( int ( C.SQLFreeHandle ( C.SQLSMALLINT ( handleType )
+ , unsafe.Pointer ( inputHandle.GsHandle ) ) ) );
+}
+
func toStringByLength ( buf * C.SQLCHAR, length int )
string
{

0 comments on commit d46297e

Please sign in to comment.