consolidate duplicated code for creating table entries from an array

This commit is contained in:
zotlabs 2016-10-12 22:12:58 -07:00
parent e314510005
commit 48026efddf
4 changed files with 51 additions and 161 deletions

View File

@ -298,15 +298,8 @@ class Import extends \Zotlabs\Web\Controller {
); );
if($r) if($r)
continue; continue;
dbesc_array($xchan); create_table_from_array('xchan',$xchan);
$r = dbq("INSERT INTO xchan (" . TQUOT
. implode(TQUOT . ", " . TQUOT, array_keys($xchan))
. TQUOT . ") VALUES ('"
. implode("', '", array_values($xchan))
. "')" );
require_once('include/photo/photo_driver.php'); require_once('include/photo/photo_driver.php');
$photos = import_xchan_photo($xchan['xchan_photo_l'],$xchan['xchan_hash']); $photos = import_xchan_photo($xchan['xchan_photo_l'],$xchan['xchan_hash']);
@ -386,13 +379,8 @@ class Import extends \Zotlabs\Web\Controller {
continue; continue;
} }
dbesc_array($abook); create_table_from_array('abook',$abook);
$r = dbq("INSERT INTO abook (" . TQUOT
. implode(TQUOT . ", " . TQUOT, array_keys($abook))
. TQUOT . ") VALUES ('"
. implode("', '", array_values($abook))
. "')" );
$friends ++; $friends ++;
if(intval($abook['abook_feed'])) if(intval($abook['abook_feed']))
$feeds ++; $feeds ++;
@ -427,13 +415,9 @@ class Import extends \Zotlabs\Web\Controller {
unset($group['name']); unset($group['name']);
} }
unset($group['id']); unset($group['id']);
$group['uid'] = $channel['channel_id']; $group['uid'] = $channel['channel_id'];
dbesc_array($group);
$r = dbq("INSERT INTO groups (" . TQUOT create_table_from_array('groups',$group);
. implode(TQUOT . ", " . TQUOT, array_keys($group))
. TQUOT . ") VALUES ('"
. implode("', '", array_values($group))
. "')" );
} }
$r = q("select * from groups where uid = %d", $r = q("select * from groups where uid = %d",
intval($channel['channel_id']) intval($channel['channel_id'])
@ -455,12 +439,7 @@ class Import extends \Zotlabs\Web\Controller {
if($x['old'] == $group_member['gid']) if($x['old'] == $group_member['gid'])
$group_member['gid'] = $x['new']; $group_member['gid'] = $x['new'];
} }
dbesc_array($group_member); create_table_from_array('group_member',$group_member);
$r = dbq("INSERT INTO group_member (" . TQUOT
. implode(TQUOT . ", " . TQUOT, array_keys($group_member))
. TQUOT . ") VALUES ('"
. implode("', '", array_values($group_member))
. "')" );
} }
} }
logger('import step 9'); logger('import step 9');

View File

