473,320 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Wrapper for ADOdb_lite -- best way to do it?

Tom
I'm trying to adapt a function I'd written to insert arrays in MySQL to
the ADOdb_lite class. The best way to do this would probably be to
extend the ADOdb_lite class, but there doesn't seem to be a nice way to
do that. So what I've done is created two wrapper functions:

1. wrapper_ado_cx($db_name='_default', $db_user='_default',
$db_pass='_default', $db_server='_default', $db_type='mysql')
2. wrapper_ado_insert_array($ARRAY, $table, $check_cols=1,
$db_name='_default')

The first creates the ADO object and manages the database connection
using the ADOdb_lite class. The second inserts an array into a table by
preparing a statement that matches the array keys to the table columns.

I'd like to know if I'm going about this is the most efficient way or
if anyone has any suggestions for making this more efficient. The goal
is to package the connection and insert code into the functions as
neatly as possible and have each function always refer to the same ADO
object. I do this by making the ADO object a global. Is there a better
way to do this?

The functions are below. If the formatting gets mangled, please see
this site for clean formatting:

http://project1020.pbwiki.com/adodb_...0wrappers#Code

Thanks,
Tom
// wrapper_ado_cx
/*_________________________________________________ ___________________________*/
function wrapper_ado_cx($db_name='_default', $db_user='_default',
$db_pass='_default', $db_server='_default', $db_type='mysql')
{
// *** DATA

# Global
global $O_WRAP_ADO;
// *** MANIPULATE

// Sanity Checks

# DB Name
if ( $db_name == '_default' )
{
if ( defined('DBA_NAME') )
{
$db_name = DBA_NAME;
}
else
{
trigger_error('invalid db name - DBA_NAME not set',
E_USER_WARNING);
return 0;
}
}

# DB User
if ( $db_user == '_default' )
{
if ( defined('DBA_USER') )
{
$db_user = DBA_USER;
}
else
{
trigger_error('invalid db user - DBA_USER not set',
E_USER_WARNING);
return 0;
}
}

# DB Pass
if ( $db_pass == '_default' )
{
if ( defined('DBA_PASS') )
{
$db_pass = DBA_PASS;
}
else
{
trigger_error('invalid db name - DBA_PASS not set',
E_USER_WARNING);
return 0;
}
}

# DB Server
if ( $db_server == '_default' )
{
if ( defined('DBA_SERVER') )
{
$db_server = DBA_SERVER;
}
else
{
trigger_error('invalid db name - DBA_SERVER not set',
E_USER_WARNING);
return 0;
}
}

# New ADO Object
if ( !is_object($O_WRAP_ADO['DB']) )
{
# Create New Object
$O_WRAP_ADO['DB'] = ADONewConnection($db_type);
$O_WRAP_ADO['DB']->Connect($db_server, $db_user, $db_pass,
$db_name);
$O_WRAP_ADO['DB']->db_name = $db_name;
}

# Retrieve Existing Object
else
{
# New Connection
if ( !$O_WRAP_ADO['DB']->IsConnected() )
{
$O_WRAP_ADO['DB']->Connect($db_server, $db_user, $db_pass,
$db_name);
$O_WRAP_ADO['DB']->db_name = $db_name;
}

# Existing Connection (check db name)
elseif ( $O_WRAP_ADO['DB']->db_name <> $db_name )
{
$O_WRAP_ADO['DB']->SelectDB($db_name);
$O_WRAP_ADO['DB']->db_name = $db_name;
}
}
// *** RETURN

return $O_WRAP_ADO['DB'];

} # end Fx
/*_________________________________________________ ___________________________*/
// wrapper_ado_insert_array
/*_________________________________________________ ___________________________*/
function wrapper_ado_insert_array($ARRAY, $table, $check_cols=1,
$db_name='_default')
{
// *** DATA

# Globals
global $O_WRAP_ADO;

# SQL
$_SQL = array();
$_SQL['check_cols'] = '';
$_SQL['insert_array'] = '';

# flags
$_FLAG['check_cols'] = $check_cols;
$_FLAG['is_col'] = 0;

# internal
$_link = 0;
$_insert_line = '';
$_values_line = '';
$_TABLE_COL = array();
$_COLUMN = array();
$_DATA_BIND = array();
$_ADO_r = 0;

# return
$insert_id = 0;
// *** MANIPULATE

// Check DB Connection
if ( !is_object($O_WRAP_ADO['DB']) ||
!$O_WRAP_ADO['DB']->IsConnected() )
{
if ( !wrapper_ado_cx($db_name) )
{
trigger_error('could not establish db connection',
E_USER_WARNING);
return 0;
}
}

// Check DB

if ( $db_name <> '_default' && $O_WRAP_ADO['DB']->db_name <>
$db_name )
{
$O_WRAP_ADO['DB']->SelectDB($db_name);
$O_WRAP_ADO['DB']->db_name = $db_name;
}

// Set Fetch Mode

$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;

// Check Table Columns
if ( $check_cols )
{
# SQL Query
$_SQL['check_cols'] = "SHOW COLUMNS FROM $table";

# Run Query
if ( !$_ADO_r = $O_WRAP_ADO['DB']->Execute($_SQL['check_cols']) )

{
trigger_error("Unable to check columns for table [$table]:
{$_SQL['check_cols']}", E_USER_WARNING);
return 0;
}

# Sanity Check
if ( !$_ADO_r->RecordCount() )
{
trigger_error("No columns found for table [$table]",
E_USER_WARNING);
return 0;
}

# Collect Table Columns
else
{
while ( !$_ADO_r->EOF )
{
$_TABLE_COL = $_ADO_r->fields;
$_COLUMN[] = $_TABLE_COL['Field'];
$_ADO_r->MoveNext();
}
}
}

// Construct Query String

# cycle array
foreach ( $ARRAY as $_key => $_value )
{
# check to see ARRAY key is a table column [$_COLUMN]
if ( !$check_cols || in_array($_key, $_COLUMN ) )
{
# extend insert, values line
$_insert_line .= "`$_key`, ";
$_values_line .= "?, ";

# add value to data binding array
$_DATA_BIND[] = $_value;
}
else
{
trigger_error("key [$_key] is not column in table [$table] ->
will not be inserted; please correct code", E_USER_NOTICE);
}
}

# assemble query

# trim insert superfluous comma from query strings
$_insert_line = substr($_insert_line, 0, strrpos($_insert_line,
','));
$_values_line = substr($_values_line, 0, strrpos($_values_line,
','));

# SQL query
$_SQL['insert_array'] = <<<SQL
INSERT INTO `$table` ( $_insert_line )
VALUES ( $_values_line )
SQL;

# debug
if ( OWRAP_DEBUG ) trigger_notice($_SQL['insert_array']);
// Run Query

if ( !$_ADO_r = $O_WRAP_ADO['DB']->Execute($_SQL['insert_array'],
$_DATA_BIND) )
{
trigger_error($O_WRAP_ADO['DB']->ErrorMsg(), E_USER_WARNING);
return 0;
}
else
{
$insert_id = $O_WRAP_ADO['DB']->Insert_ID();
}
// Close DB

$O_WRAP_ADO['DB']->Close();
// *** RETURN

return $insert_id;

} # end Fx
/*_________________________________________________ ___________________________*/

Jul 1 '06 #1
0 1359

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Christian Christmann | last post by:
Hi, I need an STL list and was thinking of putting the list in wrapper class. The reason for my decision is that you can much better perform consistency checks. For instance, I need a...
5
by: niidjan | last post by:
I would like to know more about C++ wrappers. What is a wrapper object in C++ and why is it neccessary? I am trying to create a templated linked list data structure to store any object or primitive...
15
by: Steve Richfield | last post by:
To All, First, some of your replies to me have been posted through DevelopersDex, and these are NOT posted on USENET for the world to see. DevelopersDex appears to be trying to hijack USENET,...
1
by: Curious George | last post by:
I would like to create a connection object wrapper for my web application. I would like my app to request a connection from my wrapper and have my wrapper function choose the best DB to connect...
1
by: Adam Clauss | last post by:
I have an unmanaged C++ library that I need to use through C#. I created a Managed C++ class library and through it wrote a wrapper class to call what I need. However, one of the methods (an...
4
by: Gary Bond | last post by:
Hi All, Can anybody point me to some 'how-to' documentation, tutorials, etc as to how to write a shrink/protect wrapper for .Net exes/dlls, (like the Shrinkwrap product for instance). I have...
4
by: a_agaga | last post by:
Hi! Do you know different alternatives to convert exceptions in many methods of some wrapper classes. User -Wrapper classes -LibraryClasses -... Wrapper classes catch an exception of only...
12
by: joshuapking | last post by:
I'm having a very difficult time coming across an appropriate solution for this seemingly simple problem. I've written a managed wrapper class for some legacy C++ routines. One routine generates...
0
by: Ed | last post by:
Hi, dear all, I want to wrapper the legacy C++ Template Class to be used by the C# code, so I try to wrapper the legacy C++ code into CLI/C++ code first. One problem is how to wrapper C++...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.