[futurebasic] Re: [FB] continued - Problem converting Pascal strings to C strings - inside CFStringRef

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : October 2011 : Group Archive : Group : All Groups

From: Thomas Peters <thomasg_peters@...>
Date: Sun, 2 Oct 2011 10:42:50 -0400
>	Ken wrote: Is there a variable that tells the runtime to expect a C
>string buffer?

I would like to know the answer, for the purpose of testing.




>	Bernie said:  Another small problem with this code is edit field (id =
>1) is being recreated on each iteration of the loop.

Yes... I understand, however this is a simple test and not meant to be
considered anything else other a comparison test.




>	Ken wrote again: I took me a while to understand that Thomas was
>comparing
	the results of the CFStringRefs in the CFArray


Exactly right Ken.  I do not necessarily care that the CStrings do not
work in an edit field.  However, if I can get an answer to the above, then
I can better validate my concept.  It fact, it's just as you said, I am
just trying to validate the values.  I went this route because I am
getting a SQL Error when when trying to use the Cstring in the
MYSQL_BIND.buffer.  :-)





	Ken wrote again, saying:  Hre's another approach that takes the C Strings
and reconverts
	them back into CFStringRefs to test their integrity.

	Ken

	and: As usual, Bernie and Brian bailed me out.


Thanks so much for jumping into this discussion so deeply guys...  :-)
Awesome example Ken.


	Bernie wrote: We will get a similar message if the following is compiled
with 'Allow dim a%Š' unchecked.

Thanks Bernie...   I unchecked that compiler preference setting in my
instance.


Here is a working example that does a prepared insert with: <<<  note
you'll need the mysql libraries installed and a mysql server installed at
localhost.

'--------------
include "MxLog.incl"
include "CommandLineTool"

BeginCDeclaration

#include "/usr/include/mysql/mysql.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define STRING_SIZE 50

#define DROP_SAMPLE_TABLE      "DROP TABLE IF EXISTS test_table"
#define CREATE_SAMPLE_TABLE    "CREATE TABLE test_table(col1 INT, col2
VARCHAR(40), col3 SMALLINT, col4 TIMESTAMP)"
#define INSERT_SAMPLE          "INSERT INTO test_table(col1,col2,col3)
VALUES(?,?,?)"

int Test();
EndC

BeginCFunction

