Exemplo n.º 1
0
void
sql::write_update(
    const binomen &table,
    const abstract_mapper_base &dest,
    const abstract_mapper_base &src,
    optional<column_id> excluded
) {
    vector<const column_mapper *> src_columns;
    src.for_each_column([&](const column_mapper &c) {
        src_columns.push_back(&c);
    });
    auto src_column_iter = src_columns.begin();

    write("UPDATE ");
    write_quoted(table);
    write(" SET ");
    comma_separated_list_scope list_scope(*this);
    dest.for_each_persistent_column([&](const persistent_column_mapper &p) {
        if (p.id() != excluded) {
            list_scope.start_item();
            write_quoted(p.name());
            write(" = ");
            write_evaluation(**src_column_iter++);
        }
    });
}
Exemplo n.º 2
0
void
sql::write_quoted(const binomen &binomen) {
    if (binomen._enclosure) {
        write_quoted(*binomen._enclosure);
        write(".");
    }
    write_quoted(binomen._local);
}
Exemplo n.º 3
0
void
sql::write_persistent_column(const string &table_name, const string &column_name) {
    if (_implicit_table)
        assert(table_name == *_implicit_table);
    else {
        write_quoted(table_name);
        write(".");
    }
    write_quoted(column_name);
}
Exemplo n.º 4
0
void
sql::write_title(const persistent_column_mapper &pcm, optional<column_id> generated_key) {
    const bool is_generated = generated_key == pcm.id();

    write_quoted(pcm.name());
    write(" ");
    write(column_type_name(pcm.get_column_type(is_generated)));
}
Exemplo n.º 5
0
void
sql::write_insert(
    const binomen &table,
    const abstract_mapper_base &value_mapper,
    optional<column_id> excluded
) {
    write("INSERT INTO ");
    write_quoted(table);
    write_parenthesized_persistent_column_list(value_mapper, excluded);
}
Exemplo n.º 6
0
void
sql::write_unconstrain_as_not_null(const binomen &table, const abstract_mapper_base &mapper) {
    write_alter_table(table);
    comma_separated_list_scope list_scope(*this);
    mapper.for_each_persistent_column([&](const persistent_column_mapper &p) {
        list_scope.start_item();
        write("ALTER COLUMN ");
        write_quoted(p.name());
        write(" DROP NOT NULL");
    });
}
Exemplo n.º 7
0
void
sql::write_update(
    const binomen &table,
    const abstract_mapper_base &dest,
    const row &src,
    optional<column_id> excluded
) {
    // TODO: factor out the code in common with the other overload
    //
    write("UPDATE ");
    write_quoted(table);
    write(" SET ");
    comma_separated_list_scope list_scope(*this);
    dest.for_each_persistent_column([&](const persistent_column_mapper &p) {
        if (p.id() != excluded) {
            list_scope.start_item();
            write_quoted(p.name());
            write(" = ");
            write_value(*src.find_cell(p.name()));
        }
    });
}
Exemplo n.º 8
0
void
sql::write_parenthesized_persistent_column_list(const abstract_mapper_base &mapper, optional<column_id> excluded) {
    write("(");
    comma_separated_list_scope list_scope(*this);
    mapper.for_each_persistent_column(
        [&](const persistent_column_mapper &p) {
            if (p.id() != excluded) {
                list_scope.start_item();
                write_quoted(p.name());
            }
        }
    );
    write(")");
}
Exemplo n.º 9
0
void
sql::write_create_table(
    const binomen &table,
    const abstract_mapper_base &value_mapper,
    const abstract_mapper_base &key_mapper,
    optional<column_id> generated_key,
    const vector<foreign_spec> &foreign_specs
) {
    write("CREATE TABLE ");
    write_quoted(table);
    write(" (");
    write_titles(value_mapper, generated_key);
    write(", PRIMARY KEY ");
    write_parenthesized_persistent_column_list(key_mapper);
    for (const foreign_spec &fs: foreign_specs) {
        write(", FOREIGN KEY ");
        write_parenthesized_persistent_column_list(*fs._foreign);
        write(" REFERENCES ");
        write_quoted(fs._target_table);
        write_parenthesized_persistent_column_list(*fs._target_mapper);
    }
    write(")");
}
Exemplo n.º 10
0
void
sql::write_function_call(const string &function_name, const vector<const column_mapper *> &args) {
    // If function_name contains caps then it must be quoted.  Otherwise it doesn't
    // need to be quoted and, in case of certain built-ins may need not to be.
    //
    bool got_cap = false;
    for (char c: function_name) if (isupper(c)) got_cap = true;
    if (got_cap)
        write(function_name);
    else
        write_quoted(function_name);
    
    write("(");
    comma_separated_list_scope list_scope(*this);
    for (const auto arg: args) {
        list_scope.start_item();
        write_evaluation(*arg);
    }
    write(")");
}
Exemplo n.º 11
0
/** ini_puts()
 * \param Section     the name of the section to write the string in
 * \param Key         the name of the entry to write, or NULL to erase all keys in the section
 * \param Value       a pointer to the buffer the string, or NULL to erase the key
 * \param Filename    the name and full path of the .ini file to write to
 *
 * \return            1 if successful, otherwise 0
 */
