Exemplo n.º 1
0
        /// <summary>
        /// Retrieve a record based on table name and rowid
        /// </summary>
        /// <param name="tableName">Name of the table.</param>
        /// <param name="rowId">The row identifier.</param>
        /// <returns>a list of TableFields one for each field the first contains
        /// the rowid for the record</returns>
        public TableField[] getRecord(string tableName, long rowId)
        {
            string sql = "Select rowid As rowid, * From '" + tableName + "' Where rowid = " + rowId;

            // retrieves field types, pk, ... from database
            FieldDescr[] tabledef = getTableStructureDef(tableName);
            TableField[] tfs;
            int          fields;

            using (SqliteDataReader cursor = ExecuteReader(sql))
            {
                fields = cursor.FieldCount;      //cursor.getColumnCount();
                tfs    = new TableField[fields]; //cursor.getColumnCount()
                cursor.Read();
                for (int j = 0; j < fields; j++)
                {
                    TableField tf = new TableField();
                    tf.setName(cursor.GetName(j));        //getColumnName
                    tf.setDisplayName(cursor.GetName(j)); //getColumnName
                    // The extra field rowid
                    if (j == 0)
                    {
                        // Don't allow updating of rowid
                        tf.setUpdateable(false);
                        tf.setType(TableField.TYPE_INTEGER);
                    }
                    else
                    {
                        tf.setUpdateable(true);
                        tf.setType(tabledef[j - 1].getType());
                        tf.setNotNull(tabledef[j - 1].isNotNull());
                        tf.setPrimaryKey(tabledef[j - 1].isPk());
                        tf.setDefaultValue(tabledef[j - 1].getDefaultValue());
                        //TODO need to retrieve the foreign key
                    }
                    //TODO Implement BLOB edit
                    //is it a BLOB field turn edit off
                    try
                    {
                        //tf.setValue(cursor.GetString(j));
                        tf.setValue(cursor.GetValue(j).ToString());
                    }
                    catch (Exception)
                    {
                        tf.setUpdateable(false);
                        tf.setValue("BLOB");
                    }
                    tfs[j] = tf;
                }
            }                   // Get foreign keys
            sql = "PRAGMA foreign_key_list([" + tableName + "])";
            using (SqliteDataReader cursor = ExecuteReader(sql))
                while (cursor.Read())
                {
                    //Go through all fields to see if the fields has FK
                    for (int i = 0; i < fields; i++)
                    {
                        if (tfs[i].getName().Equals(cursor.GetString(3)))//fkName
                        {
                            tfs[i].setForeignKey("Select [" + cursor.GetString(4) + "] From [" + cursor.GetString(2) + "]");
                            break;
                        }
                    }
                }
            return(tfs);
        }