'main_',
'by_postcode' => 'test_d6_c22.main_',
'states' => 'test_d6_c22.main_',
'postcodes' => 'test_d6_c22.main_',
'civicrm_acl' => 'test_d6_c22.',
'civicrm_acl_cache' => 'test_d6_c22.',
'civicrm_acl_entity_role' => 'test_d6_c22.',
'civicrm_activity' => 'test_d6_c22.',
'civicrm_activity_assignment' => 'test_d6_c22.',
'civicrm_activity_target' => 'test_d6_c22.',
'civicrm_address' => 'test_d6_c22.',
'civicrm_cache' => 'test_d6_c22.',
'civicrm_case' => 'test_d6_c22.',
'civicrm_case_activity' => 'test_d6_c22.',
'civicrm_case_contact' => 'test_d6_c22.',
'civicrm_component' => 'test_d6_c22.',
'civicrm_contact' => 'test_d6_c22.',
'civicrm_contribution' => 'test_d6_c22.',
'civicrm_contribution_page' => 'test_d6_c22.',
'civicrm_contribution_product' => 'test_d6_c22.',
'civicrm_contribution_recur' => 'test_d6_c22.',
'civicrm_contribution_type' => 'test_d6_c22.',
'civicrm_contribution_widget' => 'test_d6_c22.',
'civicrm_country' => 'test_d6_c22.',
'civicrm_county' => 'test_d6_c22.',
'civicrm_currency' => 'test_d6_c22.',
'civicrm_custom_field' => 'test_d6_c22.',
'civicrm_custom_group' => 'test_d6_c22.',
'civicrm_dedupe_rule' => 'test_d6_c22.',
'civicrm_dedupe_rule_group' => 'test_d6_c22.',
'civicrm_discount' => 'test_d6_c22.',
'civicrm_domain' => 'test_d6_c22.',
'civicrm_email' => 'test_d6_c22.',
'civicrm_entity_file' => 'test_d6_c22.',
'civicrm_entity_tag' => 'test_d6_c22.',
'civicrm_event' => 'test_d6_c22.',
'civicrm_event_page' => 'test_d6_c22.',
'civicrm_file' => 'test_d6_c22.',
'civicrm_financial_trxn' => 'test_d6_c22.',
'civicrm_grant' => 'test_d6_c22.',
'civicrm_group' => 'test_d6_c22.',
'civicrm_group_contact' => 'test_d6_c22.',
'civicrm_group_contact_cache' => 'test_d6_c22.',
'civicrm_group_nesting' => 'test_d6_c22.',
'civicrm_group_organization' => 'test_d6_c22.',
'civicrm_im' => 'test_d6_c22.',
'civicrm_line_item' => 'test_d6_c22.',
'civicrm_loc_block' => 'test_d6_c22.',
'civicrm_location_type' => 'test_d6_c22.',
'civicrm_log' => 'test_d6_c22.',
'civicrm_mailing' => 'test_d6_c22.',
'civicrm_mailing_bounce_pattern' => 'test_d6_c22.',
'civicrm_mailing_bounce_type' => 'test_d6_c22.',
'civicrm_mailing_component' => 'test_d6_c22.',
'civicrm_mailing_event_bounce' => 'test_d6_c22.',
'civicrm_mailing_event_confirm' => 'test_d6_c22.',
'civicrm_mailing_event_delivered' => 'test_d6_c22.',
'civicrm_mailing_event_forward' => 'test_d6_c22.',
'civicrm_mailing_event_opened' => 'test_d6_c22.',
'civicrm_mailing_event_queue' => 'test_d6_c22.',
'civicrm_mailing_event_reply' => 'test_d6_c22.',
'civicrm_mailing_event_subscribe' => 'test_d6_c22.',
'civicrm_mailing_event_trackable_url_open' => 'test_d6_c22.',
'civicrm_mailing_event_unsubscribe' => 'test_d6_c22.',
'civicrm_mailing_group' => 'test_d6_c22.',
'civicrm_mailing_job' => 'test_d6_c22.',
'civicrm_mailing_spool' => 'test_d6_c22.',
'civicrm_mailing_trackable_url' => 'test_d6_c22.',
'civicrm_mapping' => 'test_d6_c22.',
'civicrm_mapping_field' => 'test_d6_c22.',
'civicrm_membership' => 'test_d6_c22.',
'civicrm_membership_block' => 'test_d6_c22.',
'civicrm_membership_log' => 'test_d6_c22.',
'civicrm_membership_payment' => 'test_d6_c22.',
'civicrm_membership_status' => 'test_d6_c22.',
'civicrm_membership_type' => 'test_d6_c22.',
'civicrm_menu' => 'test_d6_c22.',
'civicrm_msg_template' => 'test_d6_c22.',
'civicrm_note' => 'test_d6_c22.',
'civicrm_openid' => 'test_d6_c22.',
'civicrm_openid_associations' => 'test_d6_c22.',
'civicrm_openid_nonces' => 'test_d6_c22.',
'civicrm_option_group' => 'test_d6_c22.',
'civicrm_option_value' => 'test_d6_c22.',
'civicrm_participant' => 'test_d6_c22.',
'civicrm_participant_payment' => 'test_d6_c22.',
'civicrm_payment_processor' => 'test_d6_c22.',
'civicrm_payment_processor_type' => 'test_d6_c22.',
'civicrm_phone' => 'test_d6_c22.',
'civicrm_pledge' => 'test_d6_c22.',
'civicrm_pledge_block' => 'test_d6_c22.',
'civicrm_pledge_payment' => 'test_d6_c22.',
'civicrm_preferences' => 'test_d6_c22.',
'civicrm_preferences_date' => 'test_d6_c22.',
'civicrm_premiums' => 'test_d6_c22.',
'civicrm_premiums_product' => 'test_d6_c22.',
'civicrm_price_field' => 'test_d6_c22.',
'civicrm_price_set' => 'test_d6_c22.',
'civicrm_price_set_entity' => 'test_d6_c22.',
'civicrm_product' => 'test_d6_c22.',
'civicrm_project' => 'test_d6_c22.',
'civicrm_relationship' => 'test_d6_c22.',
'civicrm_relationship_type' => 'test_d6_c22.',
'civicrm_saved_search' => 'test_d6_c22.',
'civicrm_state_province' => 'test_d6_c22.',
'civicrm_subscription_history' => 'test_d6_c22.',
'civicrm_tag' => 'test_d6_c22.',
'civicrm_task' => 'test_d6_c22.',
'civicrm_task_status' => 'test_d6_c22.',
'civicrm_tell_friend' => 'test_d6_c22.',
'civicrm_value_donations_10' => 'test_d6_c22.',
'civicrm_worldregion' => 'test_d6_c22.',
'civicrm_value_donation_info_20' => 'test_d6_c22.',
'civicrm_value_electorates' => 'test_d6_c22.'
);
//$db_prefix = 'main_';
class DatabaseConnection {
protected $defaultPrefix = '';
protected $prefixes = array();
function __construct() {
global $db_prefix;
$this->setPrefix($db_prefix);
}
protected function setPrefix($prefix) {
if (is_array($prefix)) {
$this->defaultPrefix = isset($prefix['default']) ? $prefix['default'] : '';
unset($prefix['default']);
$this->prefixes = $prefix;
} else {
$this->defaultPrefix = $prefix;
$this->prefixes = array();
}
}
public function prefixTables($sql) {
// Replace specific table prefixes first.
foreach ($this->prefixes as $key => $val) {
$sql = strtr($sql, array('{' . $key . '}' => $val . $key));
}
// Then replace remaining tables with the default prefix.
return strtr($sql, array('{' => $this->defaultPrefix , '}' => ''));
}
public function prefixTablesNew($sql) {
if (!empty($this->prefixes)) {
$find = $this->prefixes;
$find = array_keys($find);
array_walk($find, array($this, 'prefixTablesFind'));
$replace = $this->prefixes;
array_walk($replace, array($this, 'prefixTablesReplace'));
} else {
$find = $replace = array();
}
array_push($find, '{', '}');
array_push($replace, $this->defaultPrefix, '');
return str_replace($find, $replace, $sql);
}
protected function prefixTablesFind(&$value) {
$value = '{' . $value . '}';
}
protected function prefixTablesReplace(&$value, $key) {
$value = $value . $key;
}
}
$dc = new DatabaseConnection();
sleep(2);
$start = microtime(TRUE);
for ($i=0; $i < 1000; $i++) {
$var = $dc->prefixTables('test {civicrm_subscription_history} {foo} {postcodes} {bar}
');
}
$middle = microtime(TRUE);
for ($i=0; $i < 1000; $i++) {
$var = $dc->prefixTablesNew('test {civicrm_subscription_history} {foo} {postcodes} {bar}
');
}
$end = microtime(TRUE);
$old = $middle - $start;
$new = $end - $middle;
echo 'OLD: ' . $old . ' seconds
';
echo 'NEW: ' . $new . ' seconds
';