[darwinbuild-changes] [719] branches/PR-7489777/darwinup
source_changes at macosforge.org
source_changes at macosforge.org
Wed Feb 24 08:58:27 PST 2010
Revision: 719
http://trac.macosforge.org/projects/darwinbuild/changeset/719
Author: wsiegrist at apple.com
Date: 2010-02-24 08:58:27 -0800 (Wed, 24 Feb 2010)
Log Message:
-----------
Cleanup, document, and reorganize Table
Modified Paths:
--------------
branches/PR-7489777/darwinup/Column.cpp
branches/PR-7489777/darwinup/Database.cpp
branches/PR-7489777/darwinup/Table.cpp
branches/PR-7489777/darwinup/Table.h
branches/PR-7489777/darwinup/Utils.cpp
branches/PR-7489777/darwinup/Utils.h
Modified: branches/PR-7489777/darwinup/Column.cpp
===================================================================
--- branches/PR-7489777/darwinup/Column.cpp 2010-02-24 16:18:52 UTC (rev 718)
+++ branches/PR-7489777/darwinup/Column.cpp 2010-02-24 16:58:27 UTC (rev 719)
@@ -33,6 +33,7 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
+
#include "Column.h"
Column::Column(const char* name, uint32_t type) {
Modified: branches/PR-7489777/darwinup/Database.cpp
===================================================================
--- branches/PR-7489777/darwinup/Database.cpp 2010-02-24 16:18:52 UTC (rev 718)
+++ branches/PR-7489777/darwinup/Database.cpp 2010-02-24 16:58:27 UTC (rev 719)
@@ -184,7 +184,7 @@
int Database::create_tables() {
int res = SQLITE_OK;
for (uint32_t i=0; i<m_table_count; i++) {
- res = this->sql_once(m_tables[i]->create());
+ res = this->execute(m_tables[i]->create(this->m_db));
if (res!=SQLITE_OK) {
fprintf(stderr, "Error: sql error trying to create table: %s: %s\n",
m_tables[i]->name(), m_error);
Modified: branches/PR-7489777/darwinup/Table.cpp
===================================================================
--- branches/PR-7489777/darwinup/Table.cpp 2010-02-24 16:18:52 UTC (rev 718)
+++ branches/PR-7489777/darwinup/Table.cpp 2010-02-24 16:58:27 UTC (rev 719)
@@ -33,34 +33,13 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
+
#include "Table.h"
#include "Database.h"
-// how much we grow by when we need more space
-#define REALLOC_FACTOR 4
-
-Table::Table() {
- m_column_max = 1;
- m_column_count = 0;
- m_columns = (Column**)malloc(sizeof(Column*) * m_column_max);
- m_columns_size = 0;
- m_result_max = 1;
- m_result_count = 0;
- m_results = (uint8_t**)malloc(sizeof(uint8_t*) * m_result_max);
- m_name = strdup("unnamed_table");
- m_create_sql = NULL;
- m_insert_sql = NULL;
- m_update_sql = NULL;
- m_delete_sql = NULL;
- m_prepared_insert = NULL;
- m_prepared_update = NULL;
- m_prepared_delete = NULL;
-
-}
-
Table::Table(const char* name) {
- m_column_max = 1;
+ m_column_max = 2;
m_column_count = 0;
m_columns = (Column**)malloc(sizeof(Column*) * m_column_max);
m_columns_size = 0;
@@ -104,20 +83,10 @@
}
-
const char* Table::name() {
return m_name;
}
-uint32_t Table::row_size() {
- return m_columns_size;
-}
-
-const Column** Table::columns() {
- return (const Column**)m_columns;
-}
-
-
int Table::add_column(Column* c) {
// accumulate offsets for columns in m_columns_size
c->m_offset = this->m_columns_size;
@@ -137,10 +106,22 @@
return 0;
}
-int Table::offset(int column) {
- return this->m_columns[column]->offset();
+Column* Table::column(uint32_t index) {
+ if (index < m_column_count) {
+ return this->m_columns[index];
+ } else {
+ return NULL;
+ }
}
+int Table::offset(uint32_t index) {
+ return this->m_columns[index]->offset();
+}
+
+uint32_t Table::row_size() {
+ return m_columns_size;
+}
+
uint8_t* Table::alloc_result() {
if (m_result_count >= m_result_max) {
m_results = (uint8_t**)realloc(m_results, m_result_max * sizeof(uint8_t*) * REALLOC_FACTOR);
@@ -155,24 +136,6 @@
return m_results[m_result_count-1];
}
-int Table::free_row(uint8_t* row) {
- uint8_t* current = row;
- void* ptr;
- for (uint32_t i=0; i < m_column_count; i++) {
- switch (m_columns[i]->type()) {
- case SQLITE_INTEGER:
- current += sizeof(uint64_t);
- // nothing to free
- break;
- default:
- memcpy(&ptr, current, sizeof(void*));
- free(ptr);
- current += sizeof(void*);
- }
- }
- return 0;
-}
-
int Table::free_result(uint8_t* result) {
for (uint32_t i=0; i < m_result_count; i++) {
// look for matching result
@@ -192,54 +155,6 @@
return 0;
}
-void Table::dump_results() {
- fprintf(stderr, "====================================================================\n");
- for (uint32_t i=0; i < m_result_count; i++) {
- fprintf(stderr, "%p %u:\n", m_results[i], i);
- __data_hex(m_results[i], 48);
- }
- fprintf(stderr, "====================================================================\n");
-}
-
-char* Table::create() {
- if (!m_create_sql) {
- uint32_t i = 0;
-
- // size of "create table ( );" plus table name, plus 1 for each column to separate
- size_t size = strlen(m_name) + 22 + m_column_count;
- for (i=0; i<m_column_count; i++) {
- // size for column spec
- size += strlen(m_columns[i]->create());
- // size for create index query
- size += 26 + 2*strlen(m_columns[i]->name()) + 2*strlen(m_name);
- }
-
- // create creation sql
- m_create_sql = (char*)malloc(size);
- strlcpy(m_create_sql, "CREATE TABLE ", size);
- strlcat(m_create_sql, m_name, size);
- strlcat(m_create_sql, " (", size);
- // get creation sql for each column
- for (i=0; i<m_column_count; i++) {
- if (i) strlcat(m_create_sql, ", ", size); // comma separate after 0th column
- strlcat(m_create_sql, m_columns[i]->create(), size);
- }
- strlcat(m_create_sql, "); ", size);
-
- for (i=0; i<m_column_count; i++) {
- if (m_columns[i]->is_index()) {
- char* buf;
- asprintf(&buf, "CREATE INDEX %s_%s ON %s (%s);",
- m_name, m_columns[i]->name(), m_name, m_columns[i]->name());
- strlcat(m_create_sql, buf, size);
- free(buf);
- }
- }
- }
-
- return m_create_sql;
-}
-
#define __alloc_stmt_query \
size_t size = 256; \
size_t used = 0; \
@@ -299,7 +214,54 @@
return NULL; \
}
+sqlite3_stmt* Table::create(sqlite3* db) {
+ size_t size;
+ if (!m_create_sql) {
+ uint32_t i = 0;
+
+ // size of "create table ( );" plus table name, plus 1 for each column to separate
+ size = strlen(m_name) + 22 + m_column_count;
+ for (i=0; i<m_column_count; i++) {
+ // size for column spec
+ size += strlen(m_columns[i]->create());
+ // size for create index query
+ size += 26 + 2*strlen(m_columns[i]->name()) + 2*strlen(m_name);
+ }
+
+ // create creation sql
+ m_create_sql = (char*)malloc(size);
+ strlcpy(m_create_sql, "CREATE TABLE ", size);
+ strlcat(m_create_sql, m_name, size);
+ strlcat(m_create_sql, " (", size);
+ // get creation sql for each column
+ for (i=0; i<m_column_count; i++) {
+ if (i) strlcat(m_create_sql, ", ", size); // comma separate after 0th column
+ strlcat(m_create_sql, m_columns[i]->create(), size);
+ }
+ strlcat(m_create_sql, "); ", size);
+
+ for (i=0; i<m_column_count; i++) {
+ if (m_columns[i]->is_index()) {
+ char* buf;
+ asprintf(&buf, "CREATE INDEX %s_%s ON %s (%s);",
+ m_name, m_columns[i]->name(), m_name, m_columns[i]->name());
+ strlcat(m_create_sql, buf, size);
+ free(buf);
+ }
+ }
+ }
+ sqlite3_stmt* stmt = (sqlite3_stmt*)malloc(sizeof(sqlite3_stmt*));
+ int res = sqlite3_prepare_v2(db, m_create_sql, size, &stmt, NULL); \
+ if (res != SQLITE_OK) { \
+ fprintf(stderr, "Error: unable to prepare CREATE statement: %s\n",
+ sqlite3_errmsg(db)); \
+ return NULL; \
+ }
+
+ return stmt;
+}
+
sqlite3_stmt* Table::count(sqlite3* db) {
sqlite3_stmt* stmt = (sqlite3_stmt*)malloc(sizeof(sqlite3_stmt*));
char* query;
@@ -483,20 +445,6 @@
return m_prepared_insert;
}
-
-Column* Table::column(uint32_t index) {
- if (index < m_column_count) {
- return this->m_columns[index];
- } else {
- return NULL;
- }
-}
-
-uint32_t Table::column_count() {
- return this->m_column_count;
-}
-
-
sqlite3_stmt* Table::del(sqlite3* db) {
// we only need to prepare once, return if we already have it
if (m_prepared_delete) return m_prepared_delete;
@@ -547,3 +495,38 @@
return stmt;
}
+
+const Column** Table::columns() {
+ return (const Column**)m_columns;
+}
+
+uint32_t Table::column_count() {
+ return this->m_column_count;
+}
+
+int Table::free_row(uint8_t* row) {
+ uint8_t* current = row;
+ void* ptr;
+ for (uint32_t i=0; i < m_column_count; i++) {
+ switch (m_columns[i]->type()) {
+ case SQLITE_INTEGER:
+ current += sizeof(uint64_t);
+ // nothing to free
+ break;
+ default:
+ memcpy(&ptr, current, sizeof(void*));
+ free(ptr);
+ current += sizeof(void*);
+ }
+ }
+ return 0;
+}
+
+void Table::dump_results(FILE* f) {
+ fprintf(f, "====================================================================\n");
+ for (uint32_t i=0; i < m_result_count; i++) {
+ fprintf(f, "%p %u:\n", m_results[i], i);
+ __data_hex(f, m_results[i], 48);
+ }
+ fprintf(f, "====================================================================\n");
+}
Modified: branches/PR-7489777/darwinup/Table.h
===================================================================
--- branches/PR-7489777/darwinup/Table.h 2010-02-24 16:18:52 UTC (rev 718)
+++ branches/PR-7489777/darwinup/Table.h 2010-02-24 16:58:27 UTC (rev 719)
@@ -35,46 +35,65 @@
#include <stdint.h>
#include <sqlite3.h>
+
#include "Column.h"
struct Table {
- Table();
Table(const char* name);
virtual ~Table();
const char* name();
- const Column** columns();
+
+ // Column handling
+ int add_column(Column*);
Column* column(uint32_t index);
- uint32_t column_count();
- int offset(int column);
+ // get the result record offset for column at index
+ int offset(uint32_t index);
+ // get total size of result record
+ uint32_t row_size();
+ // Result record handling
uint8_t* alloc_result();
int free_result(uint8_t* result);
- int add_column(Column*);
- uint32_t row_size();
-
- // return SQL statements for this table
- char* create();
-
+ /**
+ * sql statement generators
+ *
+ * - order is either ORDER_BY_ASC or ORDER_BY_DESC
+ * - count parameters should be the number of items in the va_list
+ * - args should be a va_list with sets of 3 parameters for WHERE clause:
+ * Column*, char, value
+ *
+ * - Column* is the column to match against
+ * - char is how to compare, one of '=', '!', '>', or '<'
+ * - value is the value to match, which is ignored by these API
+ * since they leave placeholders instead
+ *
+ */
+ sqlite3_stmt* create(sqlite3* db);
sqlite3_stmt* count(sqlite3* db);
sqlite3_stmt* count(sqlite3* db, uint32_t count, va_list args);
- sqlite3_stmt* get_column(sqlite3* db, Column* value_column, uint32_t count,
- va_list args);
+ sqlite3_stmt* get_column(sqlite3* db, Column* value_column,
+ uint32_t count, va_list args);
sqlite3_stmt* get_row(sqlite3* db, uint32_t count, va_list args);
sqlite3_stmt* get_row_ordered(sqlite3* db, Column* order_by, int order,
uint32_t count, va_list args);
- sqlite3_stmt* update_value(sqlite3* db, Column* value_column, uint32_t count,
- va_list args);
+ sqlite3_stmt* update_value(sqlite3* db, Column* value_column,
+ uint32_t count, va_list args);
+ sqlite3_stmt* update(sqlite3* db);
sqlite3_stmt* insert(sqlite3* db);
- sqlite3_stmt* update(sqlite3* db);
sqlite3_stmt* del(sqlite3* db);
sqlite3_stmt* del(sqlite3* db, uint32_t count, va_list args);
protected:
- void dump_results();
+
+ const Column** columns();
+ uint32_t column_count();
+
+ // free the out-of-band columns (text, blob) from a result record
int free_row(uint8_t* row);
+ void dump_results(FILE* f);
char* m_name;
Modified: branches/PR-7489777/darwinup/Utils.cpp
===================================================================
--- branches/PR-7489777/darwinup/Utils.cpp 2010-02-24 16:18:52 UTC (rev 718)
+++ branches/PR-7489777/darwinup/Utils.cpp 2010-02-24 16:58:27 UTC (rev 719)
@@ -246,20 +246,20 @@
return NULL;
}
-void __data_hex(uint8_t* data, uint32_t size) {
+void __data_hex(FILE* f, uint8_t* data, uint32_t size) {
if (!size) return;
for (uint32_t i=0; i < size; i++) {
if (!(i%8)) {
- if (i<10) fprintf(stderr, " ");
- fprintf(stderr, "%d", i);
+ if (i<10) fprintf(f, " ");
+ fprintf(f, "%d", i);
} else {
- fprintf(stderr, " ");
+ fprintf(f, " ");
}
}
- fprintf(stderr, "\n");
+ fprintf(f, "\n");
for (uint32_t i=0; i < size; i++) {
- fprintf(stderr, "%02x", data[i]);
+ fprintf(f, "%02x", data[i]);
}
- fprintf(stderr, "\n");
+ fprintf(f, "\n");
}
Modified: branches/PR-7489777/darwinup/Utils.h
===================================================================
--- branches/PR-7489777/darwinup/Utils.h 2010-02-24 16:18:52 UTC (rev 718)
+++ branches/PR-7489777/darwinup/Utils.h 2010-02-24 16:58:27 UTC (rev 719)
@@ -62,7 +62,7 @@
char* fetch_url(const char* srcpath, const char* dstpath);
char* fetch_userhost(const char* srcpath, const char* dstpath);
-void __data_hex(uint8_t* data, uint32_t size);
+void __data_hex(FILE* f, uint8_t* data, uint32_t size);
inline int INFO_TEST(uint32_t word, uint32_t flag) { return ((word & flag) != 0); }
inline int INFO_SET(uint32_t word, uint32_t flag) { return (word | flag); }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/darwinbuild-changes/attachments/20100224/38a0a550/attachment-0001.html>
More information about the darwinbuild-changes
mailing list