[Greater-commits] r399 - trunk/GREAT-ER-DB/tools

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Mon Jul 11 12:22:53 CEST 2011


Author: aheinecke
Date: 2011-07-11 12:22:53 +0200 (Mon, 11 Jul 2011)
New Revision: 399

Added:
   trunk/GREAT-ER-DB/tools/export_blobs.py
Log:
Add export_blobs tool to export all bin_objs from a database with
their file objects


Added: trunk/GREAT-ER-DB/tools/export_blobs.py
===================================================================
--- trunk/GREAT-ER-DB/tools/export_blobs.py	2011-07-11 10:11:49 UTC (rev 398)
+++ trunk/GREAT-ER-DB/tools/export_blobs.py	2011-07-11 10:22:53 UTC (rev 399)
@@ -0,0 +1,75 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# Copyright (C) 2011 by Intevation GmbH
+# Authors:
+# Andre Heinecke <aheinecke at intevation.de>
+#
+# This program is free software under the GPL (>=v2)
+# Read the file COPYING coming with the software for details.
+
+# This tool was used to export the binary objects from
+# a Windows Oracle installation and thus is specific
+# written for that task. If you want to use it you most
+# likely need to edit the code
+
+
+from GreaterDB.interface import da_db_connect
+from GreaterDB.interface import da_db_disconnect
+from GreaterDB.interface import set_api_interface
+from GreaterDB.interface import DA_S_bin_obj
+from GreaterDB.interface import da_get_bin_obj
+
+def pretty_attr(obj):
+    if isinstance(obj, int) or isinstance(obj, long):
+        return "%d" % obj
+    if isinstance(obj, str):
+        return "'%s'" % obj
+    else:
+        return "NULL"
+
+def bin_obj_to_stmt(bin_obj):
+    # Statement
+    output = "Insert into BIN_OBJ_TAB "
+    # Fields
+    fields = ['bin_obj_id',
+        'object_id',
+        'name',
+        'user_id',
+        'privs',
+        'obj_type',
+        'obj_sub_type',
+        'file_type',
+        'file_size',
+        'cre_date',
+        'mod_date',
+       # 'write',
+        'remark',
+        'file_obj']
+    output += "( " + ", ".join(fields) + " )"
+    # Values
+    output += "\n Values \n"
+    output += "("
+    output += ", ".join([pretty_attr(bin_obj.__getattr__(field)) \
+            for field in fields[:-1]])
+    output += ", lo_import('%d.obj'));\n" % bin_obj.bin_obj_id
+    return output
+
+def write_file_obj(bin_obj):
+    fptr = open("%d.obj" % bin_obj.bin_obj_id, "w")
+    fptr.write(bin_obj.file_obj)
+    fptr.close()
+
+def main():
+    set_api_interface("_dagreater_pg")
+    da_db_connect("greater2", "123123", "greater")
+
+    all_objs = da_get_bin_obj(-1, "%", -1, "Y")
+
+    sql_file = open("insert_bin_obj.sql", "w")
+    for bin_obj in all_objs:
+        sql_file.write(bin_obj_to_stmt(bin_obj))
+        write_file_obj(bin_obj)
+
+
+if __name__ == '__main__':
+    main()



More information about the Greater-commits mailing list