int ini_puts(const TCHAR *Section, const TCHAR *Key, const TCHAR *Value, const TCHAR *Filename)
{
  INI_FILETYPE rfp;
  INI_FILETYPE wfp;
  TCHAR *sp, *ep;
  TCHAR LocalBuffer[INI_BUFFERSIZE];
  int len, match, count;

  assert(Filename!=NULL);
  if (!ini_openread(Filename, &rfp)) {
    /* If the .ini file doesn't exist, make a new file */
    if (Key!=NULL && Value!=NULL) {
      if (!ini_openwrite(Filename, &wfp))
        return 0;
      writesection(LocalBuffer, Section, &wfp);
      writekey(LocalBuffer, Key, Value, &wfp);
      ini_close(&wfp);
    } /* if */
    return 1;
  } /* if */

  /* If parameters Key and Value are valid (so this is not an "erase" request)
   * and the setting already exists and it already has the correct value, do
   * nothing. This early bail-out avoids rewriting the INI file for no reason.
   */
  if (Key!=NULL && Value!=NULL) {
    match = getkeystring(&rfp, Section, Key, -1, -1, LocalBuffer, sizearray(LocalBuffer));
    if (match && _tcscmp(LocalBuffer,Value)==0) {
      ini_close(&rfp);
      return 1;
    } /* if */
    /* key not found, or different value -> proceed (but rewind the input file first) */
    ini_rewind(&rfp);
  } /* if */

  /* Get a temporary file name to copy to. Use the existing name, but with
   * the last character set to a '~'.
   */
  ini_tempname(LocalBuffer, Filename, INI_BUFFERSIZE);
  if (!ini_openwrite(LocalBuffer, &wfp)) {
    ini_close(&rfp);
    return 0;
  } /* if */

  /* Move through the file one line at a time until a section is
   * matched or until EOF. Copy to temp file as it is read.
   */
  count = 0;
  len = (Section != NULL) ? _tcslen(Section) : 0;
  if (len > 0) {
    do {
      if (!ini_read(LocalBuffer, INI_BUFFERSIZE, &rfp)) {
        /* Failed to find section, so add one to the end */
        if (Key!=NULL && Value!=NULL) {
            ini_write(INI_LINETERM, &wfp);  /* force a new line (there may not have been one) behind the last line of the INI file */
            writesection(LocalBuffer, Section, &wfp);
            writekey(LocalBuffer, Key, Value, &wfp);
        } /* if */
        /* Clean up and rename */
        ini_close(&rfp);
        ini_close(&wfp);
        ini_remove(Filename);
        ini_tempname(LocalBuffer, Filename, INI_BUFFERSIZE);
        ini_rename(LocalBuffer, Filename);
        return 1;
      } /* if */
      /* Copy the line from source to dest, but not if this is the section that
       * we are looking for and this section must be removed
       */
      sp = skipleading(LocalBuffer);
      ep = _tcschr(sp, ']');
      match = (*sp == '[' && ep != NULL && (int)(ep-sp-1) == len && _tcsnicmp(sp + 1,Section,len) == 0);
      if (!match || Key!=NULL) {
        /* Remove blank lines, but insert a blank line (possibly one that was
         * removed on the previous iteration) before a new section. This creates
         * "neat" INI files.
         */
        if (_tcslen(sp) > 0) {
          if (*sp == '[' && count > 0)
            ini_write(INI_LINETERM, &wfp);
          ini_write(sp, &wfp);
          count++;
        } /* if */
      } /* if */
    } while (!match);
  } /* if */

  /* Now that the section has been found, find the entry. Stop searching
   * upon leaving the section's area. Copy the file as it is read
   * and create an entry if one is not found.
   */
  len = (Key!=NULL) ? _tcslen(Key) : 0;
  for( ;; ) {
    if (!ini_read(LocalBuffer, INI_BUFFERSIZE, &rfp)) {
      /* EOF without an entry so make one */
      if (Key!=NULL && Value!=NULL) {
          ini_write(INI_LINETERM, &wfp);  /* force a new line (there may not have been one) behind the last line of the INI file */
          writekey(LocalBuffer, Key, Value, &wfp);
      } /* if */
      /* Clean up and rename */
      ini_close(&rfp);
      ini_close(&wfp);
      ini_remove(Filename);
      ini_tempname(LocalBuffer, Filename, INI_BUFFERSIZE);
      ini_rename(LocalBuffer, Filename);
      return 1;
    } /* if */
    sp = skipleading(LocalBuffer);
    ep = _tcschr(sp, '='); /* Parse out the equal sign */
    if (ep == NULL)
      ep = _tcschr(sp, ':');
    match = (ep != NULL && (int)(skiptrailing(ep,sp)-sp) == len && _tcsnicmp(sp,Key,len) == 0);
    if ((Key!=NULL && match) || *sp == '[')
      break;  /* found the key, or found a new section */
    /* in the section that we re-write, do not copy empty lines */
    if (Key!=NULL && _tcslen(sp) > 0)
      ini_write(sp, &wfp);
  } /* for */
  if (*sp == '[') {
    /* found start of new section, the key was not in the specified
     * section, so we add it just before the new section
     */
    if (Key!=NULL && Value!=NULL) {
      /* We cannot use "writekey()" here, because we need to preserve the
       * contents of LocalBuffer.
       */
      ini_write(Key, &wfp);
      ini_write("=", &wfp);
      write_quoted(Value, &wfp);
      ini_write(INI_LINETERM INI_LINETERM, &wfp); /* put a blank line between the current and the next section */
    } /* if */
    /* write the new section header that we read previously */
    ini_write(sp, &wfp);
  } else {
    /* We found the key; ignore the line just read (with the key and
     * the current value) and write the key with the new value.
     */
    if (Key!=NULL && Value!=NULL)
      writekey(LocalBuffer, Key, Value, &wfp);
  } /* if */
  /* Copy the rest of the INI file (removing empty lines, except before a section) */
  while (ini_read(LocalBuffer, INI_BUFFERSIZE, &rfp)) {
    sp = skipleading(LocalBuffer);
    if (_tcslen(sp) > 0) {
      if (*sp == '[')
        ini_write(INI_LINETERM, &wfp);
      ini_write(sp, &wfp);
    } /* if */
  } /* while */
  /* Clean up and rename */
  ini_close(&rfp);
  ini_close(&wfp);
  ini_remove(Filename);
  ini_tempname(LocalBuffer, Filename, INI_BUFFERSIZE);
  ini_rename(LocalBuffer, Filename);
  return 1;
}
Exemplo n.º 12
0
 bool write_string(const char* name, const char* value) {
     return write_attribute(name) &&
            write_quoted(value) &&
            next();
 }