@ -82,14 +82,7 @@ function import_channel($channel, $account_id, $seize) {
} }
if($clean) { if($clean) {
dbesc_array($clean); create_table_from_array('channel',$clean);
$r = dbq("INSERT INTO channel (" . TQUOT
. implode(TQUOT . ", " . TQUOT, array_keys($clean))
. TQUOT . ") VALUES ('"
. implode("', '", array_values($clean))
. "')"
);
} }
if(! $r) { if(! $r) {
@ -131,12 +124,7 @@ function import_config($channel,$configs) {
foreach($configs as $config) { foreach($configs as $config) {
unset($config['id']); unset($config['id']);
$config['uid'] = $channel['channel_id']; $config['uid'] = $channel['channel_id'];
dbesc_array($config); create_table_from_array('pconfig',$config);
$r = dbq("INSERT INTO pconfig (" . TQUOT
. implode(TQUOT . ", " . TQUOT, array_keys($config))
. TQUOT . ") VALUES ('"
. implode("', '", array_values($config))
. "')" );
} }
load_pconfig($channel['channel_id']); load_pconfig($channel['channel_id']);
} }
@ -161,14 +149,7 @@ function import_profiles($channel,$profiles) {
$profile['photo'] = z_root() . '/photo/profile/l/' . $channel['channel_id']; $profile['photo'] = z_root() . '/photo/profile/l/' . $channel['channel_id'];
$profile['thumb'] = z_root() . '/photo/profile/m/' . $channel['channel_id']; $profile['thumb'] = z_root() . '/photo/profile/m/' . $channel['channel_id'];
create_table_from_array('profile',$profile);
dbesc_array($profile);
$r = dbq("INSERT INTO profile (" . TQUOT
. implode(TQUOT . ", " . TQUOT, array_keys($profile))
. TQUOT . ") VALUES ('"
. implode("', '", array_values($profile))
. "')"
);
} }
} }
} }
@ -203,14 +184,7 @@ function import_hublocs($channel,$hublocs,$seize) {
if(! zot_gethub($arr)) { if(! zot_gethub($arr)) {
unset($hubloc['hubloc_id']); unset($hubloc['hubloc_id']);
dbesc_array($hubloc); create_table_from_array('hubloc',$hubloc);
$r = dbq("INSERT INTO hubloc (" . TQUOT
. implode(TQUOT . ", " . TQUOT, array_keys($hubloc))
. TQUOT . ") VALUES ('"
. implode("', '", array_values($hubloc))
. "')"
);
} }
} }
} }
@ -242,14 +216,7 @@ function import_objs($channel,$objs) {
$obj['obj_imgurl'] = $x[0]; $obj['obj_imgurl'] = $x[0];
} }
dbesc_array($obj); create_table_from_array('obj',$obj);
$r = dbq("INSERT INTO obj (" . TQUOT
. implode(TQUOT . ", " . TQUOT, array_keys($obj))
. TQUOT . ") VALUES ('"
. implode("', '", array_values($obj))
. "')"
);
} }
} }
} }
@ -313,15 +280,7 @@ function sync_objs($channel,$objs) {
} }
} }
else { else {
create_table_from_array('obj',$obj);
dbesc_array($obj);
$r = dbq("INSERT INTO obj (" . TQUOT
. implode(TQUOT . ", " . TQUOT, array_keys($obj))
. TQUOT . ") VALUES ('"
. implode("', '", array_values($obj))
. "')"
);
} }
} }
} }
@ -351,13 +310,7 @@ function import_apps($channel,$apps) {
$hash = $app['app_id']; $hash = $app['app_id'];
dbesc_array($app); create_table_from_array('app',$app);
$r = dbq("INSERT INTO app (" . TQUOT
. implode(TQUOT . ", " . TQUOT, array_keys($app))
. TQUOT . ") VALUES ('"
. implode("', '", array_values($app))
. "')"
);
if($term) { if($term) {
$x = q("select * from app where app_id = '%s' and app_channel = %d limit 1", $x = q("select * from app where app_id = '%s' and app_channel = %d limit 1",
@ -460,13 +413,8 @@ function sync_apps($channel,$apps) {
} }
} }
else { else {
dbesc_array($app); create_table_from_array('app',$app);
$r = dbq("INSERT INTO app (" . TQUOT
. implode(TQUOT . ", " . TQUOT, array_keys($app))
. TQUOT . ") VALUES ('"
. implode("', '", array_values($app))
. "')"
);
if($term) { if($term) {
$x = q("select * from app where app_id = '%s' and app_channel = %d limit 1", $x = q("select * from app where app_id = '%s' and app_channel = %d limit 1",
dbesc($hash), dbesc($hash),
@ -502,13 +450,7 @@ function import_chatrooms($channel,$chatrooms) {
$chatroom['cr_aid'] = $channel['channel_account_id']; $chatroom['cr_aid'] = $channel['channel_account_id'];
$chatroom['cr_uid'] = $channel['channel_id']; $chatroom['cr_uid'] = $channel['channel_id'];
dbesc_array($chatroom); create_table_from_array('chatroom',$chatroom);
$r = dbq("INSERT INTO chatroom (" . TQUOT
. implode(TQUOT . ", " . TQUOT, array_keys($chatroom))
. TQUOT . ") VALUES ('"
. implode("', '", array_values($chatroom))
. "')"
);
} }
} }
} }
@ -568,13 +510,7 @@ function sync_chatrooms($channel,$chatrooms) {
} }
} }
else { else {
dbesc_array($chatroom); create_table_from_array('chatroom',$chatroom);
$r = dbq("INSERT INTO chatroom (" . TQUOT
. implode(TQUOT . ", " . TQUOT, array_keys($chatroom))
. TQUOT . ") VALUES ('"
. implode("', '", array_values($chatroom))
. "')"
);
} }
} }
} }
@ -684,13 +620,7 @@ function import_events($channel,$events) {
convert_oldfields($event,'type','etype'); convert_oldfields($event,'type','etype');
convert_oldfields($event,'ignore','dismissed'); convert_oldfields($event,'ignore','dismissed');
dbesc_array($event); create_table_from_array('event',$event);
$r = dbq("INSERT INTO event (" . TQUOT
. implode(TQUOT . ", " . TQUOT, array_keys($event))
. TQUOT . ") VALUES ('"
. implode("', '", array_values($event))
. "')"
);
} }
} }
} }
@ -745,13 +675,7 @@ function sync_events($channel,$events) {
} }
} }
else { else {
dbesc_array($event); create_table_from_array('event',$event);
$r = dbq("INSERT INTO event (" . TQUOT
. implode(TQUOT . ", " . TQUOT, array_keys($event))
. TQUOT . ") VALUES ('"
. implode("', '", array_values($event))
. "')"
);
} }
} }
} }
@ -927,12 +851,7 @@ function import_likes($channel,$likes) {
if($r) if($r)
continue; continue;
dbesc_array($like); create_table_from_array('likes',$like);
$r = dbq("INSERT INTO likes (" . TQUOT
. implode(TQUOT . ", " . TQUOT, array_keys($like))
. TQUOT . ") VALUES ('"
. implode("', '", array_values($like))
. "')" );
} }
} }
} }
@ -959,13 +878,7 @@ function import_conv($channel,$convs) {
); );
if($r) if($r)
continue; continue;
create_table_from_array('conv',$conv);
dbesc_array($conv);
$r = dbq("INSERT INTO conv (" . TQUOT
. implode(TQUOT . ", " . TQUOT, array_keys($conv))
. TQUOT . ") VALUES ('"
. implode("', '", array_values($conv))
. "')" );
} }
} }
} }
@ -1110,25 +1023,21 @@ function sync_files($channel,$files) {
if(!isset($att['os_path'])) if(!isset($att['os_path']))
$att['os_path'] = ''; $att['os_path'] = '';
dbesc_array($att);
if($attach_exists) { if($attach_exists) {
logger('sync_files attach exists: ' . print_r($att,true), LOGGER_DEBUG); logger('sync_files attach exists: ' . print_r($att,true), LOGGER_DEBUG);
dbesc_array($att);
$str = ''; $str = '';
foreach($att as $k => $v) { foreach($att as $k => $v) {
if($str) if($str)
$str .= ","; $str .= ",";
$str .= " " . TQUOT . $k . TQUOT . " = '" . $v . "' "; $str .= " " . TQUOT . $k . TQUOT . " = '" . $v . "' ";
} }
$r = dbq("update attach set " . $str . " where id = " . intval($attach_id) ); $r = dbq("update attach set " . $str . " where id = " . intval($attach_id) );
} }
else { else {
logger('sync_files attach does not exists: ' . print_r($att,true), LOGGER_DEBUG); logger('sync_files attach does not exists: ' . print_r($att,true), LOGGER_DEBUG);
$r = dbq("INSERT INTO attach (" . TQUOT create_table_from_array('attach',$att);
. implode(TQUOT . ", " . TQUOT, array_keys($att))
. TQUOT . ") VALUES ('"
. implode("', '", array_values($att))
. "')" );
} }
@ -1229,9 +1138,9 @@ function sync_files($channel,$files) {
intval($channel['channel_id']) intval($channel['channel_id'])
); );
dbesc_array($p);
if($exists) { if($exists) {
dbesc_array($p);
$str = ''; $str = '';
foreach($p as $k => $v) { foreach($p as $k => $v) {
if($str) if($str)
@ -1241,11 +1150,7 @@ function sync_files($channel,$files) {
$r = dbq("update photo set " . $str . " where id = " . intval($exists[0]['id']) ); $r = dbq("update photo set " . $str . " where id = " . intval($exists[0]['id']) );
} }
else { else {
$r = dbq("INSERT INTO photo (" . TQUOT create_attach_from_array('photo',$p);
. implode(TQUOT . ", " . TQUOT, array_keys($p))
. TQUOT . ") VALUES ('"
. implode("', '", array_values($p))
. "')" );
} }
} }
} }