int Test() {

  MxLog( PSstrcpy( STACK_PUSH, "\pstart up" ) );

  //cout << "start up" << endl;
  MYSQL * connection;
  connection = mysql_init(0);
  // update for your own environment
  connection = 
mysql_real_connect(connection,"localhost","tpeters","river123","process_eng
ine",0,NULL,0);
  // connection = mysql_real_connect(connection, 0, "root", "", "test", 0,
"/opt/local/var/mysql/mysql.sock", 0);

  if (0 == connection) {
    MxLog( PSstrcpy( STACK_PUSH, "\pconnect failed:" ) );
    // cerr << "connect failed:" << mysql_error(connection) << endl;
    return -1;
  }

  // static const char *sql_statement = "SELECT application_id, user_id,
created_by, date_created FROM process_engine.application_user WHERE
application_id=? AND user_id=?";
  static const char *sql_statement = "INSERT INTO
process_engine.application_user (application_id, user_id, created_by,
date_created) VALUES ( ?, ?, ?, ? )";
  MYSQL_STMT * stmt_handle = mysql_stmt_init(connection);
  if (0 == stmt_handle) {
    MxLog( PSstrcpy( STACK_PUSH, "\pstmt init failed:" ) );
    // cerr << "stmt init failed:" << mysql_error(connection) << endl;
    return -1;
  }

  int mysql_return_code = mysql_stmt_prepare(stmt_handle, sql_statement,
strlen(sql_statement));

  if (0 != mysql_return_code) {
    MxLog( PSstrcpy( STACK_PUSH, "\pstmt prepare failed:" ) );
    MxLog( PSstrcpy( STACK_PUSH, mysql_stmt_error(stmt_handle) ) );
    // cerr << "stmt prepare failed:" << mysql_stmt_error(stmt_handle) <<
endl;
    mysql_stmt_close(stmt_handle);
    return -1;
  }

  MYSQL_BIND bind_var[4];
  unsigned long var_length[3];
  my_bool not_null = 0;
  char config_key[31] = {'\0'};
  char section_key[31] = {'\0'};
  char property_key[256] = {'\0'};

  // char application_id[31] = {'\1'};
  // char user_id[31] = {'\jonny'};
  // char created_by[31] = {'\Thomas Peters'};
  char application_id[31] = {'\1'};
  char user_id[50] = "tommy";
  char created_by[50] = "Thomas Peters";
  // char date_created[31] = {'\2011-09-03'};
  
  MYSQL_TIME  ts;
  ts.year = 2011;
  ts.month = 03;
  ts.day = 03;

  // ts.hour = 00;
  // ts.minute = 00;
  // ts.second = 00;
  
  bzero(bind_var, sizeof(bind_var));
  bind_var[0].buffer_type = MYSQL_TYPE_LONG;
  bind_var[0].buffer = (char *)application_id;
  bind_var[0].buffer_length = sizeof(application_id);
  bind_var[0].is_null = &not_null;

  bind_var[1].buffer_type = MYSQL_TYPE_VAR_STRING;
  bind_var[1].buffer = (char *)user_id;
  bind_var[1].buffer_length = sizeof(user_id);
  bind_var[1].is_null = &not_null;

  bind_var[2].buffer_type = MYSQL_TYPE_VAR_STRING;
  bind_var[2].buffer = (char *)created_by;
  bind_var[2].buffer_length = sizeof(created_by);
  bind_var[2].is_null = &not_null;

  bind_var[3].buffer_type = MYSQL_TYPE_DATE;
  bind_var[3].buffer = (char *)&ts;
  bind_var[3].buffer_length = 0;
  bind_var[3].is_null = 0;
  /*   MYSQL_TYPE_DATE
  bzero(bind_var, sizeof(bind_var));
  bind_var[0].buffer_type = MYSQL_TYPE_VAR_STRING;
  bind_var[0].buffer = (char *)config_key;
  bind_var[0].buffer_length = sizeof(config_key);
  bind_var[0].is_null = &not_null;

  bind_var[1].buffer_type = MYSQL_TYPE_VAR_STRING;
  bind_var[1].buffer = (char *)section_key;
  bind_var[1].buffer_length = sizeof(section_key);
  bind_var[1].is_null = &not_null;

  bind_var[2].buffer_type = MYSQL_TYPE_VAR_STRING;
  bind_var[2].buffer = (char *)property_key;
  bind_var[2].buffer_length = sizeof(property_key);
  bind_var[2].is_null = &not_null; */

  strncpy(config_key, "svp001", sizeof(section_key)-1);
  strncpy(section_key, "stats", sizeof(section_key)-1);
  strncpy(property_key, "number.of.threads", sizeof(property_key)-1);

  mysql_return_code = mysql_stmt_bind_param(stmt_handle, bind_var);
  if (0 != mysql_return_code) {
    MxLog( PSstrcpy( STACK_PUSH, "\pstmt bind failed:" ) );
    MxLog( PSstrcpy( STACK_PUSH, mysql_stmt_error(stmt_handle) ) );
    // cerr << "stmt bind failed:" << mysql_stmt_error(stmt_handle) <<
endl;
    mysql_stmt_close(stmt_handle);
    return -1;
  }

  mysql_return_code = mysql_stmt_execute(stmt_handle);
  if (0 != mysql_return_code) {
    MxLog( PSstrcpy( STACK_PUSH, "\pstmt execute failed:" ) );
    MxLog( PSstrcpy( STACK_PUSH, mysql_stmt_error(stmt_handle) ) );
    // cerr << "stmt execute failed:" << mysql_stmt_error(stmt_handle) <<
endl;
    mysql_stmt_close(stmt_handle);
    return -1;
  }

  char property_value[256] = {'\0'};
  MYSQL_BIND result_var[1];
  unsigned long result_var_length[1];
  my_bool is_null[1];

  bzero(result_var, sizeof(result_var));
  result_var[0].buffer_type = MYSQL_TYPE_VAR_STRING;
  result_var[0].buffer = (char *)property_value;
  result_var[0].buffer_length = sizeof(property_value)-1;
  result_var[0].is_null = &is_null[0];
  result_var[0].length = &result_var_length[0];

  mysql_return_code = mysql_stmt_bind_result(stmt_handle, result_var);
  if (0 != mysql_return_code) {
    MxLog( PSstrcpy( STACK_PUSH, "\pstmt bind rslt failed:" ) );
    MxLog( PSstrcpy( STACK_PUSH, mysql_stmt_error(stmt_handle) ) );
    // cerr << "stmt bind rslt failed:" << mysql_stmt_error(stmt_handle)
<< endl;
    mysql_stmt_close(stmt_handle);
    return -1;
  }

  mysql_return_code = mysql_stmt_store_result(stmt_handle);
  if (0 != mysql_return_code) {
    MxLog( PSstrcpy( STACK_PUSH, "\pstmt store rslt failed:" ) );
    MxLog( PSstrcpy( STACK_PUSH, mysql_stmt_error(stmt_handle) ) );
    // cerr << "stmt store rslt failed:" << mysql_stmt_error(stmt_handle)
<< endl;
    mysql_stmt_close(stmt_handle);
    return -1;
  }

  mysql_return_code = mysql_stmt_fetch(stmt_handle);
  if (0 != mysql_return_code && MYSQL_NO_DATA != mysql_return_code) {
    MxLog( PSstrcpy( STACK_PUSH, "\pstmt fetch failed:" ) );
    // cerr << "stmt fetch failed:" << mysql_stmt_error(stmt_handle) <<
endl;
    mysql_stmt_free_result(stmt_handle);
    mysql_stmt_close(stmt_handle);
    return -1;
  }

  if (MYSQL_NO_DATA != mysql_return_code) {
    MxLog( PSstrcpy( STACK_PUSH, "\pstmt - got: " ) );
    // cout << "stmt - got: " << property_value << endl;
  }

  mysql_stmt_free_result(stmt_handle);
  mysql_stmt_close(stmt_handle);
  mysql_close(connection);
  mysql_server_end();
  MxLog( PSstrcpy( STACK_PUSH, "\pdone" ) );
  // cout << "done" << endl;
  return 0;
}

EndC

toolbox fn Test = SInt32

// main
fn Test

do
HandleEvents
until gFBQuit

'--------------