function JB_schema_add_field($table_name, $field_id, $field_type, $field_label) { $add_sql = ''; JBPLUG_do_callback('add_field', $add_sql, $field_id, $field_type, $field_label); if ($add_sql) { return $add_sql; } preg_match('#\\d#', mysql_get_server_info(), $m); if ($m[0] > 5 && strlen($field_label) > 0) { // mysql v5 or higher? add a comment $comment = "COMMENT '" . addslashes($field_label) . "'"; } $def = JB_get_definition($field_type); if (is_array($def)) { foreach ($def as $postfix => $data_type) { $sql = "ALTER TABLE `{$table_name}` ADD `" . $field_id . "_" . $postfix . "` " . $data_type; JB_mysql_query($sql); } $sql = "ALTER TABLE `{$table_name}` ADD `{$field_id}` INT(11) NOT NULL "; // useful for storing id JB_mysql_query($sql); } else { $sql = "ALTER TABLE `{$table_name}` ADD `{$field_id}` " . $def . " {$comment} "; JB_mysql_query($sql); } return true; }
function jb_fit_to_db_size($field_type, $value) { if (!is_string($value)) { return $value; } // only works on strings. $def = JB_get_definition($field_type); if (is_array($def)) { return; } $def = strtoupper($def); if (strpos($def, 'CHAR') !== false) { // extract the size preg_match('/\\d+/', $def, $m); if (is_numeric($size = $m[0])) { // get the first match $temp_str = substr($value, 0, 255); if (preg_match('/&#?\\d*?$/', $temp_str, $m, PREG_OFFSET_CAPTURE)) { $offset = $m[0][1]; $temp_str = substr($value, 0, $offset); } $value = $temp_str; } } return $value; }