Beispiel #1
0
 private static void setResult(Sqlite3.sqlite3_context ctx, object result)
 {
     if (result == null)
     {
         Sqlite3.sqlite3_result_null(ctx);
     }
     else if (result is bool)
     {
         Sqlite3.sqlite3_result_int64(ctx, ((bool)result) ? 1 : 0);
     }
     else if (result is int)
     {
         Sqlite3.sqlite3_result_int64(ctx, (int)result);
     }
     else if (result is long)
     {
         Sqlite3.sqlite3_result_int64(ctx, (long)result);
     }
     else if (result is System.Numerics.BigInteger)
     {
         Sqlite3.sqlite3_result_int64(ctx, (long)((System.Numerics.BigInteger)result));
     }
     else if (result is float)
     {
         Sqlite3.sqlite3_result_double(ctx, (float)result);
     }
     else if (result is double)
     {
         Sqlite3.sqlite3_result_double(ctx, (double)result);
     }
     else if (result is string)
     {
         Sqlite3.sqlite3_result_text(ctx, (string)result, -1, Sqlite3.SQLITE_TRANSIENT);
     }
     else if (result is byte[])
     {
         byte[] b = (byte[])result;
         string s = Latin1.GetString(b, 0, b.Length);
         Sqlite3.sqlite3_result_blob(ctx, s, s.Length, Sqlite3.SQLITE_TRANSIENT);
     }
     else if (result is PythonBuffer)
     {
         PythonBuffer buffer = (PythonBuffer)result;
         string       s      = buffer.__getslice__(0, null).ToString();
         Sqlite3.sqlite3_result_blob(ctx, s, s.Length, Sqlite3.SQLITE_TRANSIENT);
     }
     else
     {
         // TODO raise error
     }
 }
Beispiel #2
0
            private object fetchOneRow(CodeContext context)
            {
                int numcols = Sqlite3.sqlite3_data_count(this.statement.st);

                object[] row       = new object[numcols];
                object   converter = null;

                for (int i = 0; i < numcols; ++i)
                {
                    object converted = null;

                    if (this.connection.detect_types != 0)
                    {
                        converter = row_cast_map[i];
                    }
                    else
                    {
                        converter = null;
                    }

                    if (converter != null)
                    {
                        byte[] val = Sqlite3.sqlite3_column_blob(this.statement.st, i);
                        if (val == null)
                        {
                            converted = null;
                        }
                        else
                        {
                            string item = Latin1.GetString(val, 0, val.Length);
                            converted = PythonCalls.Call(context, converter, item);
                        }
                    }
                    else
                    {
                        int coltype = Sqlite3.sqlite3_column_type(this.statement.st, i);

                        switch (coltype)
                        {
                        case Sqlite3.SQLITE_NULL:
                            converted = null;
                            break;

                        case Sqlite3.SQLITE_INTEGER:
                            long l = Sqlite3.sqlite3_column_int64(this.statement.st, i);
                            if (l < int.MinValue || l > int.MaxValue)
                            {
                                converted = l;
                            }
                            else
                            {
                                converted = (int)l;
                            }

                            break;

                        case Sqlite3.SQLITE_FLOAT:
                            converted = Sqlite3.sqlite3_column_double(this.statement.st, i);
                            break;

                        case Sqlite3.SQLITE_TEXT:
                            converted = Sqlite3.sqlite3_column_text(this.statement.st, i);
                            break;

                        case Sqlite3.SQLITE_BLOB:
                        default:
                            converted = new Bytes(Sqlite3.sqlite3_column_blob(this.statement.st, i) ?? new byte[0]);     // TODO: avoid creating a copy
                            break;
                        }
                    }

                    row[i] = converted;
                }

                return(new PythonTuple(row));
            }