if (isset($_POST['acc_modind_doit'])) { if (modify_index($s_mod_index)) { // on success don't show the modify index form again unset($s_mod_index); } } // modifying an index was canceled if (isset($_POST['acc_modind_cancel'])) { unset($s_mod_index); } } // // generator stuff // if (have_panel_permissions($s_login['user'], 'acc_gen', TRUE)) { $quote = identifier_quote($s_login['dialect']); // init array generators[] $lsql = 'SELECT RDB$GENERATOR_NAME AS GNAME FROM RDB$GENERATORS ' . 'WHERE RDB$SYSTEM_FLAG IS NULL OR RDB$SYSTEM_FLAG = 0'; $res = fbird_query($dbhandle, $lsql) or ib_error(); while ($row = fbird_fetch_object($res)) { $lsql = 'SELECT gen_id(' . $quote . fb_escape_string($row->GNAME) . $quote . ', 0) AS VAL FROM RDB$DATABASE'; $res1 = fbird_query($dbhandle, $lsql) or ib_error(); $row1 = fbird_fetch_object($res1); $generators[] = array('name' => trim($row->GNAME), 'value' => (int) $row1->VAL); fbird_free_result($res1); } // one of the Drop buttons on the generator panel was pushed if ($name = drop_generator_pushed()) { $deps = get_dependencies(OT_GENERATOR, $name); if (count($deps) > 0) { $message = sprintf($MESSAGES['HAVE_DEPENDENCIES'], $acc_strings['Generator'], $name, dependencies_string($deps));
function insert_row($table, $cols, $values) { $quote = identifier_quote($GLOBALS['s_login']['dialect']); $sql = 'INSERT INTO ' . $quote . $table . $quote . ' (' . $quote . implode($quote . ', ' . $quote, $cols) . $quote . ')' . ' VALUES (' . substr(str_repeat('?, ', count($values)), 0, -2) . ')'; if (DEBUG) { add_debug('$sql: ' . $sql, __FILE__, __LINE__); } $query = fbird_prepare($GLOBALS['dbhandle'], $sql) or ib_error(__FILE__, __LINE__, $sql); $ib_error = ''; call_user_func_array('fbird_execute', array_merge(array($query), $values)) or $ib_error = fbird_errmsg(); fbird_free_query($query); return $ib_error; }
function export_table_query($table, $export) { global $warning; $columns = array(); foreach ($GLOBALS['s_fields'][$table] as $field) { // only text-blobs are handled if ($field['type'] == 'BLOB' && $field['stype'] != 1) { $warning .= $WARNINGS['CAN_NOT_EXPORT_BLOBS']; continue; } // for sql-export include computed fields only when requested if ($export['format'] == 'sql' && $field['comp'] && !$export['sql']['cfields']) { continue; } $columns[] = $field['name']; } $quote = identifier_quote($GLOBALS['s_login']['dialect']); $query = 'SELECT ' . $quote . implode($quote . ',' . $quote, $columns) . $quote . ' FROM ' . $table; return $query; }
function init_edit_values($edit_where, $fields) { $values = array(); $quote = identifier_quote($GLOBALS['s_login']['dialect']); $sql = 'SELECT * FROM ' . $quote . $edit_where['table'] . $quote . ' ' . $edit_where['where']; $res = fbird_query($GLOBALS['dbhandle'], $sql) or ib_error(); if ($row = fbird_fetch_assoc($res, IBASE_TEXT)) { fbird_free_result($res); foreach ($fields as $field) { if (isset($field['comp'])) { $values[] = $field['csource']; } else { $values[] = $row[$field['name']]; } } } else { $GLOBALS['ib_error'] = "Query didn't return a result: " . $sql; } return $values; }
function get_where_str($obj) { static $quote; if (!isset($quote)) { $quote = identifier_quote($GLOBALS['s_login']['dialect']); } $where = 'WHERE '; foreach ($GLOBALS['s_fields'][$GLOBALS['s_wt']['table']] as $field) { if (isset($field['primary']) && !empty($field['primary']) || isset($field['unique']) && !empty($field['unique'])) { $where .= $quote . $field['name'] . $quote . '='; $where .= is_number($field) ? $obj->{$field['name']} : "'" . str_replace("'", "''", $obj->{$field['name']}) . "'"; $where .= ' AND '; } } $where = substr($where, 0, -5); $where = urlencode($where); return $where; }
function get_tables() { global $dbhandle, $ib_error, $s_tables, $s_fields, $s_foreigns, $s_primaries, $s_uniques, $s_login; global $s_charsets, $s_tables_counts, $s_views_counts, $s_tables_def, $s_tables_comp; $previous = $s_tables; $s_tables = array(); $s_fields = array(); // get the tablenames, owner and view flag $sql = 'SELECT RDB$RELATION_NAME AS RNAME,' . ' RDB$VIEW_BLR AS VBLR,' . ' RDB$OWNER_NAME AS OWNER' . ' FROM RDB$RELATIONS' . ' WHERE RDB$SYSTEM_FLAG=0' . ' ORDER BY RDB$RELATION_NAME'; $res = @fbird_query($dbhandle, $sql) or ib_error(__FILE__, __LINE__, $sql); if (!is_resource($res)) { return FALSE; } // initialize $s_tables[] while ($row = fbird_fetch_object($res)) { $tablename = trim($row->RNAME); $s_tables[$tablename]['status'] = isset($previous[$tablename]) ? $previous[$tablename]['status'] : 'close'; $s_tables[$tablename]['is_view'] = isset($row->VBLR) && $row->VBLR !== NULL ? TRUE : FALSE; $s_tables[$tablename]['owner'] = trim($row->OWNER); $s_tables[$tablename]['privileges'] = array(); } fbird_free_result($res); unset($previous); // get privileges on tables for the current user and for the role used at login $sql = 'SELECT R.RDB$RELATION_NAME AS RNAME,' . ' P1.RDB$PRIVILEGE AS PRIV' . ' FROM RDB$RELATIONS R' . ' INNER JOIN RDB$USER_PRIVILEGES P1' . ' ON R.RDB$RELATION_NAME=P1.RDB$RELATION_NAME' . ' WHERE R.RDB$SYSTEM_FLAG=0' . " AND (P1.RDB\$USER='******'user'] . "' OR P1.RDB\$USER='******')"; if (!empty($s_login['role'])) { $sql .= ' UNION' . ' SELECT R.RDB$RELATION_NAME AS RNAME,' . ' P2.RDB$PRIVILEGE AS PRIV' . ' FROM RDB$USER_PRIVILEGES P1' . ' INNER JOIN RDB$USER_PRIVILEGES P2 ON P1.RDB$RELATION_NAME=P2.RDB$USER' . ' INNER JOIN RDB$RELATIONS R ON R.RDB$RELATION_NAME=P2.RDB$RELATION_NAME' . " WHERE P1.RDB\$PRIVILEGE='M'" . ' AND R.RDB$SYSTEM_FLAG=0' . " AND P1.RDB\$RELATION_NAME='" . $s_login['role'] . "'" . " AND (P1.RDB\$USER='******'user'] . "' OR P1.RDB\$USER='******')"; } $res = @fbird_query($dbhandle, $sql) or ib_error(__FILE__, __LINE__, $sql); while ($row = fbird_fetch_object($res)) { $s_tables[trim($row->RNAME)]['privileges'][] = trim($row->PRIV); } fbird_free_result($res); // find the check, not null, unique, pk and fk and constraints $sql = 'SELECT RC.RDB$RELATION_NAME TNAME,' . ' RC.RDB$CONSTRAINT_TYPE RTYPE,' . ' RC.RDB$CONSTRAINT_NAME CNAME,' . ' RC.RDB$INDEX_NAME INAME,' . ' CC.RDB$TRIGGER_NAME TRIGNAME,' . ' SE.RDB$FIELD_NAME SENAME,' . ' SE.RDB$FIELD_POSITION POS,' . ' DP.RDB$FIELD_NAME DPNAME' . ' FROM RDB$RELATION_CONSTRAINTS RC' . ' LEFT JOIN RDB$CHECK_CONSTRAINTS CC' . ' ON RC.RDB$CONSTRAINT_NAME=CC.RDB$CONSTRAINT_NAME' . " AND RC.RDB\$CONSTRAINT_TYPE='CHECK'" . ' LEFT JOIN RDB$INDEX_SEGMENTS SE' . ' ON RC.RDB$INDEX_NAME=SE.RDB$INDEX_NAME' . ' LEFT JOIN RDB$DEPENDENCIES DP' . ' ON CC.RDB$TRIGGER_NAME=DP.RDB$DEPENDENT_NAME' . ' ORDER BY RC.RDB$RELATION_NAME'; $res = @fbird_query($dbhandle, $sql) or ib_error(__FILE__, __LINE__, $sql); // reset the index infos $s_foreigns = array(); $s_primaries = array(); $s_uniques = array(); $constraints = array(); while ($row = fbird_fetch_object($res)) { $cname = trim($row->CNAME); switch (trim($row->RTYPE)) { case 'CHECK': $constraints[trim($row->TNAME)][trim($row->DPNAME)]['check'] = $cname; break; case 'UNIQUE': $constraints[trim($row->TNAME)][trim($row->SENAME)]['unique'] = $cname; $s_uniques[$cname]['index'] = trim($row->INAME); $s_uniques[$cname]['cols'] = isset($s_uniques[$cname]['cols']) ? $s_uniques[$cname]['cols']++ : 1; break; case 'FOREIGN KEY': $constraints[trim($row->TNAME)][trim($row->SENAME)]['foreign'] = $cname; $s_foreigns[$cname]['index'] = trim($row->INAME); $s_foreigns[$cname]['cols'] = isset($s_foreigns[$cname]['cols']) ? $s_foreigns[$cname]['cols']++ : 1; break; case 'PRIMARY KEY': $constraints[trim($row->TNAME)][trim($row->SENAME)]['primary'] = $cname; $s_primaries[$cname]['index'] = trim($row->INAME); $s_primaries[$cname]['cols'] = isset($s_primaries[$cname]['cols']) ? $s_primaries[$cname]['cols']++ : 1; break; } } fbird_free_result($res); // debug_var($sql); // debug_var($constraints); // debug_var($s_foreigns); // debug_var($s_primaries); // find the field properties for all non-system tables $sql = 'SELECT DISTINCT R.RDB$FIELD_NAME AS FNAME,' . ' R.RDB$NULL_FLAG AS NFLAG,' . ' R.RDB$DEFAULT_SOURCE AS DSOURCE,' . ' R.RDB$FIELD_POSITION,' . ' R.RDB$RELATION_NAME AS TNAME,' . ' R.RDB$COLLATION_ID AS COLLID,' . ' F.RDB$FIELD_NAME AS DNAME,' . ' F.RDB$FIELD_TYPE AS FTYPE,' . ' F.RDB$FIELD_SUB_TYPE AS STYPE,' . ' F.RDB$FIELD_LENGTH AS FLEN,' . ' F.RDB$COMPUTED_SOURCE AS CSOURCE,' . ' F.RDB$FIELD_PRECISION AS FPREC,' . ' F.RDB$FIELD_SCALE AS FSCALE,' . ' F.RDB$SEGMENT_LENGTH AS SEGLEN,' . ' F.RDB$CHARACTER_SET_ID AS CHARID,' . ' D.RDB$LOWER_BOUND AS LBOUND,' . ' D.RDB$UPPER_BOUND AS UBOUND' . ' FROM RDB$RELATION_FIELDS R ' . ' JOIN RDB$FIELDS F ON R.RDB$FIELD_SOURCE=F.RDB$FIELD_NAME' . ' LEFT JOIN RDB$FIELD_DIMENSIONS D ON R.RDB$FIELD_SOURCE=D.RDB$FIELD_NAME' . ' WHERE F.RDB$SYSTEM_FLAG=0' . ' ORDER BY R.RDB$FIELD_POSITION'; $res = @fbird_query($dbhandle, $sql) or ib_error(__FILE__, __LINE__, $sql); //initialize $s_fields[] $idx = 0; while ($row = fbird_fetch_object($res)) { $tname = trim($row->TNAME); $field = $s_fields[$tname][$idx]['name'] = trim($row->FNAME); if (strpos($row->DNAME, 'RDB$') !== 0) { $s_fields[$tname][$idx]['domain'] = 'Yes'; $s_fields[$tname][$idx]['type'] = trim($row->DNAME); } else { $s_fields[$tname][$idx]['stype'] = isset($row->STYPE) ? $row->STYPE : NULL; $s_fields[$tname][$idx]['type'] = get_datatype($row->FTYPE, $s_fields[$tname][$idx]['stype']); } if ($s_fields[$tname][$idx]['type'] == 'VARCHAR' || $s_fields[$tname][$idx]['type'] == 'CHARACTER') { $s_fields[$tname][$idx]['size'] = $row->FLEN; } // field is defined as NOT NULL if (!empty($row->NFLAG)) { $s_fields[$tname][$idx]['notnull'] = 'Yes'; } // this field is computed if (isset($row->CSOURCE)) { $s_fields[$tname][$idx]['comp'] = 'Yes'; $s_fields[$tname][$idx]['csource'] = FALSE; } // this field has a default value if (isset($row->DSOURCE)) { $s_fields[$tname][$idx]['default'] = 'Yes'; $s_fields[$tname][$idx]['dsource'] = FALSE; } if ($s_fields[$tname][$idx]['type'] == 'DECIMAL' or $s_fields[$tname][$idx]['type'] == 'NUMERIC') { $s_fields[$tname][$idx]['prec'] = $row->FPREC; $s_fields[$tname][$idx]['scale'] = -$row->FSCALE; } if ($s_fields[$tname][$idx]['type'] == 'BLOB') { $s_fields[$tname][$idx]['segsize'] = $row->SEGLEN; } $s_fields[$tname][$idx]['charset'] = isset($row->CHARID) ? $s_charsets[$row->CHARID]['name'] : NULL; $s_fields[$tname][$idx]['collate'] = isset($row->COLLID) && $row->COLLID != 0 && isset($s_charsets[$row->CHARID]['collations'][$row->COLLID]) ? $s_charsets[$row->CHARID]['collations'][$row->COLLID] : NULL; // optional array dimensions if (isset($row->LBOUND)) { $s_fields[$tname][$idx]['lower_bound'] = $row->LBOUND; $s_fields[$tname][$idx]['upper_bound'] = $row->UBOUND; } // column constraints foreach (array('check', 'unique', 'foreign', 'primary') as $ctype) { if (isset($constraints[$tname][$field][$ctype])) { $s_fields[$tname][$idx][$ctype] = $constraints[$tname][$field][$ctype]; } } $idx++; } // debug_var($s_fields); $quote = identifier_quote($s_login['dialect']); foreach ($s_tables as $name => $properties) { if ($s_tables_def == TRUE) { $s_fields = get_table_defaults_sources($name, $s_fields); } if ($s_tables_comp == TRUE) { $s_fields = get_table_computed_sources($name, $s_fields); } if (!in_array('S', $properties['privileges'])) { continue; } if ($properties['is_view'] == FALSE && $s_tables_counts == TRUE || $properties['is_view'] == TRUE && $s_views_counts == TRUE) { $sql = 'SELECT COUNT(*) AS CNT FROM ' . $quote . $name . $quote; $res = fbird_query($dbhandle, $sql) or $ib_error .= fbird_errmsg() . "<br>\n"; if (is_resource($res)) { $row = fbird_fetch_object($res); $s_tables[$name]['count'] = $row->CNT; fbird_free_result($res); } } } return TRUE; }