View File

@ -1853,13 +1853,8 @@ function item_store($arr, $allow_exec = false, $deliver = true) {
logger('item_store: ' . print_r($arr,true), LOGGER_DATA); logger('item_store: ' . print_r($arr,true), LOGGER_DATA);
dbesc_array($arr);
$r = dbq("INSERT INTO " . TQUOT . 'item' . TQUOT . " (" . TQUOT create_table_from_array('item',$arr);
. implode(TQUOT . ', ' . TQUOT, array_keys($arr))
. TQUOT . ") VALUES ('"
. implode("', '", array_values($arr))
. "')" );
// find the item we just created // find the item we just created
@ -3088,15 +3083,9 @@ function mail_store($arr) {
return 0; return 0;
} }
dbesc_array($arr);
logger('mail_store: ' . print_r($arr,true), LOGGER_DATA); logger('mail_store: ' . print_r($arr,true), LOGGER_DATA);
$r = dbq("INSERT INTO mail (" . TQUOT create_table_from_array('mail', $arr);
. implode(TQUOT . ', ' . TQUOT, array_keys($arr))
. TQUOT . ") VALUES ('"
. implode("', '", array_values($arr))
. "')" );
// find the item we just created // find the item we just created

View File

@ -3028,3 +3028,20 @@ function array2XML($obj, $array)
} }
} }
} }
function create_table_from_array($table,$arr) {
if(! ($arr && $table))
return false;
dbesc_array($arr);
$r = dbq("INSERT INTO " . TQUOT . $table . TQUOT . " (" . TQUOT
. implode(TQUOT . ', ' . TQUOT, array_keys($arr))
. TQUOT . ") VALUES ('"
. implode("', '", array_values($arr))
. "')" );
return $r;
}