Ejemplo n.º 1
0
/* ************************************************************************** */
bool MDBMigrate::drv_readTableSchema(const QString& originalName,
                                     KexiDB::TableSchema& tableSchema)
{
    // Get the column meta-data
    MdbTableDef *tableDef = getTableDef(originalName);
    if (!tableDef) {
        kDebug() << "MDBMigrate::drv_getTableDef: couldn't find table "
        << originalName;
        return false;
    }
    mdb_read_columns(tableDef);
    kDebug() << "MDBMigrate::drv_readTableSchema: #cols = "
    << tableDef->num_cols;

    /*! Convert column data to Kexi TableSchema
        Nice mix of terminology here, MDBTools has columns, Kexi has fields. */
    MdbColumn *col;
    for (unsigned int i = 0; i < tableDef->num_cols; i++) {
        col = (MdbColumn*) g_ptr_array_index(tableDef->columns, i);

        // Field name
        QString fldName = QString::fromUtf8(col->name);
        kDebug() << "MDBMigrate::drv_readTableSchema: got column "
        << fldName << "\"" << col->name;

        QString fldID(KexiUtils::string2Identifier(fldName));

        // Field type
        KexiDB::Field *fld =
            new KexiDB::Field(fldID, type(col->col_type));

        kDebug() << "MDBMigrate::drv_readTableSchema: size "
        << col->col_size << " type " << type(col->col_type);
        fld->setCaption(fldName);
        tableSchema.addField(fld);
    }

    getPrimaryKey(&tableSchema, tableDef);

    // Free the column meta-data - as soon as it doesn't seg fault.
    //mdb_free_tabledef(tableDef);

    return true;
}
Ejemplo n.º 2
0
/*! Get the types and properties for each column. */
bool MySQLMigrate::drv_readTableSchema(
	const QString& originalName, KexiDB::TableSchema& tableSchema)
{
//	m_table = new KexiDB::TableSchema(table);

//	//TODO IDEA: ask for user input for captions
//	tableSchema.setCaption(table + " table");

	//Perform a query on the table to get some data
	QString query = QString("SELECT * FROM `") + drv_escapeIdentifier(originalName) + "` LIMIT 0";
	if(d->executeSQL(query)) {
		MYSQL_RES *res = mysql_store_result(d->mysql);
		if (res != NULL) {
		
			unsigned int numFlds = mysql_num_fields(res);
			MYSQL_FIELD *fields = mysql_fetch_fields(res); 
			
			for(unsigned int i = 0; i < numFlds; i++) {
				QString fldName(fields[i].name);
				QString fldID( KexiUtils::string2Identifier(fldName) );

				KexiDB::Field *fld = 
				  new KexiDB::Field(fldID, type(originalName, &fields[i]));
				
				if(fld->type() == KexiDB::Field::Enum) {
					QStringList values = examineEnumField(originalName, &fields[i]);
				}
				
				fld->setCaption(fldName);
				getConstraints(fields[i].flags, fld);
				getOptions(fields[i].flags, fld);
				tableSchema.addField(fld);
			}
			mysql_free_result(res);
		} else {
			kdDebug() << "MySQLMigrate::drv_tableNames: null result" << endl;
		}
		return true;
	} else {
	  return false;
	}
}
Ejemplo n.º 3
0
bool OdbMigrate::drv_readTableSchema(
    const QString& originalName, KexiDB::TableSchema& tableSchema)
{
    char* tableName=originalName.toAscii().data();
    jmethodID getTableNames = env->GetMethodID(clsH,"getTableSchema","(Ljava/lang/String;)Ljava/lang/String;");
    jstring returnString = (jstring) env->CallObjectMethod(java_class_object,getTableNames,tableName);
    const char* tablesstring = env->GetStringUTFChars(returnString, NULL);
    QString jsonString(tablesstring);
    QStringList list = jsonString.split(",");

    for(int i=0;i<list.size();i+=2)
    {
        QString fldID(KexiUtils::stringToIdentifier(list.at(i+1)));
        KexiDB::Field *fld =
            new KexiDB::Field(fldID, type(list.at(i+1)));
        fld->setCaption(list.at(i));
        tableSchema.addField(fld);
    }

    return false;
}
Ejemplo n.º 4
0
//==================================================================================
//This is probably going to be quite complex...need to get the types for all columns
//any any other attributes required by kexi
//helped by reading the 'tables' test program
bool PqxxMigrate::drv_readTableSchema(
    const QString& originalName, KexiDB::TableSchema& tableSchema)
{
//    m_table = new KexiDB::TableSchema(table);

    //TODO IDEA: ask for user input for captions
//moved    m_table->setCaption(table + " table");

    //Perform a query on the table to get some data
    kDebug();
    tableSchema.setName(originalName);
    if (!query("select * from " + drv_escapeIdentifier(originalName) + " limit 1"))
        return false;
    //Loop round the fields
    for (uint i = 0; i < (uint)m_res->columns(); i++) {
        QString fldName(m_res->column_name(i));
        KexiDB::Field::Type fldType = type(m_res->column_type(i), fldName);
        QString fldID(KexiUtils::string2Identifier(fldName));
        const pqxx::oid toid = tableOid(originalName);
        if (toid == 0)
            return false;
        KexiDB::Field *f = new KexiDB::Field(fldID, fldType);
        f->setCaption(fldName);
        f->setPrimaryKey(primaryKey(toid, i));
        f->setUniqueKey(uniqueKey(toid, i));
        f->setAutoIncrement(autoInc(toid, i));//This should be safe for all field types
        tableSchema.addField(f);

        // Do this for var/char types
        //m_f->setLength(m_res->at(0)[i].size());

        // Do this for numeric type
        /*m_f->setScale(0);
        m_f->setPrecision(0);*/

        kDebug() << "Added field [" << f->name() << "] type [" << f->typeName()
        << ']';
    }
    return true;
}