コード例 #1
0
ファイル: json2db.py プロジェクト: klahnakoski/json2sql
    def __exit__(self, type, value, traceback):
        if isinstance(value, BaseException):
            self.db.rollback()
            self.db.close()
            return

        try:
            self.db.commit()
        except Exception, e:
            Log.warning(u"can not commit()", e)
コード例 #2
0
ファイル: json2db.py プロジェクト: klahnakoski/json2sql
    def enhance_schema(self, json, path, schema):
        #LOOK FOR NEW PRIMITIVE ATTRIBUTES
        #PRIMITIVE TO MULTIVALUED
        #OBJECT TO NESTED

        for k, v in json.items():
            full_path = ".".join([path, k.replace(".", "\.")])

            if not schema[k]:
                #MAKE NEW PROPERTY
                column_def = self.defaults.getSchema(full_path, example=v)  # PATTERN MATCHING SCHEMA GENERATOR
                # ALTER TABLE TO HANDLE NEW SCHEMA

                desc=[]  #ADDED COLUMN DEFINITIONS
                self.add_columns(path, desc, [column_def])
                self.build_schema(column_def, full_path)
                self.db.execute("ALTER TABLE {{table_name}} ({{details}}", {
                    "table_name": self.db.quote_column(path),
                    "details": "\n".join(desc)
                })
            elif isinstance(v, list):
                if schema[k][MULTI]:
                    self.enhance_schema(full_path, v, schema[k])
                elif schema[k].type == NESTED:
                    self.enhance_schema(full_path, v, schema[k])
                else:
                    #MOVE FROM PRIMITIVE TO MULTI
                    #MOVE FROM OBJECT TO NESTED
                    pass
            elif isinstance(v, dict):
                if schema[k].type in PRIMITIVES:
                    Log.error("type={{type}} can not be expanded to {{expected}}", {
                        "type": schema[k],
                        "expected": OBJECT
                    })
            else:
                Log.error("WTF!")
コード例 #3
0
ファイル: json2db.py プロジェクト: klahnakoski/json2sql
    def _add_multi(self, json, path, schema, parent_id):
        if schema.type not in PRIMITIVES:
            Log.error("Expecting primitive type")

        for i, j in enumerate(json):
            record = Struct()

            if schema.id_field:
                record["_id"] = json[schema.id_field]

            record["_parent"] = parent_id

            if schema.multi == ORDERED:
                record["_order"] = i

            if schema.type in PRIMITIVES:
                record["_value"] = j
                continue

            # OBJECT FIELDS
            self._fill_record(record, json, None, schema)
            self.db.insert(path, record)

            self._add_children(json, path, schema, schema.id_field)