Пример #1
0
    def write_row(self, table, col_specs, row, srid=None):
        if not srid: srid=self.srid
        col_specs = colspecs.fix(col_specs)

        def code_value(col_name, col_type, value, ref):
            if col_type == 'POINT':
                return (self.code_point(value,srid),None)
            if col_type == 'LINESTRING':
                return (self.code_linestring(value,srid),None)
            if col_type == 'TEXT':
                if not isinstance(value,basestring): value = str(value)
                return (ref, value)
            return (ref, value)

        # create row dictionary
        if isinstance(row,dict):
            rowd = row.copy()
        else:
            # is this even used?
            assert len(row) == len(col_specs)
            rowd = {}
            for i in range(len(col_specs)): rowd[col_specs[i][2]] = row[i]

        # col and value list strings
        columns = []
        values = []
        parms = []
        for col_name,col_type,prop_name in col_specs:
            if not prop_name in rowd.keys(): continue
            value = rowd[prop_name]
            if value == None: continue
            columns.append( '"' + col_name + '"')
            (value,parm) = code_value(col_name, col_type, value, self._interpolation_ref)
            values.append(value)
            if parm!=None: parms.append(parm)
            
        columns = ','.join(columns)
        values = ','.join(values)

        # create sql template for row
        sql_template  = ''' 
                    INSERT INTO %(table)s 
		        (%(columns)s)
                    VALUES
		        (%(values)s);
                    ''' % {
                        'table':table,
                        'columns':columns,
                        'values': values
                        }

        # and execute sql
        #print 'sql_template:', sql_template
        self.exec_sql(sql_template,parms)
Пример #2
0
    def create_table(self, table, col_specs):
        col_specs = colspecs.fix(col_specs)
        self.exec_sql('DROP TABLE IF EXISTS %s' % table)

        data_specs = []  
        geom_specs = []
        for col,typ,prop in col_specs:
            if typ=='LINESTRING' or typ=='POINT':
                geom_specs.append((col,typ,prop))
            else:
                data_specs.append((col,typ,prop))
        spec_str = [ '"%s" %s' % spec[:2] for spec in data_specs ]
        spec_str = ', '.join(spec_str)

        self.exec_sql('CREATE TABLE %s (%s)' % (table, spec_str))

        # add geometry column(s)
        for col_name, col_type, parm_name in geom_specs:
            self.add_geometry_column(table,col_name,col_type)