[darwinbuild-changes] [679] branches/PR-7489777/darwinup
source_changes at macosforge.org
source_changes at macosforge.org
Fri Jan 29 17:06:16 PST 2010
Revision: 679
http://trac.macosforge.org/projects/darwinbuild/changeset/679
Author: wsiegrist at apple.com
Date: 2010-01-29 17:06:14 -0800 (Fri, 29 Jan 2010)
Log Message:
-----------
Implementation of construction and destruction of database system
Modified Paths:
--------------
branches/PR-7489777/darwinup/Column.cpp
branches/PR-7489777/darwinup/Column.h
branches/PR-7489777/darwinup/Database.cpp
branches/PR-7489777/darwinup/Database.h
branches/PR-7489777/darwinup/Table.cpp
branches/PR-7489777/darwinup/Table.h
Modified: branches/PR-7489777/darwinup/Column.cpp
===================================================================
--- branches/PR-7489777/darwinup/Column.cpp 2010-01-29 22:09:45 UTC (rev 678)
+++ branches/PR-7489777/darwinup/Column.cpp 2010-01-30 01:06:14 UTC (rev 679)
@@ -30,5 +30,47 @@
* @APPLE_BSD_LICENSE_HEADER_END@
*/
+#include <string.h>
+#include <stdlib.h>
#include "Column.h"
+Column::Column() {
+ Column("unnamed_column", SQLITE_INTEGER);
+}
+
+Column::Column(const char* name, uint32_t type) {
+ Column(name, type, false, false, false);
+}
+
+Column::Column(const char* name, uint32_t type, bool is_index, bool is_pk, bool is_unique) {
+ m_name = strdup(name);
+ m_type = type;
+ m_is_index = is_index;
+ m_is_pk = is_pk;
+ m_is_unique = is_unique;
+}
+
+Column::~Column() {
+ free(m_name);
+}
+
+const char* Column::name() {
+ return m_name;
+}
+
+const int Column::type() {
+ return m_type;
+}
+
+const bool Column::is_index() {
+ return m_is_index;
+}
+
+const bool Column::is_pk() {
+ return m_is_pk;
+}
+
+const bool Column::is_unique() {
+ return m_is_unique;
+}
+
Modified: branches/PR-7489777/darwinup/Column.h
===================================================================
--- branches/PR-7489777/darwinup/Column.h 2010-01-29 22:09:45 UTC (rev 678)
+++ branches/PR-7489777/darwinup/Column.h 2010-01-30 01:06:14 UTC (rev 679)
@@ -30,25 +30,26 @@
* @APPLE_BSD_LICENSE_HEADER_END@
*/
+#include <stdint.h>
#include <sqlite3.h>
struct Column {
Column();
- Column(const char* name, int type);
- Column(const char* name, int type, bool is_index, bool is_pk, bool is_unique);
+ Column(const char* name, uint32_t type);
+ Column(const char* name, uint32_t type, bool is_index, bool is_pk, bool is_unique);
virtual ~Column();
- const char* name();
- const int type();
+ const char* name();
+ const int type();
- bool is_index();
- bool is_pk();
- bool is_unique();
+ const bool is_index();
+ const bool is_pk();
+ const bool is_unique();
protected:
char* m_name;
- int m_type; // SQLITE_* type definition
+ uint32_t m_type; // SQLITE_* type definition
bool m_is_index;
bool m_is_pk;
bool m_is_unique;
Modified: branches/PR-7489777/darwinup/Database.cpp
===================================================================
--- branches/PR-7489777/darwinup/Database.cpp 2010-01-29 22:09:45 UTC (rev 678)
+++ branches/PR-7489777/darwinup/Database.cpp 2010-01-30 01:06:14 UTC (rev 679)
@@ -30,5 +30,83 @@
* @APPLE_BSD_LICENSE_HEADER_END@
*/
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
#include "Database.h"
+Database::Database() {
+ // XXX: make the initial allocation for 2 to tailor to darwinup usage
+ m_table_max = 1;
+ m_table_count = 0;
+ m_tables = (Table**)malloc(sizeof(Table*) * m_table_max);
+ m_db = NULL;
+ m_path = NULL;
+}
+
+Database::Database(const char* path) {
+ m_table_max = 1;
+ m_table_count = 0;
+ m_tables = (Table**)malloc(sizeof(Table*) * m_table_max);
+ m_db = NULL;
+ m_path = strdup(path);
+}
+
+Database::~Database() {
+ for (uint32_t i = 0; i < m_table_count; i++) {
+ delete m_tables[i];
+ }
+ free(m_tables);
+ free(m_path);
+}
+
+
+bool Database::add_table(Table* t) {
+ if (m_table_count >= m_table_max) {
+ m_tables = (Table**)realloc(m_tables, m_table_max * sizeof(Table*) * 4);
+ if (!m_tables) {
+ fprintf(stderr, "Error: unable to reallocate memory to add a table\n");
+ return false;
+ }
+ m_table_max *= 4;
+ }
+ m_tables[m_table_count++] = t;
+
+ return true;
+}
+
+
+DarwinupDatabase::DarwinupDatabase() {
+ m_path = strdup("");
+}
+
+DarwinupDatabase::DarwinupDatabase(const char* path) {
+ m_path = strdup(path);
+
+ Table* archives = new Table("archives");
+ assert(archives->add_column(new Column("serial", SQLITE_INTEGER, false, true, false)));
+ assert(archives->add_column(new Column("uuid", SQLITE_BLOB, true, false, true)));
+ assert(archives->add_column(new Column("name", SQLITE3_TEXT)));
+ assert(archives->add_column(new Column("date_added", SQLITE_INTEGER)));
+ assert(archives->add_column(new Column("active", SQLITE_INTEGER)));
+ assert(archives->add_column(new Column("info", SQLITE_INTEGER)));
+ assert(add_table(archives));
+
+ Table* files = new Table("files");
+ assert(files->add_column(new Column("serial", SQLITE_INTEGER, false, true, false)));
+ assert(files->add_column(new Column("archive", SQLITE_INTEGER)));
+ assert(files->add_column(new Column("info", SQLITE_INTEGER)));
+ assert(files->add_column(new Column("mode", SQLITE_INTEGER)));
+ assert(files->add_column(new Column("uid", SQLITE_INTEGER)));
+ assert(files->add_column(new Column("gid", SQLITE_INTEGER)));
+ assert(files->add_column(new Column("size", SQLITE_INTEGER)));
+ assert(files->add_column(new Column("digest", SQLITE_BLOB)));
+ assert(files->add_column(new Column("path", SQLITE3_TEXT)));
+ assert(add_table(files));
+}
+
+DarwinupDatabase::~DarwinupDatabase() {
+}
+
+
Modified: branches/PR-7489777/darwinup/Database.h
===================================================================
--- branches/PR-7489777/darwinup/Database.h 2010-01-29 22:09:45 UTC (rev 678)
+++ branches/PR-7489777/darwinup/Database.h 2010-01-30 01:06:14 UTC (rev 679)
@@ -30,7 +30,9 @@
* @APPLE_BSD_LICENSE_HEADER_END@
*/
+#include <stdint.h>
#include <sqlite3.h>
+#include "Table.h"
/**
*
@@ -50,15 +52,17 @@
const char* get_column(const char* table, const char* column, const char* where);
const char* get_all(const char* table, const char* where);
- int count(const char* table, const char* where);
+ uint32_t count(const char* table, const char* where);
- bool update(const char* table, const char* set, const char* where, int &count);
- bool del(const char* table, const char* where, int &count);
+ bool update(const char* table, const char* set, const char* where, uint32_t &count);
+ bool del(const char* table, const char* where, uint32_t &count);
bool insert(const char* table, const char* columns, const char* values);
bool begin_transaction();
bool rollback_transaction();
bool commit_transaction();
+
+ bool add_table(Table*);
protected:
@@ -67,6 +71,8 @@
char* m_path;
sqlite3* m_db;
Table** m_tables;
+ uint32_t m_table_count;
+ uint32_t m_table_max;
};
Modified: branches/PR-7489777/darwinup/Table.cpp
===================================================================
--- branches/PR-7489777/darwinup/Table.cpp 2010-01-29 22:09:45 UTC (rev 678)
+++ branches/PR-7489777/darwinup/Table.cpp 2010-01-30 01:06:14 UTC (rev 679)
@@ -30,5 +30,44 @@
* @APPLE_BSD_LICENSE_HEADER_END@
*/
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
#include "Table.h"
+
+Table::Table() {
+ m_column_max = 1;
+ m_column_count = 0;
+ m_columns = (Column**)malloc(sizeof(Column*) * m_column_max);
+ m_name = NULL;
+}
+
+Table::Table(const char* name) {
+ Table();
+ m_name = strdup(name);
+}
+
+Table::~Table() {
+ for (uint32_t i = 0; i < m_column_count; i++) {
+ delete m_columns[i];
+ }
+ free(m_columns);
+ free(m_name);
+}
+
+
+bool Table::add_column(Column* c) {
+ if (m_column_count >= m_column_max) {
+ m_columns = (Column**)realloc(m_columns, m_column_max * sizeof(Column*) * 4);
+ if (!m_columns) {
+ fprintf(stderr, "Error: unable to reallocate memory to add a column\n");
+ return false;
+ }
+ m_column_max *= 4;
+ }
+ m_columns[m_column_count++] = c;
+
+ return true;
+}
+
Modified: branches/PR-7489777/darwinup/Table.h
===================================================================
--- branches/PR-7489777/darwinup/Table.h 2010-01-29 22:09:45 UTC (rev 678)
+++ branches/PR-7489777/darwinup/Table.h 2010-01-30 01:06:14 UTC (rev 679)
@@ -30,32 +30,36 @@
* @APPLE_BSD_LICENSE_HEADER_END@
*/
+#include <stdint.h>
#include <sqlite3.h>
+#include "Column.h"
-struct Column;
-
-struct Table {
+struct Table {
Table();
Table(const char* name);
virtual ~Table();
- const char* name();
+ const char* name();
const Column** columns();
// return SQL statements for this table
- const char* create();
- const char* drop();
- const char* count(const char* where);
- const char* select(const char* where);
- const char* select_column(const char* column, const char* where);
- const char* update(const char* set, const char* where, int &count);
- const char* del(const char* where, int &count);
- const char* insert(const char* columns, const char* values);
+ const char* create();
+ const char* drop();
+ const char* count(const char* where);
+ const char* select(const char* where);
+ const char* select_column(const char* column, const char* where);
+ const char* update(const char* set, const char* where, uint32_t &count);
+ const char* del(const char* where, uint32_t &count);
+ const char* insert(const char* columns, const char* values);
+ bool add_column(Column*);
protected:
- char* m_name;
- Column** m_columns;
+
+ char* m_name;
+ Column** m_columns;
+ uint32_t m_column_count;
+ uint32_t m_column_max;
};
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/darwinbuild-changes/attachments/20100129/9892f930/attachment-0001.html>
More information about the darwinbuild-changes
mailing list