Exemple #1
0
function user_read_base($fields = "*", $condition = "", $get_type = "", $num = "", $by_col = "user_id", $order = "desc", $cache = "", $cache_key = "")
{
    global $allow_cols;
    global $tablePreStr;
    $t_user = $tablePreStr . "users";
    $fields_str = '';
    $result_rs = array();
    $dbo = new dbex();
    dbplugin('r');
    $allow_cols = array("user_id", "user_marry", "user_email", "user_name", "user_sex", "user_call", "birth_province", "birth_city", "reside_province", "reside_city", "user_ico", "user_add_time", "birth_year", "birth_month", "birth_day", "user_blood", "user_qq", "creat_group", "join_group", "guest_num", "integral", "lastlogin_datetime", "hidden_pals_id", "hidden_type_id", "inputmess_limit", "palsreq_limit", "is_pass", "access_limit", "access_questions", "access_answers", "forget_pass");
    $limit = $num ? " limit {$num} " : "";
    $by_col = $by_col ? " {$by_col} " : " user_id ";
    $order = $order ? $order : "desc";
    $get_type = $get_type == 'getRow' ? "getRow" : "getRs";
    if ($fields == "*") {
        $fields = join(",", $allow_cols);
    } elseif (strpos($fields, ",")) {
        $fields = check_item($fields, $allow_cols);
    } else {
        if (!in_array($fields, $allow_cols)) {
            $fields = 'user_id';
        }
    }
    $sql = " select {$fields} from {$t_user} where is_pass=1 {$condition} order by {$by_col} {$order} {$limit} ";
    if (empty($result_rs)) {
        $result_rs = $dbo->{$get_type}($sql);
    }
    return $result_rs;
}
function generate_content($name, array $table, array $types, $primaryKey, $addKey = true)
{
    $haveHeader = false;
    $sql = "";
    $results = array();
    foreach ($table as $key => $entity) {
        $data = $addKey ? array('id' => $key) : array();
        $data = array_merge($data, get_object_vars($entity));
        if (!$haveHeader) {
            generate_table($name, array_keys($data), $types, $primaryKey);
            $sql = "INSERT INTO {$name} (" . implode(", ", array_keys($data)) . ") VALUES\n";
            $haveHeader = true;
        }
        $data = array_values($data);
        assert(count($data) == count($types) && count($data) >= 1);
        $r = "";
        $isString = $types[0] != 'INT';
        check_item($data[0], $isString);
        if ($isString) {
            $r .= "\"{$data[0]}\"";
        } else {
            $r .= $data[0];
        }
        for ($i = 1; $i < count($data); ++$i) {
            $r .= ", ";
            $isString = $types[$i] != 'INT';
            check_item($data[$i], $isString);
            if ($isString) {
                $r .= "\"{$data[$i]}\"";
            } else {
                $r .= $data[$i];
            }
        }
        $results[] = "({$r})";
    }
    $sql .= implode(",\n", $results) . ";\n";
    fwrite(STDOUT, $sql);
}