diff options
author | Marc-André Lureau <marcandre.lureau@gmail.com> | 2012-12-10 00:01:22 +0100 |
---|---|---|
committer | Paolo Bonzini <pbonzini@redhat.com> | 2012-12-11 11:23:19 +0100 |
commit | ae4c81e4fca5bb5816b5cbabb17a7dcc22f58a3b (patch) | |
tree | 76012c4f3d176b748961c24f96e87724804f260a /libmsi/libmsi-query.c | |
parent | 0ffc9bc6e55b5e42766e68fb6cc087f6cbead6e0 (diff) | |
download | msitools-ae4c81e4fca5bb5816b5cbabb17a7dcc22f58a3b.tar.gz msitools-ae4c81e4fca5bb5816b5cbabb17a7dcc22f58a3b.tar.xz msitools-ae4c81e4fca5bb5816b5cbabb17a7dcc22f58a3b.zip |
convert to GObject
Diffstat (limited to 'libmsi/libmsi-query.c')
-rw-r--r-- | libmsi/libmsi-query.c | 215 |
1 files changed, 163 insertions, 52 deletions
diff --git a/libmsi/libmsi-query.c b/libmsi/libmsi-query.c index b5a48ad..34512d9 100644 --- a/libmsi/libmsi-query.c +++ b/libmsi/libmsi-query.c @@ -25,6 +25,9 @@ #include "windef.h" #include "winbase.h" #include "winerror.h" + +#include "libmsi-query.h" + #include "debug.h" #include "unicode.h" #include "libmsi.h" @@ -35,20 +38,124 @@ #include "query.h" +enum +{ + PROP_0, + + PROP_DATABASE, + PROP_QUERY, +}; + +G_DEFINE_TYPE (LibmsiQuery, libmsi_query, G_TYPE_OBJECT); + +static void +libmsi_query_init (LibmsiQuery *p) +{ + list_init (&p->mem); +} -static void _libmsi_query_destroy( LibmsiObject *arg ) +static void +libmsi_query_finalize (GObject *object) { - LibmsiQuery *query = (LibmsiQuery*) arg; + LibmsiQuery *self = LIBMSI_QUERY (object); + LibmsiQuery *p = self; struct list *ptr, *t; - if( query->view && query->view->ops->delete ) - query->view->ops->delete( query->view ); - msiobj_release( &query->db->hdr ); + if (p->view && p->view->ops->delete) + p->view->ops->delete (p->view); - LIST_FOR_EACH_SAFE( ptr, t, &query->mem ) - { - msi_free( ptr ); + if (p->database) + g_object_unref (p->database); + + LIST_FOR_EACH_SAFE (ptr, t, &p->mem) { + msi_free (ptr); } + + g_free (p->query); + + G_OBJECT_CLASS (libmsi_query_parent_class)->finalize (object); +} + +static void +libmsi_query_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) +{ + LibmsiQuery *p = LIBMSI_QUERY (object); + g_return_if_fail (LIBMSI_IS_QUERY (object)); + + switch (prop_id) { + case PROP_DATABASE: + g_return_if_fail (p->database == NULL); + p->database = g_value_dup_object (value); + break; + case PROP_QUERY: + g_return_if_fail (p->query == NULL); + p->query = g_value_dup_string (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +libmsi_query_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) +{ + LibmsiQuery *p = LIBMSI_QUERY (object); + g_return_if_fail (LIBMSI_IS_QUERY (object)); + + switch (prop_id) { + case PROP_DATABASE: + g_value_set_object (value, p->database); + break; + case PROP_QUERY: + g_value_set_string (value, p->query); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +libmsi_query_constructed (GObject *object) +{ + G_OBJECT_CLASS (libmsi_query_parent_class)->constructed (object); +} + +static void +libmsi_query_class_init (LibmsiQueryClass *klass) +{ + GObjectClass* object_class = G_OBJECT_CLASS (klass); + + object_class->finalize = libmsi_query_finalize; + object_class->set_property = libmsi_query_set_property; + object_class->get_property = libmsi_query_get_property; + object_class->constructed = libmsi_query_constructed; + + g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DATABASE, + g_param_spec_object ("database", "database", "database", LIBMSI_TYPE_DATABASE, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_QUERY, + g_param_spec_string ("query", "query", "query", NULL, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); +} + +/* TODO: use GInitable */ +static gboolean +init (LibmsiQuery *self, GError **error) +{ + LibmsiQuery *p = self; + WCHAR *szwQuery; + unsigned r; + + szwQuery = strdupAtoW(p->query); + r = _libmsi_parse_sql (p->database, szwQuery, &p->view, &p->mem); + msi_free(szwQuery); + + return r == LIBMSI_RESULT_SUCCESS; } unsigned _libmsi_view_find_column( LibmsiView *table, const WCHAR *name, const WCHAR *table_name, unsigned *n ) @@ -100,44 +207,26 @@ LibmsiResult libmsi_database_open_query(LibmsiDatabase *db, if( !db ) return LIBMSI_RESULT_INVALID_HANDLE; - msiobj_addref( &db->hdr); + g_object_ref(db); r = _libmsi_database_open_query( db, szwQuery, pQuery ); - msiobj_release( &db->hdr ); + g_object_unref(db); msi_free( szwQuery ); return r; } unsigned _libmsi_database_open_query(LibmsiDatabase *db, - const WCHAR *szQuery, LibmsiQuery **pView) + const WCHAR *szwQuery, LibmsiQuery **pView) { - LibmsiQuery *query; - unsigned r; - + char *szQuery; TRACE("%s %p\n", debugstr_w(szQuery), pView); - if( !szQuery) - return LIBMSI_RESULT_INVALID_PARAMETER; + szQuery = strdupWtoA(szwQuery); + *pView = libmsi_query_new (db, szQuery, NULL); + msi_free(szQuery); - /* pre allocate a handle to hold a pointer to the view */ - query = alloc_msiobject( sizeof (LibmsiQuery), _libmsi_query_destroy ); - if( !query ) - return LIBMSI_RESULT_FUNCTION_FAILED; - - msiobj_addref( &db->hdr ); - query->db = db; - list_init( &query->mem ); - - r = _libmsi_parse_sql( db, szQuery, &query->view, &query->mem ); - if( r == LIBMSI_RESULT_SUCCESS ) - { - msiobj_addref( &query->hdr ); - *pView = query; - } - - msiobj_release( &query->hdr ); - return r; + return *pView ? LIBMSI_RESULT_SUCCESS : LIBMSI_RESULT_BAD_QUERY_SYNTAX; } unsigned _libmsi_query_open( LibmsiDatabase *db, LibmsiQuery **view, const WCHAR *fmt, ... ) @@ -186,7 +275,7 @@ unsigned _libmsi_query_iterate_records( LibmsiQuery *view, unsigned *count, break; if (func) r = func( rec, param ); - msiobj_release( &rec->hdr ); + g_object_unref(rec); if( r != LIBMSI_RESULT_SUCCESS ) break; } @@ -233,7 +322,7 @@ LibmsiRecord *_libmsi_query_get_record( LibmsiDatabase *db, const WCHAR *fmt, .. _libmsi_query_execute( view, NULL ); _libmsi_query_fetch( view, &rec ); libmsi_query_close( view ); - msiobj_release( &view->hdr ); + g_object_unref(view); } return rec; } @@ -254,7 +343,7 @@ unsigned msi_view_get_row(LibmsiDatabase *db, LibmsiView *view, unsigned row, Li if (row >= row_count) return LIBMSI_RESULT_NO_MORE_ITEMS; - *rec = libmsi_record_new(col_count); + *rec = libmsi_record_new (col_count); if (!*rec) return LIBMSI_RESULT_FUNCTION_FAILED; @@ -327,7 +416,7 @@ LibmsiResult _libmsi_query_fetch(LibmsiQuery *query, LibmsiRecord **prec) if( !view ) return LIBMSI_RESULT_FUNCTION_FAILED; - r = msi_view_get_row(query->db, view, query->row, prec); + r = msi_view_get_row(query->database, view, query->row, prec); if (r == LIBMSI_RESULT_SUCCESS) query->row ++; @@ -346,9 +435,9 @@ LibmsiResult libmsi_query_fetch(LibmsiQuery *query, LibmsiRecord **record) if( !query ) return LIBMSI_RESULT_INVALID_HANDLE; - msiobj_addref( &query->hdr); + g_object_ref(query); ret = _libmsi_query_fetch( query, record ); - msiobj_release( &query->hdr ); + g_object_unref(query); return ret; } @@ -362,7 +451,7 @@ LibmsiResult libmsi_query_close(LibmsiQuery *query) if( !query ) return LIBMSI_RESULT_INVALID_HANDLE; - msiobj_addref( &query->hdr ); + g_object_ref(query); view = query->view; if( !view ) return LIBMSI_RESULT_FUNCTION_FAILED; @@ -370,7 +459,7 @@ LibmsiResult libmsi_query_close(LibmsiQuery *query) return LIBMSI_RESULT_FUNCTION_FAILED; ret = view->ops->close( view ); - msiobj_release( &query->hdr ); + g_object_unref(query); return ret; } @@ -398,16 +487,16 @@ LibmsiResult libmsi_query_execute(LibmsiQuery *query, LibmsiRecord *rec) if( !query ) return LIBMSI_RESULT_INVALID_HANDLE; - msiobj_addref( &query->hdr ); + g_object_ref(query); if( rec ) - msiobj_addref( &rec->hdr ); + g_object_ref(rec); ret = _libmsi_query_execute( query, rec ); - msiobj_release( &query->hdr ); + g_object_unref(query); if( rec ) - msiobj_release( &rec->hdr ); + g_object_unref(rec); return ret; } @@ -469,7 +558,7 @@ unsigned _libmsi_query_get_column_info( LibmsiQuery *query, LibmsiColInfo info, if( !count ) return LIBMSI_RESULT_INVALID_PARAMETER; - rec = libmsi_record_new( count ); + rec = libmsi_record_new (count); if( !rec ) return LIBMSI_RESULT_FUNCTION_FAILED; @@ -503,9 +592,9 @@ LibmsiResult libmsi_query_get_column_info(LibmsiQuery *query, LibmsiColInfo info if( !query ) return LIBMSI_RESULT_INVALID_HANDLE; - msiobj_addref ( &query->hdr ); + g_object_ref(query); r = _libmsi_query_get_column_info( query, info, prec ); - msiobj_release( &query->hdr ); + g_object_unref(query); return r; } @@ -524,7 +613,7 @@ LibmsiDBError libmsi_query_get_error( LibmsiQuery *query, char *buffer, unsigned if (!query) return LIBMSI_DB_ERROR_INVALIDARG; - msiobj_addref( &query->hdr); + g_object_ref(query); if ((r = query->view->error)) column = query->view->error_column; else column = szEmpty; @@ -536,7 +625,29 @@ LibmsiDBError libmsi_query_get_error( LibmsiQuery *query, char *buffer, unsigned else r = LIBMSI_DB_ERROR_MOREDATA; } - *buflen = len - 1; - msiobj_release( &query->hdr ); + + *buflen = len; + g_object_unref(query); return r; } + +LibmsiQuery* +libmsi_query_new (LibmsiDatabase *database, const char *query, GError **error) +{ + LibmsiQuery *self; + + g_return_val_if_fail (LIBMSI_IS_DATABASE (database), NULL); + g_return_val_if_fail (query != NULL, NULL); + + self = g_object_new (LIBMSI_TYPE_QUERY, + "database", database, + "query", query, + NULL); + + if (!init (self, error)) { + g_object_unref (self); + return NULL; + } + + return self; +} |