Exemplo n.º 13
0
void
sql::write_alter_table(const binomen &table) {
    write("ALTER TABLE ");
    write_quoted(table);
    write(" ");
}
Exemplo n.º 14
0
void
sql::write_drop_table_if_exists(const binomen &table) {
    write("DROP TABLE IF EXISTS ");
    write_quoted(table);
}
Exemplo n.º 15
0
void
sql::write_drop_table(const binomen &table) {
    write("DROP TABLE ");
    write_quoted(table);
}
Exemplo n.º 16
0
void
sql::write_rename_table(const binomen &old, const string &new_local) {
    write_alter_table(old);
    write("RENAME TO ");
    write_quoted(new_local);
}
Exemplo n.º 17
0
void
sql::write_set_search_path(const string &schema_name) {
    write("SET search_path TO ");
    write_quoted(schema_name);
}
Exemplo n.º 18
0
void
sql::write_delete_from(const binomen &table) {
    write("DELETE FROM ");
    write_quoted(table);
}
Exemplo n.º 19
0
 bool write_attribute(const char* name) {
     return
             write_quoted(name) &&
             write(':');
 }
Exemplo n.º 20
0
 bool end_list() {
     return write_attribute("_") &&
            write_quoted("");
 }
Exemplo n.º 21
0
void
sql::write_select_none(const binomen &table) {
    write("SELECT * FROM ");
    write_quoted(table);
    write(" WHERE FALSE");
}